remove C struct duplication and python plasma manager

This commit is contained in:
Philipp Moritz
2016-09-05 15:37:17 -07:00
parent a0490846a2
commit ad1a8454d5
7 changed files with 157 additions and 151 deletions
+4 -3
View File
@@ -16,6 +16,8 @@
int main(int argc, char *argv[]) {
int conn = -1;
int64_t size;
void *data;
int c;
plasma_id id = {{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255}};
@@ -26,14 +28,14 @@ int main(int argc, char *argv[]) {
break;
case 'c':
assert(conn != -1);
plasma_create(conn, id, 100);
plasma_create(conn, id, 100, &data);
break;
case 'f':
assert(conn != -1);
plasma_seal(conn, id);
break;
case 'g':
plasma_get(conn, id);
plasma_get(conn, id, &size, &data);
break;
default:
abort();
@@ -42,4 +44,3 @@ int main(int argc, char *argv[]) {
assert(conn != -1);
close(conn);
}
+9 -4
View File
@@ -65,11 +65,16 @@ typedef struct {
int writable;
} plasma_buffer;
// Connect to the local plasma store UNIX domain socket
int plasma_store_connect(const char* socket_name);
plasma_buffer plasma_create(int conn, plasma_id object_id, int64_t size);
plasma_buffer plasma_get(int conn, plasma_id object_id);
void plasma_seal(int fd, plasma_id object_id);
void plasma_send(int fd, plasma_request *req);
// Connect to a possibly remote plasma manager
int plasma_manager_connect(const char* addr, int port);
void plasma_create(int store, plasma_id object_id, int64_t size, void **data);
void plasma_get(int store, plasma_id object_id, int64_t *size, void **data);
void plasma_seal(int store, plasma_id object_id);
void plasma_send(int conn, plasma_request *req);
#endif
+45 -10
View File
@@ -23,7 +23,7 @@ void plasma_send(int fd, plasma_request *req) {
}
}
plasma_buffer plasma_create(int conn, plasma_id object_id, int64_t size) {
void plasma_create(int conn, plasma_id object_id, int64_t size, void **data) {
LOG_INFO("called plasma_create on conn %d with size %" PRId64, conn, size);
plasma_request req = { .type = PLASMA_CREATE, .object_id = object_id, .size = size };
plasma_send(conn, &req);
@@ -31,16 +31,14 @@ plasma_buffer plasma_create(int conn, plasma_id object_id, int64_t size) {
int fd = recv_fd(conn, (char*)&reply, sizeof(plasma_reply));
assert(reply.type == PLASMA_OBJECT);
assert(reply.size == size);
void *data = mmap(NULL, reply.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
*data = mmap(NULL, reply.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (*data == MAP_FAILED) {
LOG_ERR("mmap failed");
exit(-1);
}
plasma_buffer buffer = { object_id, data, size, 1 };
return buffer;
}
plasma_buffer plasma_get(int conn, plasma_id object_id) {
void plasma_get(int conn, plasma_id object_id, int64_t *size, void **data) {
plasma_request req = { .type = PLASMA_GET, .object_id = object_id };
plasma_send(conn, &req);
plasma_reply reply;
@@ -52,13 +50,12 @@ plasma_buffer plasma_get(int conn, plasma_id object_id) {
fd = new_fd;
}
assert(reply.type == PLASMA_OBJECT);
void *data = mmap(NULL, reply.size, PROT_READ, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
*data = mmap(NULL, reply.size, PROT_READ, MAP_SHARED, fd, 0);
if (*data == MAP_FAILED) {
LOG_ERR("mmap failed");
exit(-1);
}
plasma_buffer buffer = { object_id, data, reply.size, 0 };
return buffer;
*size = reply.size;
}
void plasma_seal(int fd, plasma_id object_id) {
@@ -94,3 +91,41 @@ int plasma_store_connect(const char* socket_name) {
}
return fd;
}
#define h_addr h_addr_list[0]
int plasma_manager_connect(const char* ip_addr, int port) {
int fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
LOG_ERR("could not create socket");
exit(-1);
}
struct hostent *manager = gethostbyname(ip_addr); // TODO(pcm): cache this
if (!manager) {
LOG_ERR("plasma manager %s not found", ip_addr);
exit(-1);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
bcopy(manager->h_addr, &addr.sin_addr.s_addr, manager->h_length);
addr.sin_port = htons(port);
int r = connect(fd, (struct sockaddr*) &addr, sizeof(addr));
if (r < 0) {
LOG_ERR("could not establish connection to manager with id %s:%d", &ip_addr[0], port);
exit(-1);
}
return fd;
}
void plasma_transfer(int manager, const char* addr, int port, plasma_id object_id) {
plasma_request req = {.type = PLASMA_TRANSFER, .object_id = object_id, .port = port};
char* end = NULL;
for (int i = 0; i < 4; ++i) {
req.addr[i] = strtol(end ? end : addr, &end, 10);
end += 1; // skip the '.'
}
plasma_send(manager, &req);
}
+12 -68
View File
@@ -22,43 +22,7 @@
#include <netdb.h>
#include "plasma.h"
#define MAX_CONNECTIONS 2048
#define MAX_NUM_MANAGERS 1024
enum conn_type {
// Connection to send commands to the manager.
CONN_CONTROL,
// Connection to send data to another manager.
CONN_WRITE_DATA,
// Connection to receive data from another manager.
CONN_READ_DATA
};
typedef struct {
// Unique identifier for the connection.
int id;
// Of type conn_type.
int type;
// Socket of the plasma store that is accessed for reading or writing data for
// this connection.
int store_conn;
// Buffer this connection is reading from or writing to.
plasma_buffer buf;
// Current position in the buffer.
int64_t cursor;
} conn_state;
typedef struct {
// Name of the socket connecting to local plasma store.
const char* store_socket_name;
// Number of connections.
int num_conn;
// For the "poll" system call.
struct pollfd waiting[MAX_CONNECTIONS];
// Status of connections (both control and data).
conn_state conn[MAX_CONNECTIONS];
} plasma_manager_state;
#include "plasma_manager.h"
void init_manager_state(plasma_manager_state *s, const char* store_socket_name) {
memset(&s->waiting, 0, sizeof(s->waiting));
@@ -67,22 +31,17 @@ void init_manager_state(plasma_manager_state *s, const char* store_socket_name)
s->store_socket_name = store_socket_name;
}
#define h_addr h_addr_list[0]
// Add connection for sending commands or data to another plasma manager
// (returns the connection id).
// (returns the connection index).
int add_conn(plasma_manager_state* s, int type, int fd, int events, plasma_buffer* buf) {
static int conn_id = 0;
s->waiting[s->num_conn].fd = fd;
s->waiting[s->num_conn].events = events;
s->conn[s->num_conn].id = conn_id;
s->conn[s->num_conn].type = type;
if (buf) {
s->conn[s->num_conn].buf = *buf;
}
s->conn[s->num_conn].cursor = 0;
s->num_conn += 1;
return conn_id++;
return s->num_conn++;
}
// Remove connection with index i by swapping it with the last element.
@@ -100,33 +59,15 @@ void remove_conn(plasma_manager_state* s, int i) {
// the data header to the other object manager.
void initiate_transfer(plasma_manager_state* state, plasma_request* req) {
int c = plasma_store_connect(state->store_socket_name);
plasma_buffer buf = plasma_get(c, req->object_id);
int fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
LOG_ERR("could not create socket");
exit(-1);
}
plasma_buffer buf = { .object_id = req->object_id, .writable = 0 };
plasma_get(c, req->object_id, &buf.size, &buf.data);
char ip_addr[16];
snprintf(ip_addr, 32, "%d.%d.%d.%d",
req->addr[0], req->addr[1],
req->addr[2], req->addr[3]);
struct hostent *manager = gethostbyname(ip_addr); // TODO(pcm): cache this
if (!manager) {
LOG_ERR("plasma manager %s not found", ip_addr);
exit(-1);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
bcopy(manager->h_addr, &addr.sin_addr.s_addr, manager->h_length);
addr.sin_port = htons(req->port);
int r = connect(fd, (struct sockaddr*) &addr, sizeof(addr));
if (r < 0) {
LOG_ERR("could not establish connection to manager with id %s:%d", &ip_addr[0], req->port);
exit(-1);
}
int fd = plasma_manager_connect(&ip_addr[0], req->port);
add_conn(state, CONN_WRITE_DATA, fd, POLLOUT, &buf);
@@ -139,7 +80,10 @@ void setup_data_connection(int conn_idx, plasma_manager_state* state, plasma_req
int store_conn = plasma_store_connect(state->store_socket_name);
state->conn[conn_idx].type = CONN_READ_DATA;
state->conn[conn_idx].store_conn = store_conn;
state->conn[conn_idx].buf = plasma_create(store_conn, req->object_id, req->size);
state->conn[conn_idx].buf.object_id = req->object_id;
state->conn[conn_idx].buf.size = req->size;
state->conn[conn_idx].buf.writable = 1;
plasma_create(store_conn, req->object_id, req->size, &state->conn[conn_idx].buf.data);
state->conn[conn_idx].cursor = 0;
}
@@ -170,7 +114,7 @@ void read_from_socket(plasma_manager_state* state, int i, plasma_request* req) {
if (r == 1) {
LOG_ERR("read error");
} else if (r == 0) {
LOG_INFO("connection with id %d disconnected", state->conn[i].id);
LOG_INFO("connection with index %d disconnected", i);
remove_conn(state, i);
} else {
process_command(i, state, req);
+42
View File
@@ -0,0 +1,42 @@
#ifndef PLASMA_MANAGER_H
#define PLASMA_MANAGER_H
#include <poll.h>
#define MAX_CONNECTIONS 2048
enum conn_type {
// Connection to send commands to the manager.
CONN_CONTROL,
// Connection to send data to another manager.
CONN_WRITE_DATA,
// Connection to receive data from another manager.
CONN_READ_DATA
};
typedef struct {
// Of type conn_type.
int type;
// Socket of the plasma store that is accessed for reading or writing data for
// this connection.
int store_conn;
// Buffer this connection is reading from or writing to.
plasma_buffer buf;
// Current position in the buffer.
int64_t cursor;
} conn_state;
typedef struct {
// ID of this manager
int64_t manager_id;
// Name of the socket connecting to local plasma store.
const char* store_socket_name;
// Number of connections.
int num_conn;
// For the "poll" system call.
struct pollfd waiting[MAX_CONNECTIONS];
// Status of connections (both control and data).
conn_state conn[MAX_CONNECTIONS];
} plasma_manager_state;
#endif