Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,19 @@ public OffsetMetadataManager build() {
private final TimelineHashMap<String, TimelineHashSet<Long>> openTransactionsByGroup;

private class Offsets {
/**
* Whether to preserve empty entries for groups when removing offsets.
* We use this to keep track of the groups associated with pending transactions.
*/
private final boolean preserveGroups;

/**
* The offsets keyed by group id, topic name and partition id.
*/
private final TimelineHashMap<String, TimelineHashMap<String, TimelineHashMap<Integer, OffsetAndMetadata>>> offsetsByGroup;

private Offsets() {
private Offsets(boolean preserveGroups) {
this.preserveGroups = preserveGroups;
this.offsetsByGroup = new TimelineHashMap<>(snapshotRegistry, 0);
Comment on lines +212 to 214
Copy link

Choose a reason for hiding this comment

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

Boolean Flag Parameter

Constructor with boolean parameter creates unclear API. The boolean flag parameter makes code less readable and intent harder to understand at call sites. Consider using named factory methods or builder pattern.

}

Expand Down Expand Up @@ -256,7 +263,7 @@ private OffsetAndMetadata remove(
if (partitionOffsets.isEmpty())
topicOffsets.remove(topic);

if (topicOffsets.isEmpty())
if (!preserveGroups && topicOffsets.isEmpty())
offsetsByGroup.remove(groupId);
Comment on lines +266 to 267
Copy link

Choose a reason for hiding this comment

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

Group Preservation Implementation

Original implementation unconditionally removes groups when topic offsets are empty, failing to preserve groups with pending transactions. New logic adds a preserveGroups flag to conditionally maintain empty groups with pending transactions.


return removedValue;
Expand All @@ -278,7 +285,7 @@ private OffsetAndMetadata remove(
this.groupMetadataManager = groupMetadataManager;
this.config = config;
this.metrics = metrics;
this.offsets = new Offsets();
this.offsets = new Offsets(false);
this.pendingTransactionalOffsets = new TimelineHashMap<>(snapshotRegistry, 0);
this.openTransactionsByGroup = new TimelineHashMap<>(snapshotRegistry, 0);
}
Expand Down Expand Up @@ -851,7 +858,7 @@ public boolean cleanupExpiredOffsets(String groupId, List<CoordinatorRecord> rec
TimelineHashMap<String, TimelineHashMap<Integer, OffsetAndMetadata>> offsetsByTopic =
offsets.offsetsByGroup.get(groupId);
if (offsetsByTopic == null) {
return true;
return !openTransactionsByGroup.containsKey(groupId);
Copy link

Choose a reason for hiding this comment

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

Group Deletion Logic

Original logic incorrectly returns true (allowing group deletion) when offsetsByTopic is null, without checking for pending transactions. This could lead to data inconsistency by deleting groups with pending transactional offsets.

Copy link

Choose a reason for hiding this comment

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

Inconsistent Group Preservation

The code returns true (allowing group deletion) when no open transactions exist, but doesn't check for pending transactional offsets that might be deleted. This could lead to data loss if a group with deleted pending offsets is removed before transaction completion.

Standards
  • ISO-IEC-25010-Reliability-Fault-Tolerance
  • ISO-IEC-25010-Functional-Correctness-Appropriateness
  • DbC-State-Consistency

}
Comment on lines +861 to 862
Copy link

Choose a reason for hiding this comment

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

Incomplete Transaction Handling

The condition change prevents group deletion only when transactions are open, but doesn't check for pending transactional offsets. This creates inconsistency with preserveGroups flag implementation.


// We expect the group to exist.
Expand Down Expand Up @@ -995,7 +1002,7 @@ public void replay(
// offsets store. Pending offsets there are moved to the main store when
// the transaction is committed; or removed when the transaction is aborted.
pendingTransactionalOffsets
.computeIfAbsent(producerId, __ -> new Offsets())
.computeIfAbsent(producerId, __ -> new Offsets(true))
.put(
groupId,
topic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,103 @@ public void testCleanupExpiredOffsetsWithPendingTransactionalOffsets() {
assertEquals(List.of(), records);
}

@Test
public void testCleanupExpiredOffsetsWithDeletedPendingTransactionalOffsets() {
GroupMetadataManager groupMetadataManager = mock(GroupMetadataManager.class);
Group group = mock(Group.class);

OffsetMetadataManagerTestContext context = new OffsetMetadataManagerTestContext.Builder()
.withGroupMetadataManager(groupMetadataManager)
.withOffsetsRetentionMinutes(1)
.build();

long commitTimestamp = context.time.milliseconds();

context.commitOffset("group-id", "foo", 0, 100L, 0, commitTimestamp);
context.commitOffset(10L, "group-id", "foo", 1, 101L, 0, commitTimestamp + 500);

when(groupMetadataManager.group("group-id")).thenReturn(group);
when(group.offsetExpirationCondition()).thenReturn(Optional.of(
new OffsetExpirationConditionImpl(offsetAndMetadata -> offsetAndMetadata.commitTimestampMs)));
when(group.isSubscribedToTopic("foo")).thenReturn(false);

// Delete the pending transactional offset.
OffsetDeleteRequestData.OffsetDeleteRequestTopicCollection requestTopicCollection =
new OffsetDeleteRequestData.OffsetDeleteRequestTopicCollection(List.of(
new OffsetDeleteRequestData.OffsetDeleteRequestTopic()
.setName("foo")
.setPartitions(List.of(
new OffsetDeleteRequestData.OffsetDeleteRequestPartition().setPartitionIndex(1)
))
).iterator());
CoordinatorResult<OffsetDeleteResponseData, CoordinatorRecord> result = context.deleteOffsets(
new OffsetDeleteRequestData()
.setGroupId("group-id")
.setTopics(requestTopicCollection)
);
List<CoordinatorRecord> expectedRecords = List.of(
GroupCoordinatorRecordHelpers.newOffsetCommitTombstoneRecord("group-id", "foo", 1)
);
assertEquals(expectedRecords, result.records());

context.time.sleep(Duration.ofMinutes(1).toMillis());

// The group should not be deleted because it has a pending transaction.
expectedRecords = List.of(
GroupCoordinatorRecordHelpers.newOffsetCommitTombstoneRecord("group-id", "foo", 0)
);
List<CoordinatorRecord> records = new ArrayList<>();
assertFalse(context.cleanupExpiredOffsets("group-id", records));
assertEquals(expectedRecords, records);

// Commit the ongoing transaction.
context.replayEndTransactionMarker(10L, TransactionResult.COMMIT);

// The group should be deletable now.
context.commitOffset("group-id", "foo", 0, 100L, 0, commitTimestamp);
context.time.sleep(Duration.ofMinutes(1).toMillis());

records = new ArrayList<>();
assertTrue(context.cleanupExpiredOffsets("group-id", records));
assertEquals(expectedRecords, records);
}

@Test
public void testCleanupExpiredOffsetsWithPendingTransactionalOffsetsOnly() {
GroupMetadataManager groupMetadataManager = mock(GroupMetadataManager.class);
Group group = mock(Group.class);

OffsetMetadataManagerTestContext context = new OffsetMetadataManagerTestContext.Builder()
.withGroupMetadataManager(groupMetadataManager)
.withOffsetsRetentionMinutes(1)
.build();

long commitTimestamp = context.time.milliseconds();

context.commitOffset("group-id", "foo", 0, 100L, 0, commitTimestamp);
context.commitOffset(10L, "group-id", "foo", 1, 101L, 0, commitTimestamp + 500);

context.time.sleep(Duration.ofMinutes(1).toMillis());

when(groupMetadataManager.group("group-id")).thenReturn(group);
when(group.offsetExpirationCondition()).thenReturn(Optional.of(
new OffsetExpirationConditionImpl(offsetAndMetadata -> offsetAndMetadata.commitTimestampMs)));
when(group.isSubscribedToTopic("foo")).thenReturn(false);

// foo-0 is expired, but the group is not deleted beacuse it has pending transactional offset commits.
List<CoordinatorRecord> expectedRecords = List.of(
GroupCoordinatorRecordHelpers.newOffsetCommitTombstoneRecord("group-id", "foo", 0)
);
List<CoordinatorRecord> records = new ArrayList<>();
assertFalse(context.cleanupExpiredOffsets("group-id", records));
assertEquals(expectedRecords, records);

// No offsets are expired, and the group is still not deleted because it has pending transactional offset commits.
records = new ArrayList<>();
assertFalse(context.cleanupExpiredOffsets("group-id", records));
assertEquals(List.of(), records);
}

private static OffsetFetchResponseData.OffsetFetchResponsePartitions mkOffsetPartitionResponse(
int partition,
long offset,
Expand Down
Loading