Hazelcast C++ Client
Hazelcast C++ Client Library
All Classes Functions Variables Enumerations Enumerator Pages
config.cpp
1 /*
2  * Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
19  *
20  * Licensed under the Apache License, Version 2.0 (the "License");
21  * you may not use this file except in compliance with the License.
22  * You may obtain a copy of the License at
23  *
24  * http://www.apache.org/licenses/LICENSE-2.0
25  *
26  * Unless required by applicable law or agreed to in writing, software
27  * distributed under the License is distributed on an "AS IS" BASIS,
28  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  * See the License for the specific language governing permissions and
30  * limitations under the License.
31  */
32 #include <atomic>
33 
34 #include "hazelcast/client/client_config.h"
35 #include "hazelcast/client/serialization_config.h"
36 #include "hazelcast/client/config/ssl_config.h"
37 #include "hazelcast/util/Preconditions.h"
38 #include "hazelcast/client/config/client_flake_id_generator_config.h"
39 #include "hazelcast/client/exception/protocol_exceptions.h"
40 #include "hazelcast/client/internal/partition/strategy/StringPartitioningStrategy.h"
41 #include "hazelcast/client/address.h"
42 #include "hazelcast/client/config/client_network_config.h"
43 #include "hazelcast/client/config/client_aws_config.h"
44 #include "hazelcast/client/config/reliable_topic_config.h"
45 #include "hazelcast/client/config/client_connection_strategy_config.h"
46 #include "hazelcast/client/config/logger_config.h"
47 #include "hazelcast/client/config/index_config.h"
48 #include "hazelcast/client/config/matcher/matching_point_config_pattern_matcher.h"
49 #include "hazelcast/client/query/predicates.h"
50 #include "hazelcast/client/lifecycle_listener.h"
51 #include "hazelcast/client/config/eviction_strategy_type.h"
52 #include "hazelcast/client/cluster.h"
53 #include "hazelcast/client/initial_membership_event.h"
54 
55 namespace hazelcast {
56  namespace client {
58  }
59 
61  return version_;
62  }
63 
65  this->version_ = v;
66  return *this;
67  }
68 
69  std::shared_ptr<serialization::global_serializer> serialization_config::get_global_serializer() const {
70  return global_serializer_;
71  }
72 
74  const std::shared_ptr<serialization::global_serializer> &global_serializer) {
75  global_serializer_ = global_serializer;
76  return *this;
77  }
78 
79  serialization_config &serialization_config::set_byte_order(boost::endian::order byte_order) {
80  byte_order_ = byte_order;
81  return *this;
82  }
83 
84  boost::endian::order serialization_config::get_byte_order() const {
85  return byte_order_;
86  }
87 
88  namespace config {
89  ssl_config::ssl_config()
90 #ifdef HZ_BUILD_WITH_SSL
91  : enabled_(false), ssl_protocol_(tlsv12)
92 #endif
93  {}
94 
95 #ifdef HZ_BUILD_WITH_SSL
96  ssl_config &ssl_config::set_context(boost::asio::ssl::context ctx) {
97  util::Preconditions::check_ssl("ssl_config::set_context");
98  if (enabled_) {
99  throw exception::illegal_argument(
100  "You should either use the deprecated methods or this method to enable ssl. You already used the deprecated way.");
101  }
102  ssl_context_ = std::make_shared<boost::asio::ssl::context>(std::move(ctx));
103  return *this;
104  }
105 
106  bool ssl_config::is_enabled() const {
107  return ssl_context_ || enabled_;
108  }
109 
110  ssl_config &ssl_config::set_enabled(bool is_enabled) {
111  util::Preconditions::check_ssl("ssl_config::set_enabled");
112  check_context_enabled_already();
113  this->enabled_ = is_enabled;
114  return *this;
115  }
116 
117  void ssl_config::check_context_enabled_already() const {
118  if (ssl_context_) {
119  throw exception::illegal_argument("You should either use set_context or this method.");
120  }
121  }
122 
123  ssl_config &ssl_config::set_protocol(ssl_protocol protocol) {
124  check_context_enabled_already();
125  this->ssl_protocol_ = protocol;
126  return *this;
127  }
128 
129  ssl_protocol ssl_config::get_protocol() const {
130  check_context_enabled_already();
131  return ssl_protocol_;
132  }
133 
134  const std::vector<std::string> &ssl_config::get_verify_files() const {
135  check_context_enabled_already();
136  return client_verify_files_;
137  }
138 
139  ssl_config &ssl_config::add_verify_file(const std::string &filename) {
140  check_context_enabled_already();
141  this->client_verify_files_.push_back(filename);
142  return *this;
143  }
144 
145  const std::string &ssl_config::get_cipher_list() const {
146  return cipher_list_;
147  }
148 
149  ssl_config &ssl_config::set_cipher_list(const std::string &ciphers) {
150  this->cipher_list_ = ciphers;
151  return *this;
152  }
153 #endif
154 
156 
157  client_flake_id_generator_config::client_flake_id_generator_config(const std::string &name)
158  : name_(name), prefetch_count_(client_flake_id_generator_config::DEFAULT_PREFETCH_COUNT),
159  prefetch_validity_duration_(client_flake_id_generator_config::DEFAULT_PREFETCH_VALIDITY_MILLIS) {}
160 
161  const std::string &client_flake_id_generator_config::get_name() const {
162  return name_;
163  }
164 
165  client_flake_id_generator_config &client_flake_id_generator_config::set_name(const std::string &n) {
166  client_flake_id_generator_config::name_ = n;
167  return *this;
168  }
169 
170  int32_t client_flake_id_generator_config::get_prefetch_count() const {
171  return prefetch_count_;
172  }
173 
174  client_flake_id_generator_config &client_flake_id_generator_config::set_prefetch_count(int32_t count) {
175  std::ostringstream out;
176  out << "prefetch-count must be 1.." << MAXIMUM_PREFETCH_COUNT << ", not " << count;
177  util::Preconditions::check_true(count > 0 && count <= MAXIMUM_PREFETCH_COUNT, out.str());
178  prefetch_count_ = count;
179  return *this;
180  }
181 
182  std::chrono::milliseconds client_flake_id_generator_config::get_prefetch_validity_duration() const {
183  return prefetch_validity_duration_;
184  }
185 
187  client_flake_id_generator_config::set_prefetch_validity_duration(std::chrono::milliseconds duration) {
188  util::Preconditions::check_not_negative(duration.count(),
189  "duration must be nonnegative");
190  prefetch_validity_duration_ = duration;
191  return *this;
192  }
193 
194  constexpr std::chrono::milliseconds connection_retry_config::INITIAL_BACKOFF;
195  constexpr std::chrono::milliseconds connection_retry_config::MAX_BACKOFF;
196  constexpr std::chrono::milliseconds connection_retry_config::CLUSTER_CONNECT_TIMEOUT;
197  constexpr double connection_retry_config::JITTER;
198 
199  std::chrono::milliseconds connection_retry_config::get_initial_backoff_duration() const {
200  return initial_backoff_duration_;
201  }
202 
204  connection_retry_config::set_initial_backoff_duration(std::chrono::milliseconds initial_backoff_duration) {
205  util::Preconditions::check_not_negative(initial_backoff_duration.count(),
206  "Initial backoff must be non-negative!");
207  initial_backoff_duration_ = initial_backoff_duration;
208  return *this;
209  }
210 
211  std::chrono::milliseconds connection_retry_config::get_max_backoff_duration() const {
212  return max_backoff_duration_;
213  }
214 
216  connection_retry_config::set_max_backoff_duration(std::chrono::milliseconds max_backoff_duration) {
217  util::Preconditions::check_not_negative(max_backoff_duration.count(),
218  "Max backoff must be non-negative!");
219  max_backoff_duration_ = max_backoff_duration;
220  return *this;
221  }
222 
223  double connection_retry_config::get_multiplier() const {
224  return multiplier_;
225  }
226 
227  connection_retry_config &connection_retry_config::set_multiplier(double m) {
228  util::Preconditions::check_true(m >= 1.0, "Multiplier must be greater than or equal to 1.0!");
229  multiplier_ = m;
230  return *this;
231  }
232 
233  std::chrono::milliseconds connection_retry_config::get_cluster_connect_timeout() const {
234  return cluster_connect_timeout_;
235  }
236 
237  connection_retry_config &connection_retry_config::set_cluster_connect_timeout(
238  std::chrono::milliseconds cluster_connect_timeout) {
239  util::Preconditions::check_not_negative(cluster_connect_timeout.count(),
240  "Cluster connect timeout must be non-negative!");
241  cluster_connect_timeout_ = cluster_connect_timeout;
242  return *this;
243  }
244 
245  double connection_retry_config::get_jitter() const {
246  return jitter_;
247  }
248 
249  connection_retry_config &connection_retry_config::set_jitter(double jitter) {
250  util::Preconditions::check_true(jitter >= 0.0 && jitter <= 1.0, "Jitter must be in range [0.0, 1.0]");
251  jitter_ = jitter;
252  return *this;
253  }
254 
255  client_network_config::client_network_config()
256  : connection_timeout_(5000), smart_routing_(true) {}
257 
259  return ssl_config_;
260  }
261 
263  ssl_config_ = config;
264  return *this;
265  }
266 
267  std::chrono::milliseconds client_network_config::get_connection_timeout() const {
268  return connection_timeout_;
269  }
270 
272  this->client_aws_config_ = client_aws_config;
273  return *this;
274  }
275 
277  return client_aws_config_;
278  }
279 
281  return cloud_config_;
282  }
283 
285  return smart_routing_;
286  }
287 
289  client_network_config::smart_routing_ = smart_routing;
290  return *this;
291  }
292 
293  std::vector<address> client_network_config::get_addresses() const {
294  return address_list_;
295  }
296 
297  client_network_config &client_network_config::add_addresses(const std::vector<address> &addresses) {
298  address_list_.insert(address_list_.end(), addresses.begin(), addresses.end());
299  return *this;
300  }
301 
302  client_network_config &client_network_config::set_addresses(const std::vector<address> &addresses) {
303  address_list_ = addresses;
304  return *this;
305  }
306 
308  address_list_.push_back(address);
309  return *this;
310  }
311 
312  socket_options &client_network_config::get_socket_options() {
313  return socket_options_;
314  }
315 
316  client_network_config &client_network_config::set_connection_timeout(const std::chrono::milliseconds &timeout) {
317  connection_timeout_ = timeout;
318  return *this;
319  }
320 
321  client_connection_strategy_config::client_connection_strategy_config() : async_start_(false), reconnect_mode_(ON) {
322  }
323 
325  return reconnect_mode_;
326  }
327 
329  return async_start_;
330  }
331 
333  this->async_start_ = async_start;
334  return *this;
335  }
336 
339  this->reconnect_mode_ = reconnect_mode;
340  return *this;
341  }
342 
344  return retry_config_;
345  }
346 
349  retry_config_ = std::move(retry_config);
350  return *this;
351  }
352 
353  constexpr int reliable_topic_config::DEFAULT_READ_BATCH_SIZE;
354 
355  reliable_topic_config::reliable_topic_config() = default;
356 
357  reliable_topic_config::reliable_topic_config(std::string topic_name) : read_batch_size_(DEFAULT_READ_BATCH_SIZE),
358 
359  name_(std::move(topic_name)) {
360  }
361 
362  const std::string &reliable_topic_config::get_name() const {
363  return name_;
364  }
365 
367  return read_batch_size_;
368  }
369 
371  if (batch_size <= 0) {
372  BOOST_THROW_EXCEPTION(exception::illegal_argument("ReliableTopicConfig::setReadBatchSize",
373  "readBatchSize should be positive"));
374  }
375 
376  this->read_batch_size_ = batch_size;
377 
378  return *this;
379  }
380 
381  socket_options::socket_options() : tcp_no_delay_(true), keep_alive_(true), reuse_address_(true), linger_seconds_(3),
382  buffer_size_(DEFAULT_BUFFER_SIZE_BYTE) {}
383 
385  return tcp_no_delay_;
386  }
387 
389  socket_options::tcp_no_delay_ = tcp_no_delay;
390  return *this;
391  }
392 
394  return keep_alive_;
395  }
396 
398  socket_options::keep_alive_ = keep_alive;
399  return *this;
400  }
401 
403  return reuse_address_;
404  }
405 
407  socket_options::reuse_address_ = reuse_address;
408  return *this;
409  }
410 
412  return linger_seconds_;
413  }
414 
416  socket_options::linger_seconds_ = linger_seconds;
417  return *this;
418  }
419 
421  return buffer_size_;
422  }
423 
425  socket_options::buffer_size_ = buffer_size;
426  return *this;
427  }
428 
429  client_aws_config::client_aws_config() : enabled_(false), region_("us-east-1"), host_header_("ec2.amazonaws.com"),
430  inside_aws_(false) {
431  }
432 
433  const std::string &client_aws_config::get_access_key() const {
434  return access_key_;
435  }
436 
437  client_aws_config &client_aws_config::set_access_key(const std::string &access_key) {
438  this->access_key_ = util::Preconditions::check_has_text(access_key, "accessKey must contain text");
439  return *this;
440  }
441 
442  const std::string &client_aws_config::get_secret_key() const {
443  return secret_key_;
444  }
445 
446  client_aws_config &client_aws_config::set_secret_key(const std::string &secret_key) {
447  this->secret_key_ = util::Preconditions::check_has_text(secret_key, "secretKey must contain text");
448  return *this;
449  }
450 
451  const std::string &client_aws_config::get_region() const {
452  return region_;
453  }
454 
455  client_aws_config &client_aws_config::set_region(const std::string &region) {
456  this->region_ = util::Preconditions::check_has_text(region, "region must contain text");
457  return *this;
458  }
459 
460  const std::string &client_aws_config::get_host_header() const {
461  return host_header_;
462  }
463 
464  client_aws_config &client_aws_config::set_host_header(const std::string &host_header) {
465  this->host_header_ = util::Preconditions::check_has_text(host_header, "hostHeader must contain text");
466  return *this;
467  }
468 
470  util::Preconditions::check_ssl("get_aws_config");
471  this->enabled_ = enabled;
472  return *this;
473  }
474 
476  return enabled_;
477  }
478 
479  client_aws_config &client_aws_config::set_security_group_name(const std::string &security_group_name) {
480  this->security_group_name_ = security_group_name;
481  return *this;
482  }
483 
484  const std::string &client_aws_config::get_security_group_name() const {
485  return security_group_name_;
486  }
487 
488  client_aws_config &client_aws_config::set_tag_key(const std::string &tag_key) {
489  this->tag_key_ = tag_key;
490  return *this;
491  }
492 
493  const std::string &client_aws_config::get_tag_key() const {
494  return tag_key_;
495  }
496 
497  client_aws_config &client_aws_config::set_tag_value(const std::string &tag_value) {
498  this->tag_value_ = tag_value;
499  return *this;
500  }
501 
502  const std::string &client_aws_config::get_tag_value() const {
503  return tag_value_;
504  }
505 
506  const std::string &client_aws_config::get_iam_role() const {
507  return iam_role_;
508  }
509 
510  client_aws_config &client_aws_config::set_iam_role(const std::string &iam_role) {
511  this->iam_role_ = iam_role;
512  return *this;
513  }
514 
516  return inside_aws_;
517  }
518 
520  this->inside_aws_ = inside_aws;
521  return *this;
522  }
523 
524  std::ostream &operator<<(std::ostream &out, const client_aws_config &config) {
525  return out << "client_aws_config{"
526  << "enabled=" << config.is_enabled()
527  << ", region='" << config.get_region() << '\''
528  << ", securityGroupName='" << config.get_security_group_name() << '\''
529  << ", tagKey='" << config.get_tag_key() << '\''
530  << ", tagValue='" << config.get_tag_value() << '\''
531  << ", hostHeader='" << config.get_host_header() << '\''
532  << ", iamRole='" << config.get_iam_role() << "\'}";
533  }
534 
535  namespace matcher {
536  std::shared_ptr<std::string>
537  matching_point_config_pattern_matcher::matches(const std::vector<std::string> &config_patterns,
538  const std::string &item_name) const {
539  std::shared_ptr<std::string> candidate;
540  std::shared_ptr<std::string> duplicate;
541  int lastMatchingPoint = -1;
542  for (const std::string &pattern : config_patterns) {
543  int matchingPoint = get_matching_point(pattern, item_name);
544  if (matchingPoint > -1 && matchingPoint >= lastMatchingPoint) {
545  if (matchingPoint == lastMatchingPoint) {
546  duplicate = candidate;
547  } else {
548  duplicate.reset();
549  }
550  lastMatchingPoint = matchingPoint;
551  candidate.reset(new std::string(pattern));
552  }
553  }
554  if (duplicate.get() != NULL) {
556  "MatchingPointConfigPatternMatcher::matches") << "Configuration " << item_name
557  << " has duplicate configuration. Candidate:"
558  << *candidate << ", duplicate:"
559  << *duplicate).build();
560  }
561  return candidate;
562  }
563 
564  int matching_point_config_pattern_matcher::get_matching_point(const std::string &pattern,
565  const std::string &item_name) const {
566  size_t index = pattern.find('*');
567  if (index == std::string::npos) {
568  return -1;
569  }
570 
571  std::string firstPart = pattern.substr(0, index);
572  if (item_name.find(firstPart) != 0) {
573  return -1;
574  }
575 
576  std::string secondPart = pattern.substr(index + 1);
577  if (item_name.rfind(secondPart) != (item_name.length() - secondPart.length())) {
578  return -1;
579  }
580 
581  return (int) (firstPart.length() + secondPart.length());
582  }
583  }
584 
585  const std::string index_config::bitmap_index_options::DEFAULT_KEY = query::query_constants::KEY_ATTRIBUTE_NAME;
586  const index_config::bitmap_index_options::unique_key_transformation index_config::bitmap_index_options::DEFAULT_TRANSFORMATION = index_config::bitmap_index_options::unique_key_transformation::OBJECT;
587 
589  transformation(DEFAULT_TRANSFORMATION) {}
590 
591  const index_config::index_type index_config::DEFAULT_TYPE = index_config::index_type::SORTED;
592 
593  index_config::index_config() : type(DEFAULT_TYPE) {}
594 
595  index_config::index_config(index_config::index_type type) : type(type) {}
596 
597  void index_config::add_attributes() {}
598 
599  eviction_config::eviction_config() : size_(DEFAULT_MAX_ENTRY_COUNT), max_size_policy_(DEFAULT_MAX_SIZE_POLICY),
600  eviction_policy_(DEFAULT_EVICTION_POLICY) {}
601 
602  int32_t eviction_config::get_size() const {
603  return size_;
604  }
605 
606  eviction_config &eviction_config::set_size(int32_t size) {
607  this->size_ = util::Preconditions::check_positive(size, "Size must be positive number!");
608  return *this;
609  }
610 
611  eviction_config::max_size_policy eviction_config::get_maximum_size_policy() const {
612  return max_size_policy_;
613  }
614 
615  eviction_config &eviction_config::set_maximum_size_policy(const eviction_config::max_size_policy &max_size_policy) {
616  this->max_size_policy_ = max_size_policy;
617  return *this;
618  }
619 
620  eviction_policy eviction_config::get_eviction_policy() const {
621  return eviction_policy_;
622  }
623 
624  eviction_config &eviction_config::set_eviction_policy(eviction_policy policy) {
625  this->eviction_policy_ = policy;
626  return *this;
627  }
628 
629  eviction_strategy_type eviction_config::get_eviction_strategy_type() const {
630  // TODO: add support for other/custom eviction strategies
631  return eviction_strategy_type::DEFAULT_EVICTION_STRATEGY;
632  }
633 
634  std::ostream &operator<<(std::ostream &out, const eviction_config &config) {
635  out << "EvictionConfig{"
636  << "size=" << config.get_size()
637  << ", maxSizePolicy=" << config.get_maximum_size_policy()
638  << ", evictionPolicy=" << config.get_eviction_policy()
639  << '}';
640 
641  return out;
642  }
643 
644  near_cache_config::near_cache_config() : name_("default"), time_to_live_seconds_(DEFAULT_TTL_SECONDS),
645  max_idle_seconds_(DEFAULT_MAX_IDLE_SECONDS),
646  in_memory_format_(DEFAULT_MEMORY_FORMAT),
647  local_update_policy_(INVALIDATE), invalidate_on_change_(true),
648  cache_local_entries_(false) {
649  }
650 
651  near_cache_config::near_cache_config(const std::string &cache_name) : near_cache_config() {
652  name_ = cache_name;
653  }
654 
655  near_cache_config::near_cache_config(const std::string &cache_name, in_memory_format memory_format)
656  : near_cache_config(name_) {
657  this->in_memory_format_ = memory_format;
658  }
659 
660  near_cache_config::near_cache_config(int32_t time_to_live_seconds, int32_t max_idle_seconds, bool invalidate_on_change,
661  in_memory_format in_memory_format, const eviction_config &evict_config)
662  : near_cache_config(name_, in_memory_format) {
663  this->time_to_live_seconds_ = time_to_live_seconds;
664  this->max_idle_seconds_ = max_idle_seconds;
665  this->invalidate_on_change_ = invalidate_on_change;
666  this->eviction_config_ = evict_config;
667  }
668 
669  const std::string &near_cache_config::get_name() const {
670  return name_;
671  }
672 
673  near_cache_config &near_cache_config::set_name(const std::string &name) {
674  this->name_ = name;
675  return *this;
676  }
677 
679  return time_to_live_seconds_;
680  }
681 
683  this->time_to_live_seconds_ = util::Preconditions::check_not_negative(time_to_live_seconds,
684  "TTL seconds cannot be negative!");
685  return *this;
686  }
687 
689  return max_idle_seconds_;
690  }
691 
693  this->max_idle_seconds_ = util::Preconditions::check_not_negative(max_idle_seconds,
694  "Max-Idle seconds cannot be negative!");
695  return *this;
696  }
697 
699  return invalidate_on_change_;
700  }
701 
703  this->invalidate_on_change_ = invalidate_on_change;
704  return *this;
705  }
706 
707  const in_memory_format &near_cache_config::get_in_memory_format() const {
708  return in_memory_format_;
709  }
710 
711  near_cache_config &near_cache_config::set_in_memory_format(const in_memory_format &in_memory_format) {
712  this->in_memory_format_ = in_memory_format;
713  return *this;
714  }
715 
717  return cache_local_entries_;
718  }
719 
721  this->cache_local_entries_ = cache_local_entries;
722  return *this;
723  }
724 
725  const near_cache_config::local_update_policy &near_cache_config::get_local_update_policy() const {
726  return local_update_policy_;
727  }
728 
729  near_cache_config &near_cache_config::set_local_update_policy(const local_update_policy &local_update_policy) {
730  this->local_update_policy_ = local_update_policy;
731  return *this;
732  }
733 
735  return eviction_config_;
736  }
737 
739  this->eviction_config_ = eviction_config;
740  return *this;
741  }
742 
743  int32_t near_cache_config::calculate_max_size(int32_t max_size) {
744  return (max_size == 0) ? INT32_MAX : util::Preconditions::check_not_negative(max_size,
745  "Max-size cannot be negative!");
746  }
747 
748  std::ostream &operator<<(std::ostream &out, const near_cache_config &config) {
749  out << "NearCacheConfig{"
750  << "timeToLiveSeconds=" << config.time_to_live_seconds_
751  << ", maxIdleSeconds=" << config.max_idle_seconds_
752  << ", invalidateOnChange=" << config.invalidate_on_change_
753  << ", inMemoryFormat=" << config.in_memory_format_
754  << ", cacheLocalEntries=" << config.cache_local_entries_
755  << ", localUpdatePolicy=" << config.local_update_policy_
756  << config.eviction_config_;
757  out << '}';
758 
759  return out;
760  }
761  }
762 
763  client_config::client_config() : cluster_name_("dev"), redo_operation_(false),
764  socket_interceptor_(), executor_pool_size_(-1) {}
765 
767 
768  client_config &client_config::operator=(client_config &&rhs) = default;
769 
771  this->redo_operation_ = redo_operation;
772  return *this;
773  }
774 
776  return redo_operation_;
777  }
778 
780  if (!load_balancer_) {
781  auto index = std::make_shared<std::atomic<size_t>>(0);
782  load_balancer_ = load_balancer().next([=](cluster &c) {
783  auto members = c.get_members();
784  if (members.empty()) {
785  return boost::optional<member>();
786  }
787  auto i = index->fetch_add(1);
788  return boost::make_optional(std::move(members[i % members.size()]));
789  });
790  }
791  return *load_balancer_;
792  }
793 
795  this->load_balancer_ = std::move(load_balancer);
796  return *this;
797  }
798 
800  return logger_config_;
801  }
802 
804  lifecycle_listeners_.emplace_back(std::move(listener));
805  return *this;
806  }
807 
809  membership_listeners_.emplace_back(std::move(listener));
810  return *this;
811  }
812 
813  const std::vector<lifecycle_listener> &client_config::get_lifecycle_listeners() const {
814  return lifecycle_listeners_;
815  }
816 
817  const std::vector<membership_listener> &client_config::get_membership_listeners() const {
818  return membership_listeners_;
819  }
820 
822  this->socket_interceptor_ = std::move(interceptor);
823  return *this;
824  }
825 
827  return socket_interceptor_;
828  }
829 
831  return serialization_config_;
832  }
833 
835  this->serialization_config_ = serialization_config;
836  return *this;
837  }
838 
839  const std::unordered_map<std::string, std::string> &client_config::get_properties() const {
840  return properties_;
841  }
842 
843  client_config &client_config::set_property(const std::string &name, const std::string &value) {
844  properties_[name] = value;
845  return *this;
846  }
847 
849  reliable_topic_config_map_[reliable_topic_config.get_name()] = reliable_topic_config;
850  return *this;
851  }
852 
854  auto it = reliable_topic_config_map_.find(name);
855  if (it != reliable_topic_config_map_.end()) {
856  return it->second;
857  }
858 
859  return reliable_topic_config_map_.emplace(name, name).first->second;
860  }
861 
862  const config::reliable_topic_config *client_config::lookup_reliable_topic_config(const std::string &name) const {
863  auto it = reliable_topic_config_map_.find(name);
864  if (it != reliable_topic_config_map_.end()) {
865  return &it->second;
866  }
867 
868  return nullptr;
869  }
870 
872  return network_config_;
873  }
874 
876  near_cache_config_map_.emplace(near_cache_config.get_name(), near_cache_config);
877  return *this;
878  }
879 
880  const config::near_cache_config *client_config::get_near_cache_config(const std::string &name) const {
881  auto nearCacheConfig = internal::config::ConfigUtils::lookup_by_pattern(
882  config_pattern_matcher_, near_cache_config_map_, name);
883  if (nearCacheConfig) {
884  return nearCacheConfig;
885  }
886 
887  auto config_it = near_cache_config_map_.find("default");
888  if (config_it != near_cache_config_map_.end()) {
889  return &near_cache_config_map_.find("default")->second;
890  }
891 
892  // not needed for c++ client since it is always native memory
893  //initDefaultMaxSizeForOnHeapMaps(nearCacheConfig);
894  return nullptr;
895  }
896 
898  this->network_config_ = network_config;
899  return *this;
900  }
901 
902  const boost::optional<std::string> &client_config::get_instance_name() const {
903  return instance_name_;
904  }
905 
906  client_config &client_config::set_instance_name(const std::string &instance_name) {
907  client_config::instance_name_ = instance_name;
908  return *this;
909  }
910 
912  return executor_pool_size_;
913  }
914 
915  void client_config::set_executor_pool_size(int32_t executor_pool_size) {
916  client_config::executor_pool_size_ = executor_pool_size;
917  }
918 
919  config::client_connection_strategy_config &client_config::get_connection_strategy_config() {
920  return connection_strategy_config_;
921  }
922 
923  client_config &client_config::set_connection_strategy_config(
924  const config::client_connection_strategy_config &connection_strategy_config) {
925  client_config::connection_strategy_config_ = connection_strategy_config;
926  return *this;
927  }
928 
929  const config::client_flake_id_generator_config *
931  std::string baseName = internal::partition::strategy::StringPartitioningStrategy::get_base_name(name);
932  auto config = internal::config::ConfigUtils::lookup_by_pattern<config::client_flake_id_generator_config>(
933  config_pattern_matcher_, flake_id_generator_config_map_, baseName);
934  if (config) {
935  return config;
936  }
937  return get_flake_id_generator_config("default");
938  }
939 
940 
943  std::string baseName = internal::partition::strategy::StringPartitioningStrategy::get_base_name(name);
944  auto config = internal::config::ConfigUtils::lookup_by_pattern<config::client_flake_id_generator_config>(
945  config_pattern_matcher_, flake_id_generator_config_map_, baseName);
946  if (config) {
947  return config;
948  }
949  auto defConfig = flake_id_generator_config_map_.find("default");
950  if (defConfig == flake_id_generator_config_map_.end()) {
951  flake_id_generator_config_map_.emplace("default", config::client_flake_id_generator_config("default"));
952  }
953  defConfig = flake_id_generator_config_map_.find("default");
954  config::client_flake_id_generator_config new_config = defConfig->second;
955  new_config.set_name(name);
956  flake_id_generator_config_map_.emplace(name, std::move(new_config));
957  return &flake_id_generator_config_map_.find(name)->second;
958  }
959 
960  client_config &
962  flake_id_generator_config_map_.emplace(config.get_name(), config);
963  return *this;
964  }
965 
966  const std::string &client_config::get_cluster_name() const {
967  return cluster_name_;
968  }
969 
970  client_config &client_config::set_cluster_name(const std::string &cluster_name) {
971  cluster_name_ = cluster_name;
972  return *this;
973  }
974 
975  const std::unordered_set<std::string> &client_config::get_labels() const {
976  return labels_;
977  }
978 
979  client_config &client_config::set_labels(const std::unordered_set<std::string> &labels) {
980  labels_ = labels;
981  return *this;
982  }
983 
984  client_config &client_config::add_label(const std::string &label) {
985  labels_.insert(label);
986  return *this;
987  }
988 
990  backup_acks_enabled_ = enabled;
991  return *this;
992  }
993 
995  return backup_acks_enabled_;
996  }
997 
998  const std::shared_ptr<security::credentials> &client_config::get_credentials() const {
999  return credentials_;
1000  }
1001 
1002  client_config &client_config::set_credentials(const std::shared_ptr<security::credentials> &credential) {
1003  credentials_ = credential;
1004  return *this;
1005  }
1006 
1007  namespace security {
1008  username_password_credentials::username_password_credentials(const std::string &name,
1009  const std::string &password) : credentials(name),
1010  password_(password) {}
1011 
1012  const std::string &username_password_credentials::password() const {
1013  return password_;
1014  }
1015 
1016  const credentials::credential_type username_password_credentials::type() const {
1017  return credentials::credential_type::username_password;
1018  }
1019 
1020  const std::vector<byte> &token_credentials::token() const {
1021  return token_;
1022  }
1023 
1024  const credentials::credential_type token_credentials::type() const {
1025  return credentials::credential_type::token;
1026  }
1027 
1028  token_credentials::token_credentials(const std::vector<byte> &token) : credentials(
1029  token.empty() ? "<empty>" : "<token>"), token_(token) {}
1030 
1031  credentials::~credentials() {}
1032 
1033  const std::string &credentials::name() const {
1034  return name_;
1035  }
1036 
1037  credentials::credentials(const std::string &name) : name_(name) {}
1038  }
1039  }
1040 }
Represents an address of a client or member in the cluster.
Definition: address.h:36
hazelcast_client configuration class.
client_config & set_serialization_config(serialization_config const &serialization_config)
SerializationConfig is used to.
Definition: config.cpp:834
serialization_config & get_serialization_config()
Definition: config.cpp:830
const config::reliable_topic_config & get_reliable_topic_config(const std::string &name)
Gets the ClientReliableTopicConfig for a given reliable topic name.
Definition: config.cpp:853
client_config & set_socket_interceptor(socket_interceptor &&interceptor)
Will be called with the Socket, each time client creates a connection to any Member.
Definition: config.cpp:821
client_config & set_redo_operation(bool redo_operation)
If true, client will redo the operations that were executing on the server and client lost the connec...
Definition: config.cpp:770
void set_executor_pool_size(int32_t executor_pool_size)
Sets Client side Executor pool size.
Definition: config.cpp:915
const config::near_cache_config * get_near_cache_config(const std::string &name) const
Gets the NearCacheConfig configured for the map / cache with name.
Definition: config.cpp:880
client_config & add_listener(lifecycle_listener &&listener)
Adds a listener to configuration to be registered when hazelcast_client starts.
Definition: config.cpp:803
client_config & set_credentials(const std::shared_ptr< security::credentials > &credential)
There are two types of credentials you can provide, \username_password_credentials and \token_credent...
Definition: config.cpp:1002
const std::unordered_map< std::string, std::string > & get_properties() const
Gets a reference to properties map.
Definition: config.cpp:839
const socket_interceptor & get_socket_interceptor() const
Will be called with the Socket, each time client creates a connection to any Member.
Definition: config.cpp:826
client_config()
Constructor with default values.
Definition: config.cpp:763
config::client_network_config & get_network_config()
Gets {}.
Definition: config.cpp:871
int32_t get_executor_pool_size() const
Pool size for internal ExecutorService which handles responses etc.
Definition: config.cpp:911
client_config & add_reliable_topic_config(const config::reliable_topic_config &reliable_topic_config)
Adds a ClientReliableTopicConfig.
Definition: config.cpp:848
bool is_redo_operation() const
see setRedoOperation returns redoOperation
Definition: config.cpp:775
const std::vector< membership_listener > & get_membership_listeners() const
Returns registered membershipListeners.
Definition: config.cpp:817
client_config & add_flake_id_generator_config(const config::client_flake_id_generator_config &config)
Adds a flake ID generator configuration.
Definition: config.cpp:961
const std::string & get_cluster_name() const
Returns the configured cluster name.
Definition: config.cpp:966
config::logger_config & get_logger_config()
Definition: config.cpp:799
bool backup_acks_enabled()
Note that backup acks to client can be enabled only for smart client.
Definition: config.cpp:994
client_config & set_load_balancer(load_balancer &&load_balancer)
Used to distribute the operations to multiple connections.
Definition: config.cpp:794
load_balancer & get_load_balancer()
Used to distribute the operations to multiple Endpoints.
Definition: config.cpp:779
client_config & set_network_config(const config::client_network_config &network_config)
Sets {}.
Definition: config.cpp:897
const config::client_flake_id_generator_config * get_flake_id_generator_config(const std::string &name)
Returns the {} for the given name, creating one if necessary and adding it to the collection of known...
Definition: config.cpp:942
client_config & add_near_cache_config(const config::near_cache_config &near_cache_config)
Helper method to add a new NearCacheConfig.
Definition: config.cpp:875
const config::client_flake_id_generator_config * find_flake_id_generator_config(const std::string &name)
Returns a {} configuration for the given flake ID generator name.
Definition: config.cpp:930
client_config & set_property(const std::string &name, const std::string &value)
Sets the value of a named property.
Definition: config.cpp:843
const std::vector< lifecycle_listener > & get_lifecycle_listeners() const
Definition: config.cpp:813
Hazelcast cluster interface.
Definition: cluster.h:36
std::vector< member > get_members()
Set of current members of the cluster.
Definition: cluster.cpp:53
The client_aws_config contains the configuration for client to connect to nodes in aws environment.
const std::string & get_access_key() const
Gets the access key to access AWS.
Definition: config.cpp:433
const std::string & get_tag_key() const
Gets the tag key.
Definition: config.cpp:493
const std::string & get_security_group_name() const
Gets the security group name.
Definition: config.cpp:484
const std::string & get_region() const
Gets the region where the EC2 instances running the Hazelcast members will be running.
Definition: config.cpp:451
const std::string & get_host_header() const
Gets the host header; the address where the EC2 API can be found.
Definition: config.cpp:460
client_aws_config & set_secret_key(const std::string &secret_key)
Sets the secret key to access AWS.
Definition: config.cpp:446
client_aws_config & set_host_header(const std::string &host_header)
Sets the host header; the address where the EC2 API can be found.
Definition: config.cpp:464
client_aws_config & set_tag_key(const std::string &tag_key)
Sets the tag key.
Definition: config.cpp:488
client_aws_config & set_iam_role(const std::string &iam_role)
Sets the tag value.
Definition: config.cpp:510
client_aws_config & set_inside_aws(bool inside_aws)
Set to true if client is inside aws environment Default value is false.
Definition: config.cpp:519
client_aws_config & set_region(const std::string &region)
Sets the region where the EC2 instances running the Hazelcast members will be running.
Definition: config.cpp:455
bool is_enabled() const
Checks if the aws join mechanism is enabled.
Definition: config.cpp:475
bool is_inside_aws() const
If client is inside aws, it will use private ip addresses directly, otherwise it will convert private...
Definition: config.cpp:515
const std::string & get_secret_key() const
Gets the secret key to access AWS.
Definition: config.cpp:442
client_aws_config & set_enabled(bool enabled)
Enables or disables the aws join mechanism.
Definition: config.cpp:469
const std::string & get_tag_value() const
Gets the tag value.
Definition: config.cpp:502
client_aws_config & set_access_key(const std::string &access_key)
Sets the access key to access AWS.
Definition: config.cpp:437
client_aws_config & set_tag_value(const std::string &tag_value)
Sets the tag value.
Definition: config.cpp:497
const std::string & get_iam_role() const
Gets the iamRole name.
Definition: config.cpp:506
client_aws_config & set_security_group_name(const std::string &security_group_name)
Sets the security group name.
Definition: config.cpp:479
Client connection strategy configuration is used for setting custom strategies and configuring strate...
client_connection_strategy_config & set_reconnect_mode(reconnect_mode reconnect_mode)
How a client reconnect to cluster after a disconnect can be configured.
Definition: config.cpp:338
client_connection_strategy_config & set_retry_config(connection_retry_config retry_config)
connection_retry_config controls the period among the retries and when a client should give up retryi...
Definition: config.cpp:348
client_connection_strategy_config & set_async_start(bool async_start)
Set true for non blocking hazelcast_client(const client_config &).
Definition: config.cpp:332
connection_retry_config & get_retry_config()
connection_retry_config controls the period among the retries and when a client should give up retryi...
Definition: config.cpp:343
bool is_async_start() const
Client instance creation won't block on hazelcast_client(ClientConfig &) if this value is true.
Definition: config.cpp:328
static constexpr int64_t DEFAULT_PREFETCH_VALIDITY_MILLIS
Default value for getPrefetchValidityMillis().
const std::string & get_name() const
Returns the configuration name.
Definition: config.cpp:161
client_flake_id_generator_config & set_name(const std::string &n)
Sets the name or name pattern for this config.
Definition: config.cpp:165
Contains configuration parameters for client network related behaviour.
client_network_config & set_addresses(const std::vector< address > &addresses)
Adds given addresses to candidate address list that client will use to establish initial connection.
Definition: config.cpp:302
bool is_smart_routing() const
See client_network_config::setSmartRouting(boolean) for details.
Definition: config.cpp:284
std::vector< address > get_addresses() const
Returns the list of candidate addresses that client will use to establish initial connection.
Definition: config.cpp:293
client_network_config & add_addresses(const std::vector< address > &addresses)
Adds given addresses to candidate address list that client will use to establish initial connection.
Definition: config.cpp:297
client_network_config & add_address(const address &address)
Adds given address to candidate address list that client will use to establish initial connection.
Definition: config.cpp:307
client_network_config & set_smart_routing(bool smart_routing)
If.
Definition: config.cpp:288
client_network_config & set_connection_timeout(const std::chrono::milliseconds &timeout)
Definition: config.cpp:316
client_aws_config & get_aws_config()
Returns the current client_aws_config.
Definition: config.cpp:276
ssl_config & get_ssl_config()
Returns the current \ssl_config .
Definition: config.cpp:258
std::chrono::milliseconds get_connection_timeout() const
Connection timeout value for connecting to a member server.
Definition: config.cpp:267
cloud_config & get_cloud_config()
Returns the current cloud_config.
Definition: config.cpp:280
client_network_config & set_aws_config(const client_aws_config &client_aws_config)
Sets configuration to connect nodes in aws environment.
Definition: config.cpp:271
client_network_config & set_ssl_config(const config::ssl_config &config)
Sets the ssl_config.
Definition: config.cpp:262
Connection Retry Config is controls the period among the retries and when should a client gave up ret...
std::shared_ptr< std::string > matches(const std::vector< std::string > &config_patterns, const std::string &item_name) const override
Returns the best match for an item name out of a list of configuration patterns.
Definition: config.cpp:537
Contains the configuration for a Near Cache.
near_cache_config & set_cache_local_entries(bool cache_local_entries)
True to cache local entries also.
Definition: config.cpp:720
virtual near_cache_config & set_in_memory_format(const in_memory_format &in_memory_format)
Sets the data type used to store entries.
Definition: config.cpp:711
near_cache_config & set_eviction_config(const eviction_config &eviction_config)
Sets the eviction configuration.
Definition: config.cpp:738
const in_memory_format & get_in_memory_format() const
Gets the data type used to store entries.
Definition: config.cpp:707
int32_t get_time_to_live_seconds() const
Gets the maximum number of seconds for each entry to stay in the Near Cache.
Definition: config.cpp:678
near_cache_config & set_time_to_live_seconds(int32_t time_to_live_seconds)
Sets the maximum number of seconds for each entry to stay in the Near Cache.
Definition: config.cpp:682
near_cache_config & set_invalidate_on_change(bool invalidate_on_change)
True to evict the cached entries if the entries are changed (updated or removed).
Definition: config.cpp:702
const std::string & get_name() const
Gets the name of the Near Cache.
Definition: config.cpp:669
eviction_config & get_eviction_config()
The eviction configuration.
Definition: config.cpp:734
bool is_cache_local_entries() const
If true, cache local entries also.
Definition: config.cpp:716
bool is_invalidate_on_change() const
True to evict the cached entries if the entries are changed (updated or removed).
Definition: config.cpp:698
near_cache_config & set_name(const std::string &name)
Sets the name of the Near Cache.
Definition: config.cpp:673
int32_t get_max_idle_seconds() const
Maximum number of seconds each entry can stay in the Near Cache as untouched (not-read).
Definition: config.cpp:688
near_cache_config & set_max_idle_seconds(int32_t max_idle_seconds)
Maximum number of seconds each entry can stay in the Near Cache as untouched (not-read).
Definition: config.cpp:692
const std::string & get_name() const
Gets the name of the reliable topic.
Definition: config.cpp:362
reliable_topic_config & set_read_batch_size(int batch_size)
Sets the read batch size.
Definition: config.cpp:370
int get_read_batch_size() const
Gets the maximum number of items to read in a batch.
Definition: config.cpp:366
socket_options & set_tcp_no_delay(bool tcp_no_delay)
Enable/disable TCP_NODELAY socket option.
Definition: config.cpp:388
socket_options & set_reuse_address(bool reuse_address)
Enable/disable the SO_REUSEADDR socket option.
Definition: config.cpp:406
socket_options & set_linger_seconds(int linger_seconds)
Enable/disable SO_LINGER with the specified linger time in seconds.
Definition: config.cpp:415
bool is_keep_alive() const
SO_KEEPALIVE socket option.
Definition: config.cpp:393
socket_options & set_keep_alive(bool keep_alive)
Enable/disable SO_KEEPALIVE socket option.
Definition: config.cpp:397
bool is_reuse_address() const
SO_REUSEADDR socket option.
Definition: config.cpp:402
bool is_tcp_no_delay() const
TCP_NODELAY socket option.
Definition: config.cpp:384
int get_buffer_size_in_bytes() const
If set to 0 or less, then it is not set on the socket.
Definition: config.cpp:420
socket_options & set_buffer_size_in_bytes(int buffer_size)
If set to 0 or less, then it is not set on the socket.
Definition: config.cpp:424
int get_linger_seconds() const
Gets SO_LINGER with the specified linger time in seconds.
Definition: config.cpp:411
Contains configuration parameters for ssl related behaviour.
Definition: ssl_config.h:61
Listener object for listening lifecycle events of hazelcast instance.
load_balancer allows you to send operations to one of a number of endpoints(Members).
Definition: load_balancer.h:37
load_balancer & next(Handler &&h) &
The function returns the next member to route to.
Definition: load_balancer.h:66
SerializationConfig is used to.
serialization_config & set_portable_version(int v)
Definition: config.cpp:64
serialization_config & set_global_serializer(const std::shared_ptr< serialization::global_serializer > &global_serializer)
Definition: config.cpp:73
serialization_config & set_byte_order(boost::endian::order byte_order)
Definition: config.cpp:79
int get_portable_version() const
Portable version will be used to differentiate two same class that have changes on it ,...
Definition: config.cpp:60
serialization_config()
Constructor default value of version is zero.
Definition: config.cpp:57
boost::endian::order get_byte_order() const
Definition: config.cpp:84
An interface that provides the ability to intercept the creation of sockets.
hazelcast.cloud configuration to let the client connect the cluster via hazelcast....
Definition: cloud_config.h:31
bitmap_index_options()
Constructs a new bitmap index options instance with all options set to default values.
Definition: config.cpp:588
static const unique_key_transformation DEFAULT_TRANSFORMATION
The default for \transformation.
Definition: index_config.h:76
static const std::string DEFAULT_KEY
The default for \key.
Definition: index_config.h:71
index_type type
Type of the index.
Definition: index_config.h:100