|
5 | 5 | * |
6 | 6 | * Copyright (c) 2024-2025, pgElephant, Inc. |
7 | 7 | * |
| 8 | + * This file provides C function declarations for calling pgraft extension |
| 9 | + * functions from the ramd daemon. These functions handle the interface |
| 10 | + * between ramd and the pgraft PostgreSQL extension. |
| 11 | + * |
8 | 12 | *------------------------------------------------------------------------- |
9 | 13 | */ |
10 | 14 |
|
|
15 | 19 | extern "C" { |
16 | 20 | #endif |
17 | 21 |
|
18 | | -/* Function declarations for pgraft functions used by ramd */ |
19 | | -extern int pgraft_get_healthy_nodes_count(void); |
| 22 | +#include "libpq-fe.h" |
| 23 | + |
| 24 | +/* Return codes for pgraft functions */ |
| 25 | +#define RAMD_PGRAFT_SUCCESS 0 |
| 26 | +#define RAMD_PGRAFT_ERROR -1 |
| 27 | +#define RAMD_PGRAFT_NOT_INITIALIZED -2 |
| 28 | +#define RAMD_PGRAFT_NOT_LEADER -3 |
| 29 | + |
| 30 | +/* Core pgraft functions used by ramd */ |
| 31 | + |
| 32 | +/* |
| 33 | + * Initialize the pgraft system |
| 34 | + * Returns: RAMD_PGRAFT_SUCCESS on success, error code on failure |
| 35 | + */ |
| 36 | +extern int ramd_pgraft_init(PGconn* conn, int node_id, const char* hostname, int port); |
| 37 | + |
| 38 | +/* |
| 39 | + * Start the pgraft system |
| 40 | + * Returns: RAMD_PGRAFT_SUCCESS on success, error code on failure |
| 41 | + */ |
| 42 | +extern int ramd_pgraft_start(PGconn* conn); |
| 43 | + |
| 44 | +/* |
| 45 | + * Stop the pgraft system |
| 46 | + * Returns: RAMD_PGRAFT_SUCCESS on success, error code on failure |
| 47 | + */ |
| 48 | +extern int ramd_pgraft_stop(PGconn* conn); |
| 49 | + |
| 50 | +/* |
| 51 | + * Get the current state of the pgraft system |
| 52 | + * Returns: state string ("stopped", "running", "error") |
| 53 | + */ |
| 54 | +extern char* ramd_pgraft_get_state(PGconn* conn); |
| 55 | + |
| 56 | +/* |
| 57 | + * Check if the current node is the Raft leader |
| 58 | + * Returns: 1 if leader, 0 if not leader, -1 on error |
| 59 | + */ |
| 60 | +extern int ramd_pgraft_is_leader(PGconn* conn); |
| 61 | + |
| 62 | +/* |
| 63 | + * Get the current Raft leader node ID |
| 64 | + * Returns: leader node ID, or -1 on error |
| 65 | + */ |
| 66 | +extern int ramd_pgraft_get_leader(PGconn* conn); |
| 67 | + |
| 68 | +/* |
| 69 | + * Get the current Raft term |
| 70 | + * Returns: current term, or -1 on error |
| 71 | + */ |
| 72 | +extern long long ramd_pgraft_get_term(PGconn* conn); |
| 73 | + |
| 74 | +/* |
| 75 | + * Get the list of nodes in the Raft cluster |
| 76 | + * Returns: JSON string with node information, or NULL on error |
| 77 | + * Caller must free the returned string |
| 78 | + */ |
| 79 | +extern char* ramd_pgraft_get_nodes(PGconn* conn); |
| 80 | + |
| 81 | +/* Cluster management functions */ |
| 82 | + |
| 83 | +/* |
| 84 | + * Add a node to the Raft cluster |
| 85 | + * Returns: RAMD_PGRAFT_SUCCESS on success, error code on failure |
| 86 | + */ |
| 87 | +extern int ramd_pgraft_add_node(PGconn* conn, int node_id, const char* hostname, int port); |
| 88 | + |
| 89 | +/* |
| 90 | + * Remove a node from the Raft cluster |
| 91 | + * Returns: RAMD_PGRAFT_SUCCESS on success, error code on failure |
| 92 | + */ |
| 93 | +extern int ramd_pgraft_remove_node(PGconn* conn, int node_id); |
| 94 | + |
| 95 | +/* |
| 96 | + * Get cluster health information |
| 97 | + * Returns: JSON string with health information, or NULL on error |
| 98 | + * Caller must free the returned string |
| 99 | + */ |
| 100 | +extern char* ramd_pgraft_get_cluster_health(PGconn* conn); |
| 101 | + |
| 102 | +/* |
| 103 | + * Check if the cluster is healthy |
| 104 | + * Returns: 1 if healthy, 0 if not healthy, -1 on error |
| 105 | + */ |
| 106 | +extern int ramd_pgraft_is_cluster_healthy(PGconn* conn); |
| 107 | + |
| 108 | +/* |
| 109 | + * Get cluster performance metrics |
| 110 | + * Returns: JSON string with metrics, or NULL on error |
| 111 | + * Caller must free the returned string |
| 112 | + */ |
| 113 | +extern char* ramd_pgraft_get_performance_metrics(PGconn* conn); |
| 114 | + |
| 115 | +/* Log and consensus functions */ |
| 116 | + |
| 117 | +/* |
| 118 | + * Append a log entry to the Raft log |
| 119 | + * Returns: RAMD_PGRAFT_SUCCESS on success, error code on failure |
| 120 | + */ |
| 121 | +extern int ramd_pgraft_append_log(PGconn* conn, const char* log_data); |
| 122 | + |
| 123 | +/* |
| 124 | + * Get Raft log entries |
| 125 | + * Returns: JSON string with log entries, or NULL on error |
| 126 | + * Caller must free the returned string |
| 127 | + */ |
| 128 | +extern char* ramd_pgraft_get_log(PGconn* conn); |
| 129 | + |
| 130 | +/* |
| 131 | + * Get Raft statistics |
| 132 | + * Returns: JSON string with statistics, or NULL on error |
| 133 | + * Caller must free the returned string |
| 134 | + */ |
| 135 | +extern char* ramd_pgraft_get_stats(PGconn* conn); |
| 136 | + |
| 137 | +/* Utility functions */ |
| 138 | + |
| 139 | +/* |
| 140 | + * Get the pgraft extension version |
| 141 | + * Returns: version string, or NULL on error |
| 142 | + * Caller must free the returned string |
| 143 | + */ |
| 144 | +extern char* ramd_pgraft_get_version(PGconn* conn); |
| 145 | + |
| 146 | +/* |
| 147 | + * Test the pgraft system (for debugging) |
| 148 | + * Returns: test result integer, or -1 on error |
| 149 | + */ |
| 150 | +extern int ramd_pgraft_test(PGconn* conn); |
| 151 | + |
| 152 | +/* |
| 153 | + * Get the last error message from pgraft operations |
| 154 | + * Returns: error message string, or NULL if no error |
| 155 | + * This is a static string, do not free it |
| 156 | + */ |
| 157 | +extern const char* ramd_pgraft_get_last_error(void); |
| 158 | + |
| 159 | +/* |
| 160 | + * Check if pgraft extension is available and properly configured |
| 161 | + * Returns: 1 if available, 0 if not available, -1 on error |
| 162 | + */ |
| 163 | +extern int ramd_pgraft_check_availability(PGconn* conn); |
| 164 | + |
| 165 | +/* Replica setup functions */ |
| 166 | + |
| 167 | +/* |
| 168 | + * Set up a new replica node using base backup and add to Raft cluster |
| 169 | + * This function takes a base backup from the current node and sets up |
| 170 | + * a new replica node, then adds it to the Raft cluster |
| 171 | + * |
| 172 | + * Parameters: |
| 173 | + * conn - PostgreSQL connection to current node |
| 174 | + * replica_node_id - ID of the new replica node |
| 175 | + * replica_hostname - Hostname of the new replica node |
| 176 | + * replica_port - PostgreSQL port of the new replica node |
| 177 | + * backup_dir - Directory to store the base backup |
| 178 | + * |
| 179 | + * Returns: RAMD_PGRAFT_SUCCESS on success, error code on failure |
| 180 | + */ |
| 181 | +extern int ramd_pgraft_setup_replica(PGconn* conn, int replica_node_id, |
| 182 | + const char* replica_hostname, int replica_port, |
| 183 | + const char* backup_dir); |
20 | 184 |
|
21 | 185 | #ifdef __cplusplus |
22 | 186 | } |
|
0 commit comments