From d311b0cac400e83e8fc24b2f3de2afd28718cb7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Podg=C3=B3rski?= Date: Wed, 3 Mar 2021 23:47:47 +0100 Subject: [PATCH] Comment Status function + make waitForStatus more generic --- http/client.go | 63 ++++++++++++++------------- internal/clustertests/cluster_test.go | 8 ++-- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/http/client.go b/http/client.go index 53482ddeb..3adce376a 100644 --- a/http/client.go +++ b/http/client.go @@ -104,36 +104,6 @@ func (c *InternalClient) maxShardByIndex(ctx context.Context) (map[string]uint64 return rsp.Standard, nil } -func (c *InternalClient) Status(ctx context.Context) (string, error) { - span, ctx := tracing.StartSpanFromContext(ctx, "InternalClient.Status") - defer span.Finish() - - // Execute request against the host. - u := c.defaultURI.Path("/status") - - // Build request. - req, err := http.NewRequest("GET", u, nil) - if err != nil { - return "", errors.Wrap(err, "creating request") - } - - req.Header.Set("User-Agent", "pilosa/"+pilosa.Version) - req.Header.Set("Accept", "application/json") - - // Execute request. - resp, err := c.executeRequest(req.WithContext(ctx)) - if err != nil { - return "", err - } - defer resp.Body.Close() - - var rsp getStatusResponse - if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil { - return "", fmt.Errorf("json decode: %s", err) - } - return rsp.State, nil -} - // SchemaNode returns all index and field schema information from the specified // node. func (c *InternalClient) SchemaNode(ctx context.Context, uri *pnet.URI, views bool) ([]*pilosa.IndexInfo, error) { @@ -2108,3 +2078,36 @@ func (c *InternalClient) ImportFieldKeys(ctx context.Context, uri *pnet.URI, ind defer resp.Body.Close() return nil } + +// Status function is just a public function for this particular implementation of InternalClient. +// It's not require by pilosa.InternalClient interface. +// The function returns pilosa cluster state as a string ("NORMAL", "DEGRADED", "DOWN", "RESIZING", ...) +func (c *InternalClient) Status(ctx context.Context) (string, error) { + span, ctx := tracing.StartSpanFromContext(ctx, "InternalClient.Status") + defer span.Finish() + + // Execute request against the host. + u := c.defaultURI.Path("/status") + + // Build request. + req, err := http.NewRequest("GET", u, nil) + if err != nil { + return "", errors.Wrap(err, "creating request") + } + + req.Header.Set("User-Agent", "pilosa/"+pilosa.Version) + req.Header.Set("Accept", "application/json") + + // Execute request. + resp, err := c.executeRequest(req.WithContext(ctx)) + if err != nil { + return "", err + } + defer resp.Body.Close() + + var rsp getStatusResponse + if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil { + return "", fmt.Errorf("json decode: %s", err) + } + return rsp.State, nil +} diff --git a/internal/clustertests/cluster_test.go b/internal/clustertests/cluster_test.go index 2f443642c..94e0f7b10 100644 --- a/internal/clustertests/cluster_test.go +++ b/internal/clustertests/cluster_test.go @@ -91,7 +91,7 @@ func TestClusterStuff(t *testing.T) { } t.Log("done with pause, waiting for stability") - waitForStatus(t, cli1, string(disco.ClusterStateNormal), 30, time.Second) + waitForStatus(t, cli1.Status, string(disco.ClusterStateNormal), 30, time.Second) t.Log("done waiting for stability") // Check query results from each node. @@ -107,11 +107,11 @@ func TestClusterStuff(t *testing.T) { }) } -func waitForStatus(t *testing.T, c *picli.InternalClient, status string, n int, sleep time.Duration) { +func waitForStatus(t *testing.T, stator func(context.Context) (string, error), status string, n int, sleep time.Duration) { t.Helper() for i := 0; i < n; i++ { - s, err := c.Status(context.TODO()) + s, err := stator(context.TODO()) if err != nil { t.Logf("Status (try %d/%d): %v (retrying in %s)", i, n, err, sleep.String()) } else { @@ -123,7 +123,7 @@ func waitForStatus(t *testing.T, c *picli.InternalClient, status string, n int, time.Sleep(sleep) } - s, err := c.Status(context.TODO()) + s, err := stator(context.TODO()) if err != nil { t.Fatalf("querying status: %v", err) }