vitess.io/vitess@v0.16.2/go/vt/srvtopo/discover_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 srvtopo
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"sort"
    23  	"testing"
    24  	"time"
    25  
    26  	"vitess.io/vitess/go/vt/topo/memorytopo"
    27  
    28  	querypb "vitess.io/vitess/go/vt/proto/query"
    29  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    30  )
    31  
    32  // To sort []*querypb.Target for comparison.
    33  type TargetArray []*querypb.Target
    34  
    35  func (a TargetArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    36  func (a TargetArray) Len() int      { return len(a) }
    37  func (a TargetArray) Less(i, j int) bool {
    38  	if a[i].Cell != a[j].Cell {
    39  		return a[i].Cell < a[j].Cell
    40  	}
    41  	if a[i].Keyspace != a[j].Keyspace {
    42  		return a[i].Keyspace < a[j].Keyspace
    43  	}
    44  	if a[i].Shard != a[j].Shard {
    45  		return a[i].Shard < a[j].Shard
    46  	}
    47  	return a[i].TabletType < a[j].TabletType
    48  }
    49  
    50  func TestFindAllTargets(t *testing.T) {
    51  	ctx := context.Background()
    52  	ts := memorytopo.NewServer("cell1", "cell2")
    53  
    54  	srvTopoCacheRefresh = 0
    55  	srvTopoCacheTTL = 0
    56  	defer func() {
    57  		srvTopoCacheRefresh = 1 * time.Second
    58  		srvTopoCacheTTL = 1 * time.Second
    59  
    60  	}()
    61  	rs := NewResilientServer(ts, "TestFindAllKeyspaceShards")
    62  
    63  	// No keyspace / shards.
    64  	ks, err := FindAllTargets(ctx, rs, "cell1", []topodatapb.TabletType{topodatapb.TabletType_PRIMARY})
    65  	if err != nil {
    66  		t.Errorf("unexpected error: %v", err)
    67  	}
    68  	if len(ks) > 0 {
    69  		t.Errorf("why did I get anything? %v", ks)
    70  	}
    71  
    72  	// Add one.
    73  	if err := ts.UpdateSrvKeyspace(ctx, "cell1", "test_keyspace", &topodatapb.SrvKeyspace{
    74  		Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{
    75  			{
    76  				ServedType: topodatapb.TabletType_PRIMARY,
    77  				ShardReferences: []*topodatapb.ShardReference{
    78  					{
    79  						Name: "test_shard0",
    80  					},
    81  				},
    82  			},
    83  		},
    84  	}); err != nil {
    85  		t.Fatalf("can't add srvKeyspace: %v", err)
    86  	}
    87  
    88  	// Get it.
    89  	ks, err = FindAllTargets(ctx, rs, "cell1", []topodatapb.TabletType{topodatapb.TabletType_PRIMARY})
    90  	if err != nil {
    91  		t.Errorf("unexpected error: %v", err)
    92  	}
    93  	if !reflect.DeepEqual(ks, []*querypb.Target{
    94  		{
    95  			Cell:       "cell1",
    96  			Keyspace:   "test_keyspace",
    97  			Shard:      "test_shard0",
    98  			TabletType: topodatapb.TabletType_PRIMARY,
    99  		},
   100  	}) {
   101  		t.Errorf("got wrong value: %v", ks)
   102  	}
   103  
   104  	// Add another one.
   105  	if err := ts.UpdateSrvKeyspace(ctx, "cell1", "test_keyspace2", &topodatapb.SrvKeyspace{
   106  		Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{
   107  			{
   108  				ServedType: topodatapb.TabletType_PRIMARY,
   109  				ShardReferences: []*topodatapb.ShardReference{
   110  					{
   111  						Name: "test_shard1",
   112  					},
   113  				},
   114  			},
   115  			{
   116  				ServedType: topodatapb.TabletType_REPLICA,
   117  				ShardReferences: []*topodatapb.ShardReference{
   118  					{
   119  						Name: "test_shard2",
   120  					},
   121  				},
   122  			},
   123  		},
   124  	}); err != nil {
   125  		t.Fatalf("can't add srvKeyspace: %v", err)
   126  	}
   127  
   128  	// Get it for all types.
   129  	ks, err = FindAllTargets(ctx, rs, "cell1", []topodatapb.TabletType{topodatapb.TabletType_PRIMARY, topodatapb.TabletType_REPLICA})
   130  	if err != nil {
   131  		t.Errorf("unexpected error: %v", err)
   132  	}
   133  	sort.Sort(TargetArray(ks))
   134  	if !reflect.DeepEqual(ks, []*querypb.Target{
   135  		{
   136  			Cell:       "cell1",
   137  			Keyspace:   "test_keyspace",
   138  			Shard:      "test_shard0",
   139  			TabletType: topodatapb.TabletType_PRIMARY,
   140  		},
   141  		{
   142  			Cell:       "cell1",
   143  			Keyspace:   "test_keyspace2",
   144  			Shard:      "test_shard1",
   145  			TabletType: topodatapb.TabletType_PRIMARY,
   146  		},
   147  		{
   148  			Cell:       "cell1",
   149  			Keyspace:   "test_keyspace2",
   150  			Shard:      "test_shard2",
   151  			TabletType: topodatapb.TabletType_REPLICA,
   152  		},
   153  	}) {
   154  		t.Errorf("got wrong value: %v", ks)
   155  	}
   156  
   157  	// Only get the REPLICA targets.
   158  	ks, err = FindAllTargets(ctx, rs, "cell1", []topodatapb.TabletType{topodatapb.TabletType_REPLICA})
   159  	if err != nil {
   160  		t.Errorf("unexpected error: %v", err)
   161  	}
   162  	if !reflect.DeepEqual(ks, []*querypb.Target{
   163  		{
   164  			Cell:       "cell1",
   165  			Keyspace:   "test_keyspace2",
   166  			Shard:      "test_shard2",
   167  			TabletType: topodatapb.TabletType_REPLICA,
   168  		},
   169  	}) {
   170  		t.Errorf("got wrong value: %v", ks)
   171  	}
   172  }