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.

87 lines
2.6 KiB

  1. package shell
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  9. )
  10. func TestEcDistribution(t *testing.T) {
  11. topologyInfo := parseOutput(topoData)
  12. // find out all volume servers with one slot left.
  13. ecNodes, totalFreeEcSlots := collectEcVolumeServersByDc(topologyInfo, "")
  14. sortEcNodesByFreeslotsDescending(ecNodes)
  15. if totalFreeEcSlots < erasure_coding.TotalShardsCount {
  16. t.Errorf("not enough free ec shard slots: %d", totalFreeEcSlots)
  17. }
  18. allocatedDataNodes := ecNodes
  19. if len(allocatedDataNodes) > erasure_coding.TotalShardsCount {
  20. allocatedDataNodes = allocatedDataNodes[:erasure_coding.TotalShardsCount]
  21. }
  22. for _, dn := range allocatedDataNodes {
  23. // fmt.Printf("info %+v %+v\n", dn.info, dn)
  24. fmt.Printf("=> %+v %+v\n", dn.info.Id, dn.freeEcSlot)
  25. }
  26. }
  27. func TestVolumeIdToReplicaPlacement(t *testing.T) {
  28. topo1 := parseOutput(topoData)
  29. topo2 := parseOutput(topoData2)
  30. testCases := []struct {
  31. topology *master_pb.TopologyInfo
  32. vid string
  33. want string
  34. wantErr string
  35. }{
  36. {topo1, "", "", "failed to resolve replica placement for volume ID 0"},
  37. {topo1, "0", "", "failed to resolve replica placement for volume ID 0"},
  38. {topo1, "1", "100", ""},
  39. {topo1, "296", "100", ""},
  40. {topo2, "", "", "failed to resolve replica placement for volume ID 0"},
  41. {topo2, "19012", "", "failed to resolve replica placement for volume ID 19012"},
  42. {topo2, "6271", "002", ""},
  43. {topo2, "17932", "002", ""},
  44. }
  45. for _, tc := range testCases {
  46. vid, _ := needle.NewVolumeId(tc.vid)
  47. ecNodes, _ := collectEcVolumeServersByDc(tc.topology, "")
  48. got, gotErr := volumeIdToReplicaPlacement(vid, ecNodes)
  49. if tc.wantErr == "" && gotErr != nil {
  50. t.Errorf("expected no error for volume '%s', got '%s'", tc.vid, gotErr.Error())
  51. continue
  52. }
  53. if tc.wantErr != "" {
  54. if gotErr == nil {
  55. t.Errorf("got no error for volume '%s', expected '%s'", tc.vid, tc.wantErr)
  56. continue
  57. }
  58. if gotErr.Error() != tc.wantErr {
  59. t.Errorf("expected error '%s' for volume '%s', got '%s'", tc.wantErr, tc.vid, gotErr.Error())
  60. continue
  61. }
  62. }
  63. if got == nil {
  64. if tc.want != "" {
  65. t.Errorf("expected replica placement '%s' for volume '%s', got nil", tc.want, tc.vid)
  66. }
  67. continue
  68. }
  69. want, _ := super_block.NewReplicaPlacementFromString(tc.want)
  70. if !got.Equals(want) {
  71. t.Errorf("got replica placement '%s' for volune '%s', want '%s'", got.String(), tc.vid, want.String())
  72. }
  73. }
  74. }