Skip to content

Commit

Permalink
roachprod/failure-injection: add failure-injection registry
Browse files Browse the repository at this point in the history
This registry will allow for future usage of the failure
injection library through the CLI and the failure injection
planner/controller.
  • Loading branch information
DarrylWong committed Feb 7, 2025
1 parent 5941334 commit 3ee4bee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/roachprod/failureinjection/failures/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_library(
srcs = [
"failure.go",
"network_partition.go",
"registry.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/roachprod/failureinjection/failures",
visibility = ["//visibility:public"],
Expand Down
4 changes: 4 additions & 0 deletions pkg/roachprod/failureinjection/failures/network_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func MakeIPTablesPartitionNode(

const IPTablesNetworkPartitionName = "iptables-network-partition"

func registerIPTablesPartitionNode(r *FailureRegistry) {
r.add(IPTablesNetworkPartitionName, NetworkPartitionArgs{}, MakeIPTablesPartitionNode)
}

func (f *IPTablesPartitionNode) Description() string {
return IPTablesNetworkPartitionName
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/roachprod/failureinjection/failures/registry.go
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)
}

0 comments on commit 3ee4bee

Please sign in to comment.