-
Notifications
You must be signed in to change notification settings - Fork 38
/
file_location_cache.go
173 lines (163 loc) · 4.96 KB
/
file_location_cache.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
package indexer
import (
"context"
"fmt"
"sync"
"time"
"github.com/0glabs/0g-storage-client/common/shard"
"github.com/0glabs/0g-storage-client/core"
"github.com/0glabs/0g-storage-client/node"
"github.com/hashicorp/golang-lru/v2/expirable"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const defaultFindFileCooldown = time.Minute * 60
const defaultDiscoveredURLRetryInterval = time.Minute * 10
const defaultSuccessCallLifetime = time.Minute * 10
type FileLocationCacheConfig struct {
CacheSize int
Expiry time.Duration
DiscoveryNode string
DiscoveryPorts []int
}
type successCall struct {
node *shard.ShardedNode
ts time.Time
}
type FileLocationCache struct {
cache *expirable.LRU[uint64, []*shard.ShardedNode]
latestFindFile sync.Map // tx seq -> time.Time
latestFailedCall sync.Map // url -> time.Time
latestSuccessCall sync.Map // url -> successCall
discoverNode *node.AdminClient
discoveryPorts []int
}
var defaultFileLocationCache FileLocationCache
func InitFileLocationCache(config FileLocationCacheConfig) (cache *FileLocationCache, err error) {
if len(config.DiscoveryNode) > 0 {
if defaultFileLocationCache.discoverNode, err = node.NewAdminClient(config.DiscoveryNode, defaultZgsClientOpt); err != nil {
return nil, errors.WithMessage(err, "Failed to create admin client to discover peers")
}
}
defaultFileLocationCache.cache = expirable.NewLRU[uint64, []*shard.ShardedNode](config.CacheSize, nil, config.Expiry)
defaultFileLocationCache.discoveryPorts = config.DiscoveryPorts
return &defaultFileLocationCache, nil
}
func (c *FileLocationCache) Close() {
if c.discoverNode != nil {
c.discoverNode.Close()
}
}
func (c *FileLocationCache) GetFileLocations(ctx context.Context, txSeq uint64) ([]*shard.ShardedNode, error) {
if nodes, ok := c.cache.Get(txSeq); ok {
return nodes, nil
}
var nodes []*shard.ShardedNode
// fetch from trusted
selected := make(map[string]struct{})
trusted := defaultNodeManager.TrustedClients()
var segNum uint64
for _, v := range trusted {
start := time.Now()
fileInfo, err := v.GetFileInfoByTxSeq(ctx, txSeq)
if fileInfo != nil {
segNum = core.NumSplits(int64(fileInfo.Tx.Size), core.DefaultSegmentSize)
}
if err != nil || fileInfo == nil || !fileInfo.Finalized {
continue
}
config, err := v.GetShardConfig(context.Background())
if err != nil || !config.IsValid() {
continue
}
nodes = append(nodes, &shard.ShardedNode{
URL: v.URL(),
Config: config,
Latency: time.Since(start).Milliseconds(),
})
selected[v.URL()] = struct{}{}
}
if segNum == 0 {
return nil, fmt.Errorf("file info not found")
}
logrus.Debugf("find file #%v from trusted nodes, got %v nodes holding the file", txSeq, len(nodes))
if _, covered := shard.Select(nodes, 1, false); covered {
c.cache.Add(txSeq, nodes)
return nodes, nil
}
// trusted nodes do not hold all shards of the file, try to find file
if c.discoverNode != nil {
locations, err := c.discoverNode.GetFileLocation(ctx, txSeq, false)
if err != nil {
return nil, err
}
logrus.Debugf("find file #%v from location cache, got %v nodes holding the file", txSeq, len(locations))
for _, location := range locations {
for _, port := range c.discoveryPorts {
url := fmt.Sprintf("http://%v:%v", location.Ip, port)
if _, ok := selected[url]; ok {
break
}
if val, ok := c.latestSuccessCall.Load(url); ok {
call := val.(successCall)
if time.Since(call.ts) < defaultSuccessCallLifetime {
nodes = append(nodes, call.node)
break
}
}
if val, ok := c.latestFailedCall.Load(url); ok {
if time.Since(val.(time.Time)) < defaultDiscoveredURLRetryInterval {
continue
}
}
zgsClient, err := node.NewZgsClient(url, defaultZgsClientOpt)
if err != nil {
continue
}
defer zgsClient.Close()
fileInfo, err := zgsClient.GetFileInfoByTxSeq(ctx, txSeq)
if err != nil {
c.latestFailedCall.Store(url, time.Now())
continue
}
if fileInfo == nil || !fileInfo.Finalized {
continue
}
start := time.Now()
config, err := zgsClient.GetShardConfig(context.Background())
if err != nil {
c.latestFailedCall.Store(url, time.Now())
continue
}
if !config.IsValid() {
continue
}
call := successCall{
node: &shard.ShardedNode{
URL: url,
Config: config,
Latency: time.Since(start).Milliseconds(),
},
ts: time.Now(),
}
nodes = append(nodes, call.node)
c.latestSuccessCall.Store(url, call)
selected[url] = struct{}{}
break
}
}
if _, covered := shard.Select(nodes, 1, false); covered {
c.cache.Add(txSeq, nodes)
return nodes, nil
}
if val, ok := c.latestFindFile.Load(txSeq); ok {
if time.Since(val.(time.Time)) < defaultFindFileCooldown {
return nil, nil
}
}
logrus.Debugf("triggering FindFile for tx seq %v", txSeq)
c.discoverNode.FindFile(ctx, txSeq)
c.latestFindFile.Store(txSeq, time.Now())
}
return nil, nil
}