Skip to content

Commit

Permalink
FilerStore: add redis_lua
Browse files Browse the repository at this point in the history
  • Loading branch information
banjiaojuhao committed Feb 15, 2022
1 parent 17ac524 commit b5ec346
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 0 deletions.
34 changes: 34 additions & 0 deletions weed/command/scaffold/filer.toml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,40 @@ routeByLatency = false
# This changes the data layout. Only add new directories. Removing/Updating will cause data loss.
superLargeDirectories = []

[redis_lua]
enabled = false
address = "localhost:6379"
password = ""
database = 0
# This changes the data layout. Only add new directories. Removing/Updating will cause data loss.
superLargeDirectories = []

[redis_lua_sentinel]
enabled = false
addresses = ["172.22.12.7:26379","172.22.12.8:26379","172.22.12.9:26379"]
masterName = "master"
username = ""
password = ""
database = 0

[redis_lua_cluster]
enabled = false
addresses = [
"localhost:30001",
"localhost:30002",
"localhost:30003",
"localhost:30004",
"localhost:30005",
"localhost:30006",
]
password = ""
# allows reads from slave servers or the master, but all writes still go to the master
readOnly = false
# automatically use the closest Redis server for reads
routeByLatency = false
# This changes the data layout. Only add new directories. Removing/Updating will cause data loss.
superLargeDirectories = []

[redis3] # beta
enabled = false
address = "localhost:6379"
Expand Down
44 changes: 44 additions & 0 deletions weed/filer/redis_lua/redis_cluster_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package redis_lua

import (
"github.com/chrislusf/seaweedfs/weed/filer"
"github.com/chrislusf/seaweedfs/weed/util"
"github.com/go-redis/redis/v8"
)

func init() {
filer.Stores = append(filer.Stores, &RedisLuaClusterStore{})
}

type RedisLuaClusterStore struct {
UniversalRedisLuaStore
}

func (store *RedisLuaClusterStore) GetName() string {
return "redis_lua_cluster"
}

func (store *RedisLuaClusterStore) Initialize(configuration util.Configuration, prefix string) (err error) {

configuration.SetDefault(prefix+"useReadOnly", false)
configuration.SetDefault(prefix+"routeByLatency", false)

return store.initialize(
configuration.GetStringSlice(prefix+"addresses"),
configuration.GetString(prefix+"password"),
configuration.GetBool(prefix+"useReadOnly"),
configuration.GetBool(prefix+"routeByLatency"),
configuration.GetStringSlice(prefix+"superLargeDirectories"),
)
}

func (store *RedisLuaClusterStore) initialize(addresses []string, password string, readOnly, routeByLatency bool, superLargeDirectories []string) (err error) {
store.Client = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: addresses,
Password: password,
ReadOnly: readOnly,
RouteByLatency: routeByLatency,
})
store.loadSuperLargeDirectories(superLargeDirectories)
return
}
45 changes: 45 additions & 0 deletions weed/filer/redis_lua/redis_sentinel_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package redis_lua

import (
"github.com/chrislusf/seaweedfs/weed/filer"
"github.com/chrislusf/seaweedfs/weed/util"
"github.com/go-redis/redis/v8"
"time"
)

func init() {
filer.Stores = append(filer.Stores, &RedisLuaSentinelStore{})
}

type RedisLuaSentinelStore struct {
UniversalRedisLuaStore
}

func (store *RedisLuaSentinelStore) GetName() string {
return "redis_lua_sentinel"
}

func (store *RedisLuaSentinelStore) Initialize(configuration util.Configuration, prefix string) (err error) {
return store.initialize(
configuration.GetStringSlice(prefix+"addresses"),
configuration.GetString(prefix+"masterName"),
configuration.GetString(prefix+"username"),
configuration.GetString(prefix+"password"),
configuration.GetInt(prefix+"database"),
)
}

func (store *RedisLuaSentinelStore) initialize(addresses []string, masterName string, username string, password string, database int) (err error) {
store.Client = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: masterName,
SentinelAddrs: addresses,
Username: username,
Password: password,
DB: database,
MinRetryBackoff: time.Millisecond * 100,
MaxRetryBackoff: time.Minute * 1,
ReadTimeout: time.Second * 30,
WriteTimeout: time.Second * 5,
})
return
}
38 changes: 38 additions & 0 deletions weed/filer/redis_lua/redis_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package redis_lua

