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.

195 lines
5.9 KiB

12 years ago
6 years ago
6 years ago
12 years ago
4 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
7 years ago
7 years ago
  1. package topology
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  4. "github.com/chrislusf/seaweedfs/weed/sequence"
  5. "github.com/chrislusf/seaweedfs/weed/storage"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  8. "testing"
  9. )
  10. func TestRemoveDataCenter(t *testing.T) {
  11. topo := setup(topologyLayout)
  12. topo.UnlinkChildNode(NodeId("dc2"))
  13. if topo.GetActiveVolumeCount() != 15 {
  14. t.Fail()
  15. }
  16. topo.UnlinkChildNode(NodeId("dc3"))
  17. if topo.GetActiveVolumeCount() != 12 {
  18. t.Fail()
  19. }
  20. }
  21. func TestHandlingVolumeServerHeartbeat(t *testing.T) {
  22. topo := NewTopology("weedfs", sequence.NewMemorySequencer(), 32*1024, 5, false)
  23. dc := topo.GetOrCreateDataCenter("dc1")
  24. rack := dc.GetOrCreateRack("rack1")
  25. dn := rack.GetOrCreateDataNode("127.0.0.1", 34534, "127.0.0.1", 25, 12)
  26. {
  27. volumeCount := 7
  28. var volumeMessages []*master_pb.VolumeInformationMessage
  29. for k := 1; k <= volumeCount; k++ {
  30. volumeMessage := &master_pb.VolumeInformationMessage{
  31. Id: uint32(k),
  32. Size: uint64(25432),
  33. Collection: "",
  34. FileCount: uint64(2343),
  35. DeleteCount: uint64(345),
  36. DeletedByteCount: 34524,
  37. ReadOnly: false,
  38. ReplicaPlacement: uint32(0),
  39. Version: uint32(needle.CurrentVersion),
  40. Ttl: 0,
  41. }
  42. volumeMessages = append(volumeMessages, volumeMessage)
  43. }
  44. for k := 1; k <= volumeCount; k++ {
  45. volumeMessage := &master_pb.VolumeInformationMessage{
  46. Id: uint32(volumeCount + k),
  47. Size: uint64(25432),
  48. Collection: "",
  49. FileCount: uint64(2343),
  50. DeleteCount: uint64(345),
  51. DeletedByteCount: 34524,
  52. ReadOnly: false,
  53. ReplicaPlacement: uint32(0),
  54. Version: uint32(needle.CurrentVersion),
  55. Ttl: 0,
  56. DiskType: "ssd",
  57. }
  58. volumeMessages = append(volumeMessages, volumeMessage)
  59. }
  60. topo.SyncDataNodeRegistration(volumeMessages, dn)
  61. assert(t, "activeVolumeCount1", int(topo.activeVolumeCount), volumeCount*2)
  62. assert(t, "volumeCount", int(topo.volumeCount), volumeCount)
  63. assert(t, "ssdVolumeCount", int(topo.ssdVolumeCount), volumeCount)
  64. }
  65. {
  66. volumeCount := 7 - 1
  67. var volumeMessages []*master_pb.VolumeInformationMessage
  68. for k := 1; k <= volumeCount; k++ {
  69. volumeMessage := &master_pb.VolumeInformationMessage{
  70. Id: uint32(k),
  71. Size: uint64(254320),
  72. Collection: "",
  73. FileCount: uint64(2343),
  74. DeleteCount: uint64(345),
  75. DeletedByteCount: 345240,
  76. ReadOnly: false,
  77. ReplicaPlacement: uint32(0),
  78. Version: uint32(needle.CurrentVersion),
  79. Ttl: 0,
  80. }
  81. volumeMessages = append(volumeMessages, volumeMessage)
  82. }
  83. topo.SyncDataNodeRegistration(volumeMessages, dn)
  84. //rp, _ := storage.NewReplicaPlacementFromString("000")
  85. //layout := topo.GetVolumeLayout("", rp, needle.EMPTY_TTL)
  86. //assert(t, "writables", len(layout.writables), volumeCount)
  87. assert(t, "activeVolumeCount1", int(topo.activeVolumeCount), volumeCount)
  88. assert(t, "volumeCount", int(topo.volumeCount), volumeCount)
  89. }
  90. {
  91. volumeCount := 6
  92. newVolumeShortMessage := &master_pb.VolumeShortInformationMessage{
  93. Id: uint32(3),
  94. Collection: "",
  95. ReplicaPlacement: uint32(0),
  96. Version: uint32(needle.CurrentVersion),
  97. Ttl: 0,
  98. }
  99. topo.IncrementalSyncDataNodeRegistration(
  100. []*master_pb.VolumeShortInformationMessage{newVolumeShortMessage},
  101. nil,
  102. dn)
  103. rp, _ := super_block.NewReplicaPlacementFromString("000")
  104. layout := topo.GetVolumeLayout("", rp, needle.EMPTY_TTL, storage.HardDriveType)
  105. assert(t, "writables after repeated add", len(layout.writables), volumeCount)
  106. assert(t, "activeVolumeCount1", int(topo.activeVolumeCount), volumeCount)
  107. assert(t, "volumeCount", int(topo.volumeCount), volumeCount)
  108. topo.IncrementalSyncDataNodeRegistration(
  109. nil,
  110. []*master_pb.VolumeShortInformationMessage{newVolumeShortMessage},
  111. dn)
  112. assert(t, "writables after deletion", len(layout.writables), volumeCount-1)
  113. assert(t, "activeVolumeCount1", int(topo.activeVolumeCount), volumeCount-1)
  114. assert(t, "volumeCount", int(topo.volumeCount), volumeCount-1)
  115. topo.IncrementalSyncDataNodeRegistration(
  116. []*master_pb.VolumeShortInformationMessage{newVolumeShortMessage},
  117. nil,
  118. dn)
  119. for vid := range layout.vid2location {
  120. println("after add volume id", vid)
  121. }
  122. for _, vid := range layout.writables {
  123. println("after add writable volume id", vid)
  124. }
  125. assert(t, "writables after add back", len(layout.writables), volumeCount)
  126. }
  127. topo.UnRegisterDataNode(dn)
  128. assert(t, "activeVolumeCount2", int(topo.activeVolumeCount), 0)
  129. }
  130. func assert(t *testing.T, message string, actual, expected int) {
  131. if actual != expected {
  132. t.Fatalf("unexpected %s: %d, expected: %d", message, actual, expected)
  133. }
  134. }
  135. func TestAddRemoveVolume(t *testing.T) {
  136. topo := NewTopology("weedfs", sequence.NewMemorySequencer(), 32*1024, 5, false)
  137. dc := topo.GetOrCreateDataCenter("dc1")
  138. rack := dc.GetOrCreateRack("rack1")
  139. dn := rack.GetOrCreateDataNode("127.0.0.1", 34534, "127.0.0.1", 25, 12)
  140. v := storage.VolumeInfo{
  141. Id: needle.VolumeId(1),
  142. Size: 100,
  143. Collection: "xcollection",
  144. DiskType: "ssd",
  145. FileCount: 123,
  146. DeleteCount: 23,
  147. DeletedByteCount: 45,
  148. ReadOnly: false,
  149. Version: needle.CurrentVersion,
  150. ReplicaPlacement: &super_block.ReplicaPlacement{},
  151. Ttl: needle.EMPTY_TTL,
  152. }
  153. dn.UpdateVolumes([]storage.VolumeInfo{v})
  154. topo.RegisterVolumeLayout(v, dn)
  155. topo.RegisterVolumeLayout(v, dn)
  156. if _, hasCollection := topo.FindCollection(v.Collection); !hasCollection {
  157. t.Errorf("collection %v should exist", v.Collection)
  158. }
  159. topo.UnRegisterVolumeLayout(v, dn)
  160. if _, hasCollection := topo.FindCollection(v.Collection); hasCollection {
  161. t.Errorf("collection %v should not exist", v.Collection)
  162. }
  163. }