Chris Lu 4 лет назад
Родитель
Сommit
18b98fdb72
1 измененных файлов с 121 добавлено и 55 удалено
  1. 121 55
      weed/shell/command_volume_fix_replication_test.go

+ 121 - 55
weed/shell/command_volume_fix_replication_test.go

@@ -8,11 +8,11 @@ import (
 )
 
 type testcase struct {
-	name              string
-	replication       string
-	existingLocations []location
-	possibleLocation  location
-	expected          bool
+	name             string
+	replication      string
+	replicas         []*VolumeReplica
+	possibleLocation location
+	expected         bool
 }
 
 func TestSatisfyReplicaPlacementComplicated(t *testing.T) {
@@ -21,8 +21,10 @@ func TestSatisfyReplicaPlacementComplicated(t *testing.T) {
 		{
 			name:        "test 100 negative",
 			replication: "100",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
 			expected:         false,
@@ -30,8 +32,10 @@ func TestSatisfyReplicaPlacementComplicated(t *testing.T) {
 		{
 			name:        "test 100 positive",
 			replication: "100",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
 			},
 			possibleLocation: location{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
 			expected:         true,
@@ -39,10 +43,16 @@ func TestSatisfyReplicaPlacementComplicated(t *testing.T) {
 		{
 			name:        "test 022 positive",
 			replication: "022",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
-				{"dc1", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
+				{
+					location: &location{"dc1", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn4"}},
 			expected:         true,
@@ -50,10 +60,16 @@ func TestSatisfyReplicaPlacementComplicated(t *testing.T) {
 		{
 			name:        "test 022 negative",
 			replication: "022",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
-				{"dc1", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
+				{
+					location: &location{"dc1", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r4", &master_pb.DataNodeInfo{Id: "dn4"}},
 			expected:         false,
@@ -61,10 +77,16 @@ func TestSatisfyReplicaPlacementComplicated(t *testing.T) {
 		{
 			name:        "test 210 moved from 200 positive",
 			replication: "210",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
-				{"dc3", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
+				{
+					location: &location{"dc3", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r4", &master_pb.DataNodeInfo{Id: "dn4"}},
 			expected:         true,
@@ -72,10 +94,16 @@ func TestSatisfyReplicaPlacementComplicated(t *testing.T) {
 		{
 			name:        "test 210 moved from 200 negative extra dc",
 			replication: "210",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
-				{"dc3", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
+				{
+					location: &location{"dc3", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+				},
 			},
 			possibleLocation: location{"dc4", "r4", &master_pb.DataNodeInfo{Id: "dn4"}},
 			expected:         false,
@@ -83,10 +111,16 @@ func TestSatisfyReplicaPlacementComplicated(t *testing.T) {
 		{
 			name:        "test 210 moved from 200 negative extra data node",
 			replication: "210",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
-				{"dc3", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
+				{
+					location: &location{"dc3", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn4"}},
 			expected:         false,
@@ -103,9 +137,13 @@ func TestSatisfyReplicaPlacement01x(t *testing.T) {
 		{
 			name:        "test 011 same existing rack",
 			replication: "011",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn3"}},
 			expected:         true,
@@ -113,9 +151,13 @@ func TestSatisfyReplicaPlacement01x(t *testing.T) {
 		{
 			name:        "test 011 negative",
 			replication: "011",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
 			expected:         false,
@@ -123,9 +165,13 @@ func TestSatisfyReplicaPlacement01x(t *testing.T) {
 		{
 			name:        "test 011 different existing racks",
 			replication: "011",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn3"}},
 			expected:         true,
@@ -133,9 +179,13 @@ func TestSatisfyReplicaPlacement01x(t *testing.T) {
 		{
 			name:        "test 011 different existing racks negative",
 			replication: "011",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
 			expected:         false,
@@ -152,8 +202,10 @@ func TestSatisfyReplicaPlacement00x(t *testing.T) {
 		{
 			name:        "test 001",
 			replication: "001",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
 			expected:         true,
@@ -161,9 +213,13 @@ func TestSatisfyReplicaPlacement00x(t *testing.T) {
 		{
 			name:        "test 002 positive",
 			replication: "002",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
 			expected:         true,
@@ -171,9 +227,13 @@ func TestSatisfyReplicaPlacement00x(t *testing.T) {
 		{
 			name:        "test 002 negative, repeat the same node",
 			replication: "002",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
 			expected:         false,
@@ -181,10 +241,16 @@ func TestSatisfyReplicaPlacement00x(t *testing.T) {
 		{
 			name:        "test 002 negative, enough node already",
 			replication: "002",
-			existingLocations: []location{
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
-				{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
+			replicas: []*VolumeReplica{
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
+				},
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
+				},
+				{
+					location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
+				},
 			},
 			possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn4"}},
 			expected:         false,
@@ -199,9 +265,9 @@ func runTests(tests []testcase, t *testing.T) {
 	for _, tt := range tests {
 		replicaPlacement, _ := super_block.NewReplicaPlacementFromString(tt.replication)
 		println("replication:", tt.replication, "expected", tt.expected, "name:", tt.name)
-		if satisfyReplicaPlacement(replicaPlacement, tt.existingLocations, tt.possibleLocation) != tt.expected {
+		if satisfyReplicaPlacement(replicaPlacement, tt.replicas, tt.possibleLocation) != tt.expected {
 			t.Errorf("%s: expect %v add %v to %s %+v",
-				tt.name, tt.expected, tt.possibleLocation, tt.replication, tt.existingLocations)
+				tt.name, tt.expected, tt.possibleLocation, tt.replication, tt.replicas)
 		}
 	}
 }