Files
tasks/common/persistence/sql/sqlplugin/sqlite/queue_v2.go
T
Prathyush PVandGitHub d98eb745ad Adding FOR UPDATE for SelectFromQueueV2Metadata (#5075)
<!-- 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?**
2023-11-04 09:56:35 -07:00

131 lines
5.4 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 sqlite
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 = ? and queue_name = ? and queue_partition = ? and message_id >= ? ORDER BY message_id ASC LIMIT ?`
templateRangeDeleteMessagesQueryV2 = `DELETE FROM queue_messages WHERE queue_type = ? and queue_name = ? and queue_partition = ? and message_id >= ? and message_id <= ?`
templateGetLastMessageIDQueryV2 = `SELECT message_id FROM queue_messages WHERE queue_type=? and queue_name=? and queue_partition=? ORDER BY message_id DESC LIMIT 1`
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=? and queue_name=?`
templateGetNameFromQueueMetadataV2 = `SELECT queue_name from queues WHERE queue_type=? LIMIT ? OFFSET ?`
)
func (sdb *db) InsertIntoQueueV2Metadata(ctx context.Context, row *sqlplugin.QueueV2MetadataRow) (sql.Result, error) {
return sdb.conn.NamedExecContext(ctx,
templateCreateQueueMetadataQueryV2,
row,
)
}
func (sdb *db) UpdateQueueV2Metadata(ctx context.Context, row *sqlplugin.QueueV2MetadataRow) (sql.Result, error) {
return sdb.conn.NamedExecContext(ctx,
templateUpdateQueueMetadataQueryV2,
row,
)
}
func (sdb *db) SelectFromQueueV2Metadata(ctx context.Context, filter sqlplugin.QueueV2MetadataFilter) (*sqlplugin.QueueV2MetadataRow, error) {
var row sqlplugin.QueueV2MetadataRow
err := sdb.conn.GetContext(ctx,
&row,
templateGetQueueMetadataQueryV2,
filter.QueueType,
filter.QueueName,
)
if err != nil {
return nil, err
}
return &row, nil
}
func (sdb *db) SelectFromQueueV2MetadataForUpdate(ctx context.Context, filter sqlplugin.QueueV2MetadataFilter) (*sqlplugin.QueueV2MetadataRow, error) {
// sqlite does not have FOR UPDATE clause. Calling SelectFromQueueV2Metadata() itself.
return sdb.SelectFromQueueV2Metadata(ctx, filter)
}
func (sdb *db) SelectNameFromQueueV2Metadata(ctx context.Context, filter sqlplugin.QueueV2MetadataTypeFilter) ([]sqlplugin.QueueV2MetadataRow, error) {
var rows []sqlplugin.QueueV2MetadataRow
err := sdb.conn.GetContext(ctx,
&rows,
templateGetNameFromQueueMetadataV2,
filter.QueueType,
filter.PageSize,
filter.PageOffset,
)
if err != nil {
return nil, err
}
return rows, nil
}
func (sdb *db) InsertIntoQueueV2Messages(ctx context.Context, row []sqlplugin.QueueV2MessageRow) (sql.Result, error) {
return sdb.conn.NamedExecContext(ctx,
templateEnqueueMessageQueryV2,
row,
)
}
func (sdb *db) RangeSelectFromQueueV2Messages(ctx context.Context, filter sqlplugin.QueueV2MessagesFilter) ([]sqlplugin.QueueV2MessageRow, error) {
var rows []sqlplugin.QueueV2MessageRow
err := sdb.conn.SelectContext(ctx,
&rows,
templateGetMessagesQueryV2,
filter.QueueType,
filter.QueueName,
filter.Partition,
filter.MinMessageID,
filter.PageSize,
)
return rows, err
}
func (sdb *db) RangeDeleteFromQueueV2Messages(ctx context.Context, filter sqlplugin.QueueV2MessagesFilter) (sql.Result, error) {
return sdb.conn.ExecContext(ctx,
templateRangeDeleteMessagesQueryV2,
filter.QueueType,
filter.QueueName,
filter.Partition,
filter.MinMessageID,
filter.MaxMessageID,
)
}
func (sdb *db) GetLastEnqueuedMessageIDForUpdateV2(ctx context.Context, filter sqlplugin.QueueV2Filter) (int64, error) {
var lastMessageID int64
err := sdb.conn.GetContext(ctx,
&lastMessageID,
templateGetLastMessageIDQueryV2,
filter.QueueType,
filter.QueueName,
filter.Partition,
)
return lastMessageID, err
}