locator.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "defs.h"
  3. #include <util/generic/hash.h>
  4. #include <util/generic/map.h>
  5. #include <util/generic/set.h>
  6. #include <util/generic/string.h>
  7. #include <util/network/interface.h>
  8. #include <util/system/mutex.h>
  9. namespace NBus {
  10. ///////////////////////////////////////////////
  11. /// \brief Client interface to locator service
  12. /// This interface abstracts clustering/location service that
  13. /// allows clients find servers (address, port) using "name" and "key".
  14. /// The instance lives in TBusMessageQueue-object, but can be shared by different queues.
  15. class TBusLocator: public TAtomicRefCount<TBusLocator>, public TNonCopyable {
  16. private:
  17. typedef ui64 TServiceId;
  18. typedef TSet<TString> TServiceIdSet;
  19. TServiceIdSet ServiceIdSet;
  20. TServiceId GetServiceId(const char* name);
  21. typedef TMap<TNetAddr, TString> THostAddrMap;
  22. THostAddrMap HostAddrMap;
  23. TNetworkInterfaceList MyInterfaces;
  24. struct TItem {
  25. TServiceId ServiceId;
  26. TBusKey Start;
  27. TBusKey End;
  28. TNetAddr Addr;
  29. bool operator<(const TItem& y) const;
  30. bool operator==(const TItem& y) const;
  31. TItem(TServiceId serviceId, TBusKey start, TBusKey end, const TNetAddr& addr);
  32. };
  33. typedef TMultiSet<TItem> TItems;
  34. TItems Items;
  35. TMutex Lock;
  36. int RegisterBreak(TServiceId serviceId, const TBusKey start, const TNetAddr& addr);
  37. int UnregisterBreak(TServiceId serviceId, const TNetAddr& addr);
  38. void NormalizeBreaks(TServiceId serviceId);
  39. private:
  40. int Register(TBusService service, TBusKey start, TBusKey end, const TNetAddr& addr);
  41. public:
  42. /// creates instance that obtains location table from locator server (not implemented)
  43. TBusLocator();
  44. /// returns true if this address is on the same node for YBUS_KEYLOCAL
  45. bool IsLocal(const TNetAddr& addr);
  46. /// returns first address for service and key
  47. int Locate(TBusService service, TBusKey key, TNetAddr* addr);
  48. /// returns all addresses mathing service and key
  49. int LocateAll(TBusService service, TBusKey key, TVector<TNetAddr>& addrs);
  50. /// returns actual host name for service and key
  51. int LocateHost(TBusService service, TBusKey key, TString* host, int* port, bool* isLocal = nullptr);
  52. /// returns all key ranges for the given service
  53. int LocateKeys(TBusService service, TBusKeyVec& keys, bool onlyLocal = false);
  54. /// returns port on the local node for the service
  55. int GetLocalPort(TBusService service);
  56. /// returns addresses of the local node for the service
  57. int GetLocalAddresses(TBusService service, TVector<TNetAddr>& addrs);
  58. /// register service instance
  59. int Register(TBusService service, TBusKey start, TBusKey end, const TNetworkAddress& addr, EIpVersion requireVersion = EIP_VERSION_4, EIpVersion preferVersion = EIP_VERSION_ANY);
  60. /// @throws yexception
  61. int Register(TBusService service, const char* host, int port, TBusKey start = YBUS_KEYMIN, TBusKey end = YBUS_KEYMAX, EIpVersion requireVersion = EIP_VERSION_4, EIpVersion preferVersion = EIP_VERSION_ANY);
  62. /// unregister service instance
  63. int Unregister(TBusService service, TBusKey start, TBusKey end);
  64. int RegisterBreak(TBusService service, const TVector<TBusKey>& starts, const TNetAddr& addr);
  65. int UnregisterBreak(TBusService service, const TNetAddr& addr);
  66. };
  67. }