vitess.io/vitess@v0.16.2/go/vt/topo/keyspace_test.go (about)

     1  /*
     2  Copyright 2019 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 topo
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    24  )
    25  
    26  // This file tests the keyspace related object functionalities.
    27  
    28  func TestUpdateServedFromMap(t *testing.T) {
    29  	// TODO(deepthi): delete this test once legacy resharding code is deleted
    30  	ki := &KeyspaceInfo{
    31  		keyspace: "ks",
    32  		version:  nil,
    33  		Keyspace: &topodatapb.Keyspace{
    34  			ServedFroms: []*topodatapb.Keyspace_ServedFrom{
    35  				{
    36  					TabletType: topodatapb.TabletType_RDONLY,
    37  					Cells:      nil,
    38  					Keyspace:   "source",
    39  				},
    40  				{
    41  					TabletType: topodatapb.TabletType_PRIMARY,
    42  					Cells:      nil,
    43  					Keyspace:   "source",
    44  				},
    45  			},
    46  		},
    47  	}
    48  	allCells := []string{"first", "second", "third"}
    49  
    50  	// migrate one cell
    51  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_RDONLY, []string{"first"}, "source", true, allCells); err != nil || !reflect.DeepEqual(ki.ServedFroms, []*topodatapb.Keyspace_ServedFrom{
    52  		{
    53  			TabletType: topodatapb.TabletType_RDONLY,
    54  			Cells:      []string{"second", "third"},
    55  			Keyspace:   "source",
    56  		},
    57  		{
    58  			TabletType: topodatapb.TabletType_PRIMARY,
    59  			Cells:      nil,
    60  			Keyspace:   "source",
    61  		},
    62  	}) {
    63  		t.Fatalf("one cell add failed: %v", ki)
    64  	}
    65  
    66  	// re-add that cell, going back
    67  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_RDONLY, []string{"first"}, "source", false, nil); err != nil || !reflect.DeepEqual(ki.ServedFroms, []*topodatapb.Keyspace_ServedFrom{
    68  		{
    69  			TabletType: topodatapb.TabletType_RDONLY,
    70  			Cells:      []string{"second", "third", "first"},
    71  			Keyspace:   "source",
    72  		},
    73  		{
    74  			TabletType: topodatapb.TabletType_PRIMARY,
    75  			Cells:      nil,
    76  			Keyspace:   "source",
    77  		},
    78  	}) {
    79  		t.Fatalf("going back should have remove the record: %#v", ki.Keyspace.ServedFroms)
    80  	}
    81  
    82  	// now remove the cell again
    83  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_RDONLY, []string{"first"}, "source", true, allCells); err != nil || !reflect.DeepEqual(ki.ServedFroms, []*topodatapb.Keyspace_ServedFrom{
    84  		{
    85  			TabletType: topodatapb.TabletType_RDONLY,
    86  			Cells:      []string{"second", "third"},
    87  			Keyspace:   "source",
    88  		},
    89  		{
    90  			TabletType: topodatapb.TabletType_PRIMARY,
    91  			Cells:      nil,
    92  			Keyspace:   "source",
    93  		},
    94  	}) {
    95  		t.Fatalf("one cell add failed: %v", ki)
    96  	}
    97  
    98  	// couple error cases
    99  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_RDONLY, []string{"second"}, "othersource", true, allCells); err == nil || (err.Error() != "inconsistent keyspace specified in migration: othersource != source for type MASTER" && err.Error() != "inconsistent keyspace specified in migration: othersource != source for type RDONLY") {
   100  		t.Fatalf("different keyspace should fail: %v", err)
   101  	}
   102  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_PRIMARY, nil, "source", true, allCells); err == nil || err.Error() != "cannot migrate master into ks until everything else is migrated" {
   103  		t.Fatalf("migrate the master early should have failed: %v", err)
   104  	}
   105  
   106  	// now remove all cells
   107  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_RDONLY, []string{"second", "third"}, "source", true, allCells); err != nil || !reflect.DeepEqual(ki.ServedFroms, []*topodatapb.Keyspace_ServedFrom{
   108  		{
   109  			TabletType: topodatapb.TabletType_PRIMARY,
   110  			Cells:      nil,
   111  			Keyspace:   "source",
   112  		},
   113  	}) {
   114  		t.Fatalf("remove all cells failed: %v", ki)
   115  	}
   116  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_RDONLY, nil, "source", true, allCells); err == nil || err.Error() != "supplied type cannot be migrated" {
   117  		t.Fatalf("migrate rdonly again should have failed: %v", err)
   118  	}
   119  
   120  	// finally migrate the primary
   121  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_PRIMARY, []string{"second"}, "source", true, allCells); err == nil || err.Error() != "cannot migrate only some cells for master removal in keyspace ks" {
   122  		t.Fatalf("migrate master with cells should have failed: %v", err)
   123  	}
   124  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_PRIMARY, nil, "source", true, allCells); err != nil || ki.ServedFroms != nil {
   125  		t.Fatalf("migrate the master failed: %v", ki)
   126  	}
   127  
   128  	// error case again
   129  	if err := ki.UpdateServedFromMap(topodatapb.TabletType_PRIMARY, nil, "source", true, allCells); err == nil || err.Error() != "supplied type cannot be migrated" {
   130  		t.Fatalf("migrate the master again should have failed: %v", err)
   131  	}
   132  }
   133  
   134  func TestComputeCellServedFrom(t *testing.T) {
   135  	ki := &KeyspaceInfo{
   136  		keyspace: "ks",
   137  		version:  nil,
   138  		Keyspace: &topodatapb.Keyspace{
   139  			ServedFroms: []*topodatapb.Keyspace_ServedFrom{
   140  				{
   141  					TabletType: topodatapb.TabletType_PRIMARY,
   142  					Cells:      nil,
   143  					Keyspace:   "source",
   144  				},
   145  				{
   146  					TabletType: topodatapb.TabletType_REPLICA,
   147  					Cells:      []string{"c1", "c2"},
   148  					Keyspace:   "source",
   149  				},
   150  			},
   151  		},
   152  	}
   153  
   154  	m := ki.ComputeCellServedFrom("c3")
   155  	if !reflect.DeepEqual(m, []*topodatapb.SrvKeyspace_ServedFrom{
   156  		{
   157  			TabletType: topodatapb.TabletType_PRIMARY,
   158  			Keyspace:   "source",
   159  		},
   160  	}) {
   161  		t.Fatalf("c3 failed: %v", m)
   162  	}
   163  
   164  	m = ki.ComputeCellServedFrom("c2")
   165  	if !reflect.DeepEqual(m, []*topodatapb.SrvKeyspace_ServedFrom{
   166  		{
   167  			TabletType: topodatapb.TabletType_PRIMARY,
   168  			Keyspace:   "source",
   169  		},
   170  		{
   171  			TabletType: topodatapb.TabletType_REPLICA,
   172  			Keyspace:   "source",
   173  		},
   174  	}) {
   175  		t.Fatalf("c2 failed: %v", m)
   176  	}
   177  }