Skip to content

Commit

Permalink
dev: dynamic raft config. part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsingerus committed Oct 8, 2024
1 parent cf1e5a6 commit 244e2ca
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 145 deletions.
11 changes: 5 additions & 6 deletions pkg/controller/chi/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/altinity/clickhouse-operator/pkg/model/chi/schemer"
"github.com/altinity/clickhouse-operator/pkg/model/chi/tags/labeler"
"github.com/altinity/clickhouse-operator/pkg/model/common/action_plan"
commonConfig "github.com/altinity/clickhouse-operator/pkg/model/common/config"
commonCreator "github.com/altinity/clickhouse-operator/pkg/model/common/creator"
commonMacro "github.com/altinity/clickhouse-operator/pkg/model/common/macro"
commonNormalizer "github.com/altinity/clickhouse-operator/pkg/model/common/normalizer"
Expand Down Expand Up @@ -113,7 +114,6 @@ func configGeneratorOptions(cr *api.ClickHouseInstallation) *config.GeneratorOpt
}
}

// newContext creates new reconcile task
func (w *worker) newTask(cr *api.ClickHouseInstallation) {
w.task = common.NewTask(
commonCreator.NewCreator(
Expand Down Expand Up @@ -663,12 +663,11 @@ func (w *worker) walkHosts(ctx context.Context, chi *api.ClickHouseInstallation,
}

// getRemoteServersGeneratorOptions build base set of RemoteServersOptions
// which are applied on each of `remote_servers` reconfiguration during reconcile cycle
func (w *worker) getRemoteServersGeneratorOptions() *config.RemoteServersOptions {
// Base chiModel.RemoteServersOptions specifies to exclude:
func (w *worker) getRemoteServersGeneratorOptions() *commonConfig.HostSelector {
// Base model specifies to exclude:
// 1. all newly added hosts
// 2. all explicitly excluded hosts
return config.NewRemoteServersOptions().ExcludeReconcileAttributes(
return commonConfig.NewHostSelector().ExcludeReconcileAttributes(
api.NewHostReconcileAttributes().
SetAdd().
SetExclude(),
Expand All @@ -682,7 +681,7 @@ func (w *worker) options() *config.FilesGeneratorOptions {
return config.NewFilesGeneratorOptions().SetRemoteServersOptions(opts)
}

// createCHIFromObjectMeta
// createCRFromObjectMeta
func (w *worker) createCRFromObjectMeta(meta meta.Object, isCHI bool, options *commonNormalizer.Options) (*api.ClickHouseInstallation, error) {
w.a.V(3).M(meta).S().P()
defer w.a.V(3).M(meta).E().P()
Expand Down
19 changes: 17 additions & 2 deletions pkg/controller/chk/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/altinity/clickhouse-operator/pkg/model/chk/normalizer"
"github.com/altinity/clickhouse-operator/pkg/model/chk/tags/labeler"
"github.com/altinity/clickhouse-operator/pkg/model/common/action_plan"
commonConfig "github.com/altinity/clickhouse-operator/pkg/model/common/config"
commonCreator "github.com/altinity/clickhouse-operator/pkg/model/common/creator"
commonMacro "github.com/altinity/clickhouse-operator/pkg/model/common/macro"
commonNormalizer "github.com/altinity/clickhouse-operator/pkg/model/common/normalizer"
Expand Down Expand Up @@ -395,12 +396,26 @@ func (w *worker) walkHosts(ctx context.Context, chk *apiChk.ClickHouseKeeperInst
})
}

// getRaftGeneratorOptions build base set of RaftOptions
func (w *worker) getRaftGeneratorOptions() *commonConfig.HostSelector {
// Raft specifies to exclude:
// 1. all newly added hosts
// 2. all explicitly excluded hosts
return commonConfig.NewHostSelector().ExcludeReconcileAttributes(
api.NewHostReconcileAttributes().
SetAdd().
SetExclude(),
)
}

// options build FilesGeneratorOptionsClickHouse
func (w *worker) options() *config.FilesGeneratorOptions {
return config.NewFilesGeneratorOptions()
opts := w.getRaftGeneratorOptions()
w.a.Info("RaftOptions: %s", opts)
return config.NewFilesGeneratorOptions().SetRaftOptions(opts)
}

// createCHIFromObjectMeta
// createCRFromObjectMeta
func (w *worker) createCRFromObjectMeta(meta meta.Object, isCHI bool, options *commonNormalizer.Options) (*apiChk.ClickHouseKeeperInstallation, error) {
w.a.V(3).M(meta).S().P()
defer w.a.V(3).M(meta).E().P()
Expand Down
1 change: 0 additions & 1 deletion pkg/model/chi/config/files_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func (c *FilesGenerator) createConfigFilesGroupCommon(options *FilesGeneratorOpt
}

func (c *FilesGenerator) createConfigFilesGroupCommonDomain(configSections map[string]string, options *FilesGeneratorOptions) {
// remote servers
util.IncludeNonEmpty(configSections, createConfigSectionFilename(configRemoteServers), c.configGenerator.getRemoteServers(options.GetRemoteServersOptions()))
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/model/chi/config/files_generator_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@

package config

import api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
import (
api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/model/common/config"
)

// FilesGeneratorOptions specifies options for configuration files generator
type FilesGeneratorOptions struct {
RemoteServersOptions *RemoteServersOptions
host *api.Host
RemoteServersOptions *config.HostSelector
}

// defaultFilesGeneratorOptions creates new default options for files generator
Expand Down Expand Up @@ -49,15 +52,15 @@ func (o *FilesGeneratorOptions) SetHost(host *api.Host) *FilesGeneratorOptions {
}

// GetRemoteServersOptions gets remote-servers generator options
func (o *FilesGeneratorOptions) GetRemoteServersOptions() *RemoteServersOptions {
func (o *FilesGeneratorOptions) GetRemoteServersOptions() *config.HostSelector {
if o == nil {
return nil
}
return o.RemoteServersOptions
}

// SetRemoteServersOptions sets remote-servers generator options
func (o *FilesGeneratorOptions) SetRemoteServersOptions(opts *RemoteServersOptions) *FilesGeneratorOptions {
func (o *FilesGeneratorOptions) SetRemoteServersOptions(opts *config.HostSelector) *FilesGeneratorOptions {
if o == nil {
return nil
}
Expand Down
35 changes: 18 additions & 17 deletions pkg/model/chi/config/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package config
import (
"bytes"
"fmt"
"github.com/altinity/clickhouse-operator/pkg/model/common/config"

log "github.com/altinity/clickhouse-operator/pkg/announcer"
chi "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
Expand Down Expand Up @@ -180,10 +181,10 @@ func (c *Generator) getHostZookeeper(host *chi.Host) string {
}

// chiHostsNum count hosts according to the options
func (c *Generator) chiHostsNum(options *RemoteServersOptions) int {
func (c *Generator) chiHostsNum(selector *config.HostSelector) int {
num := 0
c.cr.WalkHosts(func(host *chi.Host) error {
if options.Include(host) {
if selector.Include(host) {
num++
}
return nil
Expand All @@ -192,21 +193,21 @@ func (c *Generator) chiHostsNum(options *RemoteServersOptions) int {
}

// clusterHostsNum count hosts according to the options
func (c *Generator) clusterHostsNum(cluster chi.ICluster, options *RemoteServersOptions) int {
func (c *Generator) clusterHostsNum(cluster chi.ICluster, selector *config.HostSelector) int {
num := 0
// Build each shard XML
cluster.WalkShards(func(index int, shard chi.IShard) error {
num += c.shardHostsNum(shard, options)
num += c.shardHostsNum(shard, selector)
return nil
})
return num
}

// shardHostsNum count hosts according to the options
func (c *Generator) shardHostsNum(shard chi.IShard, options *RemoteServersOptions) int {
func (c *Generator) shardHostsNum(shard chi.IShard, selector *config.HostSelector) int {
num := 0
shard.WalkHosts(func(host *chi.Host) error {
if options.Include(host) {
if selector.Include(host) {
num++
}
return nil
Expand Down Expand Up @@ -234,9 +235,9 @@ func (c *Generator) getRemoteServersReplica(host *chi.Host, b *bytes.Buffer) {
}

// getRemoteServers creates "remote_servers.xml" content and calculates data generation parameters for other sections
func (c *Generator) getRemoteServers(options *RemoteServersOptions) string {
if options == nil {
options = defaultRemoteServersOptions()
func (c *Generator) getRemoteServers(selector *config.HostSelector) string {
if selector == nil {
selector = defaultRemoteServersOptions()
}

b := &bytes.Buffer{}
Expand All @@ -250,7 +251,7 @@ func (c *Generator) getRemoteServers(options *RemoteServersOptions) string {

// Build each cluster XML
c.cr.WalkClusters(func(cluster chi.ICluster) error {
if c.clusterHostsNum(cluster, options) < 1 {
if c.clusterHostsNum(cluster, selector) < 1 {
// Skip empty cluster
return nil
}
Expand All @@ -269,7 +270,7 @@ func (c *Generator) getRemoteServers(options *RemoteServersOptions) string {

// Build each shard XML
cluster.WalkShards(func(index int, shard chi.IShard) error {
if c.shardHostsNum(shard, options) < 1 {
if c.shardHostsNum(shard, selector) < 1 {
// Skip empty shard
return nil
}
Expand All @@ -285,7 +286,7 @@ func (c *Generator) getRemoteServers(options *RemoteServersOptions) string {
}

shard.WalkHosts(func(host *chi.Host) error {
if options.Include(host) {
if selector.Include(host) {
c.getRemoteServersReplica(host, b)
log.V(2).M(host).Info("Adding host to remote servers: %s", host.GetName())
} else {
Expand All @@ -307,7 +308,7 @@ func (c *Generator) getRemoteServers(options *RemoteServersOptions) string {

// Auto-generated clusters

if c.chiHostsNum(options) < 1 {
if c.chiHostsNum(selector) < 1 {
util.Iline(b, 8, "<!-- Autogenerated clusters are skipped due to absence of hosts -->")
} else {
util.Iline(b, 8, "<!-- Autogenerated clusters -->")
Expand All @@ -321,7 +322,7 @@ func (c *Generator) getRemoteServers(options *RemoteServersOptions) string {
util.Iline(b, 8, " <shard>")
util.Iline(b, 8, " <internal_replication>true</internal_replication>")
c.cr.WalkHosts(func(host *chi.Host) error {
if options.Include(host) {
if selector.Include(host) {
c.getRemoteServersReplica(host, b)
}
return nil
Expand All @@ -338,7 +339,7 @@ func (c *Generator) getRemoteServers(options *RemoteServersOptions) string {
clusterName = AllShardsOneReplicaClusterName
util.Iline(b, 8, "<%s>", clusterName)
c.cr.WalkHosts(func(host *chi.Host) error {
if options.Include(host) {
if selector.Include(host) {
// <shard>
// <internal_replication>
util.Iline(b, 12, "<shard>")
Expand All @@ -361,15 +362,15 @@ func (c *Generator) getRemoteServers(options *RemoteServersOptions) string {
util.Iline(b, 8, "<%s>", clusterName)
c.cr.WalkClusters(func(cluster chi.ICluster) error {
cluster.WalkShards(func(index int, shard chi.IShard) error {
if c.shardHostsNum(shard, options) < 1 {
if c.shardHostsNum(shard, selector) < 1 {
// Skip empty shard
return nil
}
util.Iline(b, 12, "<shard>")
util.Iline(b, 12, " <internal_replication>%s</internal_replication>", shard.GetInternalReplication())

shard.WalkHosts(func(host *chi.Host) error {
if options.Include(host) {
if selector.Include(host) {
c.getRemoteServersReplica(host, b)
}
return nil
Expand Down
107 changes: 3 additions & 104 deletions pkg/model/chi/config/generator_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
package config

import (
"fmt"
"strings"

api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/model/common/config"
)

type GeneratorOptions struct {
Expand All @@ -31,105 +29,6 @@ type GeneratorOptions struct {
Files *api.Settings
}

// RemoteServersOptions specifies options for remote-servers generator
type RemoteServersOptions struct {
exclude struct {
attributes *api.HostReconcileAttributes
hosts []*api.Host
}
}

// NewRemoteServersOptions creates new remote-servers generator options
func NewRemoteServersOptions() *RemoteServersOptions {
return &RemoteServersOptions{}
}

// ExcludeHost specifies to exclude a host
func (o *RemoteServersOptions) ExcludeHost(host *api.Host) *RemoteServersOptions {
if (o == nil) || (host == nil) {
return o
}

o.exclude.hosts = append(o.exclude.hosts, host)
return o
}

// ExcludeHosts specifies to exclude list of hosts
func (o *RemoteServersOptions) ExcludeHosts(hosts ...*api.Host) *RemoteServersOptions {
if (o == nil) || (len(hosts) == 0) {
return o
}

o.exclude.hosts = append(o.exclude.hosts, hosts...)
return o
}

// ExcludeReconcileAttributes specifies to exclude reconcile attributes
func (o *RemoteServersOptions) ExcludeReconcileAttributes(attrs *api.HostReconcileAttributes) *RemoteServersOptions {
if (o == nil) || (attrs == nil) {
return o
}

o.exclude.attributes = attrs
return o
}

// Exclude tells whether to exclude the host
func (o *RemoteServersOptions) Exclude(host *api.Host) bool {
if o == nil {
return false
}

if o.exclude.attributes.Any(host.GetReconcileAttributes()) {
// Reconcile attributes specify to exclude this host
return true
}

for _, val := range o.exclude.hosts {
// Host is in the list to be excluded
if val == host {
return true
}
}

return false
}

// Include tells whether to include the host
func (o *RemoteServersOptions) Include(host *api.Host) bool {
if o == nil {
return false
}

if o.exclude.attributes.Any(host.GetReconcileAttributes()) {
// Reconcile attributes specify to exclude this host
return false
}

for _, val := range o.exclude.hosts {
// Host is in the list to be excluded
if val == host {
return false
}
}

return true
}

// String returns string representation
func (o *RemoteServersOptions) String() string {
if o == nil {
return "(nil)"
}

var hostnames []string
for _, host := range o.exclude.hosts {
hostnames = append(hostnames, host.Name)
}
return fmt.Sprintf("exclude hosts: %s, attributes: %s", "["+strings.Join(hostnames, ",")+"]", o.exclude.attributes)
}

// defaultRemoteServersOptions
func defaultRemoteServersOptions() *RemoteServersOptions {
return NewRemoteServersOptions()
func defaultRemoteServersOptions() *config.HostSelector {
return config.NewHostSelector()
}
2 changes: 1 addition & 1 deletion pkg/model/chk/config/files_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *FilesGenerator) createConfigFilesGroupCommon(options *FilesGeneratorOpt
}

func (c *FilesGenerator) createConfigFilesGroupCommonDomain(configSections map[string]string, options *FilesGeneratorOptions) {
util.IncludeNonEmpty(configSections, createConfigSectionFilename(configRaft), c.configGenerator.getRaftConfig())
util.IncludeNonEmpty(configSections, createConfigSectionFilename(configRaft), c.configGenerator.getRaftConfig(options.GetRaftOptions()))
}

func (c *FilesGenerator) createConfigFilesGroupCommonGeneric(configSections map[string]string, options *FilesGeneratorOptions) {
Expand Down
Loading

0 comments on commit 244e2ca

Please sign in to comment.