|
5 | 5 | #include "storage/shmem.h" |
6 | 6 | #include "storage/spin.h" |
7 | 7 |
|
| 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 | + |
8 | 75 | /* Core consensus types */ |
9 | 76 | typedef struct pgraft_node |
10 | 77 | { |
@@ -48,4 +115,23 @@ void pgraft_core_cleanup(void); |
48 | 115 | void pgraft_core_init_shared_memory(void); |
49 | 116 | pgraft_cluster_t *pgraft_core_get_shared_memory(void); |
50 | 117 |
|
| 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 | + |
51 | 137 | #endif |
0 commit comments