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] DistinctErrorLog lock free. #325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class DistinctErrorLog
private final EpochClock clock;
private final AtomicBuffer buffer;
private final Charset charset;
private DistinctObservation[] distinctObservations = new DistinctObservation[0];
private volatile DistinctObservation[] distinctObservations = new DistinctObservation[0];

/**
* Create a new error log that will be written to a provided {@link AtomicBuffer}.
Expand Down Expand Up @@ -157,38 +157,83 @@ public Charset charset()
*/
public boolean record(final Throwable observation)
{
final long timestampMs;
DistinctObservation distinctObservation;
final long timestampMs = clock.time();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the above logic should be moved into the updateLastObservationTimestamp method.


timestampMs = clock.time();
synchronized (this)
DistinctObservation distinctObservation;
DistinctObservation[] currentDistinctObservations;
int searchIndex = 0;
for (; ; )
{
distinctObservation = find(distinctObservations, observation);
currentDistinctObservations = this.distinctObservations;
distinctObservation = find(currentDistinctObservations, searchIndex, observation);
searchIndex = currentDistinctObservations.length;

if (null == distinctObservation)
if (distinctObservation != null)
{
// A distinctObservation was found
break;
}
else if (currentDistinctObservations == distinctObservations)
{
distinctObservation = newObservation(timestampMs, observation);
if (INSUFFICIENT_SPACE == distinctObservation)
// A distinctObservation was not found, and now an attempt is made to create the new observation
synchronized (this)
{
return false;
// One more find is needed, because it could be a new observation was added prior to
// obtaining the lock.
if (currentDistinctObservations != distinctObservations)
{
distinctObservation = find(distinctObservations, searchIndex, observation);
}

if (null == distinctObservation)
{
distinctObservation = newObservation(timestampMs, observation);
if (INSUFFICIENT_SPACE == distinctObservation)
{
return false;
}
}
}

break;
}
}

final int offset = distinctObservation.offset;
buffer.getAndAddInt(offset + OBSERVATION_COUNT_OFFSET, 1);
buffer.putLongRelease(offset + LAST_OBSERVATION_TIMESTAMP_OFFSET, timestampMs);

updateLastObservationTimestamp(offset, timestampMs);
return true;
}

private void updateLastObservationTimestamp(final int offset, final long timestampMs)
{
// A cas loop to ensure that the timestamp is monotonic increasing.
for (; ; )
{
final long current = buffer.getLongVolatile(offset + LAST_OBSERVATION_TIMESTAMP_OFFSET);
if (current < timestampMs)
{
if (buffer.compareAndSetLong(offset + LAST_OBSERVATION_TIMESTAMP_OFFSET, current, timestampMs))
{
break;
}
}
else
{
break;
}
}
}

private static DistinctObservation find(
final DistinctObservation[] existingObservations, final Throwable observation)
final DistinctObservation[] existingObservations, final int startIndex, final Throwable observation)
{
DistinctObservation existingObservation = null;

for (final DistinctObservation o : existingObservations)
for (int k = startIndex; k < existingObservations.length; k++)
{
final DistinctObservation o = existingObservations[k];
if (equals(o.throwable, observation))
{
existingObservation = o;
Expand Down
Loading