Skip to content

Commit edacb8f

Browse files
author
Shlomi Noach
authored
Merge pull request #116 from github/nice-ratio-float
nice-ratio is now float64
2 parents 836f751 + be8a023 commit edacb8f

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

go/base/context.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type MigrationContext struct {
6363

6464
defaultNumRetries int64
6565
ChunkSize int64
66-
NiceRatio int64
66+
niceRatio float64
6767
MaxLagMillisecondsThrottleThreshold int64
6868
replicationLagQuery string
6969
throttleControlReplicaKeys *mysql.InstanceKeyMap
@@ -382,6 +382,26 @@ func (this *MigrationContext) GetCriticalLoad() LoadMap {
382382
return this.criticalLoad.Duplicate()
383383
}
384384

385+
func (this *MigrationContext) GetNiceRatio() float64 {
386+
this.throttleMutex.Lock()
387+
defer this.throttleMutex.Unlock()
388+
389+
return this.niceRatio
390+
}
391+
392+
func (this *MigrationContext) SetNiceRatio(newRatio float64) {
393+
if newRatio < 0.0 {
394+
newRatio = 0.0
395+
}
396+
if newRatio > 100.0 {
397+
newRatio = 100.0
398+
}
399+
400+
this.throttleMutex.Lock()
401+
defer this.throttleMutex.Unlock()
402+
this.niceRatio = newRatio
403+
}
404+
385405
// ReadMaxLoad parses the `--max-load` flag, which is in multiple key-value format,
386406
// such as: 'Threads_running=100,Threads_connected=500'
387407
// It only applies changes in case there's no parsing error.

go/cmd/gh-ost/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func main() {
7272
chunkSize := flag.Int64("chunk-size", 1000, "amount of rows to handle in each iteration (allowed range: 100-100,000)")
7373
defaultRetries := flag.Int64("default-retries", 60, "Default number of retries for various operations before panicking")
7474
cutOverLockTimeoutSeconds := flag.Int64("cut-over-lock-timeout-seconds", 3, "Max number of seconds to hold locks on tables while attempting to cut-over (retry attempted when lock exceeds timeout)")
75-
flag.Int64Var(&migrationContext.NiceRatio, "nice-ratio", 0, "force being 'nice', imply sleep time per chunk time. Example values: 0 is aggressive. 3: for every ms spend in a rowcopy chunk, spend 3ms sleeping immediately after")
75+
niceRatio := flag.Float64("nice-ratio", 0, "force being 'nice', imply sleep time per chunk time; range: [0.0..100.0]. Example values: 0 is aggressive. 1.5: for every ms spend in a rowcopy chunk, spend 1.5ms sleeping immediately after")
7676

7777
maxLagMillis := flag.Int64("max-lag-millis", 1500, "replication lag at which to throttle operation")
7878
replicationLagQuery := flag.String("replication-lag-query", "", "Query that detects replication lag in seconds. Result can be a floating point (by default gh-ost issues SHOW SLAVE STATUS and reads Seconds_behind_master). If you're using pt-heartbeat, query would be something like: SELECT ROUND(UNIX_TIMESTAMP() - MAX(UNIX_TIMESTAMP(ts))) AS delay FROM my_schema.heartbeat")
@@ -169,6 +169,7 @@ func main() {
169169
if migrationContext.ServeSocketFile == "" {
170170
migrationContext.ServeSocketFile = fmt.Sprintf("/tmp/gh-ost.%s.%s.sock", migrationContext.DatabaseName, migrationContext.OriginalTableName)
171171
}
172+
migrationContext.SetNiceRatio(*niceRatio)
172173
migrationContext.SetChunkSize(*chunkSize)
173174
migrationContext.SetMaxLagMillisecondsThrottleThreshold(*maxLagMillis)
174175
migrationContext.SetReplicationLagQuery(*replicationLagQuery)

go/logic/migrator.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,11 @@ help # This message
705705
}
706706
case "nice-ratio":
707707
{
708-
if niceRatio, err := strconv.Atoi(arg); err != nil {
708+
if niceRatio, err := strconv.ParseFloat(arg, 64); err != nil {
709709
fmt.Fprintf(writer, "%s\n", err.Error())
710710
return log.Errore(err)
711711
} else {
712-
atomic.StoreInt64(&this.migrationContext.NiceRatio, int64(niceRatio))
712+
this.migrationContext.SetNiceRatio(niceRatio)
713713
this.printStatus(ForcePrintStatusAndHint, writer)
714714
}
715715
}
@@ -866,12 +866,12 @@ func (this *Migrator) printMigrationStatusHint(writers ...io.Writer) {
866866
))
867867
maxLoad := this.migrationContext.GetMaxLoad()
868868
criticalLoad := this.migrationContext.GetCriticalLoad()
869-
fmt.Fprintln(w, fmt.Sprintf("# chunk-size: %+v; max-lag-millis: %+vms; max-load: %s; critical-load: %s; nice-ratio: %d",
869+
fmt.Fprintln(w, fmt.Sprintf("# chunk-size: %+v; max-lag-millis: %+vms; max-load: %s; critical-load: %s; nice-ratio: %f",
870870
atomic.LoadInt64(&this.migrationContext.ChunkSize),
871871
atomic.LoadInt64(&this.migrationContext.MaxLagMillisecondsThrottleThreshold),
872872
maxLoad.String(),
873873
criticalLoad.String(),
874-
atomic.LoadInt64(&this.migrationContext.NiceRatio),
874+
this.migrationContext.GetNiceRatio(),
875875
))
876876
if replicationLagQuery := this.migrationContext.GetReplicationLagQuery(); replicationLagQuery != "" {
877877
fmt.Fprintln(w, fmt.Sprintf("# Replication lag query: %+v",
@@ -1190,9 +1190,10 @@ func (this *Migrator) executeWriteFuncs() error {
11901190
if err := copyRowsFunc(); err != nil {
11911191
return log.Errore(err)
11921192
}
1193-
if niceRatio := atomic.LoadInt64(&this.migrationContext.NiceRatio); niceRatio > 0 {
1193+
if niceRatio := this.migrationContext.GetNiceRatio(); niceRatio > 0 {
11941194
copyRowsDuration := time.Now().Sub(copyRowsStartTime)
1195-
sleepTime := copyRowsDuration * time.Duration(niceRatio)
1195+
sleepTimeNanosecondFloat64 := niceRatio * float64(copyRowsDuration.Nanoseconds())
1196+
sleepTime := time.Duration(time.Duration(int64(sleepTimeNanosecondFloat64)) * time.Nanosecond)
11961197
time.Sleep(sleepTime)
11971198
}
11981199
}

0 commit comments

Comments
 (0)