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+
2326typedef 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+ }
0 commit comments