volume_layout_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package topology
  2. import (
  3. "testing"
  4. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  5. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  6. )
  7. func TestVolumesBinaryState(t *testing.T) {
  8. vids := []needle.VolumeId{
  9. needle.VolumeId(1),
  10. needle.VolumeId(2),
  11. needle.VolumeId(3),
  12. needle.VolumeId(4),
  13. needle.VolumeId(5),
  14. }
  15. dns := []*DataNode{
  16. &DataNode{
  17. Ip: "127.0.0.1",
  18. Port: 8081,
  19. },
  20. &DataNode{
  21. Ip: "127.0.0.1",
  22. Port: 8082,
  23. },
  24. &DataNode{
  25. Ip: "127.0.0.1",
  26. Port: 8083,
  27. },
  28. }
  29. rp, _ := super_block.NewReplicaPlacementFromString("002")
  30. state_exist := NewVolumesBinaryState(readOnlyState, rp, ExistCopies())
  31. state_exist.Add(vids[0], dns[0])
  32. state_exist.Add(vids[0], dns[1])
  33. state_exist.Add(vids[1], dns[2])
  34. state_exist.Add(vids[2], dns[1])
  35. state_exist.Add(vids[4], dns[1])
  36. state_exist.Add(vids[4], dns[2])
  37. state_no := NewVolumesBinaryState(readOnlyState, rp, NoCopies())
  38. state_no.Add(vids[0], dns[0])
  39. state_no.Add(vids[0], dns[1])
  40. state_no.Add(vids[3], dns[1])
  41. tests := []struct {
  42. name string
  43. state *volumesBinaryState
  44. expectResult []bool
  45. update func()
  46. expectResultAfterUpdate []bool
  47. }{
  48. {
  49. name: "mark true when exist copies",
  50. state: state_exist,
  51. expectResult: []bool{true, true, true, false, true},
  52. update: func() {
  53. state_exist.Remove(vids[0], dns[2])
  54. state_exist.Remove(vids[1], dns[2])
  55. state_exist.Remove(vids[3], dns[2])
  56. state_exist.Remove(vids[4], dns[1])
  57. state_exist.Remove(vids[4], dns[2])
  58. },
  59. expectResultAfterUpdate: []bool{true, false, true, false, false},
  60. },
  61. {
  62. name: "mark true when inexist copies",
  63. state: state_no,
  64. expectResult: []bool{false, true, true, false, true},
  65. update: func() {
  66. state_no.Remove(vids[0], dns[2])
  67. state_no.Remove(vids[1], dns[2])
  68. state_no.Add(vids[2], dns[1])
  69. state_no.Remove(vids[3], dns[1])
  70. state_no.Remove(vids[4], dns[2])
  71. },
  72. expectResultAfterUpdate: []bool{false, true, false, true, true},
  73. },
  74. }
  75. for _, test := range tests {
  76. t.Run(test.name, func(t *testing.T) {
  77. var result []bool
  78. for index, _ := range vids {
  79. result = append(result, test.state.IsTrue(vids[index]))
  80. }
  81. if len(result) != len(test.expectResult) {
  82. t.Fatalf("len(result) != len(expectResult), got %d, expected %d\n",
  83. len(result), len(test.expectResult))
  84. }
  85. for index, val := range result {
  86. if val != test.expectResult[index] {
  87. t.Fatalf("result not matched, index %d, got %v, expect %v\n",
  88. index, val, test.expectResult[index])
  89. }
  90. }
  91. test.update()
  92. var updateResult []bool
  93. for index, _ := range vids {
  94. updateResult = append(updateResult, test.state.IsTrue(vids[index]))
  95. }
  96. if len(updateResult) != len(test.expectResultAfterUpdate) {
  97. t.Fatalf("len(updateResult) != len(expectResultAfterUpdate), got %d, expected %d\n",
  98. len(updateResult), len(test.expectResultAfterUpdate))
  99. }
  100. for index, val := range updateResult {
  101. if val != test.expectResultAfterUpdate[index] {
  102. t.Fatalf("update result not matched, index %d, got %v, expect %v\n",
  103. index, val, test.expectResultAfterUpdate[index])
  104. }
  105. }
  106. })
  107. }
  108. }