-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utilize new ES fleet polling API for global checkpoint monitoring (#200)
* Utilize fleet polling API for global checkpoint monitoring * Adjust for API changes, configurable poll timeout * Update retryDelay to 3 secs * Update tests for monitor API change * Adjust to the latest API changes * Remove fleet indexes bootstrapping for tests, it is done by fleet system index plugin now * Fix unit tests
- Loading branch information
Showing
22 changed files
with
391 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package es | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/elastic/fleet-server/v7/internal/pkg/sqn" | ||
"github.com/elastic/go-elasticsearch/v8/esapi" | ||
) | ||
|
||
// The wrapper for the new _fleet global_checkpoints that is not the part of the | ||
// standard client library at the moment. | ||
// The shape mimics the official client API and should be easy drop-in replacement in the future. | ||
// This should be replaced the official client library when/if the new API makes it in. | ||
|
||
func NewGlobalCheckpointsRequest(t esapi.Transport) GlobalCheckpoints { | ||
return func(o ...func(*GlobalCheckpointsRequest)) (*esapi.Response, error) { | ||
var r = GlobalCheckpointsRequest{} | ||
for _, f := range o { | ||
f(&r) | ||
} | ||
return r.Do(r.ctx, t) | ||
} | ||
} | ||
|
||
// Copied from the official client | ||
func formatDuration(d time.Duration) string { | ||
if d < time.Millisecond { | ||
return strconv.FormatInt(int64(d), 10) + "nanos" | ||
} | ||
return strconv.FormatInt(int64(d)/int64(time.Millisecond), 10) + "ms" | ||
} | ||
|
||
type GlobalCheckpoints func(o ...func(*GlobalCheckpointsRequest)) (*esapi.Response, error) | ||
|
||
// GlobalCheckpointsRequest configures the _fleet API global_checkpoints request. | ||
// | ||
type GlobalCheckpointsRequest struct { | ||
ctx context.Context | ||
|
||
Index string | ||
WaitForAdvance *bool | ||
Checkpoints []int64 | ||
Timeout time.Duration | ||
|
||
Header http.Header | ||
} | ||
|
||
// Do executes the request and returns response or error. | ||
// | ||
func (r GlobalCheckpointsRequest) Do(ctx context.Context, transport esapi.Transport) (*esapi.Response, error) { | ||
var ( | ||
method string | ||
path strings.Builder | ||
params map[string]string | ||
) | ||
|
||
method = "GET" | ||
|
||
path.Grow(1 + len(r.Index) + len("/_fleet/global_checkpoints")) | ||
if len(r.Index) > 0 { | ||
path.WriteString("/") | ||
path.WriteString(r.Index) | ||
} | ||
path.WriteString("/_fleet/global_checkpoints") | ||
|
||
params = make(map[string]string) | ||
|
||
if r.WaitForAdvance != nil { | ||
params["wait_for_advance"] = strconv.FormatBool(*r.WaitForAdvance) | ||
} | ||
|
||
if len(r.Checkpoints) > 0 { | ||
seqNo := sqn.SeqNo(r.Checkpoints) | ||
params["checkpoints"] = seqNo.String() | ||
} | ||
|
||
if r.Timeout != 0 { | ||
params["timeout"] = formatDuration(r.Timeout) | ||
} | ||
|
||
req, err := http.NewRequest(method, path.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(params) > 0 { | ||
q := req.URL.Query() | ||
for k, v := range params { | ||
q.Set(k, v) | ||
} | ||
req.URL.RawQuery = q.Encode() | ||
} | ||
|
||
if len(r.Header) > 0 { | ||
if len(req.Header) == 0 { | ||
req.Header = r.Header | ||
} else { | ||
for k, vv := range r.Header { | ||
for _, v := range vv { | ||
req.Header.Add(k, v) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if ctx != nil { | ||
req = req.WithContext(ctx) | ||
} | ||
|
||
res, err := transport.Perform(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
response := esapi.Response{ | ||
StatusCode: res.StatusCode, | ||
Body: res.Body, | ||
Header: res.Header, | ||
} | ||
|
||
return &response, nil | ||
} | ||
|
||
// WithContext sets the request context. | ||
// | ||
func (f GlobalCheckpoints) WithContext(v context.Context) func(*GlobalCheckpointsRequest) { | ||
return func(r *GlobalCheckpointsRequest) { | ||
r.ctx = v | ||
} | ||
} | ||
|
||
// WithIndex - an index name | ||
// | ||
func (f GlobalCheckpoints) WithIndex(index string) func(*GlobalCheckpointsRequest) { | ||
return func(r *GlobalCheckpointsRequest) { | ||
r.Index = index | ||
} | ||
} | ||
|
||
func (f GlobalCheckpoints) WithWaitForAdvance(v bool) func(*GlobalCheckpointsRequest) { | ||
return func(r *GlobalCheckpointsRequest) { | ||
r.WaitForAdvance = &v | ||
} | ||
} | ||
|
||
func (f GlobalCheckpoints) WithCheckpoints(checkpoints []int64) func(*GlobalCheckpointsRequest) { | ||
return func(r *GlobalCheckpointsRequest) { | ||
r.Checkpoints = checkpoints | ||
} | ||
} | ||
|
||
func (f GlobalCheckpoints) WithTimeout(to time.Duration) func(*GlobalCheckpointsRequest) { | ||
return func(r *GlobalCheckpointsRequest) { | ||
r.Timeout = to | ||
} | ||
} |
Oops, something went wrong.