Skip to content

Commit ee3718c

Browse files
istoicapcmoritz
authored andcommitted
Ion and Philipp's table retries (#10)
* Ion and Philipp's table retries * Refactor the retry struct: - Rename it from retry_struct to retry_info - Retry information contains the failure callback, not the retry callback - All functions take in retry information as an arg instead of its expanded fields * Rename cb -> callback * Remove prints * Fix compiler warnings * Change some CHECKs to greatest ASSERTs * Key outstanding callbacks hash table with timer ID instead of callback data pointer * Use the new retry API for table commands * Memory cleanup in plasma unit tests * fix Robert's comments * add valgrind for common
1 parent 84c581c commit ee3718c

23 files changed

+1914
-240
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ matrix:
3737
- sudo apt-get update -qq
3838
- sudo apt-get install -qq valgrind
3939
script:
40+
- cd src/common
41+
- make valgrind
42+
- cd ../..
43+
4044
- cd src/plasma
4145
- make valgrind
4246
- cd ../..

src/common/Makefile

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
CC = gcc
2-
CFLAGS = -g -Wall --std=c99 -D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200809L -fPIC -I. -Ithirdparty -Ithirdparty/ae -Wno-typedef-redefinition -Werror
2+
CFLAGS = -g -Wall -Wno-typedef-redefinition --std=c99 -D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200809L -fPIC -I. -Ithirdparty -Ithirdparty/ae
33
BUILD = build
44

55
all: hiredis $(BUILD)/libcommon.a
66

7-
$(BUILD)/libcommon.a: event_loop.o common.o task.o io.o state/redis.o thirdparty/ae/ae.o
7+
$(BUILD)/libcommon.a: event_loop.o common.o task.o io.o state/redis.o state/table.o state/object_table.o state/task_log.o thirdparty/ae/ae.o
88
ar rcs $@ $^
99

1010
$(BUILD)/common_tests: test/common_tests.c $(BUILD)/libcommon.a
@@ -13,6 +13,12 @@ $(BUILD)/common_tests: test/common_tests.c $(BUILD)/libcommon.a
1313
$(BUILD)/db_tests: hiredis test/db_tests.c $(BUILD)/libcommon.a
1414
$(CC) -o $@ test/db_tests.c $(BUILD)/libcommon.a thirdparty/hiredis/libhiredis.a $(CFLAGS)
1515

16+
$(BUILD)/object_table_tests: hiredis test/object_table_tests.c $(BUILD)/libcommon.a
17+
$(CC) -o $@ test/object_table_tests.c $(BUILD)/libcommon.a thirdparty/hiredis/libhiredis.a $(CFLAGS)
18+
19+
$(BUILD)/task_log_tests: hiredis test/task_log_tests.c $(BUILD)/libcommon.a
20+
$(CC) -o $@ test/task_log_tests.c $(BUILD)/libcommon.a thirdparty/hiredis/libhiredis.a $(CFLAGS)
21+
1622
$(BUILD)/io_tests: test/io_tests.c $(BUILD)/libcommon.a
1723
$(CC) -o $@ $^ $(CFLAGS)
1824

@@ -32,15 +38,17 @@ redis:
3238
hiredis:
3339
git submodule update --init --recursive -- "thirdparty/hiredis" ; cd thirdparty/hiredis ; make
3440

35-
test: hiredis redis $(BUILD)/common_tests $(BUILD)/db_tests $(BUILD)/io_tests $(BUILD)/task_tests $(BUILD)/redis_tests FORCE
41+
test: hiredis redis $(BUILD)/common_tests $(BUILD)/task_log_tests $(BUILD)/object_table_tests $(BUILD)/db_tests $(BUILD)/io_tests $(BUILD)/task_tests $(BUILD)/redis_tests FORCE
3642
./thirdparty/redis-3.2.3/src/redis-server &
37-
sleep 1s ; ./build/common_tests ; ./build/db_tests ; ./build/io_tests ; ./build/task_tests ; ./build/redis_tests
43+
sleep 1s ; ./build/common_tests ; ./build/db_tests ; ./build/task_log_tests ; ./build/object_table_tests ; ./build/io_tests ; ./build/task_tests ; ./build/redis_tests
3844

3945
valgrind: test
4046
valgrind --leak-check=full --error-exitcode=1 ./build/common_tests
4147
valgrind --leak-check=full --error-exitcode=1 ./build/db_tests
4248
valgrind --leak-check=full --error-exitcode=1 ./build/io_tests
4349
valgrind --leak-check=full --error-exitcode=1 ./build/task_tests
4450
valgrind --leak-check=full --error-exitcode=1 ./build/redis_tests
51+
valgrind --leak-check=full --error-exitcode=1 ./build/task_log_tests
52+
valgrind --leak-check=full --error-exitcode=1 ./build/object_table_tests
4553

4654
FORCE:

src/common/common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdlib.h>
66
#include <string.h>
77
#include <errno.h>
8+
#include <inttypes.h>
89

