Object table subscribe with new semantics (#62)

* new plasma subscribe implementation

* object table subscribe with test

* clang-format

* fix

* fix test

* fix tests

* fix clang-format

* add check

* final clang-format

* final fixes

* fix clang-format
This commit is contained in:
Philipp Moritz
2016-11-27 21:26:23 -08:00
committed by Robert Nishihara
parent bc1d7db926
commit c7073d623b
2 changed files with 197 additions and 16 deletions
+60 -16
View File
@@ -56,6 +56,10 @@ db_handle *db_connect(const char *address,
redisContext *context = redisConnect(address, port);
CHECK_REDIS_CONNECT(redisContext, context, "could not connect to redis %s:%d",
address, port);
/* Enable keyspace events. */
reply = redisCommand(context, "CONFIG SET notify-keyspace-events AKE");
CHECK(reply != NULL);
freeReplyObject(reply);
/* Add new client using optimistic locking. */
db_client_id client = globally_unique_id();
while (true) {
@@ -388,9 +392,38 @@ void redis_object_table_get_entry(redisAsyncContext *c,
}
}
void object_table_redis_callback(redisAsyncContext *c,
void *r,
void *privdata) {
void redis_object_table_subscribe_lookup(redisAsyncContext *c,
void *r,
void *privdata) {
REDIS_CALLBACK_HEADER(db, callback_data, r);
redisReply *reply = r;
if (reply->type == REDIS_REPLY_ARRAY) {
if (reply->elements > 0) {
CHECK(reply->element[0]->len == UNIQUE_ID_SIZE);
/* Check that the reply corresponds to the right object ID. */
CHECK(strncmp(reply->element[0]->str, callback_data->id.id,
UNIQUE_ID_SIZE));
object_table_subscribe_data *data = callback_data->data;
if (data->object_available_callback) {
data->object_available_callback(callback_data->id,
data->subscribe_context);
}
}
} else {
LOG_FATAL("expected integer or string, received type %d", reply->type);
}
if (callback_data->done_callback) {
object_table_done_callback done_callback = callback_data->done_callback;
done_callback(callback_data->id, callback_data->user_context);
}
event_loop_remove_timer(db->loop, callback_data->timer_id);
}
void object_table_redis_subscribe_callback(redisAsyncContext *c,
void *r,
void *privdata) {
REDIS_CALLBACK_HEADER(db, callback_data, r);
redisReply *reply = r;
@@ -399,19 +432,30 @@ void object_table_redis_callback(redisAsyncContext *c,
CHECK(reply->elements > 2);
/* If this condition is true, we got the initial message that acknowledged the
* subscription. */
if (strncmp(reply->element[1]->str, "add", 3) != 0) {
if (callback_data->done_callback) {
object_table_done_callback done_callback = callback_data->done_callback;
done_callback(callback_data->id, callback_data->user_context);
bool is_add =
reply->element[1]->str && strcmp(reply->element[1]->str, "sadd") == 0;
if (is_add) {
/* Do a lookup to see if the key has been in redis before we started the
* subscription. */
int status =
redisAsyncCommand(db->context, redis_object_table_subscribe_lookup,
(void *) callback_data->timer_id, "SMEMBERS obj:%b",
callback_data->id.id, sizeof(callback_data->id.id));
if ((status == REDIS_ERR) || db->context->err) {
LOG_REDIS_ERROR(db->context,
"error in redis_object_table_subscribe_callback");
}
event_loop_remove_timer(db->loop, callback_data->timer_id);
return;
}
/* Otherwise, parse the task and call the callback. */
object_table_subscribe_data *data = callback_data->data;
if (data->object_available_callback) {
data->object_available_callback(callback_data->id, data->subscribe_context);
/* If the subscription is issued, parse the task and call the callback. */
if (strcmp(reply->element[0]->str, "message") == 0) {
object_table_subscribe_data *data = callback_data->data;
if (data->object_available_callback) {
data->object_available_callback(callback_data->id,
data->subscribe_context);
}
}
}
@@ -420,10 +464,10 @@ void redis_object_table_subscribe(table_callback_data *callback_data) {
/* subscribe to key notification associated to object id */
object_id id = callback_data->id;
int status = redisAsyncCommand(db->sub_context, object_table_redis_callback,
(void *) callback_data->timer_id,
"SUBSCRIBE __keyspace@0__:%b add", id.id,
sizeof(id.id));
int status = redisAsyncCommand(
db->sub_context, object_table_redis_subscribe_callback,
(void *) callback_data->timer_id, "SUBSCRIBE __keyspace@0__:obj:%b sadd",
id.id, sizeof(id.id));
if ((status == REDIS_ERR) || db->sub_context->err) {
LOG_REDIS_DEBUG(db->sub_context,
"error in redis_object_table_subscribe_callback");