vitess.io/vitess@v0.16.2/go/vt/vtctl/reparentutil/reparent_sorter_test.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     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 reparentutil
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"vitess.io/vitess/go/mysql"
    25  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    26  )
    27  
    28  // TestReparentSorter tests that the sorting for tablets works correctly
    29  func TestReparentSorter(t *testing.T) {
    30  	sid1 := mysql.SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    31  	sid2 := mysql.SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16}
    32  	cell1 := "cell1"
    33  	cell2 := "cell2"
    34  	tabletReplica1_100 := &topodatapb.Tablet{
    35  		Alias: &topodatapb.TabletAlias{
    36  			Cell: cell1,
    37  			Uid:  100,
    38  		},
    39  		Type: topodatapb.TabletType_REPLICA,
    40  	}
    41  	tabletReplica2_100 := &topodatapb.Tablet{
    42  		Alias: &topodatapb.TabletAlias{
    43  			Cell: cell2,
    44  			Uid:  100,
    45  		},
    46  		Type: topodatapb.TabletType_REPLICA,
    47  	}
    48  	tabletReplica1_101 := &topodatapb.Tablet{
    49  		Alias: &topodatapb.TabletAlias{
    50  			Cell: cell1,
    51  			Uid:  101,
    52  		},
    53  		Type: topodatapb.TabletType_REPLICA,
    54  	}
    55  	tabletRdonly1_102 := &topodatapb.Tablet{
    56  		Alias: &topodatapb.TabletAlias{
    57  			Cell: cell1,
    58  			Uid:  102,
    59  		},
    60  		Type: topodatapb.TabletType_RDONLY,
    61  	}
    62  
    63  	mysqlGTID1 := mysql.Mysql56GTID{
    64  		Server:   sid1,
    65  		Sequence: 9,
    66  	}
    67  	mysqlGTID2 := mysql.Mysql56GTID{
    68  		Server:   sid2,
    69  		Sequence: 10,
    70  	}
    71  	mysqlGTID3 := mysql.Mysql56GTID{
    72  		Server:   sid1,
    73  		Sequence: 11,
    74  	}
    75  
    76  	positionMostAdvanced := mysql.Position{GTIDSet: mysql.Mysql56GTIDSet{}}
    77  	positionMostAdvanced.GTIDSet = positionMostAdvanced.GTIDSet.AddGTID(mysqlGTID1)
    78  	positionMostAdvanced.GTIDSet = positionMostAdvanced.GTIDSet.AddGTID(mysqlGTID2)
    79  	positionMostAdvanced.GTIDSet = positionMostAdvanced.GTIDSet.AddGTID(mysqlGTID3)
    80  
    81  	positionEmpty := mysql.Position{GTIDSet: mysql.Mysql56GTIDSet{}}
    82  
    83  	positionIntermediate1 := mysql.Position{GTIDSet: mysql.Mysql56GTIDSet{}}
    84  	positionIntermediate1.GTIDSet = positionIntermediate1.GTIDSet.AddGTID(mysqlGTID1)
    85  
    86  	positionIntermediate2 := mysql.Position{GTIDSet: mysql.Mysql56GTIDSet{}}
    87  	positionIntermediate2.GTIDSet = positionIntermediate2.GTIDSet.AddGTID(mysqlGTID1)
    88  	positionIntermediate2.GTIDSet = positionIntermediate2.GTIDSet.AddGTID(mysqlGTID2)
    89  
    90  	testcases := []struct {
    91  		name          string
    92  		tablets       []*topodatapb.Tablet
    93  		positions     []mysql.Position
    94  		containsErr   string
    95  		sortedTablets []*topodatapb.Tablet
    96  	}{
    97  		{
    98  			name:          "all advanced, sort via promotion rules",
    99  			tablets:       []*topodatapb.Tablet{nil, tabletReplica1_100, tabletRdonly1_102},
   100  			positions:     []mysql.Position{positionMostAdvanced, positionMostAdvanced, positionMostAdvanced},
   101  			sortedTablets: []*topodatapb.Tablet{tabletReplica1_100, tabletRdonly1_102, nil},
   102  		}, {
   103  			name:          "ordering by position",
   104  			tablets:       []*topodatapb.Tablet{tabletReplica1_101, tabletReplica2_100, tabletReplica1_100, tabletRdonly1_102},
   105  			positions:     []mysql.Position{positionEmpty, positionIntermediate1, positionIntermediate2, positionMostAdvanced},
   106  			sortedTablets: []*topodatapb.Tablet{tabletRdonly1_102, tabletReplica1_100, tabletReplica2_100, tabletReplica1_101},
   107  		}, {
   108  			name:        "tablets and positions count error",
   109  			tablets:     []*topodatapb.Tablet{tabletReplica1_101, tabletReplica2_100},
   110  			positions:   []mysql.Position{positionEmpty, positionIntermediate1, positionMostAdvanced},
   111  			containsErr: "unequal number of tablets and positions",
   112  		}, {
   113  			name:          "promotion rule check",
   114  			tablets:       []*topodatapb.Tablet{tabletReplica1_101, tabletRdonly1_102},
   115  			positions:     []mysql.Position{positionMostAdvanced, positionMostAdvanced},
   116  			sortedTablets: []*topodatapb.Tablet{tabletReplica1_101, tabletRdonly1_102},
   117  		}, {
   118  			name:          "mixed",
   119  			tablets:       []*topodatapb.Tablet{tabletReplica1_101, tabletReplica2_100, tabletReplica1_100, tabletRdonly1_102},
   120  			positions:     []mysql.Position{positionEmpty, positionIntermediate1, positionMostAdvanced, positionIntermediate1},
   121  			sortedTablets: []*topodatapb.Tablet{tabletReplica1_100, tabletReplica2_100, tabletRdonly1_102, tabletReplica1_101},
   122  		},
   123  	}
   124  
   125  	durability, err := GetDurabilityPolicy("none")
   126  	require.NoError(t, err)
   127  	for _, testcase := range testcases {
   128  		t.Run(testcase.name, func(t *testing.T) {
   129  			err := sortTabletsForReparent(testcase.tablets, testcase.positions, durability)
   130  			if testcase.containsErr != "" {
   131  				require.EqualError(t, err, testcase.containsErr)
   132  			} else {
   133  				require.NoError(t, err)
   134  				require.Equal(t, testcase.sortedTablets, testcase.tablets)
   135  			}
   136  		})
   137  	}
   138  }