Files
tasks/common/rpc/interceptor/namespace_handover.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

143 lines
4.1 KiB
Go

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:
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
}