only recreate if we need to seek backward (requested offset < current offset), not on any mismatch

This commit is contained in:
chrislu
2025-10-14 00:26:55 -07:00
parent 6947d906a8
commit 6ef2f66198
4 changed files with 152 additions and 104 deletions
+3 -3
View File
@@ -108,7 +108,7 @@ func testConsumerGroupResumption(t *testing.T, addr, topic, groupID string) {
// Verify total consumption
totalConsumed := len(consumed1) + len(consumed2)
t.Logf("=== Verification: Total consumed %d messages (expected %d) ===", totalConsumed, len(messages))
// Check for duplicates
offsetsSeen := make(map[int64]bool)
duplicateCount := 0
@@ -119,11 +119,11 @@ func testConsumerGroupResumption(t *testing.T, addr, topic, groupID string) {
}
offsetsSeen[msg.Offset] = true
}
if duplicateCount > 0 {
t.Logf("ERROR: Found %d duplicate messages", duplicateCount)
}
testutil.AssertEqual(t, len(messages), totalConsumed, "Should consume all messages after restart")
t.Logf("SUCCESS: Consumer group resumption test completed - no duplicates, all messages consumed exactly once")
+7
View File
@@ -140,6 +140,10 @@ func (k *KafkaGoClient) ConsumeWithGroup(topicName, groupID string, expectedCoun
})
defer reader.Close()
// Log the initial offset position
offset := reader.Offset()
k.t.Logf("Consumer group reader created for group %s, initial offset: %d", groupID, offset)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
@@ -151,14 +155,17 @@ func (k *KafkaGoClient) ConsumeWithGroup(topicName, groupID string, expectedCoun
return messages, fmt.Errorf("read message %d: %w", i, err)
}
messages = append(messages, msg)
k.t.Logf(" Fetched message %d: offset=%d, partition=%d", i, msg.Offset, msg.Partition)
// Commit with simple retry to handle transient connection churn
var commitErr error
for attempt := 0; attempt < 3; attempt++ {
commitErr = reader.CommitMessages(ctx, msg)
if commitErr == nil {
k.t.Logf(" ✓ Committed offset %d (attempt %d)", msg.Offset, attempt+1)
break
}
k.t.Logf(" × Commit attempt %d failed for offset %d: %v", attempt+1, msg.Offset, commitErr)
// brief backoff
time.Sleep(time.Duration(50*(1<<attempt)) * time.Millisecond)
}