Skip to content

Commit

Permalink
[Java] Encode cluster event in an SBE compatible fashion.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed May 26, 2019
1 parent 7af00bd commit 7f4410a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.agrona.BitUtil;
import org.agrona.MutableDirectBuffer;

import static org.agrona.BitUtil.SIZE_OF_LONG;

final class ClusterEventDissector
{
static void electionStateChange(
Expand All @@ -26,9 +28,9 @@ static void electionStateChange(
final int offset,
final StringBuilder builder)
{
final String stateName = buffer.getStringAscii(offset);
final long timestampMs = buffer.getLong(offset + BitUtil.SIZE_OF_INT + stateName.length());
builder.append("CLUSTER: Election State -> ").append(stateName).append(' ').append(timestampMs);
final long timestampMs = buffer.getLong(offset);
final String stateChange = buffer.getStringAscii(offset + SIZE_OF_LONG);
builder.append("CLUSTER: Election State -> ").append(stateChange).append(' ').append(timestampMs);
}

static void newLeadershipTerm(
Expand All @@ -39,13 +41,13 @@ static void newLeadershipTerm(
{
int relativeOffset = offset;
final long logLeadershipTermId = buffer.getLong(relativeOffset);
relativeOffset += BitUtil.SIZE_OF_LONG;
relativeOffset += SIZE_OF_LONG;
final long logPosition = buffer.getLong(relativeOffset);
relativeOffset += BitUtil.SIZE_OF_LONG;
relativeOffset += SIZE_OF_LONG;
final long leadershipTermId = buffer.getLong(relativeOffset);
relativeOffset += BitUtil.SIZE_OF_LONG;
relativeOffset += SIZE_OF_LONG;
final long maxLogPosition = buffer.getLong(relativeOffset);
relativeOffset += BitUtil.SIZE_OF_LONG;
relativeOffset += SIZE_OF_LONG;
final int leaderMemberId = buffer.getInt(relativeOffset);
relativeOffset += BitUtil.SIZE_OF_INT;
final int logSessionId = buffer.getInt(relativeOffset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ static int encodeElectionStateChange(
final Election.State newState,
final long nowMs)
{
final int stringLength = oldState.name().length() + " -> ".length() + newState.name().length();
encodedBuffer.putLong(0, nowMs);
int offset = BitUtil.SIZE_OF_LONG;

encodedBuffer.putInt(0, stringLength);
int offset = BitUtil.SIZE_OF_INT;
final int stringLength = oldState.name().length() + " -> ".length() + newState.name().length();
encodedBuffer.putInt(offset, stringLength);
offset += BitUtil.SIZE_OF_INT;

offset += encodedBuffer.putStringWithoutLengthAscii(offset, oldState.name());
offset += encodedBuffer.putStringWithoutLengthAscii(offset, " -> ");
offset += encodedBuffer.putStringWithoutLengthAscii(offset, newState.name());

encodedBuffer.putLong(offset, nowMs);

return offset + BitUtil.SIZE_OF_LONG;
return offset;
}

static int newLeadershipTerm(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static io.aeron.agent.EventConfiguration.EVENT_READER_FRAME_LIMIT;
import static io.aeron.agent.EventConfiguration.EVENT_RING_BUFFER;
import static org.agrona.BitUtil.SIZE_OF_LONG;
import static org.mockito.Mockito.mock;

import io.aeron.archive.Archive;
Expand Down Expand Up @@ -153,7 +154,7 @@ public void onMessage(final int msgTypeId, final MutableDirectBuffer buffer, fin

if (ClusterEventLogger.toEventCodeId(ClusterEventCode.ELECTION_STATE_CHANGE) == msgTypeId)
{
final String stateChange = buffer.getStringAscii(index);
final String stateChange = buffer.getStringAscii(index + SIZE_OF_LONG);
if (stateChange.endsWith("CLOSE"))
{
LATCH.countDown();
Expand Down

0 comments on commit 7f4410a

Please sign in to comment.