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.

287 lines
8.8 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. vid, _ := needle.NewVolumeId(tc.vid)
  102. ecNodes, _ := collectEcVolumeServersByDc(tc.topology, "")
  103. ecb := ecBalancer{
  104. ecNodes: ecNodes,
  105. replicaPlacement: ecReplicaPlacement,
  106. }
  107. got, gotErr := ecb.volumeIdToReplicaPlacement(vid)
  108. if err := errorCheck(gotErr, tc.wantErr); err != nil {
  109. t.Errorf("volume %q: %s", tc.vid, err.Error())
  110. continue
  111. }
  112. if got == nil {
  113. if tc.want != "" {
  114. t.Errorf("expected replica placement %q for volume %q, got nil", tc.want, tc.vid)
  115. }
  116. continue
  117. }
  118. want, _ := super_block.NewReplicaPlacementFromString(tc.want)
  119. if !got.Equals(want) {
  120. t.Errorf("got replica placement %q for volune %q, want %q", got.String(), tc.vid, want.String())
  121. }
  122. }
  123. }
  124. func TestPickRackToBalanceShardsInto(t *testing.T) {
  125. testCases := []struct {
  126. topology *master_pb.TopologyInfo
  127. vid string
  128. replicaPlacement string
  129. wantOneOf []string
  130. wantErr string
  131. }{
  132. // Non-EC volumes. We don't care about these, but the function should return all racks as a safeguard.
  133. {topologyEc, "", "123", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}, ""},
  134. {topologyEc, "6225", "123", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}, ""},
  135. {topologyEc, "6226", "123", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}, ""},
  136. {topologyEc, "6241", "123", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}, ""},
  137. {topologyEc, "6242", "123", []string{"rack1", "rack2", "rack3", "rack4", "rack5", "rack6"}, ""},
  138. // EC volumes.
  139. {topologyEc, "9577", "", nil, "shards 1 >= replica placement limit for other racks (0)"},
  140. {topologyEc, "9577", "111", nil, "shards 1 >= replica placement limit for other racks (1)"},
  141. {topologyEc, "9577", "222", []string{"rack1", "rack2", "rack3"}, ""},
  142. {topologyEc, "10457", "222", []string{"rack1"}, ""},
  143. {topologyEc, "12737", "222", []string{"rack2"}, ""},
  144. {topologyEc, "14322", "222", []string{"rack3"}, ""},
  145. }
  146. for _, tc := range testCases {
  147. vid, _ := needle.NewVolumeId(tc.vid)
  148. ecNodes, _ := collectEcVolumeServersByDc(tc.topology, "")
  149. rp, _ := super_block.NewReplicaPlacementFromString(tc.replicaPlacement)
  150. ecb := &ecBalancer{
  151. ecNodes: ecNodes,
  152. replicaPlacement: rp,
  153. }
  154. racks := ecb.racks()
  155. rackToShardCount := countShardsByRack(vid, ecNodes)
  156. averageShardsPerEcRack := ceilDivide(erasure_coding.TotalShardsCount, len(racks))
  157. got, gotErr := ecb.pickRackToBalanceShardsInto(racks, rackToShardCount, averageShardsPerEcRack)
  158. if err := errorCheck(gotErr, tc.wantErr); err != nil {
  159. t.Errorf("volume %q: %s", tc.vid, err.Error())
  160. continue
  161. }
  162. if string(got) == "" && len(tc.wantOneOf) == 0 {
  163. continue
  164. }
  165. found := false
  166. for _, want := range tc.wantOneOf {
  167. if got := string(got); got == want {
  168. found = true
  169. break
  170. }
  171. }
  172. if !(found) {
  173. t.Errorf("expected one of %v for volume %q, got %q", tc.wantOneOf, tc.vid, got)
  174. }
  175. }
  176. }
  177. func TestPickEcNodeToBalanceShardsInto(t *testing.T) {
  178. testCases := []struct {
  179. topology *master_pb.TopologyInfo
  180. nodeId string
  181. vid string
  182. wantOneOf []string
  183. wantErr string
  184. }{
  185. {topologyEc, "", "", nil, "INTERNAL: missing source nodes"},
  186. {topologyEc, "idontexist", "12737", nil, "INTERNAL: missing source nodes"},
  187. // Non-EC nodes. We don't care about these, but the function should return all available target nodes as a safeguard.
  188. {
  189. topologyEc, "172.19.0.10:8702", "6225", []string{
  190. "172.19.0.13:8701", "172.19.0.14:8711", "172.19.0.16:8704", "172.19.0.17:8703",
  191. "172.19.0.19:8700", "172.19.0.20:8706", "172.19.0.21:8710", "172.19.0.3:8708",
  192. "172.19.0.4:8707", "172.19.0.5:8705", "172.19.0.6:8713", "172.19.0.8:8709",
  193. "172.19.0.9:8712"},
  194. "",
  195. },
  196. {
  197. topologyEc, "172.19.0.8:8709", "6226", []string{
  198. "172.19.0.10:8702", "172.19.0.13:8701", "172.19.0.14:8711", "172.19.0.16:8704",
  199. "172.19.0.17:8703", "172.19.0.19:8700", "172.19.0.20:8706", "172.19.0.21:8710",
  200. "172.19.0.3:8708", "172.19.0.4:8707", "172.19.0.5:8705", "172.19.0.6:8713",
  201. "172.19.0.9:8712"},
  202. "",
  203. },
  204. // EC volumes.
  205. {topologyEc, "172.19.0.10:8702", "14322", []string{
  206. "172.19.0.14:8711", "172.19.0.5:8705", "172.19.0.6:8713"},
  207. ""},
  208. {topologyEc, "172.19.0.13:8701", "10457", []string{
  209. "172.19.0.10:8702", "172.19.0.6:8713"},
  210. ""},
  211. {topologyEc, "172.19.0.17:8703", "12737", []string{
  212. "172.19.0.13:8701"},
  213. ""},
  214. {topologyEc, "172.19.0.20:8706", "14322", []string{
  215. "172.19.0.14:8711", "172.19.0.5:8705", "172.19.0.6:8713"},
  216. ""},
  217. }
  218. for _, tc := range testCases {
  219. vid, _ := needle.NewVolumeId(tc.vid)
  220. allEcNodes, _ := collectEcVolumeServersByDc(tc.topology, "")
  221. ecb := &ecBalancer{
  222. ecNodes: allEcNodes,
  223. }
  224. // Resolve target node by name
  225. var ecNode *EcNode
  226. for _, n := range allEcNodes {
  227. if n.info.Id == tc.nodeId {
  228. ecNode = n
  229. break
  230. }
  231. }
  232. averageShardsPerEcNode := 5
  233. got, gotErr := ecb.pickEcNodeToBalanceShardsInto(vid, ecNode, allEcNodes, averageShardsPerEcNode)
  234. if err := errorCheck(gotErr, tc.wantErr); err != nil {
  235. t.Errorf("node %q, volume %q: %s", tc.nodeId, tc.vid, err.Error())
  236. continue
  237. }
  238. if got == nil {
  239. if len(tc.wantOneOf) == 0 {
  240. continue
  241. }
  242. t.Errorf("node %q, volume %q: got no node, want %q", tc.nodeId, tc.vid, tc.wantOneOf)
  243. continue
  244. }
  245. found := false
  246. for _, want := range tc.wantOneOf {
  247. if got := got.info.Id; got == want {
  248. found = true
  249. break
  250. }
  251. }
  252. if !(found) {
  253. t.Errorf("expected one of %v for volume %q, got %q", tc.wantOneOf, tc.vid, got.info.Id)
  254. }
  255. }
  256. }