Skip to content

Commit c3eeb1e

Browse files
Issue (#2): Refector Code.
1 parent 268f7e3 commit c3eeb1e

7 files changed

Lines changed: 89 additions & 125 deletions

File tree

pgraft/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# PostgreSQL extension using etcd-io/raft for distributed consensus
33

44
MODULE_big = pgraft
5-
OBJS = src/pgraft.o src/pgraft_core.o src/pgraft_go.o src/pgraft_state.o src/pgraft_log.o src/pgraft_sql.o src/pgraft_guc.o src/pgraft_worker.o src/pgraft_util.o
5+
OBJS = src/pgraft.o src/pgraft_core.o src/pgraft_go.o src/pgraft_state.o src/pgraft_log.o src/pgraft_sql.o src/pgraft_guc.o src/pgraft_util.o
66

77
EXTENSION = pgraft
88
DATA = pgraft--1.0.sql

pgraft/include/pgraft_core.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,73 @@
55
#include "storage/shmem.h"
66
#include "storage/spin.h"
77

8+
/* Worker status enum */
9+
typedef enum {
10+
WORKER_STATUS_IDLE = 0,
11+
WORKER_STATUS_INITIALIZING = 1,
12+
WORKER_STATUS_RUNNING = 2,
13+
WORKER_STATUS_STOPPING = 3,
14+
WORKER_STATUS_STOPPED = 4
15+
} WORKER_STATUS;
16+
17+
/* Command types for worker queue */
18+
typedef enum {
19+
COMMAND_INIT = 1,
20+
COMMAND_ADD_NODE = 2,
21+
COMMAND_REMOVE_NODE = 3,
22+
COMMAND_LOG_APPEND = 4,
23+
COMMAND_LOG_COMMIT = 5,
24+
COMMAND_LOG_APPLY = 6,
25+
COMMAND_SHUTDOWN = 7
26+
} COMMAND_TYPE;
27+
28+
/* Command status enum */
29+
typedef enum {
30+
COMMAND_STATUS_PENDING = 0,
31+
COMMAND_STATUS_PROCESSING = 1,
32+
COMMAND_STATUS_COMPLETED = 2,
33+
COMMAND_STATUS_FAILED = 3
34+
} COMMAND_STATUS;
35+
36+
/* Command structure with status tracking */
37+
typedef struct {
38+
COMMAND_TYPE type;
39+
int node_id;
40+
char address[256];
41+
int port;
42+
char cluster_id[256];
43+
/* Log operation fields */
44+
char log_data[1024]; /* For log append/commit data */
45+
int log_index; /* For log operations */
46+
/* Status tracking */
47+
COMMAND_STATUS status;
48+
char error_message[512]; /* Error message if failed */
49+
int64_t timestamp; /* Command timestamp */
50+
} pgraft_command_t;
51+
52+
/* Command queue configuration */
53+
#define MAX_COMMANDS 100
54+
55+
/* Background worker state structure */
56+
typedef struct {
57+
int node_id;
58+
char address[256];
59+
int port;
60+
WORKER_STATUS status;
61+
62+
/* Fixed-size circular buffer for commands */
63+
pgraft_command_t commands[MAX_COMMANDS];
64+
int command_head; /* Index of next command to process */
65+
int command_tail; /* Index of next slot to write */
66+
int command_count; /* Number of commands in queue */
67+
68+
/* Fixed-size circular buffer for command status */
69+
pgraft_command_t status_commands[MAX_COMMANDS];
70+
int status_head; /* Index of next status to read */
71+
int status_tail; /* Index of next slot to write */
72+
int status_count; /* Number of status entries */
73+
} pgraft_worker_state_t;
74+
875
/* Core consensus types */
976
typedef struct pgraft_node
1077
{
@@ -48,4 +115,23 @@ void pgraft_core_cleanup(void);
48115
void pgraft_core_init_shared_memory(void);
49116
pgraft_cluster_t *pgraft_core_get_shared_memory(void);
50117

118+
/* Background worker functions */
119+
void pgraft_worker_main(Datum main_arg);
120+
void pgraft_register_worker(void);
121+
122+
/* Worker control functions */
123+
pgraft_worker_state_t *pgraft_worker_get_state(void);
124+
125+
/* Command queue functions */
126+
bool pgraft_queue_command(COMMAND_TYPE type, int node_id, const char *address, int port, const char *cluster_id);
127+
bool pgraft_queue_log_command(COMMAND_TYPE type, const char *log_data, int log_index);
128+
bool pgraft_dequeue_command(pgraft_command_t *cmd);
129+
bool pgraft_queue_is_empty(void);
130+
131+
/* Command status functions */
132+
bool pgraft_add_command_to_status(pgraft_command_t *cmd);
133+
bool pgraft_get_command_status(int64_t timestamp, pgraft_command_t *status_cmd);
134+
bool pgraft_update_command_status(int64_t timestamp, COMMAND_STATUS status, const char *error_message);
135+
bool pgraft_remove_completed_commands(void);
136+
51137
#endif

pgraft/include/pgraft_worker.h

Lines changed: 0 additions & 100 deletions
This file was deleted.

pgraft/src/pgraft.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "../include/pgraft_state.h"
2323
#include "../include/pgraft_log.h"
2424
#include "../include/pgraft_guc.h"
25-
#include "../include/pgraft_worker.h"
25+
/* Worker types and functions now in pgraft_core.h */
2626

2727
/* Temporary function declarations to fix compilation */
2828
bool pgraft_go_is_loaded(void);

pgraft/src/pgraft_sql.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "../include/pgraft_state.h"
2626
#include "../include/pgraft_log.h"
2727
#include "../include/pgraft_guc.h"
28-
#include "../include/pgraft_worker.h"
2928

3029
/* Function info macros for core functions */
3130
PG_FUNCTION_INFO_V1(pgraft_init);

pgraft/src/pgraft_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "utils/palloc.h"
99
#include "lib/ilist.h"
1010
#include "nodes/pg_list.h"
11-
#include "../include/pgraft_worker.h"
11+
#include "../include/pgraft_core.h"
1212

1313
#include <time.h>
1414

pgraft/src/pgraft_worker.c

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)