Home > other >  Jedis source code analysis (below)
Jedis source code analysis (below)

Time:10-07

chapter 3 cache initialization process
Every web service starts executing cache initialization process, the said storage slot and node slots of global variables to the corresponding relationship was assignment in cache initialization time, actually. There will be a global variable assignment during initialization, the nodes, the variable to store all the cluster nodes information, the web server startup, will call RedisCacheStoreInitializationImpl initCache () method to initialize the cache cluster
3.1 source code analysis
3.1.1 RedisCacheStoreInitializationImpl initCache ()
Public Boolean initCache ()
{
If (" TRUE ". EqualsIgnoreCase (this) cacheStoreConfig) getEnabled ())) {
The Properties of props=loadProperty (" jedis. Properties ");
Props. SetProperty (" host ", this. CacheStoreConfig. GetHost ());

This. IsClusterEnabled=Boolean. ParseBoolean (props. GetProperty (" enableCluster "));
If (this. IsClusterEnabled)
ClusterNativeClient. Init (props);
The else {
SingleNativeClient. Init (props);
}

This. CacheStoreConfig. SetState (1);
Enclosing the info (" Redis cache: cache started ");
return true;
}
Description:
If the cluster configuration, called ClusterNativeClient init method,
3.1.2 ClusterNativeClient init method
Public static void init (Properties props)
{
String [] the servers=props. GetProperty (" host "). The split (", ");
HashSet nodes=new HashSet ();
For (String s: the servers) {
String [] ipAndPort=s.s plit (" : ");
String IP=ipAndPort [0];
Int port=Integer. The valueOf (ipAndPort [1]). IntValue ();
Nodes. The add (new HostAndPort (IP, port));
}

JedisPoolConfig config=new JedisPoolConfig ();
Config. SetMaxIdle (Integer. ParseInt (props, getProperty (" maxIdle ")));
Config. SetMaxTotal (Integer. ParseInt (props, getProperty (" maxTotal ")));
The pool=new JedisCluster (nodes, the config);
}
Description:
In the init method will be the new a JedisCluster object,
Public JedisCluster (Set Nodes, GenericObjectPoolConfig poolConfig) {
This (nodes, 2000, 5, poolConfig);
}
Public JedisCluster (Set JedisClusterNode, int timeout, int maxRedirections, GenericObjectPoolConfig poolConfig)
{
Enclosing connectionHandler=new JedisSlotBasedConnectionHandler (jedisClusterNode poolConfig, timeout);

Enclosing maxRedirections=maxRedirections;
}
Here jedis assign default values will give two parameters: the timeout=2000, maxRedirections=5, the timeout for the connection timeout, maxRedirections for maximum redirect times,

3.1.3 JedisSlotBasedConnectionHandler constructor
Public JedisSlotBasedConnectionHandler (Set Nodes, GenericObjectPoolConfig poolConfig, int timeout)
{
Super (nodes, poolConfig, timeout);
}
Description:
JedisSlotBasedConnectionHandler constructor will continue to call his father class JedisClusterConnectionHandler constructor,
3.1.4 JedisClusterConnectionHandler constructor
Public JedisClusterConnectionHandler (Set Nodes, GenericObjectPoolConfig poolConfig, int timeout)
{
Enclosing the cache=new JedisClusterInfoCache (poolConfig, timeout);
InitializeSlotsCache (nodes, poolConfig);
}
Description:
In JedisClusterConnectionHandler construction method performs initialization method initializeSlotsCache,
3.1.5 initialization method initializeSlotsCache
Private void initializeSlotsCache (Set StartNodes, GenericObjectPoolConfig poolConfig) {
The Iterator I $=startNodes. The Iterator ();
Jedis Jedis;
If (I $. HasNext ()) {HostAndPort HostAndPort=(HostAndPort) I $. Next ();
Jedis=new jedis (hostAndPort getHost (), hostAndPort. The getPort ()); } the Iterator I $;
Try {this. Cache. DiscoverClusterNodesAndSlots (jedis);

If (jedis!=null)
Jedis. Close ();
}
The catch (JedisConnectionException e)
{
If (jedis!=null)
Jedis. Close ();
}
The finally
{
If (jedis!=null) {
Jedis. Close ();
}

}

While (I $. HasNext ()) {HostAndPort node=(HostAndPort) I $. Next ();
This. Cache. SetNodeIfNotExist (node);
}
}
Description:
InitializeSlotsCache method tell us two information: 1, initialization and server to interact, is the first node selection and attempt to and its connections, this node is the cache - applicationContext. We configure the first node in an XML file,


2, when the connection exception occurs, for example, the node is down, don't throw exceptions, but will directly close the connection, and perform setNodeIfNotExist, add all the nodes in the configuration file to the nodes, discoverClusterNodesAndSlots method is initialized to slots, which is called clusterNodes () method, this method is to give the server sends a cluster nodes command, send the command to get the basic information of the current cluster:

192.168.174.24:7001 & gt; Cluster nodes
Eb52be41cd51117460254a0c6a7badf6e82a3b49 192.168.174.24: master 7002-0 1476165816509 7 connected. 5461-10922
9 e62f8dd7e1dc8b021dc01d9b37b58fce175022b 192.168.174.26: master 7001-0 1476165819511 10 connected 0-5460
0 b0c0bae6e73389466c178a23f6b9cba560824cb 192.168.174.26:7000 slave eb52be41cd51117460254a0c6a7badf6e82a3b49 0 1476165818511 7 connected
6 df2966a46d8ee812b3757094731290604a917c1 192.168.174.24:7000 slave 9 e62f8dd7e1dc8b021dc01d9b37b58fce175022b 0 1476165817509 10 connected
595 bd6dd6f5e89d6a936bf9d4d8be7fef5018b80 192.168.174.26:7002 slave 2 e171799e798b6f72b1d0b1396a17f6584e13b30 0 1476165820511 12 connected
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related