/
mode_put.go
297 lines (262 loc) · 8.48 KB
/
mode_put.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package localstore
import (
"bytes"
"context"
"errors"
"time"
"github.com/gauss-project/aurorafs/pkg/boson"
"github.com/gauss-project/aurorafs/pkg/sctx"
"github.com/gauss-project/aurorafs/pkg/shed"
"github.com/gauss-project/aurorafs/pkg/shed/driver"
"github.com/gauss-project/aurorafs/pkg/storage"
)
// Put stores Chunks to database and depending
// on the Putter mode, it updates required indexes.
// Put is required to implement storage.Store
// interface.
func (db *DB) Put(ctx context.Context, mode storage.ModePut, chs ...boson.Chunk) (exist []bool, err error) {
rootHash := sctx.GetRootHash(ctx)
db.metrics.ModePut.Inc()
defer totalTimeMetric(db.metrics.TotalTimePut, time.Now())
exist, err = db.put(mode, rootHash, chs...)
if err != nil {
db.metrics.ModePutFailure.Inc()
}
return exist, err
}
// put stores Chunks to database and updates other indexes. It acquires lockAddr
// to protect two calls of this function for the same address in parallel. Item
// fields Address and Data must not be with their nil values. If chunks with the
// same address are passed in arguments, only the first chunk will be stored,
// and following ones will have exist set to true for their index in exist
// slice. This is the same behaviour as if the same chunks are passed one by one
// in multiple put method calls.
func (db *DB) put(mode storage.ModePut, rootAddr boson.Address, chs ...boson.Chunk) (exist []bool, err error) {
// this is an optimization that tries to optimize on already existing chunks
// not needing to acquire batchMu. This is in order to reduce lock contention
// when chunks are retried across the network for whatever reason.
if len(chs) == 1 && mode != storage.ModePutRequestPin && mode != storage.ModePutUploadPin {
has, err := db.retrievalDataIndex.Has(chunkToItem(chs[0]))
if err != nil {
return nil, err
}
if has {
return []bool{true}, nil
}
}
// protect parallel updates
db.batchMu.Lock()
defer db.batchMu.Unlock()
if db.gcRunning {
for _, ch := range chs {
db.dirtyAddresses = append(db.dirtyAddresses, ch.Address())
}
}
batch := db.shed.NewBatch()
// variables that provide information for operations
// to be done after write batch function successfully executes
var gcSizeChange int64 // number to add or subtract from gcSize
exist = make([]bool, len(chs))
// A lazy populated map of bin ids to properly set
// BinID values for new chunks based on initial value from database
// and incrementing them.
// Values from this map are stored with the batch
binIDs := make(map[uint8]uint64)
rootItem := addressToItem(rootAddr)
switch mode {
case storage.ModePutRequest, storage.ModePutRequestPin:
for i, ch := range chs {
if containsChunk(ch.Address(), chs[:i]...) {
exist[i] = true
continue
}
pin := mode == storage.ModePutRequestPin
exists, c, err := db.putRequest(batch, binIDs, chunkToItem(ch), rootItem, pin)
if err != nil {
return nil, err
}
exist[i] = exists
gcSizeChange += c
}
case storage.ModePutUpload, storage.ModePutUploadPin:
for i, ch := range chs {
if containsChunk(ch.Address(), chs[:i]...) {
exist[i] = true
continue
}
item := chunkToItem(ch)
exists, err := db.putUpload(batch, binIDs, chunkToItem(ch))
if err != nil {
return nil, err
}
exist[i] = exists
if mode == storage.ModePutUploadPin {
_, err = db.setPin(batch, item, addressToItem(rootAddr))
if err != nil {
return nil, err
}
}
}
default:
return nil, ErrInvalidMode
}
for po, id := range binIDs {
db.binIDs.PutInBatch(batch, uint64(po), id)
}
err = db.incGCSizeInBatch(batch, gcSizeChange)
if err != nil {
return nil, err
}
err = batch.Commit()
if err != nil {
return nil, err
}
return exist, nil
}
// putRequest adds an Item to the batch by updating required indexes:
// - put to indexes: retrieve, gc
// - it does not enter the syncpool
// The batch can be written to the database.
// Provided batch and binID map are updated.
func (db *DB) putRequest(batch driver.Batching, binIDs map[uint8]uint64, item, rootItem shed.Item, forcePin bool) (exists bool, gcSizeChange int64, err error) {
has, err := db.retrievalDataIndex.Has(item)
if err != nil {
return false, 0, err
}
if has {
return true, 0, nil
}
item.StoreTimestamp = now()
item.BinID, err = db.incBinID(binIDs, db.po(boson.NewAddress(item.Address)))
if err != nil {
return false, 0, err
}
err = db.retrievalDataIndex.PutInBatch(batch, item)
if err != nil {
return false, 0, err
}
if bytes.Equal(item.Address, rootItem.Address) {
rootItem.BinID = item.BinID
}
gcSizeChange, err = db.preserveOrCache(batch, item, rootItem, forcePin)
if err != nil {
return false, gcSizeChange, err
}
return false, gcSizeChange, nil
}
// putUpload adds an Item to the batch by updating required indexes:
// - put to indexes: retrieve, push, pull
// The batch can be written to the database.
// Provided batch and binID map are updated.
func (db *DB) putUpload(batch driver.Batching, binIDs map[uint8]uint64, item shed.Item) (exists bool, err error) {
exists, err = db.retrievalDataIndex.Has(item)
if err != nil {
return false, err
}
if exists {
return true, nil
}
item.StoreTimestamp = now()
item.BinID, err = db.incBinID(binIDs, db.po(boson.NewAddress(item.Address)))
if err != nil {
return false, err
}
err = db.retrievalDataIndex.PutInBatch(batch, item)
if err != nil {
return false, err
}
return false, nil
}
// preserveOrCache is a helper function used to add chunks to either a pinned reserve or gc cache
// (the retrieval access index and the gc index)
func (db *DB) preserveOrCache(batch driver.Batching, item, rootItem shed.Item, forcePin bool) (gcSizeChange int64, err error) {
if forcePin {
return db.setPin(batch, item, rootItem)
}
return db.setGC(batch, rootItem)
}
// setGC is a helper function used to add chunks to the retrieval access
// index and the gc index in the cases that the putToGCCheck condition
// warrants a gc set. this is to mitigate index leakage in edge cases where
// a chunk is added to a node's localstore and given that the chunk is
// already within that node's NN (thus, it can be added to the gc index
// safely)
func (db *DB) setGC(batch driver.Batching, item shed.Item) (gcSizeChange int64, err error) {
if item.Address == nil {
return 0, nil
}
i, err := db.retrievalAccessIndex.Get(item)
if err != nil {
if !errors.Is(err, driver.ErrNotFound) {
return 0, err
}
i.Address = item.Address
i.AccessTimestamp = now()
err = db.retrievalAccessIndex.PutInBatch(batch, i)
if err != nil {
return 0, err
}
}
item.AccessTimestamp = i.AccessTimestamp
if item.BinID == 0 {
i, err = db.retrievalDataIndex.Get(item)
if err != nil {
return 0, err
}
item.BinID = i.BinID
}
gcItem, err := db.gcIndex.Get(item)
if err != nil {
if !errors.Is(err, driver.ErrNotFound) {
return 0, err
}
gcItem = item
gcItem.GCounter = 1
} else {
gcItem.GCounter++
}
gcSizeChange++
err = db.gcIndex.PutInBatch(batch, gcItem)
if err != nil {
return 0, err
}
return gcSizeChange, nil
}
// incBinID is a helper function for db.put* methods that increments bin id
// based on the current value in the database. This function must be called under
// a db.batchMu lock. Provided binID map is updated.
func (db *DB) incBinID(binIDs map[uint8]uint64, po uint8) (id uint64, err error) {
if _, ok := binIDs[po]; !ok {
binIDs[po], err = db.binIDs.Get(uint64(po))
if err != nil {
return 0, err
}
}
binIDs[po]++
return binIDs[po], nil
}
// containsChunk returns true if the chunk with a specific address
// is present in the provided chunk slice.
func containsChunk(addr boson.Address, chs ...boson.Chunk) bool {
for _, c := range chs {
if addr.Equal(c.Address()) {
return true
}
}
return false
}