volume_write_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package storage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/stretchr/testify/assert"
  6. "os"
  7. "testing"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  12. )
  13. func TestSearchVolumesWithDeletedNeedles(t *testing.T) {
  14. dir := t.TempDir()
  15. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0, 0)
  16. if err != nil {
  17. t.Fatalf("volume creation: %v", err)
  18. }
  19. count := 20
  20. for i := 1; i < count; i++ {
  21. n := newRandomNeedle(uint64(i))
  22. _, _, _, err := v.writeNeedle2(n, true, false)
  23. if err != nil {
  24. t.Fatalf("write needle %d: %v", i, err)
  25. }
  26. }
  27. for i := 1; i < 15; i++ {
  28. n := newEmptyNeedle(uint64(i))
  29. err := v.nm.Put(n.Id, types.Offset{}, types.TombstoneFileSize)
  30. if err != nil {
  31. t.Fatalf("delete needle %d: %v", i, err)
  32. }
  33. }
  34. ts1 := time.Now().UnixNano()
  35. for i := 15; i < count; i++ {
  36. n := newEmptyNeedle(uint64(i))
  37. _, err := v.doDeleteRequest(n)
  38. if err != nil {
  39. t.Fatalf("delete needle %d: %v", i, err)
  40. }
  41. }
  42. offset, isLast, err := v.BinarySearchByAppendAtNs(uint64(ts1))
  43. if err != nil {
  44. t.Fatalf("lookup by ts: %v", err)
  45. }
  46. fmt.Printf("offset: %v, isLast: %v\n", offset.ToActualOffset(), isLast)
  47. }
  48. func isFileExist(path string) (bool, error) {
  49. if _, err := os.Stat(path); err == nil {
  50. return true, nil
  51. } else if errors.Is(err, os.ErrNotExist) {
  52. return false, nil
  53. } else {
  54. return false, err
  55. }
  56. }
  57. func assertFileExist(t *testing.T, expected bool, path string) {
  58. exist, err := isFileExist(path)
  59. if err != nil {
  60. t.Fatalf("isFileExist: %v", err)
  61. }
  62. assert.Equal(t, expected, exist)
  63. }
  64. func TestDestroyEmptyVolumeWithOnlyEmpty(t *testing.T) {
  65. dir := t.TempDir()
  66. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0, 0)
  67. if err != nil {
  68. t.Fatalf("volume creation: %v", err)
  69. }
  70. path := v.DataBackend.Name()
  71. // should can Destroy empty volume with onlyEmpty
  72. assertFileExist(t, true, path)
  73. err = v.Destroy(true)
  74. if err != nil {
  75. t.Fatalf("destroy volume: %v", err)
  76. }
  77. assertFileExist(t, false, path)
  78. }
  79. func TestDestroyEmptyVolumeWithoutOnlyEmpty(t *testing.T) {
  80. dir := t.TempDir()
  81. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0, 0)
  82. if err != nil {
  83. t.Fatalf("volume creation: %v", err)
  84. }
  85. path := v.DataBackend.Name()
  86. // should can Destroy empty volume without onlyEmpty
  87. assertFileExist(t, true, path)
  88. err = v.Destroy(false)
  89. if err != nil {
  90. t.Fatalf("destroy volume: %v", err)
  91. }
  92. assertFileExist(t, false, path)
  93. }
  94. func TestDestroyNonemptyVolumeWithOnlyEmpty(t *testing.T) {
  95. dir := t.TempDir()
  96. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0, 0)
  97. if err != nil {
  98. t.Fatalf("volume creation: %v", err)
  99. }
  100. path := v.DataBackend.Name()
  101. // should return "volume not empty" error and do not delete file when Destroy non-empty volume
  102. _, _, _, err = v.writeNeedle2(newRandomNeedle(1), true, false)
  103. if err != nil {
  104. t.Fatalf("write needle: %v", err)
  105. }
  106. assert.Equal(t, uint64(1), v.FileCount())
  107. assertFileExist(t, true, path)
  108. err = v.Destroy(true)
  109. assert.EqualError(t, err, "volume not empty")
  110. assertFileExist(t, true, path)
  111. // should keep working after "volume not empty"
  112. _, _, _, err = v.writeNeedle2(newRandomNeedle(2), true, false)
  113. if err != nil {
  114. t.Fatalf("write needle: %v", err)
  115. }
  116. assert.Equal(t, uint64(2), v.FileCount())
  117. }
  118. func TestDestroyNonemptyVolumeWithoutOnlyEmpty(t *testing.T) {
  119. dir := t.TempDir()
  120. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0, 0)
  121. if err != nil {
  122. t.Fatalf("volume creation: %v", err)
  123. }
  124. path := v.DataBackend.Name()
  125. // should can Destroy non-empty volume without onlyEmpty
  126. _, _, _, err = v.writeNeedle2(newRandomNeedle(1), true, false)
  127. if err != nil {
  128. t.Fatalf("write needle: %v", err)
  129. }
  130. assert.Equal(t, uint64(1), v.FileCount())
  131. assertFileExist(t, true, path)
  132. err = v.Destroy(false)
  133. if err != nil {
  134. t.Fatalf("destroy volume: %v", err)
  135. }
  136. assertFileExist(t, false, path)
  137. }