Skip to content

Commit f6ce9da

Browse files
wenyihu6tbg
authored andcommitted
asim: add SetSimulationSettings as a mutation event
This commit adds SetSimulationSettings as a delayed mutation event, enabling data-driven tests to modify simulation settings in the middle of the simulation. Note that this is left unused as of now. This will support future changes like updating the rebalance mode during a run. Epic: none Release note: none
1 parent 98c975b commit f6ce9da

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

pkg/kv/kvserver/asim/event/mutation_event.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,19 @@ type SetNodeLocalityEvent struct {
5151
LocalityString string
5252
}
5353

54+
// SetSimulationSettingsEvent represents a mutation event responsible for
55+
// changing a simulation setting during the simulation.
56+
type SetSimulationSettingsEvent struct {
57+
Key string
58+
Value interface{}
59+
}
60+
5461
var _ Event = &SetSpanConfigEvent{}
5562
var _ Event = &AddNodeEvent{}
5663
var _ Event = &SetNodeLivenessEvent{}
5764
var _ Event = &SetCapacityOverrideEvent{}
5865
var _ Event = &SetNodeLocalityEvent{}
66+
var _ Event = &SetSimulationSettingsEvent{}
5967

6068
func (se SetSpanConfigEvent) Func() EventFunc {
6169
return MutationFunc(func(ctx context.Context, s state.State) {
@@ -129,3 +137,13 @@ func (sne SetNodeLocalityEvent) Func() EventFunc {
129137
func (sne SetNodeLocalityEvent) String() string {
130138
return fmt.Sprintf("set node locality event with nodeID=%d, locality=%v", sne.NodeID, sne.LocalityString)
131139
}
140+
141+
func (se SetSimulationSettingsEvent) Func() EventFunc {
142+
return MutationFunc(func(ctx context.Context, s state.State) {
143+
s.SetSimulationSettings(se.Key, se.Value)
144+
})
145+
}
146+
147+
func (se SetSimulationSettingsEvent) String() string {
148+
return fmt.Sprintf("set simulation settings event with key=%s, value=%v", se.Key, se.Value)
149+
}

pkg/kv/kvserver/asim/state/impl.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"context"
1212
"fmt"
1313
"math"
14+
"reflect"
1415
"slices"
1516
"sort"
1617
"strconv"
@@ -1319,6 +1320,27 @@ func (s *state) RegisterConfigChangeListener(listener ConfigChangeListener) {
13191320
s.configChangeListeners = append(s.configChangeListeners, listener)
13201321
}
13211322

1323+
// SetSimulationSettings sets the simulation setting for the given key to the
1324+
// given value.
1325+
func (s *state) SetSimulationSettings(Key string, Value interface{}) {
1326+
settingsValue := reflect.ValueOf(s.settings).Elem()
1327+
settingsType := settingsValue.Type()
1328+
1329+
for i := 0; i < settingsValue.NumField(); i++ {
1330+
field := settingsType.Field(i)
1331+
if field.Name == Key {
1332+
fieldValue := settingsValue.Field(i)
1333+
if fieldValue.CanSet() {
1334+
newValue := reflect.ValueOf(Value)
1335+
if newValue.Type().ConvertibleTo(fieldValue.Type()) {
1336+
fieldValue.Set(newValue.Convert(fieldValue.Type()))
1337+
}
1338+
}
1339+
break
1340+
}
1341+
}
1342+
}
1343+
13221344
// node is an implementation of the Node interface.
13231345
type node struct {
13241346
nodeID NodeID

pkg/kv/kvserver/asim/state/state.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ type State interface {
197197
// RegisterConfigChangeListener registers a listener which will be called
198198
// when a cluster configuration change occurs such as a store being added.
199199
RegisterConfigChangeListener(ConfigChangeListener)
200+
// SetSimulationSettings sets the simulation settings for the state.
201+
SetSimulationSettings(Key string, Value interface{})
200202
}
201203

202204
// Node is a container for stores and is part of a cluster.

0 commit comments

Comments
 (0)