forked from leotons/estuary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackbs.go
219 lines (183 loc) · 5.14 KB
/
trackbs.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
package main
import (
"context"
"fmt"
"time"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
"golang.org/x/xerrors"
"gorm.io/gorm"
)
type TrackingBlockstore struct {
bs blockstore.Blockstore
db *gorm.DB
cidReq func(context.Context, cid.Cid) (blocks.Block, error)
buffer map[cid.Cid]accesses
getCh chan cid.Cid
hasCh chan cid.Cid
countsReq chan getCountsReq
accessReq chan accessReq
}
type accesses struct {
Last time.Time
Get int
Has int
}
func NewTrackingBlockstore(bs blockstore.Blockstore, db *gorm.DB) *TrackingBlockstore {
cidReq := func(context.Context, cid.Cid) (blocks.Block, error) {
return nil, blockstore.ErrNotFound
}
tbs := &TrackingBlockstore{
bs: bs,
db: db,
cidReq: cidReq,
buffer: make(map[cid.Cid]accesses),
getCh: make(chan cid.Cid, 32),
hasCh: make(chan cid.Cid, 32),
countsReq: make(chan getCountsReq, 32),
accessReq: make(chan accessReq, 32),
}
go tbs.coalescer()
return tbs
}
var _ (blockstore.Blockstore) = (*TrackingBlockstore)(nil)
func (tbs *TrackingBlockstore) SetCidReqFunc(f func(context.Context, cid.Cid) (blocks.Block, error)) {
tbs.cidReq = f
}
func (tbs *TrackingBlockstore) Under() blockstore.Blockstore {
return tbs.bs
}
type getCountsReq struct {
req []Object
resp chan []int
}
func (tbs *TrackingBlockstore) GetCounts(objects []Object) ([]int, error) {
req := getCountsReq{
req: objects,
resp: make(chan []int),
}
tbs.countsReq <- req
resp := <-req.resp
return resp, nil
}
func (tbs *TrackingBlockstore) coalescer() {
ticker := time.Tick(time.Minute * 10)
for {
select {
case c := <-tbs.getCh:
acc := tbs.buffer[c]
acc.Get++
acc.Last = time.Now()
tbs.buffer[c] = acc
case c := <-tbs.hasCh:
acc := tbs.buffer[c]
acc.Has++
tbs.buffer[c] = acc
case req := <-tbs.countsReq:
resp := make([]int, len(req.req))
for i, o := range req.req {
resp[i] = tbs.buffer[o.Cid.CID].Get
}
req.resp <- resp
case req := <-tbs.accessReq:
acc := tbs.buffer[req.c]
req.resp <- acc
case <-ticker:
oldbuffer := tbs.buffer
tbs.buffer = make(map[cid.Cid]accesses)
go tbs.persistAccessCounts(oldbuffer)
}
}
}
func (tbs *TrackingBlockstore) persistAccessCounts(buf map[cid.Cid]accesses) {
for c, acc := range buf {
if acc.Get > 0 {
err := tbs.db.Model(&Object{}).Where("cid = ?", c.Bytes()).Updates(map[string]interface{}{
"reads": gorm.Expr("reads + ?", acc.Get),
"last_access": acc.Last,
}).Error
if err != nil {
log.Errorf("failed to update object in db: %s", err)
}
}
}
}
type accessReq struct {
c cid.Cid
resp chan accesses
}
func (tbs *TrackingBlockstore) LastAccess(ctx context.Context, c cid.Cid) (time.Time, error) {
req := accessReq{
c: c,
resp: make(chan accesses),
}
select {
case tbs.accessReq <- req:
case <-ctx.Done():
return time.Time{}, ctx.Err()
}
select {
case resp := <-req.resp:
return resp.Last, nil
case <-ctx.Done():
return time.Time{}, ctx.Err()
}
}
func (tbs *TrackingBlockstore) AllKeysChan(context.Context) (<-chan cid.Cid, error) {
return nil, fmt.Errorf("not supported")
}
func (tbs *TrackingBlockstore) DeleteBlock(ctx context.Context, _ cid.Cid) error {
return fmt.Errorf("deleting blocks not supported through this interface")
}
func (tbs *TrackingBlockstore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
tbs.getCh <- c // TODO: should we be tracking all requests? or all successful servings?
blk, err := tbs.bs.Get(ctx, c)
if err != nil {
if xerrors.Is(err, blockstore.ErrNotFound) {
var obj Object
if dberr := tbs.db.First(&obj, "where cid = ?", c.Bytes()).Error; dberr != nil {
if xerrors.Is(dberr, gorm.ErrRecordNotFound) {
// explicitly return original error
return nil, err
}
return nil, dberr
}
// having the object here in our database implies we are tracking it
// So since we don't have it, and are tracking it, we need to retrieve it
// TODO: this will wait for the retrieval to complete, which *might* take a while.
// maybe we return not found now, and get back to it later?
return tbs.cidReq(context.TODO(), c)
// TODO: We should very explicitly record failures here, this is
// one of the most critical services estuary performs
}
return nil, err
}
return blk, nil
}
func (tbs *TrackingBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
return tbs.bs.GetSize(ctx, c)
}
func (tbs *TrackingBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
has, err := tbs.bs.Has(ctx, c)
if err != nil {
return false, err
}
if has {
tbs.hasCh <- c
}
return has, nil
}
func (tbs *TrackingBlockstore) HashOnRead(hashOnRead bool) {
tbs.bs.HashOnRead(hashOnRead)
}
func (tbs *TrackingBlockstore) Put(ctx context.Context, blk blocks.Block) error {
// TODO:
// return fmt.Errorf("should not be writing blocks through this blockstore")
return tbs.bs.Put(ctx, blk)
}
func (tbs *TrackingBlockstore) PutMany(ctx context.Context, blks []blocks.Block) error {
// TODO:
// return fmt.Errorf("should not be writing blocks through this blockstore")
return tbs.bs.PutMany(ctx, blks)
}