Skip to content

Commit ec8b5a3

Browse files
Issue (#1): Initial Commit.
1 parent cf4ad1b commit ec8b5a3

19 files changed

Lines changed: 1955 additions & 3242 deletions

ramd/include/ramd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <time.h>
2727
#include <stdbool.h>
2828
#include <stdint.h>
29+
#include <libpq-fe.h>
2930

3031
/* Version information */
3132
#define RAMD_VERSION_MAJOR 1
@@ -86,6 +87,7 @@ extern void ramd_cleanup(void);
8687
extern void ramd_run(void);
8788
extern void ramd_stop(void);
8889
extern bool ramd_is_running(void);
90+
extern PGconn* ramd_get_postgres_connection(void);
8991

9092
/* Signal handling */
9193
extern void ramd_setup_signals(void);

ramd/include/ramd_basebackup.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* ramd_basebackup.h
4+
* PostgreSQL RAM Daemon - Base backup function declarations
5+
*
6+
* Copyright (c) 2024-2025, pgElephant, Inc.
7+
*
8+
* This file provides function declarations for base backup functionality
9+
* in the ramd daemon. It provides functions to take and manage base
10+
* backups using pg_basebackup.
11+
*
12+
*-------------------------------------------------------------------------
13+
*/
14+
15+
#ifndef RAMD_BASEBACKUP_H
16+
#define RAMD_BASEBACKUP_H
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
#include <libpq-fe.h>
23+
24+
/*
25+
* Take a base backup using pg_basebackup
26+
*
27+
* Parameters:
28+
* conn - PostgreSQL connection
29+
* target_dir - Directory where backup should be stored
30+
* label - Label for the backup
31+
*
32+
* Returns: 0 on success, -1 on failure
33+
*/
34+
extern int ramd_take_basebackup(PGconn *conn, const char *target_dir, const char *label);
35+
36+
#ifdef __cplusplus
37+
}
38+
#endif
39+
40+
#endif /* RAMD_BASEBACKUP_H */

ramd/include/ramd_cluster.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "ramd.h"
1515
#include "ramd_config.h"
16+
#include <libpq-fe.h>
1617

1718
/* Node information structure */
1819
typedef struct ramd_node_t
@@ -49,6 +50,8 @@ typedef struct ramd_cluster_t
4950
bool in_failover;
5051
time_t last_topology_change;
5152
time_t last_health_check;
53+
/* PostgreSQL connection for pgraft integration */
54+
PGconn* pg_conn;
5255
} ramd_cluster_t;
5356

5457
/* Cluster management functions */

ramd/include/ramd_conn.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* ramd_conn.h
4+
* PostgreSQL Auto-Failover Daemon - PostgreSQL Connection API
5+
*
6+
* Copyright (c) 2024-2025, pgElephant, Inc.
7+
*
8+
*-------------------------------------------------------------------------
9+
*/
10+
11+
#ifndef RAMD_CONN_H
12+
#define RAMD_CONN_H
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
#include <libpq-fe.h>
19+
#include "ramd.h"
20+
21+
/* Initialize connection subsystem */
22+
extern bool ramd_conn_init(void);
23+
24+
/* Cleanup connection subsystem */
25+
extern void ramd_conn_cleanup(void);
26+
27+
/* Get a connection to PostgreSQL */
28+
extern PGconn* ramd_conn_get(const char* host, int32_t port, const char* dbname,
29+
const char* user, const char* password);
30+
31+
/* Get a cached connection to PostgreSQL */
32+
extern PGconn* ramd_conn_get_cached(int32_t node_id, const char* host, int32_t port,
33+
const char* dbname, const char* user, const char* password);
34+
35+
/* Execute a query and return result */
36+
extern PGresult* ramd_conn_exec(PGconn* conn, const char* query);
37+
38+
/* Close a connection */
39+
extern void ramd_conn_close(PGconn* conn);
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif
44+
45+
#endif /* RAMD_CONN_H */

ramd/include/ramd_pgraft.h

Lines changed: 166 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*
66
* Copyright (c) 2024-2025, pgElephant, Inc.
77
*
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+
*
812
*-------------------------------------------------------------------------
913
*/
1014

@@ -15,8 +19,168 @@
1519
extern "C" {
1620
#endif
1721

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);
20184

21185
#ifdef __cplusplus
22186
}

0 commit comments

Comments
 (0)