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

Make cluster.Metadata a struct and stop using mocks for it #4851

Merged
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
66 changes: 21 additions & 45 deletions common/cluster/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination metadata_mock.go

package cluster

import (
Expand All @@ -31,24 +29,7 @@ import (

type (
// Metadata provides information about clusters
Metadata interface {
// IsPrimaryCluster whether current cluster is the primary cluster
IsPrimaryCluster() bool
// GetNextFailoverVersion return the next failover version for domain failover
GetNextFailoverVersion(string, int64) int64
// IsVersionFromSameCluster return true if 2 version are used for the same cluster
IsVersionFromSameCluster(version1 int64, version2 int64) bool
// GetPrimaryClusterName return the primary cluster name
GetPrimaryClusterName() string
// GetCurrentClusterName return the current cluster name
GetCurrentClusterName() string
// GetAllClusterInfo return the all cluster name -> corresponding info
GetAllClusterInfo() map[string]config.ClusterInformation
// ClusterNameForFailoverVersion return the corresponding cluster name for a given failover version
ClusterNameForFailoverVersion(failoverVersion int64) string
}

metadataImpl struct {
Metadata struct {
// failoverVersionIncrement is the increment of each cluster's version when failover happen
failoverVersionIncrement int64
// primaryClusterName is the name of the primary cluster, only the primary cluster can register / update domain
Expand All @@ -75,7 +56,7 @@ func NewMetadata(
versionToClusterName[info.InitialFailoverVersion] = clusterName
}

return &metadataImpl{
return Metadata{
failoverVersionIncrement: failoverVersionIncrement,
primaryClusterName: primaryClusterName,
currentClusterName: currentClusterName,
Expand All @@ -85,60 +66,55 @@ func NewMetadata(
}

// GetNextFailoverVersion return the next failover version based on input
func (metadata *metadataImpl) GetNextFailoverVersion(cluster string, currentFailoverVersion int64) int64 {
info, ok := metadata.clusterGroup[cluster]
func (m Metadata) GetNextFailoverVersion(cluster string, currentFailoverVersion int64) int64 {
info, ok := m.clusterGroup[cluster]
if !ok {
panic(fmt.Sprintf(
"Unknown cluster name: %v with given cluster initial failover version map: %v.",
cluster,
metadata.clusterGroup,
m.clusterGroup,
))
}
failoverVersion := currentFailoverVersion/metadata.failoverVersionIncrement*metadata.failoverVersionIncrement + info.InitialFailoverVersion
failoverVersion := currentFailoverVersion/m.failoverVersionIncrement*m.failoverVersionIncrement + info.InitialFailoverVersion
if failoverVersion < currentFailoverVersion {
return failoverVersion + metadata.failoverVersionIncrement
return failoverVersion + m.failoverVersionIncrement
}
return failoverVersion
}

// IsVersionFromSameCluster return true if 2 version are used for the same cluster
func (metadata *metadataImpl) IsVersionFromSameCluster(version1 int64, version2 int64) bool {
return (version1-version2)%metadata.failoverVersionIncrement == 0
}

func (metadata *metadataImpl) IsPrimaryCluster() bool {
return metadata.primaryClusterName == metadata.currentClusterName
func (m Metadata) IsVersionFromSameCluster(version1 int64, version2 int64) bool {
return (version1-version2)%m.failoverVersionIncrement == 0
}

// GetPrimaryClusterName return the primary cluster name
func (metadata *metadataImpl) GetPrimaryClusterName() string {
return metadata.primaryClusterName
func (m Metadata) IsPrimaryCluster() bool {
return m.primaryClusterName == m.currentClusterName
}

// GetCurrentClusterName return the current cluster name
func (metadata *metadataImpl) GetCurrentClusterName() string {
return metadata.currentClusterName
func (m Metadata) GetCurrentClusterName() string {
return m.currentClusterName
}

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

// ClusterNameForFailoverVersion return the corresponding cluster name for a given failover version
func (metadata *metadataImpl) ClusterNameForFailoverVersion(failoverVersion int64) string {
func (m Metadata) ClusterNameForFailoverVersion(failoverVersion int64) string {
if failoverVersion == common.EmptyVersion {
return metadata.currentClusterName
return m.currentClusterName
}

initialFailoverVersion := failoverVersion % metadata.failoverVersionIncrement
clusterName, ok := metadata.versionToClusterName[initialFailoverVersion]
initialFailoverVersion := failoverVersion % m.failoverVersionIncrement
clusterName, ok := m.versionToClusterName[initialFailoverVersion]
if !ok {
panic(fmt.Sprintf(
"Unknown initial failover version %v with given cluster initial failover version map: %v and failover version increment %v.",
initialFailoverVersion,
metadata.clusterGroup,
metadata.failoverVersionIncrement,
m.clusterGroup,
m.failoverVersionIncrement,
))
}
return clusterName
Expand Down
16 changes: 16 additions & 0 deletions common/cluster/metadataTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ var (
RPCTransport: TestClusterXDCTransport,
},
}

// TestActiveClusterMetadata is metadata for an active cluster
TestActiveClusterMetadata = NewMetadata(
TestFailoverVersionIncrement,
TestCurrentClusterName,
TestCurrentClusterName,
TestAllClusterInfo,
)

// TestPassiveClusterMetadata is metadata for a passive cluster
TestPassiveClusterMetadata = NewMetadata(
TestFailoverVersionIncrement,
TestCurrentClusterName,
TestAlternativeClusterName,
TestAllClusterInfo,
)
)

// GetTestClusterMetadata return an cluster metadata instance, which is initialized
Expand Down
156 changes: 0 additions & 156 deletions common/cluster/metadata_mock.go

This file was deleted.

27 changes: 3 additions & 24 deletions common/domain/attrValidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/suite"

"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/mocks"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/types"
)
Expand All @@ -35,9 +34,8 @@ type (
attrValidatorSuite struct {
suite.Suite

minRetentionDays int
mockClusterMetadata *mocks.ClusterMetadata
validator *AttrValidatorImpl
minRetentionDays int
validator *AttrValidatorImpl
}
)

Expand All @@ -54,8 +52,7 @@ func (s *attrValidatorSuite) TearDownSuite() {

func (s *attrValidatorSuite) SetupTest() {
s.minRetentionDays = 1
s.mockClusterMetadata = &mocks.ClusterMetadata{}
s.validator = newAttrValidator(s.mockClusterMetadata, int32(s.minRetentionDays))
s.validator = newAttrValidator(cluster.TestActiveClusterMetadata, int32(s.minRetentionDays))
}

func (s *attrValidatorSuite) TearDownTest() {
Expand Down Expand Up @@ -88,10 +85,6 @@ func (s *attrValidatorSuite) TestValidateConfigRetentionPeriod() {
}

func (s *attrValidatorSuite) TestClusterName() {
s.mockClusterMetadata.On("GetAllClusterInfo").Return(
cluster.TestAllClusterInfo,
)

err := s.validator.validateClusterName("some random foo bar")
s.IsType(&types.BadRequestError{}, err)

Expand All @@ -103,13 +96,6 @@ func (s *attrValidatorSuite) TestClusterName() {
}

func (s *attrValidatorSuite) TestValidateDomainReplicationConfigForLocalDomain() {
s.mockClusterMetadata.On("GetCurrentClusterName").Return(
cluster.TestCurrentClusterName,
)
s.mockClusterMetadata.On("GetAllClusterInfo").Return(
cluster.TestAllClusterInfo,
)

err := s.validator.validateDomainReplicationConfigForLocalDomain(
&persistence.DomainReplicationConfig{
ActiveClusterName: cluster.TestAlternativeClusterName,
Expand Down Expand Up @@ -154,13 +140,6 @@ func (s *attrValidatorSuite) TestValidateDomainReplicationConfigForLocalDomain()
}

func (s *attrValidatorSuite) TestValidateDomainReplicationConfigForGlobalDomain() {
s.mockClusterMetadata.On("GetCurrentClusterName").Return(
cluster.TestCurrentClusterName,
)
s.mockClusterMetadata.On("GetAllClusterInfo").Return(
cluster.TestAllClusterInfo,
)

err := s.validator.validateDomainReplicationConfigForGlobalDomain(
&persistence.DomainReplicationConfig{
ActiveClusterName: cluster.TestCurrentClusterName,
Expand Down
Loading