910
#ifndef RAY_COMMON_DEBUG
1011
#define LOG_DEBUG(M, ...)
@@ -36,6 +37,10 @@
3637
} \
3738
} while (0);
3839

40+
/** This macro indicates that this pointer owns the data it is pointing to
41+
* and is responsible for freeing it. */
42+
#define OWNER
43+
3944
#define UNIQUE_ID_SIZE 20
4045

4146
/* Cleanup method for running tests with the greatest library.

src/common/event_loop.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@ void event_loop_remove_file(event_loop *loop, int fd) {
4242
}
4343

4444
int64_t event_loop_add_timer(event_loop *loop,
45-
int64_t milliseconds,
45+
int64_t timeout,
4646
event_loop_timer_handler handler,
4747
void *context) {
48-
return aeCreateTimeEvent(loop, milliseconds, handler, context, NULL);
48+
return aeCreateTimeEvent(loop, timeout, handler, context, NULL);
4949
}
5050

51-
void event_loop_remove_timer(event_loop *loop, timer_id timer_id) {
52-
int err = aeDeleteTimeEvent(loop, timer_id);
53-
CHECK(err == AE_OK); /* timer id found? */
51+
int event_loop_remove_timer(event_loop *loop, int64_t id) {
52+
return aeDeleteTimeEvent(loop, id);
5453
}
5554

