4 Copyright (C) Amitay Isaacs 2015
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef __CTDB_CLIENT_H__
21 #define __CTDB_CLIENT_H__
26 #include "protocol/protocol.h"
27 #include "common/srvid.h"
32 * @brief Client api to talk to ctdb daemon
34 * This API allows to connect to ctdb daemon, perform various database
35 * operations, send controls to ctdb daemon and send messages to other ctdb
40 * @brief The abstract context that holds client connection to ctdb daemon
42 struct ctdb_client_context
;
45 * @brief The abstract context that represents a clustered database
47 struct ctdb_db_context
;
50 * @brief The abstract context that represents a record from a distributed
53 struct ctdb_record_handle
;
56 * @brief The abstract context that represents a transaction on a replicated
59 struct ctdb_transaction_handle
;
62 * @brief Client callback function
64 * This function can be registered to be invoked in case of ctdb daemon going
67 typedef void (*ctdb_client_callback_func_t
)(void *private_data
);
70 * @brief Initialize and connect to ctdb daemon
72 * This returns a ctdb client context. Freeing this context will free the
73 * connection to ctdb daemon and any memory associated with it.
75 * If the connection to ctdb daemon is lost, the client will terminate
76 * automatically as the library will call exit(). If the client code
77 * wants to perform cleanup or wants to re-establish a new connection,
78 * the client should register a disconnect callback function.
80 * @see ctdb_client_set_disconnect_callback
82 * When a disconnect callback function is registered, client library will
83 * not call exit(). It is the responsibility of the client code to take
86 * @param[in] mem_ctx Talloc memory context
87 * @param[in] ev Tevent context
88 * @param[in] sockpath Path to ctdb daemon unix domain socket
89 * @param[out] result The new ctdb client context
90 * @return 0 on succcess, errno on failure
92 int ctdb_client_init(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
94 struct ctdb_client_context
**result
);
97 * @brief Register a callback in case of client disconnection
99 * This allows client code to know if the connection to ctdb daemon is lost.
100 * This is useful if the client wants to re-establish a new connection to ctdb
103 * @param[in] client Client connection context
104 * @param[in] func Callback function
105 * @param[in] private_data private data for callback function
107 void ctdb_client_set_disconnect_callback(struct ctdb_client_context
*client
,
108 ctdb_client_callback_func_t func
,
112 * @brief Get the node number of the current node
114 * @param[in] client Client connection context
115 * return node number on success, CTDB_UNKNOWN_PNN on error
117 uint32_t ctdb_client_pnn(struct ctdb_client_context
*client
);
120 * @brief Client event loop waiting for a flag
122 * This can used to wait for asynchronous computations to complete.
123 * When this function is called, it will run tevent event loop and wait
124 * till the done flag is set to true. This function will block and will
125 * not return as long as the done flag is false.
127 * @param[in] ev Tevent context
128 * @param[in] done Boolean flag to indicate when to stop waiting
130 void ctdb_client_wait(struct tevent_context
*ev
, bool *done
);
133 * @brief Client event loop waiting for a flag with timeout
135 * This can be used to wait for asynchronous computations to complete.
136 * When this function is called, it will run tevent event loop and wait
137 * till the done flag is set to true or if the timeout occurs.
139 * This function will return when either
140 * - done flag is set to true, or
141 * - timeout has occurred.
143 * @param[in] ev Tevent context
144 * @param[in] done Boolean flag to indicate when to stop waiting
145 * @param[in] timeout How long to wait
146 * @return 0 on succes, ETIME on timeout, and errno on failure
148 int ctdb_client_wait_timeout(struct tevent_context
*ev
, bool *done
,
149 struct timeval timeout
);
152 * @brief Async computation start to wait till recovery is completed
154 * CTDB daemon does not perform many operations while in recovery (especially
155 * database operations). This computation allows to wait till ctdb daemon has
158 * @param[in] mem_ctx Talloc memory context
159 * @param[in] ev Tevent context
160 * @param[in] client Client connection context
161 * @return new tevent request, or NULL on failure
163 struct tevent_req
*ctdb_recovery_wait_send(TALLOC_CTX
*mem_ctx
,
164 struct tevent_context
*ev
,
165 struct ctdb_client_context
*client
);
168 * @brief Async computation end to wait till recovery is completed
170 * @param[in] req Tevent request
171 * @param[out] perr errno in case of failure
172 * @return true on success, false on failure
174 bool ctdb_recovery_wait_recv(struct tevent_req
*req
, int *perr
);
177 * @brief Sync wrapper for ctdb_recovery_wait computation
179 * @param[in] ev Tevent context
180 * @param[in] client Client connection context
181 * @return true on success, false on failure
183 bool ctdb_recovery_wait(struct tevent_context
*ev
,
184 struct ctdb_client_context
*client
);
187 * @brief Async computation start to migrate a database record
189 * This sends a request to ctdb daemon to migrate a database record to
190 * the local node. CTDB daemon will locate the data master for the record
191 * and will migrate record (and the data master) to the current node.
193 * @see ctdb_fetch_lock_send
195 * @param[in] mem_ctx Talloc memory context
196 * @param[in] ev Tevent context
197 * @param[in] client Client connection context
198 * @param[in] request CTDB request data
199 * @return a new tevent req, or NULL on failure
201 struct tevent_req
*ctdb_client_call_send(TALLOC_CTX
*mem_ctx
,
202 struct tevent_context
*ev
,
203 struct ctdb_client_context
*client
,
204 struct ctdb_req_call
*request
);
207 * @brief Async computation end to migrate a database record
209 * @param[in] req Tevent request
210 * @param[in] mem_ctx Talloc memory context
211 * @param[out] reply CTDB reply data
212 * @param[out] perr errno in case of failure
213 * @return true on success, false on failure
215 bool ctdb_client_call_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
216 struct ctdb_reply_call
**reply
, int *perr
);
220 * @brief Async computation start to send a message to remote client(s)
222 * This sends a message to ctdb clients on a remote node. All the
223 * messages are associated with a specific SRVID. All the clients on the
224 * remote node listening to that SRVID, will get the message.
226 * Clients can register and deregister for messages for a SRVID using
227 * ctdb_client_set_message_handler() and ctdb_client_remove_message_handler().
229 * @see ctdb_client_set_message_handler_send,
230 * ctdb_client_remove_message_handler_send
232 * @param[in] mem_ctx Talloc memory context
233 * @param[in] ev Tevent context
234 * @param[in] client Client connection context
235 * @param[in] destnode Remote node id
236 * @param[in] message Message to send
237 * @return a new tevent req on success, NULL on failure
239 struct tevent_req
*ctdb_client_message_send(TALLOC_CTX
*mem_ctx
,
240 struct tevent_context
*ev
,
241 struct ctdb_client_context
*client
,
243 struct ctdb_req_message
*message
);
246 * @brief Async computation end to send a message to remote client(s)
248 * @param[in] req Tevent request
249 * @param[out] perr errno in case of failure
250 * @return true on success, false on failure
252 bool ctdb_client_message_recv(struct tevent_req
*req
, int *perr
);
255 * @brief Sync wrapper to send a message to client(s) on remote node
257 * @param[in] mem_ctx Talloc memory context
258 * @param[in] ev Tevent context
259 * @param[in] client Client connection context
260 * @param[in] destnode Node id
261 * @param[in] message Message to send
263 int ctdb_client_message(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
264 struct ctdb_client_context
*client
,
265 uint32_t destnode
, struct ctdb_req_message
*message
);
268 * @brief Async computation start to send a message to multiple nodes
270 * This sends a message to ctdb clients on multiple remote nodes. All the
271 * messages are associated with a specific SRVID. All the clients on remote
272 * nodes listening to that SRVID, will get the message.
274 * Clients can register and deregister for messages for a SRVID using
275 * ctdb_client_set_message_handler() and ctdb_client_remove_message_handler().
277 * @see ctdb_client_set_message_handler_send,
278 * ctdb_client_remove_message_handler_send
280 * @param[in] mem_ctx Talloc memory context
281 * @param[in] ev Tevent context
282 * @param[in] client Client connection context
283 * @param[in] pnn_list List of node ids
284 * @param[in] count Number of node ids
285 * @param[in] message Message to send
286 * @return a new tevent req on success, NULL on failure
288 struct tevent_req
*ctdb_client_message_multi_send(
290 struct tevent_context
*ev
,
291 struct ctdb_client_context
*client
,
292 uint32_t *pnn_list
, int count
,
293 struct ctdb_req_message
*message
);
296 * @brief Async computation end to send a message to multiple nodes
298 * @param[in] req Tevent request
299 * @param[out] perr errno in case of failure
300 * @param[in] mem_ctx Talloc memory context
301 * @param[out] perr_list The status from each node id
302 * @return true on success, false on failure
304 * If perr_list is not NULL, then the status (0 on success, errno on failure)
305 * of sending message to each of the node in the specified node list. The
306 * perr_list is an array of the same size as of pnn_list.
308 bool ctdb_client_message_multi_recv(struct tevent_req
*req
, int *perr
,
309 TALLOC_CTX
*mem_ctx
, int **perr_list
);
312 * @brief Sync wrapper to send a message to multiple nodes
314 * @param[in] mem_ctx Talloc memory context
315 * @param[in] ev Tevent context
316 * @param[in] client Client connection context
317 * @param[in] pnn_list List of node ids
318 * @param[in] count Number of node ids
319 * @param[in] message Message to send
320 * @param[out] perr_list The status from each node id
321 * @return 0 on success, errno on failure
323 int ctdb_client_message_multi(TALLOC_CTX
*mem_ctx
,
324 struct tevent_context
*ev
,
325 struct ctdb_client_context
*client
,
326 uint32_t *pnn_list
, int count
,
327 struct ctdb_req_message
*message
,
331 * @brief Async computation start to receive messages for a SRVID
333 * This computation informs ctdb that the client is interested in all messages
334 * for a specific SRVID.
336 * @param[in] mem_ctx Talloc memory context
337 * @param[in] ev Tevent context
338 * @param[in] client Client connection context
339 * @param[in] srvid SRVID
340 * @param[in] handler Callback function to call when a message is received
341 * @param[in] private_data Private data for callback
342 * @return a new tevent req on success, NULL on failure
344 struct tevent_req
*ctdb_client_set_message_handler_send(
346 struct tevent_context
*ev
,
347 struct ctdb_client_context
*client
,
349 srvid_handler_fn handler
,
353 * @brief Async computation end to receive messages for a SRVID
355 * @param[in] req Tevent request
356 * @param[out] perr errno in case of failure
357 * @return true on success, false on failure
359 bool ctdb_client_set_message_handler_recv(struct tevent_req
*req
, int *perr
);
362 * Sync wrapper to receive messages for a SRVID
364 * @param[in] ev Tevent context
365 * @param[in] client Client connection context
366 * @param[in] srvid SRVID
367 * @param[in] handler Callback function to call when a message is received
368 * @param[in] private_data Private data for callback
369 * @return 0 on success, errno on failure
371 int ctdb_client_set_message_handler(struct tevent_context
*ev
,
372 struct ctdb_client_context
*client
,
373 uint64_t srvid
, srvid_handler_fn handler
,
377 * @brief Async computation start to stop receiving messages for a SRVID
379 * This computation informs ctdb that the client is no longer interested in
380 * messages for a specific SRVID.
382 * @param[in] mem_ctx Talloc memory context
383 * @param[in] ev Tevent context
384 * @param[in] client Client connection context
385 * @param[in] srvid SRVID
386 * @param[in] private_data Private data used to register callback
387 * @return a new tevent req on success, NULL on failure
389 struct tevent_req
*ctdb_client_remove_message_handler_send(
391 struct tevent_context
*ev
,
392 struct ctdb_client_context
*client
,
397 * @brief Async computation end to stop receiving messages for a SRVID
399 * @param[in] req Tevent request
400 * @param[out] perr errno in case of failure
401 * @return true on success, false on failure
403 bool ctdb_client_remove_message_handler_recv(struct tevent_req
*req
,
407 * Sync wrapper to stop receiving messages for a SRVID
409 * @param[in] ev Tevent context
410 * @param[in] client Client connection context
411 * @param[in] srvid SRVID
412 * @param[in] private_data Private data used to register callback
413 * @return 0 on success, errno on failure
415 int ctdb_client_remove_message_handler(struct tevent_context
*ev
,
416 struct ctdb_client_context
*client
,
417 uint64_t srvid
, void *private_data
);
420 * @brief Async computation start to send a control to ctdb daemon
422 * @param[in] mem_ctx Talloc memory context
423 * @param[in] ev Tevent context
424 * @param[in] client Client connection context
425 * @param[in] destnode Node id
426 * @param[in] timeout How long to wait
427 * @param[in] request Control request
428 * @return a new tevent req on success, NULL on failure
430 struct tevent_req
*ctdb_client_control_send(TALLOC_CTX
*mem_ctx
,
431 struct tevent_context
*ev
,
432 struct ctdb_client_context
*client
,
434 struct timeval timeout
,
435 struct ctdb_req_control
*request
);
438 * @brief Async computation end to send a control to ctdb daemon
440 * @param[in] req Tevent request
441 * @param[out] perr errno in case of failure
442 * @param[in] mem_ctx Talloc memory context
443 * @param[out] preply Control reply
444 * @return true on success, false on failure
446 bool ctdb_client_control_recv(struct tevent_req
*req
, int *perr
,
448 struct ctdb_reply_control
**preply
);
451 * @brief Sync wrapper to send a control to ctdb daemon
453 * @param[in] mem_ctx Talloc memory context
454 * @param[in] ev Tevent context
455 * @param[in] client Client connection context
456 * @param[in] destnode Node id
457 * @param[in] timeout How long to wait
458 * @param[in] request Control request
459 * @param[out] preply Control reply
460 * @return 0 on success, errno on failure
462 int ctdb_client_control(TALLOC_CTX
*mem_ctx
,
463 struct tevent_context
*ev
,
464 struct ctdb_client_context
*client
,
466 struct timeval timeout
,
467 struct ctdb_req_control
*request
,
468 struct ctdb_reply_control
**preply
);
471 * @brief Async computation start to send a control to multiple nodes
473 * @param[in] mem_ctx Talloc memory context
474 * @param[in] ev Tevent context
475 * @param[in] client Client connection context
476 * @param[in] pnn_list List of node ids
477 * @param[in] count Number of node ids
478 * @param[in] timeout How long to wait
479 * @param[in] request Control request
480 * @return a new tevent req on success, NULL on failure
482 struct tevent_req
*ctdb_client_control_multi_send(
484 struct tevent_context
*ev
,
485 struct ctdb_client_context
*client
,
486 uint32_t *pnn_list
, int count
,
487 struct timeval timeout
,
488 struct ctdb_req_control
*request
);
491 * @brief Async computation end to send a control to multiple nodes
493 * @param[in] req Tevent request
494 * @param[out] perr errno in case of failure
495 * @param[in] mem_ctx Talloc memory context
496 * @param[out] perr_list Status from each node
497 * @param[out] preply Control reply from each node
498 * @return true on success, false on failure
500 bool ctdb_client_control_multi_recv(struct tevent_req
*req
, int *perr
,
501 TALLOC_CTX
*mem_ctx
, int **perr_list
,
502 struct ctdb_reply_control
***preply
);
505 * @brief Sync wrapper to send a control to multiple nodes
507 * @param[in] mem_ctx Talloc memory context
508 * @param[in] ev Tevent context
509 * @param[in] client Client connection context
510 * @param[in] pnn_list List of node ids
511 * @param[in] count Number of node ids
512 * @param[in] timeout How long to wait
513 * @param[in] request Control request
514 * @param[out] perr_list Status from each node
515 * @param[out] preply Control reply from each node
516 * @return 0 on success, errno on failure
518 int ctdb_client_control_multi(TALLOC_CTX
*mem_ctx
,
519 struct tevent_context
*ev
,
520 struct ctdb_client_context
*client
,
521 uint32_t *pnn_list
, int count
,
522 struct timeval timeout
,
523 struct ctdb_req_control
*request
,
525 struct ctdb_reply_control
***preply
);
528 * @brief Check err_list for errors
530 * This is a convenience function to parse the err_list returned from
531 * functions that send requests to multiple nodes.
533 * If status from any of the node is non-zero, then return first non-zero
536 * If status from all the nodes is 0, then return 0.
538 * @param[in] pnn_list List of node ids
539 * @param[in] count Number of node ids
540 * @param[in] err_list Status from each node
541 * @param[out] pnn Node id in case of failure
542 * @return 0 if no failures, status from first failure
544 int ctdb_client_control_multi_error(uint32_t *pnn_list
, int count
,
545 int *err_list
, uint32_t *pnn
);
548 * @brief Async computation start to attach a database
550 * @param[in] mem_ctx Talloc memory context
551 * @param[in] ev Tevent context
552 * @param[in[ client Client connection context
553 * @param[in] timeout How long to wait
554 * @param[in] db_name Name of the database
555 * @param[in] db_flags Database flags
556 * @return a new tevent req on success, NULL on failure
558 struct tevent_req
*ctdb_attach_send(TALLOC_CTX
*mem_ctx
,
559 struct tevent_context
*ev
,
560 struct ctdb_client_context
*client
,
561 struct timeval timeout
,
562 const char *db_name
, uint8_t db_flags
);
565 * @brief Async computation end to attach a database
567 * @param[in] req Tevent request
568 * @param[out] perr errno in case of failure
569 * @param[out] result New database context
570 * @return true on success, false on failure
572 bool ctdb_attach_recv(struct tevent_req
*req
, int *perr
,
573 struct ctdb_db_context
**result
);
576 * @brief Sync wrapper to attach a database
578 * @param[in] ev Tevent context
579 * @param[in[ client Client connection context
580 * @param[in] timeout How long to wait
581 * @param[in] db_name Name of the database
582 * @param[in] db_flags Database flags
583 * @param[out] result New database context
584 * @return 0 on success, errno on failure
586 int ctdb_attach(struct tevent_context
*ev
,
587 struct ctdb_client_context
*client
,
588 struct timeval timeout
,
589 const char *db_name
, uint8_t db_flags
,
590 struct ctdb_db_context
**result
);
593 * @brief Async computation start to detach a database
595 * Only volatile databases can be detached at runtime.
597 * @param[in] mem_ctx Talloc memory context
598 * @param[in] ev Tevent context
599 * @param[in[ client Client connection context
600 * @param[in] timeout How long to wait
601 * @param[in] db_id Database id
602 * @return a new tevent req on success, NULL on failure
604 struct tevent_req
*ctdb_detach_send(TALLOC_CTX
*mem_ctx
,
605 struct tevent_context
*ev
,
606 struct ctdb_client_context
*client
,
607 struct timeval timeout
, uint32_t db_id
);
610 * @brief Async computation end to detach a database
612 * @param[in] req Tevent request
613 * @param[out] perr errno in case of failure
614 * @return true on success, false on failure
616 bool ctdb_detach_recv(struct tevent_req
*req
, int *perr
);
619 * @brief Sync wrapper to detach a database
621 * Only volatile databases can be detached at runtime.
623 * @param[in] ev Tevent context
624 * @param[in[ client Client connection context
625 * @param[in] timeout How long to wait
626 * @param[in] db_id Database id
627 * @return 0 on success, errno on failure
629 int ctdb_detach(struct tevent_context
*ev
,
630 struct ctdb_client_context
*client
,
631 struct timeval timeout
, uint32_t db_id
);
635 * @brief Get database id from database context
637 * @param[in] db Database context
638 * @return database id
640 uint32_t ctdb_db_id(struct ctdb_db_context
*db
);
643 * @brief Traverse a database locally on the node
645 * This function traverses a database locally on the node and for each record
646 * calls the parser function. If the parser function returns 1, the traverse
647 * will terminate. If parser function returns 0, the traverse will continue
648 * till all records in database are parsed.
650 * This is useful for replicated databases, since each node has exactly the
653 * @param[in] db Database context
654 * @param[in] readonly Is the traversal for reading or updating
655 * @param[in] extract_header Whether to extract ltdb header from record data
656 * @param[in] parser Record parsing function
657 * @param[in] private_data Private data for parser function
658 * @return 0 on success, non-zero return value from parser function
660 int ctdb_db_traverse_local(struct ctdb_db_context
*db
, bool readonly
,
662 ctdb_rec_parser_func_t parser
, void *private_data
);
665 * @brief Async computation start to a cluster-wide database traverse
667 * This function traverses a database on all the nodes and for each record
668 * calls the parser function. If the parser function returns 1, the traverse
669 * will terminate. If parser function returns 0, the traverse will continue
670 * till all records all on nodes are parsed.
672 * This is useful for distributed databases as the records are distributed
673 * among the cluster nodes.
675 * @param[in] mem_ctx Talloc memory context
676 * @param[in] ev Tevent context
677 * @param[in] client Client connection context
678 * @param[in] db Database context
679 * @param[in] destnode Node id
680 * @param[in] timeout How long to wait
681 * @param[in] parser Record parser function
682 * @param[in] private_data Private data for parser
683 * @return a new tevent req on success, NULL on failure
685 struct tevent_req
*ctdb_db_traverse_send(TALLOC_CTX
*mem_ctx
,
686 struct tevent_context
*ev
,
687 struct ctdb_client_context
*client
,
688 struct ctdb_db_context
*db
,
690 struct timeval timeout
,
691 ctdb_rec_parser_func_t parser
,
695 * @brief Async computation end to a cluster-wide database traverse
697 * @param[in] req Tevent request
698 * @param[out] perr errno in case of failure
699 * @return true on success, false on failure
701 bool ctdb_db_traverse_recv(struct tevent_req
*req
, int *perr
);
704 * @brief Sync wrapper for a cluster-wide database traverse
706 * @param[in] mem_ctx Talloc memory context
707 * @param[in] ev Tevent context
708 * @param[in] client Client connection context
709 * @param[in] db Database context
710 * @param[in] destnode Node id
711 * @param[in] timeout How long to wait
712 * @param[in] parser Record parser function
713 * @param[in] private_data Private data for parser
714 * @return 0 on success, errno on failure or non-zero status from parser
716 int ctdb_db_traverse(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
717 struct ctdb_client_context
*client
,
718 struct ctdb_db_context
*db
,
719 uint32_t destnode
, struct timeval timeout
,
720 ctdb_rec_parser_func_t parser
, void *private_data
);
723 * @brief Fetch a record from a local database
725 * This function is primarily for internal use.
726 * Clients should use ctdb_fetch_lock() instead.
728 * @param[in] db Database context
729 * @param[in] key Record key
730 * @param[out] header Record header
731 * @param[in] mem_ctx Talloc memory context
732 * @param[out] data Record data
734 int ctdb_ltdb_fetch(struct ctdb_db_context
*db
, TDB_DATA key
,
735 struct ctdb_ltdb_header
*header
,
736 TALLOC_CTX
*mem_ctx
, TDB_DATA
*data
);
739 * @brief Async computation start to fetch a locked record
741 * This function is used to fetch a record from a distributed database.
743 * If the record is already available on the local node, then lock the
744 * record and return the record handle.
746 * If the record is not available on the local node, send a CTDB request to
747 * migrate the record. Once the record is migrated to the local node, lock
748 * the record and return the record handle.
750 * At the end of the computation, a record handle is returned which holds
751 * the record lock. When the record handle is freed, the record is unlocked.
753 * @param[in] mem_ctx Talloc memory context
754 * @param[in] ev Tevent context
755 * @param[in] client Client context
756 * @param[in] db Database context
757 * @param[in] key Record key
758 * @param[in] readonly Whether to request readonly copy of the record
759 * @return a new tevent req on success, NULL on failure
761 struct tevent_req
*ctdb_fetch_lock_send(TALLOC_CTX
*mem_ctx
,
762 struct tevent_context
*ev
,
763 struct ctdb_client_context
*client
,
764 struct ctdb_db_context
*db
,
765 TDB_DATA key
, bool readonly
);
768 * @brief Async computation end to fetch a locked record
770 * @param[in] req Tevent request
771 * @param[out] header Record header
772 * @param[in] mem_ctx Talloc memory context
773 * @param[out] data Record data
774 * @param[out] perr errno in case of failure
775 * @return a new record handle, NULL on failure
777 struct ctdb_record_handle
*ctdb_fetch_lock_recv(struct tevent_req
*req
,
778 struct ctdb_ltdb_header
*header
,
780 TDB_DATA
*data
, int *perr
);
783 * @brief Sync wrapper to fetch a locked record
785 * @see ctdb_fetch_lock_send
787 * @param[in] mem_ctx Talloc memory context
788 * @param[in] ev Tevent context
789 * @param[in] client Client context
790 * @param[in] db Database context
791 * @param[in] key Record key
792 * @param[in] readonly Whether to request readonly copy of the record
793 * @param[out] header Record header
794 * @param[out] data Record data
795 * return 0 on success, errno on failure
797 int ctdb_fetch_lock(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
798 struct ctdb_client_context
*client
,
799 struct ctdb_db_context
*db
, TDB_DATA key
, bool readonly
,
800 struct ctdb_record_handle
**out
,
801 struct ctdb_ltdb_header
*header
, TDB_DATA
*data
);
804 * @brief Update a locked record
806 * This function is used to update a record in a distributed database.
808 * This function should NOT be used to store null data, instead use
809 * ctdb_delete_record().
811 * @param[in] h Record handle
812 * @param[in] data New record data
813 * @return 0 on success, errno on failure
815 int ctdb_store_record(struct ctdb_record_handle
*h
, TDB_DATA data
);
818 * @brief Async computation start to delete a locked record
820 * This function is used to delete a record in a distributed database
822 * @param[in] mem_ctx Talloc memory context
823 * @param[in] ev Tevent context
824 * @param[in] h Record handle
825 * @return a new tevent req on success, NULL on failure
827 struct tevent_req
*ctdb_delete_record_send(TALLOC_CTX
*mem_ctx
,
828 struct tevent_context
*ev
,
829 struct ctdb_record_handle
*h
);
832 * @brief Async computation end to delete a locked record
834 * @param[in] req Tevent request
835 * @param[out] perr errno in case of failure
836 * @return true on success, false on failure
838 bool ctdb_delete_record_recv(struct tevent_req
*req
, int *perr
);
841 * @brief Sync wrapper to delete a locked record
843 * @see ctdb_delete_record_send
845 * @param[in] h Record handle
846 * @return 0 on success, errno on failure
848 int ctdb_delete_record(struct ctdb_record_handle
*h
);
851 * @brief Async computation start to get a global database lock
853 * Functions related to global locks are primarily used internally for
854 * implementing transaction api.
856 * Clients should use transaction api directly.
857 * @see ctdb_transaction_start_send
859 * @param[in] mem_ctx Talloc memory context
860 * @param[in] ev Tevent context
861 * @param[in] client Client context
862 * @param[in] db Database context for g_lock.tdb
863 * @param[in] keyname Record key
864 * @param[in] sid Server id
865 * @param[in] readonly Lock type
866 * @return a new tevent req on success, NULL on failure
868 struct tevent_req
*ctdb_g_lock_lock_send(TALLOC_CTX
*mem_ctx
,
869 struct tevent_context
*ev
,
870 struct ctdb_client_context
*client
,
871 struct ctdb_db_context
*db
,
873 struct ctdb_server_id
*sid
,
877 * @brief Async computation end to get a global database lock
879 * @param[in] req Tevent request
880 * @param[out] perr errno in case of failure
881 * @return true on success, false on failure
883 bool ctdb_g_lock_lock_recv(struct tevent_req
*req
, int *perr
);
886 * @brief Async computation start to release a global database lock
888 * @param[in] mem_ctx Talloc memory context
889 * @param[in] ev Tevent context
890 * @param[in] client Client connection context
891 * @param[in] db Database context
892 * @param[in] keyname Record key
893 * @param[in] sid Server id
894 * @return a new tevent req on success, NULL on failure
896 struct tevent_req
*ctdb_g_lock_unlock_send(TALLOC_CTX
*mem_ctx
,
897 struct tevent_context
*ev
,
898 struct ctdb_client_context
*client
,
899 struct ctdb_db_context
*db
,
901 struct ctdb_server_id sid
);
904 * @brief Async computation end to release a global database lock
906 * @param[in] req Tevent request
907 * @param[out] perr errno in case of failure
908 * @return true on success, false on failure
910 bool ctdb_g_lock_unlock_recv(struct tevent_req
*req
, int *perr
);
913 * @brief Async computation start to start a transaction
915 * This function is used to start a transaction on a replicated database.
917 * To perform any updates on a replicated database
918 * - start transaction
919 * - fetch record (ctdb_transaction_fetch_record)
920 * - store record (ctdb_transaction_store_record)
921 * - delete record (ctdb_transaction_delete_record)
922 * - commit transaction (ctdb_transaction_commit_send), or
923 * - cancel transaction (ctdb_transaction_cancel_send)
925 * Starting a transaction will return a transaction handle. This is used
926 * for updating records under a transaction. This handle is automatically
927 * freed once the transacion is committed or cancelled.
929 * Clients should NOT free the transaction handle.
931 * @param[in] mem_ctx Talloc memory context
932 * @param[in] ev Tevent context
933 * @param[in] client Client connection context
934 * @param[in] timeout How long to wait
935 * @param[in] db Database context
936 * @param[in] readonly Is transaction readonly
937 * @return a new tevent req on success, NULL on failure
939 struct tevent_req
*ctdb_transaction_start_send(TALLOC_CTX
*mem_ctx
,
940 struct tevent_context
*ev
,
941 struct ctdb_client_context
*client
,
942 struct timeval timeout
,
943 struct ctdb_db_context
*db
,
947 * @brief Async computation end to start a transaction
949 * @param[in] req Tevent request
950 * @param[out] perr errno in case of failure
951 * @return a new transaction handle on success, NULL on failure
953 struct ctdb_transaction_handle
*ctdb_transaction_start_recv(
954 struct tevent_req
*req
,
958 * @brief Sync wrapper to start a transaction
960 * @see ctdb_transaction_start_send
962 * @param[in] mem_ctx Talloc memory context
963 * @param[in] ev Tevent context
964 * @param[in] client Client connection context
965 * @param[in] timeout How long to wait
966 * @param[in] db Database context
967 * @param[in] readonly Is transaction readonly
968 * @param[out] result a new transaction handle
969 * @return 0 on success, errno on failure
971 int ctdb_transaction_start(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
972 struct ctdb_client_context
*client
,
973 struct timeval timeout
,
974 struct ctdb_db_context
*db
, bool readonly
,
975 struct ctdb_transaction_handle
**result
);
978 * @brief Fetch a record under a transaction
980 * @see ctdb_transaction_start_send
982 * @param[in] h Transaction handle
983 * @param[in] key Record key
984 * @param[in] mem_ctx Talloc memory context
985 * @param[out] data Record data
986 * @return 0 on success, errno on failure
988 int ctdb_transaction_fetch_record(struct ctdb_transaction_handle
*h
,
990 TALLOC_CTX
*mem_ctx
, TDB_DATA
*data
);
993 * @brief Store a record under a transaction
995 * @see ctdb_transaction_start_send
997 * @param[in] h Transaction handle
998 * @param[in] key Record key
999 * @param[in] data New record data
1000 * @return 0 on success, errno on failure
1002 int ctdb_transaction_store_record(struct ctdb_transaction_handle
*h
,
1003 TDB_DATA key
, TDB_DATA data
);
1006 * @brief Delete a record under a transaction
1008 * @see ctdb_transaction_start_send
1010 * @param[in] h Transaction handle
1011 * @param[in] key Record key
1012 * @return 0 on success, errno on failure
1014 int ctdb_transaction_delete_record(struct ctdb_transaction_handle
*h
,
1018 * @brief Async computation start to commit a transaction
1020 * @see ctdb_transaction_start_send
1022 * @param[in] mem_ctx Talloc memory context
1023 * @param[in] ev Tevent context
1024 * @param[in] timeout How long to wait
1025 * @param[in] h Transaction handle
1026 * @return a new tevent req on success, NULL on failure
1028 struct tevent_req
*ctdb_transaction_commit_send(
1029 TALLOC_CTX
*mem_ctx
,
1030 struct tevent_context
*ev
,
1031 struct timeval timeout
,
1032 struct ctdb_transaction_handle
*h
);
1035 * @brief Async computation end to commit a transaction
1037 * @param[in] req Tevent request
1038 * @param[out] perr errno in case of failure
1039 * @return true on success, false on failure
1041 bool ctdb_transaction_commit_recv(struct tevent_req
*req
, int *perr
);
1044 * @brief Sync wrapper to commit a transaction
1046 * @see ctdb_transaction_commit_send
1048 * @param[in] h Transaction handle
1049 * @return 0 on success, errno on failure
1051 int ctdb_transaction_commit(struct ctdb_transaction_handle
*h
);
1054 * @brief Async computation start to cancel a transaction
1056 * @see ctdb_transaction_start_send
1058 * @param[in] mem_ctx Talloc memory context
1059 * @param[in] ev Tevent context
1060 * @param[in] timeout How long to wait
1061 * @param[in] h Transaction handle
1062 * @return a new tevent req on success, NULL on failure
1064 struct tevent_req
*ctdb_transaction_cancel_send(
1065 TALLOC_CTX
*mem_ctx
,
1066 struct tevent_context
*ev
,
1067 struct timeval timeout
,
1068 struct ctdb_transaction_handle
*h
);
1071 * @brief Async computation end to cancel a transaction
1073 * @param[in] req Tevent request
1074 * @param[out] perr errno in case of failure
1075 * @return true on success, false on failure
1077 bool ctdb_transaction_cancel_recv(struct tevent_req
*req
, int *perr
);
1080 * @brief Sync wrapper to cancel a transaction
1082 * @see ctdb_transaction_cancel_send
1084 * @param[in] h Transaction handle
1085 * @return 0 on success, errno on failure
1087 int ctdb_transaction_cancel(struct ctdb_transaction_handle
*h
);
1090 * @brief Utility function to extract a list of node ids from nodemap
1092 * @param[in] nodemap Node map
1093 * @param[in] flags_mask Flags to match on
1094 * @param[in] exclude_pnn Node id to exclude from the list
1095 * @param[in] mem_ctx Talloc memory context
1096 * @param[out] pnn_list List of node ids
1097 * @return number of node ids on success, -1 on failure
1099 int list_of_nodes(struct ctdb_node_map
*nodemap
,
1100 uint32_t flags_mask
, uint32_t exclude_pnn
,
1101 TALLOC_CTX
*mem_ctx
, uint32_t **pnn_list
);
1104 * @brief Utility function to extract a list of node ids for active nodes
1106 * @param[in] nodemap Node map
1107 * @param[in] exclude_pnn Node id to exclude from the list
1108 * @param[in] mem_ctx Talloc memory context
1109 * @param[out] pnn_list List of node ids
1110 * @return number of node ids on success, -1 on failure
1112 int list_of_active_nodes(struct ctdb_node_map
*nodemap
, uint32_t exclude_pnn
,
1113 TALLOC_CTX
*mem_ctx
, uint32_t **pnn_list
);
1116 * @brief Utility function to extract a list of node ids for connected nodes
1118 * @param[in] nodemap Node map
1119 * @param[in] exclude_pnn Node id to exclude from the list
1120 * @param[in] mem_ctx Talloc memory context
1121 * @param[out] pnn_list List of node ids
1122 * @return number of node ids on success, -1 on failure
1124 int list_of_connected_nodes(struct ctdb_node_map
*nodemap
,
1125 uint32_t exclude_pnn
,
1126 TALLOC_CTX
*mem_ctx
, uint32_t **pnn_list
);
1129 * @brief Construct a new server id
1131 * @param[in] client Client connection context
1132 * @param[in] task_id Task id
1133 * @return a new server id
1135 struct ctdb_server_id
ctdb_client_get_server_id(
1136 struct ctdb_client_context
*client
,
1140 * @brief Check if two server ids are the same
1142 * @param[in] sid1 Server id 1
1143 * @param[in] sid2 Server id 2
1144 * @return true if the server ids are same, false otherwise
1146 bool ctdb_server_id_equal(struct ctdb_server_id
*sid1
,
1147 struct ctdb_server_id
*sid2
);
1150 * @brief Check if the process with server id exists
1152 * @param[in] mem_ctx Talloc memory context
1153 * @param[in] ev Tevent context
1154 * @param[in] client Client connection context
1155 * @param[in] sid Server id
1156 * @param[out] exists Boolean flag to indicate if the process exists
1157 * @return 0 on success, errno on failure
1159 int ctdb_server_id_exists(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
1160 struct ctdb_client_context
*client
,
1161 struct ctdb_server_id
*sid
, bool *exists
);
1163 #endif /* __CTDB_CLIENT_H__ */