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.

241 lines
7.5 KiB

  1. package shell
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  10. )
  11. var (
  12. topology1 = parseOutput(topoData)
  13. topology2 = parseOutput(topoData2)
  14. topologyEc = parseOutput(topoDataEc)
  15. )
  16. func errorCheck(got error, want string) error {
  17. if got == nil && want == "" {
  18. return nil
  19. }
  20. if got != nil && want == "" {
  21. return fmt.Errorf("expected no error, got %q", got.Error())
  22. }
  23. if got == nil && want != "" {
  24. return fmt.Errorf("got no error, expected %q", want)
  25. }
  26. if !strings.Contains(got.Error(), want) {
  27. return fmt.Errorf("expected error %q, got %q", want, got.Error())
  28. }
  29. return nil
  30. }
  31. func TestEcDistribution(t *testing.T) {
  32. // find out all volume servers with one slot left.
  33. ecNodes, totalFreeEcSlots := collectEcVolumeServersByDc(topology1, "")
  34. sortEcNodesByFreeslotsDescending(ecNodes)
  35. if totalFreeEcSlots < erasure_coding.TotalShardsCount {
  36. t.Errorf("not enough free ec shard slots: %d", totalFreeEcSlots)
  37. }
  38. allocatedDataNodes := ecNodes
  39. if len(allocatedDataNodes) > erasure_coding.TotalShardsCount {
  40. allocatedDataNodes = allocatedDataNodes[:erasure_coding.TotalShardsCount]
  41. }
  42. for _, dn := range allocatedDataNodes {
  43. // fmt.Printf("info %+v %+v\n", dn.info, dn)
  44. fmt.Printf("=> %+v %+v\n", dn.info.Id, dn.freeEcSlot)
  45. }
  46. }
  47. func TestVolumeIdToReplicaPlacement(t *testing.T) {
  48. getDefaultReplicaPlacementOrig := getDefaultReplicaPlacement
  49. getDefaultReplicaPlacement = func(commandEnv *CommandEnv) (*super_block.ReplicaPlacement, error) {
  50. return super_block.NewReplicaPlacementFromString("123")
  51. }
  52. defer func() {
  53. getDefaultReplicaPlacement = getDefaultReplicaPlacementOrig
  54. }()
  55. testCases := []struct {
  56. topology *master_pb.TopologyInfo
  57. vid string
  58. want string
  59. wantErr string
  60. }{
  61. {topology1, "", "", "failed to resolve replica placement"},
  62. {topology1, "0", "", "failed to resolve replica placement"},
  63. {topology1, "1", "100", ""},
  64. {topology1, "296", "100", ""},
  65. {topology2, "", "", "failed to resolve replica placement"},
  66. {topology2, "19012", "", "failed to resolve replica placement"},
  67. {topology2, "6271", "002", ""},
  68. {topology2, "17932", "002", ""},
  69. {topologyEc, "", "", "failed to resolve replica placement"},
  70. {topologyEc, "0", "", "failed to resolve replica placement"},
  71. {topologyEc, "6225", "002", ""},
  72. {topologyEc, "6241", "002", ""},
  73. {topologyEc, "9577", "123", ""}, // EC volume
  74. {topologyEc, "12737", "123", ""}, // EC volume
  75. }
  76. for _, tc := range testCases {
  77. commandEnv := &CommandEnv{}
  78. vid, _ := needle.NewVolumeId(tc.vid)
  79. ecNodes, _ := collectEcVolumeServersByDc(tc.topology, "")
  80. got, gotErr := volumeIdToReplicaPlacement(commandEnv, vid, ecNodes)
  81. if err := errorCheck(gotErr, tc.wantErr); err != nil {
  82. t.Errorf("volume %q: %s", tc.vid, err.Error())
  83. continue
  84. }
  85. if got == nil {
  86. if tc.want != "" {
  87. t.Errorf("expected replica placement %q for volume %q, got nil", tc.want, tc.vid)
  88. }
  89. continue
  90. }
  91. want, _ := super_block.NewReplicaPlacementFromString(tc.want)
  92. if !got.Equals(want) {
  93. t.Errorf("got replica placement %q for volune %q, want %q", got.String(), tc.vid, want.String())
  94. }
  95. }
  96. }
  97. func TestPickRackToBalanceShardsInto(t *testing.T) {
  98. testCases := []struct {
  99. topology *master_pb.TopologyInfo
  100. vid string
  101. wantOneOf []string
  102. }{
  103. // Non-EC volumes. We don't care about these, but the function should return all racks as a safeguard.
  104. {topologyEc, "", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}},
  105. {topologyEc, "6225", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}},
  106. {topologyEc, "6226", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}},
  107. {topologyEc, "6241", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}},
  108. {topologyEc, "6242", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}},
  109. // EC volumes.
  110. {topologyEc, "9577", []string{"rack1", "rack2", "rack3"}},
  111. {topologyEc, "10457", []string{"rack1"}},
  112. {topologyEc, "12737", []string{"rack2"}},
  113. {topologyEc, "14322", []string{"rack3"}},
  114. }
  115. for _, tc := range testCases {
  116. vid, _ := needle.NewVolumeId(tc.vid)
  117. ecNodes, _ := collectEcVolumeServersByDc(tc.topology, "")
  118. racks := collectRacks(ecNodes)
  119. locations := ecNodes
  120. rackToShardCount := countShardsByRack(vid, locations)
  121. averageShardsPerEcRack := ceilDivide(erasure_coding.TotalShardsCount, len(racks))
  122. got, gotErr := pickRackToBalanceShardsInto(racks, rackToShardCount, nil, averageShardsPerEcRack)
  123. if gotErr != nil {
  124. t.Errorf("volume %q: %s", tc.vid, gotErr.Error())
  125. continue
  126. }
  127. if string(got) == "" && len(tc.wantOneOf) == 0 {
  128. continue
  129. }
  130. found := false
  131. for _, want := range tc.wantOneOf {
  132. if got := string(got); got == want {
  133. found = true
  134. break
  135. }
  136. }
  137. if !(found) {
  138. t.Errorf("expected one of %v for volume %q, got %q", tc.wantOneOf, tc.vid, got)
  139. }
  140. }
  141. }
  142. func TestPickEcNodeToBalanceShardsInto(t *testing.T) {
  143. testCases := []struct {
  144. topology *master_pb.TopologyInfo
  145. nodeId string
  146. vid string
  147. wantOneOf []string
  148. wantErr string
  149. }{
  150. {topologyEc, "", "", nil, "INTERNAL: missing source nodes"},
  151. {topologyEc, "idontexist", "12737", nil, "INTERNAL: missing source nodes"},
  152. // Non-EC nodes. We don't care about these, but the function should return all available target nodes as a safeguard.
  153. {
  154. topologyEc, "172.19.0.10:8702", "6225", []string{
  155. "172.19.0.13:8701", "172.19.0.14:8711", "172.19.0.16:8704", "172.19.0.17:8703",
  156. "172.19.0.19:8700", "172.19.0.20:8706", "172.19.0.21:8710", "172.19.0.3:8708",
  157. "172.19.0.4:8707", "172.19.0.5:8705", "172.19.0.6:8713", "172.19.0.8:8709",
  158. "172.19.0.9:8712"},
  159. "",
  160. },
  161. {
  162. topologyEc, "172.19.0.8:8709", "6226", []string{
  163. "172.19.0.10:8702", "172.19.0.13:8701", "172.19.0.14:8711", "172.19.0.16:8704",
  164. "172.19.0.17:8703", "172.19.0.19:8700", "172.19.0.20:8706", "172.19.0.21:8710",
  165. "172.19.0.3:8708", "172.19.0.4:8707", "172.19.0.5:8705", "172.19.0.6:8713",
  166. "172.19.0.9:8712"},
  167. "",
  168. },
  169. // EC volumes.
  170. {topologyEc, "172.19.0.10:8702", "14322", []string{
  171. "172.19.0.14:8711", "172.19.0.5:8705", "172.19.0.6:8713"},
  172. ""},
  173. {topologyEc, "172.19.0.13:8701", "10457", []string{
  174. "172.19.0.10:8702", "172.19.0.6:8713"},
  175. ""},
  176. {topologyEc, "172.19.0.17:8703", "12737", []string{
  177. "172.19.0.13:8701"},
  178. ""},
  179. {topologyEc, "172.19.0.20:8706", "14322", []string{
  180. "172.19.0.14:8711", "172.19.0.5:8705", "172.19.0.6:8713"},
  181. ""},
  182. }
  183. for _, tc := range testCases {
  184. vid, _ := needle.NewVolumeId(tc.vid)
  185. allEcNodes, _ := collectEcVolumeServersByDc(tc.topology, "")
  186. // Resolve target node by name
  187. var ecNode *EcNode
  188. for _, n := range allEcNodes {
  189. if n.info.Id == tc.nodeId {
  190. ecNode = n
  191. break
  192. }
  193. }
  194. averageShardsPerEcNode := 5
  195. got, gotErr := pickEcNodeToBalanceShardsInto(vid, ecNode, allEcNodes, nil, averageShardsPerEcNode)
  196. if err := errorCheck(gotErr, tc.wantErr); err != nil {
  197. t.Errorf("node %q, volume %q: %s", tc.nodeId, tc.vid, err.Error())
  198. continue
  199. }
  200. if got == nil {
  201. if len(tc.wantOneOf) == 0 {
  202. continue
  203. }
  204. t.Errorf("node %q, volume %q: got no node, want %q", tc.nodeId, tc.vid, tc.wantOneOf)
  205. continue
  206. }
  207. found := false
  208. for _, want := range tc.wantOneOf {
  209. if got := got.info.Id; got == want {
  210. found = true
  211. break
  212. }
  213. }
  214. if !(found) {
  215. t.Errorf("expected one of %v for volume %q, got %q", tc.wantOneOf, tc.vid, got.info.Id)
  216. }
  217. }
  218. }