go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/mysql/queue_v2.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2021 Datadog, Inc. 4 // 5 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 6 // 7 // Copyright (c) 2020 Uber Technologies, Inc. 8 // 9 // Permission is hereby granted, free of charge, to any person obtaining a copy 10 // of this software and associated documentation files (the "Software"), to deal 11 // in the Software without restriction, including without limitation the rights 12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 // copies of the Software, and to permit persons to whom the Software is 14 // furnished to do so, subject to the following conditions: 15 // 16 // The above copyright notice and this permission notice shall be included in 17 // all copies or substantial portions of the Software. 18 // 19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 // THE SOFTWARE. 26 27 package mysql 28 29 import ( 30 "context" 31 "database/sql" 32 33 "go.temporal.io/server/common/persistence/sql/sqlplugin" 34 ) 35 36 const ( 37 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)` 38 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 ?` 39 templateRangeDeleteMessagesQueryV2 = `DELETE FROM queue_messages WHERE queue_type = ? and queue_name = ? and queue_partition = ? and message_id >= ? and message_id <= ?` 40 templateGetLastMessageIDQueryV2 = `SELECT max(message_id) FROM queue_messages WHERE queue_type=? and queue_name=? and queue_partition=? and message_id >= (SELECT message_id FROM queue_messages WHERE queue_type=? and queue_name=? and queue_partition=? ORDER BY message_id DESC LIMIT 1) FOR UPDATE` 41 templateCreateQueueMetadataQueryV2 = `INSERT INTO queues (queue_type, queue_name, metadata_payload, metadata_encoding) VALUES(:queue_type, :queue_name, :metadata_payload, :metadata_encoding)` 42 templateUpdateQueueMetadataQueryV2 = `UPDATE queues SET metadata_payload = :metadata_payload, metadata_encoding = :metadata_encoding WHERE queue_type = :queue_type and queue_name = :queue_name` 43 templateGetQueueMetadataQueryV2 = `SELECT metadata_payload, metadata_encoding from queues WHERE queue_type=? and queue_name=?` 44 templateGetQueueMetadataQueryV2ForUpdate = `SELECT metadata_payload, metadata_encoding from queues WHERE queue_type=? and queue_name=? FOR UPDATE` 45 templateGetNameFromQueueMetadataV2 = `SELECT queue_type, queue_name, metadata_payload, metadata_encoding from queues WHERE queue_type=? LIMIT ? OFFSET ?` 46 ) 47 48 func (mdb *db) InsertIntoQueueV2Metadata(ctx context.Context, row *sqlplugin.QueueV2MetadataRow) (sql.Result, error) { 49 return mdb.conn.NamedExecContext(ctx, 50 templateCreateQueueMetadataQueryV2, 51 row, 52 ) 53 } 54 55 func (mdb *db) UpdateQueueV2Metadata(ctx context.Context, row *sqlplugin.QueueV2MetadataRow) (sql.Result, error) { 56 return mdb.conn.NamedExecContext(ctx, 57 templateUpdateQueueMetadataQueryV2, 58 row, 59 ) 60 } 61 62 func (mdb *db) SelectFromQueueV2Metadata(ctx context.Context, filter sqlplugin.QueueV2MetadataFilter) (*sqlplugin.QueueV2MetadataRow, error) { 63 var row sqlplugin.QueueV2MetadataRow 64 err := mdb.conn.GetContext(ctx, 65 &row, 66 templateGetQueueMetadataQueryV2, 67 filter.QueueType, 68 filter.QueueName, 69 ) 70 if err != nil { 71 return nil, err 72 } 73 return &row, nil 74 } 75 76 func (mdb *db) SelectFromQueueV2MetadataForUpdate(ctx context.Context, filter sqlplugin.QueueV2MetadataFilter) (*sqlplugin.QueueV2MetadataRow, error) { 77 var row sqlplugin.QueueV2MetadataRow 78 err := mdb.conn.GetContext(ctx, 79 &row, 80 templateGetQueueMetadataQueryV2ForUpdate, 81 filter.QueueType, 82 filter.QueueName, 83 ) 84 if err != nil { 85 return nil, err 86 } 87 return &row, nil 88 } 89 90 func (mdb *db) SelectNameFromQueueV2Metadata(ctx context.Context, filter sqlplugin.QueueV2MetadataTypeFilter) ([]sqlplugin.QueueV2MetadataRow, error) { 91 var rows []sqlplugin.QueueV2MetadataRow 92 err := mdb.conn.SelectContext(ctx, 93 &rows, 94 templateGetNameFromQueueMetadataV2, 95 filter.QueueType, 96 filter.PageSize, 97 filter.PageOffset, 98 ) 99 if err != nil { 100 return nil, err 101 } 102 return rows, nil 103 } 104 105 func (mdb *db) InsertIntoQueueV2Messages(ctx context.Context, row []sqlplugin.QueueV2MessageRow) (sql.Result, error) { 106 return mdb.conn.NamedExecContext(ctx, 107 templateEnqueueMessageQueryV2, 108 row, 109 ) 110 } 111 112 func (mdb *db) RangeSelectFromQueueV2Messages(ctx context.Context, filter sqlplugin.QueueV2MessagesFilter) ([]sqlplugin.QueueV2MessageRow, error) { 113 var rows []sqlplugin.QueueV2MessageRow 114 err := mdb.conn.SelectContext(ctx, 115 &rows, 116 templateGetMessagesQueryV2, 117 filter.QueueType, 118 filter.QueueName, 119 filter.Partition, 120 filter.MinMessageID, 121 filter.PageSize, 122 ) 123 return rows, err 124 } 125 126 func (mdb *db) RangeDeleteFromQueueV2Messages(ctx context.Context, filter sqlplugin.QueueV2MessagesFilter) (sql.Result, error) { 127 return mdb.conn.ExecContext(ctx, 128 templateRangeDeleteMessagesQueryV2, 129 filter.QueueType, 130 filter.QueueName, 131 filter.Partition, 132 filter.MinMessageID, 133 filter.MaxMessageID, 134 ) 135 } 136 137 func (mdb *db) GetLastEnqueuedMessageIDForUpdateV2(ctx context.Context, filter sqlplugin.QueueV2Filter) (int64, error) { 138 var lastMessageID *int64 139 err := mdb.conn.GetContext(ctx, 140 &lastMessageID, 141 templateGetLastMessageIDQueryV2, 142 filter.QueueType, 143 filter.QueueName, 144 filter.Partition, 145 filter.QueueType, 146 filter.QueueName, 147 filter.Partition, 148 ) 149 if lastMessageID == nil { 150 // The layer of code above us expects ErrNoRows when the queue is empty. MAX() yields 151 // null when the queue is empty, so we need to turn that into the correct error. 152 return 0, sql.ErrNoRows 153 } 154 return *lastMessageID, err 155 }