volume_vacuum_test.go 5.4 KB

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