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.1 KiB

7 years ago
8 years ago
6 years ago
7 years ago
7 years ago
  1. package storage
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/storage/types"
  4. "io/ioutil"
  5. "math/rand"
  6. "os"
  7. "testing"
  8. )
  9. /*
  10. makediff test steps
  11. 1. launch weed server at your local/dev environment, (option
  12. "garbageThreshold" for master and option "max" for volume should be set with specific value which would let
  13. preparing test prerequisite easier )
  14. a) ./weed master -garbageThreshold=0.99 -mdir=./m
  15. b) ./weed volume -dir=./data -max=1 -mserver=localhost:9333 -port=8080
  16. 2. upload 4 different files, you could call dir/assign to get 4 different fids
  17. a) upload file A with fid a
  18. b) upload file B with fid b
  19. c) upload file C with fid c
  20. d) upload file D with fid d
  21. 3. update file A and C
  22. a) modify file A and upload file A with fid a
  23. b) modify file C and upload file C with fid c
  24. c) record the current 1.idx's file size(lastCompactIndexOffset value)
  25. 4. Compacting the data file
  26. a) run curl http://localhost:8080/admin/vacuum/compact?volumeId=1
  27. b) verify the 1.cpd and 1.cpx is created under volume directory
  28. 5. update file B and delete file D
  29. a) modify file B and upload file B with fid b
  30. d) delete file B with fid b
  31. 6. Now you could run the following UT case, the case should be run successfully
  32. 7. Compact commit manually
  33. a) mv 1.cpd 1.dat
  34. b) mv 1.cpx 1.idx
  35. 8. Restart Volume Server
  36. 9. Now you should get updated file A,B,C
  37. */
  38. func TestMakeDiff(t *testing.T) {
  39. v := new(Volume)
  40. //lastCompactIndexOffset value is the index file size before step 4
  41. v.lastCompactIndexOffset = 96
  42. v.SuperBlock.version = 0x2
  43. /*
  44. err := v.makeupDiff(
  45. "/yourpath/1.cpd",
  46. "/yourpath/1.cpx",
  47. "/yourpath/1.dat",
  48. "/yourpath/1.idx")
  49. if err != nil {
  50. t.Errorf("makeupDiff err is %v", err)
  51. } else {
  52. t.Log("makeupDiff Succeeded")
  53. }
  54. */
  55. }
  56. func TestCompaction(t *testing.T) {
  57. dir, err := ioutil.TempDir("", "example")
  58. if err != nil {
  59. t.Fatalf("temp dir creation: %v", err)
  60. }
  61. defer os.RemoveAll(dir) // clean up
  62. v, err := NewVolume(dir, "", 1, NeedleMapInMemory, &ReplicaPlacement{}, &TTL{}, 0)
  63. if err != nil {
  64. t.Fatalf("volume creation: %v", err)
  65. }
  66. beforeCommitFileCount := 1000
  67. afterCommitFileCount := 1000
  68. infos := make([]*needleInfo, beforeCommitFileCount+afterCommitFileCount)
  69. for i := 1; i <= beforeCommitFileCount; i++ {
  70. doSomeWritesDeletes(i, v, t, infos)
  71. }
  72. v.Compact(0)
  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, "", 1, NeedleMapInMemory, nil, nil, 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)
  92. if err != nil {
  93. t.Fatalf("read file %d: %v", i, err)
  94. }
  95. if infos[i-1].size != uint32(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.writeNeedle(n)
  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.deleteNeedle(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 uint32
  127. crc CRC
  128. }
  129. func newRandomNeedle(id uint64) *Needle {
  130. n := new(Needle)
  131. n.Data = make([]byte, rand.Intn(1024))
  132. rand.Read(n.Data)
  133. n.Checksum = NewCRC(n.Data)
  134. n.Id = types.Uint64ToNeedleId(id)
  135. return n
  136. }
  137. func newEmptyNeedle(id uint64) *Needle {
  138. n := new(Needle)
  139. n.Id = types.Uint64ToNeedleId(id)
  140. return n
  141. }