All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
as_txn.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2025 Aerospike, Inc.
3 *
4 * Portions may be licensed to Aerospike, Inc. under one or more contributor
5 * license agreements.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17#pragma once
18
19#include <aerospike/as_std.h>
20#include <aerospike/as_key.h>
21#include <aerospike/as_error.h>
22#include <aerospike/as_status.h>
23#include <pthread.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29//---------------------------------
30// Macros
31//---------------------------------
32
33/**
34 * Default number of transaction read hash buckets.
35 */
36#define AS_TXN_READ_CAPACITY_DEFAULT 128
37
38/**
39 * Default number of transaction write hash buckets.
40 */
41#define AS_TXN_WRITE_CAPACITY_DEFAULT 128
42
43//---------------------------------
44// Types
45//---------------------------------
46
47/**
48 * Transaction state.
49 */
56
57/**
58 * Transaction key.
59 */
60typedef struct as_txn_key {
62 char set[64];
63 uint64_t version;
66
67/**
68 * Transaction hash map row.
69 */
70typedef struct {
71 bool used;
74
75/**
76 * Transaction hash map.
77 */
78typedef struct {
79 pthread_mutex_t lock;
80 uint32_t n_eles;
81 uint32_t n_rows;
84
85/**
86 * Transaction. Each command in the transaction must use the same namespace.
87 */
100
101/**
102 * Transaction iterator.
103 */
110
111//---------------------------------
112// Functions
113//---------------------------------
114
115/**
116 * Initialize transaction, assign random transaction id and initialize
117 * reads/writes hashmaps with default capacities.
118 *
119 * The default client transaction timeout is zero. This means use the server configuration mrt-duration
120 * as the transaction timeout. The default mrt-duration is 10 seconds.
121 *
122 * Call this function or as_txn_init_capacity(), but not both. Do not use this function for async
123 * commands (use as_txn_create() instead).
124 *
125 * @param txn Transaction.
126 */
127AS_EXTERN void
129
130/**
131 * Initialize transaction, assign random transaction id and initialize
132 * reads/writes hashmaps with given capacities.
133 *
134 * The default client transaction timeout is zero. This means use the server configuration mrt-duration
135 * as the transaction timeout. The default mrt-duration is 10 seconds.
136 *
137 * Call this function or as_txn_init(), but not both. Do not use this function for async commands
138 * (use as_txn_create_capacity() instead).
139 *
140 * @param txn Transaction.
141 * @param reads_capacity expected number of record reads in the transaction. Minimum value is 16.
142 * @param writes_capacity expected number of record writes in the transaction. Minimum value is 16.
143 */
144AS_EXTERN void
145as_txn_init_capacity(as_txn* txn, uint32_t reads_capacity, uint32_t writes_capacity);
146
147/**
148 * Create transaction on heap, assign random transaction id and initialize
149 * reads/writes hashmaps with default capacities.
150 *
151 * The default client transaction timeout is zero. This means use the server configuration mrt-duration
152 * as the transaction timeout. The default mrt-duration is 10 seconds.
153 */
156
157/**
158 * Create transaction on heap, assign random transaction id and initialize
159 * reads/writes hashmaps with given capacities.
160 *
161 * The default client transaction timeout is zero. This means use the server configuration mrt-duration
162 * as the transaction timeout. The default mrt-duration is 10 seconds.
163 *
164 * @param reads_capacity expected number of record reads in the transaction. Minimum value is 16.
165 * @param writes_capacity expected number of record writes in the transaction. Minimum value is 16.
166 */
168as_txn_create_capacity(uint32_t reads_capacity, uint32_t writes_capacity);
169
170/**
171 * Destroy transaction.
172 */
173AS_EXTERN void
175
176/**
177 * Set transaction timeout in seconds. The timer starts when the transaction monitor record is created.
178 * This occurs when the first command in the transaction is executed. If the timeout is reached before
179 * an aerospike_commit() or aerospike_abort() is called, the server will expire and rollback the transaction.
180 *
181 * If the transaction timeout is zero, the server configuration mrt-duration is used.
182 * The default mrt-duration is 10 seconds.
183 */
184static inline void
185as_txn_set_timeout(as_txn* txn, uint32_t timeout)
186{
187 txn->timeout = timeout;
188}
189
190/**
191 * Return read hash size.
192 */
193static inline uint32_t
195{
196 return txn->reads.n_eles;
197}
198
199/**
200 * Process the results of a record read. For internal use only.
201 */
202AS_EXTERN void
203as_txn_on_read(as_txn* txn, const uint8_t* digest, const char* set, uint64_t version);
204
205/**
206 * Get record version for a given key. For internal use only.
207 */
208AS_EXTERN uint64_t
209as_txn_get_read_version(as_txn* txn, const uint8_t* digest);
210
211/**
212 * Return write hash size.
213 */
214static inline uint32_t
216{
217 return txn->writes.n_eles;
218}
219
220/**
221 * Process the results of a record write. For internal use only.
222 */
223AS_EXTERN void
224as_txn_on_write(as_txn* txn, const uint8_t* digest, const char* set, uint64_t version, int rc);
225
226/**
227 * Add key to write hash when write command is in doubt (usually caused by timeout). For internal use only.
228 */
229AS_EXTERN void
230as_txn_on_write_in_doubt(as_txn* txn, const uint8_t* digest, const char* set);
231
232/**
233 * Return if writes hashmap contains the given key.
234 */
235AS_EXTERN bool
237
238/**
239 * Verify that the transaction state allows future commands.
240 */
243
244/**
245 * Set transaction namespace only if doesn't already exist.
246 * If namespace already exists, verify new namespace is the same.
247 * For internal use only.
248 */
250as_txn_set_ns(as_txn* txn, const char* ns, as_error* err);
251
252/**
253 * Return if the transaction monitor record should be closed/deleted. For internal use only.
254 */
255static inline bool
257{
258 return txn->deadline != 0 && !txn->write_in_doubt;
259}
260
261/**
262 * Does transaction monitor record exist.
263 */
264static inline bool
266{
267 return txn->deadline != 0;
268}
269
270/**
271 * Clear transaction. Remove all tracked keys. For internal use only.
272 */
273AS_EXTERN void
275
276/**
277 * Initialize read keys iterator.
278 */
279static inline void
281{
282 iter->khash = &txn->reads;
283 iter->row = txn->reads.table;
284 iter->ele = NULL;
285 iter->idx = 0;
286}
287
288/**
289 * Initialize write keys iterator.
290 */
291static inline void
293{
294 iter->khash = &txn->writes;
295 iter->row = txn->writes.table;
296 iter->ele = NULL;
297 iter->idx = 0;
298}
299
300/**
301 * Return next available hash element or NULL if no more elements are available.
302 */
305
306#ifdef __cplusplus
307} // end extern "C"
308#endif
char as_namespace[AS_NAMESPACE_MAX_SIZE]
Definition as_key.h:55
uint8_t as_digest_value[AS_DIGEST_VALUE_SIZE]
Definition as_key.h:65
uint8_t version
Definition as_proto.h:0
as_status
Definition as_status.h:30
#define AS_EXTERN
Definition as_std.h:25
AS_EXTERN void as_txn_on_write_in_doubt(as_txn *txn, const uint8_t *digest, const char *set)
static uint32_t as_txn_writes_size(as_txn *txn)
Definition as_txn.h:215
AS_EXTERN as_txn_key * as_txn_iter_next(as_txn_iter *iter)
AS_EXTERN as_status as_txn_verify_command(as_txn *txn, as_error *err)
AS_EXTERN uint64_t as_txn_get_read_version(as_txn *txn, const uint8_t *digest)
static bool as_txn_monitor_exists(as_txn *txn)
Definition as_txn.h:265
static uint32_t as_txn_reads_size(as_txn *txn)
Definition as_txn.h:194
static void as_txn_set_timeout(as_txn *txn, uint32_t timeout)
Definition as_txn.h:185
AS_EXTERN as_txn * as_txn_create(void)
AS_EXTERN as_status as_txn_set_ns(as_txn *txn, const char *ns, as_error *err)
AS_EXTERN void as_txn_init(as_txn *txn)
AS_EXTERN void as_txn_clear(as_txn *txn)
as_txn_state
Definition as_txn.h:50
@ AS_TXN_STATE_OPEN
Definition as_txn.h:51
@ AS_TXN_STATE_ABORTED
Definition as_txn.h:54
@ AS_TXN_STATE_COMMITTED
Definition as_txn.h:53
@ AS_TXN_STATE_VERIFIED
Definition as_txn.h:52
AS_EXTERN as_txn * as_txn_create_capacity(uint32_t reads_capacity, uint32_t writes_capacity)
AS_EXTERN void as_txn_destroy(as_txn *txn)
static bool as_txn_close_monitor(as_txn *txn)
Definition as_txn.h:256
AS_EXTERN void as_txn_on_write(as_txn *txn, const uint8_t *digest, const char *set, uint64_t version, int rc)
static void as_txn_iter_reads(as_txn_iter *iter, as_txn *txn)
Definition as_txn.h:280
AS_EXTERN void as_txn_init_capacity(as_txn *txn, uint32_t reads_capacity, uint32_t writes_capacity)
AS_EXTERN void as_txn_on_read(as_txn *txn, const uint8_t *digest, const char *set, uint64_t version)
AS_EXTERN bool as_txn_writes_contain(as_txn *txn, const as_key *key)
static void as_txn_iter_writes(as_txn_iter *iter, as_txn *txn)
Definition as_txn.h:292
as_txn_key head
Definition as_txn.h:72
as_txn_hash_row * table
Definition as_txn.h:82
pthread_mutex_t lock
Definition as_txn.h:79
uint32_t n_rows
Definition as_txn.h:81
uint32_t n_eles
Definition as_txn.h:80
as_txn_key * ele
Definition as_txn.h:107
uint32_t idx
Definition as_txn.h:108
as_txn_hash * khash
Definition as_txn.h:105
as_txn_hash_row * row
Definition as_txn.h:106
as_digest_value digest
Definition as_txn.h:61
struct as_txn_key * next
Definition as_txn.h:64
char set[64]
Definition as_txn.h:62
uint64_t version
Definition as_txn.h:63
as_namespace ns
Definition as_txn.h:90
as_txn_hash reads
Definition as_txn.h:91
as_txn_hash writes
Definition as_txn.h:92
uint64_t id
Definition as_txn.h:89
bool free
Definition as_txn.h:98
as_txn_state state
Definition as_txn.h:95
bool write_in_doubt
Definition as_txn.h:96
uint32_t timeout
Definition as_txn.h:93
uint32_t deadline
Definition as_txn.h:94
bool in_doubt
Definition as_txn.h:97