Skip to content

Commit

Permalink
Validate archival provider config on startup (uber#2376)
Browse files Browse the repository at this point in the history
  • Loading branch information
yycptt authored and andrewjdawson2016 committed Aug 14, 2019
1 parent 8d01472 commit 82d4c89
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
13 changes: 1 addition & 12 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,7 @@ func (s *server) startService() common.Daemon {
&s.cfg.DomainDefaults.Archival,
)

configuredForHistoryArchival := params.ArchivalMetadata.GetHistoryConfig().ClusterConfiguredForArchival()
historyArchiverProviderCfg := s.cfg.Archival.History.Provider
if (configuredForHistoryArchival && historyArchiverProviderCfg == nil) || (!configuredForHistoryArchival && historyArchiverProviderCfg != nil) {
log.Fatalf("invalid history archival config")
}

configuredForVisibilityArchival := params.ArchivalMetadata.GetVisibilityConfig().ClusterConfiguredForArchival()
visibilityArchiverProviderCfg := s.cfg.Archival.Visibility.Provider
if (configuredForVisibilityArchival && visibilityArchiverProviderCfg == nil) || (!configuredForVisibilityArchival && visibilityArchiverProviderCfg != nil) {
log.Fatalf("invalid visibility archival config")
}
params.ArchiverProvider = provider.NewArchiverProvider(historyArchiverProviderCfg, visibilityArchiverProviderCfg)
params.ArchiverProvider = provider.NewArchiverProvider(s.cfg.Archival.History.Provider, s.cfg.Archival.Visibility.Provider)

params.PersistenceConfig.TransactionSizeLimit = dc.GetIntProperty(dynamicconfig.TransactionSizeLimit, common.DefaultTransactionSizeLimit)

Expand Down
24 changes: 7 additions & 17 deletions common/archiver/archivalMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/service/config"
"github.com/uber/cadence/common/service/dynamicconfig"
)
Expand Down Expand Up @@ -146,16 +147,12 @@ func NewArchivalConfig(
panic(err)
}

ac := &archivalConfig{
return &archivalConfig{
clusterStatus: clusterStatus,
enableRead: enableRead,
domainDefaultStatus: domainDefaultStatus,
domainDefaultURI: domainDefaultURI,
}
if !ac.isValid() {
panic("invalid cluster level archival configuration")
}
return ac
}

// NewDisabledArchvialConfig returns a disabled ArchivalConfig
Expand Down Expand Up @@ -189,21 +186,14 @@ func (a *archivalConfig) GetDomainDefaultURI() string {
return a.domainDefaultURI
}

func (a *archivalConfig) isValid() bool {
URISet := len(a.domainDefaultURI) != 0
validEnabled := a.ClusterConfiguredForArchival() && URISet
validDisabled := !a.ClusterConfiguredForArchival() && !a.enableRead && a.domainDefaultStatus == shared.ArchivalStatusDisabled && !URISet
return validEnabled || validDisabled
}

func getClusterArchivalStatus(str string) (ArchivalStatus, error) {
str = strings.TrimSpace(strings.ToLower(str))
switch str {
case "", "disabled":
case "", common.ArchivalDisabled:
return ArchivalDisabled, nil
case "paused":
case common.ArchivalPaused:
return ArchivalPaused, nil
case "enabled":
case common.ArchivalEnabled:
return ArchivalEnabled, nil
}
return ArchivalDisabled, fmt.Errorf("invalid archival status of %v for cluster, valid status are: {\"\", \"disabled\", \"paused\", \"enabled\"}", str)
Expand All @@ -212,9 +202,9 @@ func getClusterArchivalStatus(str string) (ArchivalStatus, error) {
func getDomainArchivalStatus(str string) (shared.ArchivalStatus, error) {
str = strings.TrimSpace(strings.ToLower(str))
switch str {
case "", "disabled":
case "", common.ArchivalDisabled:
return shared.ArchivalStatusDisabled, nil
case "enabled":
case common.ArchivalEnabled:
return shared.ArchivalStatusEnabled, nil
}
return shared.ArchivalStatusDisabled, fmt.Errorf("invalid archival status of %v for domain, valid status are: {\"\", \"disabled\", \"enabled\"}", str)
Expand Down
9 changes: 9 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ const (
// DefaultTransactionSizeLimit is the largest allowed transaction size to persistence
DefaultTransactionSizeLimit = 14 * 1024 * 1024
)

const (
// ArchivalEnabled is the status for enabling archival
ArchivalEnabled = "enabled"
// ArchivalDisabled is the status for disabling archival
ArchivalDisabled = "disabled"
// ArchivalPaused is the status for pausing archival
ArchivalPaused = "paused"
)
55 changes: 55 additions & 0 deletions common/service/config/archival.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2019 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"

"github.com/uber/cadence/common"
)

// Validate validates the archival config
func (a *Archival) Validate(domainDefaults *ArchivalDomainDefaults) error {
if !isArchivalConfigValid(a.History.Status, a.History.EnableRead, domainDefaults.History.Status, domainDefaults.History.URI, a.History.Provider != nil) {
return errors.New("Invalid history archival config")
}

if !isArchivalConfigValid(a.Visibility.Status, a.Visibility.EnableRead, domainDefaults.Visibility.Status, domainDefaults.Visibility.URI, a.Visibility.Provider != nil) {
return errors.New("Invalid visibility archival config")
}

return nil
}

func isArchivalConfigValid(
clusterStatus string,
enableRead bool,
domainDefaultStatus string,
domianDefaultURI string,
specifiedProvider bool,
) bool {
archivalEnabled := clusterStatus == common.ArchivalEnabled
URISet := len(domianDefaultURI) != 0

validEnable := archivalEnabled && URISet && specifiedProvider
validDisabled := !archivalEnabled && !enableRead && domainDefaultStatus != common.ArchivalEnabled && !URISet && !specifiedProvider
return validEnable || validDisabled
}
5 changes: 4 additions & 1 deletion common/service/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ type (

// Validate validates this config
func (c *Config) Validate() error {
return c.Persistence.Validate()
if err := c.Persistence.Validate(); err != nil {
return err
}
return c.Archival.Validate(&c.DomainDefaults.Archival)
}

// String converts the config object into a string
Expand Down

0 comments on commit 82d4c89

Please sign in to comment.