github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/mysql/gtid.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package mysql
    21  
    22  import (
    23  	"fmt"
    24  	"regexp"
    25  	"strconv"
    26  	"strings"
    27  )
    28  
    29  var (
    30  	singleValueInterval = regexp.MustCompile("^([0-9]+)$")
    31  	multiValueInterval  = regexp.MustCompile("^([0-9]+)[-]([0-9]+)$")
    32  )
    33  
    34  // GTIDItem represents an item in a set of GTID ranges,
    35  // for example, the item: "bfaff6e9-3040-11ee-9393-eab5dfc9b22a:1-5:8-10"
    36  type GTIDItem struct {
    37  	ServerUUID string
    38  	Ranges     string
    39  }
    40  
    41  func NewGTIDItem(gtidString string) (*GTIDItem, error) {
    42  	gtidString = strings.TrimSpace(gtidString)
    43  	tokens := strings.SplitN(gtidString, ":", 2)
    44  	if len(tokens) != 2 {
    45  		return nil, fmt.Errorf("GTID wrong format: %s", gtidString)
    46  	}
    47  	if tokens[0] == "" {
    48  		return nil, fmt.Errorf("GTID no server UUID: %s", tokens[0])
    49  	}
    50  	if tokens[1] == "" {
    51  		return nil, fmt.Errorf("GTID no range: %s", tokens[1])
    52  	}
    53  	gtidItem := &GTIDItem{ServerUUID: tokens[0], Ranges: tokens[1]}
    54  	return gtidItem, nil
    55  }
    56  
    57  func (gtid *GTIDItem) String() string {
    58  	return fmt.Sprintf("%s:%s", gtid.ServerUUID, gtid.Ranges)
    59  }
    60  
    61  func (gtid *GTIDItem) Explode() (result [](*GTIDItem)) {
    62  	intervals := strings.Split(gtid.Ranges, ":")
    63  	for _, interval := range intervals {
    64  		if submatch := multiValueInterval.FindStringSubmatch(interval); submatch != nil {
    65  			intervalStart, _ := strconv.Atoi(submatch[1])
    66  			intervalEnd, _ := strconv.Atoi(submatch[2])
    67  			for i := intervalStart; i <= intervalEnd; i++ {
    68  				result = append(result, &GTIDItem{ServerUUID: gtid.ServerUUID, Ranges: fmt.Sprintf("%d", i)})
    69  			}
    70  		} else if submatch := singleValueInterval.FindStringSubmatch(interval); submatch != nil {
    71  			result = append(result, &GTIDItem{ServerUUID: gtid.ServerUUID, Ranges: interval})
    72  		}
    73  	}
    74  	return result
    75  }
    76  
    77  type GTIDSet struct {
    78  	Items []*GTIDItem
    79  }
    80  
    81  func NewOracleGtidSet(gtidSet string) (res *GTIDSet, err error) {
    82  	res = &GTIDSet{}
    83  
    84  	gtidSet = strings.TrimSpace(gtidSet)
    85  	if gtidSet == "" {
    86  		return res, nil
    87  	}
    88  	gtids := strings.Split(gtidSet, ",")
    89  	for _, gtid := range gtids {
    90  		gtid = strings.TrimSpace(gtid)
    91  		if gtid == "" {
    92  			continue
    93  		}
    94  		if gtidRange, err := NewGTIDItem(gtid); err == nil {
    95  			res.Items = append(res.Items, gtidRange)
    96  		} else {
    97  			return res, err
    98  		}
    99  	}
   100  	return res, nil
   101  }
   102  
   103  func (gtidSet *GTIDSet) RemoveUUID(uuid string) (removed bool) {
   104  	filteredEntries := [](*GTIDItem){}
   105  	for _, item := range gtidSet.Items {
   106  		if item.ServerUUID == uuid {
   107  			removed = true
   108  		} else {
   109  			filteredEntries = append(filteredEntries, item)
   110  		}
   111  	}
   112  	if removed {
   113  		gtidSet.Items = filteredEntries
   114  	}
   115  	return removed
   116  }
   117  
   118  func (gtidSet *GTIDSet) RetainUUID(uuid string) (anythingRemoved bool) {
   119  	return gtidSet.RetainUUIDs([]string{uuid})
   120  }
   121  
   122  func (gtidSet *GTIDSet) RetainUUIDs(uuids []string) (anythingRemoved bool) {
   123  	retainUUIDs := map[string]bool{}
   124  	for _, uuid := range uuids {
   125  		retainUUIDs[uuid] = true
   126  	}
   127  	filteredEntries := [](*GTIDItem){}
   128  	for _, item := range gtidSet.Items {
   129  		if retainUUIDs[item.ServerUUID] {
   130  			filteredEntries = append(filteredEntries, item)
   131  		} else {
   132  			anythingRemoved = true
   133  		}
   134  	}
   135  	if anythingRemoved {
   136  		gtidSet.Items = filteredEntries
   137  	}
   138  	return anythingRemoved
   139  }
   140  
   141  func (gtidSet *GTIDSet) SharedUUIDs(other *GTIDSet) (shared []string) {
   142  	gtidSetUUIDs := map[string]bool{}
   143  	for _, item := range gtidSet.Items {
   144  		gtidSetUUIDs[item.ServerUUID] = true
   145  	}
   146  	for _, item := range other.Items {
   147  		if gtidSetUUIDs[item.ServerUUID] {
   148  			shared = append(shared, item.ServerUUID)
   149  		}
   150  	}
   151  	return shared
   152  }
   153  
   154  func (gtidSet *GTIDSet) Explode() (result [](*GTIDItem)) {
   155  	for _, entries := range gtidSet.Items {
   156  		result = append(result, entries.Explode()...)
   157  	}
   158  	return result
   159  }
   160  
   161  func (gtidSet *GTIDSet) String() string {
   162  	tokens := []string{}
   163  	for _, item := range gtidSet.Items {
   164  		tokens = append(tokens, item.String())
   165  	}
   166  	return strings.Join(tokens, ",")
   167  }
   168  
   169  func (gtidSet *GTIDSet) IsEmpty() bool {
   170  	return len(gtidSet.Items) == 0
   171  }