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.

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