Redis channel and channel operating principle

After installing redis clusters, the next record about a very important principle channel its implementation, the first channel of migration operations before recording principle, intuitive feel.

Channel migration

To achieve channel migration, there are two ways, one is the use of redis-trib.rb ruby script, is to use native redis-cluster cluster command to complete. If you are using ruby script provided, you need to configure in advance, there are pit, reference https://www.cnblogs.com/youngchaolin/p/12027448.html  , using the native commands do not require any ruby, whose personal feeling ways to feel more intuitive migration process.

ruby script assisted migration

Now the cluster has six nodes, of which there are four main two from now 8000 node is the primary but not the channel management rights, you can give it through the channel numbers ruby ​​script auxiliary migrate some of the other master node, attention is now 8000 node is no data, ruby ​​script assisted migration currently only supports migration empty channel.

[root@node01 /home/software/redis-3.2.11]# redis-cli -p 8001
127.0.0.1:8001> cluster nodes
fd4c160edc74536d79ea29d239dca43275ec6b5a 192.168.200.140:8004 slave 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 0 1576239410838 1 connected
2c52d95c3d6d4c396469a81edfc1493d984e0f2d 192.168.200.140:8005 slave 7ce388bde879f686fc3c8491175397ca20405565 0 1576239407806 5 connected
231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 192.168 . 200.140 : 8001 myself, Master - 0  0  1 Connected 5461 - 10922 
# 8000 do not have channel management c41dbe9595ae83725d1322b032736fd198b26c49 192.168.200.140:8000 master - 0 1576239407806 0 connected 2e0f23d703874db80373f28b1be8c13f9de4fe6b 192.168.200.140:8003 master - 0 1576239409829 6 connected 0-5460 7ce388bde879f686fc3c8491175397ca20405565 192.168.200.140:8002 master - 0 1576239408818 2 connected 10923-16383

(1) using the src / redis-trib.rb reshard host: port a command to connect to any cluster node operation, the prompts to complete the operation.

# Log in to any node in preparation for migration
[root@node01 /home/software/redis-3.2.11]# src/redis-trib.rb reshard 192.168.200.140:8000
>>> Performing Cluster Check (using node 192.168.200.140:8000)
M: c41dbe9595ae83725d1322b032736fd198b26c49 192.168.200.140:8000
   slots: (0 slots) master
   0 additional replica(s)
M: 2e0f23d703874db80373f28b1be8c13f9de4fe6b 192.168.200.140:8003
   slots:0-5460 (5461 slots) master
   0 additional replica(s)
S: 2c52d95c3d6d4c396469a81edfc1493d984e0f2d 192.168.200.140:8005
   slots: (0 slots) slave
   replicates 7ce388bde879f686fc3c8491175397ca20405565
S: fd4c160edc74536d79ea29d239dca43275ec6b5a 192.168.200.140:8004
   slots: (0 slots) slave
   replicates 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83
M: 7ce388bde879f686fc3c8491175397ca20405565 192.168.200.140:8002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 192.168.200.140:8001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# Number of input channel to be relocated
How many slots do you want to move (from 1 to 16384)? 460
# Input node id to be moved, the input node id 8000
What is the receiving node ID? c41dbe9595ae83725d1322b032736fd198b26c49
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
# There is no use to all evenly distributed, but specify the source node 8003
Source node #1:2e0f23d703874db80373f28b1be8c13f9de4fe6b
After sharing point done #
Source node #2:done
... process slightly
# After the end of the need to reconfirm again, enter yes
Do you want to proceed with the proposed reshard plan (yes/no)? yes

(2) again to view the cluster channel assignments found 8000 node successfully assigned to the required number of channels.

