Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Cole Miller <[email protected]>
  • Loading branch information
cole-miller committed Oct 15, 2024
1 parent 0137ab5 commit 48ccd7a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
42 changes: 34 additions & 8 deletions src/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ void gateway__close(struct gateway *g)
/* Encode fa failure response and invoke the request callback */
static void failure(struct handle *req, int code, const char *message)
{
tracef("failure %s", message);
struct response_failure failure;
size_t n;
char *cursor;
Expand Down Expand Up @@ -227,10 +228,14 @@ static struct leader *gw_get_readwrite(const struct gateway *g,
}
opened_readonly = l->flags & DQLITE_OPEN_ALLOW_STALE;
if (raft_state(g->raft) != RAFT_LEADER) {
failure(req, SQLITE_IOERR_NOT_LEADER, "not leader");
if (req != NULL) {
failure(req, SQLITE_IOERR_NOT_LEADER, "not leader");
}
return NULL;
} else if (opened_readonly) {
failure(req, SQLITE_READONLY, "dqlite connection is readonly");
if (req != NULL) {
failure(req, SQLITE_READONLY, "dqlite connection is readonly");
}
return NULL;
}
return l;
Expand Down Expand Up @@ -418,6 +423,7 @@ static int handle_prepare(struct gateway *g, struct handle *req)
struct stmt *stmt;
struct request_prepare request = { 0 };
struct leader *l;
bool conn_readonly;
int rc;

if (req->schema != DQLITE_PREPARE_STMT_SCHEMA_V0 &&
Expand All @@ -430,9 +436,14 @@ static int handle_prepare(struct gateway *g, struct handle *req)
return rc;
}

l = gw_get_readonly(g, request.db_id, req);
conn_readonly = false;
l = gw_get_readwrite(g, request.db_id, NULL);
if (l == NULL) {
return 0;
conn_readonly = true;
l = gw_get_readonly(g, request.db_id, req);
if (l == NULL) {
return 0;
}
}

rc = stmt__registry_add(&g->stmts, &stmt);
Expand All @@ -447,6 +458,12 @@ static int handle_prepare(struct gateway *g, struct handle *req)
req->stmt_id = stmt->id;
req->sql = request.sql;
g->req = req;
if (conn_readonly) {
/* XXX */
g->barrier.data = g;
prepare_barrier_cb(&g->barrier, 0);
return 0;
}
rc = leader_barrier_v2(g->leader, &g->barrier, prepare_barrier_cb);
if (rc == LEADER_NOT_ASYNC) {
prepare_barrier_cb(&g->barrier, 0);
Expand Down Expand Up @@ -713,7 +730,10 @@ static int handle_query(struct gateway *g, struct handle *req)
g->req = req;

if (conn_readonly) {
rv = eager_query_stub();
/* XXX */
g->barrier.data = g;
query_barrier_cb(&g->barrier, 0);
rv = 0;
} else if (stmt_readonly) {
rv = leader_barrier_v2(g->leader, &g->barrier, query_barrier_cb);
if (rv == LEADER_NOT_ASYNC) {
Expand Down Expand Up @@ -949,6 +969,7 @@ static void query_sql_barrier_cb(struct barrier *barrier, int status)
const char *tail;
sqlite3_stmt *tail_stmt;
int tuple_format;
struct leader *l;
int rv;

if (status != 0) {
Expand Down Expand Up @@ -1001,6 +1022,10 @@ static void query_sql_barrier_cb(struct barrier *barrier, int status)
if (sqlite3_stmt_readonly(stmt)) {
query_batch(g);
} else {
l = gw_get_readwrite(g, req->db_id, req);
if (l == NULL) {
return;
}
rv = leader_exec_v2(g->leader, &g->exec, stmt,
modifying_query_sql_exec_cb);
if (rv == LEADER_NOT_ASYNC) {
Expand Down Expand Up @@ -1046,13 +1071,14 @@ static int handle_query_sql(struct gateway *g, struct handle *req)

FAIL_IF_CHECKPOINTING;

req->sql = request.sql;
g->req = req;
if (conn_readonly) {
prepare_transient_and_query_readonly_stub();
g->barrier.data = g;
query_sql_barrier_cb(&g->barrier, 0);
return 0;
}

req->sql = request.sql;
g->req = req;
rv = leader_barrier_v2(g->leader, &g->barrier, query_sql_barrier_cb);
if (rv == LEADER_NOT_ASYNC) {
query_sql_barrier_cb(&g->barrier, 0);
Expand Down
1 change: 0 additions & 1 deletion src/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,6 @@ static int vfsFileShmLock(sqlite3_file *file, int ofst, int n, int flags)
/* When releasing the write lock, if we find a pending
* uncommitted transaction then a rollback must have occurred.
* In that case we delete the pending transaction. */
tracef("ROLLBACK TIME");
if (flags == (SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE)) {
vfsWalRollbackIfUncommitted(wal);
}
Expand Down
1 change: 1 addition & 0 deletions test/unit/test_concurrency.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct connection {
rc = buffer__init(&c->response); \
munit_assert_int(rc, ==, 0); \
open.filename = "test"; \
open.flags = 0; \
open.vfs = ""; \
ENCODE(c, &open, open); \
HANDLE(c, OPEN); \
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ TEST_CASE(query, allow_stale_modifying, NULL)
ENCODE(&f->request, query);
HANDLE(QUERY);
ASSERT_CALLBACK(0, FAILURE);
ASSERT_FAILURE(SQLITE_IOERR_NOT_LEADER, "not leader");
ASSERT_FAILURE(SQLITE_READONLY, "dqlite connection is readonly");
return MUNIT_OK;
}

Expand Down

0 comments on commit 48ccd7a

Please sign in to comment.