-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmulti_redis.go
123 lines (114 loc) · 3.14 KB
/
multi_redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package kasper
import (
"fmt"
"github.com/garyburd/redigo/redis"
"sort"
)
// MultiRedis is an implementation of MultiStore that uses Redis.
// Each instance provides multitenant key-value access to keys of the form {tenant}/{keyPrefix}/{key}.
// This implementation uses Gary Burd's Go Redis client.
// See https://github.com/garyburd/redigo
type MultiRedis struct {
config *Config
conn redis.Conn
stores map[string]Store
keyPrefix string
logger Logger
labelValues []string
pushCounter Counter
fetchCounter Counter
}
// NewMultiRedis creates MultiRedis instances.
// All keys read and written will be of the form:
// {tenant}/{keyPrefix}/{key}
func NewMultiRedis(config *Config, conn redis.Conn, keyPrefix string) *MultiRedis {
metrics := config.MetricsProvider
labelNames := []string{"topicProcessor", "keyPrefix"}
s := &MultiRedis{
config,
conn,
make(map[string]Store),
keyPrefix,
config.Logger,
[]string{config.TopicProcessorName, keyPrefix},
metrics.NewCounter("MultiRedis_Push", "Counter of Push() calls", labelNames...),
metrics.NewCounter("MultiRedis_Fetch", "Counter of Fetch() calls", labelNames...),
}
return s
}
func (s *MultiRedis) getPrefixedKey(tenant string, key string) string {
return fmt.Sprintf("%s/%s/%s", tenant, s.keyPrefix, key)
}
// Tenant returns an Redis Store for the given tenant.
// Created instances are cached on future invocations.
func (s *MultiRedis) Tenant(tenant string) Store {
tenantKeyPrefix := fmt.Sprintf("%s/%s", tenant, s.keyPrefix)
kv, found := s.stores[tenant]
if !found {
kv = NewRedis(s.config, s.conn, tenantKeyPrefix)
s.stores[tenant] = kv
}
return kv
}
// AllTenants returns the list of tenants known to this instance.
func (s *MultiRedis) AllTenants() []string {
tenants := make([]string, len(s.stores))
i := 0
for tenant := range s.stores {
tenants[i] = tenant
i++
}
sort.Strings(tenants)
return tenants
}
// Fetch performs a single MULTI GET Redis command across multiple tenants.
func (s *MultiRedis) Fetch(keys []TenantKey) (*MultiMap, error) {
s.fetchCounter.Inc(s.labelValues...)
res := NewMultiMap(len(keys) / 10)
if len(keys) == 0 {
return res, nil
}
err := s.conn.Send("MULTI")
if err != nil {
return nil, err
}
for _, key := range keys {
err = s.conn.Send("GET", s.getPrefixedKey(key.Tenant, key.Key))
if err != nil {
return nil, err
}
}
values, err := redis.Values(s.conn.Do("EXEC"))
if err != nil {
return nil, err
}
for i, value := range values {
bytes, err := redis.Bytes(value, err)
if err != nil {
return nil, err
}
err = res.Tenant(keys[i].Tenant).Put(keys[i].Key, bytes)
if err != nil {
return nil, err
}
}
return res, nil
}
// Fetch performs a single MULTI SET Redis command across multiple tenants.
func (s *MultiRedis) Push(entries *MultiMap) error {
s.pushCounter.Inc(s.labelValues...)
err := s.conn.Send("MULTI")
if err != nil {
return err
}
for _, tenant := range entries.AllTenants() {
for key, value := range entries.Tenant(tenant).(*Map).GetMap() {
err = s.conn.Send("SET", s.getPrefixedKey(tenant, key), value)
if err != nil {
return err
}
}
}
_, err = s.conn.Do("EXEC")
return err
}