You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.2 KiB

5 years ago
  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 testcase struct {
  8. name string
  9. replication string
  10. existingLocations []location
  11. possibleLocation location
  12. expected bool
  13. }
  14. func TestSatisfyReplicaPlacementComplicated(t *testing.T) {
  15. var tests = []testcase{
  16. {
  17. name: "test 100 negative",
  18. replication: "100",
  19. existingLocations: []location{
  20. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  21. },
  22. possibleLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  23. expected: false,
  24. },
  25. {
  26. name: "test 100 positive",
  27. replication: "100",
  28. existingLocations: []location{
  29. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  30. },
  31. possibleLocation: location{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  32. expected: true,
  33. },
  34. {
  35. name: "test 022 positive",
  36. replication: "022",
  37. existingLocations: []location{
  38. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  39. {"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  40. {"dc1", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
  41. },
  42. possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn4"}},
  43. expected: true,
  44. },
  45. {
  46. name: "test 022 negative",
  47. replication: "022",
  48. existingLocations: []location{
  49. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  50. {"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  51. {"dc1", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
  52. },
  53. possibleLocation: location{"dc1", "r4", &master_pb.DataNodeInfo{Id: "dn4"}},
  54. expected: false,
  55. },
  56. {
  57. name: "test 210 moved from 200 positive",
  58. replication: "210",
  59. existingLocations: []location{
  60. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  61. {"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  62. {"dc3", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
  63. },
  64. possibleLocation: location{"dc1", "r4", &master_pb.DataNodeInfo{Id: "dn4"}},
  65. expected: true,
  66. },
  67. {
  68. name: "test 210 moved from 200 negative extra dc",
  69. replication: "210",
  70. existingLocations: []location{
  71. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  72. {"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  73. {"dc3", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
  74. },
  75. possibleLocation: location{"dc4", "r4", &master_pb.DataNodeInfo{Id: "dn4"}},
  76. expected: false,
  77. },
  78. {
  79. name: "test 210 moved from 200 negative extra data node",
  80. replication: "210",
  81. existingLocations: []location{
  82. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  83. {"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  84. {"dc3", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
  85. },
  86. possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn4"}},
  87. expected: false,
  88. },
  89. }
  90. runTests(tests, t)
  91. }
  92. func TestSatisfyReplicaPlacement01x(t *testing.T) {
  93. var tests = []testcase{
  94. {
  95. name: "test 011 same existing rack",
  96. replication: "011",
  97. existingLocations: []location{
  98. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  99. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  100. },
  101. possibleLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn3"}},
  102. expected: true,
  103. },
  104. {
  105. name: "test 011 negative",
  106. replication: "011",
  107. existingLocations: []location{
  108. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  109. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  110. },
  111. possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
  112. expected: false,
  113. },
  114. {
  115. name: "test 011 different existing racks",
  116. replication: "011",
  117. existingLocations: []location{
  118. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  119. {"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  120. },
  121. possibleLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn3"}},
  122. expected: true,
  123. },
  124. {
  125. name: "test 011 different existing racks negative",
  126. replication: "011",
  127. existingLocations: []location{
  128. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  129. {"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  130. },
  131. possibleLocation: location{"dc1", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
  132. expected: false,
  133. },
  134. }
  135. runTests(tests, t)
  136. }
  137. func TestSatisfyReplicaPlacement00x(t *testing.T) {
  138. var tests = []testcase{
  139. {
  140. name: "test 001",
  141. replication: "001",
  142. existingLocations: []location{
  143. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  144. },
  145. possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  146. expected: true,
  147. },
  148. {
  149. name: "test 002 positive",
  150. replication: "002",
  151. existingLocations: []location{
  152. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  153. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  154. },
  155. possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
  156. expected: true,
  157. },
  158. {
  159. name: "test 002 negative, repeat the same node",
  160. replication: "002",
  161. existingLocations: []location{
  162. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  163. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  164. },
  165. possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  166. expected: false,
  167. },
  168. {
  169. name: "test 002 negative, enough node already",
  170. replication: "002",
  171. existingLocations: []location{
  172. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  173. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  174. {"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
  175. },
  176. possibleLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn4"}},
  177. expected: false,
  178. },
  179. }
  180. runTests(tests, t)
  181. }
  182. func runTests(tests []testcase, t *testing.T) {
  183. for _, tt := range tests {
  184. replicaPlacement, _ := super_block.NewReplicaPlacementFromString(tt.replication)
  185. println("replication:", tt.replication, "expected", tt.expected, "name:", tt.name)
  186. if satisfyReplicaPlacement(replicaPlacement, tt.existingLocations, tt.possibleLocation) != tt.expected {
  187. t.Errorf("%s: expect %v add %v to %s %+v",
  188. tt.name, tt.expected, tt.possibleLocation, tt.replication, tt.existingLocations)
  189. }
  190. }
  191. }