Hazelcast C++ Client
Hazelcast C++ Client Library
config.cpp
1 /*
2  * Copyright (c) 2008-2022, 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 #include <atomic>
17 
18 #include "hazelcast/client/client_config.h"
19 #include "hazelcast/client/serialization_config.h"
20 #include "hazelcast/client/config/ssl_config.h"
21 #include "hazelcast/util/Preconditions.h"
22 #include "hazelcast/client/config/client_flake_id_generator_config.h"
23 #include "hazelcast/client/exception/protocol_exceptions.h"
24 #include "hazelcast/client/internal/partition/strategy/StringPartitioningStrategy.h"
25 #include "hazelcast/client/address.h"
26 #include "hazelcast/client/config/client_network_config.h"
27 #include "hazelcast/client/config/client_aws_config.h"
28 #include "hazelcast/client/config/reliable_topic_config.h"
29 #include "hazelcast/client/config/client_connection_strategy_config.h"
30 #include "hazelcast/client/config/logger_config.h"
31 #include "hazelcast/client/config/index_config.h"
32 #include "hazelcast/client/config/matcher/matching_point_config_pattern_matcher.h"
33 #include "hazelcast/client/query/predicates.h"
34 #include "hazelcast/client/lifecycle_listener.h"
35 #include "hazelcast/client/config/eviction_strategy_type.h"
36 #include "hazelcast/client/cluster.h"
37 #include "hazelcast/client/initial_membership_event.h"
38 
39 namespace hazelcast {
40 namespace client {
42  : version_(0)
43 {}
44 
45 int
47 {
48  return version_;
49 }
50 
53 {
54  this->version_ = v;
55  return *this;
56 }
57 
58 std::shared_ptr<serialization::global_serializer>
59 serialization_config::get_global_serializer() const
60 {
61  return global_serializer_;
62 }
63 
64 serialization_config&
66  const std::shared_ptr<serialization::global_serializer>& global_serializer)
67 {
68  global_serializer_ = global_serializer;
69  return *this;
70 }
71 
73 serialization_config::set_byte_order(boost::endian::order byte_order)
74 {
75  byte_order_ = byte_order;
76  return *this;
77 }
78 
79 boost::endian::order
81 {
82  return byte_order_;
83 }
84 
85 namespace config {
86 ssl_config::ssl_config()
87 #ifdef HZ_BUILD_WITH_SSL
88  : enabled_(false)
89  , ssl_protocol_(tlsv12)
90 #endif
91 {}
92 
93 #ifdef HZ_BUILD_WITH_SSL
94 ssl_config&
95 ssl_config::set_context(boost::asio::ssl::context ctx)
96 {
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 "
101  "enable ssl. You already used the deprecated way.");
102  }
103  ssl_context_ = std::make_shared<boost::asio::ssl::context>(std::move(ctx));
104  return *this;
105 }
106 
107 bool
108 ssl_config::is_enabled() const
109 {
110  return ssl_context_ || enabled_;
111 }
112 
113 ssl_config&
114 ssl_config::set_enabled(bool is_enabled)
115 {
116  util::Preconditions::check_ssl("ssl_config::set_enabled");
117  check_context_enabled_already();
118  this->enabled_ = is_enabled;
119  return *this;
120 }
121 
122 void
123 ssl_config::check_context_enabled_already() const
124 {
125  if (ssl_context_) {
126  throw exception::illegal_argument(
127  "You should either use set_context or this method.");
128  }
129 }
130 
131 ssl_config&
132 ssl_config::set_protocol(ssl_protocol protocol)
133 {
134  check_context_enabled_already();
135  this->ssl_protocol_ = protocol;
136  return *this;
137 }
138 
139 ssl_protocol
140 ssl_config::get_protocol() const
141 {
142  check_context_enabled_already();
143  return ssl_protocol_;
144 }
145 
146 const std::vector<std::string>&
147 ssl_config::get_verify_files() const
148 {
149  check_context_enabled_already();
150  return client_verify_files_;
151 }
152 
153 ssl_config&
154 ssl_config::add_verify_file(const std::string& filename)
155 {
156  check_context_enabled_already();
157  this->client_verify_files_.push_back(filename);
158  return *this;
159 }
160 
161 const std::string&
162 ssl_config::get_cipher_list() const
163 {
164  return cipher_list_;
165 }
166 
167 ssl_config&
168 ssl_config::set_cipher_list(const std::string& ciphers)
169 {
170  this->cipher_list_ = ciphers;
171  return *this;
172 }
173 #endif
174 
175 constexpr int64_t
177 
178 client_flake_id_generator_config::client_flake_id_generator_config(
179  const std::string& name)
180  : name_(name)
181  , prefetch_count_(client_flake_id_generator_config::DEFAULT_PREFETCH_COUNT)
182  , prefetch_validity_duration_(
183  client_flake_id_generator_config::DEFAULT_PREFETCH_VALIDITY_MILLIS)
184 {}
185 
186 const std::string&
187 client_flake_id_generator_config::get_name() const
188 {
189  return name_;
190 }
191 
193 client_flake_id_generator_config::set_name(const std::string& n)
194 {
195  client_flake_id_generator_config::name_ = n;
196  return *this;
197 }
198 
199 int32_t
200 client_flake_id_generator_config::get_prefetch_count() const
201 {
202  return prefetch_count_;
203 }
204 
206 client_flake_id_generator_config::set_prefetch_count(int32_t count)
207 {
208  std::ostringstream out;
209  out << "prefetch-count must be 1.." << MAXIMUM_PREFETCH_COUNT << ", not "
210  << count;
211  util::Preconditions::check_true(
212  count > 0 && count <= MAXIMUM_PREFETCH_COUNT, out.str());
213  prefetch_count_ = count;
214  return *this;
215 }
216 
217 std::chrono::milliseconds
218 client_flake_id_generator_config::get_prefetch_validity_duration() const
219 {
220  return prefetch_validity_duration_;
221 }
222 
224 client_flake_id_generator_config::set_prefetch_validity_duration(
225  std::chrono::milliseconds duration)
226 {
227  util::Preconditions::check_not_negative(duration.count(),
228  "duration must be nonnegative");
229  prefetch_validity_duration_ = duration;
230  return *this;
231 }
232 
233 constexpr std::chrono::milliseconds connection_retry_config::INITIAL_BACKOFF;
234 constexpr std::chrono::milliseconds connection_retry_config::MAX_BACKOFF;
235 constexpr std::chrono::milliseconds
236  connection_retry_config::CLUSTER_CONNECT_TIMEOUT;
237 constexpr double connection_retry_config::JITTER;
238 
239 std::chrono::milliseconds
240 connection_retry_config::get_initial_backoff_duration() const
241 {
242  return initial_backoff_duration_;
243 }
244 
246 connection_retry_config::set_initial_backoff_duration(
247  std::chrono::milliseconds initial_backoff_duration)
248 {
249  util::Preconditions::check_not_negative(
250  initial_backoff_duration.count(),
251  "Initial backoff must be non-negative!");
252  initial_backoff_duration_ = initial_backoff_duration;
253  return *this;
254 }
255 
256 std::chrono::milliseconds
257 connection_retry_config::get_max_backoff_duration() const
258 {
259  return max_backoff_duration_;
260 }
261 
263 connection_retry_config::set_max_backoff_duration(
264  std::chrono::milliseconds max_backoff_duration)
265 {
266  util::Preconditions::check_not_negative(
267  max_backoff_duration.count(), "Max backoff must be non-negative!");
268  max_backoff_duration_ = max_backoff_duration;
269  return *this;
270 }
271 
272 double
273 connection_retry_config::get_multiplier() const
274 {
275  return multiplier_;
276 }
277 
279 connection_retry_config::set_multiplier(double m)
280 {
281  util::Preconditions::check_true(
282  m >= 1.0, "Multiplier must be greater than or equal to 1.0!");
283  multiplier_ = m;
284  return *this;
285 }
286 
287 std::chrono::milliseconds
288 connection_retry_config::get_cluster_connect_timeout() const
289 {
290  return cluster_connect_timeout_;
291 }
292 
294 connection_retry_config::set_cluster_connect_timeout(
295  std::chrono::milliseconds cluster_connect_timeout)
296 {
297  util::Preconditions::check_not_negative(
298  cluster_connect_timeout.count(),
299  "Cluster connect timeout must be non-negative!");
300  cluster_connect_timeout_ = cluster_connect_timeout;
301  return *this;
302 }
303 
304 double
305 connection_retry_config::get_jitter() const
306 {
307  return jitter_;
308 }
309 
311 connection_retry_config::set_jitter(double jitter)
312 {
313  util::Preconditions::check_true(jitter >= 0.0 && jitter <= 1.0,
314  "Jitter must be in range [0.0, 1.0]");
315  jitter_ = jitter;
316  return *this;
317 }
318 
319 client_network_config::client_network_config()
320  : connection_timeout_(5000)
321  , smart_routing_(true)
322 {}
323 
324 ssl_config&
326 {
327  return ssl_config_;
328 }
329 
332 {
333  ssl_config_ = config;
334  return *this;
335 }
336 
337 std::chrono::milliseconds
339 {
340  return connection_timeout_;
341 }
342 
346 {
347  this->client_aws_config_ = client_aws_config;
348  return *this;
349 }
350 
353 {
354  return client_aws_config_;
355 }
356 
359 {
360  return cloud_config_;
361 }
362 
363 bool
365 {
366  return smart_routing_;
367 }
368 
371 {
372  client_network_config::smart_routing_ = smart_routing;
373  return *this;
374 }
375 
376 std::vector<address>
378 {
379  return address_list_;
380 }
381 
383 client_network_config::add_addresses(const std::vector<address>& addresses)
384 {
385  address_list_.insert(
386  address_list_.end(), addresses.begin(), addresses.end());
387  return *this;
388 }
389 
391 client_network_config::set_addresses(const std::vector<address>& addresses)
392 {
393  address_list_ = addresses;
394  return *this;
395 }
396 
399 {
400  address_list_.push_back(address);
401  return *this;
402 }
403 
405 client_network_config::get_socket_options()
406 {
407  return socket_options_;
408 }
409 
410 client_network_config&
412  const std::chrono::milliseconds& timeout)
413 {
414  connection_timeout_ = timeout;
415  return *this;
416 }
417 
418 bool
420 {
421  return use_public_address_;
422 }
423 
425 client_network_config::use_public_address(bool should_use_public_address)
426 {
427  use_public_address_ = should_use_public_address;
428  return *this;
429 }
430 
431 client_connection_strategy_config::client_connection_strategy_config()
432  : async_start_(false)
433  , reconnect_mode_(ON)
434 {}
435 
438 {
439  return reconnect_mode_;
440 }
441 
442 bool
444 {
445  return async_start_;
446 }
447 
450 {
451  this->async_start_ = async_start;
452  return *this;
453 }
454 
458 {
459  this->reconnect_mode_ = reconnect_mode;
460  return *this;
461 }
462 
465 {
466  return retry_config_;
467 }
468 
471  connection_retry_config retry_config)
472 {
473  retry_config_ = std::move(retry_config);
474  return *this;
475 }
476 
477 constexpr int reliable_topic_config::DEFAULT_READ_BATCH_SIZE;
478 
479 reliable_topic_config::reliable_topic_config() = default;
480 
481 reliable_topic_config::reliable_topic_config(std::string topic_name)
482  : read_batch_size_(DEFAULT_READ_BATCH_SIZE)
483  ,
484 
485  name_(std::move(topic_name))
486 {}
487 
488 const std::string&
490 {
491  return name_;
492 }
493 
494 int
496 {
497  return read_batch_size_;
498 }
499 
502 {
503  if (batch_size <= 0) {
504  BOOST_THROW_EXCEPTION(
505  exception::illegal_argument("ReliableTopicConfig::setReadBatchSize",
506  "readBatchSize should be positive"));
507  }
508 
509  this->read_batch_size_ = batch_size;
510 
511  return *this;
512 }
513 
514 socket_options::socket_options()
515  : tcp_no_delay_(true)
516  , keep_alive_(true)
517  , reuse_address_(true)
518  , linger_seconds_(3)
519  , buffer_size_(DEFAULT_BUFFER_SIZE_BYTE)
520 {}
521 
522 bool
524 {
525  return tcp_no_delay_;
526 }
527 
530 {
531  socket_options::tcp_no_delay_ = tcp_no_delay;
532  return *this;
533 }
534 
535 bool
537 {
538  return keep_alive_;
539 }
540 
543 {
544  socket_options::keep_alive_ = keep_alive;
545  return *this;
546 }
547 
548 bool
550 {
551  return reuse_address_;
552 }
553 
556 {
557  socket_options::reuse_address_ = reuse_address;
558  return *this;
559 }
560 
561 int
563 {
564  return linger_seconds_;
565 }
566 
569 {
570  socket_options::linger_seconds_ = linger_seconds;
571  return *this;
572 }
573 
574 int
576 {
577  return buffer_size_;
578 }
579 
582 {
583  socket_options::buffer_size_ = buffer_size;
584  return *this;
585 }
586 
587 client_aws_config::client_aws_config()
588  : enabled_(false)
589  , region_("us-east-1")
590  , host_header_("ec2.amazonaws.com")
591  , inside_aws_(false)
592 {}
593 
594 const std::string&
596 {
597  return access_key_;
598 }
599 
601 client_aws_config::set_access_key(const std::string& access_key)
602 {
603  this->access_key_ = util::Preconditions::check_has_text(
604  access_key, "accessKey must contain text");
605  return *this;
606 }
607 
608 const std::string&
610 {
611  return secret_key_;
612 }
613 
615 client_aws_config::set_secret_key(const std::string& secret_key)
616 {
617  this->secret_key_ = util::Preconditions::check_has_text(
618  secret_key, "secretKey must contain text");
619  return *this;
620 }
621 
622 const std::string&
624 {
625  return region_;
626 }
627 
629 client_aws_config::set_region(const std::string& region)
630 {
631  this->region_ =
632  util::Preconditions::check_has_text(region, "region must contain text");
633  return *this;
634 }
635 
636 const std::string&
638 {
639  return host_header_;
640 }
641 
643 client_aws_config::set_host_header(const std::string& host_header)
644 {
645  this->host_header_ = util::Preconditions::check_has_text(
646  host_header, "hostHeader must contain text");
647  return *this;
648 }
649 
652 {
653  util::Preconditions::check_ssl("get_aws_config");
654  this->enabled_ = enabled;
655  return *this;
656 }
657 
658 bool
660 {
661  return enabled_;
662 }
663 
666  const std::string& security_group_name)
667 {
668  this->security_group_name_ = security_group_name;
669  return *this;
670 }
671 
672 const std::string&
674 {
675  return security_group_name_;
676 }
677 
679 client_aws_config::set_tag_key(const std::string& tag_key)
680 {
681  this->tag_key_ = tag_key;
682  return *this;
683 }
684 
685 const std::string&
687 {
688  return tag_key_;
689 }
690 
692 client_aws_config::set_tag_value(const std::string& tag_value)
693 {
694  this->tag_value_ = tag_value;
695  return *this;
696 }
697 
698 const std::string&
700 {
701  return tag_value_;
702 }
703 
704 const std::string&
706 {
707  return iam_role_;
708 }
709 
711 client_aws_config::set_iam_role(const std::string& iam_role)
712 {
713  this->iam_role_ = iam_role;
714  return *this;
715 }
716 
717 bool
719 {
720  return inside_aws_;
721 }
722 
725 {
726  this->inside_aws_ = inside_aws;
727  return *this;
728 }
729 
730 std::ostream&
731 operator<<(std::ostream& out, const client_aws_config& config)
732 {
733  return out << "client_aws_config{"
734  << "enabled=" << config.is_enabled() << ", region='"
735  << config.get_region() << '\'' << ", securityGroupName='"
736  << config.get_security_group_name() << '\'' << ", tagKey='"
737  << config.get_tag_key() << '\'' << ", tagValue='"
738  << config.get_tag_value() << '\'' << ", hostHeader='"
739  << config.get_host_header() << '\'' << ", iamRole='"
740  << config.get_iam_role() << "\'}";
741 }
742 
743 namespace matcher {
744 std::shared_ptr<std::string>
746  const std::vector<std::string>& config_patterns,
747  const std::string& item_name) const
748 {
749  std::shared_ptr<std::string> candidate;
750  std::shared_ptr<std::string> duplicate;
751  int lastMatchingPoint = -1;
752  for (const std::string& pattern : config_patterns) {
753  int matchingPoint = get_matching_point(pattern, item_name);
754  if (matchingPoint > -1 && matchingPoint >= lastMatchingPoint) {
755  if (matchingPoint == lastMatchingPoint) {
756  duplicate = candidate;
757  } else {
758  duplicate.reset();
759  }
760  lastMatchingPoint = matchingPoint;
761  candidate.reset(new std::string(pattern));
762  }
763  }
764  if (duplicate.get() != NULL) {
766  "MatchingPointConfigPatternMatcher::matches")
767  << "Configuration " << item_name
768  << " has duplicate configuration. Candidate:" << *candidate
769  << ", duplicate:" << *duplicate)
770  .build();
771  }
772  return candidate;
773 }
774 
775 int
776 matching_point_config_pattern_matcher::get_matching_point(
777  const std::string& pattern,
778  const std::string& item_name) const
779 {
780  size_t index = pattern.find('*');
781  if (index == std::string::npos) {
782  return -1;
783  }
784 
785  std::string firstPart = pattern.substr(0, index);
786  if (item_name.find(firstPart) != 0) {
787  return -1;
788  }
789 
790  std::string secondPart = pattern.substr(index + 1);
791  if (item_name.rfind(secondPart) !=
792  (item_name.length() - secondPart.length())) {
793  return -1;
794  }
795 
796  return (int)(firstPart.length() + secondPart.length());
797 }
798 } // namespace matcher
799 
801  query::query_constants::KEY_ATTRIBUTE_NAME;
804  index_config::bitmap_index_options::unique_key_transformation::OBJECT;
805 
807  : key(DEFAULT_KEY)
808  , transformation(DEFAULT_TRANSFORMATION)
809 {}
810 
811 const index_config::index_type index_config::DEFAULT_TYPE =
812  index_config::index_type::SORTED;
813 
814 index_config::index_config()
815  : type(DEFAULT_TYPE)
816 {}
817 
818 index_config::index_config(index_config::index_type type)
819  : type(type)
820 {}
821 
822 void
823 index_config::add_attributes()
824 {}
825 
826 eviction_config::eviction_config()
827  : size_(DEFAULT_MAX_ENTRY_COUNT)
828  , max_size_policy_(DEFAULT_MAX_SIZE_POLICY)
829  , eviction_policy_(DEFAULT_EVICTION_POLICY)
830 {}
831 
832 int32_t
833 eviction_config::get_size() const
834 {
835  return size_;
836 }
837 
838 eviction_config&
839 eviction_config::set_size(int32_t size)
840 {
841  this->size_ = util::Preconditions::check_positive(
842  size, "Size must be positive number!");
843  return *this;
844 }
845 
847 eviction_config::get_maximum_size_policy() const
848 {
849  return max_size_policy_;
850 }
851 
852 eviction_config&
853 eviction_config::set_maximum_size_policy(
854  const eviction_config::max_size_policy& max_size_policy)
855 {
856  this->max_size_policy_ = max_size_policy;
857  return *this;
858 }
859 
860 eviction_policy
861 eviction_config::get_eviction_policy() const
862 {
863  return eviction_policy_;
864 }
865 
866 eviction_config&
867 eviction_config::set_eviction_policy(eviction_policy policy)
868 {
869  this->eviction_policy_ = policy;
870  return *this;
871 }
872 
873 eviction_strategy_type
874 eviction_config::get_eviction_strategy_type() const
875 {
876  // TODO: add support for other/custom eviction strategies
877  return eviction_strategy_type::DEFAULT_EVICTION_STRATEGY;
878 }
879 
880 std::ostream&
881 operator<<(std::ostream& out, const eviction_config& config)
882 {
883  out << "EvictionConfig{"
884  << "size=" << config.get_size()
885  << ", maxSizePolicy=" << config.get_maximum_size_policy()
886  << ", evictionPolicy=" << config.get_eviction_policy() << '}';
887 
888  return out;
889 }
890 
891 near_cache_config::near_cache_config()
892  : name_("default")
893  , time_to_live_seconds_(DEFAULT_TTL_SECONDS)
894  , max_idle_seconds_(DEFAULT_MAX_IDLE_SECONDS)
895  , in_memory_format_(DEFAULT_MEMORY_FORMAT)
896  , local_update_policy_(INVALIDATE)
897  , invalidate_on_change_(true)
898  , cache_local_entries_(false)
899 {}
900 
901 near_cache_config::near_cache_config(const std::string& cache_name)
902  : near_cache_config()
903 {
904  name_ = cache_name;
905 }
906 
907 near_cache_config::near_cache_config(const std::string& cache_name,
908  in_memory_format memory_format)
909  : near_cache_config(name_)
910 {
911  this->in_memory_format_ = memory_format;
912 }
913 
914 near_cache_config::near_cache_config(int32_t time_to_live_seconds,
915  int32_t max_idle_seconds,
916  bool invalidate_on_change,
917  in_memory_format in_memory_format,
918  const eviction_config& evict_config)
919  : near_cache_config(name_, in_memory_format)
920 {
921  this->time_to_live_seconds_ = time_to_live_seconds;
922  this->max_idle_seconds_ = max_idle_seconds;
923  this->invalidate_on_change_ = invalidate_on_change;
924  this->eviction_config_ = evict_config;
925 }
926 
927 const std::string&
929 {
930  return name_;
931 }
932 
934 near_cache_config::set_name(const std::string& name)
935 {
936  this->name_ = name;
937  return *this;
938 }
939 
940 int32_t
942 {
943  return time_to_live_seconds_;
944 }
945 
947 near_cache_config::set_time_to_live_seconds(int32_t time_to_live_seconds)
948 {
949  this->time_to_live_seconds_ = util::Preconditions::check_not_negative(
950  time_to_live_seconds, "TTL seconds cannot be negative!");
951  return *this;
952 }
953 
954 int32_t
956 {
957  return max_idle_seconds_;
958 }
959 
962 {
963  this->max_idle_seconds_ = util::Preconditions::check_not_negative(
964  max_idle_seconds, "Max-Idle seconds cannot be negative!");
965  return *this;
966 }
967 
968 bool
970 {
971  return invalidate_on_change_;
972 }
973 
976 {
977  this->invalidate_on_change_ = invalidate_on_change;
978  return *this;
979 }
980 
981 const in_memory_format&
983 {
984  return in_memory_format_;
985 }
986 
989  const in_memory_format& in_memory_format)
990 {
991  this->in_memory_format_ = in_memory_format;
992  return *this;
993 }
994 
995 bool
997 {
998  return cache_local_entries_;
999 }
1000 
1003 {
1004  this->cache_local_entries_ = cache_local_entries;
1005  return *this;
1006 }
1007 
1009 near_cache_config::get_local_update_policy() const
1010 {
1011  return local_update_policy_;
1012 }
1013 
1014 near_cache_config&
1015 near_cache_config::set_local_update_policy(
1016  const local_update_policy& local_update_policy)
1017 {
1018  this->local_update_policy_ = local_update_policy;
1019  return *this;
1020 }
1021 
1022 eviction_config&
1024 {
1025  return eviction_config_;
1026 }
1027 
1030 {
1031  this->eviction_config_ = eviction_config;
1032  return *this;
1033 }
1034 
1035 int32_t
1036 near_cache_config::calculate_max_size(int32_t max_size)
1037 {
1038  return (max_size == 0) ? INT32_MAX
1039  : util::Preconditions::check_not_negative(
1040  max_size, "Max-size cannot be negative!");
1041 }
1042 
1043 std::ostream&
1044 operator<<(std::ostream& out, const near_cache_config& config)
1045 {
1046  out << "NearCacheConfig{"
1047  << "timeToLiveSeconds=" << config.time_to_live_seconds_
1048  << ", maxIdleSeconds=" << config.max_idle_seconds_
1049  << ", invalidateOnChange=" << config.invalidate_on_change_
1050  << ", inMemoryFormat=" << config.in_memory_format_
1051  << ", cacheLocalEntries=" << config.cache_local_entries_
1052  << ", localUpdatePolicy=" << config.local_update_policy_
1053  << config.eviction_config_;
1054  out << '}';
1055 
1056  return out;
1057 }
1058 } // namespace config
1059 
1061  : cluster_name_("dev")
1062  , redo_operation_(false)
1063  , socket_interceptor_()
1064  , executor_pool_size_(-1)
1065 {}
1066 
1068 
1070 client_config::operator=(client_config&& rhs) = default;
1071 
1074 {
1075  this->redo_operation_ = redo_operation;
1076  return *this;
1077 }
1078 
1079 bool
1081 {
1082  return redo_operation_;
1083 }
1084 
1087 {
1088  if (!load_balancer_) {
1089  auto index = std::make_shared<std::atomic<size_t>>(0);
1090  load_balancer_ = load_balancer().next([=](cluster& c) {
1091  auto members = c.get_members();
1092  if (members.empty()) {
1093  return boost::optional<member>();
1094  }
1095  auto i = index->fetch_add(1);
1096  return boost::make_optional(std::move(members[i % members.size()]));
1097  });
1098  }
1099  return *load_balancer_;
1100 }
1101 
1104 {
1105  this->load_balancer_ = std::move(load_balancer);
1106  return *this;
1107 }
1108 
1111 {
1112  return logger_config_;
1113 }
1114 
1117 {
1118  lifecycle_listeners_.emplace_back(std::move(listener));
1119  return *this;
1120 }
1121 
1124 {
1125  membership_listeners_.emplace_back(std::move(listener));
1126  return *this;
1127 }
1128 
1129 const std::vector<lifecycle_listener>&
1131 {
1132  return lifecycle_listeners_;
1133 }
1134 
1135 const std::vector<membership_listener>&
1137 {
1138  return membership_listeners_;
1139 }
1140 
1143 {
1144  this->socket_interceptor_ = std::move(interceptor);
1145  return *this;
1146 }
1147 
1148 const socket_interceptor&
1150 {
1151  return socket_interceptor_;
1152 }
1153 
1156 {
1157  return serialization_config_;
1158 }
1159 
1163 {
1164  this->serialization_config_ = serialization_config;
1165  return *this;
1166 }
1167 
1168 const std::unordered_map<std::string, std::string>&
1170 {
1171  return properties_;
1172 }
1173 
1175 client_config::set_property(const std::string& name, const std::string& value)
1176 {
1177  properties_[name] = value;
1178  return *this;
1179 }
1180 
1183  const config::reliable_topic_config& reliable_topic_config)
1184 {
1185  reliable_topic_config_map_[reliable_topic_config.get_name()] =
1186  reliable_topic_config;
1187  return *this;
1188 }
1189 
1192 {
1193  auto it = reliable_topic_config_map_.find(name);
1194  if (it != reliable_topic_config_map_.end()) {
1195  return it->second;
1196  }
1197 
1198  return reliable_topic_config_map_
1199  .emplace(name, config::reliable_topic_config{ name })
1200  .first->second;
1201 }
1202 
1204 client_config::lookup_reliable_topic_config(const std::string& name) const
1205 {
1206  auto it = reliable_topic_config_map_.find(name);
1207  if (it != reliable_topic_config_map_.end()) {
1208  return &it->second;
1209  }
1210 
1211  return nullptr;
1212 }
1213 
1214 config::client_network_config&
1216 {
1217  return network_config_;
1218 }
1219 
1222  const config::near_cache_config& near_cache_config)
1223 {
1224  near_cache_config_map_.emplace(near_cache_config.get_name(),
1225  near_cache_config);
1226  return *this;
1227 }
1228 
1230 client_config::get_near_cache_config(const std::string& name) const
1231 {
1232  auto nearCacheConfig = internal::config::ConfigUtils::lookup_by_pattern(
1233  config_pattern_matcher_, near_cache_config_map_, name);
1234  if (nearCacheConfig) {
1235  return nearCacheConfig;
1236  }
1237 
1238  auto config_it = near_cache_config_map_.find("default");
1239  if (config_it != near_cache_config_map_.end()) {
1240  return &near_cache_config_map_.find("default")->second;
1241  }
1242 
1243  // not needed for c++ client since it is always native memory
1244  // initDefaultMaxSizeForOnHeapMaps(nearCacheConfig);
1245  return nullptr;
1246 }
1247 
1250  const config::client_network_config& network_config)
1251 {
1252  this->network_config_ = network_config;
1253  return *this;
1254 }
1255 
1256 const boost::optional<std::string>&
1257 client_config::get_instance_name() const
1258 {
1259  return instance_name_;
1260 }
1261 
1262 client_config&
1263 client_config::set_instance_name(const std::string& instance_name)
1264 {
1265  client_config::instance_name_ = instance_name;
1266  return *this;
1267 }
1268 
1269 int32_t
1271 {
1272  return executor_pool_size_;
1273 }
1274 
1275 void
1276 client_config::set_executor_pool_size(int32_t executor_pool_size)
1277 {
1278  client_config::executor_pool_size_ = executor_pool_size;
1279 }
1280 
1282 client_config::get_connection_strategy_config()
1283 {
1284  return connection_strategy_config_;
1285 }
1286 
1287 client_config&
1288 client_config::set_connection_strategy_config(
1289  const config::client_connection_strategy_config& connection_strategy_config)
1290 {
1291  client_config::connection_strategy_config_ = connection_strategy_config;
1292  return *this;
1293 }
1294 
1295 const config::client_flake_id_generator_config*
1297 {
1298  std::string baseName =
1299  internal::partition::strategy::StringPartitioningStrategy::get_base_name(
1300  name);
1301  auto config = internal::config::ConfigUtils::lookup_by_pattern<
1303  config_pattern_matcher_, flake_id_generator_config_map_, baseName);
1304  if (config) {
1305  return config;
1306  }
1307  return get_flake_id_generator_config("default");
1308 }
1309 
1312 {
1313  std::string baseName =
1314  internal::partition::strategy::StringPartitioningStrategy::get_base_name(
1315  name);
1316  auto config = internal::config::ConfigUtils::lookup_by_pattern<
1318  config_pattern_matcher_, flake_id_generator_config_map_, baseName);
1319  if (config) {
1320  return config;
1321  }
1322  auto defConfig = flake_id_generator_config_map_.find("default");
1323  if (defConfig == flake_id_generator_config_map_.end()) {
1324  flake_id_generator_config_map_.emplace(
1325  "default", config::client_flake_id_generator_config("default"));
1326  }
1327  defConfig = flake_id_generator_config_map_.find("default");
1328  config::client_flake_id_generator_config new_config = defConfig->second;
1329  new_config.set_name(name);
1330  flake_id_generator_config_map_.emplace(name, std::move(new_config));
1331  return &flake_id_generator_config_map_.find(name)->second;
1332 }
1333 
1337 {
1338  flake_id_generator_config_map_.emplace(config.get_name(), config);
1339  return *this;
1340 }
1341 
1342 const std::string&
1344 {
1345  return cluster_name_;
1346 }
1347 
1349 client_config::set_cluster_name(const std::string& cluster_name)
1350 {
1351  cluster_name_ = cluster_name;
1352  return *this;
1353 }
1354 
1355 const std::unordered_set<std::string>&
1356 client_config::get_labels() const
1357 {
1358  return labels_;
1359 }
1360 
1361 client_config&
1362 client_config::set_labels(const std::unordered_set<std::string>& labels)
1363 {
1364  labels_ = labels;
1365  return *this;
1366 }
1367 
1368 client_config&
1369 client_config::add_label(const std::string& label)
1370 {
1371  labels_.insert(label);
1372  return *this;
1373 }
1374 
1375 client_config&
1377 {
1378  backup_acks_enabled_ = enabled;
1379  return *this;
1380 }
1381 
1382 bool
1384 {
1385  return backup_acks_enabled_;
1386 }
1387 
1388 const std::shared_ptr<security::credentials>&
1389 client_config::get_credentials() const
1390 {
1391  return credentials_;
1392 }
1393 
1394 client_config&
1396  const std::shared_ptr<security::credentials>& credential)
1397 {
1398  credentials_ = credential;
1399  return *this;
1400 }
1401 
1402 namespace security {
1403 username_password_credentials::username_password_credentials(
1404  const std::string& name,
1405  const std::string& password)
1406  : credentials(name)
1407  , password_(password)
1408 {}
1409 
1410 const std::string&
1411 username_password_credentials::password() const
1412 {
1413  return password_;
1414 }
1415 
1416 credentials::credential_type
1417 username_password_credentials::type() const
1418 {
1419  return credentials::credential_type::username_password;
1420 }
1421 
1422 const std::vector<byte>&
1423 token_credentials::token() const
1424 {
1425  return token_;
1426 }
1427 
1428 credentials::credential_type
1429 token_credentials::type() const
1430 {
1431  return credentials::credential_type::token;
1432 }
1433 
1434 token_credentials::token_credentials(const std::vector<byte>& token)
1435  : credentials(token.empty() ? "<empty>" : "<token>")
1436  , token_(token)
1437 {}
1438 
1439 credentials::~credentials() {}
1440 
1441 const std::string&
1442 credentials::name() const
1443 {
1444  return name_;
1445 }
1446 
1447 credentials::credentials(const std::string& name)
1448  : name_(name)
1449 {}
1450 } // namespace security
1451 } // namespace client
1452 } // namespace hazelcast
Represents an address of a client or member in the cluster.
Definition: address.h:37
hazelcast_client configuration class.
client_config & set_serialization_config(serialization_config const &serialization_config)
SerializationConfig is used to.
Definition: config.cpp:1161
serialization_config & get_serialization_config()
Definition: config.cpp:1155
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:1191
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:1142
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:1073
void set_executor_pool_size(int32_t executor_pool_size)
Sets Client side Executor pool size.
Definition: config.cpp:1276
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:1230
client_config & add_listener(lifecycle_listener &&listener)
Adds a listener to configuration to be registered when hazelcast_client starts.
Definition: config.cpp:1116
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:1395
const std::unordered_map< std::string, std::string > & get_properties() const
Gets a reference to properties map.
Definition: config.cpp:1169
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:1149
client_config()
Constructor with default values.
Definition: config.cpp:1060
config::client_network_config & get_network_config()
Gets {}.
Definition: config.cpp:1215
int32_t get_executor_pool_size() const
Pool size for internal ExecutorService which handles responses etc.
Definition: config.cpp:1270
client_config & add_reliable_topic_config(const config::reliable_topic_config &reliable_topic_config)
Adds a ClientReliableTopicConfig.
Definition: config.cpp:1182
bool is_redo_operation() const
see setRedoOperation returns redoOperation
Definition: config.cpp:1080
const std::vector< membership_listener > & get_membership_listeners() const
Returns registered membershipListeners.
Definition: config.cpp:1136
client_config & add_flake_id_generator_config(const config::client_flake_id_generator_config &config)
Adds a flake ID generator configuration.
Definition: config.cpp:1335
const std::string & get_cluster_name() const
Returns the configured cluster name.
Definition: config.cpp:1343
config::logger_config & get_logger_config()
Definition: config.cpp:1110
bool backup_acks_enabled()
Note that backup acks to client can be enabled only for smart client.
Definition: config.cpp:1383
client_config & set_load_balancer(load_balancer &&load_balancer)
Used to distribute the operations to multiple connections.
Definition: config.cpp:1103
load_balancer & get_load_balancer()
Used to distribute the operations to multiple Endpoints.
Definition: config.cpp:1086
client_config & set_network_config(const config::client_network_config &network_config)
Sets {}.
Definition: config.cpp:1249
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:1311
client_config & add_near_cache_config(const config::near_cache_config &near_cache_config)
Helper method to add a new NearCacheConfig.
Definition: config.cpp:1221
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:1296
client_config & set_property(const std::string &name, const std::string &value)
Sets the value of a named property.
Definition: config.cpp:1175
const std::vector< lifecycle_listener > & get_lifecycle_listeners() const
Definition: config.cpp:1130
Hazelcast cluster interface.
Definition: cluster.h:37
std::vector< member > get_members()
Set of current members of the cluster.
Definition: cluster.cpp:39
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:595
const std::string & get_tag_key() const
Gets the tag key.
Definition: config.cpp:686
const std::string & get_security_group_name() const
Gets the security group name.
Definition: config.cpp:673
const std::string & get_region() const
Gets the region where the EC2 instances running the Hazelcast members will be running.
Definition: config.cpp:623
const std::string & get_host_header() const
Gets the host header; the address where the EC2 API can be found.
Definition: config.cpp:637
client_aws_config & set_secret_key(const std::string &secret_key)
Sets the secret key to access AWS.
Definition: config.cpp:615
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:643
client_aws_config & set_tag_key(const std::string &tag_key)
Sets the tag key.
Definition: config.cpp:679
client_aws_config & set_iam_role(const std::string &iam_role)
Sets the tag value.
Definition: config.cpp:711
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:724
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:629
bool is_enabled() const
Checks if the aws join mechanism is enabled.
Definition: config.cpp:659
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:718
const std::string & get_secret_key() const
Gets the secret key to access AWS.
Definition: config.cpp:609
client_aws_config & set_enabled(bool enabled)
Enables or disables the aws join mechanism.
Definition: config.cpp:651
const std::string & get_tag_value() const
Gets the tag value.
Definition: config.cpp:699
client_aws_config & set_access_key(const std::string &access_key)
Sets the access key to access AWS.
Definition: config.cpp:601
client_aws_config & set_tag_value(const std::string &tag_value)
Sets the tag value.
Definition: config.cpp:692
const std::string & get_iam_role() const
Gets the iamRole name.
Definition: config.cpp:705
client_aws_config & set_security_group_name(const std::string &security_group_name)
Sets the security group name.
Definition: config.cpp:665
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:456
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:470
client_connection_strategy_config & set_async_start(bool async_start)
Set true for non blocking client_config &).
Definition: config.cpp:449
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:464
bool is_async_start() const
Client instance creation won't block on {} if this value is true.
Definition: config.cpp:443
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:187
client_flake_id_generator_config & set_name(const std::string &n)
Sets the name or name pattern for this config.
Definition: config.cpp:193
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:391
bool is_smart_routing() const
See client_network_config::setSmartRouting(boolean) for details.
Definition: config.cpp:364
std::vector< address > get_addresses() const
Returns the list of candidate addresses that client will use to establish initial connection.
Definition: config.cpp:377
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:383
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:398
client_network_config & set_smart_routing(bool smart_routing)
If.
Definition: config.cpp:370
client_network_config & set_connection_timeout(const std::chrono::milliseconds &timeout)
Definition: config.cpp:411
client_aws_config & get_aws_config()
Returns the current client_aws_config.
Definition: config.cpp:352
ssl_config & get_ssl_config()
Returns the current \ssl_config .
Definition: config.cpp:325
std::chrono::milliseconds get_connection_timeout() const
Connection timeout value for connecting to a member server.
Definition: config.cpp:338
cloud_config & get_cloud_config()
Returns the current cloud_config.
Definition: config.cpp:358
client_network_config & set_aws_config(const client_aws_config &client_aws_config)
Sets configuration to connect nodes in aws environment.
Definition: config.cpp:344
client_network_config & set_ssl_config(const config::ssl_config &config)
Sets the ssl_config.
Definition: config.cpp:331
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:745
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:1002
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:988
near_cache_config & set_eviction_config(const eviction_config &eviction_config)
Sets the eviction configuration.
Definition: config.cpp:1029
const in_memory_format & get_in_memory_format() const
Gets the data type used to store entries.
Definition: config.cpp:982
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:941
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:947
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:975
const std::string & get_name() const
Gets the name of the Near Cache.
Definition: config.cpp:928
eviction_config & get_eviction_config()
The eviction configuration.
Definition: config.cpp:1023
bool is_cache_local_entries() const
If true, cache local entries also.
Definition: config.cpp:996
bool is_invalidate_on_change() const
True to evict the cached entries if the entries are changed (updated or removed).
Definition: config.cpp:969
near_cache_config & set_name(const std::string &name)
Sets the name of the Near Cache.
Definition: config.cpp:934
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:955
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:961
const std::string & get_name() const
Gets the name of the reliable topic.
Definition: config.cpp:489
reliable_topic_config & set_read_batch_size(int batch_size)
Sets the read batch size.
Definition: config.cpp:501
int get_read_batch_size() const
Gets the maximum number of items to read in a batch.
Definition: config.cpp:495
socket_options & set_tcp_no_delay(bool tcp_no_delay)
Enable/disable TCP_NODELAY socket option.
Definition: config.cpp:529
socket_options & set_reuse_address(bool reuse_address)
Enable/disable the SO_REUSEADDR socket option.
Definition: config.cpp:555
socket_options & set_linger_seconds(int linger_seconds)
Enable/disable SO_LINGER with the specified linger time in seconds.
Definition: config.cpp:568
bool is_keep_alive() const
SO_KEEPALIVE socket option.
Definition: config.cpp:536
socket_options & set_keep_alive(bool keep_alive)
Enable/disable SO_KEEPALIVE socket option.
Definition: config.cpp:542
bool is_reuse_address() const
SO_REUSEADDR socket option.
Definition: config.cpp:549
bool is_tcp_no_delay() const
TCP_NODELAY socket option.
Definition: config.cpp:523
int get_buffer_size_in_bytes() const
If set to 0 or less, then it is not set on the socket.
Definition: config.cpp:575
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:581
int get_linger_seconds() const
Gets SO_LINGER with the specified linger time in seconds.
Definition: config.cpp:562
Contains configuration parameters for ssl related behaviour.
Definition: ssl_config.h:67
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:41
load_balancer & next(Handler &&h) &
The function returns the next member to route to.
Definition: load_balancer.h:73
SerializationConfig is used to.
serialization_config & set_portable_version(int v)
Definition: config.cpp:52
serialization_config & set_global_serializer(const std::shared_ptr< serialization::global_serializer > &global_serializer)
Definition: config.cpp:65
serialization_config & set_byte_order(boost::endian::order byte_order)
Definition: config.cpp:73
int get_portable_version() const
Portable version will be used to differentiate two same class that have changes on it ,...
Definition: config.cpp:46
serialization_config()
Constructor default value of version is zero.
Definition: config.cpp:41
boost::endian::order get_byte_order() const
Definition: config.cpp:80
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:33
bitmap_index_options()
Constructs a new bitmap index options instance with all options set to default values.
Definition: config.cpp:806
static const unique_key_transformation DEFAULT_TRANSFORMATION
The default for \transformation.
Definition: index_config.h:81
static const std::string DEFAULT_KEY
The default for \key.
Definition: index_config.h:76
index_type type
Type of the index.
Definition: index_config.h:106