Last active
May 1, 2017 18:25
-
-
Save shridharns/9140b0225f9336be8d135401ff549b55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. First place the lua script in the same directory with the name: redis-migration.lua | |
2. Place the redis-migration.sh shell script in the same directory. | |
3. Execute the script with the parameters {host} {port} for the master of Redis instance/Sentinel. | |
NOTE: This operation is idempotent, since underlying structure is SET and the script ignores self map from the list. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local keys = {}; | |
local featureNames = ""; | |
local feature = "FF4J_FEATURE_" | |
local property = "FF4J_PROPERTY_" | |
local cursor = "0" | |
local done = false; | |
local searchPattern = feature | |
if ARGV[1] == property then | |
searchPattern = property | |
end | |
repeat | |
local result = redis.call("SCAN", 0, "match", searchPattern .. "*", "count", 200000) | |
cursor = result[1]; | |
keys = result[2]; | |
for i, key in ipairs(keys) do | |
local featureName = key:gsub(searchPattern, ""); | |
-- if this script is double run, ignore MAP! | |
if featureName ~= "MAP" then | |
if i > 1 then | |
featureNames = featureNames .. ","; | |
end | |
featureNames = featureNames .. featureName; | |
end | |
end | |
if cursor == "0" then | |
done = true; | |
end | |
until done | |
return featureNames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -ne 2 ] | |
then | |
echo "Migrate keys from Redis for FF4J libraries key scanning fix, using SCAN & SADD" | |
echo "Usage: $0 {host} {port}" | |
exit 1 | |
fi | |
feature="FF4J_FEATURE_" | |
property="FF4J_PROPERTY_" | |
migrate-keys() { | |
host=$1 | |
port=$2 | |
types=$3 | |
reply=$(redis-cli -h $host -p $port --eval redis-migration.lua , $types) | |
echo "$types: $reply" | |
IFS=',' read -a myarray <<< "$reply" | |
for i in "${myarray[@]}" | |
do | |
: | |
redis-cli -h $1 -p $2 SADD $types"MAP" $i >> /dev/null | |
done | |
} | |
# Migrate features. | |
migrate-keys $1 $2 $feature | |
# Migrate properties. | |
migrate-keys $1 $2 $property |
@kutzi Redis blocks that operation. Because SCAN is non-deterministic, you won’t be able to mix it with commands that write in a Lua script.
You'll run into:
Write commands not allowed after non deterministic commands
More details here: https://redis.io/commands/EVAL
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wondering: why don't you do the SADD in the lua script directly?