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?** Adding FOR UPDATE clause when fetching queueV2 metadata and removing unused field version from queue metadata. <!-- Tell your future self why have you made these changes --> **Why?** If a queueV2 metadata row is fetched in a transaction, we have to place a lock on that row to avoid another transaction reading from that line. This can happen when two threads are trying to delete messages from the queue. Two of them will update queue metadata and write it back. This will lead to a race condition. By adding 'FOR UPDATE' SQL clause, the first transaction will get an exclusive lock on the row. The second transaction will wait until the first transaction is complete or rollback. Also, removing the version field from QueueV2 metadata table. We will not be using this field since we are using sql transactions. <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** Unit test. Executed through the debugger to make sure SelectFromQueueV2MetadataForUpdate() is called instead of SelectFromQueueV2Metadata() when executed inside a trancation. <!-- 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?**
148 lines
5.8 KiB
Go
148 lines
5.8 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2021 Datadog, Inc.
|
|
//
|
|
// 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 postgresql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"go.temporal.io/server/common/persistence/sql/sqlplugin"
|
|
)
|
|
|
|
const (
|
|
templateEnqueueMessageQueryV2 = `INSERT INTO queue_messages (queue_type, queue_name, queue_partition, message_id, message_payload, message_encoding) VALUES(:queue_type, :queue_name, :queue_partition, :message_id, :message_payload, :message_encoding)`
|
|
templateGetMessagesQueryV2 = `SELECT message_id, message_payload, message_encoding FROM queue_messages WHERE queue_type = $1 and queue_name = $2 and queue_partition = $3 and message_id >= $4 ORDER BY message_id ASC LIMIT $5`
|
|
templateRangeDeleteMessagesQueryV2 = `DELETE FROM queue_messages WHERE queue_type = $1 and queue_name = $2 and queue_partition = $3 and message_id >= $4 and message_id <= $5`
|
|
templateGetLastMessageIDQueryV2 = `SELECT message_id FROM queue_messages WHERE queue_type=$1 and queue_name=$2 and queue_partition=$3 and message_id >= (SELECT message_id FROM queue_messages WHERE queue_type=$1 and queue_name=$2 and queue_partition=$3 ORDER BY message_id DESC LIMIT 1) FOR UPDATE`
|
|
templateCreateQueueMetadataQueryV2 = `INSERT INTO queues (queue_type, queue_name, metadata_payload, metadata_encoding) VALUES(:queue_type, :queue_name, :metadata_payload, :metadata_encoding)`
|
|
templateUpdateQueueMetadataQueryV2 = `UPDATE queues SET metadata_payload = :metadata_payload, metadata_encoding = :metadata_encoding WHERE queue_type = :queue_type and queue_name = :queue_name`
|
|
templateGetQueueMetadataQueryV2 = `SELECT metadata_payload, metadata_encoding from queues WHERE queue_type=$1 and queue_name=$2`
|
|
templateGetQueueMetadataQueryV2ForUpdate = `SELECT metadata_payload, metadata_encoding from queues WHERE queue_type=$1 and queue_name=$2 FOR UPDATE`
|
|
templateGetNameFromQueueMetadataV2 = `SELECT queue_name from queues WHERE queue_type=$1 LIMIT $2 OFFSET $3`
|
|
)
|
|
|
|
func (pdb *db) InsertIntoQueueV2Metadata(ctx context.Context, row *sqlplugin.QueueV2MetadataRow) (sql.Result, error) {
|
|
return pdb.conn.NamedExecContext(ctx,
|
|
templateCreateQueueMetadataQueryV2,
|
|
row,
|
|
)
|
|
}
|
|
|
|
func (pdb *db) UpdateQueueV2Metadata(ctx context.Context, row *sqlplugin.QueueV2MetadataRow) (sql.Result, error) {
|
|
return pdb.conn.NamedExecContext(ctx,
|
|
templateUpdateQueueMetadataQueryV2,
|
|
row,
|
|
)
|
|
}
|
|
|
|
func (pdb *db) SelectFromQueueV2Metadata(ctx context.Context, filter sqlplugin.QueueV2MetadataFilter) (*sqlplugin.QueueV2MetadataRow, error) {
|
|
var row sqlplugin.QueueV2MetadataRow
|
|
err := pdb.conn.GetContext(ctx,
|
|
&row,
|
|
templateGetQueueMetadataQueryV2,
|
|
filter.QueueType,
|
|
filter.QueueName,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &row, nil
|
|
}
|
|
|
|
func (pdb *db) SelectFromQueueV2MetadataForUpdate(ctx context.Context, filter sqlplugin.QueueV2MetadataFilter) (*sqlplugin.QueueV2MetadataRow, error) {
|
|
var row sqlplugin.QueueV2MetadataRow
|
|
err := pdb.conn.GetContext(ctx,
|
|
&row,
|
|
templateGetQueueMetadataQueryV2ForUpdate,
|
|
filter.QueueType,
|
|
filter.QueueName,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &row, nil
|
|
}
|
|
|
|
func (pdb *db) SelectNameFromQueueV2Metadata(ctx context.Context, filter sqlplugin.QueueV2MetadataTypeFilter) ([]sqlplugin.QueueV2MetadataRow, error) {
|
|
var rows []sqlplugin.QueueV2MetadataRow
|
|
err := pdb.conn.GetContext(ctx,
|
|
&rows,
|
|
templateGetNameFromQueueMetadataV2,
|
|
filter.QueueType,
|
|
filter.PageSize,
|
|
filter.PageOffset,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return rows, nil
|
|
}
|
|
|
|
func (pdb *db) InsertIntoQueueV2Messages(ctx context.Context, row []sqlplugin.QueueV2MessageRow) (sql.Result, error) {
|
|
return pdb.conn.NamedExecContext(ctx,
|
|
templateEnqueueMessageQueryV2,
|
|
row,
|
|
)
|
|
}
|
|
|
|
func (pdb *db) RangeSelectFromQueueV2Messages(ctx context.Context, filter sqlplugin.QueueV2MessagesFilter) ([]sqlplugin.QueueV2MessageRow, error) {
|
|
var rows []sqlplugin.QueueV2MessageRow
|
|
err := pdb.conn.SelectContext(ctx,
|
|
&rows,
|
|
templateGetMessagesQueryV2,
|
|
filter.QueueType,
|
|
filter.QueueName,
|
|
filter.Partition,
|
|
filter.MinMessageID,
|
|
filter.PageSize,
|
|
)
|
|
return rows, err
|
|
}
|
|
|
|
func (pdb *db) RangeDeleteFromQueueV2Messages(ctx context.Context, filter sqlplugin.QueueV2MessagesFilter) (sql.Result, error) {
|
|
return pdb.conn.ExecContext(ctx,
|
|
templateRangeDeleteMessagesQueryV2,
|
|
filter.QueueType,
|
|
filter.QueueName,
|
|
filter.Partition,
|
|
filter.MinMessageID,
|
|
filter.MaxMessageID,
|
|
)
|
|
}
|
|
|
|
func (pdb *db) GetLastEnqueuedMessageIDForUpdateV2(ctx context.Context, filter sqlplugin.QueueV2Filter) (int64, error) {
|
|
var lastMessageID int64
|
|
err := pdb.conn.GetContext(ctx,
|
|
&lastMessageID,
|
|
templateGetLastMessageIDQueryV2,
|
|
filter.QueueType,
|
|
filter.QueueName,
|
|
filter.Partition,
|
|
)
|
|
return lastMessageID, err
|
|
}
|