Skip to content

Commit

Permalink
Check for zero-length meta.inf and meta.key and recreate
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
neilalexander committed Jul 31, 2024
1 parent 548544b commit a2abac1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
7 changes: 4 additions & 3 deletions server/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,8 @@ func newFileStoreWithCreated(fcfg FileStoreConfig, cfg StreamConfig, created tim
// If we expect to be encrypted check that what we are restoring is not plaintext.
// This can happen on snapshot restores or conversions.
if fs.prf != nil {
if _, err := os.Stat(keyFile); err != nil && os.IsNotExist(err) {
fi, err := os.Stat(keyFile)
if err != nil && os.IsNotExist(err) || fi != nil && fi.Size() == 0 {
if err := fs.writeStreamMeta(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -8668,7 +8669,7 @@ func (fs *fileStore) ConsumerStore(name string, cfg *ConsumerConfig) (ConsumerSt

// Write our meta data iff does not exist.
meta := filepath.Join(odir, JetStreamMetaFile)
if _, err := os.Stat(meta); err != nil && os.IsNotExist(err) {
if fi, err := os.Stat(meta); err != nil && os.IsNotExist(err) || err == nil && fi.Size() == 0 {
didCreate = true
csi.Created = time.Now().UTC()
if err := o.writeConsumerMeta(); err != nil {
Expand All @@ -8681,7 +8682,7 @@ func (fs *fileStore) ConsumerStore(name string, cfg *ConsumerConfig) (ConsumerSt
// This can happen on snapshot restores or conversions.
if o.prf != nil {
keyFile := filepath.Join(odir, JetStreamMetaFileKey)
if _, err := os.Stat(keyFile); err != nil && os.IsNotExist(err) {
if fi, err := os.Stat(keyFile); err != nil && os.IsNotExist(err) || err == nil && fi.Size() == 0 {
if err := o.writeConsumerMeta(); err != nil {
if didCreate {
os.RemoveAll(odir)
Expand Down
70 changes: 70 additions & 0 deletions server/filestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,76 @@ func TestFileStoreMeta(t *testing.T) {
}
}

func TestFileStoreMetaZeroLengthRepopulated(t *testing.T) {
fcfg := FileStoreConfig{StoreDir: t.TempDir()}
mconfig := StreamConfig{Name: "ZZ-22-33", Storage: FileStorage, Subjects: []string{"foo.*"}, Replicas: 22}
fs, err := newFileStore(fcfg, mconfig)
require_NoError(t, err)
defer fs.Stop()

metafile := filepath.Join(fcfg.StoreDir, JetStreamMetaFile)
metasum := filepath.Join(fcfg.StoreDir, JetStreamMetaFileSum)

fi, err := os.Stat(metafile)
require_NoError(t, err)
require_NotEqual(t, fi.Size(), 0)

fi, err = os.Stat(metasum)
require_NoError(t, err)
require_NotEqual(t, fi.Size(), 0)

require_NoError(t, fs.Stop())
require_NoError(t, os.Truncate(metafile, 0))
require_NoError(t, os.Truncate(metasum, 0))

fs, err = newFileStore(fcfg, mconfig)
require_NoError(t, err)

fi, err = os.Stat(metafile)
require_NoError(t, err)
require_NotEqual(t, fi.Size(), 0)

fi, err = os.Stat(metasum)
require_NoError(t, err)
require_NotEqual(t, fi.Size(), 0)

oconfig := ConsumerConfig{
DeliverSubject: "d",
FilterSubject: "foo",
AckPolicy: AckAll,
}
oname := "obs22"

obs, err := fs.ConsumerStore(oname, &oconfig)
require_NoError(t, err)

ometafile := filepath.Join(fcfg.StoreDir, consumerDir, oname, JetStreamMetaFile)
ometasum := filepath.Join(fcfg.StoreDir, consumerDir, oname, JetStreamMetaFileSum)

fi, err = os.Stat(ometafile)
require_NoError(t, err)
require_NotEqual(t, fi.Size(), 0)

fi, err = os.Stat(ometasum)
require_NoError(t, err)
require_NotEqual(t, fi.Size(), 0)

require_NoError(t, obs.Stop())
require_NoError(t, os.Truncate(ometafile, 0))
require_NoError(t, os.Truncate(ometasum, 0))

obs, err = fs.ConsumerStore(oname, &oconfig)
require_NoError(t, err)

fi, err = os.Stat(ometafile)
require_NoError(t, err)
require_NotEqual(t, fi.Size(), 0)

fi, err = os.Stat(ometasum)
require_NoError(t, err)
require_NotEqual(t, fi.Size(), 0)
}

func TestFileStoreWriteAndReadSameBlock(t *testing.T) {
testFileStoreAllPermutations(t, func(t *testing.T, fcfg FileStoreConfig) {
fs, err := newFileStoreWithCreated(fcfg, StreamConfig{Name: "zzz", Storage: FileStorage}, time.Now(), prf(&fcfg), nil)
Expand Down

0 comments on commit a2abac1

Please sign in to comment.