From 806437bd23a63183d345b7226477f7ddbd364a09 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Wed, 2 May 2018 17:06:58 +0300 Subject: [PATCH 1/4] Make sure ~ is expanded in NewServer; BroadcastReceiver uses temp path --- broadcast_test.go | 7 ++++++- server.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/broadcast_test.go b/broadcast_test.go index 5654f7c28..970a249cb 100644 --- a/broadcast_test.go +++ b/broadcast_test.go @@ -56,9 +56,14 @@ func testMessageMarshal(t *testing.T, m proto.Message) { // Ensure that BroadcastReceiver can register a BroadcastHandler. func TestBroadcast_BroadcastReceiver(t *testing.T) { + path, err := ioutil.TempDir("", "pilosa-") + if err != nil { + panic(err) + } com := server.NewCommand(bytes.NewBuffer([]byte{}), ioutil.Discard, ioutil.Discard) com.Config.Bind = "localhost:0" - err := com.SetupServer() // this test shouldn't need to import pilosa/server just to set up the Server, but it really shouldn't need to setup the Server at all. The Server should not be the implementation of Broadcast* TODO + com.Config.DataDir = path + err = com.SetupServer() // this test shouldn't need to import pilosa/server just to set up the Server, but it really shouldn't need to setup the Server at all. The Server should not be the implementation of Broadcast* TODO if err != nil { t.Fatalf("setting up server: %v", err) } diff --git a/server.go b/server.go index 06260ae5d..46f8a0dda 100644 --- a/server.go +++ b/server.go @@ -21,6 +21,7 @@ import ( "net/http" "os" "os/exec" + "path/filepath" "runtime" "strconv" "strings" @@ -230,6 +231,11 @@ func NewServer(opts ...ServerOption) (*Server, error) { } } + err := s.expandDataDirName() + if err != nil { + return nil, err + } + s.Holder.Logger = s.logger s.Holder.Stats.SetLogger(s.logger) @@ -260,6 +266,18 @@ func NewServer(opts ...ServerOption) (*Server, error) { return s, nil } +func (s *Server) expandDataDirName() error { + prefix := "~" + string(filepath.Separator) + if strings.HasPrefix(s.Holder.Path, prefix) { + HomeDir := os.Getenv("HOME") + if HomeDir == "" { + return errors.New("data directory not specified and no home dir available") + } + s.Holder.Path = filepath.Join(HomeDir, strings.TrimPrefix(s.Holder.Path, prefix)) + } + return nil +} + // Open opens and initializes the server. func (s *Server) Open() error { s.logger.Printf("open server") From 244c4e894e875ee048c56fc04c7d525a81b0e72a Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Wed, 2 May 2018 17:33:51 +0300 Subject: [PATCH 2/4] Remove ~ expanding code from Command.Start --- server/server.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/server/server.go b/server/server.go index 8fd699ce5..3098cddeb 100644 --- a/server/server.go +++ b/server/server.go @@ -28,9 +28,7 @@ import ( "net/http" "os" "os/signal" - "path/filepath" "strconv" - "strings" "syscall" "time" @@ -93,14 +91,6 @@ func NewCommand(stdin io.Reader, stdout, stderr io.Writer) *Command { // Start starts the pilosa server - it returns once the server is running. func (m *Command) Start() (err error) { defer close(m.Started) - prefix := "~" + string(filepath.Separator) - if strings.HasPrefix(m.Config.DataDir, prefix) { - HomeDir := os.Getenv("HOME") - if HomeDir == "" { - return errors.New("data directory not specified and no home dir available") - } - m.Config.DataDir = filepath.Join(HomeDir, strings.TrimPrefix(m.Config.DataDir, prefix)) - } // SetupServer err = m.SetupServer() From beaa69a7bd6eaa38e3eb0e6ed4076eb8768fb165 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Thu, 3 May 2018 00:07:56 +0300 Subject: [PATCH 3/4] Made expandDirName generic. --- server.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/server.go b/server.go index 46f8a0dda..86f14c8b3 100644 --- a/server.go +++ b/server.go @@ -231,14 +231,21 @@ func NewServer(opts ...ServerOption) (*Server, error) { } } - err := s.expandDataDirName() + path, err := expandDirName(s.Holder.Path) if err != nil { return nil, err } + s.Holder.Path = path s.Holder.Logger = s.logger s.Holder.Stats.SetLogger(s.logger) + path, err = expandDirName(s.Cluster.Path) + if err != nil { + return nil, err + } + s.Cluster.Path = path + s.Cluster.Logger = s.logger s.Cluster.Holder = s.Holder @@ -266,18 +273,6 @@ func NewServer(opts ...ServerOption) (*Server, error) { return s, nil } -func (s *Server) expandDataDirName() error { - prefix := "~" + string(filepath.Separator) - if strings.HasPrefix(s.Holder.Path, prefix) { - HomeDir := os.Getenv("HOME") - if HomeDir == "" { - return errors.New("data directory not specified and no home dir available") - } - s.Holder.Path = filepath.Join(HomeDir, strings.TrimPrefix(s.Holder.Path, prefix)) - } - return nil -} - // Open opens and initializes the server. func (s *Server) Open() error { s.logger.Printf("open server") @@ -796,3 +791,15 @@ type StatusHandler interface { ClusterStatus() (proto.Message, error) HandleRemoteStatus(proto.Message) error } + +func expandDirName(path string) (string, error) { + prefix := "~" + string(filepath.Separator) + if strings.HasPrefix(path, prefix) { + HomeDir := os.Getenv("HOME") + if HomeDir == "" { + return "", errors.New("data directory not specified and no home dir available") + } + return filepath.Join(HomeDir, strings.TrimPrefix(path, prefix)), nil + } + return path, nil +} From 0244f4b51d1a507f790ee90eabd66b9183201bb4 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Wed, 9 May 2018 01:27:42 +0300 Subject: [PATCH 4/4] Added dataDir field to Server --- server.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/server.go b/server.go index 86f14c8b3..a74ae90f3 100644 --- a/server.go +++ b/server.go @@ -77,6 +77,7 @@ type Server struct { maxWritesPerRequest int defaultClient InternalClient + dataDir string } // ServerOption is a functional option type for pilosa.Server @@ -98,8 +99,7 @@ func OptServerReplicaN(n int) ServerOption { func OptServerDataDir(dir string) ServerOption { return func(s *Server) error { - s.Cluster.Path = dir - s.Holder.Path = dir + s.dataDir = dir return nil } } @@ -231,21 +231,16 @@ func NewServer(opts ...ServerOption) (*Server, error) { } } - path, err := expandDirName(s.Holder.Path) + path, err := expandDirName(s.dataDir) if err != nil { return nil, err } - s.Holder.Path = path + s.Holder.Path = path s.Holder.Logger = s.logger s.Holder.Stats.SetLogger(s.logger) - path, err = expandDirName(s.Cluster.Path) - if err != nil { - return nil, err - } s.Cluster.Path = path - s.Cluster.Logger = s.logger s.Cluster.Holder = s.Holder