volume_read_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package storage
  2. import (
  3. "testing"
  4. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  5. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestReadNeedMetaWithWritesAndUpdates(t *testing.T) {
  10. dir := t.TempDir()
  11. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0, 0)
  12. if err != nil {
  13. t.Fatalf("volume creation: %v", err)
  14. }
  15. type WriteInfo struct {
  16. offset int64
  17. size int32
  18. }
  19. writeInfos := make([]WriteInfo, 30)
  20. mockLastUpdateTime := uint64(1000000000000)
  21. // initialize 20 needles then update first 10 needles
  22. for i := 1; i <= 30; i++ {
  23. n := newRandomNeedle(uint64(i % 20))
  24. n.Flags = 0x08
  25. n.LastModified = mockLastUpdateTime
  26. mockLastUpdateTime += 2000
  27. offset, _, _, err := v.writeNeedle2(n, true, false)
  28. if err != nil {
  29. t.Fatalf("write needle %d: %v", i, err)
  30. }
  31. writeInfos[i-1] = WriteInfo{offset: int64(offset), size: int32(n.Size)}
  32. }
  33. expectedLastUpdateTime := uint64(1000000000000)
  34. for i := 0; i < 30; i++ {
  35. testNeedle := new(needle.Needle)
  36. testNeedle.Id = types.Uint64ToNeedleId(uint64(i + 1%20))
  37. testNeedle.Flags = 0x08
  38. v.readNeedleMetaAt(testNeedle, writeInfos[i].offset, writeInfos[i].size)
  39. actualLastModifiedTime := testNeedle.LastModified
  40. if writeInfos[i].size != 0 {
  41. assert.Equal(t, expectedLastUpdateTime, actualLastModifiedTime, "The two words should be the same.")
  42. }
  43. expectedLastUpdateTime += 2000
  44. }
  45. }
  46. func TestReadNeedMetaWithDeletesThenWrites(t *testing.T) {
  47. dir := t.TempDir()
  48. v, err := NewVolume(dir, dir, "", 1, NeedleMapInMemory, &super_block.ReplicaPlacement{}, &needle.TTL{}, 0, 0, 0)
  49. if err != nil {
  50. t.Fatalf("volume creation: %v", err)
  51. }
  52. type WriteInfo struct {
  53. offset int64
  54. size int32
  55. }
  56. writeInfos := make([]WriteInfo, 10)
  57. mockLastUpdateTime := uint64(1000000000000)
  58. for i := 1; i <= 10; i++ {
  59. n := newRandomNeedle(uint64(i % 5))
  60. n.Flags = 0x08
  61. n.LastModified = mockLastUpdateTime
  62. mockLastUpdateTime += 2000
  63. offset, _, _, err := v.writeNeedle2(n, true, false)
  64. if err != nil {
  65. t.Fatalf("write needle %d: %v", i, err)
  66. }
  67. if i < 5 {
  68. size, err := v.deleteNeedle2(n)
  69. if err != nil {
  70. t.Fatalf("delete needle %d: %v", i, err)
  71. }
  72. writeInfos[i-1] = WriteInfo{offset: int64(offset), size: int32(size)}
  73. } else {
  74. writeInfos[i-1] = WriteInfo{offset: int64(offset), size: int32(n.Size)}
  75. }
  76. }
  77. expectedLastUpdateTime := uint64(1000000000000)
  78. for i := 0; i < 10; i++ {
  79. testNeedle := new(needle.Needle)
  80. testNeedle.Id = types.Uint64ToNeedleId(uint64(i + 1%5))
  81. testNeedle.Flags = 0x08
  82. v.readNeedleMetaAt(testNeedle, writeInfos[i].offset, writeInfos[i].size)
  83. actualLastModifiedTime := testNeedle.LastModified
  84. if writeInfos[i].size != 0 {
  85. assert.Equal(t, expectedLastUpdateTime, actualLastModifiedTime, "The two words should be the same.")
  86. }
  87. expectedLastUpdateTime += 2000
  88. }
  89. }