Skip to content

Commit c94f961

Browse files
Merge pull request #9 from ray-project/port
Unit testing for the plasma manager.
2 parents 5c50d97 + f3a4317 commit c94f961

File tree

9 files changed

+609
-110
lines changed

9 files changed

+609
-110
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ matrix:
3737
- sudo apt-get update -qq
3838
- sudo apt-get install -qq valgrind
3939
script:
40+
- cd src/plasma
41+
- make valgrind
42+
- cd ../..
43+
44+
- python src/plasma/test/test.py valgrind
45+
4046
- python src/photon/test/test.py valgrind
4147

4248
install:

src/plasma/Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CC = gcc
22
CFLAGS = -g -Wall --std=c99 -D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200809L -I. -I../common -I../common/thirdparty
3+
TEST_CFLAGS = -DPLASMA_TEST=1 -I.
34
BUILD = build
45

56
all: $(BUILD)/plasma_store $(BUILD)/plasma_manager $(BUILD)/plasma_client.so $(BUILD)/example $(BUILD)/libplasma_client.a
@@ -12,6 +13,9 @@ clean:
1213
cd ../common; make clean
1314
rm -r $(BUILD)/*
1415

16+
$(BUILD)/manager_tests: test/manager_tests.c plasma.h plasma_client.h plasma_client.c plasma_manager.h plasma_manager.c fling.h fling.c common
17+
$(CC) $(CFLAGS) $(TEST_CFLAGS) -o $@ test/manager_tests.c plasma_manager.c plasma_client.c fling.c ../common/build/libcommon.a ../common/thirdparty/hiredis/libhiredis.a
18+
1519
$(BUILD)/plasma_store: plasma_store.c plasma.h fling.h fling.c malloc.c malloc.h thirdparty/dlmalloc.c common
1620
$(CC) $(CFLAGS) plasma_store.c fling.c malloc.c ../common/build/libcommon.a -o $(BUILD)/plasma_store
1721

@@ -28,12 +32,19 @@ $(BUILD)/example: plasma_client.c plasma.h example.c fling.h fling.c common
2832
$(CC) $(CFLAGS) plasma_client.c example.c fling.c ../common/build/libcommon.a -o $(BUILD)/example
2933

3034
common: FORCE
31-
cd ../common; make
35+
git submodule update --init --recursive
36+
cd ../common; make
3237

3338
# Set the request timeout low for testing purposes.
3439
test: CFLAGS += -DRAY_TIMEOUT=50
35-
test: FORCE
36-
cd ../common; make redis
40+
# First, build and run all the unit tests.
41+
test: $(BUILD)/manager_tests FORCE
42+
./build/manager_tests
43+
cd ../common; make redis
44+
# Next, build all the executables for Python testing.
3745
test: all
3846

47+
valgrind: test
48+
valgrind --leak-check=full --error-exitcode=1 ./build/manager_tests
49+
3950
FORCE:

src/plasma/plasma_client.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "fling.h"
2121
#include "uthash.h"
2222

23+
/* Number of times we try connecting to a socket. */
24+
#define NUM_CONNECT_ATTEMPTS 50
25+
2326
typedef struct {
2427
/** Key that uniquely identifies the memory mapped file. In practice, we
2528
* take the numerical value of the file descriptor in the object store. */
@@ -311,7 +314,8 @@ plasma_connection *plasma_connect(const char *store_socket_name,
311314
*/
312315
int fd = -1;
313316
int connected_successfully = 0;
314-
for (int num_attempts = 0; num_attempts < 50; ++num_attempts) {
317+
for (int num_attempts = 0; num_attempts < NUM_CONNECT_ATTEMPTS;
318+
++num_attempts) {
315319
fd = connect_ipc_sock(store_socket_name);
316320
if (fd >= 0) {
317321
connected_successfully = 1;
@@ -330,6 +334,10 @@ plasma_connection *plasma_connect(const char *store_socket_name,
330334
result->store_conn = fd;
331335
if (manager_addr != NULL) {
332336
result->manager_conn = plasma_manager_connect(manager_addr, manager_port);
337+
if (result->manager_conn < 0) {
338+
LOG_ERR("Could not connect to Plasma manager %s:%d", manager_addr,
339+
manager_port);
340+
}
333341
} else {
334342
result->manager_conn = -1;
335343
}
@@ -348,18 +356,17 @@ void plasma_disconnect(plasma_connection *conn) {
348356

349357
#define h_addr h_addr_list[0]
350358

351-
/* TODO(swang): Return the error to the caller. */
352-
int plasma_manager_connect(const char *ip_addr, int port) {
359+
int plasma_manager_try_connect(const char *ip_addr, int port) {
353360
int fd = socket(PF_INET, SOCK_STREAM, 0);
354361
if (fd < 0) {
355362
LOG_ERR("could not create socket");
356-
exit(-1);
363+
return -1;
357364
}
358365

359366
struct hostent *manager = gethostbyname(ip_addr); /* TODO(pcm): cache this */
360367
if (!manager) {
361368
LOG_ERR("plasma manager %s not found", ip_addr);
362-
exit(-1);
369+
return -1;
363370
}
364371

365372
struct sockaddr_in addr;
@@ -370,10 +377,26 @@ int plasma_manager_connect(const char *ip_addr, int port) {
370377
int r = connect(fd, (struct sockaddr *) &addr, sizeof(addr));
371378
if (r < 0) {
372379
LOG_ERR(
373-
"could not establish connection to manager with id %s:%d (probably ran "
380+
"could not establish connection to manager with id %s:%d (may have run "
374381
"out of ports)",
375382
&ip_addr[0], port);
376-
exit(-1);
383+
return -1;
384+
}
385+
return fd;
386+
}
387+
388+
int plasma_manager_connect(const char *ip_addr, int port) {
389+
/* Try to connect to the Plasma manager. If unsuccessful, retry several times.
390+
*/
391+
int fd = -1;
392+
for (int num_attempts = 0; num_attempts < NUM_CONNECT_ATTEMPTS;
393+
++num_attempts) {
394+
fd = plasma_manager_try_connect(ip_addr, port);
395+
if (fd >= 0) {
396+
break;
397+
}
398+
/* Sleep for 100 milliseconds. */
399+
usleep(100000);
377400
}
378401
return fd;
379402
}
@@ -432,3 +455,7 @@ void plasma_fetch(plasma_connection *conn,
432455
"Received unexpected object ID from manager during fetch.");
433456
}
434457
}
458+
459+
int get_manager_fd(plasma_connection *conn) {
460+
return conn->manager_conn;
461+
}

src/plasma/plasma_client.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ plasma_connection *plasma_connect(const char *store_socket_name,
6262
void plasma_disconnect(plasma_connection *conn);
6363

6464
/**
65-
* Connect to a possibly remote Plasma Manager.
65+
* Try to connect to a possibly remote Plasma Manager.
6666
*
6767
* @param addr The IP address of the Plasma Manager to connect to.
6868
* @param port The port of the Plasma Manager to connect to.
69-
* @return The file descriptor to use to send messages to the Plasma Manager.
69+
* @return The file descriptor to use to send messages to the
70+
* Plasma Manager. If connection was unsuccessful, this
71+
* value is -1.
7072
*/
7173
int plasma_manager_connect(const char *addr, int port);
7274

@@ -195,4 +197,13 @@ void plasma_fetch(plasma_connection *conn,
195197
*/
196198
int plasma_subscribe(plasma_connection *conn);
197199

200+
/**
201+
* Get the file descriptor for the socket connection to the plasma manager.
202+
*
203+
* @param conn The plasma connection.
204+
* @return The file descriptor for the manager connection. If there is no
205+
* connection to the manager, this is -1.
206+
*/
207+
int get_manager_fd(plasma_connection *conn);
208+
198209
#endif

0 commit comments

Comments
 (0)