add "holder" command to start up and shut down

It would be neat to be able to observe performance of
"just open the holder". So let's make that a verb.
This commit is contained in:
Seebs 2019-07-23 17:03:55 -05:00
parent e028640ba2
commit 29a1db4550
4 changed files with 89 additions and 0 deletions

View file

@ -75,6 +75,7 @@ Build Time: ` + pilosa.BuildTime + "\n",
rc.AddCommand(newImportCommand(stdin, stdout, stderr))
rc.AddCommand(newInspectCommand(stdin, stdout, stderr))
rc.AddCommand(newServeCmd(stdin, stdout, stderr))
rc.AddCommand(newHolderCmd(stdin, stdout, stderr))
rc.SetOutput(stderr)
return rc

View file

@ -28,6 +28,33 @@ import (
// Server is global so that tests can control and verify it.
var Server *server.Command
var holder *server.Command
// newHolderCmd creates a pilosa server for just long enough to open the
// holder, then shuts it down again.
func newHolderCmd(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
holder = server.NewCommand(stdin, stdout, stderr)
serveCmd := &cobra.Command{
Use: "holder",
Short: "Load Pilosa.",
Long: `pilosa holder starts (and immediately stops) Pilosa.
It opens the data directory and loads it, then shuts down immediately.
This is only useful for diagnostic use.
`,
RunE: func(cmd *cobra.Command, args []string) error {
// Start & run the server.
if err := holder.UpAndDown(); err != nil {
return errors.Wrap(err, "running server")
}
return nil
},
}
// Attach flags to the command.
ctl.BuildServerFlags(serveCmd, holder)
return serveCmd
}
// newServeCmd creates a pilosa server and runs it with command line flags.
func newServeCmd(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {

View file

@ -388,6 +388,35 @@ func NewServer(opts ...ServerOption) (*Server, error) {
return s, nil
}
// UpAndDown brings the server up minimally and shuts it down
// again; basically, it exists for testing holder open and close.
func (s *Server) UpAndDown() error {
s.logger.Printf("open server")
// Log startup
err := s.holder.logStartup()
if err != nil {
log.Println(errors.Wrap(err, "logging startup"))
}
// Initialize id-key storage.
if err := s.holder.translateFile.Open(); err != nil {
return errors.Wrap(err, "opening TranslateFile")
}
// Open holder.
if err := s.holder.Open(); err != nil {
return errors.Wrap(err, "opening Holder")
}
errh := s.holder.Close()
if errh != nil {
return errors.Wrap(errh, "closing holder")
}
return nil
}
// Open opens and initializes the server.
func (s *Server) Open() error {
s.logger.Printf("open server")

View file

@ -161,6 +161,38 @@ func (m *Command) Start() (err error) {
return nil
}
func (m *Command) UpAndDown() (err error) {
// Seed random number generator
rand.Seed(time.Now().UTC().UnixNano())
// SetupServer
err = m.SetupServer()
if err != nil {
return errors.Wrap(err, "setting up server")
}
// SetupNetworking (so we'll have profiling)
err = m.setupNetworking()
if err != nil {
return errors.Wrap(err, "setting up networking")
}
go func() {
err := m.Handler.Serve()
if err != nil {
m.logger.Printf("handler serve error: %v", err)
}
}()
// Bring the server up, and back down again.
if err = m.Server.UpAndDown(); err != nil {
return errors.Wrap(err, "bringing server up and down")
}
m.logger.Printf("brought up and shut down again")
return nil
}
// Wait waits for the server to be closed or interrupted.
func (m *Command) Wait() error {
// First SIGKILL causes server to shut down gracefully.