github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/gcutil/gcutil.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package gcutil
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/whtcorpsinc/errors"
    20  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    21  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    22  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    23  	"github.com/whtcorpsinc/milevadb/soliton"
    24  	"github.com/whtcorpsinc/milevadb/soliton/sqlexec"
    25  )
    26  
    27  const (
    28  	selectVariableValueALLEGROSQL = `SELECT HIGH_PRIORITY variable_value FROM allegrosql.milevadb WHERE variable_name='%s'`
    29  	insertVariableValueALLEGROSQL = `INSERT HIGH_PRIORITY INTO allegrosql.milevadb VALUES ('%[1]s', '%[2]s', '%[3]s')
    30                                ON DUPLICATE KEY
    31  			                  UFIDelATE variable_value = '%[2]s', comment = '%[3]s'`
    32  )
    33  
    34  // CheckGCEnable is use to check whether GC is enable.
    35  func CheckGCEnable(ctx stochastikctx.Context) (enable bool, err error) {
    36  	allegrosql := fmt.Sprintf(selectVariableValueALLEGROSQL, "einsteindb_gc_enable")
    37  	rows, _, err := ctx.(sqlexec.RestrictedALLEGROSQLInterlockingDirectorate).InterDircRestrictedALLEGROSQL(allegrosql)
    38  	if err != nil {
    39  		return false, errors.Trace(err)
    40  	}
    41  	if len(rows) != 1 {
    42  		return false, errors.New("can not get 'einsteindb_gc_enable'")
    43  	}
    44  	return rows[0].GetString(0) == "true", nil
    45  }
    46  
    47  // DisableGC will disable GC enable variable.
    48  func DisableGC(ctx stochastikctx.Context) error {
    49  	allegrosql := fmt.Sprintf(insertVariableValueALLEGROSQL, "einsteindb_gc_enable", "false", "Current GC enable status")
    50  	_, _, err := ctx.(sqlexec.RestrictedALLEGROSQLInterlockingDirectorate).InterDircRestrictedALLEGROSQL(allegrosql)
    51  	return errors.Trace(err)
    52  }
    53  
    54  // EnableGC will enable GC enable variable.
    55  func EnableGC(ctx stochastikctx.Context) error {
    56  	allegrosql := fmt.Sprintf(insertVariableValueALLEGROSQL, "einsteindb_gc_enable", "true", "Current GC enable status")
    57  	_, _, err := ctx.(sqlexec.RestrictedALLEGROSQLInterlockingDirectorate).InterDircRestrictedALLEGROSQL(allegrosql)
    58  	return errors.Trace(err)
    59  }
    60  
    61  // ValidateSnapshot checks that the newly set snapshot time is after GC safe point time.
    62  func ValidateSnapshot(ctx stochastikctx.Context, snapshotTS uint64) error {
    63  	safePointTS, err := GetGCSafePoint(ctx)
    64  	if err != nil {
    65  		return errors.Trace(err)
    66  	}
    67  	if safePointTS > snapshotTS {
    68  		return variable.ErrSnapshotTooOld.GenWithStackByArgs(perceptron.TSConvert2Time(safePointTS).String())
    69  	}
    70  	return nil
    71  }
    72  
    73  // ValidateSnapshotWithGCSafePoint checks that the newly set snapshot time is after GC safe point time.
    74  func ValidateSnapshotWithGCSafePoint(snapshotTS, safePointTS uint64) error {
    75  	if safePointTS > snapshotTS {
    76  		return variable.ErrSnapshotTooOld.GenWithStackByArgs(perceptron.TSConvert2Time(safePointTS).String())
    77  	}
    78  	return nil
    79  }
    80  
    81  // GetGCSafePoint loads GC safe point time from allegrosql.milevadb.
    82  func GetGCSafePoint(ctx stochastikctx.Context) (uint64, error) {
    83  	allegrosql := fmt.Sprintf(selectVariableValueALLEGROSQL, "einsteindb_gc_safe_point")
    84  	rows, _, err := ctx.(sqlexec.RestrictedALLEGROSQLInterlockingDirectorate).InterDircRestrictedALLEGROSQL(allegrosql)
    85  	if err != nil {
    86  		return 0, errors.Trace(err)
    87  	}
    88  	if len(rows) != 1 {
    89  		return 0, errors.New("can not get 'einsteindb_gc_safe_point'")
    90  	}
    91  	safePointString := rows[0].GetString(0)
    92  	safePointTime, err := soliton.CompatibleParseGCTime(safePointString)
    93  	if err != nil {
    94  		return 0, errors.Trace(err)
    95  	}
    96  	ts := variable.GoTimeToTS(safePointTime)
    97  	return ts, nil
    98  }