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.

266 lines
8.8 KiB

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