forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.go
63 lines (54 loc) · 1.96 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
// Copyright 2017 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/pkg/ccl/LICENSE
package sqlccl
import (
"time"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
// For both backups and restores, we compute progress as the number of completed
// export or import requests, respectively, divided by the total number of
// requests. To avoid hammering the system.jobs table, when a response comes
// back, we issue a progress update only if a) it's been a duration of
// progressTimeThreshold since the last update, or b) the difference between the
// last logged fractionCompleted and the current fractionCompleted is more than
// progressFractionThreshold.
const (
progressTimeThreshold = time.Second
progressFractionThreshold = 0.05
)
type jobProgressLogger struct {
// These fields must be externally initialized.
jobLogger *sql.JobLogger
totalChunks int
// The remaining fields are for internal use only.
mu struct {
syncutil.Mutex
completedChunks int
lastReportedAt time.Time
lastReportedFraction float32
}
}
func (jpl *jobProgressLogger) chunkFinished(ctx context.Context) error {
jpl.mu.Lock()
jpl.mu.completedChunks++
fraction := float32(jpl.mu.completedChunks) / float32(jpl.totalChunks)
shouldLogProgress := fraction-jpl.mu.lastReportedFraction > progressFractionThreshold ||
jpl.mu.lastReportedAt.Add(progressTimeThreshold).Before(timeutil.Now())
if shouldLogProgress {
jpl.mu.lastReportedAt = timeutil.Now()
jpl.mu.lastReportedFraction = fraction
}
jpl.mu.Unlock()
if shouldLogProgress {
return jpl.jobLogger.Progressed(ctx, fraction)
}
return nil
}