Comment Status function

+ make waitForStatus more generic
This commit is contained in:
Kuba Podgórski 2021-03-03 23:47:47 +01:00
parent fa293ba6c3
commit d311b0cac4
2 changed files with 37 additions and 34 deletions

View file

@ -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
}

View file

@ -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)
}