Repository: matrixorigin/matrixone
Pull request #3865 (2022-07-11 03:46:35)

Hakeeper: fix bootstrap tests
**What type of PR is this?**

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

**Which issue(s) this PR fixes:**

issue #

**What this PR does / why we need it:**

Not Available

**Special notes for your reviewer:**

Not Available

**Additional documentation (e.g. design docs, usage docs, etc.):**

Not Available



Patch:
diff --git a/pkg/hakeeper/bootstrap/bootstrap.go b/pkg/hakeeper/bootstrap/bootstrap.go
index f9e7a2ca3..eb9d28886 100644
--- a/pkg/hakeeper/bootstrap/bootstrap.go
+++ b/pkg/hakeeper/bootstrap/bootstrap.go
@@ -58,6 +58,11 @@ func (bm *Manager) Bootstrap(alloc util.IDAllocator,
 	dnStores := dnStoresSortedByTick(dn.Stores)
 
 	for _, shardRecord := range bm.cluster.LogShards {
+		// skip HAKeeper shard
+		if shardRecord.ShardID == 0 {
+			continue
+		}
+
 		if shardRecord.NumberOfReplicas > uint64(len(logStores)) {
 			return nil, errors.New("not enough log stores")
 		}
@@ -123,13 +128,10 @@ func (bm *Manager) Bootstrap(alloc util.IDAllocator,
 }
 
 func (bm *Manager) CheckBootstrap(log pb.LogState) bool {
-	for _, shardInfo := range log.Shards {
-		var shardRecord metadata.LogShardRecord
-		for _, record := range bm.cluster.LogShards {
-			if record.ShardID == shardInfo.ShardID {
-				shardRecord = record
-				break
-			}
+	for _, shardRecord := range bm.cluster.LogShards {
+		shardInfo, ok := log.Shards[shardRecord.ShardID]
+		if !ok {
+			return false
 		}
 
 		if uint64(len(shardInfo.Replicas))*2 <= shardRecord.NumberOfReplicas {


Test patch:
diff --git a/pkg/hakeeper/bootstrap/bootstrap_test.go b/pkg/hakeeper/bootstrap/bootstrap_test.go
index 3ce9cb123..3df7e2557 100644
--- a/pkg/hakeeper/bootstrap/bootstrap_test.go
+++ b/pkg/hakeeper/bootstrap/bootstrap_test.go
@@ -188,6 +188,18 @@ func TestCheckBootstrap(t *testing.T) {
 			},
 			expected: false,
 		},
+		{
+			desc: "shard 1 not exists in log state",
+			cluster: pb.ClusterInfo{
+				LogShards: []metadata.LogShardRecord{
+					{ShardID: 1, NumberOfReplicas: 3},
+				},
+			},
+			log: pb.LogState{
+				Shards: map[uint64]pb.LogShardInfo{},
+			},
+			expected: false,
+		},
 	}
 
 	for i, c := range cases {
@@ -301,7 +313,7 @@ func TestIssue3845(t *testing.T) {
 				}},
 			},
 			log: pb.LogState{
-				Shards: map[uint64]pb.LogShardInfo{1: {
+				Shards: map[uint64]pb.LogShardInfo{0: {
 					ShardID:  0,
 					Replicas: map[uint64]string{1: "a"},
 				}},
diff --git a/pkg/logservice/hachecker_test.go b/pkg/logservice/hachecker_test.go
index 2ac6efb70..9e1127402 100644
--- a/pkg/logservice/hachecker_test.go
+++ b/pkg/logservice/hachecker_test.go
@@ -139,10 +139,10 @@ func TestSetInitialClusterInfo(t *testing.T) {
 }
 
 // FIXME: re-enable this test
-/*
+
 func TestFailedBootstrap(t *testing.T) {
 	testBootstrap(t, true)
-}*/
+}
 
 func TestBootstrap(t *testing.T) {
 	testBootstrap(t, false)
@@ -181,7 +181,7 @@ func testBootstrap(t *testing.T, fail bool) {
 		assert.Equal(t, pb.HAKeeperBootstrapCommandsReceived, state.State)
 		assert.Equal(t, uint64(checkBootstrapInterval), store.bootstrapCheckInterval)
 		require.NotNil(t, store.bootstrapMgr)
-		// assert.False(t, store.bootstrapMgr.CheckBootstrap(state.LogState))
+		assert.False(t, store.bootstrapMgr.CheckBootstrap(state.LogState))
 
 		if fail {
 			// keep checking, bootstrap will eventually be set as failed
@@ -195,10 +195,10 @@ func testBootstrap(t *testing.T, fail bool) {
 		} else {
 			cb, err := store.getCommandBatch(ctx, store.id())
 			require.NoError(t, err)
-			require.Equal(t, 2, len(cb.Commands))
+			require.Equal(t, 1, len(cb.Commands))
 			service := &Service{store: store}
 			service.handleStartReplica(cb.Commands[0])
-			service.handleStartReplica(cb.Commands[1])
+			//service.handleStartReplica(cb.Commands[1])
 
 			for i := 0; i < 100; i++ {
 				ctx, cancel := context.WithTimeout(context.Background(), time.Second)