vitess.io/vitess@v0.16.2/go/vt/vtgr/inst/instance_key.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  /*
    18  	This file has been copied over from VTOrc package
    19  */
    20  
    21  package inst
    22  
    23  import (
    24  	"fmt"
    25  	"regexp"
    26  	"strings"
    27  )
    28  
    29  // InstanceKey is an instance indicator, identifued by hostname and port
    30  type InstanceKey struct {
    31  	Hostname string
    32  	Port     int
    33  }
    34  
    35  var (
    36  	ipv4Regexp = regexp.MustCompile(`^([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)$`)
    37  )
    38  
    39  const detachHint = "//"
    40  
    41  // Constant strings for Group Replication information
    42  // See https://dev.mysql.com/doc/refman/8.0/en/replication-group-members-table.html for additional information.
    43  const (
    44  	// Group member roles
    45  	GroupReplicationMemberRolePrimary   = "PRIMARY"
    46  	GroupReplicationMemberRoleSecondary = "SECONDARY"
    47  	// Group member states
    48  	GroupReplicationMemberStateOnline      = "ONLINE"
    49  	GroupReplicationMemberStateRecovering  = "RECOVERING"
    50  	GroupReplicationMemberStateUnreachable = "UNREACHABLE"
    51  	GroupReplicationMemberStateOffline     = "OFFLINE"
    52  	GroupReplicationMemberStateError       = "ERROR"
    53  )
    54  
    55  // Equals tests equality between this key and another key
    56  func (instanceKey *InstanceKey) Equals(other *InstanceKey) bool {
    57  	if other == nil {
    58  		return false
    59  	}
    60  	return instanceKey.Hostname == other.Hostname && instanceKey.Port == other.Port
    61  }
    62  
    63  // SmallerThan returns true if this key is dictionary-smaller than another.
    64  // This is used for consistent sorting/ordering; there's nothing magical about it.
    65  func (instanceKey *InstanceKey) SmallerThan(other *InstanceKey) bool {
    66  	if instanceKey.Hostname < other.Hostname {
    67  		return true
    68  	}
    69  	if instanceKey.Hostname == other.Hostname && instanceKey.Port < other.Port {
    70  		return true
    71  	}
    72  	return false
    73  }
    74  
    75  // IsDetached returns 'true' when this hostname is logically "detached"
    76  func (instanceKey *InstanceKey) IsDetached() bool {
    77  	return strings.HasPrefix(instanceKey.Hostname, detachHint)
    78  }
    79  
    80  // IsValid uses simple heuristics to see whether this key represents an actual instance
    81  func (instanceKey *InstanceKey) IsValid() bool {
    82  	if instanceKey.Hostname == "_" {
    83  		return false
    84  	}
    85  	if instanceKey.IsDetached() {
    86  		return false
    87  	}
    88  	return len(instanceKey.Hostname) > 0 && instanceKey.Port > 0
    89  }
    90  
    91  // DetachedKey returns an instance key whose hostname is detahced: invalid, but recoverable
    92  func (instanceKey *InstanceKey) DetachedKey() *InstanceKey {
    93  	if instanceKey.IsDetached() {
    94  		return instanceKey
    95  	}
    96  	return &InstanceKey{Hostname: fmt.Sprintf("%s%s", detachHint, instanceKey.Hostname), Port: instanceKey.Port}
    97  }
    98  
    99  // ReattachedKey returns an instance key whose hostname is detahced: invalid, but recoverable
   100  func (instanceKey *InstanceKey) ReattachedKey() *InstanceKey {
   101  	if !instanceKey.IsDetached() {
   102  		return instanceKey
   103  	}
   104  	return &InstanceKey{Hostname: instanceKey.Hostname[len(detachHint):], Port: instanceKey.Port}
   105  }
   106  
   107  // StringCode returns an official string representation of this key
   108  func (instanceKey *InstanceKey) StringCode() string {
   109  	return fmt.Sprintf("%s:%d", instanceKey.Hostname, instanceKey.Port)
   110  }
   111  
   112  // DisplayString returns a user-friendly string representation of this key
   113  func (instanceKey *InstanceKey) DisplayString() string {
   114  	return instanceKey.StringCode()
   115  }
   116  
   117  // String returns a user-friendly string representation of this key
   118  func (instanceKey InstanceKey) String() string {
   119  	return instanceKey.StringCode()
   120  }
   121  
   122  // IsValid uses simple heuristics to see whether this key represents an actual instance
   123  func (instanceKey *InstanceKey) IsIPv4() bool {
   124  	return ipv4Regexp.MatchString(instanceKey.Hostname)
   125  }