[@ amdha01 the root / Home / Software / redis- 3.2 . . 11 ] -p -C # Redis CLI- 8000 -H 192.168 . 200.140 
192.168 . 200.140 : 8000 > Cluster Nodes 
# 8000 successfully assigned to the channel number c41dbe9595ae83725d1322b032736fd198b26c49
192.168.200.140:8000 myself,master - 0 0 7 connected 0-459 2e0f23d703874db80373f28b1be8c13f9de4fe6b 192.168.200.140:8003 master - 0 1576240358713 6 connected 460-5460 2c52d95c3d6d4c396469a81edfc1493d984e0f2d 192.168.200.140:8005 slave 7ce388bde879f686fc3c8491175397ca20405565 0 1576240357702 2 connected fd4c160edc74536d79ea29d239dca43275ec6b5a 192.168.200.140:8004 slave 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 0 1576240353158 1 connected 7ce388bde879f686fc3c8491175397ca20405565 192.168.200.140:8002 master - 0 1576240354673 2 connected 10923-16383 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 192.168.200.140:8001 master - 0 1576240356693 1 connected 5461-10922

redis-cluster migration command

Use redis cluster command to migrate, the migration of air into the channel, and the channel mobility of the two kinds of data.

case1. Empty channel migration

Empty channel mobility is relatively simple, because there is no corresponding data channel, there is no need to consider the migration of data, then the above results, the program channels on the 8003 node 460, the node migration up to 8,000.

(1) confirm that there are 460 slot track data, no data was migrated. Use the command cluster getkeysinslot Look channel number.

# 460 No data
[root@node01 /home/software/redis-3.2.11]# redis-cli -c -p 8003 -h 192.168.200.140
192.168.200.140:8003> cluster getkeysinslot 460 500
(empty list or set)

(2) the need to import log node 8000, the channel cluster setslot Run No. Importing source node id, node 8000 will change the status to the channel 460 importing.

[root@node01 /home/software/redis-3.2.11]# redis-cli -c -p 8000 -h 192.168.200.140
Later followed by the source node # 8003 id
192.168.200.140:8000> cluster setslot 460 importing 2e0f23d703874db80373f28b1be8c13f9de4fe6b
OK
See # 460 is introduced into the channel state
192.168.200.140:8000> cluster nodes
c41dbe9595ae83725d1322b032736fd198b26c49 192.168.200.140:8000 myself,master - 0 0 7 connected 0-459 [460-<-2e0f23d703874db80373f28b1be8c13f9de4fe6b]
2e0f23d703874db80373f28b1be8c13f9de4fe6b 192.168.200.140:8003 master - 0 1576242589111 6 connected 460-5460
2c52d95c3d6d4c396469a81edfc1493d984e0f2d 192.168.200.140:8005 slave 7ce388bde879f686fc3c8491175397ca20405565 0 1576242588103 2 connected
fd4c160edc74536d79ea29d239dca43275ec6b5a 192.168.200.140:8004 slave 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 0 1576242587095 1 connected
7ce388bde879f686fc3c8491175397ca20405565 192.168.200.140:8002 master - 0 1576242588606 2 connected 10923-16383
231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 192.168.200.140:8001 master - 0 1576242590119 1 connected 5461-10922

(3) Log in to be exported node 8003, the Executive Order No. migrating cluster setslot channel target node id, change the status of the channel 460 on the 8003 node is migrating.

[root@node01 /home/software/redis-3.2.11]# redis-cli -c -p 8003 -h 192.168.200.140
Later followed by the destination node id #
192.168.200.140:8003> cluster setslot 460 migrating c41dbe9595ae83725d1322b032736fd198b26c49
OK
View # 460 channels of state export
192.168.200.140:8003> cluster nodes
c41dbe9595ae83725d1322b032736fd198b26c49 192.168.200.140:8000 master - 0 1576242963942 7 connected 0-459
2c52d95c3d6d4c396469a81edfc1493d984e0f2d 192.168.200.140:8005 slave 7ce388bde879f686fc3c8491175397ca20405565 0 1576242964952 5 connected
7ce388bde879f686fc3c8491175397ca20405565 192.168.200.140:8002 master - 0 1576242959913 2 connected 10923-16383
fd4c160edc74536d79ea29d239dca43275ec6b5a 192.168.200.140:8004 slave 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 0 1576242960919 1 connected
2e0f23d703874db80373f28b1be8c13f9de4fe6b 
192.168.200.140:8003 myself,master - 0 0 6 connected 460-5460 [460->-c41dbe9595ae83725d1322b032736fd198b26c49]
231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 192.168.200.140:8001 master - 0 1576242962935 1 connected 5461-10922

