volume_vacuum_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package storage
  2. import (
  3. "math/rand"
  4. "testing"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  8. "github.com/chrislusf/seaweedfs/weed/storage/types"
  9. )
  10. /*
  11. makediff test steps
  12. 1. launch weed server at your local/dev environment, (option
  13. "garbageThreshold" for master and option "max" for volume should be set with specific value which would let
  14. preparing test prerequisite easier )
  15. a) ./weed master -garbageThreshold=0.99 -mdir=./m
  16. b) ./weed volume -dir=./data -max=1 -mserver=localhost:9333 -port=8080
  17. 2. upload 4 different files, you could call dir/assign to get 4 different fids
  18. a) upload file A with fid a
  19. b) upload file B with fid b
  20. c) upload file C with fid c
  21. d) upload file D with fid d
  22. 3. update file A and C
  23. a) modify file A and upload file A with fid a
  24. b) modify file C and upload file C with fid c
  25. c) record the current 1.idx's file size(lastCompactIndexOffset value)
  26. 4. Compacting the data file
  27. a) run curl http://localhost:8080/admin/vacuum/compact?volumeId=1
  28. b) verify the 1.cpd and 1.cpx is created under volume directory
  29. 5. update file B and delete file D
  30. a) modify file B and upload file B with fid b
  31. d) delete file B with fid b
  32. 6. Now you could run the following UT case, the case should be run successfully
  33. 7. Compact commit manually
  34. a) mv 1.cpd 1.dat
  35. b) mv 1.cpx 1.idx
  36. 8. Restart Volume Server
  37. 9. Now you should get updated file A,B,C
  38. */
  39. func TestMakeDiff(t *testing.T) {
  40. v := new(Volume)
  41. // lastCompactIndexOffset value is the index file size before step 4
  42. v.lastCompactIndexOffset = 96
  43. v.SuperBlock.Version = 0x2
  44. /*
  45. err := v.makeupDiff(
  46. "/yourpath/1.cpd",
  47. "/yourpath/1.cpx",
  48. "/yourpath/1.dat",
  49. "/yourpath/1.idx")
  50. if err != nil {
  51. t.Errorf("makeupDiff err is %v", err)
  52. } else {
  53. t.Log("makeupDiff Succeeded")
  54. }
  55. */
  56. }
  57. func TestCompaction(t *testing.T) {
  58. dir := t.TempDir()
  59. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0)
  60. if err != nil {
  61. t.Fatalf("volume creation: %v", err)
  62. }
  63. beforeCommitFileCount := 10000
  64. afterCommitFileCount := 10000
  65. infos := make([]*needleInfo, beforeCommitFileCount+afterCommitFileCount)
  66. for i := 1; i <= beforeCommitFileCount; i++ {
  67. doSomeWritesDeletes(i, v, t, infos)
  68. }
  69. startTime := time.Now()
  70. v.Compact2(0, 0, nil)
  71. speed := float64(v.ContentSize()) / time.Now().Sub(startTime).Seconds()
  72. t.Logf("compaction speed: %.2f bytes/s", speed)
  73. for i := 1; i <= afterCommitFileCount; i++ {
  74. doSomeWritesDeletes(i+beforeCommitFileCount, v, t, infos)
  75. }
  76. v.CommitCompact()
  77. v.Close()
  78. v, err = NewVolume(dir, dir, "", 1, NeedleMapInMemory, nil, nil, 0, 0)
  79. if err != nil {
  80. t.Fatalf("volume reloading: %v", err)
  81. }
  82. for i := 1; i <= beforeCommitFileCount+afterCommitFileCount; i++ {
  83. if infos[i-1] == nil {
  84. t.Fatal("not found file", i)
  85. continue
  86. }
  87. if infos[i-1].size == 0 {
  88. continue
  89. }
  90. n := newEmptyNeedle(uint64(i))
  91. size, err := v.readNeedle(n, nil, nil)
  92. if err != nil {
  93. t.Fatalf("read file %d: %v", i, err)
  94. }
  95. if infos[i-1].size != types.Size(size) {
  96. t.Fatalf("read file %d size mismatch expected %d found %d", i, infos[i-1].size, size)
  97. }
  98. if infos[i-1].crc != n.Checksum {
  99. t.Fatalf("read file %d checksum mismatch expected %d found %d", i, infos[i-1].crc, n.Checksum)
  100. }
  101. }
  102. }
  103. func doSomeWritesDeletes(i int, v *Volume, t *testing.T, infos []*needleInfo) {
  104. n := newRandomNeedle(uint64(i))
  105. _, size, _, err := v.writeNeedle2(n, true, false)
  106. if err != nil {
  107. t.Fatalf("write file %d: %v", i, err)
  108. }
  109. infos[i-1] = &needleInfo{
  110. size: size,
  111. crc: n.Checksum,
  112. }
  113. // println("written file", i, "checksum", n.Checksum.Value(), "size", size)
  114. if rand.Float64() < 0.03 {
  115. toBeDeleted := rand.Intn(i) + 1
  116. oldNeedle := newEmptyNeedle(uint64(toBeDeleted))
  117. v.deleteNeedle2(oldNeedle)
  118. // println("deleted file", toBeDeleted)
  119. infos[toBeDeleted-1] = &needleInfo{
  120. size: 0,
  121. crc: n.Checksum,
  122. }
  123. }
  124. }
  125. type needleInfo struct {
  126. size types.Size
  127. crc needle.CRC
  128. }
  129. func newRandomNeedle(id uint64) *needle.Needle {
  130. n := new(needle.Needle)
  131. n.Data = make([]byte, rand.Intn(1024))
  132. rand.Read(n.Data)
  133. n.Checksum = needle.NewCRC(n.Data)
  134. n.Id = types.Uint64ToNeedleId(id)
  135. return n
  136. }
  137. func newEmptyNeedle(id uint64) *needle.Needle {
  138. n := new(needle.Needle)
  139. n.Id = types.Uint64ToNeedleId(id)
  140. return n
  141. }