2016-04-01 11:36:56 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 GitHub Inc.
|
2016-05-16 09:09:17 +00:00
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
2016-04-01 11:36:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package base
|
|
|
|
|
2016-04-04 13:29:02 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-08 12:35:06 +00:00
|
|
|
"strconv"
|
2016-04-08 08:34:44 +00:00
|
|
|
"strings"
|
2016-04-11 15:27:16 +00:00
|
|
|
"sync"
|
2016-04-08 08:34:44 +00:00
|
|
|
"sync/atomic"
|
2016-04-07 13:57:12 +00:00
|
|
|
"time"
|
2016-04-04 13:29:02 +00:00
|
|
|
|
2016-05-16 09:09:17 +00:00
|
|
|
"github.com/github/gh-ost/go/mysql"
|
|
|
|
"github.com/github/gh-ost/go/sql"
|
2016-05-03 07:28:48 +00:00
|
|
|
|
|
|
|
"gopkg.in/gcfg.v1"
|
2016-04-04 13:29:02 +00:00
|
|
|
)
|
2016-04-01 11:36:56 +00:00
|
|
|
|
2016-04-05 07:14:22 +00:00
|
|
|
// RowsEstimateMethod is the type of row number estimation
|
2016-04-04 10:27:51 +00:00
|
|
|
type RowsEstimateMethod string
|
|
|
|
|
|
|
|
const (
|
|
|
|
TableStatusRowsEstimate RowsEstimateMethod = "TableStatusRowsEstimate"
|
|
|
|
ExplainRowsEstimate = "ExplainRowsEstimate"
|
|
|
|
CountRowsEstimate = "CountRowsEstimate"
|
|
|
|
)
|
|
|
|
|
2016-04-07 13:57:12 +00:00
|
|
|
const (
|
|
|
|
maxRetries = 10
|
|
|
|
)
|
|
|
|
|
2016-04-05 07:14:22 +00:00
|
|
|
// MigrationContext has the general, global state of migration. It is used by
|
|
|
|
// all components throughout the migration process.
|
2016-04-01 11:36:56 +00:00
|
|
|
type MigrationContext struct {
|
2016-04-14 11:37:56 +00:00
|
|
|
DatabaseName string
|
|
|
|
OriginalTableName string
|
|
|
|
AlterStatement string
|
|
|
|
|
2016-04-18 17:57:18 +00:00
|
|
|
CountTableRows bool
|
|
|
|
AllowedRunningOnMaster bool
|
|
|
|
SwitchToRowBinlogFormat bool
|
2016-04-14 11:37:56 +00:00
|
|
|
|
2016-05-03 07:28:48 +00:00
|
|
|
ConfigFile string
|
|
|
|
|
2016-04-08 08:34:44 +00:00
|
|
|
ChunkSize int64
|
2016-04-07 13:57:12 +00:00
|
|
|
MaxLagMillisecondsThrottleThreshold int64
|
2016-05-01 18:36:36 +00:00
|
|
|
ReplictionLagQuery string
|
|
|
|
ThrottleControlReplicaKeys *mysql.InstanceKeyMap
|
2016-04-08 08:34:44 +00:00
|
|
|
ThrottleFlagFile string
|
2016-04-11 15:27:16 +00:00
|
|
|
ThrottleAdditionalFlagFile string
|
2016-04-08 12:35:06 +00:00
|
|
|
MaxLoad map[string]int64
|
2016-05-17 12:40:37 +00:00
|
|
|
PostponeSwapTablesFlagFile string
|
2016-04-23 02:46:34 +00:00
|
|
|
SwapTablesTimeoutSeconds int64
|
2016-04-08 12:35:06 +00:00
|
|
|
|
2016-04-22 20:18:56 +00:00
|
|
|
Noop bool
|
|
|
|
TestOnReplica bool
|
|
|
|
OkToDropTable bool
|
|
|
|
QuickAndBumpySwapTables bool
|
2016-05-03 09:55:17 +00:00
|
|
|
InitiallyDropOldTable bool
|
|
|
|
InitiallyDropGhostTable bool
|
2016-04-18 17:57:18 +00:00
|
|
|
|
|
|
|
TableEngine string
|
|
|
|
RowsEstimate int64
|
|
|
|
UsedRowsEstimateMethod RowsEstimateMethod
|
|
|
|
OriginalBinlogFormat string
|
|
|
|
OriginalBinlogRowImage string
|
|
|
|
InspectorConnectionConfig *mysql.ConnectionConfig
|
|
|
|
ApplierConnectionConfig *mysql.ConnectionConfig
|
|
|
|
StartTime time.Time
|
|
|
|
RowCopyStartTime time.Time
|
|
|
|
LockTablesStartTime time.Time
|
|
|
|
RenameTablesStartTime time.Time
|
|
|
|
RenameTablesEndTime time.Time
|
|
|
|
CurrentLag int64
|
|
|
|
TotalRowsCopied int64
|
2016-04-19 11:25:32 +00:00
|
|
|
TotalDMLEventsApplied int64
|
2016-04-18 17:57:18 +00:00
|
|
|
isThrottled bool
|
|
|
|
throttleReason string
|
|
|
|
throttleMutex *sync.Mutex
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
OriginalTableColumns *sql.ColumnList
|
2016-04-08 12:35:06 +00:00
|
|
|
OriginalTableUniqueKeys [](*sql.UniqueKey)
|
2016-04-11 15:27:16 +00:00
|
|
|
GhostTableColumns *sql.ColumnList
|
2016-04-08 12:35:06 +00:00
|
|
|
GhostTableUniqueKeys [](*sql.UniqueKey)
|
|
|
|
UniqueKey *sql.UniqueKey
|
2016-04-11 15:27:16 +00:00
|
|
|
SharedColumns *sql.ColumnList
|
2016-04-08 12:35:06 +00:00
|
|
|
MigrationRangeMinValues *sql.ColumnValues
|
|
|
|
MigrationRangeMaxValues *sql.ColumnValues
|
|
|
|
Iteration int64
|
|
|
|
MigrationIterationRangeMinValues *sql.ColumnValues
|
|
|
|
MigrationIterationRangeMaxValues *sql.ColumnValues
|
2016-04-07 13:57:12 +00:00
|
|
|
|
|
|
|
CanStopStreaming func() bool
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
2016-04-01 14:05:44 +00:00
|
|
|
|
2016-04-04 10:27:51 +00:00
|
|
|
var context *MigrationContext
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
context = newMigrationContext()
|
|
|
|
}
|
2016-04-01 14:05:44 +00:00
|
|
|
|
|
|
|
func newMigrationContext() *MigrationContext {
|
2016-04-04 10:27:51 +00:00
|
|
|
return &MigrationContext{
|
2016-04-07 13:57:12 +00:00
|
|
|
ChunkSize: 1000,
|
|
|
|
InspectorConnectionConfig: mysql.NewConnectionConfig(),
|
2016-04-14 11:37:56 +00:00
|
|
|
ApplierConnectionConfig: mysql.NewConnectionConfig(),
|
2016-04-07 13:57:12 +00:00
|
|
|
MaxLagMillisecondsThrottleThreshold: 1000,
|
2016-04-23 02:46:34 +00:00
|
|
|
SwapTablesTimeoutSeconds: 3,
|
|
|
|
MaxLoad: make(map[string]int64),
|
|
|
|
throttleMutex: &sync.Mutex{},
|
2016-05-01 18:36:36 +00:00
|
|
|
ThrottleControlReplicaKeys: mysql.NewInstanceKeyMap(),
|
2016-04-04 10:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 16:19:46 +00:00
|
|
|
// GetMigrationContext
|
2016-04-04 10:27:51 +00:00
|
|
|
func GetMigrationContext() *MigrationContext {
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
2016-04-05 07:14:22 +00:00
|
|
|
// GetGhostTableName generates the name of ghost table, based on original table name
|
2016-04-04 13:29:02 +00:00
|
|
|
func (this *MigrationContext) GetGhostTableName() string {
|
2016-05-16 09:03:15 +00:00
|
|
|
return fmt.Sprintf("_%s_gst", this.OriginalTableName)
|
2016-04-04 13:29:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
// GetOldTableName generates the name of the "old" table, into which the original table is renamed.
|
|
|
|
func (this *MigrationContext) GetOldTableName() string {
|
2016-05-16 09:03:15 +00:00
|
|
|
return fmt.Sprintf("_%s_old", this.OriginalTableName)
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 13:57:12 +00:00
|
|
|
// GetChangelogTableName generates the name of changelog table, based on original table name
|
|
|
|
func (this *MigrationContext) GetChangelogTableName() string {
|
2016-05-16 09:03:15 +00:00
|
|
|
return fmt.Sprintf("_%s_osc", this.OriginalTableName)
|
2016-04-07 13:57:12 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:41:20 +00:00
|
|
|
// GetVoluntaryLockName returns a name of a voluntary lock to be used throughout
|
|
|
|
// the swap-tables process.
|
|
|
|
func (this *MigrationContext) GetVoluntaryLockName() string {
|
|
|
|
return fmt.Sprintf("%s.%s.lock", this.DatabaseName, this.OriginalTableName)
|
|
|
|
}
|
|
|
|
|
2016-04-05 07:14:22 +00:00
|
|
|
// RequiresBinlogFormatChange is `true` when the original binlog format isn't `ROW`
|
2016-04-04 10:27:51 +00:00
|
|
|
func (this *MigrationContext) RequiresBinlogFormatChange() bool {
|
|
|
|
return this.OriginalBinlogFormat != "ROW"
|
2016-04-01 14:05:44 +00:00
|
|
|
}
|
2016-04-04 13:29:02 +00:00
|
|
|
|
2016-04-14 11:37:56 +00:00
|
|
|
// InspectorIsAlsoApplier is `true` when the both inspector and applier are the
|
|
|
|
// same database instance. This would be true when running directly on master or when
|
|
|
|
// testing on replica.
|
|
|
|
func (this *MigrationContext) InspectorIsAlsoApplier() bool {
|
|
|
|
return this.InspectorConnectionConfig.Equals(this.ApplierConnectionConfig)
|
2016-04-04 13:29:02 +00:00
|
|
|
}
|
2016-04-04 16:19:46 +00:00
|
|
|
|
2016-04-05 07:14:22 +00:00
|
|
|
// HasMigrationRange tells us whether there's a range to iterate for copying rows.
|
|
|
|
// It will be `false` if the table is initially empty
|
2016-04-04 16:19:46 +00:00
|
|
|
func (this *MigrationContext) HasMigrationRange() bool {
|
2016-04-05 07:14:22 +00:00
|
|
|
return this.MigrationRangeMinValues != nil && this.MigrationRangeMaxValues != nil
|
2016-04-04 16:19:46 +00:00
|
|
|
}
|
2016-04-07 13:57:12 +00:00
|
|
|
|
|
|
|
func (this *MigrationContext) MaxRetries() int {
|
|
|
|
return maxRetries
|
|
|
|
}
|
2016-04-08 08:34:44 +00:00
|
|
|
|
|
|
|
func (this *MigrationContext) IsTransactionalTable() bool {
|
|
|
|
switch strings.ToLower(this.TableEngine) {
|
|
|
|
case "innodb":
|
|
|
|
{
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case "tokudb":
|
|
|
|
{
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ElapsedTime returns time since very beginning of the process
|
|
|
|
func (this *MigrationContext) ElapsedTime() time.Duration {
|
|
|
|
return time.Now().Sub(this.StartTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ElapsedRowCopyTime returns time since starting to copy chunks of rows
|
|
|
|
func (this *MigrationContext) ElapsedRowCopyTime() time.Duration {
|
|
|
|
return time.Now().Sub(this.RowCopyStartTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTotalRowsCopied returns the accurate number of rows being copied (affected)
|
|
|
|
// This is not exactly the same as the rows being iterated via chunks, but potentially close enough
|
|
|
|
func (this *MigrationContext) GetTotalRowsCopied() int64 {
|
|
|
|
return atomic.LoadInt64(&this.TotalRowsCopied)
|
|
|
|
}
|
2016-04-08 12:35:06 +00:00
|
|
|
|
|
|
|
func (this *MigrationContext) GetIteration() int64 {
|
|
|
|
return atomic.LoadInt64(&this.Iteration)
|
|
|
|
}
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
func (this *MigrationContext) SetThrottled(throttle bool, reason string) {
|
|
|
|
this.throttleMutex.Lock()
|
|
|
|
defer func() { this.throttleMutex.Unlock() }()
|
|
|
|
this.isThrottled = throttle
|
|
|
|
this.throttleReason = reason
|
2016-04-08 12:35:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
func (this *MigrationContext) IsThrottled() (bool, string) {
|
|
|
|
this.throttleMutex.Lock()
|
|
|
|
defer func() { this.throttleMutex.Unlock() }()
|
|
|
|
return this.isThrottled, this.throttleReason
|
2016-04-08 12:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *MigrationContext) ReadMaxLoad(maxLoadList string) error {
|
|
|
|
if maxLoadList == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
maxLoadConditions := strings.Split(maxLoadList, ",")
|
|
|
|
for _, maxLoadCondition := range maxLoadConditions {
|
|
|
|
maxLoadTokens := strings.Split(maxLoadCondition, "=")
|
|
|
|
if len(maxLoadTokens) != 2 {
|
|
|
|
return fmt.Errorf("Error parsing max-load condition: %s", maxLoadCondition)
|
|
|
|
}
|
|
|
|
if maxLoadTokens[0] == "" {
|
|
|
|
return fmt.Errorf("Error parsing status variable in max-load condition: %s", maxLoadCondition)
|
|
|
|
}
|
|
|
|
if n, err := strconv.ParseInt(maxLoadTokens[1], 10, 0); err != nil {
|
|
|
|
return fmt.Errorf("Error parsing numeric value in max-load condition: %s", maxLoadCondition)
|
|
|
|
} else {
|
|
|
|
this.MaxLoad[maxLoadTokens[0]] = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-03 07:28:48 +00:00
|
|
|
|
|
|
|
func (this *MigrationContext) ReadConfigFile() error {
|
|
|
|
if this.ConfigFile == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
conf := struct {
|
|
|
|
Client struct {
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
Osc struct {
|
|
|
|
Chunk_Size int64
|
|
|
|
Max_Lag_Millis int64
|
|
|
|
Replication_Lag_Query string
|
|
|
|
Max_Load string
|
|
|
|
}
|
|
|
|
}{}
|
|
|
|
if err := gcfg.ReadFileInto(&conf, this.ConfigFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if this.InspectorConnectionConfig.User == "" {
|
|
|
|
this.InspectorConnectionConfig.User = conf.Client.User
|
|
|
|
}
|
|
|
|
if this.InspectorConnectionConfig.Password == "" {
|
2016-05-03 08:56:53 +00:00
|
|
|
this.InspectorConnectionConfig.Password = conf.Client.Password
|
2016-05-03 07:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|