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.

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