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.

164 lines
4.0 KiB

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