chapter 1 visit to cache server process (hget)
Architecture in jedis ClusterNativeClient outer wraps a client, in this class, provides many visit redis method, including: hget, hset, hdel etc., which become the front desk business code and jedis interact bridge,
We query operation, for example, the realization of a more detailed description jedis
1.1 source code analysis
1.1.1 ClusterNativeClient hget methods:
Public String hget (String key, String field)
{
Return to the pool. Hget (key field);
}
Description:
The Pool was established cache initialization time, behind us speak in detail,
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:
The establishment of the Pool is called the JedisCluster hget method,
1.1.2 JedisCluster hget method in
Public String hget (String key, String field)
{
Return (String) new JedisClusterCommand (enclosing connectionHandler, enclosing maxRedirections, key, field)
{
Public String the execute (Jedis connection) {
Return connection. Hget (enclosing val $key, enclosing val $field);
}
}
The run (key);
}
Description:
The hget method first new a JedisClusterCommand object, the object of the run method for the new jedis connection process, through the key slot, then get the slot to connect the node, the Execute method is through the connection sends a command to the target node in order to get the results, the method can call BinaryJedis hget in (),
1.1.3 BinaryJedis hget method in
Public byte [] hget (byte [] key, byte [] field)
{
CheckIsInMulti ();
This. Client. Hget (key, field);
Return this. Client. GetBinaryBulkReply ();
}
Description:
BinaryJedis is jedis binary realization, namely parameters for binary conversion, in this layer called the client hegt method, namely send the underlying hget command, getBinaryBulkReply () to obtain the command after the results back to the upper object,
1.1.4 BinaryClient hget method in
Public void hget (byte [] key, byte [] field) {
SendCommand (Protocol.Com mand HGET, new byte [] [] {key, field});
}
Description:
BinaryClient binary implementations for the Client, in this layer calls the superclass sendCommand method of Connection, through the literal can guess is send commands to the underlying Client's meaning,
1.1.5 sendCommand method in Connection
Protected Connection sendCommand (ProtocolCommand CMD, byte [] [] args) {
Try {
The connect ().
Protocol. SendCommand (enclosing outputStream, CMD, args);
Enclosing pipelinedCommands +=1;
Return this.
}
The catch (JedisConnectionException ex) {
This. Broken=true;
} to throw the ex;
}
Description:
In Connection sendCommand method first has carried on the socket Connection to the target node, the connect () method is as follows, and then call Protocol sendCommand method, pay attention to the outputStream parameters, you can see is to send commands to the bottom in the form of the output stream,
Public void the connect () {
if (! IsConnected ())
Try {
This. The socket=new socket ();
This. Socket. SetReuseAddress (true);
This. Socket. SetKeepAlive (true);
This. Socket. SetTcpNoDelay (true);
This. Socket. SetSoLinger (true, 0).
This. Socket. Connect (new InetSocketAddress (enclosing the host, enclosing the port), enclosing connectionTimeout);
This. Socket. SetSoTimeout (enclosing soTimeout);
Enclosing the outputStream=new RedisOutputStream (this) socket) getOutputStream ());
Enclosing inputStream=new RedisInputStream (this) socket) getInputStream ());
{} the catch (IOException ex)
This. Broken=true;
Throw new JedisConnectionException (ex);
}
}
1.1.6 sendCommand method in Protocol
Private static void sendCommand (RedisOutputStream OS, byte [] command, byte [] [] args)
{
Try {
OS. Write (42);
OS. WriteIntCrLf (args. The length + 1);
OS. Write (36);
OS. WriteIntCrLf (command. Length);
OS. Write (command);
OS. WriteCrLf ();
Arg: for (byte [] args) {
OS. Write (36);
OS. WriteIntCrLf (arg. Length);
OS. Write (arg);
OS. WriteCrLf ();
}
} the catch (IOException e) {
Throw new JedisConnectionException (e);
}
}
Description:
Protocol sendCommand method can be more intuitive to see, it is to use the output to the specified connection to perform write operations,
Above, hget for jedis implementation method, other implementation method,
1.2 summary
And Redis Server communication Protocol rules in Redis. Clients. Jedis. The Protocol in this class, mainly through to the RedisInputStream and RedisOutputStream to read and write operations to complete, commands are sent through the Redis. Clients. Jedis. Protocol sendCommand to complete, is the RedisOutputStream write byte stream, the data returned is by reading RedisInputStream parsing process,
And Redis Sever Socket communication is by Redis clients. Jedis. To achieve the Connection, and maintains a Connection in the underlying Socket Connection and its I/O Stream and Protocol, the I/O Stream is in the Connection Socket Connection is established after capturing and when use to Protocol,
Chapter 2 Jedis positioning redis cluster node process
Mentioned JedisClusterCommand run method of a class of new jedis connection process, namely the access node localization process, let's look at the when a client requests to jedis is how to locate the access server nodes,
2.1 source code analysis
2.1.1 JedisClusterCommand run method of a class
Public T run (String key) {
If (key==null) {
Throw new JedisClusterException (" No way to dispatch this command to Redis Cluster. ");
}
Return runWithRetries (key, enclosing redirections, false, false);
}
Description:
Run method is very simple runWithRetries method is invoked,
2.1.2 runWithRetries method
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull