docs/ops/migrateslotCommand_en.md
Starting from Pika 3.5.0, Pika supports the Codis + Pika cluster mode. The implementation replaces redis-server in Codis with pika-server and adds the concept of slot keys (default 1024, configurable via the configuration file). Using sets as an example: when a user modifies a key, the slot is calculated as slot = crc32(key) % 1024, and the key is added to the corresponding slot. During migration, a specified slot is used to spop one key at a time and migrate it to the target pika-server. The advantage of this implementation is that data can be migrated without any extra processing. The drawbacks are reduced performance (approximately 10%–20%, so we recommend disabling it when migration is not needed) and increased storage (approximately 30%). Pika supports a toggle for slot migration; when the toggle is off, slot migration operations are not supported; when it is on, slot migration is supported.
In conf/pika.conf, you can set slotmigrate which defaults to no. Set it to yes when migration is needed:
# slotmigrate [yes | no]
slotmigrate : no
You can also configure it dynamically via the command line with config set:
$ redis-cli -h 127.0.0.1 -p 9221 config set slotmigrate yes
Description: Get the number of slots and the size of each slot in Pika.
Parameters: slotsinfo [start] [count]
Return: Returns an array of slotinfo. The first row is slotnum (slot number); the second row is slotsize (number of data items in the slot).
localhost:9221> slotsinfo 0 128
1) 1) (integer) 23
2) (integer) 2
2) 1) (integer) 29
2) (integer) 1
Description: Delete all key-values under specified slots in Pika.
Parameters: slotsdel slot1 [slot2 ...] — accepts at least 1 slot number.
Return: Returns the remaining size after deletion, typically 0.
localhost:9221> slotsdel 1013 990
1) 1) (integer) 1013
2) (integer) 0
2) 1) (integer) 990
2) (integer) 0
Description: Get the keys in a slot key; usage is similar to sscan. Available when slotmigrate is yes.
Parameters: Similar to the SCAN command.
Return: See SCAN command.
localhost:9221> slotsscan 579 0 COUNT 10
1) "10752"
2) 1) "{a}7836"
2) "{a}2167"
3) "{a}5332"
4) "{a}6292"
5) "{a}600"
6) "{a}6094"
7) "{a}7754"
8) "{a}4929"
9) "{a}9211"
10) "{a}6596"
Description: Recalculate the slot for all keys.
Parameters: No extra parameters needed.
localhost:9221> slotsreload
20230727234255 : 0
Description: Available when slotmigrate is yes. Stops the execution of the slotsreload command.
Parameters: No extra parameters needed.
Return: Returns OK.
localhost:9221> slotsreloadoff
OK
Description: Delete keys corresponding to the given slotIDs.
Parameters: slotscleanup slotID [multiple IDs allowed]
localhost:9221> slotscleanup 10 11 12 13 14 15
1) (integer) 10
2) (integer) 11
3) (integer) 12
4) (integer) 13
5) (integer) 14
6) (integer) 15
Description: Stop an ongoing background slot cleanup operation.
Parameters: slotscleanupoff
Return: Returns OK.
localhost:9221> slotscleanupoff
OK
Description: Randomly select 1 key-value from a slot and migrate it to the target machine.
Parameters: slotsmgrtslot host port timeout slot
Return: Returns an integer.
localhost:9221> set a 100
OK
localhost:9221> slotsinfo
1) 1) (integer) 579
2) (integer) 1
localhost:9221> slotsmgrtslot 127.0.0.1 6380 100 579
(integer) 1 # Migration succeeded
localhost:9221> slotsinfo
(empty list or set)
localhost:9221> slotsmgrtslot 127.0.0.1 6380 100 579 1
(integer) 0 # Migration failed; key no longer exists locally
Description: Randomly select 1 key-value from a slot and all key-values with the same tag as that key, and migrate them to the target machine.
Parameters: slotsmgrttagslot host port timeout slot
Return: Returns an integer.
localhost:9221> set a 100
OK
localhost:9221> slotsinfo
1) 1) (integer) 579
2) (integer) 1
localhost:9221> slotsmgrttagslot 127.0.0.1 6380 100 579
(integer) 1 # Migration succeeded
localhost:9221> slotsinfo
(empty list or set)
localhost:9221> slotsmgrttagslot 127.0.0.1 6380 100 579 1
(integer) 0 # Migration failed; key no longer exists locally
Description: Migrate a key to the target machine.
Parameters: slotsmgrtone host port timeout key
Return: Returns an integer.
localhost:9221> set a{tag} 100
OK
localhost:9221> slotsinfo
1) 1) (integer) 579
2) (integer) 1
localhost:9221> slotsmgrtone 127.0.0.1 6380 100 a
(integer) 1 # Migration succeeded
localhost:9221> slotsmgrtone 127.0.0.1 6380 100 a
(integer) 0 # Migration failed; key no longer exists locally
Description: Migrate all keys with the same tag as the specified key to the target machine.
Parameters: slotsmgrttagone host port timeout key
Return: Returns an integer.
localhost:9221> set a{tag} 100 # set <a{tag}, 100>
OK
localhost:9221> set b{tag} 100 # set <b{tag}, 100>
OK
localhost:9221> slotsmgrttagone 127.0.0.1 6380 1000 {tag}
(integer) 2
localhost:9221> scan 0 # Migration succeeded; keys no longer exist locally
1) "0"
2) (empty list or set)
localhost:6380> scan 0 # Data successfully migrated to target machine
1) "0"
2) 1) "a{tag}"
2) "b{tag}"
slotsmgrttagslot-async
slotsmgrttagslot-async host port timeout maxbulks maxbytes slot numkeyslocalhost:9221> slotsmgrttagslot-async pika 9221 5000 200 33554432 518 500
1) (integer) 0
2) (integer) 0
slotsmgrt-exec-wrapper
slotsmgrt-exec-wrapper $hashkey $command [$arg1 ...]localhost:9221> slotsmgrt-exec-wrapper my-hash set my-hash 100
slotsmgrt-async-status
slotsmgrt-async-statuslocalhost:9221> slotsmgrt-async-status
1) "dest server: :-1"
2) "slot number: -1"
3) "migrating : no"
4) "moved keys : -1"
5) "remain keys: -1"
slotsmgrt-async-cancel
slotsmgrt-async-cancellocalhost:9221> slotsmgrt-async-cancel
OK
Description: Calculate and return the slot number for the given keys.
Parameters: slotshashkey key1 [key2 ...]
Return: Returns an array.
localhost:9221> slotshashkey a b c # Calculate slot numbers for <a, b, c>
1) (integer) 579 # Slot number for the corresponding key
2) (integer) 1017
3) (integer) 879