(4) migration of the notification channels migrated two nodes, using the command channel number node cluster setslot move node id, you are required to operate on two nodes, this time the two nodes stored channel bit sequence ownership (a 2048 byte array) will change corresponding to the bit, the binary digits on the 8000 node 460 by the number of channels becomes 0 becomes 0 by 1 1,8001.

# Emigration execute commands on the node 8003, with later immigrated node id ID of
 192.168 . 200.140 : 8003 > Cluster setslot 460 the Node c41dbe9595ae83725d1322b032736fd198b26c49
OK
192.168.200.140:8003> quit
[root@node01 /home/software/redis-3.2.11]# redis-cli -c -p 8000 -h 192.168.200.140
# Moved to execute commands on the node 8000
192.168.200.140:8000> cluster setslot 460 node c41dbe9595ae83725d1322b032736fd198b26c49
OK
# View the cluster nodes, found that 460 successful migration, with the back to move into the node id id
192.168.200.140:8000> cluster nodes
c41dbe9595ae83725d1322b032736fd198b26c49 192.168.200.140:8000 myself,master - 0 0 7 connected 0-460
2e0f23d703874db80373f28b1be8c13f9de4fe6b 192.168.200.140:8003 master - 0 1576243857915 6 connected 461-5460
2c52d95c3d6d4c396469a81edfc1493d984e0f2d 192.168.200.140:8005 slave 7ce388bde879f686fc3c8491175397ca20405565 0 1576243859934 2 connected
fd4c160edc74536d79ea29d239dca43275ec6b5a 192.168.200.140:8004 slave 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 0 1576243855393 1 connected
7ce388bde879f686fc3c8491175397ca20405565 192.168.200.140:8002 master - 0 1576243858926 2 connected 10923-16383
231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 192.168.200.140:8001 master - 0 1576243860944 1 connected 5461-10922

case2. migrating data of the channel

The above complete migration of an empty channel, if a data channel, in addition to the channel migration, but also need to move along with the data. The need to use the command redis clusters to complete, ruby ​​script is not supported. The migration test channel 741, migrate from node 8003 to node 8000.

(1) confirm the channel number is not empty. Command consistent with the above, the use of cluster getkeysinslot channel No. Look.

# 741 found the channel number data 
192.168
. 200.140 : 8003 > Cluster getkeysinslot 741 500 . 1 ) " Age "

(2) the node to import log 8000, the channel cluster setslot Run No. Importing source node id, node status 8000 is changed to channel 741 importing.

[root@node01 /home/software/redis-3.2.11]# redis-cli -c -p 8000 -h 192.168.200.140
# 8000 is provided on node 741 is introduced into the importing, followed by the source node
192.168.200.140:8000> cluster setslot 741 importing 2e0f23d703874db80373f28b1be8c13f9de4fe6b
OK
# 741 Status Changes
192.168.200.140:8000> cluster nodes
c41dbe9595ae83725d1322b032736fd198b26c49 192.168.200.140:8000 myself,master - 0 0 7 connected 0-460 [741-<-2e0f23d703874db80373f28b1be8c13f9de4fe6b]
2e0f23d703874db80373f28b1be8c13f9de4fe6b 192.168.200.140:8003 master - 0 1576245822658 6 connected 461-5460
2c52d95c3d6d4c396469a81edfc1493d984e0f2d 192.168.200.140:8005 slave 7ce388bde879f686fc3c8491175397ca20405565 0 1576245819635 2 connected
fd4c160edc74536d79ea29d239dca43275ec6b5a 192.168.200.140:8004 slave 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 0 1576245821146 1 connected
7ce388bde879f686fc3c8491175397ca20405565 192.168.200.140:8002 master - 0 1576245821651 2 connected 10923-16383
231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 192.168.200.140:8001 master - 0 1576245820642 1 connected 5461-10922

