/
mode_get.go
184 lines (163 loc) · 4.93 KB
/
mode_get.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
// 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 (
"context"
"errors"
"time"
"github.com/penguintop/penguin/pkg/postage"
"github.com/penguintop/penguin/pkg/shed"
"github.com/penguintop/penguin/pkg/storage"
"github.com/penguintop/penguin/pkg/penguin"
"github.com/syndtr/goleveldb/leveldb"
)
// Get returns a chunk from the database. If the chunk is
// not found storage.ErrNotFound will be returned.
// All required indexes will be updated required by the
// Getter Mode. Get is required to implement chunk.Store
// interface.
func (db *DB) Get(ctx context.Context, mode storage.ModeGet, addr penguin.Address) (ch penguin.Chunk, err error) {
db.metrics.ModeGet.Inc()
defer totalTimeMetric(db.metrics.TotalTimeGet, time.Now())
defer func() {
if err != nil {
db.metrics.ModeGetFailure.Inc()
}
}()
out, err := db.get(mode, addr)
if err != nil {
if errors.Is(err, leveldb.ErrNotFound) {
return nil, storage.ErrNotFound
}
return nil, err
}
return penguin.NewChunk(penguin.NewAddress(out.Address), out.Data).
WithStamp(postage.NewStamp(out.BatchID, out.Sig)), nil
}
// get returns Item from the retrieval index
// and updates other indexes.
func (db *DB) get(mode storage.ModeGet, addr penguin.Address) (out shed.Item, err error) {
item := addressToItem(addr)
out, err = db.retrievalDataIndex.Get(item)
if err != nil {
return out, err
}
switch mode {
// update the access timestamp and gc index
case storage.ModeGetRequest:
db.updateGCItems(out)
case storage.ModeGetPin:
pinnedItem, err := db.pinIndex.Get(item)
if err != nil {
return out, err
}
return pinnedItem, nil
// no updates to indexes
case storage.ModeGetSync:
case storage.ModeGetLookup:
default:
return out, ErrInvalidMode
}
return out, nil
}
// updateGCItems is called when ModeGetRequest is used
// for Get or GetMulti to update access time and gc indexes
// for all returned chunks.
func (db *DB) updateGCItems(items ...shed.Item) {
if db.updateGCSem != nil {
// wait before creating new goroutines
// if updateGCSem buffer id full
db.updateGCSem <- struct{}{}
}
db.updateGCWG.Add(1)
go func() {
defer db.updateGCWG.Done()
if db.updateGCSem != nil {
// free a spot in updateGCSem buffer
// for a new goroutine
defer func() { <-db.updateGCSem }()
}
db.metrics.GCUpdate.Inc()
defer totalTimeMetric(db.metrics.TotalTimeUpdateGC, time.Now())
for _, item := range items {
err := db.updateGC(item)
if err != nil {
db.metrics.GCUpdateError.Inc()
db.logger.Errorf("localstore update gc: %v", err)
}
}
// if gc update hook is defined, call it
if testHookUpdateGC != nil {
testHookUpdateGC()
}
}()
}
// updateGC updates garbage collection index for
// a single item. Provided item is expected to have
// only Address and Data fields with non zero values,
// which is ensured by the get function.
func (db *DB) updateGC(item shed.Item) (err error) {
db.batchMu.Lock()
defer db.batchMu.Unlock()
if db.gcRunning {
db.dirtyAddresses = append(db.dirtyAddresses, penguin.NewAddress(item.Address))
}
batch := new(leveldb.Batch)
// update accessTimeStamp in retrieve, gc
i, err := db.retrievalAccessIndex.Get(item)
switch {
case err == nil:
item.AccessTimestamp = i.AccessTimestamp
case errors.Is(err, leveldb.ErrNotFound):
// no chunk accesses
default:
return err
}
if item.AccessTimestamp == 0 {
// chunk is not yet synced
// do not add it to the gc index
return nil
}
// delete current entry from the gc index
err = db.gcIndex.DeleteInBatch(batch, item)
if err != nil {
return err
}
// update the gc item timestamp in case
// it exists
_, err = db.gcIndex.Get(item)
item.AccessTimestamp = now()
if err == nil {
err = db.gcIndex.PutInBatch(batch, item)
if err != nil {
return err
}
} else if !errors.Is(err, leveldb.ErrNotFound) {
return err
}
// if the item is not in the gc we don't
// update the gc index, since the item is
// in the reserve.
// update retrieve access index
err = db.retrievalAccessIndex.PutInBatch(batch, item)
if err != nil {
return err
}
return db.shed.WriteBatch(batch)
}
// testHookUpdateGC is a hook that can provide
// information when a garbage collection index is updated.
var testHookUpdateGC func()