mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
<!-- Describe what has changed in this PR --> **What changed?** I renamed all of our Go source code files so that they're snake case instead of camel case. <!-- Tell your future self why have you made these changes --> **Why?** This was an annoying inconsistency we carried over from much older code in the repo. <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** The renaming process was part of an automated script (https://gist.github.com/MichaelSnowden/47576fffe27aa1cae6b26eaccec7ab55), so it should be consistent. Also, I don't see us doing anything in our codebase that involves parsing our own source files by path: <img width="687" alt="image" src="https://github.com/temporalio/temporal/assets/5942963/c6b873b0-d841-40d1-8b0f-2fe9d8079a53"> *PS: here's a link to the GPT convo I had to generate this renaming script: https://chat.openai.com/share/7f2d011a-a8da-4fa4-9c6d-e09102fdd49f* <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** Mainly merge conflicts, but I used `git mv` so that these are renames instead of deletes/adds, so that shouldn't be a big issue. <!-- Is this PR a hotfix candidate or require that a notification be sent to the broader community? (Yes/No) --> **Is hotfix candidate?** No.
442 lines
12 KiB
Go
442 lines
12 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.
|
|
|
|
//go:generate mockgen -copyright_file ../../LICENSE -package $GOPACKAGE -source $GOFILE -destination namespace_replication_queue_mock.go
|
|
|
|
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
commonpb "go.temporal.io/api/common/v1"
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
|
|
"go.temporal.io/server/api/persistence/v1"
|
|
"go.temporal.io/server/internal/goro"
|
|
|
|
replicationspb "go.temporal.io/server/api/replication/v1"
|
|
"go.temporal.io/server/common"
|
|
"go.temporal.io/server/common/convert"
|
|
"go.temporal.io/server/common/headers"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.temporal.io/server/common/metrics"
|
|
"go.temporal.io/server/common/persistence/serialization"
|
|
)
|
|
|
|
const (
|
|
purgeInterval = 5 * time.Minute
|
|
localNamespaceReplicationCluster = "namespaceReplication"
|
|
)
|
|
|
|
var _ NamespaceReplicationQueue = (*namespaceReplicationQueueImpl)(nil)
|
|
|
|
// NewNamespaceReplicationQueue creates a new NamespaceReplicationQueue instance
|
|
func NewNamespaceReplicationQueue(
|
|
queue Queue,
|
|
serializer serialization.Serializer,
|
|
clusterName string,
|
|
metricsHandler metrics.Handler,
|
|
logger log.Logger,
|
|
) (NamespaceReplicationQueue, error) {
|
|
|
|
blob, err := serializer.QueueMetadataToBlob(
|
|
&persistence.QueueMetadata{
|
|
ClusterAckLevels: make(map[string]int64),
|
|
}, enumspb.ENCODING_TYPE_PROTO3)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = queue.Init(context.TODO(), blob)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &namespaceReplicationQueueImpl{
|
|
queue: queue,
|
|
clusterName: clusterName,
|
|
metricsHandler: metricsHandler,
|
|
logger: logger,
|
|
ackNotificationChan: make(chan bool),
|
|
done: make(chan bool),
|
|
status: common.DaemonStatusInitialized,
|
|
serializer: serializer,
|
|
}, nil
|
|
}
|
|
|
|
type (
|
|
namespaceReplicationQueueImpl struct {
|
|
queue Queue
|
|
clusterName string
|
|
metricsHandler metrics.Handler
|
|
logger log.Logger
|
|
ackLevelUpdated bool
|
|
ackNotificationChan chan bool
|
|
done chan bool
|
|
status int32
|
|
gorogrp goro.Group
|
|
serializer serialization.Serializer
|
|
}
|
|
|
|
// NamespaceReplicationQueue is used to publish and list namespace replication tasks
|
|
NamespaceReplicationQueue interface {
|
|
Publish(ctx context.Context, task *replicationspb.ReplicationTask) error
|
|
GetReplicationMessages(
|
|
ctx context.Context,
|
|
lastMessageID int64,
|
|
maxCount int,
|
|
) ([]*replicationspb.ReplicationTask, int64, error)
|
|
UpdateAckLevel(ctx context.Context, lastProcessedMessageID int64, clusterName string) error
|
|
GetAckLevels(ctx context.Context) (map[string]int64, error)
|
|
|
|
PublishToDLQ(ctx context.Context, task *replicationspb.ReplicationTask) error
|
|
GetMessagesFromDLQ(
|
|
ctx context.Context,
|
|
firstMessageID int64,
|
|
lastMessageID int64,
|
|
pageSize int,
|
|
pageToken []byte,
|
|
) ([]*replicationspb.ReplicationTask, []byte, error)
|
|
UpdateDLQAckLevel(ctx context.Context, lastProcessedMessageID int64) error
|
|
GetDLQAckLevel(ctx context.Context) (int64, error)
|
|
|
|
RangeDeleteMessagesFromDLQ(ctx context.Context, firstMessageID int64, lastMessageID int64) error
|
|
DeleteMessageFromDLQ(ctx context.Context, messageID int64) error
|
|
Start()
|
|
Stop()
|
|
}
|
|
)
|
|
|
|
func (q *namespaceReplicationQueueImpl) Start() {
|
|
if !atomic.CompareAndSwapInt32(&q.status, common.DaemonStatusInitialized, common.DaemonStatusStarted) {
|
|
return
|
|
}
|
|
|
|
q.gorogrp.Go(q.purgeProcessor)
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) Stop() {
|
|
if !atomic.CompareAndSwapInt32(&q.status, common.DaemonStatusStarted, common.DaemonStatusStopped) {
|
|
return
|
|
}
|
|
close(q.done)
|
|
|
|
q.gorogrp.Cancel()
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) Publish(ctx context.Context, task *replicationspb.ReplicationTask) error {
|
|
blob, err := q.serializer.ReplicationTaskToBlob(task, enumspb.ENCODING_TYPE_PROTO3)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to encode message: %v", err)
|
|
}
|
|
return q.queue.EnqueueMessage(ctx, *blob)
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) PublishToDLQ(ctx context.Context, task *replicationspb.ReplicationTask) error {
|
|
blob, err := q.serializer.ReplicationTaskToBlob(task, enumspb.ENCODING_TYPE_PROTO3)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to encode message: %v", err)
|
|
}
|
|
messageID, err := q.queue.EnqueueMessageToDLQ(ctx, *blob)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
q.metricsHandler.Gauge(metrics.NamespaceReplicationDLQMaxLevelGauge.GetMetricName()).
|
|
Record(float64(messageID), metrics.OperationTag(metrics.PersistenceNamespaceReplicationQueueScope))
|
|
return nil
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) GetReplicationMessages(
|
|
ctx context.Context,
|
|
lastMessageID int64,
|
|
pageSize int,
|
|
) ([]*replicationspb.ReplicationTask, int64, error) {
|
|
|
|
messages, err := q.queue.ReadMessages(ctx, lastMessageID, pageSize)
|
|
if err != nil {
|
|
return nil, lastMessageID, err
|
|
}
|
|
|
|
replicationTasks := make([]*replicationspb.ReplicationTask, 0, len(messages))
|
|
for _, message := range messages {
|
|
replicationTask, err := q.serializer.ReplicationTaskFromBlob(NewDataBlob(message.Data, message.Encoding))
|
|
if err != nil {
|
|
return nil, lastMessageID, fmt.Errorf("failed to decode task: %v", err)
|
|
}
|
|
|
|
lastMessageID = message.ID
|
|
replicationTasks = append(replicationTasks, replicationTask)
|
|
}
|
|
|
|
return replicationTasks, lastMessageID, nil
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) UpdateAckLevel(
|
|
ctx context.Context,
|
|
lastProcessedMessageID int64,
|
|
clusterName string,
|
|
) error {
|
|
return q.updateAckLevelWithRetry(ctx, lastProcessedMessageID, clusterName, false)
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) updateAckLevelWithRetry(
|
|
ctx context.Context,
|
|
lastProcessedMessageID int64,
|
|
clusterName string,
|
|
isDLQ bool,
|
|
) error {
|
|
conditionFailedRetry:
|
|
for {
|
|
err := q.updateAckLevel(ctx, lastProcessedMessageID, clusterName, isDLQ)
|
|
switch err.(type) {
|
|
case *ConditionFailedError:
|
|
continue conditionFailedRetry
|
|
}
|
|
|
|
return err
|
|
}
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) updateAckLevel(
|
|
ctx context.Context,
|
|
lastProcessedMessageID int64,
|
|
clusterName string,
|
|
isDLQ bool,
|
|
) error {
|
|
var ackLevelErr error
|
|
var internalMetadata *InternalQueueMetadata
|
|
if isDLQ {
|
|
internalMetadata, ackLevelErr = q.queue.GetDLQAckLevels(ctx)
|
|
} else {
|
|
internalMetadata, ackLevelErr = q.queue.GetAckLevels(ctx)
|
|
}
|
|
|
|
if ackLevelErr != nil {
|
|
return ackLevelErr
|
|
}
|
|
|
|
ackLevels, err := q.ackLevelsFromBlob(internalMetadata.Blob)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Ignore possibly delayed message
|
|
if ack, ok := ackLevels[clusterName]; ok && ack > lastProcessedMessageID {
|
|
return nil
|
|
}
|
|
|
|
// TODO remove this block in 1.12.x
|
|
delete(ackLevels, "")
|
|
// TODO remove this block in 1.12.x
|
|
|
|
// update ack level
|
|
ackLevels[clusterName] = lastProcessedMessageID
|
|
blob, err := q.serializer.QueueMetadataToBlob(&persistence.QueueMetadata{
|
|
ClusterAckLevels: ackLevels,
|
|
}, enumspb.ENCODING_TYPE_PROTO3)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
internalMetadata.Blob = blob
|
|
if isDLQ {
|
|
err = q.queue.UpdateDLQAckLevel(ctx, internalMetadata)
|
|
} else {
|
|
err = q.queue.UpdateAckLevel(ctx, internalMetadata)
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update ack level: %v", err)
|
|
}
|
|
|
|
select {
|
|
case q.ackNotificationChan <- true:
|
|
default:
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) GetAckLevels(
|
|
ctx context.Context,
|
|
) (map[string]int64, error) {
|
|
metadata, err := q.queue.GetAckLevels(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return q.ackLevelsFromBlob(metadata.Blob)
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) ackLevelsFromBlob(blob *commonpb.DataBlob) (map[string]int64, error) {
|
|
if blob == nil {
|
|
return make(map[string]int64), nil
|
|
}
|
|
|
|
metadata, err := q.serializer.QueueMetadataFromBlob(blob)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ackLevels := metadata.ClusterAckLevels
|
|
if ackLevels == nil {
|
|
ackLevels = make(map[string]int64)
|
|
}
|
|
return ackLevels, nil
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) GetMessagesFromDLQ(
|
|
ctx context.Context,
|
|
firstMessageID int64,
|
|
lastMessageID int64,
|
|
pageSize int,
|
|
pageToken []byte,
|
|
) ([]*replicationspb.ReplicationTask, []byte, error) {
|
|
|
|
messages, token, err := q.queue.ReadMessagesFromDLQ(ctx, firstMessageID, lastMessageID, pageSize, pageToken)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var replicationTasks []*replicationspb.ReplicationTask
|
|
for _, message := range messages {
|
|
replicationTask, err := q.serializer.ReplicationTaskFromBlob(NewDataBlob(message.Data, message.Encoding))
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to decode dlq task: %v", err)
|
|
}
|
|
|
|
// Overwrite to local cluster message id
|
|
replicationTask.SourceTaskId = message.ID
|
|
replicationTasks = append(replicationTasks, replicationTask)
|
|
}
|
|
|
|
return replicationTasks, token, nil
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) UpdateDLQAckLevel(
|
|
ctx context.Context,
|
|
lastProcessedMessageID int64,
|
|
) error {
|
|
return q.updateAckLevelWithRetry(ctx, lastProcessedMessageID, localNamespaceReplicationCluster, true)
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) GetDLQAckLevel(
|
|
ctx context.Context,
|
|
) (int64, error) {
|
|
metadata, err := q.queue.GetDLQAckLevels(ctx)
|
|
if err != nil {
|
|
return EmptyQueueMessageID, err
|
|
}
|
|
dlqMetadata, err := q.ackLevelsFromBlob(metadata.Blob)
|
|
if err != nil {
|
|
return EmptyQueueMessageID, err
|
|
}
|
|
|
|
ackLevel, ok := dlqMetadata[localNamespaceReplicationCluster]
|
|
if !ok {
|
|
return EmptyQueueMessageID, nil
|
|
}
|
|
return ackLevel, nil
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) RangeDeleteMessagesFromDLQ(
|
|
ctx context.Context,
|
|
firstMessageID int64,
|
|
lastMessageID int64,
|
|
) error {
|
|
|
|
return q.queue.RangeDeleteMessagesFromDLQ(
|
|
ctx,
|
|
firstMessageID,
|
|
lastMessageID,
|
|
)
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) DeleteMessageFromDLQ(
|
|
ctx context.Context,
|
|
messageID int64,
|
|
) error {
|
|
|
|
return q.queue.DeleteMessageFromDLQ(ctx, messageID)
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) purgeAckedMessages(
|
|
ctx context.Context,
|
|
) error {
|
|
ackLevelByCluster, err := q.GetAckLevels(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to purge messages: %v", err)
|
|
}
|
|
|
|
if len(ackLevelByCluster) == 0 {
|
|
return nil
|
|
}
|
|
|
|
var minAckLevel *int64
|
|
for _, ackLevel := range ackLevelByCluster {
|
|
if minAckLevel == nil || ackLevel < *minAckLevel {
|
|
minAckLevel = convert.Int64Ptr(ackLevel)
|
|
}
|
|
}
|
|
if minAckLevel == nil {
|
|
return nil
|
|
}
|
|
|
|
err = q.queue.DeleteMessagesBefore(ctx, *minAckLevel)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to purge messages: %v", err)
|
|
}
|
|
q.metricsHandler.Gauge(metrics.NamespaceReplicationTaskAckLevelGauge.GetMetricName()).
|
|
Record(float64(*minAckLevel), metrics.OperationTag(metrics.PersistenceNamespaceReplicationQueueScope))
|
|
return nil
|
|
}
|
|
|
|
func (q *namespaceReplicationQueueImpl) purgeProcessor(
|
|
ctx context.Context,
|
|
) error {
|
|
ctx = headers.SetCallerInfo(ctx, headers.SystemPreemptableCallerInfo)
|
|
|
|
ticker := time.NewTicker(purgeInterval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-q.done:
|
|
return nil
|
|
case <-ticker.C:
|
|
if q.ackLevelUpdated {
|
|
err := q.purgeAckedMessages(ctx)
|
|
if err != nil {
|
|
q.logger.Warn("Failed to purge acked namespace replication messages.", tag.Error(err))
|
|
} else {
|
|
q.ackLevelUpdated = false
|
|
}
|
|
}
|
|
case <-q.ackNotificationChan:
|
|
q.ackLevelUpdated = true
|
|
}
|
|
}
|
|
}
|