-
Notifications
You must be signed in to change notification settings - Fork 941
Wait for handover state update before serving the requests #7028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
26f873f
656d66a
f60b2b0
557c48c
d5f4fbc
a1979a0
5871640
ba68572
a9b8214
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// 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 interceptor | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
enumspb "go.temporal.io/api/enums/v1" | ||
"go.temporal.io/server/common" | ||
"go.temporal.io/server/common/api" | ||
"go.temporal.io/server/common/clock" | ||
"go.temporal.io/server/common/dynamicconfig" | ||
"go.temporal.io/server/common/log" | ||
"go.temporal.io/server/common/metrics" | ||
"go.temporal.io/server/common/namespace" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
ctxTailRoom = time.Millisecond * 100 | ||
) | ||
|
||
var _ grpc.UnaryServerInterceptor = (*NamespaceHandoverInterceptor)(nil).Intercept | ||
|
||
type ( | ||
// NamespaceHandoverInterceptor handles the namespace in handover replication state | ||
NamespaceHandoverInterceptor struct { | ||
namespaceRegistry namespace.Registry | ||
timeSource clock.TimeSource | ||
enabledForNS dynamicconfig.BoolPropertyFnWithNamespaceFilter | ||
nsCacheRefreshInterval dynamicconfig.DurationPropertyFn | ||
metricsHandler metrics.Handler | ||
logger log.Logger | ||
} | ||
) | ||
|
||
func NewNamespaceHandoverInterceptor( | ||
dc *dynamicconfig.Collection, | ||
namespaceRegistry namespace.Registry, | ||
metricsHandler metrics.Handler, | ||
logger log.Logger, | ||
timeSource clock.TimeSource, | ||
) *NamespaceHandoverInterceptor { | ||
|
||
return &NamespaceHandoverInterceptor{ | ||
enabledForNS: dynamicconfig.EnableNamespaceHandoverWait.Get(dc), | ||
nsCacheRefreshInterval: dynamicconfig.NamespaceCacheRefreshInterval.Get(dc), | ||
namespaceRegistry: namespaceRegistry, | ||
metricsHandler: metricsHandler, | ||
logger: logger, | ||
timeSource: timeSource, | ||
} | ||
} | ||
|
||
func (i *NamespaceHandoverInterceptor) Intercept( | ||
ctx context.Context, | ||
req any, | ||
info *grpc.UnaryServerInfo, | ||
handler grpc.UnaryHandler, | ||
) (_ any, retError error) { | ||
defer log.CapturePanic(i.logger, &retError) | ||
|
||
if !strings.HasPrefix(info.FullMethod, api.WorkflowServicePrefix) { | ||
return handler(ctx, req) | ||
} | ||
|
||
// review which method is allowed | ||
methodName := api.MethodName(info.FullMethod) | ||
namespaceName := MustGetNamespaceName(i.namespaceRegistry, req) | ||
|
||
if namespaceName != namespace.EmptyName && i.enabledForNS(namespaceName.String()) { | ||
var waitTime *time.Duration | ||
defer func() { | ||
if waitTime != nil { | ||
metrics.HandoverWaitLatency.With(i.metricsHandler).Record(*waitTime) | ||
} | ||
}() | ||
waitTime, err := i.waitNamespaceHandoverUpdate(ctx, namespaceName, methodName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return handler(ctx, req) | ||
} | ||
|
||
func (i *NamespaceHandoverInterceptor) waitNamespaceHandoverUpdate( | ||
ctx context.Context, | ||
namespaceName namespace.Name, | ||
methodName string, | ||
) (waitTime *time.Duration, retErr error) { | ||
if _, ok := allowedMethodsDuringHandover[methodName]; ok { | ||
return nil, nil | ||
} | ||
|
||
startTime := i.timeSource.Now() | ||
namespaceData, err := i.namespaceRegistry.GetNamespace(namespaceName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if namespaceData.ReplicationState() == enumspb.REPLICATION_STATE_HANDOVER { | ||
cbID := uuid.New() | ||
waitReplicationStateUpdate := make(chan struct{}) | ||
i.namespaceRegistry.RegisterStateChangeCallback(cbID, func(ns *namespace.Namespace, deletedFromDb bool) { | ||
if ns.ID().String() != namespaceData.ID().String() { | ||
return | ||
} | ||
if ns.State() != enumspb.NAMESPACE_STATE_REGISTERED || | ||
deletedFromDb || | ||
ns.ReplicationState() != enumspb.REPLICATION_STATE_HANDOVER || | ||
!ns.IsGlobalNamespace() { | ||
// Stop wait on state change if: | ||
// 1. namespace is deleting/deleted | ||
// 2. namespace is not in handover | ||
// 3. namespace is not global | ||
select { | ||
case <-waitReplicationStateUpdate: | ||
yycptt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
default: | ||
close(waitReplicationStateUpdate) | ||
} | ||
} | ||
}) | ||
|
||
maxWaitDuration := i.nsCacheRefreshInterval() // cache refresh time | ||
if deadline, ok := ctx.Deadline(); ok { | ||
maxWaitDuration = max(0, time.Until(deadline)-ctxTailRoom) | ||
} | ||
returnTimer := time.NewTimer(maxWaitDuration) | ||
var handoverErr error | ||
select { | ||
case <-returnTimer.C: | ||
handoverErr = common.ErrNamespaceHandover | ||
case <-waitReplicationStateUpdate: | ||
returnTimer.Stop() | ||
} | ||
i.namespaceRegistry.UnregisterStateChangeCallback(cbID) | ||
waitTime := time.Since(startTime) | ||
return &waitTime, handoverErr | ||
} | ||
return nil, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,7 @@ var Module = fx.Options( | |
fx.Provide(MuxRouterProvider), | ||
fx.Provide(ConfigProvider), | ||
fx.Provide(NamespaceLogInterceptorProvider), | ||
fx.Provide(NamespaceHandoverInterceptorProvider), | ||
fx.Provide(RedirectionInterceptorProvider), | ||
fx.Provide(TelemetryInterceptorProvider), | ||
fx.Provide(RetryableInterceptorProvider), | ||
|
@@ -218,6 +219,7 @@ func GrpcServerOptionsProvider( | |
namespaceRateLimiterInterceptor *interceptor.NamespaceRateLimitInterceptor, | ||
namespaceCountLimiterInterceptor *interceptor.ConcurrentRequestLimitInterceptor, | ||
namespaceValidatorInterceptor *interceptor.NamespaceValidatorInterceptor, | ||
namespaceHandoverInterceptor *interceptor.NamespaceHandoverInterceptor, | ||
redirectionInterceptor *interceptor.Redirection, | ||
telemetryInterceptor *interceptor.TelemetryInterceptor, | ||
retryableInterceptor *interceptor.RetryableInterceptor, | ||
|
@@ -266,6 +268,9 @@ func GrpcServerOptionsProvider( | |
namespaceLogInterceptor.Intercept, // TODO: Deprecate this with a outer custom interceptor | ||
metrics.NewServerMetricsContextInjectorInterceptor(), | ||
authInterceptor.Intercept, | ||
// Handover interceptor has to above redirection because the request will route to the correct cluster after handover completed. | ||
// And retry cannot be performed before customInterceptors. | ||
namespaceHandoverInterceptor.Intercept, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add some comments explaining why it must be in this place? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the handover state checking logic in StateValidationIntercept removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not remove in this PR as I added the feature flag. Once we can confident we can remove it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you want to disabled that the check in StateValidationIntercept if the feature flag is enabled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Once I validate the function and set default to enable I will remove it |
||
redirectionInterceptor.Intercept, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess theoretically we can retry in redirection Interceptor? since it has the knowledge of whether redirect will actually happen or not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess so but the logic could be weird. What should we do if the redirect will not happen? |
||
telemetryInterceptor.UnaryIntercept, | ||
healthInterceptor.Intercept, | ||
|
@@ -352,6 +357,22 @@ func RedirectionInterceptorProvider( | |
) | ||
} | ||
|
||
func NamespaceHandoverInterceptorProvider( | ||
dc *dynamicconfig.Collection, | ||
namespaceCache namespace.Registry, | ||
logger log.Logger, | ||
metricsHandler metrics.Handler, | ||
timeSource clock.TimeSource, | ||
) *interceptor.NamespaceHandoverInterceptor { | ||
return interceptor.NewNamespaceHandoverInterceptor( | ||
dc, | ||
namespaceCache, | ||
metricsHandler, | ||
logger, | ||
timeSource, | ||
) | ||
} | ||
|
||
func TelemetryInterceptorProvider( | ||
logger log.Logger, | ||
metricsHandler metrics.Handler, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we probably should be change the impl to listen on changes for a specific namespace...
Can be done in a later PR.