1212// along with this program. If not, see <https://www.gnu.org/licenses/>.
1313
1414#include " TfBuilderRpc.h"
15+ #include " TfBuilderInputDefs.h"
1516#include < grpcpp/grpcpp.h>
1617
1718#include < MemoryUtils.h>
@@ -39,10 +40,11 @@ void TfBuilderRpcImpl::initDiscovery(const std::string pRpcSrvBindIp, int &lReal
3940 IDDLOG (" gRPC server is started. server_ep={}:{}" , pRpcSrvBindIp, lRealPort);
4041}
4142
42- bool TfBuilderRpcImpl::start (const std::uint64_t pBufferSize)
43+ bool TfBuilderRpcImpl::start (const std::uint64_t pBufferSize, std::shared_ptr<ConcurrentQueue<ReceivedStfMeta> > pRecvQueue )
4344{
4445 mBufferSize = pBufferSize;
4546 mCurrentTfBufferSize = pBufferSize;
47+ mReceivedDataQueue = pRecvQueue;
4648
4749 // Interact with the scheduler
4850 if (!mTfSchedulerRpcClient .should_retry_start ()) {
@@ -313,8 +315,29 @@ ::grpc::Status TfBuilderRpcImpl::BuildTfRequest(::grpc::ServerContext* /*context
313315 return a.mStfDataSize < b.mStfDataSize ;
314316 });
315317
318+ // setup renaming of topological Stfs
319+ auto lTopoStfId = mTopoStfId ;
320+ if (request->tf_source () == TOPOLOGICAL ) {
321+ auto &lStfSenderId = request->stf_size_map ().begin ()->first ;
322+
323+ std::scoped_lock lLock (mTopoTfIdLock );
324+
325+ assert (mTopoTfIdRenameMap [lStfSenderId].count (lTfId) == 0 );
326+
327+ mTopoTfIdRenameMap [lStfSenderId][lTfId] = lTopoStfId;
328+
329+ // notify Input stage about new Stf (renamed)
330+ mReceivedDataQueue ->push (ReceivedStfMeta (ReceivedStfMeta::MetaType::ADD , lTopoStfId));
331+
332+ mTopoStfId += 1 ;
333+ } else {
334+ lTopoStfId = lTfId; // set to the actual tf id if not topo
335+ // notify Input stage about new Stf (regular)
336+ mReceivedDataQueue ->push (ReceivedStfMeta (ReceivedStfMeta::MetaType::ADD , lTfId));
337+ }
338+
316339 // add the vector to the stf request map
317- mStfRequestDeque .push (lTfId, std::move (lStfRequestVector));
340+ mStfRequestQueue .push (lTfId, (request-> tf_source () == TOPOLOGICAL ), lTopoStfId , std::move (lStfRequestVector));
318341
319342 response->set_status (BuildTfResponse::OK );
320343 return ::grpc::Status::OK ;
@@ -335,14 +358,22 @@ void TfBuilderRpcImpl::StfRequestThread()
335358 while (mRunning ) {
336359 StfRequests lStfRequest;
337360 {
338- std::optional<std::pair< std::uint64_t , std::vector<StfRequests>> > lReqOpt;
339- if ((lReqOpt = mStfRequestDeque .pop_wait_for (100ms)) == std::nullopt ) {
361+ std::optional<std::tuple<std:: uint64_t , bool , std::uint64_t , std::vector<StfRequests>> > lReqOpt;
362+ if ((lReqOpt = mStfRequestQueue .pop_wait_for (100ms)) == std::nullopt ) {
340363 continue ;
341364 }
342365
343366 assert (lReqOpt);
344- const auto lTfId = lReqOpt.value ().first ;
345- auto &lReqVector = lReqOpt.value ().second ;
367+ const auto lTfId = std::get<0 >(lReqOpt.value ());
368+ const auto lIsTopo = std::get<1 >(lReqOpt.value ());
369+ const auto lTfRenamedId = std::get<2 >(lReqOpt.value ());
370+ auto &lReqVector = std::get<3 >(lReqOpt.value ());
371+
372+ std::string lStfSenderIdTopo;
373+ if (lIsTopo) {
374+ assert (lReqVector.size () == 1 );
375+ lStfSenderIdTopo = lReqVector.front ().mStfSenderId ;
376+ }
346377
347378 mMaxNumReqInFlight = std::clamp (mDiscoveryConfig ->getUInt64Param (MaxNumStfTransfersKey, MaxNumStfTransferDefault),
348379 std::uint64_t (10 ), std::uint64_t (200 ));
@@ -416,7 +447,29 @@ void TfBuilderRpcImpl::StfRequestThread()
416447 }
417448
418449 // set the number of STFs for merging thread
419- setNumberOfStfs (lTfId, lNumExpectedStfs);
450+ if (!lIsTopo) {
451+ setNumberOfStfs (lTfId, lNumExpectedStfs);
452+ } else {
453+ setNumberOfStfs (lTfRenamedId, lNumExpectedStfs);
454+ }
455+
456+ // cleanup if we reached no StfSenders
457+ if (lNumExpectedStfs == 0 ) {
458+ if (lIsTopo) {
459+ // Topological: indicate that we're deleting topological (renamed) Id
460+ std::scoped_lock lLock (mTopoTfIdLock );
461+ assert (mTopoTfIdRenameMap [lStfSenderIdTopo].count (lTfId) == 1 );
462+ assert (lTfRenamedId == mTopoTfIdRenameMap [lStfSenderIdTopo][lTfId]);
463+
464+ mTopoTfIdRenameMap [lStfSenderIdTopo].erase (lTfId);
465+
466+ // notify Input stage about new Stf (renamed)
467+ mReceivedDataQueue ->push (ReceivedStfMeta (ReceivedStfMeta::MetaType::DELETE , lTfRenamedId));
468+ } else {
469+ // notify Input stage not to wait for STFs if we reached none of StfSender
470+ mReceivedDataQueue ->push (ReceivedStfMeta (ReceivedStfMeta::MetaType::DELETE , lTfId));
471+ }
472+ }
420473 }
421474 }
422475 // send disconnect update
0 commit comments