8000 fix(chi): deprioritize the host being restarted in remote_servers by Savid · Pull Request #2177 · Altinity/clickhouse-operator · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* **Operator API packages can now be imported as a Go module without additional dependency overrides.** The fork-specific `mergo` implementation is included in `pkg/util/mergo`, removing the need for downstream consumers to add their own `mergo` `replace` directive to build against the operator's Go API packages. by [@sks](https://github.com/sks) in [#2160](https://github.com/Altinity/clickhouse-operator/pull/2160). Fixes [#2150](https://github.com/Altinity/clickhouse-operator/issues/2150)

* **A host that is about to restart is now deprioritized in `remote_servers`.** Marking a host low priority stored nothing, so its `<replica>` entry never received `<priority>1000</priority>`: the common ConfigMap was rendered unchanged, peers had nothing to pick up and kept sending Distributed subqueries to the host at normal priority while it went down. Rolling restarts now render the deprioritization, so each host restart updates the common ConfigMap twice, on exclude and on include. by [@Savid](https://github.com/Savid) in [#2177](https://github.com/Altinity/clickhouse-operator/pull/2177)

* **Malformed boolean-like configuration values no longer interrupt reconciliation** of other resources. by [@sunsingerus](https://github.com/sunsingerus)

* **Corrected ClickHouse container detection** when logging or backup sidecars appear first in a pod template. by [@sunsingerus](https://github.com/sunsingerus)
Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/common/types/reconcile_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ func (a *ReconcileAttributes) IsExclude() bool {
return a.tags.Has(TagExclude)
}

// SetLowPriority sets 'LowPriority' attribute.
// Nil tags stay a no-op: Tags.Set on a nil map does not store, and CHI
// descendHostInClickHouseCluster calls this on every restart. Allocating
// tags here would emit <priority>1000</priority> in remote_servers.
// SetLowPriority sets 'LowPriority' attribute
func (a *ReconcileAttributes) SetLowPriority() *ReconcileAttributes {
if a == nil {
return a
}
if a.tags == nil {
a.tags = NewTags()
}
a.tags.Set(TagLowPriority)
return a
}
Expand Down
117 changes: 117 additions & 0 deletions pkg/model/chi/config/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

chi "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/apis/common/types"
"github.com/altinity/clickhouse-operator/pkg/model/chi/namer"
)

// TestGetRemoteServersClusterSecret walks the secret-source matrix that drives
Expand Down Expand Up @@ -113,3 +114,119 @@ func TestGetRemoteServersClusterSecret_IndentLevel(t *testing.T) {
}
}
}

// newLowPriorityTestCR builds a minimal CR with one cluster of one shard and
// two replicas, and returns the CR together with both of its hosts. The hosts
// carry a port because the normalizer does not run here, and a rendered
// <port>0</port> would be confusing in a failure dump.
func newLowPriorityTestCR() (cr *chi.ClickHouseInstallation, first, second *chi.Host) {
first = &chi.Host{Name: "0", TCPPort: types.NewInt32(9000)}
second = &chi.Host{Name: "1", TCPPort: types.NewInt32(9000)}

cr = &chi.ClickHouseInstallation{}
cr.Name = "test"
cr.Namespace = "test"
cr.Spec.Defaults = chi.NewDefaults()
cr.Spec.Configuration = &chi.Configuration{
Clusters: []*chi.Cluster{
{
Name: "cluster",
Layout: &chi.ChiClusterLayout{
Shards: []*chi.ChiShard{
{Name: "0", Hosts: []*chi.Host{first, second}},
},
Replicas: []*chi.ChiReplica{
{Name: "0", Hosts: []*chi.Host{first}},
{Name: "1", Hosts: []*chi.Host{second}},
},
},
},
},
}
// Wires host addresses and the back-pointers to the CR, as the normalizer does.
cr.Fill()

return cr, first, second
}

// remoteServersReplica is one <replica> block of rendered remote_servers.
type remoteServersReplica struct {
host string
lowPriority bool
}

// parseRemoteServersReplicas splits rendered remote_servers into its <replica>
// blocks. A host appears once per cluster it belongs to, including the
// autogenerated ones, so blocks are returned as a list rather than a map.
func parseRemoteServersReplicas(t *testing.T, remoteServers string) (replicas []remoteServersReplica) {
t.Helper()
for _, block := range strings.Split(remoteServers, "<replica>")[1:] {
block, _, found := strings.Cut(block, "</replica>")
if !found {
t.Fatalf("unterminated <replica> block in:\n%s", remoteServers)
}
_, host, found := strings.Cut(block, "<host>")
if !found {
t.Fatalf("<replica> block without a <host> in:\n%s", remoteServers)
}
host, _, _ = strings.Cut(host, "</host>")
replicas = append(replicas, remoteServersReplica{
host: host,
lowPriority: strings.Contains(block, "<priority>1000</priority>"),
})
}
return replicas
}

// TestGetRemoteServersLowPriority covers the de-prioritisation that
// descendHostInClickHouseCluster performs before a host is restarted: the host
// it marks must come out of remote_servers with <priority>1000</priority>, so
// that peers prefer its replicas while it goes down, and must come back to
// normal priority once the mark is removed.
//
// Peers only learn about it through the common ConfigMap, so rendering the
// unmarked and the marked host identically is a silent failure: the ConfigMap
// does not change, no Pod remounts it, and no peer reloads.
func TestGetRemoteServersLowPriority(t *testing.T) {
cr, descended, peer := newLowPriorityTestCR()
generator := NewGenerator(cr, namer.New(), &GeneratorOptions{})
descendedHost := generator.getRemoteServersReplicaHostname(descended)
peerHost := generator.getRemoteServersReplicaHostname(peer)

atNormalPriority := generator.getRemoteServers(nil)
if strings.Contains(atNormalPriority, "<priority>") {
t.Fatalf("no host is marked low priority, but remote_servers carries a priority:\n%s", atNormalPriority)
}

descended.GetReconcileAttributes().SetLowPriority()
descendedOut := generator.getRemoteServers(nil)
if descendedOut == atNormalPriority {
t.Fatalf("remote_servers is unchanged after %s was marked low priority, so the ConfigMap never changes and peers never reload:\n%s",
descendedHost, descendedOut)
}

marked := 0
for _, replica := range parseRemoteServersReplicas(t, descendedOut) {
switch replica.host {
case descendedHost:
if !replica.lowPriority {
t.Errorf("replica %s is missing <priority>1000</priority> in:\n%s", replica.host, descendedOut)
}
marked++
case peerHost:
if replica.lowPriority {
t.Errorf("replica %s is not the descended host but carries <priority>1000</priority> in:\n%s", replica.host, descendedOut)
}
default:
t.Errorf("unexpected replica %s in:\n%s", replica.host, descendedOut)
}
}
if marked == 0 {
t.Fatalf("descended host %s is not present in remote_servers at all:\n%s", descendedHost, descendedOut)
}

descended.GetReconcileAttributes().UnsetLowPriority()
if restored := generator.getRemoteServers(nil); restored != atNormalPriority {
t.Errorf("unsetting low priority did not restore remote_servers.\nexpected:\n%s\ngot:\n%s", atNormalPriority, restored)
}
}
Loading
0