-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
roachprod/failure-injection: add failure-injection registry
This registry will allow for future usage of the failure injection library through the CLI and the failure injection planner/controller.
- Loading branch information
1 parent
5941334
commit 3ee4bee
Showing
3 changed files
with
77 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2025 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the CockroachDB Software License | ||
// included in the /LICENSE file. | ||
|
||
package failures | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/roachprod/logger" | ||
) | ||
|
||
type failureSpec struct { | ||
makeFailureFunc func(clusterName string, l *logger.Logger, secure bool) (FailureMode, error) | ||
args FailureArgs | ||
} | ||
type FailureRegistry struct { | ||
failures map[string]failureSpec | ||
} | ||
|
||
func NewFailureRegistry() *FailureRegistry { | ||
return &FailureRegistry{ | ||
failures: make(map[string]failureSpec), | ||
} | ||
} | ||
|
||
func (r *FailureRegistry) Register() { | ||
registerIPTablesPartitionNode(r) | ||
} | ||
|
||
func (r *FailureRegistry) add( | ||
failureName string, | ||
args FailureArgs, | ||
makeFailureFunc func(clusterName string, l *logger.Logger, secure bool) (FailureMode, error), | ||
) { | ||
if _, ok := r.failures[failureName]; ok { | ||
panic(fmt.Sprintf("failure %s already exists", failureName)) | ||
} | ||
r.failures[failureName] = failureSpec{ | ||
makeFailureFunc: makeFailureFunc, | ||
args: args, | ||
} | ||
} | ||
|
||
func (r *FailureRegistry) List(regex string) []string { | ||
var filter *regexp.Regexp | ||
if regex == "" { | ||
filter = regexp.MustCompile(`.`) | ||
} else { | ||
filter = regexp.MustCompile(regex) | ||
} | ||
|
||
var matches []string | ||
for name := range r.failures { | ||
if filter.MatchString(name) { | ||
matches = append(matches, name) | ||
} | ||
} | ||
return matches | ||
} | ||
|
||
func (r *FailureRegistry) GetFailure( | ||
clusterName, failureName string, l *logger.Logger, secure bool, | ||
) (FailureMode, error) { | ||
spec, ok := r.failures[failureName] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown failure %s", failureName) | ||
} | ||
return spec.makeFailureFunc(clusterName, l, secure) | ||
} |