/**
         * <summary>
         * This method will create a client with default configuration. Note that this method expects that
         * first node will bind rest binary protocol on default port. It also expects that partitioned cache is
         * configured in grid.</summary>
         *
         * <param name="passcode">Passcode.</param>
         * <returns>Client instance.</returns>
         * <exception cref="GridClientException">If client could not be created.</exception>
         */
        private static IGridClient CreateClient(String passcode)
        {
            var partitioned = new GridClientDataConfiguration();

            // Set remote cache name.
            partitioned.Name = "partitioned";

            // Set client partitioned affinity for this cache.
            partitioned.Affinity = new GridClientPartitionedAffinity();

            var cc = new GridClientConfiguration();

            cc.DataConfigurations.Add(partitioned);

            // Point client to a local node.
            cc.Servers.Add(ServerAddress + ":" + GridClientConfiguration.DefaultTcpPort);

            // Set passcode credentials.
            cc.Credentials = passcode;

            // If we use ssl, set appropriate key- and trust-store.
            if (UseSsl) {
                var sslCtx = new GridClientSslContext();

                sslCtx.ClientCertificates.Add(new X509Certificate2("cert\\client.pfx", "123456"));

                cc.SslContext = sslCtx;
            }

            return GridClientFactory.Start(cc);
        }
Beispiel #2
0
        /**
         * <summary>
         * This method will create a client with default configuration. Note that this method expects that
         * first node will bind rest binary protocol on default port.</summary>
         *
         * <returns>Client instance.</returns>
         * <exception cref="GridClientException">If client could not be created.</exception>
         */
        private static IGridClient CreateClient()
        {
            var cfg = new GridClientConfiguration();

            // Point client to a local node. Note that this server is only used
            // for initial connection. After having established initial connection
            // client will make decisions which grid node to use based on collocation
            // with key affinity or load balancing.
            cfg.Servers.Add(ServerAddress + ':' + GridClientConfiguration.DefaultTcpPort);

            return(GridClientFactory.Start(cfg));
        }
        /**
         * <summary>
         * This method will create a client with default configuration. Note that this method expects that
         * first node will bind rest binary protocol on default port. It also expects that partitioned cache is
         * configured in grid.</summary>
         *
         * <returns>Client instance.</returns>
         * <exception cref="GridClientException">If client could not be created.</exception>
         */
        private static IGridClient CreateClient()
        {
            var cacheCfg = new GridClientDataConfiguration();

            // Set remote cache name.
            cacheCfg.Name = "partitioned";

            // Set client partitioned affinity for this cache.
            cacheCfg.Affinity = new GridClientPartitionAffinity();

            var cfg = new GridClientConfiguration();

            cfg.DataConfigurations.Add(cacheCfg);

            // Point client to a local node. Note that this server is only used
            // for initial connection. After having established initial connection
            // client will make decisions which grid node to use based on collocation
            // with key affinity or load balancing.
            cfg.Servers.Add(ServerAddress + ':' + GridClientConfiguration.DefaultTcpPort);

            return(GridClientFactory.Start(cfg));
        }
Beispiel #4
0
        /**
         * <summary>
         * Creates a new client based on a given configuration.</summary>
         *
         * <param name="id">Client identifier.</param>
         * <param name="cfg0">Client configuration.</param>
         * <exception cref="GridClientException">If client configuration is incorrect.</exception>
         * <exception cref="GridClientServerUnreachableException">If none of the servers specified in configuration can be reached.</exception>
         */
        public GridClientImpl(Guid id, IGridClientConfiguration cfg0)
        {
            Id = id;

            cfg = new GridClientConfiguration(cfg0);

            ICollection<IPEndPoint> srvs = new List<IPEndPoint>(cfg.Servers.Count);

            foreach (String srvStr in cfg.Servers) {
                String[] split = srvStr.Split(":".ToCharArray());

                if (split.Length != 2)
                    throw new GridClientException("Failed to create client (invalid endpoint format, expected 'IPv4:Port'): " + srvStr);

                IPAddress addr;

                if (!IPAddress.TryParse(split[0], out addr))
                    throw new GridClientException("Failed to create client (invalid address format): " + srvStr);

                int port;

                if (!int.TryParse(split[1], out port))
                    throw new GridClientException("Failed to create client (invalid port format): " + srvStr);

                srvs.Add(new IPEndPoint(addr, port));
            }

            top = new GridClientTopology(id, cfg.IsTopologyCacheEnabled);

            Action<Object> addTopLsnr = o => {
                var lsnr = o as IGridClientTopologyListener;

                if (lsnr != null)
                    top.AddTopologyListener(lsnr);
            };

            // Add to topology as listeners.
            foreach (IGridClientDataConfiguration dataCfg in cfg.DataConfigurations)
                addTopLsnr(dataCfg.Affinity);

            addTopLsnr(cfg.Balancer);
            addTopLsnr(topUpdateBalancer);

            connMgr = new GridClientConnectionManager(Id, top, cfg.Credentials, cfg.Protocol,
                cfg.SslContext, cfg.ConnectTimeout);

            int retries = 3;

            while (true) {
                IGridClientConnection conn = null;

                try {
                    // Create connection to a server from the list of endpoints.
                    conn = connMgr.connection(srvs);

                    // Request topology at start to determine which node we connected to.
                    // Also this request validates TCP connection is alive.
                    conn.Topology(false, false).WaitDone();

                    break;
                }
                catch (GridClientAuthenticationException) {
                    if (conn != null)
                        conn.Close(false);

                    top.Dispose();

                    throw;
                }
                catch (GridClientException e) {
                    if (conn != null)
                        conn.Close(false);

                    if (retries-- <= 0) {
                        top.Dispose();

                        throw new GridClientException("Failed to update grid topology.", e);
                    }
                }
            }

            IDictionary<String, GridClientCacheMode> overallCaches = new GridClientNullDictionary<String, GridClientCacheMode>();

            // Topology is now updated, so we can identify current connection.
            foreach (GridClientNodeImpl node in top.Nodes())
                foreach (KeyValuePair<String, GridClientCacheMode> pair in node.Caches)
                    overallCaches[pair.Key] = pair.Value;

            foreach (KeyValuePair<String, GridClientCacheMode> entry in overallCaches)
                if (Affinity(entry.Key) is GridClientPartitionedAffinity && entry.Value != GridClientCacheMode.Partitioned)
                    Dbg.WriteLine(typeof(GridClientPartitionedAffinity) + " is used for a cache configured " +
                        "for non-partitioned mode [cacheName=" + entry.Key + ", cacheMode=" + entry.Value + ']');

            idleCheckThread = new Thread(checkIdle);

            idleCheckThread.Name = "grid-check-idle-worker--client#" + id;

            //Dbg.WriteLine("Start thread: " + idleCheckThread.Name);

            idleCheckThread.Start();

            topUpdateThread = new Thread(updateTopology);

            topUpdateThread.Name = "grid-topology-update-worker--client#" + id;

            //Dbg.WriteLine("Start thread: " + topUpdateThread.Name);

            topUpdateThread.Start();

            _compute = new GridClientComputeImpl(this, null, null, cfg.Balancer);
        }
