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.

171 lines
4.4 KiB

6 years ago
8 years ago
6 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. "io/ioutil"
  4. "math/rand"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/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 TestCompaction(t *testing.T) {
  59. dir, err := ioutil.TempDir("", "example")
  60. if err != nil {
  61. t.Fatalf("temp dir creation: %v", err)
  62. }
  63. defer os.RemoveAll(dir) // clean up
  64. v, err := NewVolume(dir, "", 1, NeedleMapInMemory, &ReplicaPlacement{}, &needle.TTL{}, 0)
  65. if err != nil {
  66. t.Fatalf("volume creation: %v", err)
  67. }
  68. beforeCommitFileCount := 10000
  69. afterCommitFileCount := 10000
  70. infos := make([]*needleInfo, beforeCommitFileCount+afterCommitFileCount)
  71. for i := 1; i <= beforeCommitFileCount; i++ {
  72. doSomeWritesDeletes(i, v, t, infos)
  73. }
  74. startTime := time.Now()
  75. v.Compact(0, 1024*1024)
  76. speed := float64(v.ContentSize()) / time.Now().Sub(startTime).Seconds()
  77. t.Logf("compaction speed: %.2f bytes/s", speed)
  78. for i := 1; i <= afterCommitFileCount; i++ {
  79. doSomeWritesDeletes(i+beforeCommitFileCount, v, t, infos)
  80. }
  81. v.CommitCompact()
  82. v.Close()
  83. v, err = NewVolume(dir, "", 1, NeedleMapInMemory, nil, nil, 0)
  84. if err != nil {
  85. t.Fatalf("volume reloading: %v", err)
  86. }
  87. for i := 1; i <= beforeCommitFileCount+afterCommitFileCount; i++ {
  88. if infos[i-1] == nil {
  89. t.Fatal("not found file", i)
  90. continue
  91. }
  92. if infos[i-1].size == 0 {
  93. continue
  94. }
  95. n := newEmptyNeedle(uint64(i))
  96. size, err := v.readNeedle(n)
  97. if err != nil {
  98. t.Fatalf("read file %d: %v", i, err)
  99. }
  100. if infos[i-1].size != uint32(size) {
  101. t.Fatalf("read file %d size mismatch expected %d found %d", i, infos[i-1].size, size)
  102. }
  103. if infos[i-1].crc != n.Checksum {
  104. t.Fatalf("read file %d checksum mismatch expected %d found %d", i, infos[i-1].crc, n.Checksum)
  105. }
  106. }
  107. }
  108. func doSomeWritesDeletes(i int, v *Volume, t *testing.T, infos []*needleInfo) {
  109. n := newRandomNeedle(uint64(i))
  110. _, size, _, err := v.writeNeedle(n)
  111. if err != nil {
  112. t.Fatalf("write file %d: %v", i, err)
  113. }
  114. infos[i-1] = &needleInfo{
  115. size: size,
  116. crc: n.Checksum,
  117. }
  118. // println("written file", i, "checksum", n.Checksum.Value(), "size", size)
  119. if rand.Float64() < 0.03 {
  120. toBeDeleted := rand.Intn(i) + 1
  121. oldNeedle := newEmptyNeedle(uint64(toBeDeleted))
  122. v.deleteNeedle(oldNeedle)
  123. // println("deleted file", toBeDeleted)
  124. infos[toBeDeleted-1] = &needleInfo{
  125. size: 0,
  126. crc: n.Checksum,
  127. }
  128. }
  129. }
  130. type needleInfo struct {
  131. size uint32
  132. crc needle.CRC
  133. }
  134. func newRandomNeedle(id uint64) *needle.Needle {
  135. n := new(needle.Needle)
  136. n.Data = make([]byte, rand.Intn(1024))
  137. rand.Read(n.Data)
  138. n.Checksum = needle.NewCRC(n.Data)
  139. n.Id = types.Uint64ToNeedleId(id)
  140. return n
  141. }
  142. func newEmptyNeedle(id uint64) *needle.Needle {
  143. n := new(needle.Needle)
  144. n.Id = types.Uint64ToNeedleId(id)
  145. return n
  146. }