go.temporal.io/server@v1.23.0/common/persistence/client/fx.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package client
    26  
    27  import (
    28  	"time"
    29  
    30  	"go.uber.org/fx"
    31  
    32  	"go.temporal.io/server/common/cluster"
    33  	"go.temporal.io/server/common/config"
    34  	"go.temporal.io/server/common/dynamicconfig"
    35  	"go.temporal.io/server/common/log"
    36  	"go.temporal.io/server/common/metrics"
    37  	"go.temporal.io/server/common/persistence"
    38  	"go.temporal.io/server/common/persistence/serialization"
    39  	"go.temporal.io/server/common/primitives"
    40  	"go.temporal.io/server/common/quotas"
    41  )
    42  
    43  type (
    44  	PersistenceMaxQps                  dynamicconfig.IntPropertyFn
    45  	PersistenceNamespaceMaxQps         dynamicconfig.IntPropertyFnWithNamespaceFilter
    46  	PersistencePerShardNamespaceMaxQPS dynamicconfig.IntPropertyFnWithNamespaceFilter
    47  	EnablePriorityRateLimiting         dynamicconfig.BoolPropertyFn
    48  	OperatorRPSRatio                   dynamicconfig.FloatPropertyFn
    49  
    50  	DynamicRateLimitingParams dynamicconfig.MapPropertyFn
    51  
    52  	ClusterName string
    53  
    54  	NewFactoryParams struct {
    55  		fx.In
    56  
    57  		DataStoreFactory                   DataStoreFactory
    58  		EventBlobCache                     persistence.XDCCache
    59  		Cfg                                *config.Persistence
    60  		PersistenceMaxQPS                  PersistenceMaxQps
    61  		PersistenceNamespaceMaxQPS         PersistenceNamespaceMaxQps
    62  		PersistencePerShardNamespaceMaxQPS PersistencePerShardNamespaceMaxQPS
    63  		EnablePriorityRateLimiting         EnablePriorityRateLimiting
    64  		OperatorRPSRatio                   OperatorRPSRatio
    65  		ClusterName                        ClusterName
    66  		ServiceName                        primitives.ServiceName
    67  		MetricsHandler                     metrics.Handler
    68  		Logger                             log.Logger
    69  		HealthSignals                      persistence.HealthSignalAggregator
    70  		DynamicRateLimitingParams          DynamicRateLimitingParams
    71  	}
    72  
    73  	FactoryProviderFn func(NewFactoryParams) Factory
    74  )
    75  
    76  var Module = fx.Options(
    77  	BeanModule,
    78  	fx.Provide(ClusterNameProvider),
    79  	fx.Provide(DataStoreFactoryProvider),
    80  	fx.Provide(HealthSignalAggregatorProvider),
    81  	fx.Provide(EventBlobCacheProvider),
    82  )
    83  
    84  func ClusterNameProvider(config *cluster.Config) ClusterName {
    85  	return ClusterName(config.CurrentClusterName)
    86  }
    87  
    88  func EventBlobCacheProvider(
    89  	dc *dynamicconfig.Collection,
    90  ) persistence.XDCCache {
    91  	return persistence.NewEventsBlobCache(
    92  		dc.GetIntProperty(dynamicconfig.XDCCacheMaxSizeBytes, 8*1024*1024)(),
    93  		20*time.Second,
    94  	)
    95  }
    96  
    97  func FactoryProvider(
    98  	params NewFactoryParams,
    99  ) Factory {
   100  	var requestRatelimiter quotas.RequestRateLimiter
   101  	if params.PersistenceMaxQPS != nil && params.PersistenceMaxQPS() > 0 {
   102  		if params.EnablePriorityRateLimiting != nil && params.EnablePriorityRateLimiting() {
   103  			requestRatelimiter = NewPriorityRateLimiter(
   104  				params.PersistenceNamespaceMaxQPS,
   105  				params.PersistenceMaxQPS,
   106  				params.PersistencePerShardNamespaceMaxQPS,
   107  				RequestPriorityFn,
   108  				params.OperatorRPSRatio,
   109  				params.HealthSignals,
   110  				params.DynamicRateLimitingParams,
   111  				params.MetricsHandler,
   112  				params.Logger,
   113  			)
   114  		} else {
   115  			requestRatelimiter = NewNoopPriorityRateLimiter(params.PersistenceMaxQPS)
   116  		}
   117  	}
   118  
   119  	return NewFactory(
   120  		params.DataStoreFactory,
   121  		params.Cfg,
   122  		requestRatelimiter,
   123  		serialization.NewSerializer(),
   124  		params.EventBlobCache,
   125  		string(params.ClusterName),
   126  		params.MetricsHandler,
   127  		params.Logger,
   128  		params.HealthSignals,
   129  	)
   130  }
   131  
   132  func HealthSignalAggregatorProvider(
   133  	dynamicCollection *dynamicconfig.Collection,
   134  	metricsHandler metrics.Handler,
   135  	logger log.ThrottledLogger,
   136  ) persistence.HealthSignalAggregator {
   137  	if dynamicCollection.GetBoolProperty(dynamicconfig.PersistenceHealthSignalMetricsEnabled, true)() {
   138  		return persistence.NewHealthSignalAggregatorImpl(
   139  			dynamicCollection.GetBoolProperty(dynamicconfig.PersistenceHealthSignalAggregationEnabled, true)(),
   140  			dynamicCollection.GetDurationProperty(dynamicconfig.PersistenceHealthSignalWindowSize, 10*time.Second)(),
   141  			dynamicCollection.GetIntProperty(dynamicconfig.PersistenceHealthSignalBufferSize, 5000)(),
   142  			metricsHandler,
   143  			dynamicCollection.GetIntProperty(dynamicconfig.ShardRPSWarnLimit, 50),
   144  			dynamicCollection.GetFloat64Property(dynamicconfig.ShardPerNsRPSWarnPercent, 0.8),
   145  			logger,
   146  		)
   147  	}
   148  
   149  	return persistence.NoopHealthSignalAggregator
   150  }