Skip to content

Commit

Permalink
Improve AsyncAppender throughput (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
swebb2066 authored Apr 30, 2024
1 parent a5b9421 commit e74b2a8
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/main/cpp/asyncappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ void AsyncAppender::append(const spi::LoggingEventPtr& event, Pool& p)
priv->bufferNotEmpty.notify_all();
break;
}
priv->bufferNotEmpty.notify_all();
//
// Following code is only reachable if buffer is full or eventCount has overflowed
//
Expand Down Expand Up @@ -506,32 +507,34 @@ void AsyncAppender::dispatch()
Pool p;
LoggingEventList events;
events.reserve(priv->bufferSize);
//
// process events after lock on buffer is released.
//
for (int count = 0; count < 2 && priv->dispatchedCount == priv->commitCount; ++count)
std::this_thread::yield(); // Wait a bit
if (priv->dispatchedCount == priv->commitCount)
{
std::unique_lock<std::mutex> lock(priv->bufferMutex);
priv->bufferNotEmpty.wait(lock, [this]() -> bool
{ return priv->dispatchedCount != priv->commitCount || priv->closed; }
);
isActive = !priv->closed;
}
isActive = !priv->closed;

while (events.size() < priv->bufferSize && priv->dispatchedCount != priv->commitCount)
{
auto index = priv->dispatchedCount % priv->buffer.size();
const auto& data = priv->buffer[index];
events.push_back(data.event);
if (data.pendingCount < pendingCountHistogram.size())
++pendingCountHistogram[data.pendingCount];
++priv->dispatchedCount;
}
while (events.size() < priv->bufferSize && priv->dispatchedCount != priv->commitCount)
{
auto index = priv->dispatchedCount % priv->buffer.size();
const auto& data = priv->buffer[index];
events.push_back(data.event);
if (data.pendingCount < pendingCountHistogram.size())
++pendingCountHistogram[data.pendingCount];
++priv->dispatchedCount;
}
priv->bufferNotFull.notify_all();
{
std::lock_guard<std::mutex> lock(priv->bufferMutex);
for (auto discardItem : priv->discardMap)
{
events.push_back(discardItem.second.createEvent(p));
}

priv->discardMap.clear();
priv->bufferNotFull.notify_all();
}

for (auto item : events)
Expand Down

0 comments on commit e74b2a8

Please sign in to comment.