Skip to content

Commit a443a93

Browse files
fix: correct ActiveClusters deepcopy method (#7394)
<!-- Describe what has changed in this PR --> **What changed?** small bugfix for a deepcopy method which wasn't working as advertised <!-- Tell your future self why have you made these changes --> **Why?** <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** <!-- Is it notable for release? e.g. schema updates, configuration or data migration required? If so, please mention it, and also update CHANGELOG.md --> **Release notes** <!-- Is there any documentation updates should be made for config, https://cadenceworkflow.io/docs/operation-guide/setup/ ? If so, please open an PR in https://github.com/cadence-workflow/cadence-docs --> **Documentation Changes** Signed-off-by: David Porter <[email protected]>
1 parent cf8d803 commit a443a93

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

common/types/shared.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2731,7 +2731,14 @@ func (v *ActiveClusters) DeepCopy() *ActiveClusters {
27312731
if v.AttributeScopes != nil {
27322732
result.AttributeScopes = make(map[string]ClusterAttributeScope, len(v.AttributeScopes))
27332733
for scopeType, scope := range v.AttributeScopes {
2734-
result.AttributeScopes[scopeType] = scope
2734+
copiedScope := ClusterAttributeScope{}
2735+
if scope.ClusterAttributes != nil {
2736+
copiedScope.ClusterAttributes = make(map[string]ActiveClusterInfo, len(scope.ClusterAttributes))
2737+
for attrName, attrInfo := range scope.ClusterAttributes {
2738+
copiedScope.ClusterAttributes[attrName] = attrInfo
2739+
}
2740+
}
2741+
result.AttributeScopes[scopeType] = copiedScope
27352742
}
27362743
}
27372744
return result

common/types/shared_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,41 @@ func TestActiveClustersConfigDeepCopy(t *testing.T) {
130130
}
131131
}
132132

133+
// Todo (david.porter) delete this test and codegen this
134+
func TestActiveClustersDeepCopyMutationIsolation(t *testing.T) {
135+
136+
t.Run("modifying nested ClusterAttributes map in original should not affect copy", func(t *testing.T) {
137+
original := &ActiveClusters{
138+
AttributeScopes: map[string]ClusterAttributeScope{
139+
"region": {
140+
ClusterAttributes: map[string]ActiveClusterInfo{
141+
"us-east-1": {
142+
ActiveClusterName: "cluster1",
143+
FailoverVersion: 100,
144+
},
145+
},
146+
},
147+
},
148+
}
149+
150+
copied := original.DeepCopy()
151+
152+
assert.Equal(t, original, copied)
153+
154+
scope := original.AttributeScopes["region"]
155+
scope.ClusterAttributes["us-west-1"] = ActiveClusterInfo{
156+
ActiveClusterName: "cluster2",
157+
FailoverVersion: 200,
158+
}
159+
original.AttributeScopes["region"] = scope
160+
161+
assert.Len(t, original.AttributeScopes["region"].ClusterAttributes, 2)
162+
assert.Len(t, copied.AttributeScopes["region"].ClusterAttributes, 1)
163+
assert.Contains(t, original.AttributeScopes["region"].ClusterAttributes, "us-west-1")
164+
assert.NotContains(t, copied.AttributeScopes["region"].ClusterAttributes, "us-west-1")
165+
})
166+
}
167+
133168
func TestIsActiveActiveDomain(t *testing.T) {
134169
tests := []struct {
135170
name string

0 commit comments

Comments
 (0)