forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredundant_storage_service.go
175 lines (158 loc) · 4.54 KB
/
redundant_storage_service.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
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"context"
"errors"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/arbstate/daprovider"
"github.com/offchainlabs/nitro/util/pretty"
)
// This is a redundant storage service, which replicates data across a set of StorageServices.
// The implementation assumes that there won't be a large number of replicas.
type RedundantStorageService struct {
innerServices []StorageService
}
func NewRedundantStorageService(ctx context.Context, services []StorageService) (StorageService, error) {
innerServices := make([]StorageService, len(services))
copy(innerServices, services)
return &RedundantStorageService{innerServices}, nil
}
type readResponse struct {
data []byte
err error
}
func (r *RedundantStorageService) GetByHash(ctx context.Context, key common.Hash) ([]byte, error) {
log.Trace("das.RedundantStorageService.GetByHash", "key", pretty.PrettyHash(key), "this", r)
subCtx, cancel := context.WithCancel(ctx)
defer cancel()
var anyError error
responsesExpected := len(r.innerServices)
resultChan := make(chan readResponse, responsesExpected)
for _, serv := range r.innerServices {
go func(s StorageService) {
data, err := s.GetByHash(subCtx, key)
resultChan <- readResponse{data, err}
}(serv)
}
for responsesExpected > 0 {
select {
case resp := <-resultChan:
if resp.err == nil {
return resp.data, nil
}
anyError = resp.err
responsesExpected--
case <-ctx.Done():
return nil, ctx.Err()
}
}
return nil, anyError
}
func (r *RedundantStorageService) Put(ctx context.Context, data []byte, expirationTime uint64) error {
logPut("das.RedundantStorageService.Store", data, expirationTime, r)
var wg sync.WaitGroup
var errorMutex sync.Mutex
var anyError error
wg.Add(len(r.innerServices))
for _, serv := range r.innerServices {
go func(s StorageService) {
err := s.Put(ctx, data, expirationTime)
if err != nil {
errorMutex.Lock()
anyError = err
errorMutex.Unlock()
}
wg.Done()
}(serv)
}
wg.Wait()
return anyError
}
func (r *RedundantStorageService) Sync(ctx context.Context) error {
var wg sync.WaitGroup
var errorMutex sync.Mutex
var anyError error
wg.Add(len(r.innerServices))
for _, serv := range r.innerServices {
go func(s StorageService) {
err := s.Sync(ctx)
if err != nil {
errorMutex.Lock()
anyError = err
errorMutex.Unlock()
}
wg.Done()
}(serv)
}
wg.Wait()
return anyError
}
func (r *RedundantStorageService) Close(ctx context.Context) error {
var wg sync.WaitGroup
var errorMutex sync.Mutex
var anyError error
wg.Add(len(r.innerServices))
for _, serv := range r.innerServices {
go func(s StorageService) {
err := s.Close(ctx)
if err != nil {
errorMutex.Lock()
anyError = err
errorMutex.Unlock()
}
wg.Done()
}(serv)
}
wg.Wait()
return anyError
}
func (r *RedundantStorageService) ExpirationPolicy(ctx context.Context) (daprovider.ExpirationPolicy, error) {
// If at least one inner service has KeepForever,
// then whole redundant service can serve after timeout.
// If no inner service has KeepForever,
// but at least one inner service has DiscardAfterArchiveTimeout,
// then whole redundant service can serve till archive timeout.
// If no inner service has KeepForever, DiscardAfterArchiveTimeout,
// but at least one inner service has DiscardAfterDataTimeout,
// then whole redundant service can serve till data timeout.
var res daprovider.ExpirationPolicy = -1
for _, serv := range r.innerServices {
expirationPolicy, err := serv.ExpirationPolicy(ctx)
if err != nil {
return -1, err
}
switch expirationPolicy {
case daprovider.KeepForever:
return daprovider.KeepForever, nil
case daprovider.DiscardAfterArchiveTimeout:
res = daprovider.DiscardAfterArchiveTimeout
case daprovider.DiscardAfterDataTimeout:
if res != daprovider.DiscardAfterArchiveTimeout {
res = daprovider.DiscardAfterDataTimeout
}
}
}
if res == -1 {
return -1, errors.New("unknown expiration policy")
}
return res, nil
}
func (r *RedundantStorageService) String() string {
str := "RedundantStorageService("
for _, serv := range r.innerServices {
str = str + serv.String() + ","
}
return str + ")"
}
func (r *RedundantStorageService) HealthCheck(ctx context.Context) error {
for _, storageService := range r.innerServices {
err := storageService.HealthCheck(ctx)
if err != nil {
return err
}
}
return nil
}