(3) Log in to be exported node 8003, the Executive Order No. migrating cluster setslot channel target node id, change the status of the channel 741 on the 8003 node is migrating.

[root@node01 /home/software/redis-3.2.11]# redis-cli -c -p 8003 -h 192.168.200.140
Set # 8003 on export node 741 is migrating, followed by the destination node id
192.168.200.140:8003> cluster setslot 741 migrating c41dbe9595ae83725d1322b032736fd198b26c49
OK
# 741 Status Changes
192.168.200.140:8003> cluster nodes
c41dbe9595ae83725d1322b032736fd198b26c49 192.168.200.140:8000 master - 0 1576245954877 7 connected 0-460
2c52d95c3d6d4c396469a81edfc1493d984e0f2d 192.168.200.140:8005 slave 7ce388bde879f686fc3c8491175397ca20405565 0 1576245960923 5 connected
7ce388bde879f686fc3c8491175397ca20405565 192.168.200.140:8002 master - 0 1576245961931 2 connected 10923-16383
fd4c160edc74536d79ea29d239dca43275ec6b5a 192.168.200.140:8004 slave 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 0 1576245957902 1 connected
2e0f23d703874db80373f28b1be8c13f9de4fe6b 192.168.200.140:8003 myself,master - 0 0 6 connected 461-5460 [741->-c41dbe9595ae83725d1322b032736fd198b26c49]
231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 192.168.200.140:8001 master - 0 1576245959917 1 connected 5461-10922

(4) Login source node 8003 , the data corresponding to 741 channels (one of the key-value), and the key information stored channel map ({channel numbers: [age, ...]}) , together with move to node 8000, using the following command is completed.

#migrate host port key| destination-db timeout [COPY] [REPLACE] [KEYS key]
# "" Represents a key match
# 0 represents the number 0 database
# 1000 on behalf of the timeout milliseconds
Behind with # keys are specific key names, only one age here
192.168.200.140:8003> migrate 192.168.200.140 8000 "" 0 1000 keys age
OK

(5) migrate two nodes notification channel migrated to this point and above the empty channel of the same migration.

# Notice emigrated node 8003
 192.168 . 200.140 : 8003 > Cluster setslot 741 the Node c41dbe9595ae83725d1322b032736fd198b26c49
OK
192.168.200.140:8003> quit
[root@node01 /home/software/redis-3.2.11]# redis-cli -c -p 8000 -h 192.168.200.140
# Notice of moving nodes 8000
192.168.200.140:8000> cluster setslot 741 node c41dbe9595ae83725d1322b032736fd198b26c49
OK
View # 741 nodes to 8000 nodes have migrated
192.168.200.140:8000> cluster nodes
c41dbe9595ae83725d1322b032736fd198b26c49 192.168.200.140:8000 myself,master - 0 0 7 connected 0-460 741
2e0f23d703874db80373f28b1be8c13f9de4fe6b 192.168.200.140:8003 master - 0 1576246974768 6 connected 461-740 742-5460
2c52d95c3d6d4c396469a81edfc1493d984e0f2d 192.168.200.140:8005 slave 7ce388bde879f686fc3c8491175397ca20405565 0 1576246973758 2 connected
fd4c160edc74536d79ea29d239dca43275ec6b5a 192.168.200.140:8004 slave 231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 0 1576246969725 1 connected
7ce388bde879f686fc3c8491175397ca20405565 192.168.200.140:8002 master - 0 1576246971741 2 connected 10923-16383
231fe9df31dc1ccf7cca5ae2fb2313979cd6fa83 192.168.200.140:8001 master - 0 1576246972750 1 connected 5461-10922
# View the data without having to find the redirect, so the data is also migrated successfully
192.168.200.140:8000> get age
"18"

