Skip to content

Commit 09052e6

Browse files
morgomeiji163
andauthored
use last second copy rate to project ETA (#1231)
* use current copy rate to project ETA * make linter happy * Replace `tests.ExpectEquals` with `require.Equal` in tests --------- Co-authored-by: meiji163 <[email protected]>
1 parent 690b1e1 commit 09052e6

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

go/base/context.go

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ type MigrationContext struct {
194194
CurrentLag int64
195195
currentProgress uint64
196196
etaNanoseonds int64
197+
EtaRowsPerSecond int64
197198
ThrottleHTTPIntervalMillis int64
198199
ThrottleHTTPStatusCode int64
199200
ThrottleHTTPTimeoutMillis int64

go/logic/migrator.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -819,11 +819,18 @@ func (this *Migrator) initiateStatus() {
819819
this.printStatus(ForcePrintStatusAndHintRule)
820820
ticker := time.NewTicker(time.Second)
821821
defer ticker.Stop()
822+
var previousCount int64
822823
for range ticker.C {
823824
if atomic.LoadInt64(&this.finishedMigrating) > 0 {
824825
return
825826
}
826827
go this.printStatus(HeuristicPrintStatusRule)
828+
totalCopied := atomic.LoadInt64(&this.migrationContext.TotalRowsCopied)
829+
if previousCount > 0 {
830+
copiedThisLoop := totalCopied - previousCount
831+
atomic.StoreInt64(&this.migrationContext.EtaRowsPerSecond, copiedThisLoop)
832+
}
833+
previousCount = totalCopied
827834
}
828835
}
829836

@@ -925,9 +932,20 @@ func (this *Migrator) getMigrationETA(rowsEstimate int64) (eta string, duration
925932
duration = 0
926933
} else if progressPct >= 0.1 {
927934
totalRowsCopied := this.migrationContext.GetTotalRowsCopied()
928-
elapsedRowCopySeconds := this.migrationContext.ElapsedRowCopyTime().Seconds()
929-
totalExpectedSeconds := elapsedRowCopySeconds * float64(rowsEstimate) / float64(totalRowsCopied)
930-
etaSeconds := totalExpectedSeconds - elapsedRowCopySeconds
935+
etaRowsPerSecond := atomic.LoadInt64(&this.migrationContext.EtaRowsPerSecond)
936+
var etaSeconds float64
937+
// If there is data available on our current row-copies-per-second rate, use it.
938+
// Otherwise we can fallback to the total elapsed time and extrapolate.
939+
// This is going to be less accurate on a longer copy as the insert rate
940+
// will tend to slow down.
941+
if etaRowsPerSecond > 0 {
942+
remainingRows := float64(rowsEstimate) - float64(totalRowsCopied)
943+
etaSeconds = remainingRows / float64(etaRowsPerSecond)
944+
} else {
945+
elapsedRowCopySeconds := this.migrationContext.ElapsedRowCopyTime().Seconds()
946+
totalExpectedSeconds := elapsedRowCopySeconds * float64(rowsEstimate) / float64(totalRowsCopied)
947+
etaSeconds = totalExpectedSeconds - elapsedRowCopySeconds
948+
}
931949
if etaSeconds >= 0 {
932950
duration = time.Duration(etaSeconds) * time.Second
933951
} else {

go/logic/migrator_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ func TestMigratorGetMigrationStateAndETA(t *testing.T) {
215215
require.Equal(t, "4h29m44s", eta)
216216
require.Equal(t, "4h29m44s", etaDuration.String())
217217
}
218+
{
219+
// Test using rows-per-second added data.
220+
migrationContext.TotalRowsCopied = 456
221+
migrationContext.EtaRowsPerSecond = 100
222+
state, eta, etaDuration := migrator.getMigrationStateAndETA(123456)
223+
require.Equal(t, "migrating", state)
224+
require.Equal(t, "20m30s", eta)
225+
require.Equal(t, "20m30s", etaDuration.String())
226+
}
218227
{
219228
migrationContext.TotalRowsCopied = 456
220229
state, eta, etaDuration := migrator.getMigrationStateAndETA(456)

0 commit comments

Comments
 (0)