mirror of
https://github.com/wassname/ray.git
synced 2026-09-09 11:32:43 +08:00
[WIP] Event loop refactoring (#19)
* task queue tests and extensions * event loop refactor * fix formating
This commit is contained in:
committed by
Robert Nishihara
parent
e1b8711a01
commit
7907992609
+22
-77
@@ -6,12 +6,12 @@
|
||||
#include "test/example_task.h"
|
||||
#include "state/db.h"
|
||||
#include "state/object_table.h"
|
||||
#include "state/task_queue.h"
|
||||
#include "state/redis.h"
|
||||
#include "task.h"
|
||||
|
||||
SUITE(db_tests);
|
||||
|
||||
int lookup_successful = 0;
|
||||
const char *manager_addr = "127.0.0.1";
|
||||
int manager_port1 = 12345;
|
||||
int manager_port2 = 12346;
|
||||
@@ -20,20 +20,11 @@ char received_port1[6] = {0};
|
||||
char received_addr2[16] = {0};
|
||||
char received_port2[6] = {0};
|
||||
|
||||
/* This is for synchronizing to make sure both entries have been written. */
|
||||
void sync_test_callback(object_id object_id,
|
||||
int manager_count,
|
||||
const char *manager_vector[]) {
|
||||
lookup_successful = 1;
|
||||
free(manager_vector);
|
||||
}
|
||||
|
||||
/* This performs the actual test. */
|
||||
/* Test if entries have been written to the database. */
|
||||
void test_callback(object_id object_id,
|
||||
int manager_count,
|
||||
const char *manager_vector[]) {
|
||||
CHECK(manager_count == 2);
|
||||
lookup_successful = 1;
|
||||
if (!manager_vector[0] ||
|
||||
sscanf(manager_vector[0], "%15[0-9.]:%5[0-9]", received_addr1,
|
||||
received_port1) != 2) {
|
||||
@@ -47,57 +38,29 @@ void test_callback(object_id object_id,
|
||||
free(manager_vector);
|
||||
}
|
||||
|
||||
int64_t timeout_handler(event_loop *loop, int64_t id, void *context) {
|
||||
event_loop_stop(loop);
|
||||
return -1;
|
||||
}
|
||||
|
||||
TEST object_table_lookup_test(void) {
|
||||
event_loop loop;
|
||||
event_loop_init(&loop);
|
||||
event_loop *loop = event_loop_create();
|
||||
db_conn conn1;
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", manager_addr, manager_port1,
|
||||
&conn1);
|
||||
db_conn conn2;
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", manager_addr, manager_port2,
|
||||
&conn2);
|
||||
int64_t index1 = db_attach(&conn1, &loop, 0);
|
||||
int64_t index2 = db_attach(&conn2, &loop, 1);
|
||||
db_attach(&conn1, loop);
|
||||
db_attach(&conn2, loop);
|
||||
unique_id id = globally_unique_id();
|
||||
object_table_add(&conn1, id);
|
||||
object_table_add(&conn2, id);
|
||||
object_table_lookup(&conn1, id, sync_test_callback);
|
||||
while (!lookup_successful) {
|
||||
int num_ready = event_loop_poll(&loop, -1);
|
||||
if (num_ready < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
for (int i = 0; i < event_loop_size(&loop); ++i) {
|
||||
struct pollfd *waiting = event_loop_get(&loop, i);
|
||||
if (waiting->revents == 0)
|
||||
continue;
|
||||
if (i == index1) {
|
||||
db_event(&conn1);
|
||||
}
|
||||
if (i == index2) {
|
||||
db_event(&conn2);
|
||||
}
|
||||
}
|
||||
}
|
||||
lookup_successful = 0;
|
||||
event_loop_add_timer(loop, 100, timeout_handler, NULL);
|
||||
event_loop_run(loop);
|
||||
object_table_lookup(&conn1, id, test_callback);
|
||||
while (!lookup_successful) {
|
||||
int num_ready = event_loop_poll(&loop, -1);
|
||||
if (num_ready < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
for (int i = 0; i < event_loop_size(&loop); ++i) {
|
||||
struct pollfd *waiting = event_loop_get(&loop, i);
|
||||
if (waiting->revents == 0)
|
||||
continue;
|
||||
if (i == index1) {
|
||||
db_event(&conn1);
|
||||
}
|
||||
if (i == index2) {
|
||||
db_event(&conn2);
|
||||
}
|
||||
}
|
||||
}
|
||||
event_loop_add_timer(loop, 100, timeout_handler, NULL);
|
||||
event_loop_run(loop);
|
||||
int port1 = atoi(received_port1);
|
||||
int port2 = atoi(received_port2);
|
||||
ASSERT_STR_EQ(&received_addr1[0], manager_addr);
|
||||
@@ -107,50 +70,32 @@ TEST object_table_lookup_test(void) {
|
||||
db_disconnect(&conn1);
|
||||
db_disconnect(&conn2);
|
||||
|
||||
event_loop_free(&loop);
|
||||
|
||||
lookup_successful = 0;
|
||||
event_loop_destroy(loop);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST task_queue_test(void) {
|
||||
event_loop loop;
|
||||
event_loop_init(&loop);
|
||||
event_loop *loop = event_loop_create();
|
||||
db_conn conn;
|
||||
db_connect("127.0.0.1", 6379, "local_scheduler", "", -1, &conn);
|
||||
int64_t index = db_attach(&conn, &loop, 0);
|
||||
db_attach(&conn, loop);
|
||||
|
||||
task_spec *task = example_task();
|
||||
task_queue_submit_task(&conn, globally_unique_id(), task);
|
||||
while (1) {
|
||||
int num_ready = event_loop_poll(&loop, 100);
|
||||
if (num_ready < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
if (num_ready == 0) {
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < event_loop_size(&loop); ++i) {
|
||||
struct pollfd *waiting = event_loop_get(&loop, i);
|
||||
if (waiting->revents == 0)
|
||||
continue;
|
||||
if (i == index) {
|
||||
db_event(&conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
event_loop_add_timer(loop, 100, timeout_handler, NULL);
|
||||
event_loop_run(loop);
|
||||
|
||||
free_task_spec(task);
|
||||
db_disconnect(&conn);
|
||||
event_loop_free(&loop);
|
||||
event_loop_destroy(loop);
|
||||
PASS();
|
||||
}
|
||||
|
||||
SUITE(db_tests) {
|
||||
redisContext *context = redisConnect("127.0.0.1", 6379);
|
||||
redisCommand(context, "FLUSHALL");
|
||||
freeReplyObject(redisCommand(context, "FLUSHALL"));
|
||||
RUN_REDIS_TEST(context, object_table_lookup_test);
|
||||
RUN_TEST(task_queue_test);
|
||||
RUN_REDIS_TEST(context, task_queue_test);
|
||||
redisFree(context);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ TEST ipc_socket_test(void) {
|
||||
uint8_t *bytes;
|
||||
read_bytes(client_fd, &bytes, &len);
|
||||
ASSERT(memcmp(test_bytes, bytes, len) == 0);
|
||||
free(bytes);
|
||||
close(client_fd);
|
||||
close(socket_fd);
|
||||
unlink(socket_pathname);
|
||||
|
||||
+112
-90
@@ -3,6 +3,8 @@
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "utarray.h"
|
||||
|
||||
#include "event_loop.h"
|
||||
#include "state/db.h"
|
||||
#include "state/redis.h"
|
||||
@@ -11,35 +13,27 @@
|
||||
|
||||
SUITE(redis_tests);
|
||||
|
||||
int lookup_successful = 0;
|
||||
const char *test_set_format = "SET %s %s";
|
||||
const char *test_get_format = "GET %s";
|
||||
const char *test_key = "foo";
|
||||
const char *test_value = "bar";
|
||||
UT_array *connections = NULL;
|
||||
|
||||
int async_redis_socket_test_callback_called = 0;
|
||||
|
||||
void async_redis_socket_test_callback(redisAsyncContext *ac,
|
||||
void *r,
|
||||
void *privdata) {
|
||||
async_redis_socket_test_callback_called = 1;
|
||||
redisContext *context = redisConnect("127.0.0.1", 6379);
|
||||
redisReply *reply = redisCommand(context, test_get_format, test_key);
|
||||
redisFree(context);
|
||||
assert(reply != NULL);
|
||||
CHECK(reply != NULL);
|
||||
if (strcmp(reply->str, test_value)) {
|
||||
freeReplyObject(reply);
|
||||
assert(0);
|
||||
CHECK(0);
|
||||
}
|
||||
freeReplyObject(reply);
|
||||
lookup_successful = 1;
|
||||
}
|
||||
|
||||
void logging_test_callback(redisAsyncContext *ac, void *r, void *privdata) {
|
||||
redisContext *context = redisConnect("127.0.0.1", 6379);
|
||||
redisReply *reply = redisCommand(context, "KEYS %s", "log:*");
|
||||
redisFree(context);
|
||||
assert(reply != NULL);
|
||||
assert(reply->elements > 0);
|
||||
freeReplyObject(reply);
|
||||
lookup_successful = 1;
|
||||
}
|
||||
|
||||
TEST redis_socket_test(void) {
|
||||
@@ -73,117 +67,145 @@ TEST redis_socket_test(void) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
void redis_read_callback(event_loop *loop, int fd, void *context, int events) {
|
||||
db_conn *conn = context;
|
||||
char *cmd = read_string(fd);
|
||||
redisAsyncCommand(conn->context, async_redis_socket_test_callback, NULL, cmd,
|
||||
conn->client_id, 0);
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
void redis_accept_callback(event_loop *loop,
|
||||
int socket_fd,
|
||||
void *context,
|
||||
int events) {
|
||||
int accept_fd = accept_client(socket_fd);
|
||||
CHECK(accept_fd >= 0);
|
||||
utarray_push_back(connections, &accept_fd);
|
||||
event_loop_add_file(loop, accept_fd, EVENT_LOOP_READ, redis_read_callback,
|
||||
context);
|
||||
}
|
||||
|
||||
int64_t timeout_handler(event_loop *loop, int64_t id, void *context) {
|
||||
event_loop_stop(loop);
|
||||
return -1;
|
||||
}
|
||||
|
||||
TEST async_redis_socket_test(void) {
|
||||
int socket_fd, server_fd, client_fd;
|
||||
event_loop loop;
|
||||
event_loop_init(&loop);
|
||||
utarray_new(connections, &ut_int_icd);
|
||||
event_loop *loop = event_loop_create();
|
||||
|
||||
/* Start IPC channel. */
|
||||
const char *socket_pathname = "async-redis-test-socket";
|
||||
socket_fd = bind_ipc_sock(socket_pathname);
|
||||
int socket_fd = bind_ipc_sock(socket_pathname);
|
||||
ASSERT(socket_fd >= 0);
|
||||
int64_t ipc_index = event_loop_attach(&loop, 1, NULL, socket_fd, POLLIN);
|
||||
utarray_push_back(connections, &socket_fd);
|
||||
|
||||
/* Start connection to Redis. */
|
||||
db_conn conn;
|
||||
db_connect("127.0.0.1", 6379, "", "", 0, &conn);
|
||||
int64_t db_index = db_attach(&conn, &loop, 0);
|
||||
db_attach(&conn, loop);
|
||||
|
||||
/* Send a command to the Redis process. */
|
||||
client_fd = connect_ipc_sock(socket_pathname);
|
||||
int client_fd = connect_ipc_sock(socket_pathname);
|
||||
ASSERT(client_fd >= 0);
|
||||
utarray_push_back(connections, &client_fd);
|
||||
write_formatted_string(client_fd, test_set_format, test_key, test_value);
|
||||
|
||||
while (!lookup_successful) {
|
||||
int num_ready = event_loop_poll(&loop, -1);
|
||||
if (num_ready < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
for (int i = 0; i < event_loop_size(&loop); ++i) {
|
||||
struct pollfd *waiting = event_loop_get(&loop, i);
|
||||
if (waiting->revents == 0)
|
||||
continue;
|
||||
if (i == db_index) {
|
||||
db_event(&conn);
|
||||
} else if (i == ipc_index) {
|
||||
/* For some reason, this check is necessary for Travis
|
||||
* to pass these tests. */
|
||||
ASSERT(waiting->revents & POLLIN);
|
||||
server_fd = accept_client(socket_fd);
|
||||
ASSERT(server_fd >= 0);
|
||||
event_loop_attach(&loop, 1, NULL, server_fd, POLLIN);
|
||||
} else {
|
||||
char *cmd = read_string(waiting->fd);
|
||||
redisAsyncCommand(conn.context, async_redis_socket_test_callback, NULL,
|
||||
cmd, conn.client_id, 0);
|
||||
free(cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
event_loop_add_file(loop, client_fd, EVENT_LOOP_READ, redis_read_callback,
|
||||
&conn);
|
||||
event_loop_add_file(loop, socket_fd, EVENT_LOOP_READ, redis_accept_callback,
|
||||
&conn);
|
||||
event_loop_add_timer(loop, 100, timeout_handler, NULL);
|
||||
event_loop_run(loop);
|
||||
|
||||
CHECK(async_redis_socket_test_callback_called);
|
||||
|
||||
db_disconnect(&conn);
|
||||
event_loop_free(&loop);
|
||||
close(server_fd);
|
||||
close(client_fd);
|
||||
close(socket_fd);
|
||||
event_loop_destroy(loop);
|
||||
for (int *p = (int *) utarray_front(connections); p != NULL;
|
||||
p = (int *) utarray_next(connections, p)) {
|
||||
close(*p);
|
||||
}
|
||||
unlink(socket_pathname);
|
||||
lookup_successful = 0;
|
||||
utarray_free(connections);
|
||||
PASS();
|
||||
}
|
||||
|
||||
int logging_test_callback_called = 0;
|
||||
|
||||
void logging_test_callback(redisAsyncContext *ac, void *r, void *privdata) {
|
||||
logging_test_callback_called = 1;
|
||||
redisContext *context = redisConnect("127.0.0.1", 6379);
|
||||
redisReply *reply = redisCommand(context, "KEYS %s", "log:*");
|
||||
redisFree(context);
|
||||
CHECK(reply != NULL);
|
||||
CHECK(reply->elements > 0);
|
||||
freeReplyObject(reply);
|
||||
}
|
||||
|
||||
void logging_read_callback(event_loop *loop,
|
||||
int fd,
|
||||
void *context,
|
||||
int events) {
|
||||
db_conn *conn = context;
|
||||
char *cmd = read_string(fd);
|
||||
redisAsyncCommand(conn->context, logging_test_callback, NULL, cmd,
|
||||
conn->client_id, 0);
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
void logging_accept_callback(event_loop *loop,
|
||||
int socket_fd,
|
||||
void *context,
|
||||
int events) {
|
||||
int accept_fd = accept_client(socket_fd);
|
||||
CHECK(accept_fd >= 0);
|
||||
utarray_push_back(connections, &accept_fd);
|
||||
event_loop_add_file(loop, accept_fd, EVENT_LOOP_READ, logging_read_callback,
|
||||
context);
|
||||
}
|
||||
|
||||
TEST logging_test(void) {
|
||||
int socket_fd, server_fd, client_fd;
|
||||
event_loop loop;
|
||||
event_loop_init(&loop);
|
||||
utarray_new(connections, &ut_int_icd);
|
||||
event_loop *loop = event_loop_create();
|
||||
|
||||
/* Start IPC channel. */
|
||||
const char *socket_pathname = "logging-test-socket";
|
||||
socket_fd = bind_ipc_sock(socket_pathname);
|
||||
int socket_fd = bind_ipc_sock(socket_pathname);
|
||||
ASSERT(socket_fd >= 0);
|
||||
int64_t ipc_index = event_loop_attach(&loop, 1, NULL, socket_fd, POLLIN);
|
||||
utarray_push_back(connections, &socket_fd);
|
||||
|
||||
/* Start connection to Redis. */
|
||||
db_conn conn;
|
||||
db_connect("127.0.0.1", 6379, "", "", 0, &conn);
|
||||
int64_t db_index = db_attach(&conn, &loop, 0);
|
||||
db_attach(&conn, loop);
|
||||
|
||||
/* Send a command to the Redis process. */
|
||||
client_fd = connect_ipc_sock(socket_pathname);
|
||||
int client_fd = connect_ipc_sock(socket_pathname);
|
||||
ASSERT(client_fd >= 0);
|
||||
utarray_push_back(connections, &client_fd);
|
||||
ray_logger *logger = init_ray_logger("worker", RAY_INFO, 0, &client_fd);
|
||||
ray_log(logger, RAY_INFO, "TEST", "Message");
|
||||
|
||||
while (!lookup_successful) {
|
||||
int num_ready = event_loop_poll(&loop, -1);
|
||||
if (num_ready < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
for (int i = 0; i < event_loop_size(&loop); ++i) {
|
||||
struct pollfd *waiting = event_loop_get(&loop, i);
|
||||
if (waiting->revents == 0)
|
||||
continue;
|
||||
if (i == db_index) {
|
||||
db_event(&conn);
|
||||
} else if (i == ipc_index) {
|
||||
/* For some reason, this check is necessary for Travis
|
||||
* to pass these tests. */
|
||||
ASSERT(waiting->revents & POLLIN);
|
||||
server_fd = accept_client(socket_fd);
|
||||
ASSERT(server_fd >= 0);
|
||||
event_loop_attach(&loop, 1, NULL, server_fd, POLLIN);
|
||||
} else {
|
||||
char *cmd = read_string(waiting->fd);
|
||||
redisAsyncCommand(conn.context, logging_test_callback, NULL, cmd,
|
||||
conn.client_id, 0);
|
||||
free(cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
event_loop_add_file(loop, socket_fd, EVENT_LOOP_READ, logging_accept_callback,
|
||||
&conn);
|
||||
event_loop_add_file(loop, client_fd, EVENT_LOOP_READ, logging_read_callback,
|
||||
&conn);
|
||||
event_loop_add_timer(loop, 100, timeout_handler, NULL);
|
||||
event_loop_run(loop);
|
||||
|
||||
CHECK(logging_test_callback_called);
|
||||
|
||||
free_ray_logger(logger);
|
||||
db_disconnect(&conn);
|
||||
event_loop_free(&loop);
|
||||
close(server_fd);
|
||||
close(client_fd);
|
||||
close(socket_fd);
|
||||
event_loop_destroy(loop);
|
||||
for (int *p = (int *) utarray_front(connections); p != NULL;
|
||||
p = (int *) utarray_next(connections, p)) {
|
||||
close(*p);
|
||||
}
|
||||
unlink(socket_pathname);
|
||||
lookup_successful = 0;
|
||||
utarray_free(connections);
|
||||
PASS();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user