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.

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