From 0eeb50b55372bf3a5be9fb2ef543c3265620af54 Mon Sep 17 00:00:00 2001 From: Pierce Lopez Date: Thu, 19 Jul 2018 02:06:36 -0400 Subject: [PATCH] nsqd: remove support for old metadata filename --- nsqd/nsqd.go | 54 +------------------------ nsqd/nsqd_test.go | 100 ---------------------------------------------- 2 files changed, 1 insertion(+), 153 deletions(-) diff --git a/nsqd/nsqd.go b/nsqd/nsqd.go index 3c5437299..8d3196b82 100644 --- a/nsqd/nsqd.go +++ b/nsqd/nsqd.go @@ -1,7 +1,6 @@ package nsqd import ( - "bytes" "crypto/tls" "crypto/x509" "encoding/json" @@ -13,7 +12,6 @@ import ( "net" "os" "path" - "runtime" "strings" "sync" "sync/atomic" @@ -272,10 +270,6 @@ func newMetadataFile(opts *Options) string { return path.Join(opts.DataPath, "nsqd.dat") } -func oldMetadataFile(opts *Options) string { - return path.Join(opts.DataPath, fmt.Sprintf("nsqd.%d.dat", opts.ID)) -} - func readOrEmpty(fn string) ([]byte, error) { data, err := ioutil.ReadFile(fn) if err != nil { @@ -305,30 +299,13 @@ func (n *NSQD) LoadMetadata() error { defer atomic.StoreInt32(&n.isLoading, 0) fn := newMetadataFile(n.getOpts()) - // old metadata filename with ID, maintained in parallel to enable roll-back - fnID := oldMetadataFile(n.getOpts()) data, err := readOrEmpty(fn) if err != nil { return err } - dataID, errID := readOrEmpty(fnID) - if errID != nil { - return errID - } - - if data == nil && dataID == nil { - return nil // fresh start - } - if data != nil && dataID != nil { - if bytes.Compare(data, dataID) != 0 { - return fmt.Errorf("metadata in %s and %s do not match (delete one)", fn, fnID) - } - } if data == nil { - // only old metadata file exists, use it - fn = fnID - data = dataID + return nil // fresh start } var m meta @@ -364,8 +341,6 @@ func (n *NSQD) LoadMetadata() error { func (n *NSQD) PersistMetadata() error { // persist metadata about what topics/channels we have, across restarts fileName := newMetadataFile(n.getOpts()) - // old metadata filename with ID, maintained in parallel to enable roll-back - fileNameID := oldMetadataFile(n.getOpts()) n.logf(LOG_INFO, "NSQ: persisting topic/channel metadata to %s", fileName) @@ -416,33 +391,6 @@ func (n *NSQD) PersistMetadata() error { } // technically should fsync DataPath here - stat, err := os.Lstat(fileNameID) - if err == nil && stat.Mode()&os.ModeSymlink != 0 { - return nil - } - - // if no symlink (yet), race condition: - // crash right here may cause next startup to see metadata conflict and abort - - tmpFileNameID := fmt.Sprintf("%s.%d.tmp", fileNameID, rand.Int()) - - if runtime.GOOS != "windows" { - err = os.Symlink(fileName, tmpFileNameID) - } else { - // on Windows need Administrator privs to Symlink - // instead write copy every time - err = writeSyncFile(tmpFileNameID, data) - } - if err != nil { - return err - } - - err = os.Rename(tmpFileNameID, fileNameID) - if err != nil { - return err - } - // technically should fsync DataPath here - return nil } diff --git a/nsqd/nsqd_test.go b/nsqd/nsqd_test.go index 150a531b8..f1f0c2117 100644 --- a/nsqd/nsqd_test.go +++ b/nsqd/nsqd_test.go @@ -158,106 +158,6 @@ func TestStartup(t *testing.T) { <-doneExitChan } -func TestMetadataMigrate(t *testing.T) { - old_meta := ` - { - "topics": [ - { - "channels": [ - {"name": "c1", "paused": false}, - {"name": "c2", "paused": false} - ], - "name": "t1", - "paused": false - } - ], - "version": "1.0.0-alpha" - }` - - tmpDir, err := ioutil.TempDir("", "nsq-test-") - if err != nil { - panic(err) - } - defer os.RemoveAll(tmpDir) - - opts := NewOptions() - opts.DataPath = tmpDir - opts.Logger = test.NewTestLogger(t) - - oldFn := oldMetadataFile(opts) - err = ioutil.WriteFile(oldFn, []byte(old_meta), 0600) - if err != nil { - panic(err) - } - - _, _, nsqd := mustStartNSQD(opts) - err = nsqd.LoadMetadata() - test.Nil(t, err) - err = nsqd.PersistMetadata() - test.Nil(t, err) - nsqd.Exit() - - oldFi, err := os.Lstat(oldFn) - test.Nil(t, err) - test.Equal(t, oldFi.Mode()&os.ModeType, os.ModeSymlink) - - _, _, nsqd = mustStartNSQD(opts) - err = nsqd.LoadMetadata() - test.Nil(t, err) - - t1, err := nsqd.GetExistingTopic("t1") - test.Nil(t, err) - test.NotNil(t, t1) - c2, err := t1.GetExistingChannel("c2") - test.Nil(t, err) - test.NotNil(t, c2) - - nsqd.Exit() -} - -func TestMetadataConflict(t *testing.T) { - old_meta := ` - { - "topics": [{ - "name": "t1", "paused": false, - "channels": [{"name": "c1", "paused": false}] - }], - "version": "1.0.0-alpha" - }` - new_meta := ` - { - "topics": [{ - "name": "t2", "paused": false, - "channels": [{"name": "c2", "paused": false}] - }], - "version": "1.0.0-alpha" - }` - - tmpDir, err := ioutil.TempDir("", "nsq-test-") - if err != nil { - panic(err) - } - defer os.RemoveAll(tmpDir) - - opts := NewOptions() - opts.DataPath = tmpDir - opts.Logger = test.NewTestLogger(t) - - err = ioutil.WriteFile(oldMetadataFile(opts), []byte(old_meta), 0600) - if err != nil { - panic(err) - } - err = ioutil.WriteFile(newMetadataFile(opts), []byte(new_meta), 0600) - if err != nil { - panic(err) - } - - _, _, nsqd := mustStartNSQD(opts) - err = nsqd.LoadMetadata() - test.NotNil(t, err) - nsqd.Exit() -} - func TestEphemeralTopicsAndChannels(t *testing.T) { // ephemeral topics/channels are lazily removed after the last channel/client is removed opts := NewOptions()