-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
block_ops.go
276 lines (237 loc) · 7.41 KB
/
block_ops.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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"time"
"github.com/keybase/client/go/kbfs/data"
"github.com/keybase/client/go/kbfs/env"
"github.com/keybase/client/go/kbfs/kbfsblock"
"github.com/keybase/client/go/kbfs/libkey"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
type blockOpsConfig interface {
data.Versioner
logMaker
blockCacher
blockServerGetter
codecGetter
cryptoPureGetter
keyGetterGetter
diskBlockCacheGetter
syncedTlfGetterSetter
initModeGetter
blockCryptVersioner
clockGetter
reporterGetter
settingsDBGetter
subscriptionManagerGetter
subscriptionManagerPublisherGetter
}
// BlockOpsStandard implements the BlockOps interface by relaying
// requests to the block server.
type BlockOpsStandard struct {
config blockOpsConfig
log traceLogger
queue *blockRetrievalQueue
}
var _ BlockOps = (*BlockOpsStandard)(nil)
// NewBlockOpsStandard creates a new BlockOpsStandard
func NewBlockOpsStandard(
config blockOpsConfig, queueSize, prefetchQueueSize int,
throttledPrefetchPeriod time.Duration,
appStateUpdater env.AppStateUpdater) *BlockOpsStandard {
bg := &realBlockGetter{config: config}
qConfig := &realBlockRetrievalConfig{
blockRetrievalPartialConfig: config,
bg: bg,
}
q := newBlockRetrievalQueue(
queueSize, prefetchQueueSize, throttledPrefetchPeriod, qConfig,
appStateUpdater)
bops := &BlockOpsStandard{
config: config,
log: traceLogger{config.MakeLogger("")},
queue: q,
}
return bops
}
// Get implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Get(ctx context.Context, kmd libkey.KeyMetadata,
blockPtr data.BlockPointer, block data.Block,
lifetime data.BlockCacheLifetime, branch data.BranchName) error {
// Check the journal explicitly first, so we don't get stuck in
// the block-fetching queue.
if journalBServer, ok := b.config.BlockServer().(journalBlockServer); ok {
data, serverHalf, found, err := journalBServer.getBlockFromJournal(
ctx, kmd.TlfID(), blockPtr.ID)
if err != nil {
return err
}
if found {
return assembleBlockLocal(
ctx, b.config.keyGetter(), b.config.Codec(),
b.config.cryptoPure(), kmd, blockPtr, block, data, serverHalf)
}
}
b.log.LazyTrace(ctx, "BOps: Requesting %s", blockPtr.ID)
action := b.config.Mode().DefaultBlockRequestAction()
if branch != data.MasterBranch {
action = action.AddNonMasterBranch()
}
errCh := b.queue.Request(
ctx, defaultOnDemandRequestPriority, kmd,
blockPtr, block, lifetime, action)
err := <-errCh
b.log.LazyTrace(ctx, "BOps: Request fulfilled for %s (err=%v)", blockPtr.ID, err)
return err
}
// GetEncodedSizes implements the BlockOps interface for
// BlockOpsStandard.
func (b *BlockOpsStandard) GetEncodedSizes(
ctx context.Context, kmd libkey.KeyMetadata,
blockPtrs []data.BlockPointer) (
sizes []uint32, statuses []keybase1.BlockStatus, err error) {
// Check the journal explicitly first, so we don't get stuck in
// the block-fetching queue.
var ids []kbfsblock.ID
var contexts []kbfsblock.Context
var indices []int
sizes = make([]uint32, len(blockPtrs))
statuses = make([]keybase1.BlockStatus, len(blockPtrs))
for i, blockPtr := range blockPtrs {
if journalBServer, ok := b.config.BlockServer().(journalBlockServer); ok {
size, found, err := journalBServer.getBlockSizeFromJournal(
ctx, kmd.TlfID(), blockPtr.ID)
if err != nil {
return nil, nil, err
}
if found && size > 0 {
sizes[i] = size
statuses[i] = keybase1.BlockStatus_LIVE
continue
}
}
// Not in journal.
ids = append(ids, blockPtr.ID)
contexts = append(contexts, blockPtr.Context)
indices = append(indices, i)
}
if len(ids) == 0 {
return sizes, statuses, nil
}
servSizes, servStatuses, err := b.config.BlockServer().GetEncodedSizes(
ctx, kmd.TlfID(), ids, contexts)
if err != nil {
return nil, nil, err
}
for i, j := range indices {
sizes[j] = servSizes[i]
statuses[j] = servStatuses[i]
}
return sizes, statuses, nil
}
// Ready implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Ready(ctx context.Context, kmd libkey.KeyMetadata,
block data.Block) (id kbfsblock.ID, plainSize int, readyBlockData data.ReadyBlockData,
err error) {
defer func() {
if err != nil {
id = kbfsblock.ID{}
plainSize = 0
readyBlockData = data.ReadyBlockData{}
}
}()
crypto := b.config.cryptoPure()
tlfCryptKey, err := b.config.keyGetter().
GetTLFCryptKeyForEncryption(ctx, kmd)
if err != nil {
return
}
// New server key half for the block.
serverHalf, err := crypto.MakeRandomBlockCryptKeyServerHalf()
if err != nil {
return
}
plainSize, encryptedBlock, err := crypto.EncryptBlock(
block, tlfCryptKey, serverHalf)
if err != nil {
return
}
buf, err := b.config.Codec().Encode(encryptedBlock)
if err != nil {
return
}
readyBlockData = data.ReadyBlockData{
Buf: buf,
ServerHalf: serverHalf,
}
encodedSize := readyBlockData.GetEncodedSize()
if encodedSize < plainSize {
err = TooLowByteCountError{
ExpectedMinByteCount: plainSize,
ByteCount: encodedSize,
}
return
}
id, err = kbfsblock.MakePermanentID(buf, encryptedBlock.Version)
if err != nil {
return
}
// Cache the encoded size.
block.SetEncodedSize(uint32(encodedSize))
return
}
// Delete implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Delete(ctx context.Context, tlfID tlf.ID,
ptrs []data.BlockPointer) (liveCounts map[kbfsblock.ID]int, err error) {
contexts := make(kbfsblock.ContextMap)
for _, ptr := range ptrs {
contexts[ptr.ID] = append(contexts[ptr.ID], ptr.Context)
}
return b.config.BlockServer().RemoveBlockReferences(ctx, tlfID, contexts)
}
// Archive implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Archive(ctx context.Context, tlfID tlf.ID,
ptrs []data.BlockPointer) error {
contexts := make(kbfsblock.ContextMap)
for _, ptr := range ptrs {
contexts[ptr.ID] = append(contexts[ptr.ID], ptr.Context)
}
return b.config.BlockServer().ArchiveBlockReferences(ctx, tlfID, contexts)
}
// GetLiveCount implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) GetLiveCount(
ctx context.Context, tlfID tlf.ID, ptrs []data.BlockPointer) (
liveCounts map[kbfsblock.ID]int, err error) {
contexts := make(kbfsblock.ContextMap)
for _, ptr := range ptrs {
contexts[ptr.ID] = append(contexts[ptr.ID], ptr.Context)
}
return b.config.BlockServer().GetLiveBlockReferences(ctx, tlfID, contexts)
}
// TogglePrefetcher implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) TogglePrefetcher(enable bool) <-chan struct{} {
return b.queue.TogglePrefetcher(enable, nil, nil)
}
// Prefetcher implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Prefetcher() Prefetcher {
return b.queue.Prefetcher()
}
// BlockRetriever implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) BlockRetriever() BlockRetriever {
return b.queue
}
// Shutdown implements the BlockOps interface for BlockOpsStandard.
func (b *BlockOpsStandard) Shutdown(ctx context.Context) error {
// Block on the queue being done.
select {
case <-b.queue.Shutdown():
return nil
case <-ctx.Done():
return errors.WithStack(ctx.Err())
}
}