Files
Michael SnowdenandGitHub e3aebc4edd Use source cluster shard ID for DLQ (#5078)
<!-- Describe what has changed in this PR -->
**What changed?**
This PR makes us use the shard ID of the task in the original cluster
instead of the shard ID in the current cluster when merging DLQ'd
replication tasks (or writing any task to the DLQ actually).

<!-- Tell your future self why have you made these changes -->
**Why?**
The number of shards between any two clusters for a given namespace
don't have to be the same. One has to be a multiple of the other,
though. However, I noticed a bug when using our local xdc config with
cluster-a and cluster-b:

```
  "Error": "AddTasks failed while re-enqueuing tasks to shard 9 (type: dlq-error-type-invalid-request, retryable: false): Task is for wrong shard: index = 0, task shard = 25, request shard = 9",
```

This error was caused by the fact that the task in question has a
different computed shard ID in cluster-b, where there are only 16
shards, than it does in cluster-a, where there are 32 shards.

<img width="740" alt="Untitled"
src="https://github.com/temporalio/temporal/assets/5942963/ac9c2754-acfc-44ae-bb8d-a98f9ecd6b4d">

Fortunately, the V2 replication DLQ hasn't even been turned on anywhere
yet, so this didn't cause any issues. It was caught as part of testing
before rollout.

<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
**How did you test it?**
I wrote some unit tests, and I also re-ran the manual test I was doing.
I even generated a significant amount of load with `maru`, forced 10s of
thousands of replication tasks to fail, and successfully merged all of
them.

<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
**Potential risks**

<!-- Is this PR a hotfix candidate or require that a notification be
sent to the broader community? (Yes/No) -->
**Is hotfix candidate?**
2023-11-07 21:13:47 +00:00

255 lines
9.0 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 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 client
import (
"go.temporal.io/api/serviceerror"
"go.temporal.io/server/common"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/metrics"
p "go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/serialization"
"go.temporal.io/server/common/quotas"
)
var (
retryPolicy = common.CreatePersistenceClientRetryPolicy()
)
type (
// Factory defines the interface for any implementation that can vend
// persistence layer objects backed by a datastore. The actual datastore
// is implementation detail hidden behind this interface
Factory interface {
// Close the factory
Close()
// NewTaskManager returns a new task manager
NewTaskManager() (p.TaskManager, error)
// NewShardManager returns a new shard manager
NewShardManager() (p.ShardManager, error)
// NewMetadataManager returns a new metadata manager
NewMetadataManager() (p.MetadataManager, error)
// NewExecutionManager returns a new execution manager
NewExecutionManager() (p.ExecutionManager, error)
// NewNamespaceReplicationQueue returns a new queue for namespace replication
NewNamespaceReplicationQueue() (p.NamespaceReplicationQueue, error)
// NewClusterMetadataManager returns a new manager for cluster specific metadata
NewClusterMetadataManager() (p.ClusterMetadataManager, error)
// NewHistoryTaskQueueManager returns a new manager for history task queues
NewHistoryTaskQueueManager() (p.HistoryTaskQueueManager, error)
}
factoryImpl struct {
dataStoreFactory DataStoreFactory
config *config.Persistence
serializer serialization.Serializer
eventBlobCache p.XDCCache
metricsHandler metrics.Handler
logger log.Logger
clusterName string
ratelimiter quotas.RequestRateLimiter
healthSignals p.HealthSignalAggregator
}
)
// NewFactory returns an implementation of factory that vends persistence objects based on
// specified configuration. This factory takes as input a config.Persistence object
// which specifies the datastore to be used for a given type of object. This config
// also contains config for individual datastores themselves.
//
// The objects returned by this factory enforce ratelimit and maxconns according to
// given configuration. In addition, all objects will emit metrics automatically
func NewFactory(
dataStoreFactory DataStoreFactory,
cfg *config.Persistence,
ratelimiter quotas.RequestRateLimiter,
serializer serialization.Serializer,
eventBlobCache p.XDCCache,
clusterName string,
metricsHandler metrics.Handler,
logger log.Logger,
healthSignals p.HealthSignalAggregator,
) Factory {
factory := &factoryImpl{
dataStoreFactory: dataStoreFactory,
config: cfg,
serializer: serializer,
eventBlobCache: eventBlobCache,
metricsHandler: metricsHandler,
logger: logger,
clusterName: clusterName,
ratelimiter: ratelimiter,
healthSignals: healthSignals,
}
factory.initDependencies()
return factory
}
// NewTaskManager returns a new task manager
func (f *factoryImpl) NewTaskManager() (p.TaskManager, error) {
taskStore, err := f.dataStoreFactory.NewTaskStore()
if err != nil {
return nil, err
}
result := p.NewTaskManager(taskStore, f.serializer)
if f.ratelimiter != nil {
result = p.NewTaskPersistenceRateLimitedClient(result, f.ratelimiter, f.logger)
}
if f.metricsHandler != nil && f.healthSignals != nil {
result = p.NewTaskPersistenceMetricsClient(result, f.metricsHandler, f.healthSignals, f.logger)
}
result = p.NewTaskPersistenceRetryableClient(result, retryPolicy, IsPersistenceTransientError)
return result, nil
}
// NewShardManager returns a new shard manager
func (f *factoryImpl) NewShardManager() (p.ShardManager, error) {
shardStore, err := f.dataStoreFactory.NewShardStore()
if err != nil {
return nil, err
}
result := p.NewShardManager(shardStore, f.serializer)
if f.ratelimiter != nil {
result = p.NewShardPersistenceRateLimitedClient(result, f.ratelimiter, f.logger)
}
if f.metricsHandler != nil && f.healthSignals != nil {
result = p.NewShardPersistenceMetricsClient(result, f.metricsHandler, f.healthSignals, f.logger)
}
result = p.NewShardPersistenceRetryableClient(result, retryPolicy, IsPersistenceTransientError)
return result, nil
}
// NewMetadataManager returns a new metadata manager
func (f *factoryImpl) NewMetadataManager() (p.MetadataManager, error) {
store, err := f.dataStoreFactory.NewMetadataStore()
if err != nil {
return nil, err
}
result := p.NewMetadataManagerImpl(store, f.serializer, f.logger, f.clusterName)
if f.ratelimiter != nil {
result = p.NewMetadataPersistenceRateLimitedClient(result, f.ratelimiter, f.logger)
}
if f.metricsHandler != nil && f.healthSignals != nil {
result = p.NewMetadataPersistenceMetricsClient(result, f.metricsHandler, f.healthSignals, f.logger)
}
result = p.NewMetadataPersistenceRetryableClient(result, retryPolicy, IsPersistenceTransientError)
return result, nil
}
// NewClusterMetadataManager returns a new cluster metadata manager
func (f *factoryImpl) NewClusterMetadataManager() (p.ClusterMetadataManager, error) {
store, err := f.dataStoreFactory.NewClusterMetadataStore()
if err != nil {
return nil, err
}
result := p.NewClusterMetadataManagerImpl(store, f.serializer, f.clusterName, f.logger)
if f.ratelimiter != nil {
result = p.NewClusterMetadataPersistenceRateLimitedClient(result, f.ratelimiter, f.logger)
}
if f.metricsHandler != nil && f.healthSignals != nil {
result = p.NewClusterMetadataPersistenceMetricsClient(result, f.metricsHandler, f.healthSignals, f.logger)
}
result = p.NewClusterMetadataPersistenceRetryableClient(result, retryPolicy, IsPersistenceTransientError)
return result, nil
}
// NewExecutionManager returns a new execution manager
func (f *factoryImpl) NewExecutionManager() (p.ExecutionManager, error) {
store, err := f.dataStoreFactory.NewExecutionStore()
if err != nil {
return nil, err
}
result := p.NewExecutionManager(store, f.serializer, f.eventBlobCache, f.logger, f.config.TransactionSizeLimit)
if f.ratelimiter != nil {
result = p.NewExecutionPersistenceRateLimitedClient(result, f.ratelimiter, f.logger)
}
if f.metricsHandler != nil && f.healthSignals != nil {
result = p.NewExecutionPersistenceMetricsClient(result, f.metricsHandler, f.healthSignals, f.logger)
}
result = p.NewExecutionPersistenceRetryableClient(result, retryPolicy, IsPersistenceTransientError)
return result, nil
}
func (f *factoryImpl) NewNamespaceReplicationQueue() (p.NamespaceReplicationQueue, error) {
result, err := f.dataStoreFactory.NewQueue(p.NamespaceReplicationQueueType)
if err != nil {
return nil, err
}
if f.ratelimiter != nil {
result = p.NewQueuePersistenceRateLimitedClient(result, f.ratelimiter, f.logger)
}
if f.metricsHandler != nil && f.healthSignals != nil {
result = p.NewQueuePersistenceMetricsClient(result, f.metricsHandler, f.healthSignals, f.logger)
}
result = p.NewQueuePersistenceRetryableClient(result, retryPolicy, IsPersistenceTransientError)
return p.NewNamespaceReplicationQueue(result, f.serializer, f.clusterName, f.metricsHandler, f.logger)
}
func (f *factoryImpl) NewHistoryTaskQueueManager() (p.HistoryTaskQueueManager, error) {
q, err := f.dataStoreFactory.NewQueueV2()
if err != nil {
return nil, err
}
return p.NewHistoryTaskQueueManager(q), nil
}
// Close closes this factory
func (f *factoryImpl) Close() {
f.dataStoreFactory.Close()
if f.healthSignals != nil {
f.healthSignals.Stop()
}
}
func IsPersistenceTransientError(err error) bool {
switch err.(type) {
case *serviceerror.Unavailable:
return true
}
return false
}
func (f *factoryImpl) initDependencies() {
if f.metricsHandler == nil && f.healthSignals == nil {
return
}
if f.metricsHandler == nil {
f.metricsHandler = metrics.NoopMetricsHandler
}
if f.healthSignals == nil {
f.healthSignals = p.NoopHealthSignalAggregator
}
f.healthSignals.Start()
}