vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/instance_utils.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 inst
    18  
    19  import (
    20  	"regexp"
    21  	"strconv"
    22  	"strings"
    23  )
    24  
    25  var (
    26  	DowntimeLostInRecoveryMessage = "lost-in-recovery"
    27  )
    28  
    29  // MajorVersion returns a MySQL major version number (e.g. given "5.5.36" it returns "5.5")
    30  func MajorVersion(version string) []string {
    31  	tokens := strings.Split(version, ".")
    32  	if len(tokens) < 2 {
    33  		return []string{"0", "0"}
    34  	}
    35  	return tokens[:2]
    36  }
    37  
    38  // IsSmallerMajorVersion tests two versions against another and returns true if
    39  // the former is a smaller "major" varsion than the latter.
    40  // e.g. 5.5.36 is NOT a smaller major version as comapred to 5.5.40, but IS as compared to 5.6.9
    41  func IsSmallerMajorVersion(version string, otherVersion string) bool {
    42  	thisMajorVersion := MajorVersion(version)
    43  	otherMajorVersion := MajorVersion(otherVersion)
    44  	for i := 0; i < len(thisMajorVersion); i++ {
    45  		thisToken, _ := strconv.Atoi(thisMajorVersion[i])
    46  		otherToken, _ := strconv.Atoi(otherMajorVersion[i])
    47  		if thisToken < otherToken {
    48  			return true
    49  		}
    50  		if thisToken > otherToken {
    51  			return false
    52  		}
    53  	}
    54  	return false
    55  }
    56  
    57  // IsSmallerBinlogFormat tests two binlog formats and sees if one is "smaller" than the other.
    58  // "smaller" binlog format means you can replicate from the smaller to the larger.
    59  func IsSmallerBinlogFormat(binlogFormat string, otherBinlogFormat string) bool {
    60  	if binlogFormat == "STATEMENT" {
    61  		return (otherBinlogFormat == "ROW" || otherBinlogFormat == "MIXED")
    62  	}
    63  	if binlogFormat == "MIXED" {
    64  		return otherBinlogFormat == "ROW"
    65  	}
    66  	return false
    67  }
    68  
    69  // RegexpMatchPatterns returns true if s matches any of the provided regexpPatterns
    70  func RegexpMatchPatterns(s string, regexpPatterns []string) bool {
    71  	for _, filter := range regexpPatterns {
    72  		if matched, err := regexp.MatchString(filter, s); err == nil && matched {
    73  			return true
    74  		}
    75  	}
    76  	return false
    77  }