8000 fix: drop removed replicas by name instead of array position by Slach · Pull Request #2128 · Altinity/clickhouse-operator · GitHub
[go: up one dir, main page]

Skip to content
Open
131 changes: 73 additions & 58 deletions pkg/apis/clickhouse.altinity.com/v1/action_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func (ap *ActionPlan) GetRemovedHostsNum() int {
return count
}

// WalkRemoved walk removed cluster items
// WalkRemoved walk removed cluster items - items present in the old CR and absent in the new one
func (ap *ActionPlan) WalkRemoved(
clusterFunc func(cluster ICluster),
shardFunc func(shard IShard),
Expand All @@ -357,38 +357,10 @@ func (ap *ActionPlan) WalkRemoved(
if ap == nil {
return
}
// TODO refactor to map[string]object handling, instead of slice
for path := range ap.specDiff.Removed {
switch ap.specDiff.Removed[path].(type) {
//case ChiCluster:
// cluster := ap.specDiff.Removed[path].(api.ChiCluster)
// clusterFunc(&cluster)
//case api.ChiShard:
// shard := ap.specDiff.Removed[path].(api.ChiShard)
// shardFunc(&shard)
//case api.Host:
// host := ap.specDiff.Removed[path].(api.Host)
// hostFunc(&host)
//case *api.ChiCluster:
// cluster := ap.specDiff.Removed[path].(*api.ChiCluster)
// clusterFunc(cluster)
case ICluster:
cluster := ap.specDiff.Removed[path].(ICluster)
clusterFunc(cluster)
//case *api.ChiShard:
// shard := ap.specDiff.Removed[path].(*api.ChiShard)
// shardFunc(shard)
case IShard:
shard := ap.specDiff.Removed[path].(IShard)
shardFunc(shard)
case *Host:
host := ap.specDiff.Removed[path].(*Host)
hostFunc(host)
}
}
walkMissing(ap.old, ap.new, clusterFunc, shardFunc, hostFunc)
}

// WalkAdded walk added cluster items
// WalkAdded walk added cluster items - items present in the new CR and absent in the old one
func (ap *ActionPlan) WalkAdded(
clusterFunc func(cluster ICluster),
shardFunc func(shard IShard),
Expand All @@ -397,35 +369,78 @@ func (ap *ActionPlan) WalkAdded(
if ap == nil {
return
}
// TODO refactor to map[string]object handling, instead of slice
for path := range ap.specDiff.Added {
switch ap.specDiff.Added[path].(type) {
//case api.ChiCluster:
// cluster := ap.specDiff.Added[path].(api.ChiCluster)
// clusterFunc(&cluster)
//case api.ChiShard:
// shard := ap.specDiff.Added[path].(api.ChiShard)
// shardFunc(&shard)
//case api.Host:
// host := ap.specDiff.Added[path].(api.Host)
// hostFunc(&host)
//case *api.ChiCluster:
// cluster := ap.specDiff.Added[path].(*api.ChiCluster)
// clusterFunc(cluster)
case ICluster:
cluster := ap.specDiff.Added[path].(ICluster)
walkMissing(ap.new, ap.old, clusterFunc, shardFunc, hostFunc)
}

// walkMissing walks items of `from` which have no same-named counterpart in `to`.
// Items are matched by name - cluster, then shard within the cluster, then host within the shard -
// and not by slice position, as the spec diff does. Otherwise removing or inserting a replica
// anywhere but at the tail of the list would shift identities and report wrong hosts.
// The most specific missing item is reported: a missing shard is reported as a shard, not as its hosts.
func walkMissing(
from, to ICustomResource,
clusterFunc func(cluster ICluster),
shardFunc func(shard IShard),
hostFunc func(host *Host),
) {
if (from == nil) || (to == nil) {
return
}
from.WalkClusters(func(cluster ICluster) error {
toCluster := findClusterByName(to, cluster.GetName())
if toCluster == nil {
clusterFunc(cluster)
//case *api.ChiShard:
// shard := ap.specDiff.Added[path].(*api.ChiShard)
// shardFunc(shard)
case IShard:
shard := ap.specDiff.Added[path].(IShard)
shardFunc(shard)
case *Host:
host := ap.specDiff.Added[path].(*Host)
hostFunc(host)
return nil
}
}
cluster.WalkShards(func(_ int, shard IShard) error {
toShard := findShardByName(toCluster, shard.GetName())
if toShard == nil {
shardFunc(shard)
return nil
}
shard.WalkHosts(func(host *Host) error {
if findHostByName(toShard, host.GetName()) == nil {
hostFunc(host)
}
return nil
})
return nil
})
return nil
})
}

// findClusterByName finds cluster by name. Returns untyped nil when not found.
func findClusterByName(cr ICustomResource, name string) (res ICluster) {
cr.WalkClusters(func(cluster ICluster) error {
if (res == nil) && (cluster.GetName() == name) {
res = cluster
}
return nil
})
return res
}

// findShardByName finds shard by name. Returns untyped nil when not found.
func findShardByName(cluster ICluster, name string) (res IShard) {
cluster.WalkShards(func(_ int, shard IShard) error {
if (res == nil) && (shard.GetName() == name) {
res = shard
}
return nil
})
return res
}

// findHostByName finds host by name
func findHostByName(shard IShard, name string) (res *Host) {
shard.WalkHosts(func(host *Host) error {
if (res == nil) && (host.GetName() == name) {
res = host
}
return nil
})
return res
}

// WalkModified walk modified cluster items
Expand Down
88 changes: 88 additions & 0 deletions pkg/apis/clickhouse.altinity.com/v1/action_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,91 @@ func TestActionPlanDeepCopyIsRaceFree(t *testing.T) {
close(start)
wg.Wait()
}

// chiWithLayout builds a CHI out of cluster -> shard -> host names
func chiWithLayout(layout map[string]map[string][]string) *ClickHouseInstallation {
cr := &ClickHouseInstallation{}
cr.Spec.Configuration = NewConfiguration()
for clusterName, shards := range layout {
cluster := &Cluster{Name: clusterName, Layout: &ChiClusterLayout{}}
for shardName, hosts := range shards {
shard := &ChiShard{Name: shardName}
for _, hostName := range hosts {
shard.Hosts = append(shard.Hosts, &Host{Name: hostName})
}
cluster.Layout.Shards = append(cluster.Layout.Shards, shard)
}
cr.Spec.Configuration.Clusters = append(cr.Spec.Configuration.Clusters, cluster)
}
return cr
}

type walked struct {
clusters, shards, hosts []string
}

func collect(walk func(func(ICluster), func(IShard), func(*Host))) (res walked) {
walk(
func(cluster ICluster) { res.clusters = append(res.clusters, cluster.GetName()) },
func(shard IShard) { res.shards = append(res.shards, shard.GetName()) },
func(host *Host) { res.hosts = append(res.hosts, host.GetName()) },
)
return res
}

// TestActionPlanWalkByName pins that added/removed items are matched by name, not by
// position in the list - https://github.com/Altinity/clickhouse-operator/issues/1545
func TestActionPlanWalkByName(t *testing.T) {
oneShard := func(hosts ...string) *ClickHouseInstallation {
return chiWithLayout(map[string]map[string][]string{"c": {"0": hosts}})
}

t.Run("removing head replicas removes them, not the tail ones", func(t *testing.T) {
plan := MakeActionPlan(
oneShard("0-0", "0-1", "0-2", "0-0-dedicated", "0-1-dedicated", "0-2-dedicated"),
oneShard("0-0-dedicated", "0-1-dedicated", "0-2-dedicated"),
)
require.Equal(t, walked{hosts: []string{"0-0", "0-1", "0-2"}}, collect(plan.WalkRemoved))
require.Equal(t, walked{}, collect(plan.WalkAdded))
require.Equal(t, 3, plan.GetRemovedHostsNum())
})

t.Run("removing tail replicas still works", func(t *testing.T) {
plan := MakeActionPlan(oneShard("0-0", "0-1", "0-2"), oneShard("0-0"))
require.Equal(t, walked{hosts: []string{"0-1", "0-2"}}, collect(plan.WalkRemoved))
require.Equal(t, walked{}, collect(plan.WalkAdded))
})

t.Run("removing a middle replica removes only it", func(t *testing.T) {
plan := MakeActionPlan(oneShard("0-0", "0-1", "0-2"), oneShard("0-0", "0-2"))
require.Equal(t, walked{hosts: []string{"0-1"}}, collect(plan.WalkRemoved))
require.Equal(t, walked{}, collect(plan.WalkAdded))
})

t.Run("inserting a replica in the middle adds only it", func(t *testing.T) {
plan := MakeActionPlan(oneShard("0-0", "0-2"), oneShard("0-0", "0-1", "0-2"))
require.Equal(t, walked{hosts: []string{"0-1"}}, collect(plan.WalkAdded))
require.Equal(t, walked{}, collect(plan.WalkRemoved))
})

t.Run("removed shard and cluster are reported as a whole", func(t *testing.T) {
plan := MakeActionPlan(
chiWithLayout(map[string]map[string][]string{
"a": {"0": {"0-0"}, "1": {"1-0", "1-1"}},
"b": {"0": {"0-0", "0-1"}},
}),
chiWithLayout(map[string]map[string][]string{
"a": {"0": {"0-0"}},
}),
)
require.Equal(t, walked{clusters: []string{"b"}, shards: []string{"1"}}, collect(plan.WalkRemoved))
require.Equal(t, walked{}, collect(plan.WalkAdded))
require.Equal(t, 4, plan.GetRemovedHostsNum())
})

t.Run("nothing changed means nothing walked", func(t *testing.T) {
plan := MakeActionPlan(oneShard("0-0", "0-1"), oneShard("0-0", "0-1"))
require.Equal(t, walked{}, collect(plan.WalkRemoved))
require.Equal(t, walked{}, collect(plan.WalkAdded))
})
}
7 changes: 6 additions & 1 deletion pkg/controller/chi/kube/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/altinity/clickhouse-operator/pkg/controller"
commonKube "github.com/altinity/clickhouse-operator/pkg/controller/common/kube"
"github.com/altinity/clickhouse-operator/pkg/interfaces"
chiNamer "github.com/altinity/clickhouse-operator/pkg/model/chi/namer"
chiLabeler "github.com/altinity/clickhouse-operator/pkg/model/chi/tags/labeler"
)

Expand Down Expand Up @@ -73,13 +74,17 @@ func (c *PVC) List(ctx context.Context, namespace string, opts meta.ListOptions)

func (c *PVC) ListForHost(ctx context.Context, host *api.Host) (*core.PersistentVolumeClaimList, error) {
ctx = k8sCtx(ctx)
return c.kubeClient.
list, err := c.kubeClient.
CoreV1().
PersistentVolumeClaims(host.Runtime.Address.Namespace).
List(
ctx,
controller.NewListOptions(labeler(host.GetCR()).Selector(interfaces.SelectorHostScope, host)),
)
if err != nil {
return list, err
}
return commonKube.FilterPVCsOfPod(list, chiNamer.New().Name(interfaces.NamePod, host)), nil
}

func labeler(cr api.ICustomResource) interfaces.ILabeler {
Expand Down
Loading
Loading
0