import (
"github.com/chrislusf/seaweedfs/weed/filer"
"github.com/chrislusf/seaweedfs/weed/util"
"github.com/go-redis/redis/v8"
)

func init() {
filer.Stores = append(filer.Stores, &RedisLuaStore{})
}

type RedisLuaStore struct {
UniversalRedisLuaStore
}

func (store *RedisLuaStore) GetName() string {
return "redis_lua"
}

func (store *RedisLuaStore) Initialize(configuration util.Configuration, prefix string) (err error) {
return store.initialize(
configuration.GetString(prefix+"address"),
configuration.GetString(prefix+"password"),
configuration.GetInt(prefix+"database"),
configuration.GetStringSlice(prefix+"superLargeDirectories"),
)
}

func (store *RedisLuaStore) initialize(hostPort string, password string, database int, superLargeDirectories []string) (err error) {
store.Client = redis.NewClient(&redis.Options{
Addr: hostPort,
Password: password,
DB: database,
})
store.loadSuperLargeDirectories(superLargeDirectories)
return
}
19 changes: 19 additions & 0 deletions weed/filer/redis_lua/stored_procedure/delete_entry.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- KEYS[1]: full path of entry
local fullpath = KEYS[1]
-- KEYS[2]: full path of entry
local fullpath_list_key = KEYS[2]
-- KEYS[3]: dir of the entry
local dir_list_key = KEYS[3]

-- ARGV[1]: isSuperLargeDirectory
local isSuperLargeDirectory = ARGV[1] == "1"
-- ARGV[2]: name of the entry
local name = ARGV[2]

redis.call("DEL", fullpath, fullpath_list_key)

if not isSuperLargeDirectory and name ~= "" then
redis.call("ZREM", dir_list_key, name)
end

return 0
15 changes: 15 additions & 0 deletions weed/filer/redis_lua/stored_procedure/delete_folder_children.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- KEYS[1]: full path of entry
local fullpath = KEYS[1]

if fullpath ~= "" and string.sub(fullpath, -1) == "/" then
fullpath = string.sub(fullpath, 0, -2)
end

local files = redis.call("ZRANGE", fullpath .. "\0", "0", "-1")

for _, name in ipairs(files) do
local file_path = fullpath .. "/" .. name
redis.call("DEL", file_path, file_path .. "\0")
end

return 0
24 changes: 24 additions & 0 deletions weed/filer/redis_lua/stored_procedure/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package stored_procedure

import (
_ "embed"
"github.com/go-redis/redis/v8"
)

func init() {
InsertEntryScript = redis.NewScript(insertEntry)
DeleteEntryScript = redis.NewScript(deleteEntry)
DeleteFolderChildrenScript = redis.NewScript(deleteFolderChildren)
}

//go:embed insert_entry.lua
var insertEntry string
var InsertEntryScript *redis.Script

//go:embed delete_entry.lua
var deleteEntry string
var DeleteEntryScript *redis.Script

//go:embed delete_folder_children.lua
var deleteFolderChildren string
var DeleteFolderChildrenScript *redis.Script
27 changes: 27 additions & 0 deletions weed/filer/redis_lua/stored_procedure/insert_entry.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- KEYS[1]: full path of entry
local full_path = KEYS[1]
-- KEYS[2]: dir of the entry
local dir_list_key = KEYS[2]

-- ARGV[1]: content of the entry
local entry = ARGV[1]
-- ARGV[2]: TTL of the entry
local ttlSec = tonumber(ARGV[2])
-- ARGV[3]: isSuperLargeDirectory
local isSuperLargeDirectory = ARGV[3] == "1"
-- ARGV[4]: zscore of the entry in zset
local zscore = tonumber(ARGV[4])
-- ARGV[5]: name of the entry
local name = ARGV[5]

if ttlSec > 0 then
redis.call("SET", full_path, entry, "EX", ttlSec)
else
redis.call("SET", full_path, entry)
end

if not isSuperLargeDirectory and name ~= "" then
redis.call("ZADD", dir_list_key, "NX", zscore, name)
end

return 0
Loading

0 comments on commit b5ec346

Please sign in to comment.