2016-08-25 12:42:49 +00:00
|
|
|
/*
|
2016-08-19 12:52:49 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 GitHub Inc.
|
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-08-20 06:24:20 +00:00
|
|
|
"path/filepath"
|
2016-09-02 08:48:29 +00:00
|
|
|
"sync/atomic"
|
2016-08-19 12:52:49 +00:00
|
|
|
|
|
|
|
"github.com/github/gh-ost/go/base"
|
2016-08-30 07:45:02 +00:00
|
|
|
"github.com/outbrain/golib/log"
|
2016-08-19 12:52:49 +00:00
|
|
|
)
|
|
|
|
|
2016-08-23 09:35:48 +00:00
|
|
|
const (
|
|
|
|
onStartup = "gh-ost-on-startup"
|
|
|
|
onValidated = "gh-ost-on-validated"
|
2016-08-25 11:55:22 +00:00
|
|
|
onRowCountComplete = "gh-ost-on-rowcount-complete"
|
2016-08-23 09:40:32 +00:00
|
|
|
onBeforeRowCopy = "gh-ost-on-before-row-copy"
|
2016-08-23 09:35:48 +00:00
|
|
|
onRowCopyComplete = "gh-ost-on-row-copy-complete"
|
|
|
|
onBeginPostponed = "gh-ost-on-begin-postponed"
|
2016-08-23 09:40:32 +00:00
|
|
|
onBeforeCutOver = "gh-ost-on-before-cut-over"
|
2016-08-23 09:35:48 +00:00
|
|
|
onInteractiveCommand = "gh-ost-on-interactive-command"
|
|
|
|
onSuccess = "gh-ost-on-success"
|
|
|
|
onFailure = "gh-ost-on-failure"
|
|
|
|
onStatus = "gh-ost-on-status"
|
|
|
|
onStopReplication = "gh-ost-on-stop-replication"
|
2017-02-07 07:31:52 +00:00
|
|
|
onStartReplication = "gh-ost-on-start-replication"
|
2016-08-23 09:35:48 +00:00
|
|
|
)
|
|
|
|
|
2016-08-19 12:52:49 +00:00
|
|
|
type HooksExecutor struct {
|
|
|
|
migrationContext *base.MigrationContext
|
|
|
|
}
|
|
|
|
|
2017-08-08 20:36:54 +00:00
|
|
|
func NewHooksExecutor(migrationContext *base.MigrationContext) *HooksExecutor {
|
2016-08-19 12:52:49 +00:00
|
|
|
return &HooksExecutor{
|
2017-08-08 20:36:54 +00:00
|
|
|
migrationContext: migrationContext,
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-20 06:24:20 +00:00
|
|
|
func (this *HooksExecutor) initHooks() error {
|
2016-08-19 12:52:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-23 21:45:21 +00:00
|
|
|
func (this *HooksExecutor) applyEnvironmentVariables(extraVariables ...string) []string {
|
2016-08-19 12:52:49 +00:00
|
|
|
env := os.Environ()
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_DATABASE_NAME=%s", this.migrationContext.DatabaseName))
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_TABLE_NAME=%s", this.migrationContext.OriginalTableName))
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_GHOST_TABLE_NAME=%s", this.migrationContext.GetGhostTableName()))
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_OLD_TABLE_NAME=%s", this.migrationContext.GetOldTableName()))
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_DDL=%s", this.migrationContext.AlterStatement))
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_ELAPSED_SECONDS=%f", this.migrationContext.ElapsedTime().Seconds()))
|
2016-09-02 08:48:29 +00:00
|
|
|
env = append(env, fmt.Sprintf("GH_OST_ELAPSED_COPY_SECONDS=%f", this.migrationContext.ElapsedRowCopyTime().Seconds()))
|
|
|
|
estimatedRows := atomic.LoadInt64(&this.migrationContext.RowsEstimate) + atomic.LoadInt64(&this.migrationContext.RowsDeltaEstimate)
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_ESTIMATED_ROWS=%d", estimatedRows))
|
|
|
|
totalRowsCopied := this.migrationContext.GetTotalRowsCopied()
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_COPIED_ROWS=%d", totalRowsCopied))
|
2016-10-04 19:18:44 +00:00
|
|
|
env = append(env, fmt.Sprintf("GH_OST_MIGRATED_HOST=%s", this.migrationContext.GetApplierHostname()))
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_INSPECTED_HOST=%s", this.migrationContext.GetInspectorHostname()))
|
2016-08-23 09:35:48 +00:00
|
|
|
env = append(env, fmt.Sprintf("GH_OST_EXECUTING_HOST=%s", this.migrationContext.Hostname))
|
2019-10-06 14:08:35 +00:00
|
|
|
env = append(env, fmt.Sprintf("GH_OST_INSPECTED_LAG=%f", this.migrationContext.GetCurrentLagDuration().Seconds()))
|
2021-02-03 09:02:58 +00:00
|
|
|
env = append(env, fmt.Sprintf("GH_OST_HEARTBEAT_LAG=%f", this.migrationContext.TimeSinceLastHeartbeatOnChangelog().Seconds()))
|
2019-10-07 03:59:56 +00:00
|
|
|
env = append(env, fmt.Sprintf("GH_OST_PROGRESS=%f", this.migrationContext.GetProgressPct()))
|
2016-08-25 11:55:22 +00:00
|
|
|
env = append(env, fmt.Sprintf("GH_OST_HOOKS_HINT=%s", this.migrationContext.HooksHintMessage))
|
2019-01-24 11:06:20 +00:00
|
|
|
env = append(env, fmt.Sprintf("GH_OST_HOOKS_HINT_OWNER=%s", this.migrationContext.HooksHintOwner))
|
|
|
|
env = append(env, fmt.Sprintf("GH_OST_HOOKS_HINT_TOKEN=%s", this.migrationContext.HooksHintToken))
|
2018-09-18 04:39:30 +00:00
|
|
|
env = append(env, fmt.Sprintf("GH_OST_DRY_RUN=%t", this.migrationContext.Noop))
|
2016-08-23 09:35:48 +00:00
|
|
|
|
|
|
|
for _, variable := range extraVariables {
|
|
|
|
env = append(env, variable)
|
|
|
|
}
|
2016-08-19 12:52:49 +00:00
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2016-08-23 09:35:48 +00:00
|
|
|
// executeHook executes a command, and sets relevant environment variables
|
2016-08-25 14:08:49 +00:00
|
|
|
// combined output & error are printed to gh-ost's standard error.
|
2016-08-23 09:35:48 +00:00
|
|
|
func (this *HooksExecutor) executeHook(hook string, extraVariables ...string) error {
|
|
|
|
cmd := exec.Command(hook)
|
2017-04-23 21:45:21 +00:00
|
|
|
cmd.Env = this.applyEnvironmentVariables(extraVariables...)
|
2016-08-19 12:52:49 +00:00
|
|
|
|
2016-08-25 14:08:49 +00:00
|
|
|
combinedOutput, err := cmd.CombinedOutput()
|
|
|
|
fmt.Fprintln(os.Stderr, string(combinedOutput))
|
|
|
|
return log.Errore(err)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 06:24:20 +00:00
|
|
|
func (this *HooksExecutor) detectHooks(baseName string) (hooks []string, err error) {
|
|
|
|
if this.migrationContext.HooksPath == "" {
|
|
|
|
return hooks, err
|
|
|
|
}
|
|
|
|
pattern := fmt.Sprintf("%s/%s*", this.migrationContext.HooksPath, baseName)
|
|
|
|
hooks, err = filepath.Glob(pattern)
|
|
|
|
return hooks, err
|
|
|
|
}
|
|
|
|
|
2016-08-23 09:35:48 +00:00
|
|
|
func (this *HooksExecutor) executeHooks(baseName string, extraVariables ...string) error {
|
2016-08-20 06:24:20 +00:00
|
|
|
hooks, err := this.detectHooks(baseName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, hook := range hooks {
|
2016-08-25 12:42:49 +00:00
|
|
|
log.Infof("executing %+v hook: %+v", baseName, hook)
|
2016-08-23 09:35:48 +00:00
|
|
|
if err := this.executeHook(hook, extraVariables...); err != nil {
|
2016-08-20 06:24:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-19 12:52:49 +00:00
|
|
|
func (this *HooksExecutor) onStartup() error {
|
2016-08-23 09:35:48 +00:00
|
|
|
return this.executeHooks(onStartup)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HooksExecutor) onValidated() error {
|
2016-08-23 09:35:48 +00:00
|
|
|
return this.executeHooks(onValidated)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 11:55:22 +00:00
|
|
|
func (this *HooksExecutor) onRowCountComplete() error {
|
|
|
|
return this.executeHooks(onRowCountComplete)
|
|
|
|
}
|
2016-08-23 09:40:32 +00:00
|
|
|
func (this *HooksExecutor) onBeforeRowCopy() error {
|
|
|
|
return this.executeHooks(onBeforeRowCopy)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HooksExecutor) onRowCopyComplete() error {
|
2016-08-23 09:35:48 +00:00
|
|
|
return this.executeHooks(onRowCopyComplete)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HooksExecutor) onBeginPostponed() error {
|
2016-08-23 09:35:48 +00:00
|
|
|
return this.executeHooks(onBeginPostponed)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 09:40:32 +00:00
|
|
|
func (this *HooksExecutor) onBeforeCutOver() error {
|
|
|
|
return this.executeHooks(onBeforeCutOver)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HooksExecutor) onInteractiveCommand(command string) error {
|
2016-08-23 09:35:48 +00:00
|
|
|
v := fmt.Sprintf("GH_OST_COMMAND='%s'", command)
|
|
|
|
return this.executeHooks(onInteractiveCommand, v)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HooksExecutor) onSuccess() error {
|
2016-08-23 09:35:48 +00:00
|
|
|
return this.executeHooks(onSuccess)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HooksExecutor) onFailure() error {
|
2016-08-23 09:35:48 +00:00
|
|
|
return this.executeHooks(onFailure)
|
2016-08-19 12:52:49 +00:00
|
|
|
}
|
2016-08-22 14:24:41 +00:00
|
|
|
|
2016-08-29 08:44:43 +00:00
|
|
|
func (this *HooksExecutor) onStatus(statusMessage string) error {
|
|
|
|
v := fmt.Sprintf("GH_OST_STATUS='%s'", statusMessage)
|
|
|
|
return this.executeHooks(onStatus, v)
|
2016-08-23 09:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HooksExecutor) onStopReplication() error {
|
|
|
|
return this.executeHooks(onStopReplication)
|
2016-08-22 14:24:41 +00:00
|
|
|
}
|
2017-02-07 07:31:52 +00:00
|
|
|
|
|
|
|
func (this *HooksExecutor) onStartReplication() error {
|
|
|
|
return this.executeHooks(onStartReplication)
|
|
|
|
}
|