vitess.io/vitess@v0.16.2/go/vt/mysqlctl/reparent.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mysqlctl
    18  
    19  /*
    20  This file contains the reparenting methods for mysqlctl.
    21  */
    22  
    23  import (
    24  	"fmt"
    25  	"time"
    26  
    27  	"vitess.io/vitess/go/vt/sidecardb"
    28  
    29  	"vitess.io/vitess/go/mysql"
    30  	"vitess.io/vitess/go/vt/log"
    31  
    32  	"context"
    33  )
    34  
    35  // GenerateInitialBinlogEntry is used to create a binlog entry when a primary comes up and we need to get a
    36  // MySQL position so that we can set it as the starting position for replicas to do MySQL Replication from.
    37  func GenerateInitialBinlogEntry() string {
    38  	return sidecardb.CreateSidecarDatabaseQuery
    39  }
    40  
    41  // PopulateReparentJournal returns the SQL command to use to populate
    42  // the _vt.reparent_journal table, as well as the time_created_ns
    43  // value used.
    44  func PopulateReparentJournal(timeCreatedNS int64, actionName, primaryAlias string, pos mysql.Position) string {
    45  	posStr := mysql.EncodePosition(pos)
    46  	if len(posStr) > mysql.MaximumPositionSize {
    47  		posStr = posStr[:mysql.MaximumPositionSize]
    48  	}
    49  	return fmt.Sprintf("INSERT INTO _vt.reparent_journal "+
    50  		"(time_created_ns, action_name, primary_alias, replication_position) "+
    51  		"VALUES (%v, '%v', '%v', '%v')",
    52  		timeCreatedNS, actionName, primaryAlias, posStr)
    53  }
    54  
    55  // queryReparentJournal returns the SQL query to use to query the database
    56  // for a reparent_journal row.
    57  func queryReparentJournal(timeCreatedNS int64) string {
    58  	return fmt.Sprintf("SELECT action_name, primary_alias, replication_position FROM _vt.reparent_journal WHERE time_created_ns=%v", timeCreatedNS)
    59  }
    60  
    61  // WaitForReparentJournal will wait until the context is done for
    62  // the row in the reparent_journal table.
    63  func (mysqld *Mysqld) WaitForReparentJournal(ctx context.Context, timeCreatedNS int64) error {
    64  	for {
    65  		qr, err := mysqld.FetchSuperQuery(ctx, queryReparentJournal(timeCreatedNS))
    66  		if err != nil {
    67  			log.Infof("Error querying reparent journal: %v", err)
    68  		}
    69  		if err == nil && len(qr.Rows) == 1 {
    70  			// we have the row, we're done
    71  			return nil
    72  		}
    73  
    74  		// wait a little bit, interrupt if context is done
    75  		t := time.After(100 * time.Millisecond)
    76  		select {
    77  		case <-ctx.Done():
    78  			log.Warning("WaitForReparentJournal failed to see row before timeout.")
    79  			return ctx.Err()
    80  		case <-t:
    81  		}
    82  	}
    83  }
    84  
    85  // Promote will promote this server to be the new primary.
    86  func (mysqld *Mysqld) Promote(hookExtraEnv map[string]string) (mysql.Position, error) {
    87  	ctx := context.TODO()
    88  	conn, err := getPoolReconnect(ctx, mysqld.dbaPool)
    89  	if err != nil {
    90  		return mysql.Position{}, err
    91  	}
    92  	defer conn.Recycle()
    93  
    94  	// Since we handle replication, just stop it.
    95  	cmds := []string{
    96  		conn.StopReplicationCommand(),
    97  		"RESET SLAVE ALL", // "ALL" makes it forget primary host:port.
    98  		// When using semi-sync and GTID, a replica first connects to the new primary with a given GTID set,
    99  		// it can take a long time to scan the current binlog file to find the corresponding position.
   100  		// This can cause commits that occur soon after the primary is promoted to take a long time waiting
   101  		// for a semi-sync ACK, since replication is not fully set up.
   102  		// More details in: https://github.com/vitessio/vitess/issues/4161
   103  		"FLUSH BINARY LOGS",
   104  	}
   105  
   106  	if err := mysqld.executeSuperQueryListConn(ctx, conn, cmds); err != nil {
   107  		return mysql.Position{}, err
   108  	}
   109  	return conn.PrimaryPosition()
   110  }