vitess.io/vitess@v0.16.2/go/vt/vtorc/logic/disable_recovery.go (about)

     1  /*
     2     Copyright 2015 Shlomi Noach, courtesy Booking.com
     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 logic
    18  
    19  // This file holds wrappers around routines to check if global
    20  // recovery is disabled or not.
    21  //
    22  // This is determined by looking in the table
    23  // vtorc.global_recovery_disable for a value 1.  Note: for
    24  // recoveries to actually happen this must be configured explicitly
    25  // in vtorc.conf.json. This setting is an emergency brake
    26  // to quickly be able to prevent recoveries happening in some large
    27  // outage type situation.  Maybe this value should be cached etc
    28  // but we won't be doing that many recoveries at once so the load
    29  // on this table is expected to be very low. It should be fine to
    30  // go to the database each time.
    31  
    32  import (
    33  	"fmt"
    34  
    35  	"vitess.io/vitess/go/vt/external/golib/sqlutils"
    36  	"vitess.io/vitess/go/vt/log"
    37  	"vitess.io/vitess/go/vt/vtorc/db"
    38  )
    39  
    40  // IsRecoveryDisabled returns true if Recoveries are disabled globally
    41  func IsRecoveryDisabled() (disabled bool, err error) {
    42  	query := `
    43  		SELECT
    44  			COUNT(*) as mycount
    45  		FROM
    46  			global_recovery_disable
    47  		WHERE
    48  			disable_recovery=?
    49  		`
    50  	err = db.QueryVTOrc(query, sqlutils.Args(1), func(m sqlutils.RowMap) error {
    51  		mycount := m.GetInt("mycount")
    52  		disabled = (mycount > 0)
    53  		return nil
    54  	})
    55  	if err != nil {
    56  		errMsg := fmt.Sprintf("recovery.IsRecoveryDisabled(): %v", err)
    57  		log.Errorf(errMsg)
    58  		err = fmt.Errorf(errMsg)
    59  	}
    60  	return disabled, err
    61  }
    62  
    63  // DisableRecovery ensures recoveries are disabled globally
    64  func DisableRecovery() error {
    65  	_, err := db.ExecVTOrc(`
    66  		INSERT IGNORE INTO global_recovery_disable
    67  			(disable_recovery)
    68  		VALUES  (1)
    69  	`,
    70  	)
    71  	return err
    72  }
    73  
    74  // EnableRecovery ensures recoveries are enabled globally
    75  func EnableRecovery() error {
    76  	// The "WHERE" clause is just to avoid full-scan reports by monitoring tools
    77  	_, err := db.ExecVTOrc(`
    78  		DELETE FROM global_recovery_disable WHERE disable_recovery >= 0
    79  	`,
    80  	)
    81  	return err
    82  }