1 #include "hmbdc/Copyright.hpp" 3 #include "hmbdc/tips/reliable/TcpEpollFd.hpp" 4 #include "hmbdc/tips/reliable/AttBufferAdaptor.hpp" 5 #include "hmbdc/app/Message.hpp" 6 #include "hmbdc/app/Config.hpp" 7 #include "hmbdc/app/Logger.hpp" 8 #include "hmbdc/pattern/SeqArb.hpp" 9 #include "hmbdc/time/Time.hpp" 10 #include "hmbdc/comm/inet/Misc.hpp" 12 #include <unordered_set> 13 #include <boost/lexical_cast.hpp> 22 namespace hmbdc {
namespace tips {
namespace reliable {
24 using Tags = std::unordered_set<uint16_t>;
26 namespace backuprecvsessiont_detail {
31 template <
typename TransportMessageHeader
32 ,
typename SessionStarted
33 ,
typename SessionDropped
35 ,
typename OutputBufferIn
37 ,
typename AttachmentAllocator>
46 , AttachmentAllocator>>;
48 , SendFrom
const& sendFrom
50 , OutputBufferIn& outputBuffer
55 , minSeq_(numeric_limits<HMBDC_SEQ_TYPE>::max())
56 , subscriptions_(subscriptions)
58 , outputBuffer_(outputBuffer)
59 , bufSize_(config.
getExt<
size_t>(
"maxTcpReadBytes"))
60 , buf_((
char*)memalign(SMP_CACHE_BYTES, bufSize_))
63 , seqArb_(numeric_limits<HMBDC_SEQ_TYPE>::max())
67 , recvBackupMessageCount_(0)
72 sendGapReport(seqArb_.expectingSeq(), 0);
79 HMBDC_LOG_N(
"BackupRecvSession retired: ",
id(),
" lifetime recved:", recvBackupMessageCount_);
83 outputBuffer_.putSome(sessionDropped_);
86 void start(in_addr_t ip, uint16_t port) {
87 sockaddr_in remoteAddr = {0};
88 remoteAddr.sin_family = AF_INET;
89 remoteAddr.sin_addr.s_addr = ip;
90 remoteAddr.sin_port = htons(port);
91 if (connect(writeFd_.fd, (sockaddr*)&remoteAddr,
sizeof(remoteAddr)) < 0) {
92 if (errno != EINPROGRESS) {
93 HMBDC_THROW(runtime_error,
"connect fail, errno=" << errno);
97 auto forRead = dup(writeFd_.fd);
99 HMBDC_THROW(std::runtime_error,
"dup failed errno=" << errno);
102 readFd_.fd = forRead;
103 utils::EpollTask::instance().add(utils::EpollTask::EPOLLOUT|utils::EpollTask::EPOLLET, writeFd_);
104 utils::EpollTask::instance().add(utils::EpollTask::EPOLLIN|utils::EpollTask::EPOLLET, readFd_);
108 if (hmbdc_unlikely(stopped_))
return false;
109 if (hmbdc_unlikely(!initialized_ && writeFd_.isFdReady())) {
112 }
catch (std::exception
const& e) {
113 HMBDC_LOG_W(e.what());
116 HMBDC_LOG_C(
"unknown exception");
123 void sendSubscribe(uint16_t t) {
125 auto l = sprintf(buf,
"+%d\t", t) - 1;
126 if (hmbdc_unlikely(send(writeFd_.fd, buf, l, MSG_NOSIGNAL) != l)) {
127 HMBDC_LOG_C(
"error when sending subscribe to ",
id());
132 void sendUnsubscribe(uint16_t
const& t) {
134 auto l = sprintf(buf,
"-%d\t", t) - 1;
135 if (hmbdc_unlikely(l != send(writeFd_.fd, buf, l, MSG_NOSIGNAL))) {
136 HMBDC_LOG_C(
"error when sending unsubscribe to ",
id(),
" errno=", errno);
141 void sendGapReport(HMBDC_SEQ_TYPE missSeq,
size_t len) {
142 if (hmbdc_likely(!gapPending_ && missSeq != numeric_limits<HMBDC_SEQ_TYPE>::max())) {
144 auto l = sprintf(buf,
"=%" PRIu64
",%zu\t", missSeq, len);
146 if (hmbdc_unlikely(l != send(writeFd_.fd, buf, l, MSG_NOSIGNAL))) {
147 HMBDC_LOG_C(
"error when sending gap report to ",
id(),
" errno=", errno);
152 gapPendingSeq_ = missSeq + len - 1;
158 char const* id()
const {
162 int accept(TransportMessageHeader* h) {
163 auto seq = h->getSeq();
164 auto a = arb(0, seq, h);
166 auto tag = h->typeTag();
167 if (tag == StartMemorySegTrain::typeTag) {
168 tag = h->template wrapped<StartMemorySegTrain>().inbandUnderlyingTypeTag;
170 if (subscriptions_.check(tag)) {
171 outputBuffer_.put(h);
177 int arb(uint16_t part, HMBDC_SEQ_TYPE seq
178 , TransportMessageHeader* h) HMBDC_RESTRICT {
180 if (hmbdc_unlikely(gapPending_ && part == 0))
return 0;
181 if (seq != numeric_limits<HMBDC_SEQ_TYPE>::max()) {
182 auto res = seqArb_(part, seq, [](
size_t) {
186 sendGapReport(seqArb_.expectingSeq(), seq - seqArb_.expectingSeq());
187 }
else if (seq == gapPendingSeq_) {
191 }
else if (h->typeTag() == SeqAlert::typeTag) {
192 auto& alert = h->template wrapped<SeqAlert>();
193 auto nextSeq = alert.expectSeq;
195 if (seqArb_.expectingSeq() < nextSeq) {
196 sendGapReport(seqArb_.expectingSeq(), nextSeq - seqArb_.expectingSeq());
204 SendFrom
const sendFrom;
207 void initializeConn() {
208 int flags = fcntl(writeFd_.fd, F_GETFL, 0);
209 flags &= ~O_NONBLOCK;
210 if (fcntl(writeFd_.fd, F_SETFL, flags) < 0) {
211 HMBDC_THROW(std::runtime_error,
"fcntl failed errno=" << errno);
214 auto sz = config_.getExt<
int>(
"tcpRecvBufferBytes");
216 if (setsockopt(readFd_.fd, SOL_SOCKET, SO_RCVBUF, &sz,
sizeof(sz)) < 0) {
217 HMBDC_LOG_C(
"failed to set send buffer size=", sz);
221 auto flag = config_.getExt<
bool>(
"nagling")?0:1;
222 if (setsockopt(writeFd_.fd, IPPROTO_TCP, TCP_NODELAY, (
char*) &flag,
sizeof(flag)) < 0) {
223 HMBDC_LOG_C(
"failed to set TCP_NODELAY, errno=", errno);
226 auto addrPort = hmbdc::comm::inet::getPeerIpPort(writeFd_.fd);
227 id_ = addrPort.first +
":" + std::to_string(addrPort.second);
228 strncpy(sessionStarted_.payload.ip, id_.c_str()
229 ,
sizeof(sessionStarted_.payload.ip));
230 sessionStarted_.payload.ip[
sizeof(sessionStarted_.payload.ip) - 1] = 0;
231 strncpy(sessionDropped_.payload.ip, id_.c_str()
232 ,
sizeof(sessionDropped_.payload.ip));
233 sessionDropped_.payload.ip[
sizeof(sessionDropped_.payload.ip) - 1] = 0;
234 outputBuffer_.putSome(sessionStarted_);
239 void sendSubscriptions() {
241 subscriptions_.exportTo([&oss](uint16_t tag,
auto&& count) {
243 oss <<
'+' << tag <<
'\t';
246 auto s = oss.str() +
"+\t";
247 auto sz = size_t(send(writeFd_.fd, s.c_str(), s.size(), MSG_NOSIGNAL));
248 if (hmbdc_unlikely(sz != s.size())) {
249 HMBDC_LOG_C(
"error when sending subscriptions to ",
id());
255 doRead() HMBDC_RESTRICT {
256 if (hmbdc_unlikely(seqArb_.expectingSeq() == numeric_limits<HMBDC_SEQ_TYPE>::max()) &&
257 filledLen_ >=
sizeof(HMBDC_SEQ_TYPE)) {
258 minSeq_ = *
reinterpret_cast<HMBDC_SEQ_TYPE*
>(bufCur_);
259 auto wireSize =
sizeof(HMBDC_SEQ_TYPE);
261 filledLen_ -= wireSize;
262 HMBDC_LOG_N(
"BackupRecvSession started ",
id(),
" with minSeq=", minSeq_);
263 seqArb_.expectingSeq() = minSeq_;
265 while (minSeq_ != numeric_limits<HMBDC_SEQ_TYPE>::max()
266 && filledLen_ >=
sizeof(TransportMessageHeader)) {
267 auto h =
reinterpret_cast<TransportMessageHeader*
>(bufCur_);
268 auto wireSize = h->wireSize();
270 if (hmbdc_likely(filledLen_ >= wireSize)) {
271 if (hmbdc_likely(h->messagePayloadLen
272 && subscriptions_.check(h->typeTag()))) {
273 outputBuffer_.put(h);
277 filledLen_ -= wireSize;
279 arb(1, seqArb_.expectingSeq(), h);
280 recvBackupMessageCount_++;
285 memmove(buf_, bufCur_, filledLen_);
288 if (readFd_.isFdReady()) {
289 auto l = recv(readFd_.fd, buf_ + filledLen_, bufSize_ - filledLen_
290 , MSG_NOSIGNAL|MSG_DONTWAIT);
291 if (hmbdc_unlikely(l < 0)) {
292 if (hmbdc_unlikely(!readFd_.checkErr())) {
293 HMBDC_LOG_C(
"recv failed errno=", errno);
297 }
else if (hmbdc_unlikely(l == 0)) {
298 HMBDC_LOG_W(
"peer dropped:",
id());
308 HMBDC_SEQ_TYPE minSeq_;
316 size_t const bufSize_;
324 size_t recvBackupMessageCount_;
326 HMBDC_SEQ_TYPE gapPendingSeq_;
327 uint16_t attTypeTag_;
328 void* attUserScratchpad_;
332 template <
typename TransportMessageHeader
333 ,
typename SessionStarted
334 ,
typename SessionDropped
336 ,
typename OutputBuffer
338 ,
typename AttachmentAllocator>
341 TransportMessageHeader
347 , AttachmentAllocator
T getExt(const path_type ¶m, bool throwIfMissing=true) const
get a value from the config
Definition: Config.hpp:238
Definition: TcpEpollFd.hpp:14
class to hold an hmbdc configuration
Definition: Config.hpp:45
Definition: SeqArb.hpp:165
Definition: TypedString.hpp:84
Definition: Timers.hpp:70
Definition: EpollTask.hpp:87
Definition: Timers.hpp:117
Definition: BackupRecvSessionT.hpp:38