18 #include <boost/uuid/uuid_io.hpp>
19 #include <boost/functional/hash.hpp>
21 #include "hazelcast/client/cluster.h"
22 #include "hazelcast/client/spi/impl/ClientClusterServiceImpl.h"
23 #include "hazelcast/client/membership_listener.h"
24 #include "hazelcast/client/initial_membership_event.h"
25 #include "hazelcast/client/member.h"
26 #include "hazelcast/client/serialization/serialization.h"
27 #include "hazelcast/client/membership_event.h"
28 #include "hazelcast/client/impl/vector_clock.h"
29 #include "hazelcast/client/member_selectors.h"
30 #include "hazelcast/client/internal/partition/strategy/StringPartitioningStrategy.h"
35 : cluster_service_(cluster_service) {
39 return cluster_service_.get_member_list();
43 return cluster_service_.add_membership_listener(std::move(listener));
47 return cluster_service_.remove_membership_listener(registration_id);
50 member::member() : lite_member_(false) {
53 member::member(address member_address, boost::uuids::uuid uuid,
bool lite,
54 std::unordered_map<std::string, std::string> attr,
55 std::unordered_map<endpoint_qualifier, address> address_map) :
56 address_(std::move(member_address)), uuid_(uuid), lite_member_(lite),
57 attributes_(std::move(attr)), address_map_(std::move(address_map)) {
60 member::member(address member_address) : address_(member_address), lite_member_(false) {
63 member::member(boost::uuids::uuid uuid) : uuid_(uuid), lite_member_(false) {
78 const std::unordered_map<std::string, std::string> &member::get_attributes()
const {
82 std::ostream &operator<<(std::ostream &out,
const member &member) {
83 const address &address = member.get_address();
85 out << address.get_host();
88 out << address.get_port();
89 out <<
" - " << boost::uuids::to_string(member.get_uuid());
90 if (member.is_lite_member()) {
97 std::unordered_map<std::string, std::string>::const_iterator it = attributes_.find(key);
98 if (attributes_.end() != it) {
106 return attributes_.find(key) != attributes_.end();
109 bool member::operator<(
const member &rhs)
const {
110 return uuid_ < rhs.uuid_;
113 const std::unordered_map<endpoint_qualifier, address> &member::address_map()
const {
117 bool operator==(
const member &lhs,
const member &rhs) {
118 return lhs.address_ == rhs.address_ &&
119 lhs.uuid_ == rhs.uuid_;
122 endpoint::endpoint(boost::uuids::uuid uuid, boost::optional<address> socket_address)
123 : uuid_(uuid), socket_address_(std::move(socket_address)) {}
130 return socket_address_;
134 const std::unordered_map<boost::uuids::uuid,
member, boost::hash<boost::uuids::uuid>> &members_list) :
135 cluster_(
cluster), member_(m), event_type_(event_type), members_(members_list) {
156 local_endpoint::local_endpoint(boost::uuids::uuid uuid, boost::optional<address> socket_address, std::string name,
157 std::unordered_set<std::string> labels) :
endpoint(uuid, std::move(socket_address)), name_(std::move(name)),
158 labels_(std::move(labels)) {}
160 const std::string &local_endpoint::get_name()
const {
165 vector_clock::vector_clock() =
default;
167 vector_clock::vector_clock(
const vector_clock::timestamp_vector &replica_logical_timestamps)
168 : replica_timestamp_entries_(replica_logical_timestamps) {
169 for (
const vector_clock::timestamp_vector::value_type &replicaTimestamp : replica_logical_timestamps) {
170 replica_timestamps_[replicaTimestamp.first] = replicaTimestamp.second;
174 vector_clock::timestamp_vector vector_clock::entry_set() {
175 return replica_timestamp_entries_;
178 bool vector_clock::is_after(vector_clock &other) {
179 bool anyTimestampGreater =
false;
180 for (
const vector_clock::timestamp_map::value_type &otherEntry : other.replica_timestamps_) {
181 const auto &replicaId = otherEntry.first;
182 int64_t otherReplicaTimestamp = otherEntry.second;
183 std::pair<bool, int64_t> localReplicaTimestamp = get_timestamp_for_replica(replicaId);
185 if (!localReplicaTimestamp.first ||
186 localReplicaTimestamp.second < otherReplicaTimestamp) {
188 }
else if (localReplicaTimestamp.second > otherReplicaTimestamp) {
189 anyTimestampGreater =
true;
193 return anyTimestampGreater || other.replica_timestamps_.size() < replica_timestamps_.size();
196 std::pair<bool, int64_t> vector_clock::get_timestamp_for_replica(boost::uuids::uuid replica_id) {
197 if (replica_timestamps_.count(replica_id) == 0) {
198 return std::make_pair(
false, -1);
200 return std::make_pair(
true, replica_timestamps_[replica_id]);
204 bool member_selectors::data_member_selector::select(
const member &member)
const {
205 return !member.is_lite_member();
208 const std::unique_ptr<member_selector> member_selectors::DATA_MEMBER_SELECTOR(
209 new member_selectors::data_member_selector());
212 namespace partition {
214 std::string StringPartitioningStrategy::get_base_name(
const std::string &name) {
215 size_t index_of = name.find(
'@');
216 if (index_of == std::string::npos) {
219 return name.substr(0, index_of);
222 std::string StringPartitioningStrategy::get_partition_key(
const std::string &key) {
223 size_t firstIndexOf = key.find(
'@');
224 if (firstIndexOf == std::string::npos) {
227 return key.substr(firstIndexOf + 1);
234 bool operator==(
const endpoint_qualifier &lhs,
const endpoint_qualifier &rhs) {
235 return lhs.type == rhs.type &&
236 lhs.identifier == rhs.identifier;
243 std::size_t seed = 0;
245 boost::hash_combine(seed, m.get_uuid());
249 std::size_t hash<hazelcast::client::endpoint_qualifier>::operator()(
251 std::size_t seed = 0;
252 boost::hash_combine(seed, e.type);
253 boost::hash_combine(seed, e.identifier);
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.
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.