From 5705b45864ba52256b1917f231a302a6545de58a Mon Sep 17 00:00:00 2001 From: Alan Bernstein Date: Tue, 11 Aug 2020 10:57:59 -0500 Subject: [PATCH] Add a CLI flag for cluster name and include it in /status response --- api.go | 10 ++++++++++ cluster.go | 3 +++ ctl/server.go | 1 + http/handler.go | 20 ++++++++++++-------- server.go | 8 ++++++++ server/config.go | 1 + server/server.go | 1 + 7 files changed, 36 insertions(+), 8 deletions(-) diff --git a/api.go b/api.go index 4e11cc838..232a510e8 100644 --- a/api.go +++ b/api.go @@ -1739,6 +1739,16 @@ func (api *API) State() string { return api.cluster.State() } +// ClusterID returns the cluster ID. +func (api *API) ClusterID() string { + return api.cluster.id +} + +// ClusterName returns the cluster name. +func (api *API) ClusterName() string { + return api.cluster.Name +} + // Version returns the Pilosa version. func (api *API) Version() string { return strings.TrimPrefix(Version, "v") diff --git a/cluster.go b/cluster.go index 1762be18d..f31a819e5 100644 --- a/cluster.go +++ b/cluster.go @@ -208,6 +208,9 @@ type cluster struct { // nolint: maligned // The number of replicas a partition has. ReplicaN int + // Human-readable name of the cluster. + Name string + // Threshold for logging long-running queries // TODO(2.0) move this out of cluster. (why is it here??) longQueryTime time.Duration diff --git a/ctl/server.go b/ctl/server.go index 968b2bd6d..221f3918f 100644 --- a/ctl/server.go +++ b/ctl/server.go @@ -47,6 +47,7 @@ func BuildServerFlags(cmd *cobra.Command, srv *server.Command) { flags.IntVarP(&srv.Config.Cluster.ReplicaN, "cluster.replicas", "", 1, "Number of hosts each piece of data should be stored on.") flags.StringSliceVarP(&srv.Config.Cluster.Hosts, "cluster.hosts", "", []string{}, "Comma separated list of hosts in cluster. Only used for testing.") flags.DurationVarP((*time.Duration)(&srv.Config.Cluster.LongQueryTime), "cluster.long-query-time", "", time.Minute, "Duration that will trigger log and stat messages for slow queries.") + flags.StringVar(&srv.Config.Cluster.Name, "cluster.name", srv.Config.Cluster.Name, "Human-readable name for the cluster.") // Translation flags.StringVarP(&srv.Config.Translation.PrimaryURL, "translation.primary-url", "", srv.Config.Translation.PrimaryURL, "DEPRECATED: URL for primary translation node for replication.") diff --git a/http/handler.go b/http/handler.go index 186e857ea..6f08664a4 100644 --- a/http/handler.go +++ b/http/handler.go @@ -700,10 +700,12 @@ func (h *Handler) handleGetStatus(w http.ResponseWriter, r *http.Request) { return } status := getStatusResponse{ - State: h.api.State(), - Nodes: h.api.Hosts(r.Context()), - NodeStates: h.api.HostStates(r.Context()), - LocalID: h.api.Node().ID, + State: h.api.State(), + Nodes: h.api.Hosts(r.Context()), + NodeStates: h.api.HostStates(r.Context()), + LocalID: h.api.Node().ID, + ClusterID: h.api.ClusterID(), + ClusterName: h.api.ClusterName(), } w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(status); err != nil { @@ -759,10 +761,12 @@ type getSchemaResponse struct { } type getStatusResponse struct { - State string `json:"state"` - Nodes []*pilosa.Node `json:"nodes"` - NodeStates map[string]string `json:"nodeStates"` - LocalID string `json:"localID"` + State string `json:"state"` + Nodes []*pilosa.Node `json:"nodes"` + NodeStates map[string]string `json:"nodeStates"` + LocalID string `json:"localID"` + ClusterID string `json:"clusterID"` + ClusterName string `json:"clusterName"` } func hash(s string) string { diff --git a/server.go b/server.go index a93df180a..a011ab0ba 100644 --- a/server.go +++ b/server.go @@ -269,6 +269,14 @@ func OptServerClusterDisabled(disabled bool, hosts []string) ServerOption { } } +// OptServerClusterName sets the human-readable cluster name. +func OptServerClusterName(name string) ServerOption { + return func(s *Server) error { + s.cluster.Name = name + return nil + } +} + // OptServerSerializer is a functional option on Server // used to set the serializer. func OptServerSerializer(ser Serializer) ServerOption { diff --git a/server/config.go b/server/config.go index c51a7f95f..3e8575c81 100644 --- a/server/config.go +++ b/server/config.go @@ -122,6 +122,7 @@ type Config struct { Coordinator bool `toml:"coordinator"` ReplicaN int `toml:"replicas"` Hosts []string `toml:"hosts"` + Name string `toml:"name"` // TODO(2.0) move this out of cluster. (why is it here??) LongQueryTime toml.Duration `toml:"long-query-time"` } `toml:"cluster"` diff --git a/server/server.go b/server/server.go index 1b7dea316..077c8a55c 100644 --- a/server/server.go +++ b/server/server.go @@ -406,6 +406,7 @@ func (m *Command) SetupServer() error { pilosa.OptServerGRPCURI(advertiseGRPCURI), pilosa.OptServerInternalClient(http.NewInternalClientFromURI(uri, c)), pilosa.OptServerClusterDisabled(m.Config.Cluster.Disabled, m.Config.Cluster.Hosts), + pilosa.OptServerClusterName(m.Config.Cluster.Name), pilosa.OptServerSerializer(proto.Serializer{}), pilosa.OptServerTxsrc(m.Config.Txsrc), coordinatorOpt,