-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
progress.go
171 lines (144 loc) · 4.04 KB
/
progress.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
// Copyright 2020 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 search
import (
"sync"
"time"
"github.com/keybase/client/go/kbfs/libkbfs"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/pkg/errors"
)
type indexType int
const (
indexNone indexType = iota
indexFull
indexIncremental
)
// Progress represents the current state of the indexer, and how far
// along the indexing is.
type Progress struct {
clock libkbfs.Clock
lock sync.RWMutex
tlfSizesToIndex map[tlf.ID]uint64
currTlf tlf.ID
currTotalToIndex uint64
currHasIndexed uint64
currIndexType indexType
currStartTime time.Time
lastIndexRate float64 // (bytes/second)
}
// NewProgress creates a new Progress instance.
func NewProgress(clock libkbfs.Clock) *Progress {
return &Progress{
clock: clock,
tlfSizesToIndex: make(map[tlf.ID]uint64),
}
}
func (p *Progress) tlfQueue(id tlf.ID, sizeEstimate uint64) {
p.lock.Lock()
defer p.lock.Unlock()
// Overwrite whatever was already there.
p.tlfSizesToIndex[id] = sizeEstimate
}
func (p *Progress) tlfUnqueue(id tlf.ID) {
p.lock.Lock()
defer p.lock.Unlock()
delete(p.tlfSizesToIndex, id)
}
func (p *Progress) startIndex(
id tlf.ID, sizeEstimate uint64, t indexType) error {
p.lock.Lock()
defer p.lock.Unlock()
if p.currTlf != tlf.NullID {
return errors.Errorf("Cannot index %s before finishing index of %s",
id, p.currTlf)
}
p.currTlf = id
delete(p.tlfSizesToIndex, id)
p.currTotalToIndex = sizeEstimate
p.currHasIndexed = 0
p.currIndexType = t
p.currStartTime = p.clock.Now()
return nil
}
func (p *Progress) indexedBytes(size uint64) {
p.lock.Lock()
defer p.lock.Unlock()
p.currHasIndexed += size
if p.currHasIndexed > p.currTotalToIndex {
// The provided size estimate was wrong. But we don't know by
// how much. So just add the newly-indexed bytes onto it, ot
// make sure we're still under the limit.
p.currTotalToIndex += size
}
}
func (p *Progress) finishIndex(id tlf.ID) error {
p.lock.Lock()
defer p.lock.Unlock()
if id != p.currTlf {
return errors.Errorf(
"Cannot finish index for %s, because %s is the current TLF",
id, p.currTlf)
}
// Estimate how long these bytes took to index.
timeSecs := p.clock.Now().Sub(p.currStartTime).Seconds()
if timeSecs > 0 {
p.lastIndexRate = float64(p.currHasIndexed) / timeSecs
} else {
p.lastIndexRate = 0
}
p.currTlf = tlf.NullID
p.currTotalToIndex = 0
p.currHasIndexed = 0
p.currIndexType = indexNone
p.currStartTime = time.Time{}
return nil
}
func (p *Progress) fillInProgressRecord(
total, soFar uint64, rate float64, rec *keybase1.IndexProgressRecord) {
if rate > 0 {
bytesLeft := total - soFar
timeLeft := time.Duration(
(float64(bytesLeft) / rate) * float64(time.Second))
rec.EndEstimate = keybase1.ToTime(p.clock.Now().Add(timeLeft))
}
rec.BytesSoFar = int64(soFar)
rec.BytesTotal = int64(total)
}
// GetStatus returns the current progress status.
func (p *Progress) GetStatus() (
currProgress, overallProgress keybase1.IndexProgressRecord,
currTlf tlf.ID, queuedTlfs []tlf.ID) {
p.lock.RLock()
defer p.lock.RUnlock()
// At what rate is the current indexer running?
rate := p.lastIndexRate
if !p.currStartTime.IsZero() && p.currHasIndexed != 0 {
timeSecs := p.clock.Now().Sub(p.currStartTime).Seconds()
rate = float64(p.currHasIndexed) / timeSecs
}
if p.currTlf != tlf.NullID {
p.fillInProgressRecord(
p.currTotalToIndex, p.currHasIndexed, rate, &currProgress)
}
queuedTlfs = make([]tlf.ID, 0, len(p.tlfSizesToIndex))
var totalSize, soFar uint64
if p.currTlf != tlf.NullID {
totalSize += p.currTotalToIndex
soFar += p.currHasIndexed
}
for id, size := range p.tlfSizesToIndex {
if id == p.currTlf {
continue
}
totalSize += size
queuedTlfs = append(queuedTlfs, id)
}
if totalSize != 0 {
p.fillInProgressRecord(
totalSize, soFar, rate, &overallProgress)
}
return currProgress, overallProgress, p.currTlf, queuedTlfs
}