Skip to content

Commit 66cb813

Browse files
committed
Teach the Raft protocol to DTMD.
1 parent a6892aa commit 66cb813

File tree

9 files changed

+311
-125
lines changed

9 files changed

+311
-125
lines changed

contrib/pg_dtm/dtmd/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ all: bin/dtmd bin/heart
1515
@echo Done.
1616
@echo Feel free to run the tests with \'make check\'.
1717

18-
bin/dtmd: obj/server.o obj/main.o obj/clog.o obj/clogfile.o obj/util.o obj/transaction.o obj/snapshot.o | bindir objdir
18+
bin/dtmd: obj/server.o obj/raft.o obj/main.o obj/clog.o obj/clogfile.o obj/util.o obj/transaction.o obj/snapshot.o | bindir objdir
1919
$(CC) -o bin/dtmd $(CFLAGS) \
20-
obj/server.o obj/main.o \
20+
obj/server.o obj/raft.o obj/main.o \
2121
obj/clog.o obj/clogfile.o obj/util.o obj/transaction.o \
2222
obj/snapshot.o \
2323
$(SOCKHUB_LDFLAGS)

contrib/pg_dtm/dtmd/include/proto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#define RES_FAILED 0xDEADBEEF
1212
#define RES_OK 0xC0FFEE
13+
#define RES_REDIRECT 404
1314
#define RES_TRANSACTION_COMMITTED 1
1415
#define RES_TRANSACTION_ABORTED 2
1516
#define RES_TRANSACTION_INPROGRESS 3

contrib/pg_dtm/dtmd/include/server.h

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,37 @@ server_t server_init(
4040
ondisconnect_callback_t ondisconnect
4141
);
4242

43+
/*
44+
* Assigns the given raft socket to the server. The server will add the socket
45+
* to the 'select' calls and give you the incoming messages.
46+
*/
47+
void server_set_raft_socket(server_t server, int sock);
48+
4349
/*
4450
* Starts the server. Returns 'true' on success, 'false' otherwise.
4551
*/
4652
bool server_start(server_t server);
4753

4854
/*
49-
* The main server loop. Does not return, so use the callbacks and signal
50-
* handlers to add more logic.
55+
* The main server loop. Returns true if there is a raft message ready, or NULL
56+
* if timed out. Use the callbacks and signal handlers to add more logic.
57+
*/
58+
bool server_tick(server_t server, int timeout_ms);
59+
60+
/*
61+
* Closes all client connections on the server and refuses to accept new ones.
62+
*/
63+
void server_disable(server_t server);
64+
65+
/*
66+
* Allows the server to accept new connections.
5167
*/
52-
void server_loop(server_t server);
68+
void server_enable(server_t server);
69+
70+
/*
71+
* Enables or disables the server depending on the argument.
72+
*/
73+
void server_set_enabled(server_t server, bool enable);
5374

5475
/*
5576
* These two methods allow you to set and get your custom 'userdata' for the
@@ -98,4 +119,11 @@ bool client_message_finish(client_t client);
98119
*/
99120
bool client_message_shortcut(client_t client, xid_t arg);
100121

122+
/*
123+
* A shortcut to send the 'redirect' message.
124+
*
125+
* Returns 'true' on success, 'false' otherwise.
126+
*/
127+
bool client_redirect(client_t client, unsigned addr, int port);
128+
101129
#endif

contrib/pg_dtm/dtmd/include/util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stdbool.h>
99
#include <sys/stat.h>
10+
#include <sys/time.h>
1011
#include <fcntl.h>
1112
#include <stdio.h>
1213
#include <stdlib.h>
@@ -29,6 +30,17 @@ static inline int rand_between(int min, int max) {
2930
return rand() % (max - min + 1) + min;
3031
}
3132

33+
// ------ timing ------
34+
35+
typedef struct mstimer_t {
36+
struct timeval tv;
37+
} mstimer_t;
38+
39+
int mstimer_reset(mstimer_t *t);
40+
struct timeval ms2tv(int ms);
41+
42+
// ------ logging ------
43+
3244
#ifndef DEBUG
3345
#define debug(...)
3446
#else

contrib/pg_dtm/dtmd/src/heart.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,6 @@ static void show_status() {
4646
shout("\n");
4747
}
4848

49-
typedef struct mstimer_t {
50-
struct timeval tv;
51-
} mstimer_t;
52-
53-
static int mstimer_reset(mstimer_t *t) {
54-
struct timeval newtime;
55-
gettimeofday(&newtime, NULL);
56-
57-
int ms =
58-
(newtime.tv_sec - t->tv.tv_sec) * 1000 +
59-
(newtime.tv_usec - t->tv.tv_usec) / 1000;
60-
61-
t->tv = newtime;
62-
63-
return ms;
64-
}
65-
6649
static void main_loop() {
6750
mstimer_t t;
6851
mstimer_reset(&t);

0 commit comments

Comments
 (0)