Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HazelcastOptions"/> class.
        /// </summary>
        private HazelcastOptions(HazelcastOptions other)
        {
            ClientName     = other.ClientName;
            ClusterName    = other.ClusterName;
            Subscribers    = new List <IHazelcastClientEventSubscriber>(other.Subscribers);
            Labels         = new HashSet <string>(other.Labels);
            LoggerFactory  = other.LoggerFactory.Clone();
            PatternMatcher = other.PatternMatcher;

            ((IClusterOptions)this).ClientNamePrefix = ((IClusterOptions)other).ClientNamePrefix;

            Core           = other.Core.Clone();
            Heartbeat      = other.Heartbeat.Clone();
            Networking     = other.Networking.Clone();
            Authentication = other.Authentication.Clone();
            LoadBalancer   = other.LoadBalancer.Clone();
            Serialization  = other.Serialization.Clone();
            Messaging      = other.Messaging.Clone();
            Events         = other.Events.Clone();
            Metrics        = other.Metrics.Clone();

            NearCache  = other.NearCache.Clone();
            NearCaches = other.NearCaches.Select(kvp
                                                 => new KeyValuePair <string, NearCacheOptions>(kvp.Key, kvp.Value.Clone()))
                         .ToDictionary();
        }
        private void Configure(IConfiguration configuration, HazelcastOptions options)
        {
            if (_configureActions == null)
            {
                return;
            }

            foreach (var configure in _configureActions)
            {
                configure(configuration, options);
            }
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HazelcastOptions"/> class.
        /// </summary>
        private HazelcastOptions(HazelcastOptions other)
        {
            ClientName  = other.ClientName;
            ClusterName = other.ClusterName;
            Subscribers = new List <IHazelcastClientEventSubscriber>(other.Subscribers);
            Labels      = new HashSet <string>(other.Labels);

            Core           = other.Core.Clone();
            Logging        = other.Logging.Clone();
            Heartbeat      = other.Heartbeat.Clone();
            Networking     = other.Networking.Clone();
            Authentication = other.Authentication.Clone();
            LoadBalancing  = other.LoadBalancing.Clone();
            Serialization  = other.Serialization.Clone();
            NearCache      = other.NearCache.Clone();
            Messaging      = other.Messaging.Clone();
        }
        // build options, optionally binding alternate keys
        private static HazelcastOptions Build(IConfiguration configuration, Action <IConfiguration, HazelcastOptions> configure, string altKey)
        {
            // must HzBind here and not simply Bind because we use our custom
            // binder which handles more situations such as ignoring and/or
            // renaming properties

            var options = new HazelcastOptions();

            configuration.HzBind(Hazelcast, options);

            // FIXME ??
            //if (altKey != null)
            //    configuration.HzBind(altKey, options);

            configure?.Invoke(configuration, options);
            return(options);
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HazelcastClient"/> class.
        /// </summary>
        /// <param name="options">The client configuration.</param>
        /// <param name="cluster">A cluster.</param>
        /// <param name="serializationService">A serialization service.</param>
        /// <param name="loggerFactory">A logger factory.</param>
        public HazelcastClient(HazelcastOptions options, Cluster cluster, SerializationService serializationService, ILoggerFactory loggerFactory)
        {
            _options             = options ?? throw new ArgumentNullException(nameof(options));
            Cluster              = cluster ?? throw new ArgumentNullException(nameof(cluster));
            SerializationService = serializationService ?? throw new ArgumentNullException(nameof(serializationService));
            _loggerFactory       = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
            _logger              = loggerFactory.CreateLogger <IHazelcastClient>();

            _distributedOjects = new DistributedObjectFactory(Cluster, serializationService, loggerFactory);
            _nearCacheManager  = new NearCacheManager(cluster, serializationService, loggerFactory, options.NearCache);

            if (options.Metrics.Enabled)
            {
                _metricsPublisher = new MetricsPublisher(cluster, options.Metrics, loggerFactory);
                _metricsPublisher.AddSource(new ClientMetricSource(cluster, loggerFactory));
                _metricsPublisher.AddSource(_nearCacheManager);
            }

            // wire components
            WireComponents();
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HazelcastClient"/> class.
        /// </summary>
        /// <param name="options">The client configuration.</param>
        /// <param name="cluster">A cluster.</param>
        /// <param name="serializationService">A serialization service.</param>
        /// <param name="loggerFactory">A logger factory.</param>
        public HazelcastClient(HazelcastOptions options, Cluster cluster, ISerializationService serializationService, ILoggerFactory loggerFactory)
        {
            _options             = options ?? throw new ArgumentNullException(nameof(options));
            Cluster              = cluster ?? throw new ArgumentNullException(nameof(cluster));
            SerializationService = serializationService ?? throw new ArgumentNullException(nameof(serializationService));
            _loggerFactory       = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
            _logger              = loggerFactory.CreateLogger <IHazelcastClient>();

            _distributedObjectFactory = new DistributedObjectFactory(Cluster, serializationService, loggerFactory);
            Cluster.Connections.OnConnectingToNewCluster = cancellationToken => _distributedObjectFactory.CreateAllAsync(cancellationToken);

            _nearCacheManager = new NearCacheManager(cluster, serializationService, loggerFactory, options.NearCache);

            // wire events
            // this way, the cluster does not need to know about the hazelcast client,
            // and we don't have circular dependencies everywhere - cleaner
            cluster.ClusterEvents.OnObjectLifecycleEvent = OnObjectLifecycleEvent;
            cluster.ClusterEvents.OnPartitionLost        = OnPartitionLost;
            cluster.ClusterEvents.OnMemberLifecycleEvent = OnMemberLifecycleEvent;
            cluster.ClusterEvents.OnClientLifecycleEvent = OnClientLifecycleEvent;
            cluster.ClusterEvents.OnPartitionsUpdated    = OnPartitionsUpdated;
            cluster.ClusterEvents.OnConnectionAdded      = OnConnectionAdded;
            cluster.ClusterEvents.OnConnectionRemoved    = OnConnectionRemoved;
            cluster.ClusterEvents.OnConnectionRemoved    = OnConnectionRemoved;

            cluster.Connections.OnFirstConnection = async cancellationToken =>
            {
                foreach (var subscriber in options.Subscribers)
                {
                    await subscriber.SubscribeAsync(this, cancellationToken).CAF();
                }
            };

            // every async operations using this client will need a proper async context
            AsyncContext.Ensure();
        }
 /// <summary>
 /// Builds the options.
 /// </summary>
 /// <returns>The options.</returns>
 public HazelcastOptions Build()
 {
     return(HazelcastOptions.Build(Setup, Configure, _altKey));
 }
Example #8
0
 /// <summary>
 /// Builds the options.
 /// </summary>
 /// <returns>The options.</returns>
 public HazelcastOptions Build()
 {
     return(HazelcastOptions.Build(_args, _keyValues, _optionsFilePath, _optionsFileName, _environmentName, ConfigureActions));
 }
Example #9
0
 /// <summary>
 /// Starts a new <see cref="IHazelcastClient"/> instance with the automatic options.
 /// </summary>
 /// <param name="cancellationToken">An optional cancellation token.</param>
 /// <returns>A new <see cref="IHazelcastClient"/> instance.</returns>
 /// <remarks>
 /// <para>Options are built via HazelcastOptions.Build method.</para>
 /// </remarks>
 public static ValueTask <IHazelcastClient> StartNewClientAsync(CancellationToken cancellationToken = default)
 => StartNewClientAsync(HazelcastOptions.Build(), cancellationToken);
Example #10
0
 /// <summary>
 /// Creates an <see cref="IHazelcastClient"/> instance with the automatic options.
 /// </summary>
 /// <returns>A new <see cref="IHazelcastClient"/> instance.</returns>
 /// <remarks>
 /// <para>Options are built via <see cref="HazelcastOptions.Build(string[], System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{string,string}}, string, string, string, System.Action{Microsoft.Extensions.Configuration.IConfiguration,Hazelcast.HazelcastOptions}(Microsoft.Extensions.Configuration.IConfiguration,Hazelcast.HazelcastOptions))"/> method.</para>
 /// </remarks>
 public static IHazelcastClient CreateClient()
 => CreateClient(HazelcastOptions.Build());
        /// <summary>
        /// Initializes a new instance of the <see cref="HazelcastClient"/> class.
        /// </summary>
        /// <param name="options">The client configuration.</param>
        /// <param name="cluster">A cluster.</param>
        /// <param name="serializationService">A serialization service.</param>
        /// <param name="loggerFactory">A logger factory.</param>
        public HazelcastClient(HazelcastOptions options, Cluster cluster, SerializationService serializationService, ILoggerFactory loggerFactory)
        {
            _options             = options ?? throw new ArgumentNullException(nameof(options));
            Cluster              = cluster ?? throw new ArgumentNullException(nameof(cluster));
            SerializationService = serializationService ?? throw new ArgumentNullException(nameof(serializationService));
            _loggerFactory       = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
            _logger              = loggerFactory.CreateLogger <IHazelcastClient>();

            _distributedOjects = new DistributedObjectFactory(Cluster, serializationService, loggerFactory);
            _nearCacheManager  = new NearCacheManager(cluster, serializationService, loggerFactory, options.NearCache);

            // wire events - clean

            // NOTE: each event that is a Func<..., ValueTask> *must* be invoked
            // through the special FuncExtensions.AwaitEach() method, see notes in
            // FuncExtensions.cs

            // when an object is created/destroyed, OnObject... triggers the user-level events
            cluster.ClusterEvents.ObjectCreated   = OnObjectCreated;
            cluster.ClusterEvents.ObjectDestroyed = OnObjectDestroyed;

            // when client/cluster state changes, OnStateChanged triggers user-level events
            cluster.State.StateChanged += OnStateChanged;

            // when partitions are updated, OnPartitionUpdated triggers the user-level event
            cluster.Events.PartitionsUpdated += OnPartitionsUpdated;

            // when a partition is lost, OnPartitionLost triggers the user-level event
            cluster.ClusterEvents.PartitionLost = OnPartitionLost;

            // when members are updated, Connections.OnMembersUpdated queues the added
            // members so that a connection is opened to each of them.
            cluster.Events.MembersUpdated += cluster.Connections.OnMembersUpdated;

            // when members are updated, OnMembersUpdated triggers the user-level event
            cluster.Events.MembersUpdated += OnMembersUpdated;

            // when a connection is created, Events.OnConnectionCreated wires the connection
            // ReceivedEvent event to Events.OnReceivedEvent, in order to handle events
            cluster.Connections.ConnectionCreated += cluster.Events.OnConnectionCreated;

            // when connecting to a new cluster, DistributedObjects.OnConnectingToNewCluster
            // re-creates all the known distributed object so far on the new cluster
            cluster.Connections.ConnectionOpened += _distributedOjects.OnConnectionOpened;

            // when a connection is added, OnConnectionOpened triggers the user-level event
            // and, if it is the first connection, subscribes to events according to the
            // subscribers defined in options.
            cluster.Connections.ConnectionOpened += OnConnectionOpened;

            // when a connection is opened, Events.OnConnectionOpened ensures that the
            // cluster connection (handling member/partition views) is set, and installs
            // subscriptions on this new connection.
            cluster.Connections.ConnectionOpened += cluster.Events.OnConnectionOpened;

            // when a connection is closed, client.OnConnectionClosed triggers the user-level event
            cluster.Connections.ConnectionClosed += OnConnectionClosed;

            // when a connection is closed, Events.OnConnectionClosed clears subscriptions
            // (cannot unsubscribe since the connection is closed) and ensures that the
            // cluster connection (handling member/partition views) is set.
            cluster.Connections.ConnectionClosed += cluster.Events.OnConnectionClosed;
        }