/
cache_redis.go
182 lines (155 loc) · 5.21 KB
/
cache_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2020 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package statedb
import (
"errors"
"runtime"
"time"
"github.com/go-redis/redis/v7"
"github.com/klaytn/klaytn/common/hexutil"
)
const (
redisSetItemChannelSize = 1024
redisSubscriptionChannelSize = 100 // default value of redis client
redisSubscriptionChannelBlock = "latestBlock"
)
var (
redisCacheDialTimeout = time.Duration(900 * time.Millisecond)
redisCacheTimeout = time.Duration(900 * time.Millisecond)
errRedisNoEndpoint = errors.New("redis endpoint not specified")
)
type RedisCache struct {
client redis.UniversalClient
setItemCh chan setItem
pubSub *redis.PubSub
}
type setItem struct {
key []byte
value []byte
}
func newRedisClient(endpoints []string, isCluster bool) (redis.UniversalClient, error) {
if endpoints == nil {
return nil, errRedisNoEndpoint
}
// cluster-enabled redis can have more than one shard
if isCluster {
return redis.NewClusterClient(&redis.ClusterOptions{
// it takes Timeout * (MaxRetries+1) to raise an error
Addrs: endpoints,
DialTimeout: redisCacheDialTimeout,
ReadTimeout: redisCacheTimeout,
WriteTimeout: redisCacheTimeout,
MaxRetries: 2,
}), nil
}
return redis.NewClient(&redis.Options{
// it takes Timeout * (MaxRetries+1) to raise an error
Addr: endpoints[0],
DialTimeout: redisCacheDialTimeout,
ReadTimeout: redisCacheTimeout,
WriteTimeout: redisCacheTimeout,
MaxRetries: 2,
}), nil
}
// newRedisCache creates a redis cache containing redis client, setItemCh and pubSub.
// It generates worker goroutines to process Set commands asynchronously.
func newRedisCache(config *TrieNodeCacheConfig) (*RedisCache, error) {
cli, err := newRedisClient(config.RedisEndpoints, config.RedisClusterEnable)
if err != nil {
logger.Error("failed to create a redis client", "err", err, "endpoint", config.RedisEndpoints,
"isCluster", config.RedisClusterEnable)
return nil, err
}
cache := &RedisCache{
client: cli,
setItemCh: make(chan setItem, redisSetItemChannelSize),
pubSub: cli.Subscribe(),
}
workerNum := runtime.NumCPU()/2 + 1
for i := 0; i < workerNum; i++ {
go func() {
for item := range cache.setItemCh {
cache.set(item.key, item.value)
}
}()
}
logger.Info("Initialized trie node cache with redis", "endpoint", config.RedisEndpoints,
"isCluster", config.RedisClusterEnable)
return cache, nil
}
func (cache *RedisCache) Get(k []byte) []byte {
val, err := cache.client.Get(hexutil.Encode(k)).Bytes()
if err != nil {
logger.Debug("cannot get an item from redis cache", "err", err, "key", hexutil.Encode(k))
return nil
}
return val
}
// Set writes data asynchronously. Not all data is written if a setItemCh is full.
// To write data synchronously, use set instead.
func (cache *RedisCache) Set(k, v []byte) {
item := setItem{key: k, value: v}
select {
case cache.setItemCh <- item:
default:
logger.Warn("redis setItem channel is full")
}
}
// set writes data synchronously.
// To write data asynchronously, use Set instead.
func (cache *RedisCache) set(k, v []byte) {
if err := cache.client.Set(hexutil.Encode(k), v, 0).Err(); err != nil {
logger.Warn("failed to set an item on redis cache", "err", err, "key", hexutil.Encode(k))
}
}
func (cache *RedisCache) Has(k []byte) ([]byte, bool) {
val := cache.Get(k)
if val == nil {
return nil, false
}
return val, true
}
func (cache *RedisCache) publish(channel string, msg string) error {
return cache.client.Publish(channel, msg).Err()
}
// subscribe subscribes the redis client to the given channel.
// It returns an existing *redis.PubSub subscribing previously registered channels also.
func (cache *RedisCache) subscribe(channel string) *redis.PubSub {
if err := cache.pubSub.Subscribe(channel); err != nil {
logger.Error("failed to subscribe channel", "err", err, "channel", channel)
}
return cache.pubSub
}
func (cache *RedisCache) PublishBlock(msg string) error {
return cache.publish(redisSubscriptionChannelBlock, msg)
}
func (cache *RedisCache) SubscribeBlockCh() <-chan *redis.Message {
return cache.subscribe(redisSubscriptionChannelBlock).ChannelSize(redisSubscriptionChannelSize)
}
func (cache *RedisCache) UnsubscribeBlock() error {
return cache.pubSub.Unsubscribe(redisSubscriptionChannelBlock)
}
func (cache *RedisCache) UpdateStats() interface{} {
return nil
}
func (cache *RedisCache) SaveToFile(filePath string, concurrency int) error {
return nil
}
func (cache *RedisCache) Close() error {
cache.pubSub.Close()
close(cache.setItemCh)
return cache.client.Close()
}