Skip to content

Commit af43fbd

Browse files
author
Shlomi Noach
authored
Merge pull request #788 from github/context-status
context, status and hooks: progressPct and CurrentLag
2 parents dcc3e90 + d0ce7c0 commit af43fbd

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

doc/hooks.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ The following variables are available on all hooks:
6565
- `GH_OST_ELAPSED_COPY_SECONDS` - row-copy time (excluding startup, row-count and postpone time)
6666
- `GH_OST_ESTIMATED_ROWS` - estimated total rows in table
6767
- `GH_OST_COPIED_ROWS` - number of rows copied by `gh-ost`
68+
- `GH_OST_INSPECTED_LAG` - lag in seconds (floating point) of inspected server
69+
- `GH_OST_PROGRESS` - progress pct ([0..100], floating point) of migration
6870
- `GH_OST_MIGRATED_HOST`
6971
- `GH_OST_INSPECTED_HOST`
7072
- `GH_OST_EXECUTING_HOST`

go/base/context.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package base
77

88
import (
99
"fmt"
10+
"math"
1011
"os"
1112
"regexp"
1213
"strings"
@@ -174,6 +175,7 @@ type MigrationContext struct {
174175
pointOfInterestTime time.Time
175176
pointOfInterestTimeMutex *sync.Mutex
176177
CurrentLag int64
178+
currentProgress uint64
177179
ThrottleHTTPStatusCode int64
178180
controlReplicasLagResult mysql.ReplicationLagResult
179181
TotalRowsCopied int64
@@ -428,6 +430,20 @@ func (this *MigrationContext) MarkRowCopyEndTime() {
428430
this.RowCopyEndTime = time.Now()
429431
}
430432

433+
func (this *MigrationContext) GetCurrentLagDuration() time.Duration {
434+
return time.Duration(atomic.LoadInt64(&this.CurrentLag))
435+
}
436+
437+
func (this *MigrationContext) GetProgressPct() float64 {
438+
return math.Float64frombits(atomic.LoadUint64(&this.currentProgress))
439+
}
440+
441+
func (this *MigrationContext) SetProgressPct(progressPct float64) {
442+
atomic.StoreUint64(&this.currentProgress, math.Float64bits(progressPct))
443+
}
444+
445+
// math.Float64bits([f=0..100])
446+
431447
// GetTotalRowsCopied returns the accurate number of rows being copied (affected)
432448
// This is not exactly the same as the rows being iterated via chunks, but potentially close enough
433449
func (this *MigrationContext) GetTotalRowsCopied() int64 {

go/logic/hooks.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func (this *HooksExecutor) applyEnvironmentVariables(extraVariables ...string) [
6363
env = append(env, fmt.Sprintf("GH_OST_MIGRATED_HOST=%s", this.migrationContext.GetApplierHostname()))
6464
env = append(env, fmt.Sprintf("GH_OST_INSPECTED_HOST=%s", this.migrationContext.GetInspectorHostname()))
6565
env = append(env, fmt.Sprintf("GH_OST_EXECUTING_HOST=%s", this.migrationContext.Hostname))
66+
env = append(env, fmt.Sprintf("GH_OST_INSPECTED_LAG=%f", this.migrationContext.GetCurrentLagDuration().Seconds()))
67+
env = append(env, fmt.Sprintf("GH_OST_PROGRESS=%f", this.migrationContext.GetProgressPct()))
6668
env = append(env, fmt.Sprintf("GH_OST_HOOKS_HINT=%s", this.migrationContext.HooksHintMessage))
6769
env = append(env, fmt.Sprintf("GH_OST_HOOKS_HINT_OWNER=%s", this.migrationContext.HooksHintOwner))
6870
env = append(env, fmt.Sprintf("GH_OST_HOOKS_HINT_TOKEN=%s", this.migrationContext.HooksHintToken))

go/logic/migrator.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,8 @@ func (this *Migrator) printStatus(rule PrintStatusRule, writers ...io.Writer) {
895895
} else {
896896
progressPct = 100.0 * float64(totalRowsCopied) / float64(rowsEstimate)
897897
}
898+
// we take the opportunity to update migration context with progressPct
899+
this.migrationContext.SetProgressPct(progressPct)
898900
// Before status, let's see if we should print a nice reminder for what exactly we're doing here.
899901
shouldPrintMigrationStatusHint := (elapsedSeconds%600 == 0)
900902
if rule == ForcePrintStatusAndHintRule {
@@ -911,7 +913,7 @@ func (this *Migrator) printStatus(rule PrintStatusRule, writers ...io.Writer) {
911913
eta := "N/A"
912914
if progressPct >= 100.0 {
913915
eta = "due"
914-
} else if progressPct >= 1.0 {
916+
} else if progressPct >= 0.1 {
915917
elapsedRowCopySeconds := this.migrationContext.ElapsedRowCopyTime().Seconds()
916918
totalExpectedSeconds := elapsedRowCopySeconds * float64(rowsEstimate) / float64(totalRowsCopied)
917919
etaSeconds = totalExpectedSeconds - elapsedRowCopySeconds
@@ -958,12 +960,13 @@ func (this *Migrator) printStatus(rule PrintStatusRule, writers ...io.Writer) {
958960

959961
currentBinlogCoordinates := *this.eventsStreamer.GetCurrentBinlogCoordinates()
960962

961-
status := fmt.Sprintf("Copy: %d/%d %.1f%%; Applied: %d; Backlog: %d/%d; Time: %+v(total), %+v(copy); streamer: %+v; State: %s; ETA: %s",
963+
status := fmt.Sprintf("Copy: %d/%d %.1f%%; Applied: %d; Backlog: %d/%d; Time: %+v(total), %+v(copy); streamer: %+v; Lag: %.2fs, State: %s; ETA: %s",
962964
totalRowsCopied, rowsEstimate, progressPct,
963965
atomic.LoadInt64(&this.migrationContext.TotalDMLEventsApplied),
964966
len(this.applyEventsQueue), cap(this.applyEventsQueue),
965967
base.PrettifyDurationOutput(elapsedTime), base.PrettifyDurationOutput(this.migrationContext.ElapsedRowCopyTime()),
966968
currentBinlogCoordinates,
969+
this.migrationContext.GetCurrentLagDuration().Seconds(),
967970
state,
968971
eta,
969972
)

0 commit comments

Comments
 (0)