Skip to content

Commit

Permalink
[Java] Fix an off-by-one error while searching for counters.
Browse files Browse the repository at this point in the history
  • Loading branch information
vyazelenko committed Jan 13, 2025
1 parent 2d0f69a commit 4bca6e6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,21 @@ public static int findCounterIdByRecording(
{
final DirectBuffer buffer = countersReader.metaDataBuffer();

for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
for (int counterId = 0, maxId = countersReader.maxCounterId(); counterId <= maxId; counterId++)
{
final int counterState = countersReader.getCounterState(i);
final int counterState = countersReader.getCounterState(counterId);
if (RECORD_ALLOCATED == counterState)
{
if (countersReader.getCounterTypeId(i) == RECORDING_POSITION_TYPE_ID)
if (countersReader.getCounterTypeId(counterId) == RECORDING_POSITION_TYPE_ID)
{
final int keyOffset = metaDataOffset(i) + KEY_OFFSET;
final int keyOffset = metaDataOffset(counterId) + KEY_OFFSET;
if (buffer.getLong(keyOffset + RECORDING_ID_OFFSET) == recordingId)
{
final int sourceIdentityLength = buffer.getInt(keyOffset + SOURCE_IDENTITY_LENGTH_OFFSET);
final int archiveIdOffset = keyOffset + SOURCE_IDENTITY_OFFSET + sourceIdentityLength;
if (Aeron.NULL_VALUE == archiveId || buffer.getLong(archiveIdOffset) == archiveId)
{
return i;
return counterId;
}
}
}
Expand Down Expand Up @@ -211,21 +211,21 @@ public static int findCounterIdBySession(
{
final DirectBuffer buffer = countersReader.metaDataBuffer();

for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
for (int counterId = 0, maxId = countersReader.maxCounterId(); counterId <= maxId; counterId++)
{
final int counterState = countersReader.getCounterState(i);
final int counterState = countersReader.getCounterState(counterId);
if (RECORD_ALLOCATED == counterState)
{
if (countersReader.getCounterTypeId(i) == RECORDING_POSITION_TYPE_ID)
if (countersReader.getCounterTypeId(counterId) == RECORDING_POSITION_TYPE_ID)
{
final int keyOffset = metaDataOffset(i) + KEY_OFFSET;
final int keyOffset = metaDataOffset(counterId) + KEY_OFFSET;
if (buffer.getInt(keyOffset + SESSION_ID_OFFSET) == sessionId)
{
final int sourceIdentityLength = buffer.getInt(keyOffset + SOURCE_IDENTITY_LENGTH_OFFSET);
final int archiveIdOffset = keyOffset + SOURCE_IDENTITY_OFFSET + sourceIdentityLength;
if (Aeron.NULL_VALUE == archiveId || buffer.getLong(archiveIdOffset) == archiveId)
{
return i;
return counterId;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public static int findCounterIdByRegistrationId(
{
final DirectBuffer buffer = countersReader.metaDataBuffer();

for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
for (int counterId = 0, maxId = countersReader.maxCounterId(); counterId <= maxId; counterId++)
{
final int counterState = countersReader.getCounterState(i);
final int counterState = countersReader.getCounterState(counterId);
if (counterState == RECORD_ALLOCATED)
{
if (countersReader.getCounterTypeId(i) == counterTypeId &&
buffer.getLong(metaDataOffset(i) + KEY_OFFSET + REGISTRATION_ID_OFFSET) == registrationId)
if (countersReader.getCounterTypeId(counterId) == counterTypeId &&
buffer.getLong(metaDataOffset(counterId) + KEY_OFFSET + REGISTRATION_ID_OFFSET) == registrationId)
{
return i;
return counterId;
}
}
else if (RECORD_UNUSED == counterState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,18 @@ public static List<String> findAddresses(
final ArrayList<String> bindings = new ArrayList<>(2);
final DirectBuffer buffer = countersReader.metaDataBuffer();

for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
for (int counterId = 0, maxId = countersReader.maxCounterId(); counterId <= maxId; counterId++)
{
final int counterState = countersReader.getCounterState(i);
final int counterState = countersReader.getCounterState(counterId);
if (RECORD_ALLOCATED == counterState)
{
if (countersReader.getCounterTypeId(i) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID)
if (countersReader.getCounterTypeId(counterId) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID)
{
final int recordOffset = CountersReader.metaDataOffset(i);
final int recordOffset = CountersReader.metaDataOffset(counterId);
final int keyIndex = recordOffset + CountersReader.KEY_OFFSET;

if (channelStatusId == buffer.getInt(keyIndex + CHANNEL_STATUS_ID_OFFSET) &&
ChannelEndpointStatus.ACTIVE == countersReader.getCounterValue(i))
ChannelEndpointStatus.ACTIVE == countersReader.getCounterValue(counterId))
{
final int length = buffer.getInt(keyIndex + LOCAL_SOCKET_ADDRESS_LENGTH_OFFSET);
if (length > 0)
Expand Down Expand Up @@ -188,18 +188,18 @@ public static String findAddress(
{
final DirectBuffer buffer = countersReader.metaDataBuffer();

for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
for (int counterId = 0, maxId = countersReader.maxCounterId(); counterId <= maxId; counterId++)
{
final int counterState = countersReader.getCounterState(i);
final int counterState = countersReader.getCounterState(counterId);
if (RECORD_ALLOCATED == counterState)
{
if (countersReader.getCounterTypeId(i) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID)
if (countersReader.getCounterTypeId(counterId) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID)
{
final int recordOffset = CountersReader.metaDataOffset(i);
final int recordOffset = CountersReader.metaDataOffset(counterId);
final int keyIndex = recordOffset + CountersReader.KEY_OFFSET;

if (channelStatusId == buffer.getInt(keyIndex + CHANNEL_STATUS_ID_OFFSET) &&
ChannelEndpointStatus.ACTIVE == countersReader.getCounterValue(i))
ChannelEndpointStatus.ACTIVE == countersReader.getCounterValue(counterId))
{
final int length = buffer.getInt(keyIndex + LOCAL_SOCKET_ADDRESS_LENGTH_OFFSET);
if (length > 0)
Expand Down Expand Up @@ -234,12 +234,12 @@ public static int findNumberOfAddressesByRegistrationId(
{
int result = 0;

for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
for (int counterId = 0, maxId = countersReader.maxCounterId(); counterId <= maxId; counterId++)
{
final int counterState = countersReader.getCounterState(i);
final int counterState = countersReader.getCounterState(counterId);
if (counterState == RECORD_ALLOCATED &&
countersReader.getCounterTypeId(i) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID &&
countersReader.getCounterRegistrationId(i) == registrationId)
countersReader.getCounterTypeId(counterId) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID &&
countersReader.getCounterRegistrationId(counterId) == registrationId)
{
result++;
}
Expand All @@ -263,18 +263,18 @@ public static boolean isActive(final CountersReader countersReader, final int ch
{
final DirectBuffer buffer = countersReader.metaDataBuffer();

for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
for (int counterId = 0, maxId = countersReader.maxCounterId(); counterId <= maxId; counterId++)
{
final int counterState = countersReader.getCounterState(i);
final int counterState = countersReader.getCounterState(counterId);
if (RECORD_ALLOCATED == counterState)
{
if (countersReader.getCounterTypeId(i) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID)
if (countersReader.getCounterTypeId(counterId) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID)
{
final int recordOffset = CountersReader.metaDataOffset(i);
final int recordOffset = CountersReader.metaDataOffset(counterId);
final int keyIndex = recordOffset + CountersReader.KEY_OFFSET;

if (channelStatusId == buffer.getInt(keyIndex + CHANNEL_STATUS_ID_OFFSET) &&
ChannelEndpointStatus.ACTIVE == countersReader.getCounterValue(i))
ChannelEndpointStatus.ACTIVE == countersReader.getCounterValue(counterId))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2055,12 +2055,12 @@ private static long networkPublicationCorrelationId(final NetworkPublication pub

private static AtomicCounter clientHeartbeatCounter(final CountersReader countersReader)
{
for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
for (int counterId = 0, maxId = countersReader.maxCounterId(); counterId <= maxId; counterId++)
{
final int counterState = countersReader.getCounterState(i);
if (counterState == RECORD_ALLOCATED && countersReader.getCounterTypeId(i) == HEARTBEAT_TYPE_ID)
final int counterState = countersReader.getCounterState(counterId);
if (counterState == RECORD_ALLOCATED && countersReader.getCounterTypeId(counterId) == HEARTBEAT_TYPE_ID)
{
return new AtomicCounter(countersReader.valuesBuffer(), i);
return new AtomicCounter(countersReader.valuesBuffer(), counterId);
}
else if (RECORD_UNUSED == counterState)
{
Expand Down
10 changes: 5 additions & 5 deletions aeron-system-tests/src/test/java/io/aeron/FlowControlTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ static int findCounterIdByRegistrationId(
{
final DirectBuffer buffer = countersReader.metaDataBuffer();

for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
for (int counterId = 0, maxId = countersReader.maxCounterId(); counterId <= maxId; counterId++)
{
if (countersReader.getCounterState(i) == RECORD_ALLOCATED &&
countersReader.getCounterTypeId(i) == counterTypeId)
if (countersReader.getCounterState(counterId) == RECORD_ALLOCATED &&
countersReader.getCounterTypeId(counterId) == counterTypeId)
{
final int recordOffset = CountersReader.metaDataOffset(i);
final int recordOffset = CountersReader.metaDataOffset(counterId);
if (buffer.getLong(recordOffset + KEY_OFFSET) == registrationId)
{
return i;
return counterId;
}
}
}
Expand Down

0 comments on commit 4bca6e6

Please sign in to comment.