forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ingest Manager] Retryable downloads of beats (elastic#19102)
[Ingest Manager] Retryable downloads of beats (elastic#19102)
- Loading branch information
1 parent
38aec1c
commit 5a6f477
Showing
9 changed files
with
249 additions
and
15 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
x-pack/elastic-agent/pkg/agent/operation/operation_retryable.go
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,95 @@ | ||
// 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 operation | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/plugin/retry" | ||
) | ||
|
||
// retryableOperations consists of multiple operations which are | ||
// retryable as a whole. | ||
// if nth operation fails all preceding are retried as well | ||
type retryableOperations struct { | ||
logger *logger.Logger | ||
operations []operation | ||
retryConfig *retry.Config | ||
} | ||
|
||
func newRetryableOperations( | ||
logger *logger.Logger, | ||
retryConfig *retry.Config, | ||
operations ...operation) *retryableOperations { | ||
|
||
return &retryableOperations{ | ||
logger: logger, | ||
retryConfig: retryConfig, | ||
operations: operations, | ||
} | ||
} | ||
|
||
// Name is human readable name identifying an operation | ||
func (o *retryableOperations) Name() string { | ||
names := make([]string, 0, len(o.operations)) | ||
for _, op := range o.operations { | ||
names = append(names, op.Name()) | ||
} | ||
return fmt.Sprintf("retryable block: %s", strings.Join(names, " ")) | ||
} | ||
|
||
// Check checks whether operation needs to be run | ||
// examples: | ||
// - Start does not need to run if process is running | ||
// - Fetch does not need to run if package is already present | ||
func (o *retryableOperations) Check(application Application) (bool, error) { | ||
for _, op := range o.operations { | ||
// finish early if at least one operation needs to be run or errored out | ||
if run, err := op.Check(application); err != nil || run { | ||
return run, err | ||
} | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
// Run runs the operation | ||
func (o *retryableOperations) Run(ctx context.Context, application Application) (err error) { | ||
return retry.Do(ctx, o.retryConfig, o.runOnce(application)) | ||
} | ||
|
||
// Run runs the operation | ||
func (o *retryableOperations) runOnce(application Application) func(context.Context) error { | ||
return func(ctx context.Context) error { | ||
for _, op := range o.operations { | ||
if ctx.Err() != nil { | ||
return ctx.Err() | ||
} | ||
|
||
shouldRun, err := op.Check(application) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !shouldRun { | ||
continue | ||
} | ||
|
||
o.logger.Debugf("running operation '%s' of the block '%s'", op.Name(), o.Name()) | ||
if err := op.Run(ctx, application); err != nil { | ||
o.logger.Errorf("operation %s failed", op.Name()) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// check interface | ||
var _ operation = &retryableOperations{} |
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
5 changes: 5 additions & 0 deletions
5
x-pack/elastic-agent/pkg/artifact/download/fs/testdata/drop/beat-8.0.0-darwin-x86_64.tar.gz
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,5 @@ | ||
sample | ||
content | ||
of | ||
a | ||
file |
1 change: 1 addition & 0 deletions
1
...astic-agent/pkg/artifact/download/fs/testdata/drop/beat-8.0.0-darwin-x86_64.tar.gz.sha512
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 @@ | ||
9af9aa016f3349aa248034629e4336ca2f4d31317bfb8c9a23a9d924c18969cf43ad93727e784da010a272690b2b5ce4c4ded3a5d2039e4408e93e1e18d113db beat-8.0.0-darwin-x86_64.tar.gz |
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