2020-09-17 23:02:40 +03:00
|
|
|
// Copyright (c) Vitaliy Filippov, 2019+
|
|
|
|
// License: VNPL-1.0 or GNU GPL-2.0+ (see README.md for details)
|
|
|
|
|
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::outbox_push(osd_op_t *cur_op)
|
2020-01-22 02:36:14 +03:00
|
|
|
{
|
2020-02-09 18:22:29 +03:00
|
|
|
assert(cur_op->peer_fd);
|
2020-10-17 13:52:21 +03:00
|
|
|
osd_client_t *cl = clients.at(cur_op->peer_fd);
|
2020-02-28 12:25:54 +03:00
|
|
|
if (cur_op->op_type == OSD_OP_OUT)
|
|
|
|
{
|
2020-03-03 00:54:42 +03:00
|
|
|
clock_gettime(CLOCK_REALTIME, &cur_op->tv_begin);
|
2020-02-28 12:25:54 +03:00
|
|
|
}
|
2020-06-01 01:53:32 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check that operation actually belongs to this client
|
|
|
|
bool found = false;
|
2020-10-17 13:52:21 +03:00
|
|
|
for (auto it = cl->received_ops.begin(); it != cl->received_ops.end(); it++)
|
2020-06-01 01:53:32 +03:00
|
|
|
{
|
|
|
|
if (*it == cur_op)
|
|
|
|
{
|
|
|
|
found = true;
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->received_ops.erase(it, it+1);
|
2020-06-01 01:53:32 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
delete cur_op;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->outbox.push_back(cur_op);
|
2020-06-26 02:18:58 +03:00
|
|
|
if (!ringloop)
|
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
while (cl->write_op || cl->outbox.size())
|
2020-06-26 02:18:58 +03:00
|
|
|
{
|
|
|
|
try_send(cl);
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
else if (cl->write_op || cl->outbox.size() > 1 || !try_send(cl))
|
2020-01-22 02:36:14 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
if (cl->write_state == 0)
|
2020-03-02 00:20:28 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->write_state = CL_WRITE_READY;
|
2020-03-02 00:20:28 +03:00
|
|
|
write_ready_clients.push_back(cur_op->peer_fd);
|
|
|
|
}
|
|
|
|
ringloop->wakeup();
|
2020-01-22 02:36:14 +03:00
|
|
|
}
|
2020-03-02 00:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-10-17 13:52:21 +03:00
|
|
|
bool osd_messenger_t::try_send(osd_client_t *cl)
|
2020-03-02 00:20:28 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
int peer_fd = cl->peer_fd;
|
|
|
|
if (!cl->write_op)
|
2020-03-02 00:20:28 +03:00
|
|
|
{
|
|
|
|
// pick next command
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->write_op = cl->outbox.front();
|
|
|
|
cl->outbox.pop_front();
|
|
|
|
cl->write_state = CL_WRITE_REPLY;
|
|
|
|
if (cl->write_op->op_type == OSD_OP_IN)
|
2020-03-02 00:20:28 +03:00
|
|
|
{
|
|
|
|
// Measure execution latency
|
2020-05-22 15:50:04 +03:00
|
|
|
timespec tv_end;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &tv_end);
|
2020-10-17 13:52:21 +03:00
|
|
|
stats.op_stat_count[cl->write_op->req.hdr.opcode]++;
|
|
|
|
if (!stats.op_stat_count[cl->write_op->req.hdr.opcode])
|
2020-05-15 00:57:49 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
stats.op_stat_count[cl->write_op->req.hdr.opcode]++;
|
|
|
|
stats.op_stat_sum[cl->write_op->req.hdr.opcode] = 0;
|
|
|
|
stats.op_stat_bytes[cl->write_op->req.hdr.opcode] = 0;
|
2020-05-15 00:57:49 +03:00
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
stats.op_stat_sum[cl->write_op->req.hdr.opcode] += (
|
|
|
|
(tv_end.tv_sec - cl->write_op->tv_begin.tv_sec)*1000000 +
|
|
|
|
(tv_end.tv_nsec - cl->write_op->tv_begin.tv_nsec)/1000
|
2020-03-02 00:20:28 +03:00
|
|
|
);
|
2020-10-17 13:52:21 +03:00
|
|
|
if (cl->write_op->req.hdr.opcode == OSD_OP_READ ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_WRITE)
|
2020-05-11 02:58:13 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
stats.op_stat_bytes[cl->write_op->req.hdr.opcode] += cl->write_op->req.rw.len;
|
2020-05-11 02:58:13 +03:00
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
else if (cl->write_op->req.hdr.opcode == OSD_OP_SEC_READ ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_SEC_WRITE ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_SEC_WRITE_STABLE)
|
2020-05-11 02:58:13 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
stats.op_stat_bytes[cl->write_op->req.hdr.opcode] += cl->write_op->req.sec_rw.len;
|
2020-05-11 02:58:13 +03:00
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->send_list.push_back(cl->write_op->reply.buf, OSD_PACKET_SIZE);
|
|
|
|
if (cl->write_op->req.hdr.opcode == OSD_OP_READ ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_SEC_READ ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_SEC_LIST ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_SHOW_CONFIG)
|
2020-06-18 02:07:20 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->send_list.append(cl->write_op->iov);
|
2020-06-18 02:07:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->send_list.push_back(cl->write_op->req.buf, OSD_PACKET_SIZE);
|
|
|
|
if (cl->write_op->req.hdr.opcode == OSD_OP_WRITE ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_SEC_WRITE ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_SEC_WRITE_STABLE ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_SEC_STABILIZE ||
|
|
|
|
cl->write_op->req.hdr.opcode == OSD_OP_SEC_ROLLBACK)
|
2020-06-18 02:07:20 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->send_list.append(cl->write_op->iov);
|
2020-06-18 02:07:20 +03:00
|
|
|
}
|
2020-03-02 00:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->write_msg.msg_iov = cl->send_list.get_iovec();
|
|
|
|
cl->write_msg.msg_iovlen = cl->send_list.get_size();
|
|
|
|
cl->refs++;
|
2020-06-22 23:44:32 +03:00
|
|
|
if (ringloop && !use_sync_send_recv)
|
|
|
|
{
|
|
|
|
io_uring_sqe* sqe = ringloop->get_sqe();
|
|
|
|
if (!sqe)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ring_data_t* data = ((ring_data_t*)sqe->user_data);
|
2020-10-17 13:52:21 +03:00
|
|
|
data->callback = [this, cl](ring_data_t *data) { handle_send(data->res, cl); };
|
|
|
|
my_uring_prep_sendmsg(sqe, peer_fd, &cl->write_msg, 0);
|
2020-06-22 23:44:32 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
int result = sendmsg(peer_fd, &cl->write_msg, MSG_NOSIGNAL);
|
2020-06-22 23:44:32 +03:00
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
result = -errno;
|
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
handle_send(result, cl);
|
2020-06-22 23:44:32 +03:00
|
|
|
}
|
2020-03-02 00:20:28 +03:00
|
|
|
return true;
|
2020-01-22 02:36:14 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 12:57:48 +03:00
|
|
|
void osd_messenger_t::send_replies()
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
|
|
|
for (int i = 0; i < write_ready_clients.size(); i++)
|
|
|
|
{
|
|
|
|
int peer_fd = write_ready_clients[i];
|
2020-03-02 00:20:28 +03:00
|
|
|
if (!try_send(clients[peer_fd]))
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
|
|
|
write_ready_clients.erase(write_ready_clients.begin(), write_ready_clients.begin() + i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write_ready_clients.clear();
|
|
|
|
}
|
|
|
|
|
2020-10-17 13:52:21 +03:00
|
|
|
void osd_messenger_t::handle_send(int result, osd_client_t *cl)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
cl->refs--;
|
|
|
|
if (cl->peer_state == PEER_STOPPED)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
if (!cl->refs)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
delete cl;
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result < 0 && result != -EAGAIN)
|
|
|
|
{
|
|
|
|
// this is a client socket, so don't panic. just disconnect it
|
|
|
|
printf("Client %d socket write error: %d (%s). Disconnecting client\n", cl->peer_fd, -result, strerror(-result));
|
|
|
|
stop_client(cl->peer_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result >= 0)
|
|
|
|
{
|
|
|
|
cl->send_list.eat(result);
|
|
|
|
if (cl->send_list.done >= cl->send_list.count)
|
2019-12-28 01:25:55 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
// Done
|
|
|
|
cl->send_list.reset();
|
|
|
|
if (cl->write_op->op_type == OSD_OP_IN)
|
2020-02-29 02:27:10 +03:00
|
|
|
{
|
2020-10-17 13:52:21 +03:00
|
|
|
delete cl->write_op;
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cl->sent_ops[cl->write_op->req.hdr.id] = cl->write_op;
|
|
|
|
}
|
|
|
|
cl->write_op = NULL;
|
|
|
|
cl->write_state = cl->outbox.size() > 0 ? CL_WRITE_READY : 0;
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
2020-10-17 13:52:21 +03:00
|
|
|
}
|
|
|
|
if (cl->write_state != 0)
|
|
|
|
{
|
|
|
|
write_ready_clients.push_back(cl->peer_fd);
|
2019-12-28 01:25:55 +03:00
|
|
|
}
|
|
|
|
}
|