command_volume_balance_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package shell
  2. import (
  3. "testing"
  4. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  5. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  6. )
  7. type testMoveCase struct {
  8. name string
  9. replication string
  10. replicas []*VolumeReplica
  11. sourceLocation location
  12. targetLocation location
  13. expected bool
  14. }
  15. func TestIsGoodMove(t *testing.T) {
  16. var tests = []testMoveCase{
  17. {
  18. name: "test 100 move to wrong data centers",
  19. replication: "100",
  20. replicas: []*VolumeReplica{
  21. {
  22. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  23. },
  24. {
  25. location: &location{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  26. },
  27. },
  28. sourceLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  29. targetLocation: location{"dc2", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
  30. expected: false,
  31. },
  32. {
  33. name: "test 100 move to spread into proper data centers",
  34. replication: "100",
  35. replicas: []*VolumeReplica{
  36. {
  37. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  38. },
  39. {
  40. location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  41. },
  42. },
  43. sourceLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  44. targetLocation: location{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn3"}},
  45. expected: true,
  46. },
  47. {
  48. name: "test move to the same node",
  49. replication: "001",
  50. replicas: []*VolumeReplica{
  51. {
  52. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  53. },
  54. {
  55. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  56. },
  57. },
  58. sourceLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  59. targetLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  60. expected: false,
  61. },
  62. {
  63. name: "test move to the same rack, but existing node",
  64. replication: "001",
  65. replicas: []*VolumeReplica{
  66. {
  67. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  68. },
  69. {
  70. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  71. },
  72. },
  73. sourceLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  74. targetLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  75. expected: false,
  76. },
  77. {
  78. name: "test move to the same rack, a new node",
  79. replication: "001",
  80. replicas: []*VolumeReplica{
  81. {
  82. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  83. },
  84. {
  85. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  86. },
  87. },
  88. sourceLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  89. targetLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
  90. expected: true,
  91. },
  92. {
  93. name: "test 010 move all to the same rack",
  94. replication: "010",
  95. replicas: []*VolumeReplica{
  96. {
  97. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  98. },
  99. {
  100. location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  101. },
  102. },
  103. sourceLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  104. targetLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
  105. expected: false,
  106. },
  107. {
  108. name: "test 010 move to spread racks",
  109. replication: "010",
  110. replicas: []*VolumeReplica{
  111. {
  112. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  113. },
  114. {
  115. location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  116. },
  117. },
  118. sourceLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  119. targetLocation: location{"dc1", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
  120. expected: true,
  121. },
  122. {
  123. name: "test 010 move to spread racks",
  124. replication: "010",
  125. replicas: []*VolumeReplica{
  126. {
  127. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  128. },
  129. {
  130. location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  131. },
  132. },
  133. sourceLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  134. targetLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn3"}},
  135. expected: true,
  136. },
  137. }
  138. for _, tt := range tests {
  139. replicaPlacement, _ := super_block.NewReplicaPlacementFromString(tt.replication)
  140. println("replication:", tt.replication, "expected", tt.expected, "name:", tt.name)
  141. sourceNode := &Node{
  142. info: tt.sourceLocation.dataNode,
  143. dc: tt.sourceLocation.dc,
  144. rack: tt.sourceLocation.rack,
  145. }
  146. targetNode := &Node{
  147. info: tt.targetLocation.dataNode,
  148. dc: tt.targetLocation.dc,
  149. rack: tt.targetLocation.rack,
  150. }
  151. if isGoodMove(replicaPlacement, tt.replicas, sourceNode, targetNode) != tt.expected {
  152. t.Errorf("%s: expect %v move from %v to %s, replication:%v",
  153. tt.name, tt.expected, tt.sourceLocation, tt.targetLocation, tt.replication)
  154. }
  155. }
  156. }