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.

165 lines
4.3 KiB

6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  1. package storage
  2. import (
  3. "math/rand"
  4. "testing"
  5. "time"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  8. "github.com/seaweedfs/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. }
  86. if infos[i-1].size == 0 {
  87. continue
  88. }
  89. n := newEmptyNeedle(uint64(i))
  90. size, err := v.readNeedle(n, nil, nil)
  91. if err != nil {
  92. t.Fatalf("read file %d: %v", i, err)
  93. }
  94. if infos[i-1].size != types.Size(size) {
  95. t.Fatalf("read file %d size mismatch expected %d found %d", i, infos[i-1].size, size)
  96. }
  97. if infos[i-1].crc != n.Checksum {
  98. t.Fatalf("read file %d checksum mismatch expected %d found %d", i, infos[i-1].crc, n.Checksum)
  99. }
  100. }
  101. }
  102. func doSomeWritesDeletes(i int, v *Volume, t *testing.T, infos []*needleInfo) {
  103. n := newRandomNeedle(uint64(i))
  104. _, size, _, err := v.writeNeedle2(n, true, false)
  105. if err != nil {
  106. t.Fatalf("write file %d: %v", i, err)
  107. }
  108. infos[i-1] = &needleInfo{
  109. size: size,
  110. crc: n.Checksum,
  111. }
  112. // println("written file", i, "checksum", n.Checksum.Value(), "size", size)
  113. if rand.Float64() < 0.03 {
  114. toBeDeleted := rand.Intn(i) + 1
  115. oldNeedle := newEmptyNeedle(uint64(toBeDeleted))
  116. v.deleteNeedle2(oldNeedle)
  117. // println("deleted file", toBeDeleted)
  118. infos[toBeDeleted-1] = &needleInfo{
  119. size: 0,
  120. crc: n.Checksum,
  121. }
  122. }
  123. }
  124. type needleInfo struct {
  125. size types.Size
  126. crc needle.CRC
  127. }
  128. func newRandomNeedle(id uint64) *needle.Needle {
  129. n := new(needle.Needle)
  130. n.Data = make([]byte, rand.Intn(1024))
  131. rand.Read(n.Data)
  132. n.Checksum = needle.NewCRC(n.Data)
  133. n.Id = types.Uint64ToNeedleId(id)
  134. return n
  135. }
  136. func newEmptyNeedle(id uint64) *needle.Needle {
  137. n := new(needle.Needle)
  138. n.Id = types.Uint64ToNeedleId(id)
  139. return n
  140. }