command_volume_balance_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 spread into proper data centers",
  19. replication: "100",
  20. replicas: []*VolumeReplica{
  21. {
  22. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  23. },
  24. {
  25. location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  26. },
  27. },
  28. sourceLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  29. targetLocation: location{"dc2", "r2", &master_pb.DataNodeInfo{Id: "dn3"}},
  30. expected: true,
  31. },
  32. {
  33. name: "test move to the same node",
  34. replication: "001",
  35. replicas: []*VolumeReplica{
  36. {
  37. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  38. },
  39. {
  40. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  41. },
  42. },
  43. sourceLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  44. targetLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn2"}},
  45. expected: false,
  46. },
  47. {
  48. name: "test move to the same rack, but existing 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: "dn1"}},
  60. expected: false,
  61. },
  62. {
  63. name: "test move to the same rack, a new 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: "dn3"}},
  75. expected: true,
  76. },
  77. {
  78. name: "test 010 move all to the same rack",
  79. replication: "010",
  80. replicas: []*VolumeReplica{
  81. {
  82. location: &location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn1"}},
  83. },
  84. {
  85. location: &location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  86. },
  87. },
  88. sourceLocation: location{"dc1", "r2", &master_pb.DataNodeInfo{Id: "dn2"}},
  89. targetLocation: location{"dc1", "r1", &master_pb.DataNodeInfo{Id: "dn3"}},
  90. expected: false,
  91. },
  92. {
  93. name: "test 010 move to spread racks",
  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", "r3", &master_pb.DataNodeInfo{Id: "dn3"}},
  105. expected: true,
  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", "r2", &master_pb.DataNodeInfo{Id: "dn3"}},
  120. expected: true,
  121. },
  122. }
  123. for _, tt := range tests {
  124. replicaPlacement, _ := super_block.NewReplicaPlacementFromString(tt.replication)
  125. println("replication:", tt.replication, "expected", tt.expected, "name:", tt.name)
  126. sourceNode := &Node{
  127. info: tt.sourceLocation.dataNode,
  128. dc: tt.sourceLocation.dc,
  129. rack: tt.sourceLocation.rack,
  130. }
  131. targetNode := &Node{
  132. info: tt.targetLocation.dataNode,
  133. dc: tt.targetLocation.dc,
  134. rack: tt.targetLocation.rack,
  135. }
  136. if isGoodMove(replicaPlacement, tt.replicas, sourceNode, targetNode) != tt.expected {
  137. t.Errorf("%s: expect %v move from %v to %s, replication:%v",
  138. tt.name, tt.expected, tt.sourceLocation, tt.targetLocation, tt.replication)
  139. }
  140. }
  141. }