Beispiel #5
0
        /**
         * <summary>
         * Creates a new client based on a given configuration.</summary>
         *
         * <param name="id">Client identifier.</param>
         * <param name="cfg0">Client configuration.</param>
         * <exception cref="GridClientException">If client configuration is incorrect.</exception>
         * <exception cref="GridClientServerUnreachableException">If none of the servers specified in configuration can be reached.</exception>
         */
        public GridClientImpl(Guid id, IGridClientConfiguration cfg0)
        {
            Id = id;

            cfg = new GridClientConfiguration(cfg0);

            ICollection<IPEndPoint> srvs = ParseServers(cfg.Servers);
            ICollection<IPEndPoint> routers = ParseServers(cfg.Routers);

            top = new GridClientTopology(id, cfg.IsTopologyCacheEnabled);

            Action<Object> addTopLsnr = o => {
                var lsnr = o as IGridClientTopologyListener;

                if (lsnr != null)
                    top.AddTopologyListener(lsnr);
            };

            // Add to topology as listeners.
            foreach (IGridClientDataConfiguration dataCfg in cfg.DataConfigurations)
                addTopLsnr(dataCfg.Affinity);

            addTopLsnr(cfg.Balancer);
            addTopLsnr(topUpdateBalancer);

            if (srvs.Count == 0)
                // Use routers for initial connection.
                srvs = routers;
            else
                // Disable routers for connection manager.
                routers = new HashSet<IPEndPoint>();

            connMgr = new GridClientConnectionManager(Id, top, routers, cfg.Credentials, cfg.Protocol, cfg.SslContext, cfg.ConnectTimeout);

            int retries = 3;

            while (true) {
                IGridClientConnection conn = null;

                try {
                    // Create connection to a server from the list of endpoints.
                    conn = connMgr.connection(srvs);

                    // Request topology at start to determine which node we connected to.
                    // Also this request validates TCP connection is alive.
                    conn.Topology(false, false, Guid.Empty).WaitDone();

                    break;
                }
                catch (GridClientAuthenticationException) {
                    if (conn != null)
                        conn.Close(false);

                    top.Dispose();

                    throw;
                }
                catch (GridClientException e) {
                    if (conn != null)
                        conn.Close(false);

                    if (retries-- <= 0) {
                        top.Dispose();

                        throw new GridClientException("Failed to update grid topology.", e);
                    }
                }
            }

            IDictionary<String, GridClientCacheMode> overallCaches = new GridClientNullDictionary<String, GridClientCacheMode>();

            // Topology is now updated, so we can identify current connection.
            foreach (GridClientNodeImpl node in top.Nodes())
                foreach (KeyValuePair<String, GridClientCacheMode> pair in node.Caches)
                    overallCaches[pair.Key] = pair.Value;

            foreach (KeyValuePair<String, GridClientCacheMode> entry in overallCaches)
                if (Affinity(entry.Key) is GridClientPartitionAffinity && entry.Value != GridClientCacheMode.Partitioned)
                    Dbg.WriteLine(typeof(GridClientPartitionAffinity) + " is used for a cache configured " +
                        "for non-partitioned mode [cacheName=" + entry.Key + ", cacheMode=" + entry.Value + ']');

            idleCheckThread = new Thread(checkIdle);

            idleCheckThread.Name = "grid-check-idle-worker--client#" + id;

            idleCheckThread.Start();

            topUpdateThread = new Thread(updateTopology);

            topUpdateThread.Name = "grid-topology-update-worker--client#" + id;

            topUpdateThread.Start();

            _compute = new GridClientComputeImpl(this, null, null, cfg.Balancer);

            Dbg.WriteLine("Client started. Id: " + Id);
        }