Skip to content

Commit

Permalink
Import raft sources; get dqlite tests passing
Browse files Browse the repository at this point in the history
Signed-off-by: Cole Miller <cole.miller@canonical.com>
  • Loading branch information
cole-miller committed Feb 6, 2024
1 parent 94938c8 commit 2961582
Show file tree
Hide file tree
Showing 111 changed files with 21,269 additions and 39 deletions.
63 changes: 61 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
ACLOCAL_AMFLAGS = -I m4
AM_CFLAGS += $(CODE_COVERAGE_CFLAGS)
AM_CFLAGS += $(SQLITE_CFLAGS) $(UV_CFLAGS) $(RAFT_CFLAGS) $(PTHREAD_CFLAGS)
AM_LDFLAGS = $(UV_LIBS) $(RAFT_LIBS) $(PTHREAD_LIBS)
AM_CFLAGS += $(SQLITE_CFLAGS) $(UV_CFLAGS) $(PTHREAD_CFLAGS)
AM_LDFLAGS = $(UV_LIBS) $(PTHREAD_LIBS)

if !BUILD_SQLITE_ENABLED
AM_LDFLAGS += $(SQLITE_LIBS)
endif

if !BUILD_RAFT_ENABLED
AM_CFLAGS += $(RAFT_CFLAGS)
AM_LDFLAGS += $(RAFT_LIBS)
endif

include_HEADERS = include/dqlite.h

lib_LTLIBRARIES = libdqlite.la
Expand Down Expand Up @@ -50,6 +55,60 @@ if BUILD_SQLITE_ENABLED
libdqlite_la_SOURCES += sqlite3.c
endif

if BUILD_RAFT_ENABLED
libdqlite_la_SOURCES += \
src/raft/byte.c \
src/raft/callbacks.c \
src/raft/client.c \
src/raft/compress.c \
src/raft/configuration.c \
src/raft/convert.c \
src/raft/election.c \
src/raft/entry.c \
src/raft/err.c \
src/raft/fixture.c \
src/raft/flags.c \
src/raft/heap.c \
src/raft/lifecycle.c \
src/raft/log.c \
src/raft/membership.c \
src/raft/progress.c \
src/raft/raft.c \
src/raft/recv.c \
src/raft/recv_append_entries.c \
src/raft/recv_append_entries_result.c \
src/raft/recv_request_vote.c \
src/raft/recv_request_vote_result.c \
src/raft/recv_install_snapshot.c \
src/raft/recv_timeout_now.c \
src/raft/replication.c \
src/raft/snapshot.c \
src/raft/start.c \
src/raft/state.c \
src/raft/syscall.c \
src/raft/tick.c \
src/raft/uv.c \
src/raft/uv_append.c \
src/raft/uv_encoding.c \
src/raft/uv_finalize.c \
src/raft/uv_fs.c \
src/raft/uv_ip.c \
src/raft/uv_list.c \
src/raft/uv_metadata.c \
src/raft/uv_os.c \
src/raft/uv_prepare.c \
src/raft/uv_recv.c \
src/raft/uv_segment.c \
src/raft/uv_send.c \
src/raft/uv_snapshot.c \
src/raft/uv_tcp.c \
src/raft/uv_tcp_listen.c \
src/raft/uv_tcp_connect.c \
src/raft/uv_truncate.c \
src/raft/uv_work.c \
src/raft/uv_writer.c
endif

