freezer: enforce readonly in table repair
This commit is contained in:
parent
63c9ce9620
commit
169e10092c
|
@ -178,7 +178,14 @@ func newTable(path string, name string, readMeter metrics.Meter, writeMeter metr
|
|||
// Compressed idx
|
||||
idxName = fmt.Sprintf("%s.cidx", name)
|
||||
}
|
||||
offsets, err := openFreezerFileForAppend(filepath.Join(path, idxName))
|
||||
var err error
|
||||
var offsets *os.File
|
||||
if readonly {
|
||||
// Will fail if table doesn't exist
|
||||
offsets, err = openFreezerFileForReadOnly(filepath.Join(path, idxName))
|
||||
} else {
|
||||
offsets, err = openFreezerFileForAppend(filepath.Join(path, idxName))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -229,6 +236,9 @@ func (t *freezerTable) repair() error {
|
|||
}
|
||||
// Ensure the index is a multiple of indexEntrySize bytes
|
||||
if overflow := stat.Size() % indexEntrySize; overflow != 0 {
|
||||
if t.readonly {
|
||||
return fmt.Errorf("table index has invalid length")
|
||||
}
|
||||
truncateFreezerFile(t.index, stat.Size()-overflow) // New file can't trigger this path
|
||||
}
|
||||
// Retrieve the file sizes and prepare for truncation
|
||||
|
@ -254,7 +264,11 @@ func (t *freezerTable) repair() error {
|
|||
|
||||
t.index.ReadAt(buffer, offsetsSize-indexEntrySize)
|
||||
lastIndex.unmarshalBinary(buffer)
|
||||
if t.readonly {
|
||||
t.head, err = t.openFile(lastIndex.filenum, openFreezerFileForReadOnly)
|
||||
} else {
|
||||
t.head, err = t.openFile(lastIndex.filenum, openFreezerFileForAppend)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -267,6 +281,9 @@ func (t *freezerTable) repair() error {
|
|||
contentExp = int64(lastIndex.offset)
|
||||
|
||||
for contentExp != contentSize {
|
||||
if t.readonly {
|
||||
return fmt.Errorf("head file has unexpected size. %d != %d", contentSize, contentExp)
|
||||
}
|
||||
// Truncate the head file to the last offset pointer
|
||||
if contentExp < contentSize {
|
||||
t.logger.Warn("Truncating dangling head", "indexed", common.StorageSize(contentExp), "stored", common.StorageSize(contentSize))
|
||||
|
@ -304,12 +321,14 @@ func (t *freezerTable) repair() error {
|
|||
}
|
||||
}
|
||||
// Ensure all reparation changes have been written to disk
|
||||
if !t.readonly {
|
||||
if err := t.index.Sync(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := t.head.Sync(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Update the item and byte counters and return
|
||||
t.items = uint64(t.itemOffset) + uint64(offsetsSize/indexEntrySize-1) // last indexEntry points to the end of the data file
|
||||
t.headBytes = contentSize
|
||||
|
@ -336,8 +355,12 @@ func (t *freezerTable) preopen() (err error) {
|
|||
return err
|
||||
}
|
||||
}
|
||||
if t.readonly {
|
||||
t.head, err = t.openFile(t.headId, openFreezerFileForReadOnly)
|
||||
} else {
|
||||
// Open head in read/write
|
||||
t.head, err = t.openFile(t.headId, openFreezerFileForAppend)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -829,3 +829,72 @@ func TestSequentialReadByteLimit(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreezerReadonlyBasics(t *testing.T) {
|
||||
// Case 1: Check it fails on non-existent file.
|
||||
_, err := newTable(os.TempDir(),
|
||||
fmt.Sprintf("readonlytest-%d", rand.Uint64()),
|
||||
metrics.NewMeter(), metrics.NewMeter(), metrics.NewGauge(), 50, true, true)
|
||||
if err == nil {
|
||||
t.Fatal("readonly table instantiation should fail for non-existent table")
|
||||
}
|
||||
|
||||
// Case 2: Check that it fails on invalid index length.
|
||||
tmpdir := os.TempDir()
|
||||
fname := fmt.Sprintf("readonlytest-%d", rand.Uint64())
|
||||
idxFile, err := openFreezerFileForAppend(filepath.Join(tmpdir, fmt.Sprintf("%s.ridx", fname)))
|
||||
if err != nil {
|
||||
t.Errorf("Failed to open index file: %v\n", err)
|
||||
}
|
||||
// size should not be a multiple of indexEntrySize.
|
||||
idxFile.Write(make([]byte, 17))
|
||||
idxFile.Close()
|
||||
_, err = newTable(tmpdir, fname,
|
||||
metrics.NewMeter(), metrics.NewMeter(), metrics.NewGauge(), 50, true, true)
|
||||
if err == nil {
|
||||
t.Errorf("readonly table instantiation should fail for invalid index size")
|
||||
}
|
||||
|
||||
// Case 3: Open table non-readonly table to write some data.
|
||||
// Then corrupt the head file and make sure opening the table
|
||||
// again in readonly triggers an error.
|
||||
fname = fmt.Sprintf("readonlytest-%d", rand.Uint64())
|
||||
f, err := newTable(os.TempDir(), fname,
|
||||
metrics.NewMeter(), metrics.NewMeter(), metrics.NewGauge(), 50, true, false)
|
||||
writeChunks(t, f, 8, 32)
|
||||
// Corrupt table file
|
||||
if _, err := f.head.Write([]byte{1, 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = newTable(os.TempDir(), fname,
|
||||
metrics.NewMeter(), metrics.NewMeter(), metrics.NewGauge(), 50, true, true)
|
||||
if err == nil {
|
||||
t.Errorf("readonly table instantiation should fail for corrupt table file")
|
||||
}
|
||||
|
||||
// Case 4: Write some data to a table and later re-open it as readonly.
|
||||
// Should be successful.
|
||||
fname = fmt.Sprintf("readonlytest-%d", rand.Uint64())
|
||||
f, err = newTable(os.TempDir(), fname,
|
||||
metrics.NewMeter(), metrics.NewMeter(), metrics.NewGauge(), 50, true, false)
|
||||
writeChunks(t, f, 32, 128)
|
||||
if err := f.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err = newTable(os.TempDir(), fname,
|
||||
metrics.NewMeter(), metrics.NewMeter(), metrics.NewGauge(), 50, true, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
v, err := f.Retrieve(10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
exp := getChunk(128, 10)
|
||||
if !bytes.Equal(v, exp) {
|
||||
t.Errorf("retrieved value is incorrect")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue