Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ClusterMetadata defaults and validation #4385

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
21 changes: 7 additions & 14 deletions cmd/server/cadence/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ func (s *server) startService() common.Daemon {
log.Printf("error creating dynamic config client, using no-op config client instead. error: %v", err)
params.DynamicConfig = dynamicconfig.NewNopClient()
}
clusterMetadata := s.cfg.ClusterMetadata
clusterGroupMetadata := s.cfg.ClusterGroupMetadata
dc := dynamicconfig.NewCollection(
params.DynamicConfig,
params.Logger,
dynamicconfig.ClusterNameFilter(clusterMetadata.CurrentClusterName),
dynamicconfig.ClusterNameFilter(clusterGroupMetadata.CurrentClusterName),
)

svcCfg := s.cfg.Services[s.name]
Expand All @@ -164,20 +164,13 @@ func (s *server) startService() common.Daemon {

params.MetricsClient = metrics.NewClient(params.MetricScope, service.GetMetricsServiceIdx(params.Name, params.Logger))

//TODO: remove this after 0.23 and mention a breaking change in config.
primaryClusterName := clusterMetadata.PrimaryClusterName
if len(primaryClusterName) == 0 {
primaryClusterName = clusterMetadata.MasterClusterName
log.Println("[Warning]MasterClusterName config is deprecated. " +
"Please replace it with PrimaryClusterName.")
}
params.ClusterMetadata = cluster.NewMetadata(
params.Logger,
dc.GetBoolProperty(dynamicconfig.EnableGlobalDomain, clusterMetadata.EnableGlobalDomain),
clusterMetadata.FailoverVersionIncrement,
primaryClusterName,
clusterMetadata.CurrentClusterName,
clusterMetadata.ClusterInformation,
dc.GetBoolProperty(dynamicconfig.EnableGlobalDomain, clusterGroupMetadata.EnableGlobalDomain),
clusterGroupMetadata.FailoverVersionIncrement,
clusterGroupMetadata.PrimaryClusterName,
clusterGroupMetadata.CurrentClusterName,
clusterGroupMetadata.ClusterGroup,
)

if s.cfg.PublicClient.HostPort != "" {
Expand Down
53 changes: 9 additions & 44 deletions common/cluster/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ type (
primaryClusterName string
// currentClusterName is the name of the current cluster
currentClusterName string
// clusterInfo contains all cluster name -> corresponding information
clusterInfo map[string]config.ClusterInformation
// clusterGroup contains all cluster name -> corresponding information
clusterGroup map[string]config.ClusterInformation
// versionToClusterName contains all initial version -> corresponding cluster name
versionToClusterName map[int64]string
}
Expand All @@ -79,46 +79,11 @@ func NewMetadata(
failoverVersionIncrement int64,
primaryClusterName string,
currentClusterName string,
clusterInfo map[string]config.ClusterInformation,
clusterGroup map[string]config.ClusterInformation,
) Metadata {

if len(clusterInfo) == 0 {
panic("Empty cluster information")
} else if len(primaryClusterName) == 0 {
panic("Primary cluster name is empty")
} else if len(currentClusterName) == 0 {
panic("Current cluster name is empty")
} else if failoverVersionIncrement == 0 {
panic("Version increment is 0")
}

versionToClusterName := make(map[int64]string)
for clusterName, info := range clusterInfo {
if failoverVersionIncrement <= info.InitialFailoverVersion || info.InitialFailoverVersion < 0 {
panic(fmt.Sprintf(
"Version increment %v is smaller than initial version: %v.",
failoverVersionIncrement,
info.InitialFailoverVersion,
))
}
if len(clusterName) == 0 {
panic("Cluster name in all cluster names is empty")
}
for clusterName, info := range clusterGroup {
versionToClusterName[info.InitialFailoverVersion] = clusterName

if info.Enabled && (len(info.RPCName) == 0 || len(info.RPCAddress) == 0) {
panic(fmt.Sprintf("Cluster %v: rpc name / address is empty", clusterName))
}
}

if _, ok := clusterInfo[currentClusterName]; !ok {
panic("Current cluster is not specified in cluster info")
}
if _, ok := clusterInfo[primaryClusterName]; !ok {
panic("Primary cluster is not specified in cluster info")
}
if len(versionToClusterName) != len(clusterInfo) {
panic("Cluster info initial versions have duplicates")
}

return &metadataImpl{
Expand All @@ -127,7 +92,7 @@ func NewMetadata(
failoverVersionIncrement: failoverVersionIncrement,
primaryClusterName: primaryClusterName,
currentClusterName: currentClusterName,
clusterInfo: clusterInfo,
clusterGroup: clusterGroup,
versionToClusterName: versionToClusterName,
}
}
Expand All @@ -140,12 +105,12 @@ func (metadata *metadataImpl) IsGlobalDomainEnabled() bool {

// GetNextFailoverVersion return the next failover version based on input
func (metadata *metadataImpl) GetNextFailoverVersion(cluster string, currentFailoverVersion int64) int64 {
info, ok := metadata.clusterInfo[cluster]
info, ok := metadata.clusterGroup[cluster]
if !ok {
panic(fmt.Sprintf(
"Unknown cluster name: %v with given cluster initial failover version map: %v.",
cluster,
metadata.clusterInfo,
metadata.clusterGroup,
))
}
failoverVersion := currentFailoverVersion/metadata.failoverVersionIncrement*metadata.failoverVersionIncrement + info.InitialFailoverVersion
Expand Down Expand Up @@ -176,7 +141,7 @@ func (metadata *metadataImpl) GetCurrentClusterName() string {

// GetAllClusterInfo return the all cluster name -> corresponding information
func (metadata *metadataImpl) GetAllClusterInfo() map[string]config.ClusterInformation {
return metadata.clusterInfo
return metadata.clusterGroup
}

// ClusterNameForFailoverVersion return the corresponding cluster name for a given failover version
Expand All @@ -191,7 +156,7 @@ func (metadata *metadataImpl) ClusterNameForFailoverVersion(failoverVersion int6
panic(fmt.Sprintf(
"Unknown initial failover version %v with given cluster initial failover version map: %v and failover version increment %v.",
initialFailoverVersion,
metadata.clusterInfo,
metadata.clusterGroup,
metadata.failoverVersionIncrement,
))
}
Expand Down
147 changes: 147 additions & 0 deletions common/config/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package config

import (
"errors"
"fmt"
"log"

"go.uber.org/multierr"
)

type (
// ClusterGroupMetadata contains all the clusters participating in a replication group(aka XDC/GlobalDomain)
ClusterGroupMetadata struct {
EnableGlobalDomain bool `yaml:"enableGlobalDomain"`
// FailoverVersionIncrement is the increment of each cluster version when failover happens
// It decides the maximum number clusters in this replication groups
FailoverVersionIncrement int64 `yaml:"failoverVersionIncrement"`
// PrimaryClusterName is the primary cluster name, only the primary cluster can register / update domain
// all clusters can do domain failover
PrimaryClusterName string `yaml:"primaryClusterName"`
// Deprecated: please use PrimaryClusterName
MasterClusterName string `yaml:"masterClusterName"`
// CurrentClusterName is the name of the cluster of current deployment
CurrentClusterName string `yaml:"currentClusterName"`
// ClusterGroup contains information for each cluster within the replication group
// Key is the clusterName
ClusterGroup map[string]ClusterInformation `yaml:"clusterGroup"`
// Deprecated: please use ClusterGroup
ClusterInformation map[string]ClusterInformation `yaml:"clusterInformation"`
Copy link
Contributor

@longquanzheng longquanzheng Aug 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to "ReplicationGroup"?

e.g. ReplicationGroup map[string]ClusterInformation

Or maybe "ClusterGroup" as it may not do any replication in one cluster setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, having the same name for both map name and item within that map does not seem right.

ClusterGroup seems reasonable. Or maybe just simply Clusters?
I think it would also make sense to rename ClusterMetadata to (maybe) ClusterGroupMetadata as it contains config for multiple clusters not just single one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having "Group" explicitly is more friendly as the "s' is quite hard to read/recognize.

Yes, I love the idea of ClusterGroupMetadata

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed:

  • ClusterInformation to ClusterGroup
  • ClusterMetadata to ClusterGroupMetadata
    This also means introducing new fields in yaml deprecating old ones. Otherwise having them different could lead to some confusion.

}

// ClusterInformation contains the information about each cluster participating in cross DC
ClusterInformation struct {
Enabled bool `yaml:"enabled"`
// InitialFailoverVersion is the identifier of each cluster. 0 <= the value < failoverVersionIncrement
InitialFailoverVersion int64 `yaml:"initialFailoverVersion"`
// RPCName indicate the remote service name
RPCName string `yaml:"rpcName"`
// Address indicate the remote service address(Host:Port). Host can be DNS name.
// For currentCluster, it's usually the same as publicClient.hostPort
RPCAddress string `yaml:"rpcAddress" validate:"nonzero"`
}
)

func (m *ClusterGroupMetadata) validate() error {
if m == nil {
return errors.New("ClusterGroupMetadata cannot be empty")
}

if !m.EnableGlobalDomain {
log.Println("[WARN] Local domain is now deprecated. Please update config to enable global domain(ClusterGroupMetadata->EnableGlobalDomain)." +
"Global domain of single cluster has zero overhead, but only advantages for future migration and fail over. Please check Cadence documentation for more details.")
}

var errs error

if len(m.PrimaryClusterName) == 0 {
errs = multierr.Append(errs, errors.New("primary cluster name is empty"))
}
if len(m.CurrentClusterName) == 0 {
errs = multierr.Append(errs, errors.New("current cluster name is empty"))
}
if m.FailoverVersionIncrement == 0 {
errs = multierr.Append(errs, errors.New("version increment is 0"))
}
if len(m.ClusterGroup) == 0 {
errs = multierr.Append(errs, errors.New("empty cluster group"))
}
if _, ok := m.ClusterGroup[m.PrimaryClusterName]; len(m.PrimaryClusterName) > 0 && !ok {
errs = multierr.Append(errs, errors.New("primary cluster is not specified in the cluster group"))
}
if _, ok := m.ClusterGroup[m.CurrentClusterName]; len(m.CurrentClusterName) > 0 && !ok {
errs = multierr.Append(errs, errors.New("current cluster is not specified in the cluster group"))
}

versionToClusterName := make(map[int64]string)
for clusterName, info := range m.ClusterGroup {
if len(clusterName) == 0 {
errs = multierr.Append(errs, errors.New("cluster with empty name defined"))
}
versionToClusterName[info.InitialFailoverVersion] = clusterName

if m.FailoverVersionIncrement <= info.InitialFailoverVersion || info.InitialFailoverVersion < 0 {
errs = multierr.Append(errs, fmt.Errorf(
"cluster %s: version increment %v is smaller than initial version: %v",
clusterName,
m.FailoverVersionIncrement,
info.InitialFailoverVersion,
))
}

if info.Enabled && (len(info.RPCName) == 0 || len(info.RPCAddress) == 0) {
errs = multierr.Append(errs, fmt.Errorf("cluster %v: rpc name / address is empty", clusterName))
}
}
if len(versionToClusterName) != len(m.ClusterGroup) {
errs = multierr.Append(errs, errors.New("initial versions of the cluster group have duplicates"))
}

return errs
}

func (m *ClusterGroupMetadata) fillDefaults() {
if m == nil {
return
}

// TODO: remove this after 0.23 and mention a breaking change in config.
if len(m.PrimaryClusterName) == 0 && len(m.MasterClusterName) != 0 {
m.PrimaryClusterName = m.MasterClusterName
log.Println("[WARN] masterClusterName config is deprecated. Please replace it with primaryClusterName.")
}

// TODO: remove this after 0.23 and mention a breaking change in config.
if len(m.ClusterGroup) == 0 && len(m.ClusterInformation) != 0 {
m.ClusterGroup = m.ClusterInformation
log.Println("[WARN] clusterInformation config is deprecated. Please replace it with clusterGroup.")
}

for name, cluster := range m.ClusterGroup {
if cluster.RPCName == "" {
// filling RPCName with a default value if empty
cluster.RPCName = "cadence-frontend"
m.ClusterGroup[name] = cluster
}
}
}
Loading