mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
## What changed? <!-- Describe what has changed in this PR --> Delete duplicate log on replication stream ## Why? <!-- Tell your future self why have you made these changes --> Reduce duplicate log emitting ## How did you test it? <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> n/a ## Potential risks <!-- Assuming the worst case, what can be broken when deploying this change to production? --> no risk ## Documentation <!-- Have you made sure this change doesn't falsify anything currently stated in `docs/`? If significant new behavior is added, have you described that in `docs/`? --> ## Is hotfix candidate? <!-- Is this PR a hotfix candidate or does it require a notification to be sent to the broader community? (Yes/No) --> no --------- Co-authored-by: Liang Mei <[email protected]>
125 lines
3.7 KiB
Go
125 lines
3.7 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 replication
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.temporal.io/api/serviceerror"
|
|
"go.temporal.io/server/api/adminservice/v1"
|
|
"go.temporal.io/server/common/cluster"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.temporal.io/server/common/metrics"
|
|
"go.temporal.io/server/service/history/shard"
|
|
)
|
|
|
|
type (
|
|
Stream BiDirectionStream[*adminservice.StreamWorkflowReplicationMessagesRequest, *adminservice.StreamWorkflowReplicationMessagesResponse]
|
|
ClusterShardKey struct {
|
|
ClusterID int32
|
|
ShardID int32
|
|
}
|
|
ClusterShardKeyPair struct {
|
|
Client ClusterShardKey
|
|
Server ClusterShardKey
|
|
}
|
|
)
|
|
|
|
func ClusterIDToClusterNameShardCount(
|
|
allClusterInfo map[string]cluster.ClusterInformation,
|
|
clusterID int32,
|
|
) (string, int32, error) {
|
|
for clusterName, clusterInfo := range allClusterInfo {
|
|
if int32(clusterInfo.InitialFailoverVersion) == clusterID {
|
|
return clusterName, clusterInfo.ShardCount, nil
|
|
}
|
|
}
|
|
return "", 0, serviceerror.NewInternal(fmt.Sprintf("unknown cluster ID: %v", clusterID))
|
|
}
|
|
|
|
func WrapEventLoop(
|
|
originalEventLoop func() error,
|
|
streamStopper func(),
|
|
logger log.Logger,
|
|
metricsHandler metrics.Handler,
|
|
fromClusterKey ClusterShardKey,
|
|
toClusterKey ClusterShardKey,
|
|
retryInterval time.Duration,
|
|
) {
|
|
defer streamStopper()
|
|
|
|
for i := 0; i < 50; i++ {
|
|
err := originalEventLoop()
|
|
|
|
if err == nil { // shutdown case
|
|
return
|
|
}
|
|
var streamError *StreamError
|
|
if errors.As(err, &streamError) {
|
|
metrics.ReplicationStreamError.With(metricsHandler).Record(
|
|
int64(1),
|
|
metrics.ServiceErrorTypeTag(streamError.cause),
|
|
metrics.FromClusterIDTag(fromClusterKey.ClusterID),
|
|
metrics.ToClusterIDTag(toClusterKey.ClusterID),
|
|
)
|
|
logger.Error("ReplicationStreamError", tag.Error(err))
|
|
} else {
|
|
metrics.ReplicationServiceError.With(metricsHandler).Record(
|
|
int64(1),
|
|
metrics.ServiceErrorTypeTag(err),
|
|
metrics.FromClusterIDTag(fromClusterKey.ClusterID),
|
|
metrics.ToClusterIDTag(toClusterKey.ClusterID),
|
|
)
|
|
logger.Error("ReplicationServiceError", tag.Error(err))
|
|
}
|
|
// if it is not a retryable error, we will not retry and terminate the stream, then let the stream_receiver_monitor to restart it
|
|
if !IsRetryableError(err) {
|
|
return
|
|
}
|
|
|
|
time.Sleep(retryInterval)
|
|
}
|
|
}
|
|
|
|
func IsStreamError(err error) bool {
|
|
var streamError *StreamError
|
|
return errors.As(err, &streamError)
|
|
}
|
|
|
|
func IsRetryableError(err error) bool {
|
|
if shard.IsShardOwnershipLostError(err) {
|
|
return false
|
|
}
|
|
switch err.(type) {
|
|
case *StreamError:
|
|
return false
|
|
default:
|
|
return true
|
|
}
|
|
}
|