Esempio n. 1
0
 /** <summary>Constructs client node bean.</summary> */
 public GridClientNodeBean()
 {
     InternalAddresses = new HashSet<String>();
     ExternalAddresses = new HashSet<String>();
     Metrics = new Dictionary<String, Object>();
     Attributes = new Dictionary<String, Object>();
     Caches = new GridClientNullDictionary<String, String>();
 }
Esempio n. 2
0
 /** <summary>Constructs client node bean.</summary> */
 public GridClientNodeBean()
 {
     TcpAddresses = new HashSet<String>();
     TcpHostNames = new HashSet<String>();
     JettyAddresses = new HashSet<String>();
     JettyHostNames = new HashSet<String>();
     Metrics = new Dictionary<String, Object>();
     Attributes = new Dictionary<String, Object>();
     Caches = new GridClientNullDictionary<String, String>();
 }
Esempio n. 3
0
 /** <summary>Constructs client node bean.</summary> */
 public GridClientNodeBean()
 {
     TcpAddresses   = new HashSet <String>();
     TcpHostNames   = new HashSet <String>();
     JettyAddresses = new HashSet <String>();
     JettyHostNames = new HashSet <String>();
     Metrics        = new Dictionary <String, Object>();
     Attributes     = new Dictionary <String, Object>();
     Caches         = new GridClientNullDictionary <String, String>();
 }
Esempio n. 4
0
 /**
  * <summary>
  * Constructs grid client node.</summary>
  *
  * <param name="nodeId">Node ID.</param>
  */
 public GridClientNodeImpl(Guid nodeId)
 {
     Id             = nodeId;
     TcpAddresses   = new List <String>();
     TcpHostNames   = new List <String>();
     JettyAddresses = new List <String>();
     JettyHostNames = new List <String>();
     Attributes     = new Dictionary <String, Object>();
     Metrics        = null;
     Caches         = new GridClientNullDictionary <String, GridClientCacheMode>();
 }
Esempio n. 5
0
        /**
         * <summary>
         * Convert the string representation of the cache mode names to an equivalent enumerated objects.</summary>
         *
         * <param name="rawCaches">Map of raw cache modes.</param>
         * <returns>Converted cache modes.</returns>
         */
        protected IDictionary <String, GridClientCacheMode> parseCacheModes(IDictionary <String, String> rawCaches)
        {
            var caches = new GridClientNullDictionary <String, GridClientCacheMode>();

            foreach (KeyValuePair <String, String> e in rawCaches)
            {
                try {
                    caches.Add(e.Key, parseCacheMode(e.Value));
                }
                catch (ArgumentException x) {
                    Dbg.WriteLine("Invalid cache mode received from remote node (will ignore)" +
                                  " [srv={0}, cacheName={1}, cacheMode={2}, e={3}]", ServerAddress, e.Key, e.Value, x);
                }
            }

            return(caches);
        }
Esempio n. 6
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);
        }
        /**
         * <summary>
         * Creates client node impl from json object representation.</summary>
         *
         * <param name="json">JSONObject (possibly JSONNull).</param>
         * <returns>Converted client node.</returns>
         */
        private GridClientNodeImpl JsonBeanToNode(IDictionary <String, Object> json)
        {
            Guid nodeId = Guid.Parse(json["nodeId"].ToString());

            GridClientNodeImpl node = new GridClientNodeImpl(nodeId);

            node.TcpAddresses.AddAll <String>(AsList <String>(json["tcpAddresses"]));
            node.TcpHostNames.AddAll <String>(AsList <String>(json["tcpHostNames"]));
            node.JettyAddresses.AddAll <String>(AsList <String>(json["jettyAddresses"]));
            node.JettyHostNames.AddAll <String>(AsList <String>(json["jettyHostNames"]));
            node.TcpPort      = (int)json["tcpPort"];
            node.HttpPort     = (int)json["jettyPort"];
            node.ConsistentId = json["consistentId"];
            node.ReplicaCount = (int)json["replicaCount"];

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

            if (json.ContainsKey("caches"))
            {
                IDictionary <String, String> rawCaches = AsMap <String, String>(json["caches"]);

                Object dfltCacheMode;

                if (json.TryGetValue("defaultCacheMode", out dfltCacheMode))
                {
                    String mode = dfltCacheMode as String;

                    if (!String.IsNullOrEmpty(mode))
                    {
                        rawCaches = rawCaches.ToNullable();

                        rawCaches.Add(null, mode);
                    }
                }

                caches = parseCacheModes(rawCaches);
            }

            if (caches.Count > 0)
            {
                node.Caches.AddAll <KeyValuePair <String, GridClientCacheMode> >(caches);
            }

            Object o;

            if (json.TryGetValue("attributes", out o) && o != null)
            {
                var map = AsMap <String, Object>(o);

                if (map.Count > 0)
                {
                    node.Attributes.AddAll <KeyValuePair <String, Object> >(map);
                }
            }

            if (json.TryGetValue("metrics", out o) && o != null)
            {
                var map = AsMap <String, Object>(o);

                if (map.Count > 0)
                {
                    node.Metrics = parseNodeMetrics(AsMap <String, Object>(o));
                }
            }

            return(node);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }