33 #include <boost/uuid/uuid_io.hpp>
34 #include <boost/functional/hash.hpp>
36 #include "hazelcast/client/cluster.h"
37 #include "hazelcast/client/spi/impl/ClientClusterServiceImpl.h"
38 #include "hazelcast/client/membership_listener.h"
39 #include "hazelcast/client/initial_membership_event.h"
40 #include "hazelcast/client/member.h"
41 #include "hazelcast/client/serialization/serialization.h"
42 #include "hazelcast/client/membership_event.h"
43 #include "hazelcast/client/impl/vector_clock.h"
44 #include "hazelcast/client/member_selectors.h"
45 #include "hazelcast/client/internal/partition/strategy/StringPartitioningStrategy.h"
50 : cluster_service_(cluster_service) {
54 return cluster_service_.get_member_list();
58 return cluster_service_.add_membership_listener(std::move(listener));
62 return cluster_service_.remove_membership_listener(registration_id);
65 member::member() : lite_member_(false) {
68 member::member(address address, boost::uuids::uuid uuid,
bool lite, std::unordered_map<std::string, std::string> attr) :
69 address_(address), uuid_(uuid), lite_member_(lite), attributes_(attr) {
72 member::member(address member_address) : address_(member_address), lite_member_(false) {
75 member::member(boost::uuids::uuid uuid) : uuid_(uuid), lite_member_(false) {
79 return uuid_ == rhs.uuid_;
94 const std::unordered_map<std::string, std::string> &member::get_attributes()
const {
98 std::ostream &operator<<(std::ostream &out,
const member &member) {
99 const address &address = member.get_address();
101 out << address.get_host();
104 out << address.get_port();
105 out <<
" - " << boost::uuids::to_string(member.get_uuid());
110 std::unordered_map<std::string, std::string>::const_iterator it = attributes_.find(key);
111 if (attributes_.end() != it) {
112 return &(it->second);
119 return attributes_.find(key) != attributes_.end();
122 bool member::operator<(
const member &rhs)
const {
123 return uuid_ < rhs.uuid_;
126 endpoint::endpoint(boost::uuids::uuid uuid, boost::optional<address> socket_address)
127 : uuid_(uuid), socket_address_(std::move(socket_address)) {}
134 return socket_address_;
138 const std::unordered_map<boost::uuids::uuid,
member, boost::hash<boost::uuids::uuid>> &members_list) :
139 cluster_(
cluster), member_(m), event_type_(event_type), members_(members_list) {
160 local_endpoint::local_endpoint(boost::uuids::uuid uuid, boost::optional<address> socket_address, std::string name,
161 std::unordered_set<std::string> labels) :
endpoint(uuid, std::move(socket_address)), name_(std::move(name)),
162 labels_(std::move(labels)) {}
164 const std::string &local_endpoint::get_name()
const {
169 vector_clock::vector_clock() =
default;
171 vector_clock::vector_clock(
const vector_clock::timestamp_vector &replica_logical_timestamps)
172 : replica_timestamp_entries_(replica_logical_timestamps) {
173 for (
const vector_clock::timestamp_vector::value_type &replicaTimestamp : replica_logical_timestamps) {
174 replica_timestamps_[replicaTimestamp.first] = replicaTimestamp.second;
178 vector_clock::timestamp_vector vector_clock::entry_set() {
179 return replica_timestamp_entries_;
182 bool vector_clock::is_after(vector_clock &other) {
183 bool anyTimestampGreater =
false;
184 for (
const vector_clock::timestamp_map::value_type &otherEntry : other.replica_timestamps_) {
185 const auto &replicaId = otherEntry.first;
186 int64_t otherReplicaTimestamp = otherEntry.second;
187 std::pair<bool, int64_t> localReplicaTimestamp = get_timestamp_for_replica(replicaId);
189 if (!localReplicaTimestamp.first ||
190 localReplicaTimestamp.second < otherReplicaTimestamp) {
192 }
else if (localReplicaTimestamp.second > otherReplicaTimestamp) {
193 anyTimestampGreater =
true;
197 return anyTimestampGreater || other.replica_timestamps_.size() < replica_timestamps_.size();
200 std::pair<bool, int64_t> vector_clock::get_timestamp_for_replica(boost::uuids::uuid replica_id) {
201 if (replica_timestamps_.count(replica_id) == 0) {
202 return std::make_pair(
false, -1);
204 return std::make_pair(
true, replica_timestamps_[replica_id]);
208 bool member_selectors::data_member_selector::select(
const member &member)
const {
209 return !member.is_lite_member();
212 const std::unique_ptr<member_selector> member_selectors::DATA_MEMBER_SELECTOR(
213 new member_selectors::data_member_selector());
216 namespace partition {
218 std::string StringPartitioningStrategy::get_base_name(
const std::string &name) {
219 size_t index_of = name.find(
'@');
220 if (index_of == std::string::npos) {
223 return name.substr(0, index_of);
226 std::string StringPartitioningStrategy::get_partition_key(
const std::string &key) {
227 size_t firstIndexOf = key.find(
'@');
228 if (firstIndexOf == std::string::npos) {
231 return key.substr(firstIndexOf + 1);
242 return boost::hash<boost::uuids::uuid>()(k.get_uuid());
Represents an address of a client or member in the cluster.
Hazelcast cluster interface.
cluster(spi::impl::ClientClusterServiceImpl &cluster_service)
Constructor.
std::vector< member > get_members()
Set of current members of the cluster.
bool remove_membership_listener(boost::uuids::uuid registration_id)
Removes the specified membership_listener.
boost::uuids::uuid add_membership_listener(membership_listener &&listener)
Adds membership_listener to listen for membership updates.
Endpoint represents a peer in the cluster.
boost::uuids::uuid get_uuid() const
Returns the UUID of this endpoint.
const boost::optional< address > & get_socket_address() const
Returns the socket address for this endpoint.
bool is_lite_member() const
Lite member is does not hold data.
boost::uuids::uuid get_uuid() const
Returns UUID of this member.
bool lookup_attribute(const std::string &key) const
check if an attribute is defined for given key.
bool operator==(const member &) const
comparison operation
const std::string * get_attribute(const std::string &key) const
Returns the value of the specified key for this member or default constructed value if value is undef...
const address & get_address() const
Returns the socket address of this member.
membership_event(cluster &cluster, const member &m, membership_event_type event_type, const std::unordered_map< boost::uuids::uuid, member, boost::hash< boost::uuids::uuid >> &members_list)
Internal API.
std::unordered_map< boost::uuids::uuid, member, boost::hash< boost::uuids::uuid > > get_members() const
Returns a consistent view of the the members exactly after this MembershipEvent has been processed.
cluster & get_cluster()
Returns the cluster of the event.
virtual ~membership_event()
Destructor.
membership_event_type get_event_type() const
Returns the membership event type; MembershipEvent::MEMBER_JOINED , MembershipEvent::MEMBER_LEFT.
const member & get_member() const
Returns the removed or added member.
Cluster membership listener.