Skip to content

Commit

Permalink
Reshuffle service.Config and resource.Params (cadence-workflow#4514)
Browse files Browse the repository at this point in the history
  • Loading branch information
vytautas-karpavicius authored Sep 28, 2021
1 parent a590cac commit 467824e
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 131 deletions.
3 changes: 2 additions & 1 deletion cmd/server/cadence/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/messaging/kafka"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/resource"
"github.com/uber/cadence/common/rpc"
"github.com/uber/cadence/common/service"
"github.com/uber/cadence/service/frontend"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (s *server) Stop() {

// startService starts a service with the given name and config
func (s *server) startService() common.Daemon {
params := service.BootstrapParams{}
params := resource.Params{}
params.Name = "cadence-" + s.name

zapLogger, err := s.cfg.Log.NewZapLogger()
Expand Down
20 changes: 16 additions & 4 deletions common/persistence/client/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ package client
import (
"sync"

"github.com/uber/cadence/common/config"
cconfig "github.com/uber/cadence/common/config"
es "github.com/uber/cadence/common/elasticsearch"
"github.com/uber/cadence/common/messaging"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/resource/config"
"github.com/uber/cadence/common/service"
)

Expand Down Expand Up @@ -75,13 +78,22 @@ type (
sync.RWMutex
shardIDToExecutionManager map[int]persistence.ExecutionManager
}

// Params contains dependencies for persistence
Params struct {
PersistenceConfig config.Persistence
MetricsClient metrics.Client
MessagingClient messaging.Client
ESClient es.GenericClient
ESConfig *config.ElasticSearchConfig
}
)

// NewBeanFromFactory crate a new store bean using factory
func NewBeanFromFactory(
factory Factory,
params *service.BootstrapParams,
resourceConfig *config.ResourceConfig,
params *Params,
serviceConfig *service.Config,
) (*BeanImpl, error) {

metadataMgr, err := factory.NewDomainManager()
Expand All @@ -94,7 +106,7 @@ func NewBeanFromFactory(
return nil, err
}

visibilityMgr, err := factory.NewVisibilityManager(params, resourceConfig)
visibilityMgr, err := factory.NewVisibilityManager(params, serviceConfig)
if err != nil {
return nil, err
}
Expand Down
11 changes: 5 additions & 6 deletions common/persistence/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/uber/cadence/common/persistence/serialization"
"github.com/uber/cadence/common/persistence/sql"
"github.com/uber/cadence/common/quotas"
rc "github.com/uber/cadence/common/resource/config"
"github.com/uber/cadence/common/service"
)

Expand All @@ -59,7 +58,7 @@ type (
// NewExecutionManager returns a new execution manager for a given shardID
NewExecutionManager(shardID int) (p.ExecutionManager, error)
// NewVisibilityManager returns a new visibility manager
NewVisibilityManager(params *service.BootstrapParams, resourceConfig *rc.ResourceConfig) (p.VisibilityManager, error)
NewVisibilityManager(params *Params, serviceConfig *service.Config) (p.VisibilityManager, error)
// NewDomainReplicationQueueManager returns a new queue for domain replication
NewDomainReplicationQueueManager() (p.QueueManager, error)
// NewConfigStoreManager returns a new config store manager
Expand Down Expand Up @@ -257,8 +256,8 @@ func (f *factoryImpl) NewExecutionManager(shardID int) (p.ExecutionManager, erro

// NewVisibilityManager returns a new visibility manager
func (f *factoryImpl) NewVisibilityManager(
params *service.BootstrapParams,
resourceConfig *rc.ResourceConfig,
params *Params,
resourceConfig *service.Config,
) (p.VisibilityManager, error) {
if resourceConfig.EnableReadVisibilityFromES == nil && resourceConfig.AdvancedVisibilityWritingMode == nil {
// No need to create visibility manager as no read/write needed
Expand Down Expand Up @@ -297,7 +296,7 @@ func (f *factoryImpl) NewVisibilityManager(
func newESVisibilityManager(
indexName string,
esClient es.GenericClient,
visibilityConfig *rc.ResourceConfig,
visibilityConfig *service.Config,
producer messaging.Producer,
metricsClient metrics.Client,
log log.Logger,
Expand All @@ -324,7 +323,7 @@ func newESVisibilityManager(
}

func (f *factoryImpl) newDBVisibilityManager(
visibilityConfig *rc.ResourceConfig,
visibilityConfig *service.Config,
) (p.VisibilityManager, error) {
enableReadFromClosedExecutionV2 := false
if visibilityConfig.EnableReadDBVisibilityFromClosedExecutionV2 != nil {
Expand Down
6 changes: 3 additions & 3 deletions common/persistence/elasticsearch/esVisibilityStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/messaging"
p "github.com/uber/cadence/common/persistence"
rc "github.com/uber/cadence/common/resource/config"
"github.com/uber/cadence/common/service"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/common/types/mapper/thrift"
)
Expand All @@ -58,7 +58,7 @@ type (
index string
producer messaging.Producer
logger log.Logger
config *rc.ResourceConfig
config *service.Config
}
)

Expand All @@ -69,7 +69,7 @@ func NewElasticSearchVisibilityStore(
esClient es.GenericClient,
index string,
producer messaging.Producer,
config *rc.ResourceConfig,
config *service.Config,
logger log.Logger,
) p.VisibilityStore {
return &esVisibilityStore{
Expand Down
4 changes: 2 additions & 2 deletions common/persistence/elasticsearch/esVisibilityStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
"github.com/uber/cadence/common/log/loggerimpl"
"github.com/uber/cadence/common/mocks"
p "github.com/uber/cadence/common/persistence"
rc "github.com/uber/cadence/common/resource/config"
"github.com/uber/cadence/common/service"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/common/types/mapper/thrift"
)
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *ESVisibilitySuite) SetupTest() {
s.Assertions = require.New(s.T())

s.mockESClient = &esMocks.GenericClient{}
config := &rc.ResourceConfig{
config := &service.Config{
ESIndexMaxResultWindow: dynamicconfig.GetIntPropertyFn(esIndexMaxResultWindow),
ValidSearchAttributes: dynamicconfig.GetMapPropertyFn(definition.GetDefaultIndexedKeys()),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/uber/cadence/common/dynamicconfig"
p "github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/persistence/client"
rc "github.com/uber/cadence/common/resource/config"
"github.com/uber/cadence/common/service"
"github.com/uber/cadence/common/types"
)
Expand Down Expand Up @@ -68,12 +67,12 @@ func (s *DBVisibilityPersistenceSuite) SetupSuite() {
// SQL currently doesn't have support for visibility manager
var err error
s.VisibilityMgr, err = visibilityFactory.NewVisibilityManager(
&service.BootstrapParams{
&client.Params{
PersistenceConfig: config.Persistence{
VisibilityStore: "something not empty",
},
},
&rc.ResourceConfig{
&service.Config{
EnableReadVisibilityFromES: dynamicconfig.GetBoolPropertyFnFilteredByDomain(false),
AdvancedVisibilityWritingMode: dynamicconfig.GetStringPropertyFn(common.AdvancedVisibilityWritingModeOff),
EnableReadDBVisibilityFromClosedExecutionV2: dynamicconfig.GetBoolPropertyFn(false),
Expand Down
89 changes: 89 additions & 0 deletions common/resource/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) 2017 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 resource

import (
"github.com/uber-go/tally"
"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/archiver"
"github.com/uber/cadence/common/archiver/provider"
"github.com/uber/cadence/common/authorization"
"github.com/uber/cadence/common/blobstore"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/config"
"github.com/uber/cadence/common/dynamicconfig"
es "github.com/uber/cadence/common/elasticsearch"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/membership"
"github.com/uber/cadence/common/messaging"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/rpc"
)

type (
// Params holds the set of parameters needed to initialize common service resources
Params struct {
Name string
InstanceID string
Logger log.Logger
ThrottledLogger log.Logger

MetricScope tally.Scope
MembershipFactory MembershipMonitorFactory
RPCFactory common.RPCFactory
PProfInitializer common.PProfInitializer
PersistenceConfig config.Persistence
ClusterMetadata cluster.Metadata
ReplicatorConfig config.Replicator
MetricsClient metrics.Client
MessagingClient messaging.Client
BlobstoreClient blobstore.Client
ESClient es.GenericClient
ESConfig *config.ElasticSearchConfig
DynamicConfig dynamicconfig.Client
DispatcherProvider rpc.DispatcherProvider
DCRedirectionPolicy config.DCRedirectionPolicy
PublicClient workflowserviceclient.Interface
ArchivalMetadata archiver.ArchivalMetadata
ArchiverProvider provider.ArchiverProvider
Authorizer authorization.Authorizer // NOTE: this can be nil. If nil, AccessControlledHandlerImpl will initiate one with config.Authorization
AuthorizationConfig config.Authorization // NOTE: empty(default) struct will get a authorization.NoopAuthorizer
}

// MembershipMonitorFactory provides a bootstrapped membership monitor
MembershipMonitorFactory interface {
// GetMembershipMonitor return a membership monitor
GetMembershipMonitor() (membership.Monitor, error)
}
)

// UpdateLoggerWithServiceName tag logging with service name from the top level
func (params *Params) UpdateLoggerWithServiceName(name string) {
if params.Logger != nil {
params.Logger = params.Logger.WithTags(tag.Service(name))
}
if params.ThrottledLogger != nil {
params.ThrottledLogger = params.ThrottledLogger.WithTags(tag.Service(name))
}
}
25 changes: 15 additions & 10 deletions common/resource/resourceImpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import (
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
persistenceClient "github.com/uber/cadence/common/persistence/client"
"github.com/uber/cadence/common/resource/config"
"github.com/uber/cadence/common/service"
)

Expand Down Expand Up @@ -124,7 +123,7 @@ type (

pprofInitializer common.PProfInitializer
runtimeMetricsReporter *metrics.RuntimeMetricsReporter
membershipFactory service.MembershipMonitorFactory
membershipFactory MembershipMonitorFactory
rpcFactory common.RPCFactory
}
)
Expand All @@ -133,13 +132,13 @@ var _ Resource = (*Impl)(nil)

// New create a new resource containing common dependencies
func New(
params *service.BootstrapParams,
params *Params,
serviceName string,
resourceConfig *config.ResourceConfig,
serviceConfig *service.Config,
) (impl *Impl, retError error) {

logger := params.Logger
throttledLogger := loggerimpl.NewThrottledLogger(logger, resourceConfig.ThrottledLoggerMaxRPS)
throttledLogger := loggerimpl.NewThrottledLogger(logger, serviceConfig.ThrottledLoggerMaxRPS)

numShards := params.PersistenceConfig.NumHistoryShards
hostName, err := os.Hostname()
Expand Down Expand Up @@ -198,19 +197,25 @@ func New(
persistenceBean, err := persistenceClient.NewBeanFromFactory(persistenceClient.NewFactory(
&params.PersistenceConfig,
func(...dynamicconfig.FilterOption) int {
if resourceConfig.PersistenceGlobalMaxQPS() > 0 {
if serviceConfig.PersistenceGlobalMaxQPS() > 0 {
ringSize, err := membershipMonitor.GetMemberCount(serviceName)
if err == nil && ringSize > 0 {
avgQuota := common.MaxInt(resourceConfig.PersistenceGlobalMaxQPS()/ringSize, 1)
return common.MinInt(avgQuota, resourceConfig.PersistenceMaxQPS())
avgQuota := common.MaxInt(serviceConfig.PersistenceGlobalMaxQPS()/ringSize, 1)
return common.MinInt(avgQuota, serviceConfig.PersistenceMaxQPS())
}
}
return resourceConfig.PersistenceMaxQPS()
return serviceConfig.PersistenceMaxQPS()
},
params.ClusterMetadata.GetCurrentClusterName(),
params.MetricsClient,
logger,
), params, resourceConfig)
), &persistenceClient.Params{
PersistenceConfig: params.PersistenceConfig,
MetricsClient: params.MetricsClient,
MessagingClient: params.MessagingClient,
ESClient: params.ESClient,
ESConfig: params.ESConfig,
}, serviceConfig)
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions common/resource/config/config.go → common/service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package config
package service

import "github.com/uber/cadence/common/dynamicconfig"

type (
// ResourceConfig is a subset of the service dynamic config for resource
// It's under a separate package to avoid circular dependency
ResourceConfig struct {
// Config is a subset of the service dynamic config for single service
Config struct {
PersistenceMaxQPS dynamicconfig.IntPropertyFn
PersistenceGlobalMaxQPS dynamicconfig.IntPropertyFn
ThrottledLoggerMaxRPS dynamicconfig.IntPropertyFn
Expand Down
Loading

0 comments on commit 467824e

Please sign in to comment.