Refactor state database (#22)

* make db_connect return the connection

* rename db_conn -> db_handle

* more renaming

* clang-format

* free the db_handle
This commit is contained in:
Philipp Moritz
2016-09-25 21:52:06 -07:00
committed by Robert Nishihara
parent 1e08629013
commit db8c0acc71
9 changed files with 67 additions and 69 deletions
+11 -12
View File
@@ -3,22 +3,21 @@
#include "event_loop.h"
typedef struct db_conn_impl db_conn;
typedef struct db_handle_impl db_handle;
/* Connect to the global system store at address and port. The last
* parameter is an output parameter and we assume the memory is
* allocated by the caller. */
void db_connect(const char *db_address,
int db_port,
const char *client_type,
const char *client_addr,
int client_port,
db_conn *db);
/* Connect to the global system store at address and port. Returns
* a handle to the database, which must be freed with db_disconnect
* after use. */
db_handle *db_connect(const char *db_address,
int db_port,
const char *client_type,
const char *client_addr,
int client_port);
/* Attach global system store connection to event loop. */
void db_attach(db_conn *db, event_loop *loop);
void db_attach(db_handle *db, event_loop *loop);
/* Disconnect from the global system store. */
void db_disconnect(db_conn *db);
void db_disconnect(db_handle *db);
#endif
+5 -3
View File
@@ -10,12 +10,14 @@ typedef void (*lookup_callback)(object_id object_id,
/* Register a new object with the directory. */
/* TODO(pcm): Retry, print for each attempt. */
void object_table_add(db_conn *db, object_id object_id);
void object_table_add(db_handle *db, object_id object_id);
/* Remove object from the directory. */
void object_table_remove(db_conn *db, object_id object_id, const char *manager);
void object_table_remove(db_handle *db,
object_id object_id,
const char *manager);
/* Look up entry from the directory */
void object_table_lookup(db_conn *db,
void object_table_lookup(db_handle *db,
object_id object_id,
lookup_callback callback);
+14 -12
View File
@@ -31,12 +31,12 @@
} \
} while (0);
void db_connect(const char *address,
int port,
const char *client_type,
const char *client_addr,
int client_port,
db_conn *db) {
db_handle *db_connect(const char *address,
int port,
const char *client_type,
const char *client_addr,
int client_port) {
db_handle *db = malloc(sizeof(db_handle));
/* Sync connection for initial handshake */
redisReply *reply;
long long num_clients;
@@ -75,9 +75,10 @@ void db_connect(const char *address,
CHECK_REDIS_CONNECT(redisAsyncContext, db->context,
"could not connect to redis %s:%d", address, port);
db->context->data = (void *) db;
return db;
}
void db_disconnect(db_conn *db) {
void db_disconnect(db_handle *db) {
redisFree(db->sync_context);
redisAsyncFree(db->context);
service_cache_entry *e, *tmp;
@@ -87,13 +88,14 @@ void db_disconnect(db_conn *db) {
free(e);
}
free(db->client_type);
free(db);
}
void db_attach(db_conn *db, event_loop *loop) {
void db_attach(db_handle *db, event_loop *loop) {
redisAeAttach(loop, db->context);
}
void object_table_add(db_conn *db, unique_id object_id) {
void object_table_add(db_handle *db, unique_id object_id) {
static char hex_object_id[2 * UNIQUE_ID_SIZE + 1];
sha1_to_hex(&object_id.id[0], &hex_object_id[0]);
redisAsyncCommand(db->context, NULL, NULL, "SADD obj:%s %d",
@@ -104,7 +106,7 @@ void object_table_add(db_conn *db, unique_id object_id) {
}
void object_table_get_entry(redisAsyncContext *c, void *r, void *privdata) {
db_conn *db = c->data;
db_handle *db = c->data;
lookup_callback_data *cb_data = privdata;
redisReply *reply = r;
if (reply == NULL)
@@ -143,7 +145,7 @@ void object_table_get_entry(redisAsyncContext *c, void *r, void *privdata) {
free(result);
}
void object_table_lookup(db_conn *db,
void object_table_lookup(db_handle *db,
object_id object_id,
lookup_callback callback) {
static char hex_object_id[2 * UNIQUE_ID_SIZE + 1];
@@ -158,7 +160,7 @@ void object_table_lookup(db_conn *db,
}
}
void task_queue_submit_task(db_conn *db, task_iid task_iid, task_spec *task) {
void task_queue_submit_task(db_handle *db, task_iid task_iid, task_spec *task) {
/* For converting an id to hex, which has double the number
* of bytes compared to the id (+ 1 byte for '\0'). */
static char hex[2 * UNIQUE_ID_SIZE + 1];
+1 -1
View File
@@ -17,7 +17,7 @@ typedef struct {
UT_hash_handle hh;
} service_cache_entry;
struct db_conn_impl {
struct db_handle_impl {
/* String that identifies this client type. */
char *client_type;
/* Unique ID for this client within the type. */
+3 -3
View File
@@ -21,13 +21,13 @@ typedef unique_id node_id;
typedef void (*task_queue_callback)(task_iid *task_iid, task_spec *task);
/* Submit task to the global scheduler. */
void task_queue_submit_task(db_conn *db, task_iid task_iid, task_spec *task);
void task_queue_submit_task(db_handle *db, task_iid task_iid, task_spec *task);
/* Submit task to a local scheduler based on the decision made by the global
* scheduler. */
void task_queue_schedule_task(db_conn *db, task_iid task_iid, node_id node);
void task_queue_schedule_task(db_handle *db, task_iid task_iid, node_id node);
/* Subscribe to task queue. */
void task_queue_register_callback(db_conn *db, task_queue_callback callback);
void task_queue_register_callback(db_handle *db, task_queue_callback callback);
#endif
+2 -2
View File
@@ -5,9 +5,9 @@
#include "task.h"
/* Add task to the task table, handle errors here. */
status task_table_add_task(db_conn *db, task_iid task_iid, task_spec *task);
status task_table_add_task(db_handle *db, task_iid task_iid, task_spec *task);
/* Get specific task from the task table. */
status task_table_get_task(db_conn *db, task_iid task_iid, task_spec *task);
status task_table_get_task(db_handle *db, task_iid task_iid, task_spec *task);
#endif /* TASK_TABLE_H */