check_PROGRAMS = \
unit-test \
integration-test
Expand Down
18 changes: 16 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,24 @@ AM_COND_IF(SANITIZE_ENABLED,

AC_ARG_ENABLE(backtrace, AS_HELP_STRING([--enable-backtrace[=ARG]], [print backtrace on assertion failure [default=no]]))
AM_CONDITIONAL(BACKTRACE_ENABLED, test "x$enable_backtrace" = "xyes")

AC_ARG_ENABLE(build-sqlite, AS_HELP_STRING([--enable-build-sqlite[=ARG]], [build libsqlite3 from sqlite3.c in the build root [default=no]]))
AM_CONDITIONAL(BUILD_SQLITE_ENABLED, test "x$enable_build_sqlite" = "xyes")

AC_ARG_ENABLE(build-raft, AS_HELP_STRING([--enable-build-raft[=ARG]], [use the bundled raft sources instead of linking to libraft [default=no]]))
AM_CONDITIONAL(BUILD_RAFT_ENABLED, test "x$enable_build_raft" = "xyes")

# Allow not linking to liblz4 even if it's present.
AC_ARG_WITH([lz4], AS_HELP_STRING([--without-lz4], [never link to liblz4]))

# Whether to enable code coverage.
AX_CODE_COVERAGE

# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h fcntl.h stdint.h stdlib.h string.h sys/socket.h unistd.h])
AC_CHECK_HEADERS([linux/io_uring.h linux/aio_abi.h])

# Checks for library functions and definitions.
AC_CHECK_DECLS(RWF_NOWAIT, [], [AC_MSG_ERROR(Linux kernel >= 4.14 required.)], [#include <linux/aio_abi.h>])

# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T
Expand All @@ -54,7 +64,11 @@ AC_SYS_LARGEFILE
# Checks for libraries
PKG_CHECK_MODULES(SQLITE, [sqlite3 >= 3.22.0], [], [])
PKG_CHECK_MODULES(UV, [libuv >= 1.8.0], [], [])
PKG_CHECK_MODULES(RAFT, [raft >= 0.17.1], [], [])
AS_IF([test "x$enable_build_raft" != "xyes"], [PKG_CHECK_MODULES(RAFT, [raft >= 0.17.1], [], [])], [])

AS_IF([test "x$with_lz4" != "xno"], [PKG_CHECK_MODULES(LZ4, [liblz4 >= 1.7.1], [have_lz4=yes], [have_lz4=no])], [have_lz4=no])
AS_IF([test "x$with_lz4" != "xno" -a "x$have_lz4" = "xno"], [AC_MSG_ERROR([liblz4 required but not found])], [])
AM_CONDITIONAL(LZ4_AVAILABLE, test "x$have_lz4" = "xyes")

CC_CHECK_FLAGS_APPEND([AM_CFLAGS],[CFLAGS],[ \
-std=c11 \
Expand Down
3 changes: 1 addition & 2 deletions src/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
#ifndef COMMAND_H_
#define COMMAND_H_

#include <raft.h>

#include "../include/dqlite.h"

#include "lib/serialize.h"
#include "raft.h"

/* Command type codes */
enum { COMMAND_OPEN = 1, COMMAND_FRAMES, COMMAND_UNDO, COMMAND_CHECKPOINT };
Expand Down
3 changes: 1 addition & 2 deletions src/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
#ifndef DQLITE_CONN_H_
#define DQLITE_CONN_H_

#include <raft/uv.h>

#include "lib/buffer.h"
#include "lib/queue.h"
#include "lib/transport.h"

#include "gateway.h"
#include "id.h"
#include "message.h"
#include "raft.h"

/**
* Callbacks.
Expand Down
3 changes: 1 addition & 2 deletions src/fsm.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <raft.h>

#include "lib/assert.h"
#include "lib/serialize.h"

#include "command.h"
#include "fsm.h"
#include "tracing.h"
#include "raft.h"
#include "vfs.h"

#include <sys/mman.h>
Expand Down
3 changes: 1 addition & 2 deletions src/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#ifndef DQLITE_FSM_H_
#define DQLITE_FSM_H_

#include <raft.h>

#include "config.h"
#include "raft.h"
#include "registry.h"

/**
Expand Down
2 changes: 1 addition & 1 deletion src/gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#ifndef DQLITE_GATEWAY_H_
#define DQLITE_GATEWAY_H_

#include <raft.h>

#include "../include/dqlite.h"

Expand All @@ -15,6 +14,7 @@
#include "config.h"
#include "id.h"
#include "leader.h"
#include "raft.h"
#include "registry.h"
#include "stmt.h"

Expand Down
2 changes: 1 addition & 1 deletion src/leader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#ifndef LEADER_H_
#define LEADER_H_

#include <raft.h>
#include <sqlite3.h>
#include <stdbool.h>

#include "./lib/queue.h"
#include "db.h"
#include "raft.h"

#define SQLITE_IOERR_NOT_LEADER (SQLITE_IOERR | (40 << 8))
#define SQLITE_IOERR_LEADERSHIP_LOST (SQLITE_IOERR | (41 << 8))
Expand Down
2 changes: 1 addition & 1 deletion src/lib/transport.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <raft.h>
#include "../raft.h"

#include "../../include/dqlite.h"

Expand Down
2 changes: 1 addition & 1 deletion src/logger.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef LOGGER_H_
#define LOGGER_H_

#include <raft.h>
#include "raft.h"

#include "../include/dqlite.h"

Expand Down
25 changes: 25 additions & 0 deletions src/raft/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Macros to manipulate contiguous arrays. */

#ifndef ARRAY_H_
#define ARRAY_H_

#include "../raft.h"

/* Append item I of type T to array A which currently has N items.
*
* A and N must both by pointers. Set RV to -1 in case of failure. */
#define ARRAY__APPEND(T, I, A, N, RV) \
{ \
T *tmp_array; \
tmp_array = raft_realloc(*A, (*N + 1) * sizeof **A); \
if (tmp_array != NULL) { \
(*N)++; \
*A = tmp_array; \
(*A)[(*N) - 1] = I; \
RV = 0; \
} else { \
RV = -1; \
} \
}

#endif /* ARRAY_H_ */
40 changes: 40 additions & 0 deletions src/raft/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Define the assert() macro, either as the standard one or the test one. */

#ifndef ASSERT_H_
#define ASSERT_H_

#if defined(RAFT_TEST)
extern void munit_errorf_ex(const char *filename,
int line,
const char *format,
...);
#define assert(expr) \
do { \
if (!expr) { \
munit_errorf_ex(__FILE__, __LINE__, "assertion failed: ", #expr); \
} \
} while (0)
#elif defined(NDEBUG)
#define assert(x) \
do { \
(void)sizeof(x); \
} while (0)
#elif defined(RAFT_ASSERT_WITH_BACKTRACE)
#include <assert.h> /* for __assert_fail */
#include <backtrace.h>
#include <stdio.h>
#undef assert
#define assert(x) \
do { \
struct backtrace_state *state_; \
if (!(x)) { \
state_ = backtrace_create_state(NULL, 0, NULL, NULL); \
backtrace_print(state_, 0, stderr); \
__assert_fail(#x, __FILE__, __LINE__, __func__); \
} \
} while (0)
#else
#include <assert.h>
#endif

#endif /* ASSERT_H_ */
Loading

0 comments on commit 2961582

Please sign in to comment.