Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Performance[mqbi::DispatcherEvent]: replace multiple inheritance with variant #396

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/groups/mqb/mqba/mqba_adminsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void AdminSession::onDispatcherEvent(const mqbi::DispatcherEvent& event)
switch (event.type()) {
case mqbi::DispatcherEventType::e_CALLBACK: {
const mqbi::DispatcherCallbackEvent* realEvent =
event.asCallbackEvent();
&event.getAs<mqbi::DispatcherCallbackEvent>();

BSLS_ASSERT_SAFE(realEvent->callback());
realEvent->callback()(dispatcherClientData().processorHandle());
Expand Down
45 changes: 25 additions & 20 deletions src/groups/mqb/mqba/mqba_clientsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2804,30 +2804,35 @@ void ClientSession::processEvent(
return; // RETURN
}

// Not a control or leader message, it's either a put or a confirm ..
mqbi::DispatcherEventType::Enum eventType;

if (event.isPutEvent()) {
eventType = mqbi::DispatcherEventType::e_PUT;
}
else if (event.isConfirmEvent()) {
eventType = mqbi::DispatcherEventType::e_CONFIRM;
}
else if (event.isRejectEvent()) {
eventType = mqbi::DispatcherEventType::e_REJECT;
// Dispatch the event
switch (event.type()) {
case bmqp::EventType::e_PUT:
case bmqp::EventType::e_CONFIRM:
case bmqp::EventType::e_REJECT: {
break; // BREAK
}
else {
default: {
BALL_LOG_ERROR << "#CLIENT_UNEXPECTED_EVENT " << description()
<< ": Unexpected event type: " << event;
return; // RETURN
}
}

// Dispatch the event
mqbi::DispatcherEvent* dispEvent = dispatcher()->getEvent(this);
bsl::shared_ptr<bdlbb::Blob> blobSp =
d_state.d_blobSpPool_p->getObject();
*blobSp = *(event.blob());
(*dispEvent).setType(eventType).setSource(this).setBlob(blobSp);

if (event.isPutEvent()) {
(*dispEvent).setSource(this).makePutEvent().setBlob(blobSp);
}
else if (event.isConfirmEvent()) {
(*dispEvent).setSource(this).makeConfirmEvent().setBlob(blobSp);
}
else if (event.isRejectEvent()) {
(*dispEvent).setSource(this).makeRejectEvent().setBlob(blobSp);
}

dispatcher()->dispatchEvent(dispEvent, this);
}
}
Expand Down Expand Up @@ -2995,23 +3000,23 @@ void ClientSession::onDispatcherEvent(const mqbi::DispatcherEvent& event)

switch (event.type()) {
case mqbi::DispatcherEventType::e_CONFIRM: {
onConfirmEvent(*(event.asConfirmEvent()));
onConfirmEvent(event.getAs<mqbi::DispatcherConfirmEvent>());
} break;
case mqbi::DispatcherEventType::e_REJECT: {
onRejectEvent(*(event.asRejectEvent()));
onRejectEvent(event.getAs<mqbi::DispatcherRejectEvent>());
} break;
case mqbi::DispatcherEventType::e_PUSH: {
onPushEvent(*(event.asPushEvent()));
onPushEvent(event.getAs<mqbi::DispatcherPushEvent>());
} break;
case mqbi::DispatcherEventType::e_PUT: {
onPutEvent(*(event.asPutEvent()));
onPutEvent(event.getAs<mqbi::DispatcherPutEvent>());
} break;
case mqbi::DispatcherEventType::e_ACK: {
onAckEvent(*(event.asAckEvent()));
onAckEvent(event.getAs<mqbi::DispatcherAckEvent>());
} break;
case mqbi::DispatcherEventType::e_CALLBACK: {
const mqbi::DispatcherCallbackEvent* realEvent =
event.asCallbackEvent();
&event.getAs<mqbi::DispatcherCallbackEvent>();

BSLS_ASSERT_SAFE(realEvent->callback());
flush(); // Flush any pending messages to guarantee ordering of events
Expand Down
31 changes: 16 additions & 15 deletions src/groups/mqb/mqba/mqba_clientsession.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,7 @@ class TestBench {
queueId);

mqbi::DispatcherEvent event(d_allocator_p);
event.setType(mqbi::DispatcherEventType::e_ACK)
.setAckMessage(ackMessage);
event.makeAckEvent().setAckMessage(ackMessage);

dispatch(event);
}
Expand Down Expand Up @@ -863,13 +862,13 @@ class TestBench {
cat);

mqbi::DispatcherEvent event(d_allocator_p);
event.setType(mqbi::DispatcherEventType::e_PUT)
.setIsRelay(true) // Relay message
event
.setSource(&d_cs) // DispatcherClient *value
.makePutEvent()
.setIsRelay(true) // Relay message
.setPutHeader(putHeader)
.setPartitionId(1) // d_state_p->partitionId()) // int value
.setBlob(eventBlob) // const bsl::shared_ptr<bdlbb::Blob>& value
.setCompressionAlgorithmType(cat);
.setPartitionId(1) // d_state_p->partitionId()) // int value
.setBlob(eventBlob); // const bsl::shared_ptr<bdlbb::Blob>& value

// Internal-ticket D167598037.
// Verify that PutMessageIterator does not change the input.
Expand Down Expand Up @@ -904,12 +903,13 @@ class TestBench {

mqbi::DispatcherEvent event(d_allocator_p);

event.setType(mqbi::DispatcherEventType::e_PUSH)
event
.setSource(&d_cs) // DispatcherClient *value
.setQueueId(queueId)
.makePushEvent()
.setBlob(blob)
.setGuid(msgGUID)
.setMessagePropertiesInfo(logic)
.setQueueId(queueId)
.setCompressionAlgorithmType(cat);

dispatch(event);
Expand Down Expand Up @@ -1917,9 +1917,10 @@ static void test9_newStylePush()
blobSp.createInplace(s_allocator_p, &tb.d_bufferFactory, s_allocator_p);
*blobSp = peb.blob();

putEvent.setType(mqbi::DispatcherEventType::e_PUT)
.setIsRelay(true) // Relay message
putEvent
.setSource(&tb.d_cs) // DispatcherClient *value
.makePutEvent()
.setIsRelay(true) // Relay message
.setPutHeader(putIt.header())
.setBlob(blobSp); // const bsl::shared_ptr<bdlbb::Blob>& value

Expand Down Expand Up @@ -2025,12 +2026,12 @@ static void test10_newStyleCompressedPush()
blobSp.createInplace(s_allocator_p, &tb.d_bufferFactory, s_allocator_p);
*blobSp = peb.blob();

putEvent.setType(mqbi::DispatcherEventType::e_PUT)
.setIsRelay(true) // Relay message
putEvent
.setSource(&tb.d_cs) // DispatcherClient *value
.makePutEvent()
.setIsRelay(true) // Relay message
.setPutHeader(putIt.header())
.setBlob(blobSp) // const bsl::shared_ptr<bdlbb::Blob>& value
.setCompressionAlgorithmType(bmqt::CompressionAlgorithmType::e_ZLIB);
.setBlob(blobSp); // const bsl::shared_ptr<bdlbb::Blob>& value

tb.dispatch(putEvent);

Expand Down
31 changes: 14 additions & 17 deletions src/groups/mqb/mqba/mqba_dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ void Dispatcher_Executor::post(const bsl::function<void()>& f) const
bmqc::MultiQueueThreadPool<mqbi::DispatcherEvent>::Event* event =
d_processorPool_p->getUnmanagedEvent();

event->object()
.setType(mqbi::DispatcherEventType::e_DISPATCHER)
.setCallback(mqbi::Dispatcher::voidToProcessorFunctor(f));
event->object().makeDispatcherEvent().setCallback(
mqbi::Dispatcher::voidToProcessorFunctor(f));

// submit the event
int rc = d_processorPool_p->enqueueEvent(event, d_processorHandle);
Expand Down Expand Up @@ -182,9 +181,9 @@ void Dispatcher_ClientExecutor::post(const bsl::function<void()>& f) const
processorPool()->getUnmanagedEvent();

event->object()
.setType(mqbi::DispatcherEventType::e_CALLBACK)
.setCallback(mqbi::Dispatcher::voidToProcessorFunctor(f))
.setDestination(const_cast<mqbi::DispatcherClient*>(d_client_p));
.setDestination(const_cast<mqbi::DispatcherClient*>(d_client_p))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little wary of this const cast, what's the reason this is okay?

.makeDispatcherEvent()
.setCallback(mqbi::Dispatcher::voidToProcessorFunctor(f));

// submit the event
int rc = processorPool()->enqueueEvent(event, processorHandle());
Expand Down Expand Up @@ -362,7 +361,7 @@ void Dispatcher::queueEventCb(mqbi::DispatcherClientType::Enum type,
if (event->object().type() ==
mqbi::DispatcherEventType::e_DISPATCHER) {
const mqbi::DispatcherDispatcherEvent* realEvent =
event->object().asDispatcherEvent();
&event->object().getAs<mqbi::DispatcherDispatcherEvent>();

// We must flush now (and irrespective of a callback actually being
// set on the event) to ensure the flushList is empty before
Expand Down Expand Up @@ -402,7 +401,7 @@ void Dispatcher::queueEventCb(mqbi::DispatcherClientType::Enum type,
if (event->object().type() ==
mqbi::DispatcherEventType::e_DISPATCHER) {
const mqbi::DispatcherDispatcherEvent* realEvent =
event->object().asDispatcherEvent();
&event->object().getAs<mqbi::DispatcherDispatcherEvent>();

if (realEvent->finalizeCallback()) {
BALL_LOG_TRACE << "Calling finalizeCallback on queue "
Expand Down Expand Up @@ -594,13 +593,13 @@ Dispatcher::registerClient(mqbi::DispatcherClient* client,
mqbi::DispatcherEvent* event =
&context.d_processorPool_mp->getUnmanagedEvent()->object();
(*event)
.setType(mqbi::DispatcherEventType::e_DISPATCHER)
.setDestination(client) // not needed
.makeDispatcherEvent()
.setCallback(
bdlf::BindUtil::bind(&Dispatcher::onNewClient,
this,
type,
bdlf::PlaceHolders::_1)) // processor
.setDestination(client); // not needed
bdlf::PlaceHolders::_1)); // processor
context.d_processorPool_mp->enqueueEvent(event, processor);
return processor; // RETURN
} // break;
Expand Down Expand Up @@ -691,7 +690,7 @@ void Dispatcher::execute(const mqbi::Dispatcher::ProcessorFunctor& functor,
if (processorPool[i] != 0) {
mqbi::DispatcherEvent* qEvent =
&processorPool[i]->getUnmanagedEvent()->object();
qEvent->setType(mqbi::DispatcherEventType::e_DISPATCHER)
qEvent->makeDispatcherEvent()
.setCallback(functor)
.setFinalizeCallback(doneCallback);
processorPool[i]->enqueueEventOnAllQueues(qEvent);
Expand All @@ -717,11 +716,9 @@ void Dispatcher::synchronize(mqbi::DispatcherClientType::Enum type,

bslmt::Semaphore semaphore;
mqbi::DispatcherEvent* event = getEvent(type);
(*event)
.setType(mqbi::DispatcherEventType::e_DISPATCHER)
.setCallback(
bdlf::BindUtil::bind(static_cast<PostFn>(&bslmt::Semaphore::post),
&semaphore));
(*event).makeDispatcherEvent().setCallback(
bdlf::BindUtil::bind(static_cast<PostFn>(&bslmt::Semaphore::post),
&semaphore));
dispatchEvent(event, type, handle);
semaphore.wait();
}
Expand Down
16 changes: 10 additions & 6 deletions src/groups/mqb/mqba/mqba_dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,14 @@ inline void Dispatcher::execute(const mqbi::Dispatcher::VoidFunctor& functor,
BSLS_ASSERT_SAFE(functor);

mqbi::DispatcherEvent* event = getEvent(client);

(*event).setType(type).setCallback(
mqbi::Dispatcher::voidToProcessorFunctor(functor));
if (type == mqbi::DispatcherEventType::e_DISPATCHER) {
(*event).makeDispatcherEvent().setCallback(
mqbi::Dispatcher::voidToProcessorFunctor(functor));
}
else {
(*event).makeCallbackEvent().setCallback(
mqbi::Dispatcher::voidToProcessorFunctor(functor));
}

dispatchEvent(event, client);
}
Expand All @@ -583,9 +588,8 @@ inline void Dispatcher::execute(const mqbi::Dispatcher::VoidFunctor& functor,

mqbi::DispatcherEvent* event = getEvent(client.clientType());

(*event)
.setType(mqbi::DispatcherEventType::e_DISPATCHER)
.setCallback(mqbi::Dispatcher::voidToProcessorFunctor(functor));
(*event).makeDispatcherEvent().setCallback(
mqbi::Dispatcher::voidToProcessorFunctor(functor));

dispatchEvent(event, client.clientType(), client.processorHandle());
}
Expand Down
Loading
Loading