go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/sqlite/queue.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 sqlite 28 29 import ( 30 "context" 31 "database/sql" 32 33 "go.temporal.io/server/common/persistence" 34 "go.temporal.io/server/common/persistence/sql/sqlplugin" 35 ) 36 37 const ( 38 templateEnqueueMessageQuery = `INSERT INTO queue (queue_type, message_id, message_payload, message_encoding) VALUES(:queue_type, :message_id, :message_payload, :message_encoding)` 39 templateGetMessageQuery = `SELECT message_id, message_payload, message_encoding FROM queue WHERE queue_type = ? and message_id = ?` 40 templateGetMessagesQuery = `SELECT message_id, message_payload, message_encoding FROM queue WHERE queue_type = ? and message_id > ? and message_id <= ? ORDER BY message_id ASC LIMIT ?` 41 templateDeleteMessageQuery = `DELETE FROM queue WHERE queue_type = ? and message_id = ?` 42 templateRangeDeleteMessagesQuery = `DELETE FROM queue WHERE queue_type = ? and message_id > ? and message_id <= ?` 43 44 templateGetLastMessageIDQuery = `SELECT message_id FROM queue WHERE message_id >= (SELECT message_id FROM queue WHERE queue_type=? ORDER BY message_id DESC LIMIT 1)` 45 46 templateCreateQueueMetadataQuery = `INSERT INTO queue_metadata (queue_type, data, data_encoding, version) VALUES(:queue_type, :data, :data_encoding, :version)` 47 templateUpdateQueueMetadataQuery = `UPDATE queue_metadata SET data = :data, data_encoding = :data_encoding, version = :version+1 WHERE queue_type = :queue_type and version = :version` 48 templateGetQueueMetadataQuery = `SELECT data, data_encoding, version from queue_metadata WHERE queue_type = ?` 49 templateLockQueueMetadataQuery = templateGetQueueMetadataQuery 50 ) 51 52 // InsertIntoMessages inserts a new row into queue table 53 func (mdb *db) InsertIntoMessages( 54 ctx context.Context, 55 row []sqlplugin.QueueMessageRow, 56 ) (sql.Result, error) { 57 return mdb.conn.NamedExecContext(ctx, 58 templateEnqueueMessageQuery, 59 row, 60 ) 61 } 62 63 func (mdb *db) SelectFromMessages( 64 ctx context.Context, 65 filter sqlplugin.QueueMessagesFilter, 66 ) ([]sqlplugin.QueueMessageRow, error) { 67 var rows []sqlplugin.QueueMessageRow 68 err := mdb.conn.SelectContext(ctx, 69 &rows, 70 templateGetMessageQuery, 71 filter.QueueType, 72 filter.MessageID, 73 ) 74 return rows, err 75 } 76 77 func (mdb *db) RangeSelectFromMessages( 78 ctx context.Context, 79 filter sqlplugin.QueueMessagesRangeFilter, 80 ) ([]sqlplugin.QueueMessageRow, error) { 81 var rows []sqlplugin.QueueMessageRow 82 err := mdb.conn.SelectContext(ctx, 83 &rows, 84 templateGetMessagesQuery, 85 filter.QueueType, 86 filter.MinMessageID, 87 filter.MaxMessageID, 88 filter.PageSize, 89 ) 90 return rows, err 91 } 92 93 // DeleteFromMessages deletes message with a messageID from the queue 94 func (mdb *db) DeleteFromMessages( 95 ctx context.Context, 96 filter sqlplugin.QueueMessagesFilter, 97 ) (sql.Result, error) { 98 return mdb.conn.ExecContext(ctx, 99 templateDeleteMessageQuery, 100 filter.QueueType, 101 filter.MessageID, 102 ) 103 } 104 105 // RangeDeleteFromMessages deletes messages before messageID from the queue 106 func (mdb *db) RangeDeleteFromMessages( 107 ctx context.Context, 108 filter sqlplugin.QueueMessagesRangeFilter, 109 ) (sql.Result, error) { 110 return mdb.conn.ExecContext(ctx, 111 templateRangeDeleteMessagesQuery, 112 filter.QueueType, 113 filter.MinMessageID, 114 filter.MaxMessageID, 115 ) 116 } 117 118 // GetLastEnqueuedMessageIDForUpdate returns the last enqueued message ID 119 func (mdb *db) GetLastEnqueuedMessageIDForUpdate( 120 ctx context.Context, 121 queueType persistence.QueueType, 122 ) (int64, error) { 123 var lastMessageID int64 124 err := mdb.conn.GetContext(ctx, 125 &lastMessageID, 126 templateGetLastMessageIDQuery, 127 queueType, 128 ) 129 return lastMessageID, err 130 } 131 132 func (mdb *db) InsertIntoQueueMetadata( 133 ctx context.Context, 134 row *sqlplugin.QueueMetadataRow, 135 ) (sql.Result, error) { 136 return mdb.conn.NamedExecContext(ctx, 137 templateCreateQueueMetadataQuery, 138 row, 139 ) 140 } 141 142 func (mdb *db) UpdateQueueMetadata( 143 ctx context.Context, 144 row *sqlplugin.QueueMetadataRow, 145 ) (sql.Result, error) { 146 return mdb.conn.NamedExecContext(ctx, 147 templateUpdateQueueMetadataQuery, 148 row, 149 ) 150 } 151 152 func (mdb *db) SelectFromQueueMetadata( 153 ctx context.Context, 154 filter sqlplugin.QueueMetadataFilter, 155 ) (*sqlplugin.QueueMetadataRow, error) { 156 var row sqlplugin.QueueMetadataRow 157 err := mdb.conn.GetContext(ctx, 158 &row, 159 templateGetQueueMetadataQuery, 160 filter.QueueType, 161 ) 162 if err != nil { 163 return nil, err 164 } 165 return &row, nil 166 } 167 168 func (mdb *db) LockQueueMetadata( 169 ctx context.Context, 170 filter sqlplugin.QueueMetadataFilter, 171 ) (*sqlplugin.QueueMetadataRow, error) { 172 var row sqlplugin.QueueMetadataRow 173 err := mdb.conn.GetContext(ctx, 174 &row, 175 templateLockQueueMetadataQuery, 176 filter.QueueType, 177 ) 178 if err != nil { 179 return nil, err 180 } 181 return &row, nil 182 }