Skip to content

Instantly share code, notes, and snippets.

@shridharns
Last active May 1, 2017 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shridharns/9140b0225f9336be8d135401ff549b55 to your computer and use it in GitHub Desktop.
Save shridharns/9140b0225f9336be8d135401ff549b55 to your computer and use it in GitHub Desktop.
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.
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
#!/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
Copy link

kutzi commented Apr 30, 2017

Wondering: why don't you do the SADD in the lua script directly?

@shridharns
Copy link
Author

@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