udpmsgpk.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifdef _WIN32
  14. #pragma warning( disable : 4786)
  15. #pragma warning( disable : 4018)
  16. #endif
  17. #include "platform.h"
  18. #include <string>
  19. #include <map>
  20. #include <queue>
  21. #include "jthread.hpp"
  22. #include "jlog.hpp"
  23. #include "jisem.hpp"
  24. #include "jencrypt.hpp"
  25. #include "jsecrets.hpp"
  26. #include "udplib.hpp"
  27. #include "udptrr.hpp"
  28. #include "udptrs.hpp"
  29. #include "udpmsgpk.hpp"
  30. #include "roxiemem.hpp"
  31. #include "roxie.hpp"
  32. using roxiemem::DataBuffer;
  33. using roxiemem::IRowManager;
  34. RelaxedAtomic<unsigned> unwantedDiscarded;
  35. // PackageSequencer ====================================================================================
  36. //
  37. typedef DataBuffer * data_buffer_ptr;
  38. int g_sequence_compare(const void *arg1, const void *arg2 )
  39. {
  40. DataBuffer *dataBuff1 = *(DataBuffer **) arg1;
  41. DataBuffer *dataBuff2 = *(DataBuffer **) arg2;
  42. UdpPacketHeader *pktHdr1 = (UdpPacketHeader*) dataBuff1->data;
  43. UdpPacketHeader *pktHdr2 = (UdpPacketHeader*) dataBuff2->data;
  44. if (pktHdr1->pktSeq < pktHdr2->pktSeq) return -1;
  45. if (pktHdr1->pktSeq > pktHdr2->pktSeq) return 1;
  46. return 0;
  47. }
  48. class PackageSequencer : public CInterface, implements IInterface
  49. {
  50. DataBuffer *firstPacket;
  51. DataBuffer *lastContiguousPacket;
  52. DataBuffer *tail = nullptr;
  53. unsigned metaSize;
  54. unsigned headerSize;
  55. const void *header;
  56. unsigned maxSeqSeen = 0;
  57. unsigned numPackets = 0;
  58. #ifdef _DEBUG
  59. unsigned scans = 0;
  60. unsigned overscans = 0;
  61. #endif
  62. MemoryBuffer metadata;
  63. InterruptableSemaphore dataAvailable; // MORE - need to work out when to interrupt it!
  64. bool encrypted = false;
  65. public:
  66. IMPLEMENT_IINTERFACE;
  67. PackageSequencer(bool _encrypted) : encrypted(_encrypted)
  68. {
  69. if (checkTraceLevel(TRACE_MSGPACK, 3))
  70. DBGLOG("UdpCollator: PackageSequencer::PackageSequencer this=%p", this);
  71. metaSize = 0;
  72. headerSize = 0;
  73. header = NULL;
  74. firstPacket = NULL;
  75. lastContiguousPacket = NULL;
  76. }
  77. ~PackageSequencer()
  78. {
  79. if (checkTraceLevel(TRACE_MSGPACK, 3))
  80. DBGLOG("UdpCollator: PackageSequencer::~PackageSequencer this=%p", this);
  81. DataBuffer *finger = firstPacket;
  82. while (finger)
  83. {
  84. DataBuffer *goer = finger;
  85. finger = finger->msgNext;
  86. goer->Release();
  87. }
  88. }
  89. DataBuffer *next(DataBuffer *after)
  90. {
  91. dataAvailable.wait(); // MORE - when do I interrupt? Should I time out? Will potentially block indefinitely if sender restarts (leading to an abandoned packet) or stalls.
  92. DataBuffer *ret;
  93. if (after)
  94. ret = after->msgNext;
  95. else
  96. ret = firstPacket;
  97. if (checkTraceLevel(TRACE_MSGPACK, 5))
  98. {
  99. if (ret)
  100. {
  101. StringBuffer s;
  102. UdpPacketHeader *pktHdr = (UdpPacketHeader*) ret->data;
  103. DBGLOG("UdpCollator: PackageSequencer::next returns ruid=" RUIDF " id=0x%.8X mseq=%u pkseq=0x%.8X node=%s dataBuffer=%p this=%p",
  104. pktHdr->ruid, pktHdr->msgId, pktHdr->msgSeq, pktHdr->pktSeq, pktHdr->node.getTraceText(s).str(), ret, this);
  105. }
  106. else
  107. DBGLOG("UdpCollator: PackageSequencer::next returns NULL this=%p", this);
  108. }
  109. return ret;
  110. }
  111. bool insert(DataBuffer *dataBuff, std::atomic<unsigned> &duplicates, std::atomic<unsigned> &resends) // returns true if message is complete.
  112. {
  113. bool res = false;
  114. assert(dataBuff->msgNext == NULL);
  115. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  116. unsigned pktseq = pktHdr->pktSeq;
  117. if ((pktseq & UDP_PACKET_SEQUENCE_MASK) > maxSeqSeen)
  118. maxSeqSeen = pktseq & UDP_PACKET_SEQUENCE_MASK;
  119. if (pktseq & UDP_PACKET_RESENT)
  120. {
  121. pktseq &= ~UDP_PACKET_RESENT;
  122. pktHdr->pktSeq = pktseq;
  123. resends++;
  124. }
  125. if (checkTraceLevel(TRACE_MSGPACK, 5))
  126. {
  127. StringBuffer s;
  128. DBGLOG("UdpCollator: PackageSequencer::insert ruid=" RUIDF " id=0x%.8X mseq=%u pkseq=0x%.8X sendSeq=%" SEQF "u node=%s dataBuffer=%p this=%p",
  129. pktHdr->ruid, pktHdr->msgId, pktHdr->msgSeq, pktHdr->pktSeq, pktHdr->sendSeq, pktHdr->node.getTraceText(s).str(), dataBuff, this);
  130. }
  131. // Optimize the (very) common case where I need to add to the end
  132. DataBuffer *finger;
  133. DataBuffer *prev;
  134. if (tail && (pktseq > ((UdpPacketHeader*) tail->data)->pktSeq))
  135. {
  136. assert(tail->msgNext==nullptr);
  137. finger = nullptr;
  138. prev = tail;
  139. tail = dataBuff;
  140. }
  141. else
  142. {
  143. // This is an insertion sort - YUK!
  144. if (lastContiguousPacket)
  145. {
  146. UdpPacketHeader *oldHdr = (UdpPacketHeader*) lastContiguousPacket->data;
  147. if (pktHdr->pktSeq <= oldHdr->pktSeq)
  148. {
  149. // discard duplicated incoming packet - should be uncommon unless we requested a resend for a packet already in flight
  150. if (checkTraceLevel(TRACE_MSGPACK, 5))
  151. DBGLOG("UdpCollator: Discarding duplicate incoming packet %u (we have all up to %u)", pktHdr->pktSeq, oldHdr->pktSeq);
  152. dataBuff->Release();
  153. duplicates++;
  154. return false;
  155. }
  156. finger = lastContiguousPacket->msgNext;
  157. prev = lastContiguousPacket;
  158. }
  159. else
  160. {
  161. finger = firstPacket;
  162. prev = NULL;
  163. }
  164. while (finger)
  165. {
  166. #ifdef _DEBUG
  167. scans++;
  168. if (scans==1000000)
  169. {
  170. overscans++;
  171. DBGLOG("%u million scans in UdpCollator insert(DataBuffer *dataBuff", overscans);
  172. if (lastContiguousPacket)
  173. {
  174. UdpPacketHeader *oldHdr = (UdpPacketHeader*) lastContiguousPacket->data;
  175. DBGLOG("lastContiguousPacket is at %u , last packet seen is %u", oldHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK, pktHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK);
  176. }
  177. else
  178. DBGLOG("lastContiguousPacket is NULL , last packet seen is %u", pktHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK);
  179. scans = 0;
  180. }
  181. #endif
  182. UdpPacketHeader *oldHdr = (UdpPacketHeader*) finger->data;
  183. if (pktHdr->pktSeq == oldHdr->pktSeq)
  184. {
  185. // discard duplicated incoming packet - should be uncommon unless we requested a resend for a packet already in flight
  186. if (checkTraceLevel(TRACE_MSGPACK, 5))
  187. DBGLOG("UdpCollator: Discarding duplicate incoming packet %u", pktHdr->pktSeq);
  188. dataBuff->Release();
  189. return false;
  190. }
  191. else if (pktHdr->pktSeq < oldHdr->pktSeq)
  192. {
  193. break;
  194. }
  195. else
  196. {
  197. prev = finger;
  198. finger = finger->msgNext;
  199. }
  200. }
  201. }
  202. if (prev)
  203. {
  204. assert(prev->msgNext == finger);
  205. prev->msgNext = dataBuff;
  206. }
  207. else
  208. {
  209. firstPacket = dataBuff;
  210. if (!tail)
  211. tail = dataBuff;
  212. }
  213. dataBuff->msgNext = finger;
  214. #ifdef _DEBUG
  215. numPackets++;
  216. #endif
  217. // Now that we know we are keeping it, decrypt it
  218. // MORE - could argue that we would prefer to wait even longer - until we know consumer wants it - but that might be complex
  219. if (encrypted)
  220. {
  221. // MORE - This is decrypting in-place. Is that ok?? Seems to be with the code we currently use, but if that changed
  222. // might need to rethink this
  223. const MemoryAttr &udpkey = getSecretUdpKey(true);
  224. size_t decryptedSize = aesDecrypt(udpkey.get(), udpkey.length(), pktHdr+1, pktHdr->length-sizeof(UdpPacketHeader), pktHdr+1, DATA_PAYLOAD-sizeof(UdpPacketHeader));
  225. if (checkTraceLevel(TRACE_MSGPACK, 5))
  226. DBGLOG("Decrypted %u bytes at %p resulting in %u bytes", (unsigned) (pktHdr->length-sizeof(UdpPacketHeader)), pktHdr+1, (unsigned) decryptedSize);
  227. pktHdr->length = decryptedSize + sizeof(UdpPacketHeader);
  228. }
  229. if (prev == lastContiguousPacket)
  230. {
  231. unsigned prevseq;
  232. if (lastContiguousPacket)
  233. {
  234. UdpPacketHeader *lastHdr = (UdpPacketHeader*) lastContiguousPacket->data;
  235. prevseq = lastHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK;
  236. finger = lastContiguousPacket->msgNext;
  237. }
  238. else
  239. {
  240. prevseq = (unsigned) -1;
  241. finger = firstPacket;
  242. }
  243. while (finger)
  244. {
  245. UdpPacketHeader *fingerHdr = (UdpPacketHeader*) finger->data;
  246. unsigned pktseq = fingerHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK;
  247. if (pktseq == prevseq+1)
  248. {
  249. unsigned packetDataSize = fingerHdr->length - fingerHdr->metalength - sizeof(UdpPacketHeader);
  250. assert(packetDataSize < roxiemem::DATA_ALIGNMENT_SIZE);
  251. if (pktseq == 0)
  252. {
  253. // MORE - Is this safe - header lifetime is somewhat unpredictable without a copy of it...
  254. // Client header is at the start of packet 0
  255. headerSize = *(unsigned short *)(finger->data + sizeof(UdpPacketHeader));
  256. header = finger->data + sizeof(UdpPacketHeader) + sizeof(unsigned short);
  257. packetDataSize -= headerSize + sizeof(unsigned short);
  258. }
  259. if (fingerHdr->metalength)
  260. {
  261. // MORE - may be worth taking the effort to avoid copy of metadata unless it's split up
  262. metaSize += fingerHdr->metalength;
  263. metadata.append(fingerHdr->metalength, finger->data + fingerHdr->length - fingerHdr->metalength);
  264. }
  265. lastContiguousPacket = finger;
  266. dataAvailable.signal();
  267. if (fingerHdr->pktSeq & UDP_PACKET_COMPLETE)
  268. {
  269. res = true;
  270. dataAvailable.signal(); // allowing us to read the NULL that signifies end of message. May prefer to use the flag to stop?
  271. }
  272. }
  273. else
  274. break;
  275. finger = finger->msgNext;
  276. prevseq = pktseq;
  277. }
  278. }
  279. return res;
  280. }
  281. inline const void *getMetaData(unsigned &length)
  282. {
  283. length = metadata.length();
  284. return metadata.toByteArray();
  285. }
  286. inline const void *getMessageHeader()
  287. {
  288. return header;
  289. }
  290. inline unsigned getHeaderSize()
  291. {
  292. return headerSize;
  293. }
  294. void dump()
  295. {
  296. DBGLOG("Contains %u packets, lastSeq = %u", numPackets, maxSeqSeen);
  297. if (lastContiguousPacket)
  298. {
  299. UdpPacketHeader *hdr = (UdpPacketHeader*) lastContiguousPacket->data;
  300. DBGLOG("lastContiguousPacket is %u %" SEQF "u", hdr->pktSeq, hdr->sendSeq);
  301. }
  302. }
  303. };
  304. // MessageResult ====================================================================================
  305. //
  306. class CMessageUnpackCursor: implements IMessageUnpackCursor, public CInterface
  307. {
  308. PackageSequencer *pkSequencer;
  309. DataBuffer *dataBuff;
  310. unsigned current_pos;
  311. Linked<IRowManager> rowMgr;
  312. public:
  313. IMPLEMENT_IINTERFACE;
  314. CMessageUnpackCursor(PackageSequencer *_pkSqncr, IRowManager *_rowMgr) : rowMgr(_rowMgr)
  315. {
  316. if (checkTraceLevel(TRACE_MSGPACK, 3))
  317. DBGLOG("UdpCollator: CMessageUnpackCursor::CMessageUnpackCursor this=%p", this);
  318. pkSequencer = _pkSqncr;
  319. dataBuff = pkSequencer->next(NULL);
  320. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  321. current_pos = sizeof(UdpPacketHeader) + pkSequencer->getHeaderSize() + sizeof(unsigned short); // YUK - code neads cleaning!
  322. unsigned packetDataLimit = pktHdr->length - pktHdr->metalength;
  323. while (current_pos >= packetDataLimit)
  324. {
  325. current_pos = sizeof(UdpPacketHeader);
  326. dataBuff = pkSequencer->next(dataBuff); // NULL, we expect, unless packing was really weird.
  327. if (dataBuff)
  328. {
  329. pktHdr = (UdpPacketHeader*) dataBuff->data;
  330. packetDataLimit = pktHdr->length - pktHdr->metalength;
  331. }
  332. else
  333. break;
  334. }
  335. }
  336. ~CMessageUnpackCursor()
  337. {
  338. if (checkTraceLevel(TRACE_MSGPACK, 3))
  339. DBGLOG("UdpCollator: CMessageUnpackCursor::~CMessageUnpackCursor this=%p", this);
  340. pkSequencer->Release();
  341. }
  342. virtual bool atEOF() const
  343. {
  344. return dataBuff == NULL;
  345. }
  346. virtual bool isSerialized() const
  347. {
  348. return true;
  349. }
  350. virtual const void *getNext(int length)
  351. {
  352. // YUK horrid code! Though packer is even more horrid
  353. void *res = 0;
  354. if (dataBuff)
  355. {
  356. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  357. if (checkTraceLevel(TRACE_MSGPACK, 4))
  358. {
  359. StringBuffer s;
  360. DBGLOG("UdpCollator: CMessageUnpackCursor::getNext(%u) pos=%u pktLength=%u metaLen=%u ruid=" RUIDF " id=0x%.8X mseq=%u pkseq=0x%.8X node=%s dataBuff=%p this=%p",
  361. length, current_pos, pktHdr->length, pktHdr->metalength,
  362. pktHdr->ruid, pktHdr->msgId, pktHdr->msgSeq, pktHdr->pktSeq,
  363. pktHdr->node.getTraceText(s).str(), dataBuff, this);
  364. }
  365. unsigned packetDataLimit = pktHdr->length - pktHdr->metalength;
  366. if ((packetDataLimit - current_pos) >= (unsigned) length)
  367. {
  368. // Simple case - no need to copy
  369. res = &dataBuff->data[current_pos];
  370. current_pos += length;
  371. while (current_pos >= packetDataLimit)
  372. {
  373. dataBuff = pkSequencer->next(dataBuff);
  374. current_pos = sizeof(UdpPacketHeader);
  375. if (dataBuff)
  376. {
  377. pktHdr = (UdpPacketHeader*) dataBuff->data;
  378. packetDataLimit = pktHdr->length - pktHdr->metalength;
  379. }
  380. else
  381. break;
  382. }
  383. LinkRoxieRow(res);
  384. return res;
  385. }
  386. char *currResLoc = (char*)rowMgr->allocate(length, 0);
  387. res = currResLoc;
  388. while (length && dataBuff)
  389. {
  390. // Spans more than one block - allocate and copy
  391. unsigned cpyLen = packetDataLimit - current_pos;
  392. if (cpyLen > (unsigned) length) cpyLen = length;
  393. memcpy(currResLoc, &dataBuff->data[current_pos], cpyLen);
  394. length -= cpyLen;
  395. currResLoc += cpyLen;
  396. current_pos += cpyLen;
  397. while (current_pos >= packetDataLimit)
  398. {
  399. dataBuff = pkSequencer->next(dataBuff);
  400. if (dataBuff)
  401. {
  402. current_pos = sizeof(UdpPacketHeader);
  403. pktHdr = (UdpPacketHeader*) dataBuff->data;
  404. packetDataLimit = pktHdr->length - pktHdr->metalength;
  405. }
  406. else
  407. {
  408. current_pos = 0;
  409. pktHdr = NULL;
  410. packetDataLimit = 0;
  411. break;
  412. }
  413. }
  414. }
  415. assertex(!length); // fail if not enough data available
  416. }
  417. else
  418. res = NULL;
  419. return res;
  420. }
  421. };
  422. class CMessageResult : public IMessageResult, CInterface {
  423. PackageSequencer *pkSequencer;
  424. mutable MemoryBuffer metaInfo;
  425. mutable CriticalSection metaCrit;
  426. public:
  427. IMPLEMENT_IINTERFACE;
  428. CMessageResult(PackageSequencer *_pkSqncr)
  429. {
  430. if (checkTraceLevel(TRACE_MSGPACK, 3))
  431. DBGLOG("UdpCollator: CMessageResult::CMessageResult pkSqncr=%p, this=%p", _pkSqncr, this);
  432. pkSequencer = _pkSqncr;
  433. }
  434. ~CMessageResult()
  435. {
  436. if (checkTraceLevel(TRACE_MSGPACK, 3))
  437. DBGLOG("UdpCollator: CMessageResult::~CMessageResult this=%p", this);
  438. pkSequencer->Release();
  439. }
  440. virtual IMessageUnpackCursor *getCursor(IRowManager *rowMgr) const
  441. {
  442. return new CMessageUnpackCursor(LINK(pkSequencer), rowMgr);
  443. }
  444. virtual const void *getMessageHeader(unsigned &length) const
  445. {
  446. length = pkSequencer->getHeaderSize();
  447. return pkSequencer->getMessageHeader();
  448. }
  449. virtual const void *getMessageMetadata(unsigned &length) const
  450. {
  451. return pkSequencer->getMetaData(length);
  452. }
  453. virtual void discard() const
  454. {
  455. if (checkTraceLevel(TRACE_MSGPACK, 2))
  456. DBGLOG("UdpCollator: CMessageResult - Roxie server discarded a packet");
  457. unwantedDiscarded++;
  458. }
  459. };
  460. // MessageCollator ====================================================================================
  461. //
  462. PUID GETPUID(DataBuffer *dataBuff)
  463. {
  464. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  465. unsigned ip4 = pktHdr->node.getIp4();
  466. return (((PUID) ip4) << 32) | (PUID) pktHdr->msgSeq;
  467. }
  468. CMessageCollator::CMessageCollator(IRowManager *_rowMgr, unsigned _ruid, bool _encrypted) : rowMgr(_rowMgr), ruid(_ruid), encrypted(_encrypted)
  469. {
  470. if (checkTraceLevel(TRACE_MSGPACK, 3))
  471. DBGLOG("UdpCollator: CMessageCollator::CMessageCollator rowMgr=%p this=%p ruid=" RUIDF "", _rowMgr, this, ruid);
  472. memLimitExceeded = false;
  473. activity = false;
  474. totalBytesReceived = 0;
  475. }
  476. CMessageCollator::~CMessageCollator()
  477. {
  478. if (checkTraceLevel(TRACE_MSGPACK, 3))
  479. DBGLOG("UdpCollator: CMessageCollator::~CMessageCollator ruid=" RUIDF ", this=%p", ruid, this);
  480. while (!queue.empty())
  481. {
  482. PackageSequencer *pkSqncr = queue.front();
  483. queue.pop();
  484. pkSqncr->Release();
  485. }
  486. }
  487. void CMessageCollator::noteDuplicate(bool isResend)
  488. {
  489. totalDuplicates++;
  490. if (isResend)
  491. totalResends++;
  492. }
  493. unsigned CMessageCollator::queryBytesReceived() const
  494. {
  495. return totalBytesReceived;
  496. }
  497. unsigned CMessageCollator::queryDuplicates() const
  498. {
  499. return totalDuplicates;
  500. }
  501. unsigned CMessageCollator::queryResends() const
  502. {
  503. return totalResends;
  504. }
  505. bool CMessageCollator::attach_databuffer(DataBuffer *dataBuff)
  506. {
  507. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  508. totalBytesReceived += pktHdr->length;
  509. if (pktHdr->node.isNull()) // Indicates a packet that has been identified as a duplicate to be logged and discarded
  510. {
  511. noteDuplicate((pktHdr->pktSeq & UDP_PACKET_RESENT) != 0);
  512. dataBuff->Release();
  513. return true;
  514. }
  515. activity = true;
  516. if (memLimitExceeded || roxiemem::memPoolExhausted())
  517. {
  518. DBGLOG("UdpCollator: mem limit exceeded");
  519. return false;
  520. }
  521. if (!dataBuff->attachToRowMgr(rowMgr))
  522. {
  523. memLimitExceeded = true;
  524. DBGLOG("UdpCollator: mem limit exceeded");
  525. return(false);
  526. }
  527. collate(dataBuff);
  528. return true;
  529. }
  530. bool CMessageCollator::attach_data(const void *data, unsigned len)
  531. {
  532. // Simple code can allocate databuffer, copy data in, then call attach_databuffer
  533. // MORE - we can attach as we create may be more sensible (and simplify roxiemem rather if it was the ONLY way)
  534. activity = true;
  535. totalBytesReceived += len;
  536. if (memLimitExceeded || roxiemem::memPoolExhausted())
  537. {
  538. DBGLOG("UdpCollator: mem limit exceeded");
  539. return false;
  540. }
  541. DataBuffer *dataBuff = bufferManager->allocate();
  542. assertex(len <= DATA_PAYLOAD);
  543. memcpy(dataBuff->data, data, len);
  544. if (!dataBuff->attachToRowMgr(rowMgr))
  545. {
  546. memLimitExceeded = true;
  547. DBGLOG("UdpCollator: mem limit exceeded");
  548. dataBuff->Release();
  549. return(false);
  550. }
  551. collate(dataBuff);
  552. return true;
  553. }
  554. void CMessageCollator::collate(DataBuffer *dataBuff)
  555. {
  556. PUID puid = GETPUID(dataBuff);
  557. // MORE - we leak (at least until query terminates) a PackageSequencer for messages that we only receive parts of - maybe only an issue for "catchall" case
  558. PackageSequencer *pkSqncr = mapping.getValue(puid);
  559. if (!pkSqncr)
  560. {
  561. pkSqncr = new PackageSequencer(encrypted);
  562. mapping.setValue(puid, pkSqncr);
  563. pkSqncr->Release();
  564. }
  565. bool isComplete = pkSqncr->insert(dataBuff, totalDuplicates, totalResends);
  566. if (isComplete)
  567. {
  568. pkSqncr->Link();
  569. mapping.remove(puid);
  570. queueCrit.enter();
  571. queue.push(pkSqncr);
  572. queueCrit.leave();
  573. sem.signal();
  574. }
  575. }
  576. IMessageResult *CMessageCollator::getNextResult(unsigned time_out, bool &anyActivity)
  577. {
  578. if (checkTraceLevel(TRACE_MSGPACK, 3))
  579. DBGLOG("UdpCollator: CMessageCollator::getNextResult() timeout=%.8X ruid=%u rowMgr=%p this=%p", time_out, ruid, (void*) rowMgr, this);
  580. if (memLimitExceeded)
  581. {
  582. DBGLOG("UdpCollator: CMessageCollator::getNextResult() throwing memory limit exceeded exception - rowMgr=%p this=%p", (void*) rowMgr, this);
  583. throw MakeStringException(0, "memory limit exceeded");
  584. }
  585. else if (roxiemem::memPoolExhausted())
  586. {
  587. DBGLOG("UdpCollator: CMessageCollator::getNextResult() throwing memory pool exhausted exception - rowMgr=%p this=%p", (void*)rowMgr, this);
  588. throw MakeStringException(0, "memory pool exhausted");
  589. }
  590. if (sem.wait(time_out))
  591. {
  592. queueCrit.enter();
  593. PackageSequencer *pkSqncr = queue.front();
  594. queue.pop();
  595. queueCrit.leave();
  596. anyActivity = true;
  597. activity = false;
  598. return new CMessageResult(pkSqncr);
  599. }
  600. anyActivity = activity;
  601. activity = false;
  602. if (!anyActivity && ruid>=RUID_FIRST && checkTraceLevel(TRACE_MSGPACK, 1)) // suppress the tracing for pings where we expect the timeout...
  603. {
  604. #ifdef _DEBUG
  605. DBGLOG("GetNextResult timeout: mapping has %d partial results", mapping.ordinality());
  606. HashIterator h(mapping);
  607. ForEach(h)
  608. {
  609. auto *r = mapping.mapToValue(&h.query());
  610. PUID puid = *(PUID *) h.query().getKey();
  611. DBGLOG("puid=%" I64F "x:", puid);
  612. PackageSequencer *pkSqncr = mapping.getValue(puid);
  613. pkSqncr->dump();
  614. }
  615. #endif
  616. DBGLOG("UdpCollator: CMessageCollator::GetNextResult timeout");
  617. }
  618. return 0;
  619. }
  620. void CMessageCollator::interrupt(IException *E)
  621. {
  622. sem.interrupt(E);
  623. }
  624. // ====================================================================================
  625. //