forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder_branch_status.go
303 lines (268 loc) · 8.54 KB
/
folder_branch_status.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
298
299
300
301
302
303
// 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 (
"fmt"
"reflect"
"sync"
"github.com/keybase/client/go/libkb"
"github.com/keybase/kbfs/kbfsmd"
"github.com/keybase/kbfs/tlf"
"golang.org/x/net/context"
)
// FolderBranchStatus is a simple data structure describing the
// current status of a particular folder-branch. It is suitable for
// encoding directly as JSON.
type FolderBranchStatus struct {
Staged bool
BranchID string
HeadWriter libkb.NormalizedUsername
DiskUsage uint64
RekeyPending bool
LatestKeyGeneration kbfsmd.KeyGen
FolderID string
Revision kbfsmd.Revision
MDVersion kbfsmd.MetadataVer
RootBlockID string
SyncEnabled bool
PrefetchStatus string
UsageBytes int64
LimitBytes int64
GitUsageBytes int64
GitLimitBytes int64
// DirtyPaths are files that have been written, but not flushed.
// They do not represent unstaged changes in your local instance.
DirtyPaths []string
// If we're in the staged state, these summaries show the
// diverging operations per-file
Unmerged []*crChainSummary
Merged []*crChainSummary
Journal *TLFJournalStatus `json:",omitempty"`
PermanentErr string `json:",omitempty"`
}
// KBFSStatus represents the content of the top-level status file. It is
// suitable for encoding directly as JSON.
// TODO: implement magical status update like FolderBranchStatus
type KBFSStatus struct {
CurrentUser string
IsConnected bool
UsageBytes int64
LimitBytes int64
GitUsageBytes int64
GitLimitBytes int64
FailingServices map[string]error
JournalServer *JournalServerStatus `json:",omitempty"`
DiskCacheStatus map[string]DiskBlockCacheStatus `json:",omitempty"`
}
// StatusUpdate is a dummy type used to indicate status has been updated.
type StatusUpdate struct{}
// folderBranchStatusKeeper holds and updates the status for a given
// folder-branch, and produces FolderBranchStatus instances suitable
// for callers outside this package to consume.
type folderBranchStatusKeeper struct {
config Config
nodeCache NodeCache
dataMutex sync.Mutex
md ImmutableRootMetadata
permErr error
dirtyNodes map[NodeID]Node
unmerged []*crChainSummary
merged []*crChainSummary
quotaUsage *EventuallyConsistentQuotaUsage
updateChan chan StatusUpdate
updateMutex sync.Mutex
}
func newFolderBranchStatusKeeper(
config Config, nodeCache NodeCache) *folderBranchStatusKeeper {
return &folderBranchStatusKeeper{
config: config,
nodeCache: nodeCache,
dirtyNodes: make(map[NodeID]Node),
updateChan: make(chan StatusUpdate, 1),
}
}
// dataMutex should be taken by the caller
func (fbsk *folderBranchStatusKeeper) signalChangeLocked() {
fbsk.updateMutex.Lock()
defer fbsk.updateMutex.Unlock()
close(fbsk.updateChan)
fbsk.updateChan = make(chan StatusUpdate, 1)
}
// setRootMetadata sets the current head metadata for the
// corresponding folder-branch.
func (fbsk *folderBranchStatusKeeper) setRootMetadata(md ImmutableRootMetadata) {
fbsk.dataMutex.Lock()
defer fbsk.dataMutex.Unlock()
if fbsk.md.MdID() == md.MdID() {
return
}
fbsk.md = md
fbsk.signalChangeLocked()
}
func (fbsk *folderBranchStatusKeeper) setCRSummary(unmerged []*crChainSummary,
merged []*crChainSummary) {
fbsk.dataMutex.Lock()
defer fbsk.dataMutex.Unlock()
if reflect.DeepEqual(unmerged, fbsk.unmerged) &&
reflect.DeepEqual(merged, fbsk.merged) {
return
}
fbsk.unmerged = unmerged
fbsk.merged = merged
fbsk.signalChangeLocked()
}
func (fbsk *folderBranchStatusKeeper) setPermErr(err error) {
fbsk.dataMutex.Lock()
defer fbsk.dataMutex.Unlock()
fbsk.permErr = err
fbsk.signalChangeLocked()
}
func (fbsk *folderBranchStatusKeeper) addNode(m map[NodeID]Node, n Node) bool {
fbsk.dataMutex.Lock()
defer fbsk.dataMutex.Unlock()
id := n.GetID()
_, ok := m[id]
if ok {
return false
}
m[id] = n
fbsk.signalChangeLocked()
return true
}
func (fbsk *folderBranchStatusKeeper) rmNode(m map[NodeID]Node, n Node) bool {
fbsk.dataMutex.Lock()
defer fbsk.dataMutex.Unlock()
id := n.GetID()
_, ok := m[id]
if !ok {
return false
}
delete(m, id)
fbsk.signalChangeLocked()
return true
}
func (fbsk *folderBranchStatusKeeper) addDirtyNode(n Node) bool {
return fbsk.addNode(fbsk.dirtyNodes, n)
}
func (fbsk *folderBranchStatusKeeper) rmDirtyNode(n Node) bool {
return fbsk.rmNode(fbsk.dirtyNodes, n)
}
// dataMutex should be taken by the caller
func (fbsk *folderBranchStatusKeeper) convertNodesToPathsLocked(
m map[NodeID]Node) []string {
var ret []string
for _, n := range m {
ret = append(ret, fbsk.nodeCache.PathFromNode(n).String())
}
return ret
}
func (fbsk *folderBranchStatusKeeper) getStatusWithoutJournaling(
ctx context.Context) (
FolderBranchStatus, <-chan StatusUpdate, tlf.ID, error) {
fbsk.dataMutex.Lock()
defer fbsk.dataMutex.Unlock()
fbsk.updateMutex.Lock()
defer fbsk.updateMutex.Unlock()
var fbs FolderBranchStatus
tlfID := tlf.NullID
if fbsk.md != (ImmutableRootMetadata{}) {
tlfID = fbsk.md.TlfID()
fbs.Staged = fbsk.md.IsUnmergedSet()
fbs.BranchID = fbsk.md.BID().String()
name, err := fbsk.config.KBPKI().GetNormalizedUsername(
ctx, fbsk.md.LastModifyingWriter().AsUserOrTeam())
if err != nil {
return FolderBranchStatus{}, nil, tlf.NullID, err
}
fbs.HeadWriter = name
fbs.DiskUsage = fbsk.md.DiskUsage()
fbs.RekeyPending = fbsk.config.RekeyQueue().IsRekeyPending(fbsk.md.TlfID())
fbs.LatestKeyGeneration = fbsk.md.LatestKeyGeneration()
fbs.FolderID = fbsk.md.TlfID().String()
fbs.Revision = fbsk.md.Revision()
fbs.MDVersion = fbsk.md.Version()
fbs.SyncEnabled = fbsk.config.IsSyncedTlf(fbsk.md.TlfID())
prefetchStatus := fbsk.config.PrefetchStatus(ctx, fbsk.md.TlfID(),
fbsk.md.Data().Dir.BlockPointer)
fbs.PrefetchStatus = prefetchStatus.String()
fbs.RootBlockID = fbsk.md.Data().Dir.BlockPointer.ID.String()
if fbsk.quotaUsage == nil {
loggerSuffix := fmt.Sprintf("status-%s", fbsk.md.TlfID())
chargedTo, err := chargedToForTLF(
ctx, fbsk.config.KBPKI(), fbsk.config.KBPKI(),
fbsk.md.GetTlfHandle())
if err != nil {
return FolderBranchStatus{}, nil, tlf.NullID, err
}
// TODO: somehow share this quota usage instance with the
// journal for the TLF?
if chargedTo.IsTeam() {
fbsk.quotaUsage = NewEventuallyConsistentTeamQuotaUsage(
fbsk.config, chargedTo.AsTeamOrBust(), loggerSuffix)
} else {
fbsk.quotaUsage = NewEventuallyConsistentQuotaUsage(
fbsk.config, loggerSuffix)
}
}
_, usageBytes, limitBytes, gitUsageBytes, gitLimitBytes, quErr :=
fbsk.quotaUsage.GetAllTypes(ctx, 0, 0)
if quErr != nil {
// The error is ignored here so that other fields can
// still be populated even if this fails.
log := fbsk.config.MakeLogger("")
log.CDebugf(ctx, "Getting quota usage error: %v", quErr)
}
fbs.UsageBytes = usageBytes
fbs.LimitBytes = limitBytes
fbs.GitUsageBytes = gitUsageBytes
fbs.GitLimitBytes = gitLimitBytes
}
fbs.DirtyPaths = fbsk.convertNodesToPathsLocked(fbsk.dirtyNodes)
fbs.Unmerged = fbsk.unmerged
fbs.Merged = fbsk.merged
if fbsk.permErr != nil {
fbs.PermanentErr = fbsk.permErr.Error()
}
return fbs, fbsk.updateChan, tlfID, nil
}
// getStatus returns a FolderBranchStatus-representation of the
// current status. If blocks != nil, the paths of any unflushed files
// in the journals will be included in the status. The returned
// channel is closed whenever the status changes, except for journal
// status changes.
func (fbsk *folderBranchStatusKeeper) getStatus(ctx context.Context,
blocks *folderBlockOps) (FolderBranchStatus, <-chan StatusUpdate, error) {
fbs, ch, tlfID, err := fbsk.getStatusWithoutJournaling(ctx)
if err != nil {
return FolderBranchStatus{}, nil, err
}
if tlfID == tlf.NullID {
return fbs, ch, nil
}
// Fetch journal info without holding any locks, to avoid possible
// deadlocks with folderBlockOps.
// TODO: Ideally, the journal would push status
// updates to this object instead, so we can notify
// listeners.
jServer, err := GetJournalServer(fbsk.config)
if err != nil {
return fbs, ch, nil
}
var jStatus TLFJournalStatus
if blocks != nil {
jStatus, err =
jServer.JournalStatusWithPaths(ctx, tlfID, blocks)
} else {
jStatus, err =
jServer.JournalStatus(tlfID)
}
if err != nil {
log := fbsk.config.MakeLogger("")
log.CWarningf(ctx, "Error getting journal status for %s: %v",
tlfID, err)
} else {
fbs.Journal = &jStatus
}
return fbs, ch, nil
}