github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/operator/steps_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Portions of this file are additionally subject to the following
    15  // copyright.
    16  //
    17  // Copyright (C) 2021 Matrix Origin.
    18  //
    19  // Modified some tests.
    20  
    21  package operator
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  
    27  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestIsFinish(t *testing.T) {
    32  	logState := pb.LogState{
    33  		Shards: map[uint64]pb.LogShardInfo{1: {
    34  			ShardID:  1,
    35  			Replicas: map[uint64]string{1: "a", 2: "b", 3: "c"},
    36  			Epoch:    1,
    37  		}},
    38  		Stores: nil,
    39  	}
    40  
    41  	tnState := pb.TNState{}
    42  	cnState := pb.CNState{}
    43  	proxyState := pb.ProxyState{}
    44  
    45  	assert.False(t, AddLogService{Replica: Replica{UUID: "d", ShardID: 1, ReplicaID: 4}}.
    46  		IsFinish(ClusterState{
    47  			LogState:   logState,
    48  			TNState:    tnState,
    49  			CNState:    cnState,
    50  			ProxyState: proxyState,
    51  		}),
    52  	)
    53  	assert.True(t, AddLogService{Replica: Replica{UUID: "c", ShardID: 1, ReplicaID: 3}}.
    54  		IsFinish(ClusterState{
    55  			LogState:   logState,
    56  			TNState:    tnState,
    57  			CNState:    cnState,
    58  			ProxyState: proxyState,
    59  		}),
    60  	)
    61  	assert.False(t, RemoveLogService{Replica: Replica{UUID: "c", ShardID: 1, ReplicaID: 3}}.
    62  		IsFinish(ClusterState{
    63  			LogState:   logState,
    64  			TNState:    tnState,
    65  			CNState:    cnState,
    66  			ProxyState: proxyState,
    67  		}),
    68  	)
    69  	assert.True(t, RemoveLogService{Replica: Replica{UUID: "d", ShardID: 1, ReplicaID: 4}}.
    70  		IsFinish(ClusterState{
    71  			LogState:   logState,
    72  			TNState:    tnState,
    73  			CNState:    cnState,
    74  			ProxyState: proxyState,
    75  		}),
    76  	)
    77  }
    78  
    79  func TestAddLogService(t *testing.T) {
    80  	cases := []struct {
    81  		desc     string
    82  		command  AddLogService
    83  		state    pb.LogState
    84  		expected bool
    85  	}{
    86  		{
    87  			desc: "add log service completed",
    88  			command: AddLogService{
    89  				Replica: Replica{
    90  					UUID:      "a",
    91  					ShardID:   1,
    92  					ReplicaID: 1},
    93  			},
    94  			state: pb.LogState{
    95  				Shards: map[uint64]pb.LogShardInfo{
    96  					1: {ShardID: 1, Replicas: map[uint64]string{1: "a"}},
    97  				},
    98  			},
    99  			expected: true,
   100  		},
   101  		{
   102  			desc: "add log service not completed",
   103  			command: AddLogService{
   104  				Replica: Replica{
   105  					UUID:      "a",
   106  					ShardID:   1,
   107  					ReplicaID: 1},
   108  			},
   109  			state: pb.LogState{
   110  				Shards: map[uint64]pb.LogShardInfo{
   111  					1: {ShardID: 1, Replicas: map[uint64]string{}},
   112  				},
   113  			},
   114  			expected: false,
   115  		},
   116  	}
   117  
   118  	for i, c := range cases {
   119  		fmt.Printf("case %v: %s\n", i, c.desc)
   120  		assert.Equal(t, c.expected, c.command.IsFinish(ClusterState{
   121  			LogState:   c.state,
   122  			TNState:    pb.TNState{},
   123  			CNState:    pb.CNState{},
   124  			ProxyState: pb.ProxyState{},
   125  		}))
   126  	}
   127  }
   128  
   129  func TestRemoveLogService(t *testing.T) {
   130  	cases := []struct {
   131  		desc     string
   132  		command  RemoveLogService
   133  		state    pb.LogState
   134  		expected bool
   135  	}{
   136  		{
   137  			desc: "remove log service not completed",
   138  			command: RemoveLogService{
   139  				Replica: Replica{
   140  					UUID:      "a",
   141  					ShardID:   1,
   142  					ReplicaID: 1},
   143  			},
   144  			state: pb.LogState{
   145  				Shards: map[uint64]pb.LogShardInfo{
   146  					1: {ShardID: 1, Replicas: map[uint64]string{1: "a"}},
   147  				},
   148  			},
   149  			expected: false,
   150  		},
   151  		{
   152  			desc: "remove log service completed",
   153  			command: RemoveLogService{
   154  				Replica: Replica{
   155  					UUID:      "a",
   156  					ShardID:   1,
   157  					ReplicaID: 1,
   158  				},
   159  			},
   160  			state: pb.LogState{
   161  				Shards: map[uint64]pb.LogShardInfo{
   162  					1: {ShardID: 1, Replicas: map[uint64]string{}},
   163  				},
   164  			},
   165  			expected: true,
   166  		},
   167  	}
   168  
   169  	for i, c := range cases {
   170  		fmt.Printf("case %v: %s\n", i, c.desc)
   171  		assert.Equal(t, c.expected, c.command.IsFinish(ClusterState{
   172  			LogState:   c.state,
   173  			TNState:    pb.TNState{},
   174  			CNState:    pb.CNState{},
   175  			ProxyState: pb.ProxyState{},
   176  		}))
   177  	}
   178  }
   179  
   180  func TestStartLogService(t *testing.T) {
   181  	cases := []struct {
   182  		desc     string
   183  		command  StartLogService
   184  		state    pb.LogState
   185  		expected bool
   186  	}{
   187  		{
   188  			desc: "start log service not completed",
   189  			command: StartLogService{
   190  				Replica: Replica{
   191  					UUID:      "a",
   192  					ShardID:   1,
   193  					ReplicaID: 1,
   194  				},
   195  			},
   196  			state: pb.LogState{
   197  				Stores: map[string]pb.LogStoreInfo{"a": {
   198  					Replicas: []pb.LogReplicaInfo{{}},
   199  				}},
   200  			},
   201  			expected: false,
   202  		},
   203  		{
   204  			desc: "start log service completed",
   205  			command: StartLogService{
   206  				Replica: Replica{
   207  					UUID:      "a",
   208  					ShardID:   1,
   209  					ReplicaID: 1,
   210  				},
   211  			},
   212  			state: pb.LogState{
   213  				Stores: map[string]pb.LogStoreInfo{"a": {
   214  					Replicas: []pb.LogReplicaInfo{{
   215  						LogShardInfo: pb.LogShardInfo{
   216  							ShardID:  1,
   217  							Replicas: map[uint64]string{1: "a"},
   218  						},
   219  						ReplicaID: 1,
   220  					}},
   221  				}},
   222  			},
   223  			expected: true,
   224  		},
   225  	}
   226  
   227  	for i, c := range cases {
   228  		fmt.Printf("case %v: %s\n", i, c.desc)
   229  		assert.Equal(t, c.expected, c.command.IsFinish(ClusterState{
   230  			LogState:   c.state,
   231  			TNState:    pb.TNState{},
   232  			CNState:    pb.CNState{},
   233  			ProxyState: pb.ProxyState{},
   234  		}))
   235  	}
   236  }
   237  
   238  func TestStopLogService(t *testing.T) {
   239  	cases := []struct {
   240  		desc     string
   241  		command  StopLogService
   242  		state    pb.LogState
   243  		expected bool
   244  	}{
   245  		{
   246  			desc: "stop log service completed",
   247  			command: StopLogService{
   248  				Replica: Replica{
   249  					UUID:    "a",
   250  					ShardID: 1,
   251  				},
   252  			},
   253  			state: pb.LogState{
   254  				Stores: map[string]pb.LogStoreInfo{"a": {
   255  					Replicas: []pb.LogReplicaInfo{{}},
   256  				}},
   257  			},
   258  			expected: true,
   259  		},
   260  		{
   261  			desc: "stop log service not completed",
   262  			command: StopLogService{
   263  				Replica: Replica{
   264  					UUID:    "a",
   265  					ShardID: 1,
   266  				},
   267  			},
   268  			state: pb.LogState{
   269  				Stores: map[string]pb.LogStoreInfo{"a": {
   270  					Replicas: []pb.LogReplicaInfo{{
   271  						LogShardInfo: pb.LogShardInfo{
   272  							ShardID:  1,
   273  							Replicas: map[uint64]string{1: "a"},
   274  						},
   275  						ReplicaID: 1,
   276  					}},
   277  				}},
   278  			},
   279  			expected: false,
   280  		},
   281  	}
   282  
   283  	for i, c := range cases {
   284  		fmt.Printf("case %v: %s\n", i, c.desc)
   285  		assert.Equal(t, c.expected, c.command.IsFinish(ClusterState{
   286  			LogState:   c.state,
   287  			TNState:    pb.TNState{},
   288  			CNState:    pb.CNState{},
   289  			ProxyState: pb.ProxyState{},
   290  		}))
   291  	}
   292  }
   293  
   294  func TestAddTnReplica(t *testing.T) {
   295  	cases := []struct {
   296  		desc     string
   297  		command  AddTnReplica
   298  		state    pb.TNState
   299  		expected bool
   300  	}{
   301  		{
   302  			desc: "add tn replica completed",
   303  			command: AddTnReplica{
   304  				StoreID:   "a",
   305  				ShardID:   1,
   306  				ReplicaID: 1,
   307  			},
   308  			state: pb.TNState{
   309  				Stores: map[string]pb.TNStoreInfo{"a": {
   310  					Tick: 0,
   311  					Shards: []pb.TNShardInfo{{
   312  						ShardID:   1,
   313  						ReplicaID: 1,
   314  					}},
   315  				}},
   316  			},
   317  			expected: true,
   318  		},
   319  		{
   320  			desc: "add tn replica not completed",
   321  			command: AddTnReplica{
   322  				StoreID:   "a",
   323  				ShardID:   1,
   324  				ReplicaID: 1,
   325  			},
   326  			state: pb.TNState{
   327  				Stores: map[string]pb.TNStoreInfo{"a": {
   328  					Tick:   0,
   329  					Shards: []pb.TNShardInfo{},
   330  				}},
   331  			},
   332  			expected: false,
   333  		},
   334  	}
   335  
   336  	for i, c := range cases {
   337  		fmt.Printf("case %v: %s\n", i, c.desc)
   338  		assert.Equal(t, c.expected, c.command.IsFinish(ClusterState{
   339  			LogState:   pb.LogState{},
   340  			TNState:    c.state,
   341  			CNState:    pb.CNState{},
   342  			ProxyState: pb.ProxyState{},
   343  		}))
   344  	}
   345  }
   346  
   347  func TestRemoveTnReplica(t *testing.T) {
   348  	cases := []struct {
   349  		desc     string
   350  		command  RemoveTnReplica
   351  		state    pb.TNState
   352  		expected bool
   353  	}{
   354  		{
   355  			desc: "remove tn replica not completed",
   356  			command: RemoveTnReplica{
   357  				StoreID:   "a",
   358  				ShardID:   1,
   359  				ReplicaID: 1,
   360  			},
   361  			state: pb.TNState{
   362  				Stores: map[string]pb.TNStoreInfo{"a": {
   363  					Tick: 0,
   364  					Shards: []pb.TNShardInfo{{
   365  						ShardID:   1,
   366  						ReplicaID: 1,
   367  					}},
   368  				}},
   369  			},
   370  			expected: false,
   371  		},
   372  		{
   373  			desc: "remove tn replica completed",
   374  			command: RemoveTnReplica{
   375  				StoreID:   "a",
   376  				ShardID:   1,
   377  				ReplicaID: 1,
   378  			},
   379  			state: pb.TNState{
   380  				Stores: map[string]pb.TNStoreInfo{"a": {
   381  					Tick:   0,
   382  					Shards: []pb.TNShardInfo{},
   383  				}},
   384  			},
   385  			expected: true,
   386  		},
   387  	}
   388  
   389  	for i, c := range cases {
   390  		fmt.Printf("case %v: %s\n", i, c.desc)
   391  		assert.Equal(t, c.expected, c.command.IsFinish(ClusterState{
   392  			LogState:   pb.LogState{},
   393  			TNState:    c.state,
   394  			CNState:    pb.CNState{},
   395  			ProxyState: pb.ProxyState{},
   396  		}))
   397  	}
   398  }
   399  
   400  func TestDeleteProxyStore(t *testing.T) {
   401  	cases := []struct {
   402  		desc     string
   403  		command  DeleteProxyStore
   404  		state    pb.ProxyState
   405  		expected bool
   406  	}{
   407  		{
   408  			desc: "delete proxy store not finished",
   409  			command: DeleteProxyStore{
   410  				StoreID: "a",
   411  			},
   412  			state: pb.ProxyState{
   413  				Stores: map[string]pb.ProxyStore{"a": {
   414  					UUID: "a",
   415  				}},
   416  			},
   417  			expected: false,
   418  		},
   419  		{
   420  			desc: "delete proxy store finished",
   421  			command: DeleteProxyStore{
   422  				StoreID: "a",
   423  			},
   424  			state: pb.ProxyState{
   425  				Stores: map[string]pb.ProxyStore{},
   426  			},
   427  			expected: true,
   428  		},
   429  	}
   430  
   431  	for i, c := range cases {
   432  		fmt.Printf("case %v: %s\n", i, c.desc)
   433  		assert.Equal(t, c.expected, c.command.IsFinish(ClusterState{
   434  			LogState:   pb.LogState{},
   435  			TNState:    pb.TNState{},
   436  			CNState:    pb.CNState{},
   437  			ProxyState: c.state,
   438  		}))
   439  	}
   440  }