2020-06-04 12:57:48 +03:00
|
|
|
#include "messenger.h"
|
2019-12-28 01:25:55 +03:00
|
|
|
|
2020-06-04 12:57:48 +03:00
|
|
|
void osd_messenger_t::read_requests()
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-03-01 17:18:46 +03:00
|
|
|
while (read_ready_clients.size() > 0)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-03-01 17:18:46 +03:00
|
|
|
int peer_fd = read_ready_clients[0];
|
2019-12-28 01:25:55 +03:00
|
|
|
auto & cl = clients[peer_fd];
|
2020-03-02 02:58:00 +03:00
|
|
|
if (!cl.read_op || cl.read_remaining < receive_buffer_size)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-03-02 02:58:00 +03:00
|
|
|
cl.read_iov.iov_base = cl.in_buf;
|
|
|
|
cl.read_iov.iov_len = receive_buffer_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cl.read_iov.iov_base = cl.read_buf;
|
|
|
|
cl.read_iov.iov_len = cl.read_remaining;
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
|
|
|
cl.read_msg.msg_iov = &cl.read_iov;
|
|
|
|
cl.read_msg.msg_iovlen = 1;
|
2020-03-01 17:18:46 +03:00
|
|
|
read_ready_clients.erase(read_ready_clients.begin(), read_ready_clients.begin() + 1);
|
|
|
|
int result = recvmsg(peer_fd, &cl.read_msg, 0);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
result = -errno;
|
|
|
|
}
|
|
|
|
handle_read(result, peer_fd);
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:57:48 +03:00
|
|
|
bool osd_messenger_t::handle_read(int result, int peer_fd)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
|
|
|
auto cl_it = clients.find(peer_fd);
|
|
|
|
if (cl_it != clients.end())
|
|
|
|
{
|
|
|
|
auto & cl = cl_it->second;
|
2020-05-31 15:03:27 +03:00
|
|
|
if (result < 0 && result != -EAGAIN)
|
2020-03-04 18:03:16 +03:00
|
|
|
{
|
|
|
|
// this is a client socket, so don't panic. just disconnect it
|
2020-05-31 15:03:27 +03:00
|
|
|
printf("Client %d socket read error: %d (%s). Disconnecting client\n", peer_fd, -result, strerror(-result));
|
2020-03-04 18:03:16 +03:00
|
|
|
stop_client(peer_fd);
|
2020-05-31 15:03:27 +03:00
|
|
|
return false;
|
2020-03-04 18:03:16 +03:00
|
|
|
}
|
2020-05-31 15:03:27 +03:00
|
|
|
if (result == -EAGAIN || result < cl.read_iov.iov_len)
|
2020-02-29 01:46:12 +03:00
|
|
|
{
|
|
|
|
cl.read_ready--;
|
|
|
|
if (cl.read_ready > 0)
|
|
|
|
read_ready_clients.push_back(peer_fd);
|
|
|
|
}
|
2020-03-04 18:03:16 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
read_ready_clients.push_back(peer_fd);
|
|
|
|
}
|
2020-05-31 15:03:27 +03:00
|
|
|
if (result > 0)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-03-02 02:58:00 +03:00
|
|
|
if (cl.read_iov.iov_base == cl.in_buf)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-03-02 02:58:00 +03:00
|
|
|
// Compose operation(s) from the buffer
|
2020-05-31 15:03:27 +03:00
|
|
|
int remain = result;
|
2020-03-02 02:58:00 +03:00
|
|
|
void *curbuf = cl.in_buf;
|
|
|
|
while (remain > 0)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-03-02 02:58:00 +03:00
|
|
|
if (!cl.read_op)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-03-02 02:58:00 +03:00
|
|
|
cl.read_op = new osd_op_t;
|
|
|
|
cl.read_op->peer_fd = peer_fd;
|
|
|
|
cl.read_op->op_type = OSD_OP_IN;
|
|
|
|
cl.read_buf = cl.read_op->req.buf;
|
|
|
|
cl.read_remaining = OSD_PACKET_SIZE;
|
|
|
|
cl.read_state = CL_READ_HDR;
|
|
|
|
}
|
|
|
|
if (cl.read_remaining > remain)
|
|
|
|
{
|
|
|
|
memcpy(cl.read_buf, curbuf, remain);
|
|
|
|
cl.read_remaining -= remain;
|
|
|
|
cl.read_buf += remain;
|
|
|
|
remain = 0;
|
|
|
|
if (cl.read_remaining <= 0)
|
|
|
|
handle_finished_read(cl);
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-02 02:58:00 +03:00
|
|
|
memcpy(cl.read_buf, curbuf, cl.read_remaining);
|
|
|
|
curbuf += cl.read_remaining;
|
|
|
|
remain -= cl.read_remaining;
|
|
|
|
cl.read_remaining = 0;
|
|
|
|
cl.read_buf = NULL;
|
|
|
|
handle_finished_read(cl);
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
|
|
|
}
|
2020-03-02 02:58:00 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Long data
|
2020-05-31 15:03:27 +03:00
|
|
|
cl.read_remaining -= result;
|
|
|
|
cl.read_buf += result;
|
2020-03-02 02:58:00 +03:00
|
|
|
if (cl.read_remaining <= 0)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-03-02 02:58:00 +03:00
|
|
|
handle_finished_read(cl);
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 15:03:27 +03:00
|
|
|
if (result >= cl.read_iov.iov_len)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 15:03:27 +03:00
|
|
|
return false;
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 12:57:48 +03:00
|
|
|
void osd_messenger_t::handle_finished_read(osd_client_t & cl)
|
2020-03-02 02:58:00 +03:00
|
|
|
{
|
|
|
|
if (cl.read_state == CL_READ_HDR)
|
|
|
|
{
|
|
|
|
if (cl.read_op->req.hdr.magic == SECONDARY_OSD_REPLY_MAGIC)
|
|
|
|
handle_reply_hdr(&cl);
|
|
|
|
else
|
|
|
|
handle_op_hdr(&cl);
|
|
|
|
}
|
|
|
|
else if (cl.read_state == CL_READ_DATA)
|
|
|
|
{
|
|
|
|
// Operation is ready
|
2020-06-01 01:53:32 +03:00
|
|
|
cl.received_ops.push_back(cl.read_op);
|
2020-03-02 02:58:00 +03:00
|
|
|
exec_op(cl.read_op);
|
|
|
|
cl.read_op = NULL;
|
|
|
|
cl.read_state = 0;
|
|
|
|
}
|
|
|
|
else if (cl.read_state == CL_READ_REPLY_DATA)
|
|
|
|
{
|
|
|
|
// Reply is ready
|
|
|
|
auto req_it = cl.sent_ops.find(cl.read_reply_id);
|
|
|
|
osd_op_t *request = req_it->second;
|
|
|
|
cl.sent_ops.erase(req_it);
|
|
|
|
cl.read_reply_id = 0;
|
2020-03-28 19:09:20 +03:00
|
|
|
delete cl.read_op;
|
2020-03-02 02:58:00 +03:00
|
|
|
cl.read_op = NULL;
|
|
|
|
cl.read_state = 0;
|
|
|
|
// Measure subop latency
|
|
|
|
timespec tv_end;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &tv_end);
|
2020-05-26 21:23:01 +03:00
|
|
|
stats.subop_stat_count[request->req.hdr.opcode]++;
|
|
|
|
if (!stats.subop_stat_count[request->req.hdr.opcode])
|
2020-05-15 00:57:49 +03:00
|
|
|
{
|
2020-05-26 21:23:01 +03:00
|
|
|
stats.subop_stat_count[request->req.hdr.opcode]++;
|
|
|
|
stats.subop_stat_sum[request->req.hdr.opcode] = 0;
|
2020-05-15 00:57:49 +03:00
|
|
|
}
|
2020-05-26 21:23:01 +03:00
|
|
|
stats.subop_stat_sum[request->req.hdr.opcode] += (
|
2020-03-02 02:58:00 +03:00
|
|
|
(tv_end.tv_sec - request->tv_begin.tv_sec)*1000000 +
|
|
|
|
(tv_end.tv_nsec - request->tv_begin.tv_nsec)/1000
|
|
|
|
);
|
|
|
|
request->callback(request);
|
|
|
|
}
|
2020-03-28 19:09:20 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
}
|
2020-03-02 02:58:00 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 12:57:48 +03:00
|
|
|
void osd_messenger_t::handle_op_hdr(osd_client_t *cl)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
|
|
|
osd_op_t *cur_op = cl->read_op;
|
2020-02-25 01:20:45 +03:00
|
|
|
if (cur_op->req.hdr.opcode == OSD_OP_SECONDARY_READ)
|
|
|
|
{
|
|
|
|
if (cur_op->req.sec_rw.len > 0)
|
2020-04-14 19:19:56 +03:00
|
|
|
cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.sec_rw.len);
|
2020-02-25 01:20:45 +03:00
|
|
|
cl->read_remaining = 0;
|
|
|
|
}
|
2020-02-25 22:52:03 +03:00
|
|
|
else if (cur_op->req.hdr.opcode == OSD_OP_SECONDARY_WRITE)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-02-25 01:20:45 +03:00
|
|
|
if (cur_op->req.sec_rw.len > 0)
|
2020-04-14 19:19:56 +03:00
|
|
|
cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.sec_rw.len);
|
2020-02-25 01:20:45 +03:00
|
|
|
cl->read_remaining = cur_op->req.sec_rw.len;
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
2020-02-25 22:52:03 +03:00
|
|
|
else if (cur_op->req.hdr.opcode == OSD_OP_SECONDARY_STABILIZE ||
|
|
|
|
cur_op->req.hdr.opcode == OSD_OP_SECONDARY_ROLLBACK)
|
|
|
|
{
|
|
|
|
if (cur_op->req.sec_stab.len > 0)
|
2020-04-14 19:19:56 +03:00
|
|
|
cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.sec_stab.len);
|
2020-02-25 22:52:03 +03:00
|
|
|
cl->read_remaining = cur_op->req.sec_stab.len;
|
|
|
|
}
|
2020-02-25 01:20:45 +03:00
|
|
|
else if (cur_op->req.hdr.opcode == OSD_OP_READ)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-02-25 01:20:45 +03:00
|
|
|
if (cur_op->req.rw.len > 0)
|
2020-04-14 19:19:56 +03:00
|
|
|
cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.rw.len);
|
2020-02-25 01:20:45 +03:00
|
|
|
cl->read_remaining = 0;
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
2020-02-25 01:20:45 +03:00
|
|
|
else if (cur_op->req.hdr.opcode == OSD_OP_WRITE)
|
|
|
|
{
|
|
|
|
if (cur_op->req.rw.len > 0)
|
2020-04-14 19:19:56 +03:00
|
|
|
cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.rw.len);
|
2020-02-25 01:20:45 +03:00
|
|
|
cl->read_remaining = cur_op->req.rw.len;
|
|
|
|
}
|
|
|
|
if (cl->read_remaining > 0)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
|
|
|
// Read data
|
|
|
|
cl->read_buf = cur_op->buf;
|
|
|
|
cl->read_state = CL_READ_DATA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Operation is ready
|
|
|
|
cl->read_op = NULL;
|
|
|
|
cl->read_state = 0;
|
2020-06-01 01:53:32 +03:00
|
|
|
cl->received_ops.push_back(cur_op);
|
2019-12-28 01:25:55 +03:00
|
|
|
exec_op(cur_op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:57:48 +03:00
|
|
|
void osd_messenger_t::handle_reply_hdr(osd_client_t *cl)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
|
|
|
osd_op_t *cur_op = cl->read_op;
|
2020-02-23 19:03:06 +03:00
|
|
|
auto req_it = cl->sent_ops.find(cur_op->req.hdr.id);
|
2019-12-28 01:25:55 +03:00
|
|
|
if (req_it == cl->sent_ops.end())
|
|
|
|
{
|
|
|
|
// Command out of sync. Drop connection
|
2020-02-25 22:52:03 +03:00
|
|
|
printf("Client %d command out of sync: id %lu\n", cl->peer_fd, cur_op->req.hdr.id);
|
2019-12-28 01:25:55 +03:00
|
|
|
stop_client(cl->peer_fd);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-23 19:03:06 +03:00
|
|
|
osd_op_t *op = req_it->second;
|
|
|
|
memcpy(op->reply.buf, cur_op->req.buf, OSD_PACKET_SIZE);
|
2020-06-06 01:39:58 +03:00
|
|
|
if ((op->reply.hdr.opcode == OSD_OP_SECONDARY_READ || op->reply.hdr.opcode == OSD_OP_READ) &&
|
2020-02-23 19:03:06 +03:00
|
|
|
op->reply.hdr.retval > 0)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-02-24 01:01:34 +03:00
|
|
|
// Read data. In this case we assume that the buffer is preallocated by the caller (!)
|
|
|
|
assert(op->buf);
|
2019-12-28 01:25:55 +03:00
|
|
|
cl->read_state = CL_READ_REPLY_DATA;
|
2020-02-23 19:03:06 +03:00
|
|
|
cl->read_reply_id = op->req.hdr.id;
|
|
|
|
cl->read_buf = op->buf;
|
|
|
|
cl->read_remaining = op->reply.hdr.retval;
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
2020-06-06 01:39:58 +03:00
|
|
|
else if (op->reply.hdr.opcode == OSD_OP_SECONDARY_LIST && op->reply.hdr.retval > 0)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-04-14 19:19:56 +03:00
|
|
|
op->buf = memalign(MEM_ALIGNMENT, sizeof(obj_ver_id) * op->reply.hdr.retval);
|
2019-12-28 01:25:55 +03:00
|
|
|
cl->read_state = CL_READ_REPLY_DATA;
|
2020-02-23 19:03:06 +03:00
|
|
|
cl->read_reply_id = op->req.hdr.id;
|
|
|
|
cl->read_buf = op->buf;
|
|
|
|
cl->read_remaining = sizeof(obj_ver_id) * op->reply.hdr.retval;
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
2020-06-06 01:39:58 +03:00
|
|
|
else if (op->reply.hdr.opcode == OSD_OP_SHOW_CONFIG && op->reply.hdr.retval > 0)
|
2020-04-21 00:08:54 +03:00
|
|
|
{
|
|
|
|
op->buf = malloc(op->reply.hdr.retval);
|
|
|
|
cl->read_state = CL_READ_REPLY_DATA;
|
|
|
|
cl->read_reply_id = op->req.hdr.id;
|
|
|
|
cl->read_buf = op->buf;
|
|
|
|
cl->read_remaining = op->reply.hdr.retval;
|
|
|
|
}
|
2019-12-28 01:25:55 +03:00
|
|
|
else
|
|
|
|
{
|
2020-03-28 19:09:20 +03:00
|
|
|
delete cl->read_op;
|
2019-12-28 01:25:55 +03:00
|
|
|
cl->read_state = 0;
|
2020-03-02 02:58:00 +03:00
|
|
|
cl->read_op = NULL;
|
2019-12-28 01:25:55 +03:00
|
|
|
cl->sent_ops.erase(req_it);
|
2020-02-28 12:25:54 +03:00
|
|
|
// Measure subop latency
|
2020-03-03 00:54:42 +03:00
|
|
|
timespec tv_end;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &tv_end);
|
2020-05-26 21:23:01 +03:00
|
|
|
stats.subop_stat_count[op->req.hdr.opcode]++;
|
|
|
|
if (!stats.subop_stat_count[op->req.hdr.opcode])
|
2020-05-15 00:57:49 +03:00
|
|
|
{
|
2020-05-26 21:23:01 +03:00
|
|
|
stats.subop_stat_count[op->req.hdr.opcode]++;
|
|
|
|
stats.subop_stat_sum[op->req.hdr.opcode] = 0;
|
2020-05-15 00:57:49 +03:00
|
|
|
}
|
2020-05-26 21:23:01 +03:00
|
|
|
stats.subop_stat_sum[op->req.hdr.opcode] += (
|
2020-02-28 12:25:54 +03:00
|
|
|
(tv_end.tv_sec - op->tv_begin.tv_sec)*1000000 +
|
2020-03-03 00:54:42 +03:00
|
|
|
(tv_end.tv_nsec - op->tv_begin.tv_nsec)/1000
|
2020-02-28 12:25:54 +03:00
|
|
|
);
|
2020-03-31 17:50:50 +03:00
|
|
|
// Copy lambda to be unaffected by `delete op`
|
|
|
|
std::function<void(osd_op_t*)>(op->callback)(op);
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
|
|
|
}
|