Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions cpp/cdc/CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,46 +668,48 @@ struct CDCServer : Loop {
void _processCDCMessages() {
int startUpdateSize = _updateSize();
for (auto& msg: _channel.protocolMessages(CDC_REQ_PROTOCOL_VERSION)) {
// First, try to parse the header
CDCReqMsg cdcMsg;
try {
cdcMsg.unpack(msg.buf);
} catch (const BincodeException& err) {
LOG_ERROR(_env, "could not parse: %s", err.what());
RAISE_ALERT(_env, "could not parse request from %s, dropping it.", msg.clientAddr);
continue;
}
while (msg.buf.remaining()) {
// First, try to parse the header
CDCReqMsg cdcMsg;
try {
cdcMsg.unpack(msg.buf);
} catch (const BincodeException& err) {
LOG_ERROR(_env, "could not parse: %s", err.what());
RAISE_ALERT(_env, "could not parse request from %s, dropping it.", msg.clientAddr);
break;
}

LOG_DEBUG(_env, "received request id %s, kind %s", cdcMsg.id, cdcMsg.body.kind());
auto receivedAt = ternNow();
LOG_DEBUG(_env, "received request id %s, kind %s", cdcMsg.id, cdcMsg.body.kind());
auto receivedAt = ternNow();

if (unlikely(cdcMsg.body.kind() == CDCMessageKind::CDC_SNAPSHOT)) {
_processCDCSnapshotMessage(cdcMsg, msg);
continue;
}
if (unlikely(cdcMsg.body.kind() == CDCMessageKind::CDC_SNAPSHOT)) {
_processCDCSnapshotMessage(cdcMsg, msg);
continue;
}

// If we're already processing this request, drop it to try to not clog the queue
if (_inFlightCDCReqs.contains(InFlightCDCRequestKey(cdcMsg.id, msg.clientAddr))) {
LOG_DEBUG(_env, "dropping req id %s from %s since it's already being processed", cdcMsg.id, msg.clientAddr);
continue;
}
// If we're already processing this request, drop it to try to not clog the queue
if (_inFlightCDCReqs.contains(InFlightCDCRequestKey(cdcMsg.id, msg.clientAddr))) {
LOG_DEBUG(_env, "dropping req id %s from %s since it's already being processed", cdcMsg.id, msg.clientAddr);
continue;
}

if (unlikely(_shared.isLeader.load(std::memory_order_relaxed) == false)) {
LOG_DEBUG(_env, "dropping request since we're not the leader %s", cdcMsg);
continue;
}
if (unlikely(_shared.isLeader.load(std::memory_order_relaxed) == false)) {
LOG_DEBUG(_env, "dropping request since we're not the leader %s", cdcMsg);
continue;
}

auto& cdcReq = _cdcReqs.emplace_back(std::move(cdcMsg.body));
auto& cdcReq = _cdcReqs.emplace_back(std::move(cdcMsg.body));

_inFlightCDCReqs.insert(InFlightCDCRequestKey(cdcMsg.id, msg.clientAddr));
_inFlightCDCReqs.insert(InFlightCDCRequestKey(cdcMsg.id, msg.clientAddr));

LOG_DEBUG(_env, "CDC request %s successfully parsed, will process soon", cdcReq.kind());
_cdcReqsInfo.emplace_back(CDCReqInfo{
.reqId = cdcMsg.id,
.clientAddr = msg.clientAddr,
.receivedAt = receivedAt,
.sockIx = msg.socketIx,
});
LOG_DEBUG(_env, "CDC request %s successfully parsed, will process soon", cdcReq.kind());
_cdcReqsInfo.emplace_back(CDCReqInfo{
.reqId = cdcMsg.id,
.clientAddr = msg.clientAddr,
.receivedAt = receivedAt,
.sockIx = msg.socketIx,
});
}
}
}

Expand Down
20 changes: 10 additions & 10 deletions cpp/core/Protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ inline std::ostream& operator<<(std::ostream& out, const ShardCheckPointedResp&
return out << "checkPointIdx: " << resp.checkPointIdx << " resp: " << resp.resp;
}

using ShardReqMsg = ProtocolMessage<SHARD_REQ_PROTOCOL_VERSION, ShardReqContainer>;
using ShardRespMsg = ProtocolMessage<SHARD_RESP_PROTOCOL_VERSION, ShardRespContainer>;
using CdcToShardReqMsg = SignedProtocolMessage<CDC_TO_SHARD_REQ_PROTOCOL_VERSION, ShardReqContainer>;
using CdcToShardRespMsg = SignedProtocolMessage<CDC_TO_SHARD_RESP_PROTOCOL_VERSION, ShardCheckPointedResp>;
using CDCReqMsg = ProtocolMessage<CDC_REQ_PROTOCOL_VERSION, CDCReqContainer>;
using CDCRespMsg = ProtocolMessage<CDC_RESP_PROTOCOL_VERSION, CDCRespContainer>;
using LogReqMsg = SignedProtocolMessage<LOG_REQ_PROTOCOL_VERSION, LogReqContainer>;
using LogRespMsg = SignedProtocolMessage<LOG_RESP_PROTOCOL_VERSION, LogRespContainer>;
using ProxyShardReqMsg = SignedProtocolMessage<PROXY_SHARD_REQ_PROTOCOL_VERSION, ShardReqContainer>;
using ProxyShardRespMsg = SignedProtocolMessage<PROXY_SHARD_RESP_PROTOCOL_VERSION, ShardCheckPointedResp>;
using ShardReqMsg = ProtocolMessage<SHARD_REQ_PROTOCOL_VERSION, ShardReqContainer, false>;
using ShardRespMsg = ProtocolMessage<SHARD_RESP_PROTOCOL_VERSION, ShardRespContainer, false>;
using CdcToShardReqMsg = SignedProtocolMessage<CDC_TO_SHARD_REQ_PROTOCOL_VERSION, ShardReqContainer, false>;
using CdcToShardRespMsg = SignedProtocolMessage<CDC_TO_SHARD_RESP_PROTOCOL_VERSION, ShardCheckPointedResp, false>;
using CDCReqMsg = ProtocolMessage<CDC_REQ_PROTOCOL_VERSION, CDCReqContainer, false>;
using CDCRespMsg = ProtocolMessage<CDC_RESP_PROTOCOL_VERSION, CDCRespContainer, false>;
using LogReqMsg = SignedProtocolMessage<LOG_REQ_PROTOCOL_VERSION, LogReqContainer, false>;
using LogRespMsg = SignedProtocolMessage<LOG_RESP_PROTOCOL_VERSION, LogRespContainer, false>;
using ProxyShardReqMsg = SignedProtocolMessage<PROXY_SHARD_REQ_PROTOCOL_VERSION, ShardReqContainer, false>;
using ProxyShardRespMsg = SignedProtocolMessage<PROXY_SHARD_RESP_PROTOCOL_VERSION, ShardCheckPointedResp, false>;
90 changes: 46 additions & 44 deletions cpp/shard/Shard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,61 +472,63 @@ struct ShardServer : Loop {
return;
}

ShardReqMsg req;
try {
switch (protocol) {
case CDC_TO_SHARD_REQ_PROTOCOL_VERSION:
{
CdcToShardReqMsg signedReq;
signedReq.unpack(msg.buf, _expandedCDCKey);
req.id = signedReq.id;
req.body = std::move(signedReq.body);
}
break;
case SHARD_REQ_PROTOCOL_VERSION:
req.unpack(msg.buf);
if (isPrivilegedRequestKind((uint8_t)req.body.kind())) {
LOG_ERROR(_env, "Received unauthenticated request %s from %s", req.body.kind(), msg.clientAddr);
return;
}
break;
case PROXY_SHARD_REQ_PROTOCOL_VERSION:
{
ProxyShardReqMsg signedReq;
signedReq.unpack(msg.buf, _expandedShardKey);
req.id = signedReq.id;
req.body = std::move(signedReq.body);
while (msg.buf.remaining()) {
ShardReqMsg req;
try {
switch (protocol) {
case CDC_TO_SHARD_REQ_PROTOCOL_VERSION:
{
CdcToShardReqMsg signedReq;
signedReq.unpack(msg.buf, _expandedCDCKey);
req.id = signedReq.id;
req.body = std::move(signedReq.body);
}
break;
case SHARD_REQ_PROTOCOL_VERSION:
req.unpack(msg.buf);
if (isPrivilegedRequestKind((uint8_t)req.body.kind())) {
LOG_ERROR(_env, "Received unauthenticated request %s from %s", req.body.kind(), msg.clientAddr);
return;
}
break;
case PROXY_SHARD_REQ_PROTOCOL_VERSION:
{
ProxyShardReqMsg signedReq;
signedReq.unpack(msg.buf, _expandedShardKey);
req.id = signedReq.id;
req.body = std::move(signedReq.body);
}
break;
default:
ALWAYS_ASSERT(false, "Unknown protocol version");
}
break;
default:
ALWAYS_ASSERT(false, "Unknown protocol version");
} catch (const BincodeException& err) {
LOG_ERROR(_env, "Could not parse: %s", err.what());
RAISE_ALERT(_env, "could not parse request from %s, dropping it.", msg.clientAddr);
return;
}
} catch (const BincodeException& err) {
LOG_ERROR(_env, "Could not parse: %s", err.what());
RAISE_ALERT(_env, "could not parse request from %s, dropping it.", msg.clientAddr);
return;
}

auto t0 = ternNow();
auto t0 = ternNow();

LOG_DEBUG(_env, "received request id %s, kind %s, from %s", req.id, req.body.kind(), msg.clientAddr);
LOG_DEBUG(_env, "received request id %s, kind %s, from %s", req.id, req.body.kind(), msg.clientAddr);

if (bigRequest(req.body.kind())) {
if (bigRequest(req.body.kind())) {
if (unlikely(_env._shouldLog(LogLevel::LOG_TRACE))) {
LOG_TRACE(_env, "parsed request: %s", req);
} else {
LOG_DEBUG(_env, "parsed request: <omitted>");
}
} else {
LOG_DEBUG(_env, "parsed request: %s", req);
}
} else {
LOG_DEBUG(_env, "parsed request: %s", req);
}

auto& entry = _requestNeedsConsistency(req.body.kind(), protocol) ? _writeReqs.emplace_back() : _readRequests.emplace_back();
entry.sockIx = msg.socketIx;
entry.clientAddr = msg.clientAddr;
entry.receivedAt = t0;
entry.protocol = protocol;
entry.msg = std::move(req);
auto& entry = _requestNeedsConsistency(req.body.kind(), protocol) ? _writeReqs.emplace_back() : _readRequests.emplace_back();
entry.sockIx = msg.socketIx;
entry.clientAddr = msg.clientAddr;
entry.receivedAt = t0;
entry.protocol = protocol;
entry.msg = std::move(req);
}
}

// All write requests fall into this category. Some read requests issues by CDC also need cross regional consistency
Expand Down
Loading