5655
void event_loop_run(event_loop *loop) {

src/common/event_loop.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <stdint.h>
55
#include "ae/ae.h"
66

7+
/* Unique timer ID that will be generated when the timer is added to the
8+
* event loop. Will not be reused later on in another call
9+
* to event_loop_add_timer. */
710
typedef long long timer_id;
811

912
typedef aeEventLoop event_loop;
@@ -57,16 +60,30 @@ void event_loop_add_file(event_loop *loop,
5760
/* Remove a registered file event handler from the event loop. */
5861
void event_loop_remove_file(event_loop *loop, int fd);
5962

60-
/* Register a handler that will be called after a time slice of
61-
* "milliseconds" milliseconds. Can specify a context that will be passed
62-
* as an argument to the handler. Return the id of the time event. */
63+
/** Register a handler that will be called after a time slice of
64+
* "timeout" milliseconds.
65+
*
66+
* @param loop The event loop.
67+
* @param timeout The timeout in milliseconds.
68+
* @param handler The handler for the timeout.
69+
* @param context User context that can be passed in and will be passed in
70+
* as an argument for the timer handler.
71+
* @return The ID of the timer.
72+
*/
6373
int64_t event_loop_add_timer(event_loop *loop,
64-
int64_t milliseconds,
74+
int64_t timeout,
6575
event_loop_timer_handler handler,
6676
void *context);
6777

68-
/* Remove a registered time event handler from the event loop. */
69-
void event_loop_remove_timer(event_loop *loop, timer_id timer_id);
78+
/**
79+
* Remove a registered time event handler from the event loop. Can be called
80+
* multiple times on the same timer.
81+
*
82+
* @param loop The event loop.
83+
* @param timer_id The ID of the timer to be removed.
84+
* @return Returns 0 if the removal was successful.
85+
*/
86+
int event_loop_remove_timer(event_loop *loop, int64_t timer_id);
7087

7188
/* Run the event loop. */
7289
void event_loop_run(event_loop *loop);

src/common/state/db.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "event_loop.h"
55

6-
typedef struct db_handle_impl db_handle;
6+
typedef struct db_handle db_handle;
77

88
/* Connect to the global system store at address and port. Returns
99
* a handle to the database, which must be freed with db_disconnect

src/common/state/object_table.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "object_table.h"
2+
#include "redis.h"
3+
4+
void object_table_lookup(db_handle *db_handle,
5+
object_id object_id,
6+
retry_info *retry,
7+
object_table_lookup_done_callback done_callback,
8+
void *user_context) {
9+
init_table_callback(db_handle, object_id, NULL, retry, done_callback,
10+
redis_object_table_lookup, user_context);
11+
}
12+
13+
void object_table_add(db_handle *db_handle,
14+
object_id object_id,
15+
retry_info *retry,
16+
object_table_done_callback done_callback,
17+
void *user_context) {
18+
init_table_callback(db_handle, object_id, NULL, retry, done_callback,
19+
redis_object_table_add, user_context);
20+
}
21+
22+
void object_table_subscribe(
23+
db_handle *db_handle,
24+
object_id object_id,
25+
object_table_object_available_callback object_available_callback,
26+
void *subscribe_context,
27+
retry_info *retry,
28+
object_table_done_callback done_callback,
29+
void *user_context) {
30+
object_table_subscribe_data *sub_data =
31+
malloc(sizeof(object_table_subscribe_data));
32+
utarray_push_back(db_handle->callback_freelist, &sub_data);
33+
sub_data->object_available_callback = object_available_callback;
34+
sub_data->subscribe_context = subscribe_context;
35+
36+
init_table_callback(db_handle, object_id, sub_data, retry, done_callback,
37+
redis_object_table_subscribe, user_context);
38+
}

src/common/state/object_table.h

Lines changed: 116 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,126 @@
1+
#ifndef OBJECT_TABLE_H
2+
#define OBJECT_TABLE_H
3+
14
#include "common.h"
5+
#include "table.h"
26
#include "db.h"
37

4-
/* The callback that is called when the result of a lookup
5-
* in the object table comes back. The callback should free
6-
* the manager_vector array, but NOT the strings they are pointing to. */
7-
typedef void (*lookup_callback)(object_id object_id,
8-
int manager_count,
9-
const char *manager_vector[],
10-
void *context);
8+
/*
9+
* ==== Lookup call and callback ====
10+
*/
1111

12-
/* Register a new object with the directory. */
13-
/* TODO(pcm): Retry, print for each attempt. */
14-
void object_table_add(db_handle *db, object_id object_id);
12+
/* Callback called when the lookup completes. The callback should free
13+
* the manager_vector array, but NOT the strings they are pointing to.
14+
*/
15+
typedef void (*object_table_lookup_done_callback)(
16+
object_id object_id,
17+
int manager_count,
18+
OWNER const char *manager_vector[],
19+
void *user_context);
1520

16-
/* Remove object from the directory. */
17-
void object_table_remove(db_handle *db,
21+
/**
22+
* Return the list of nodes storing object_id in their plasma stores.
23+
*
24+
* @param db_handle Handle to object_table database.
25+
* @param object_id ID of the object being looked up.
26+
* @param retry Information about retrying the request to the database.
27+
* @param done_callback Function to be called when database returns result.
28+
* @param user_context Context passed by the caller.
29+
* @return Void.
30+
*/
31+
void object_table_lookup(db_handle *db_handle,
1832
object_id object_id,
19-
const char *manager);
33+
retry_info *retry,
34+
object_table_lookup_done_callback done_callback,
35+
void *user_context);
36+
37+
/*
38+
* ==== Add object call and callback ====
39+
*/
40+
41+
/* Callback called when the object add/remove operation completes. */
42+
typedef void (*object_table_done_callback)(object_id object_id,
43+
void *user_context);
2044

21-
/* Look up entry from the directory */
22-
void object_table_lookup(db_handle *db,
45+
/**
46+
* Add the plasma manager that created the db_handle to the
47+
* list of plasma managers that have the object_id.
48+
*
49+
* @param db_handle Handle to db.
50+
* @param object_id Object unique identifier.
51+
* @param retry Information about retrying the request to the database.
52+
* @param done_callback Callback to be called when lookup completes.
53+
* @param user_context User context to be passed in the callbacks.
54+
* @return Void.
55+
*/
56+
void object_table_add(db_handle *db_handle,
57+
object_id object_id,
58+
retry_info *retry,
59+
object_table_done_callback done_callback,
60+
void *user_context);
61+
62+
/*
63+
* ==== Remove object call and callback ====
64+
*/
65+
66+
/**
67+
* Object remove function.
68+
*
69+
* @param db_handle Handle to db.
70+
* @param object_id Object unique identifier.
71+
* @param retry Information about retrying the request to the database.
72+
* @param done_callback Callback to be called when lookup completes.
73+
* @param user_context User context to be passed in the callbacks.
74+
* @return Void.
75+
*/
76+
/*
77+
void object_table_remove(db_handle *db,
2378
object_id object_id,
2479
lookup_callback callback,
2580
void *context);
81+
retry_info *retry,
82+
object_table_done_callback done_callback,
83+
void *user_context);
84+
*/
85+
86+
/*
87+
* ==== Subscribe to be announced when new object available ====
88+
*/
89+
90+
/* Callback called when object object_id is available. */
91+
typedef void (*object_table_object_available_callback)(object_id object_id,
92+
void *user_context);
93+
94+
/**
95+
* Subcribing to new object available function.
96+
*
97+
* @param db_handle Handle to db.
98+
* @param object_id Object unique identifier.
99+
* @param object_available_callback callback to be called when new object
100+
* becomes
101+
* available.
102+
* @param subscribe_context caller context which will be passed back in the
103+
* object_available_callback.
104+
* @param retry Information about retrying the request to the database.
105+
* @param done_callback Callback to be called when subscription is installed.
106+
* @param user_context User context to be passed in the callbacks.
107+
* @return Void.
108+
*/
109+
110+
void object_table_subscribe(
111+
db_handle *db,
112+
object_id object_id,
113+
object_table_object_available_callback object_available_callback,
114+
void *subscribe_context,
115+
retry_info *retry,
116+
object_table_done_callback done_callback,
117+
void *user_context);
118+
119+
/* Data that is needed to register new object available callbacks with the state
120+
* database. */
121+
typedef struct {
122+
object_table_object_available_callback object_available_callback;
123+
void *subscribe_context;
124+
} object_table_subscribe_data;
125+
126+
#endif /* OBJECT_TABLE_H */

0 commit comments

Comments
 (0)