Channel principle

Description of the principle of channels, many online Bowen, Bowen and the following reference "Redis design and practice" to record your own understanding.

When a casual connection redis known cluster node wants to store data, or through a set key value get key command to retrieve data based on first key value, the value calculated by the algorithm results Remainder% 16384 calculates this key should fall enter the channel number, then the next you just need to find which node to manage the channel number, you can at this node to operate.

The above case where there are two possibilities , either the current node manages the connection to the channel, the current node is directly set it. Either the node does not manage the channel number, need to look for nodes. If you want to determine whether the channel is owned by a node management, you need to use a can mark things would channels and nodes linked in redis cluster, there are two key attributes two key classes and the inside (clusterNode and the clusterState property based slots), to provide the logic determination as follows:

(. 1) slots attribute clusternode class : clusterNode class correspondence information of a node, information of a single node cluster nodes command obtained, including information node id, ip, port, etc. Similarly, it has a key attribute slots, the type is a 2048 byte array, corresponding to 16 384 bit is bit by bit in this array is 1 or 0, to determine whether the node has the right to manage the channel, each node has its own this array, and there are an array of information from other nodes, ① shown below.

(2) slots class attribute CLUSTERSTATE : clusterState class information recorded cluster states, similar to the cluster information obtained cluster info command. It has a key attribute of slots, it is a type 16384 clusterNode array length, which records the channel information of the entire cluster, this array according to the index, to find the corresponding node, as shown in ② in FIG.

(3) nodes clusterState class attribute: This attribute is a type of dictionary data, key as node id, value of clusterNode objects, the information can be obtained for all cluster nodes in the cluster by this property. But this property is not directly related with the channel, it will help in understanding the above two properties at the same time the necessity of the presence of, as shown in FIG ③.

Here, and the above two cases can be solved, by taking the remainder result of key value calculation, may be slots array clusterNode object of the current node, find the value of the bit position corresponding to, if it is 1 it means that the current node has the management of the channel, you can set the data, if it is 0 you need to find the correct node, this time on the need to find the current slots array node clusterState objects, still by just calculated modulo result as a subscript to find the corresponding node, after finding redirected to the correct node, it will be the rest of the judgment to the correct node to process, still need to determine the management of the channel again.

In addition, if the node only needs clusterNode or clusterState object if a slots can?

(1)如果只有clusterNode的slots,当想判断某个槽道是否有分配出去,如果这个节点分配出去了还好,如果没有分配出去,则需要将所有节点的clusterNode对象都遍历一遍才能确认。因为通过某个clusterNode上对应槽道的标识0,还不能确认是否真的没有分配,还需要确认下一个节点,结果下一个节点也是0,这样就需要遍历完每个节点,对应的时间复杂度为O(n),n就是上面字典的key的数目。如果有clusterState,只需要通过下标指向的内容是否为Null,就可以判断出来,时间复杂度为O(1)。

(2)如果只有clusterState的slots,为了将某个节点如8000所管的槽道信息发送给另外一个节点如8001,则需要循环遍历一遍clusterState.slots数组得到结果后才能发送出去,这比单独发送一个8000节点的clusterNode.slots数组显得低效。

 

以上就是对redis集群中槽道迁移的记录,以及槽道原理的简单说明,后续继续完善。

 

参考博文

(1)《Redis设计与实践》槽道原理

(2)https://www.cnblogs.com/nanlinghan/p/9939161.html

Guess you like

Origin www.cnblogs.com/youngchaolin/p/12034660.html