generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 83
/
blockstoremanager.go
149 lines (134 loc) · 3.12 KB
/
blockstoremanager.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
package decision
import (
"context"
"fmt"
"sync"
bstore "github.com/ipfs/boxo/blockstore"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-metrics-interface"
)
// blockstoreManager maintains a pool of workers that make requests to the blockstore.
type blockstoreManager struct {
bs bstore.Blockstore
workerCount int
jobs chan func()
pendingGauge metrics.Gauge
activeGauge metrics.Gauge
workerWG sync.WaitGroup
stopChan chan struct{}
stopOnce sync.Once
}
// newBlockstoreManager creates a new blockstoreManager with the given context
// and number of workers
func newBlockstoreManager(
bs bstore.Blockstore,
workerCount int,
pendingGauge metrics.Gauge,
activeGauge metrics.Gauge,
) *blockstoreManager {
return &blockstoreManager{
bs: bs,
workerCount: workerCount,
jobs: make(chan func()),
pendingGauge: pendingGauge,
activeGauge: activeGauge,
stopChan: make(chan struct{}),
}
}
func (bsm *blockstoreManager) start() {
bsm.workerWG.Add(bsm.workerCount)
for i := 0; i < bsm.workerCount; i++ {
go bsm.worker()
}
}
func (bsm *blockstoreManager) stop() {
bsm.stopOnce.Do(func() {
close(bsm.stopChan)
})
bsm.workerWG.Wait()
}
func (bsm *blockstoreManager) worker() {
defer bsm.workerWG.Done()
for {
select {
case <-bsm.stopChan:
return
case job := <-bsm.jobs:
bsm.pendingGauge.Dec()
bsm.activeGauge.Inc()
job()
bsm.activeGauge.Dec()
}
}
}
func (bsm *blockstoreManager) addJob(ctx context.Context, job func()) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-bsm.stopChan:
return fmt.Errorf("shutting down")
case bsm.jobs <- job:
bsm.pendingGauge.Inc()
return nil
}
}
func (bsm *blockstoreManager) getBlockSizes(ctx context.Context, ks []cid.Cid) (map[cid.Cid]int, error) {
res := make(map[cid.Cid]int)
if len(ks) == 0 {
return res, nil
}
var lk sync.Mutex
return res, bsm.jobPerKey(ctx, ks, func(c cid.Cid) {
size, err := bsm.bs.GetSize(ctx, c)
if err != nil {
if !ipld.IsNotFound(err) {
// Note: this isn't a fatal error. We shouldn't abort the request
log.Errorf("blockstore.GetSize(%s) error: %s", c, err)
}
} else {
lk.Lock()
res[c] = size
lk.Unlock()
}
})
}
func (bsm *blockstoreManager) getBlocks(ctx context.Context, ks []cid.Cid) (map[cid.Cid]blocks.Block, error) {
res := make(map[cid.Cid]blocks.Block, len(ks))
if len(ks) == 0 {
return res, nil
}
var lk sync.Mutex
return res, bsm.jobPerKey(ctx, ks, func(c cid.Cid) {
blk, err := bsm.bs.Get(ctx, c)
if err != nil {
if !ipld.IsNotFound(err) {
// Note: this isn't a fatal error. We shouldn't abort the request
log.Errorf("blockstore.Get(%s) error: %s", c, err)
}
return
}
lk.Lock()
res[c] = blk
lk.Unlock()
})
}
func (bsm *blockstoreManager) jobPerKey(ctx context.Context, ks []cid.Cid, jobFn func(c cid.Cid)) error {
var err error
var wg sync.WaitGroup
for _, k := range ks {
c := k
wg.Add(1)
err = bsm.addJob(ctx, func() {
jobFn(c)
wg.Done()
})
if err != nil {
wg.Done()
break
}
}
wg.Wait()
return err
}