|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | + |
| 3 | +package scheduler |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/twmb/franz-go/pkg/kadm" |
| 13 | + "github.com/twmb/franz-go/pkg/kgo" |
| 14 | + |
| 15 | + "github.com/grafana/loki/v3/pkg/kafka/testkafka" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + testTopic = "test" |
| 20 | + testGroup = "testgroup" |
| 21 | +) |
| 22 | + |
| 23 | +func TestKafkaGetGroupLag(t *testing.T) { |
| 24 | + ctx, cancel := context.WithCancelCause(context.Background()) |
| 25 | + t.Cleanup(func() { cancel(errors.New("test done")) }) |
| 26 | + |
| 27 | + _, addr := testkafka.CreateClusterWithoutCustomConsumerGroupsSupport(t, 3, testTopic) |
| 28 | + kafkaClient := mustKafkaClient(t, addr) |
| 29 | + admClient := kadm.NewClient(kafkaClient) |
| 30 | + |
| 31 | + const numRecords = 5 |
| 32 | + |
| 33 | + var producedRecords []kgo.Record |
| 34 | + kafkaTime := time.Now().Add(-12 * time.Hour) |
| 35 | + for i := int64(0); i < numRecords; i++ { |
| 36 | + kafkaTime = kafkaTime.Add(time.Minute) |
| 37 | + |
| 38 | + // Produce and keep records to partition 0. |
| 39 | + res := produceRecords(ctx, t, kafkaClient, kafkaTime, "1", testTopic, 0, []byte(`test value`)) |
| 40 | + rec, err := res.First() |
| 41 | + require.NoError(t, err) |
| 42 | + require.NotNil(t, rec) |
| 43 | + |
| 44 | + producedRecords = append(producedRecords, *rec) |
| 45 | + |
| 46 | + // Produce same records to partition 1 (this partition won't have any commits). |
| 47 | + produceRecords(ctx, t, kafkaClient, kafkaTime, "1", testTopic, 1, []byte(`test value`)) |
| 48 | + } |
| 49 | + require.Len(t, producedRecords, numRecords) |
| 50 | + |
| 51 | + // Commit last produced record from partition 0. |
| 52 | + rec := producedRecords[len(producedRecords)-1] |
| 53 | + offsets := make(kadm.Offsets) |
| 54 | + offsets.Add(kadm.Offset{ |
| 55 | + Topic: rec.Topic, |
| 56 | + Partition: rec.Partition, |
| 57 | + At: rec.Offset + 1, |
| 58 | + LeaderEpoch: rec.LeaderEpoch, |
| 59 | + }) |
| 60 | + err := admClient.CommitAllOffsets(ctx, testGroup, offsets) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + // Truncate partition 1 after second to last record to emulate the retention |
| 64 | + // Note Kafka sets partition's start offset to the requested offset. Any records within the segment before the requested offset can no longer be read. |
| 65 | + // Note the difference between DeleteRecords and DeleteOffsets in kadm docs. |
| 66 | + deleteRecOffsets := make(kadm.Offsets) |
| 67 | + deleteRecOffsets.Add(kadm.Offset{ |
| 68 | + Topic: testTopic, |
| 69 | + Partition: 1, |
| 70 | + At: numRecords - 2, |
| 71 | + }) |
| 72 | + _, err = admClient.DeleteRecords(ctx, deleteRecOffsets) |
| 73 | + require.NoError(t, err) |
| 74 | + |
| 75 | + getTopicPartitionLag := func(t *testing.T, lag kadm.GroupLag, topic string, part int32) int64 { |
| 76 | + l, ok := lag.Lookup(topic, part) |
| 77 | + require.True(t, ok) |
| 78 | + return l.Lag |
| 79 | + } |
| 80 | + |
| 81 | + t.Run("fallbackOffset=milliseconds", func(t *testing.T) { |
| 82 | + // get the timestamp of the last produced record |
| 83 | + rec := producedRecords[len(producedRecords)-1] |
| 84 | + fallbackOffset := rec.Timestamp.Add(-time.Millisecond).UnixMilli() |
| 85 | + groupLag, err := GetGroupLag(ctx, admClient, testTopic, testGroup, fallbackOffset) |
| 86 | + require.NoError(t, err) |
| 87 | + |
| 88 | + require.EqualValues(t, 0, getTopicPartitionLag(t, groupLag, testTopic, 0), "partition 0 must have no lag") |
| 89 | + require.EqualValues(t, 1, getTopicPartitionLag(t, groupLag, testTopic, 1), "partition 1 must fall back to known record and get its lag from there") |
| 90 | + require.EqualValues(t, 0, getTopicPartitionLag(t, groupLag, testTopic, 2), "partition 2 has no data and must have no lag") |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("fallbackOffset=before-earliest", func(t *testing.T) { |
| 94 | + // get the timestamp of third to last produced record (record before earliest in partition 1) |
| 95 | + rec := producedRecords[len(producedRecords)-3] |
| 96 | + fallbackOffset := rec.Timestamp.Add(-time.Millisecond).UnixMilli() |
| 97 | + groupLag, err := GetGroupLag(ctx, admClient, testTopic, testGroup, fallbackOffset) |
| 98 | + require.NoError(t, err) |
| 99 | + |
| 100 | + require.EqualValues(t, 0, getTopicPartitionLag(t, groupLag, testTopic, 0), "partition 0 must have no lag") |
| 101 | + require.EqualValues(t, 2, getTopicPartitionLag(t, groupLag, testTopic, 1), "partition 1 must fall back to earliest and get its lag from there") |
| 102 | + require.EqualValues(t, 0, getTopicPartitionLag(t, groupLag, testTopic, 2), "partition 2 has no data and must have no lag") |
| 103 | + }) |
| 104 | + |
| 105 | + t.Run("fallbackOffset=0", func(t *testing.T) { |
| 106 | + groupLag, err := GetGroupLag(ctx, admClient, testTopic, testGroup, 0) |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + require.EqualValues(t, 0, getTopicPartitionLag(t, groupLag, testTopic, 0), "partition 0 must have no lag") |
| 110 | + require.EqualValues(t, 2, getTopicPartitionLag(t, groupLag, testTopic, 1), "partition 1 must fall back to the earliest and get its lag from there") |
| 111 | + require.EqualValues(t, 0, getTopicPartitionLag(t, groupLag, testTopic, 2), "partition 2 has no data and must have no lag") |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("group=unknown", func(t *testing.T) { |
| 115 | + groupLag, err := GetGroupLag(ctx, admClient, testTopic, "unknown", 0) |
| 116 | + require.NoError(t, err) |
| 117 | + |
| 118 | + // This group doesn't have any commits, so it must calc its lag from the fallback. |
| 119 | + require.EqualValues(t, numRecords, getTopicPartitionLag(t, groupLag, testTopic, 0)) |
| 120 | + require.EqualValues(t, 2, getTopicPartitionLag(t, groupLag, testTopic, 1), "partition 1 must fall back to the earliest and get its lag from there") |
| 121 | + require.EqualValues(t, 0, getTopicPartitionLag(t, groupLag, testTopic, 2), "partition 2 has no data and must have no lag") |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +func mustKafkaClient(t *testing.T, addrs ...string) *kgo.Client { |
| 126 | + writeClient, err := kgo.NewClient( |
| 127 | + kgo.SeedBrokers(addrs...), |
| 128 | + kgo.AllowAutoTopicCreation(), |
| 129 | + // We will choose the partition of each record. |
| 130 | + kgo.RecordPartitioner(kgo.ManualPartitioner()), |
| 131 | + ) |
| 132 | + require.NoError(t, err) |
| 133 | + t.Cleanup(writeClient.Close) |
| 134 | + return writeClient |
| 135 | +} |
| 136 | + |
| 137 | +func produceRecords( |
| 138 | + ctx context.Context, |
| 139 | + t *testing.T, |
| 140 | + kafkaClient *kgo.Client, |
| 141 | + ts time.Time, |
| 142 | + userID string, |
| 143 | + topic string, |
| 144 | + part int32, |
| 145 | + val []byte, |
| 146 | +) kgo.ProduceResults { |
| 147 | + rec := &kgo.Record{ |
| 148 | + Timestamp: ts, |
| 149 | + Key: []byte(userID), |
| 150 | + Value: val, |
| 151 | + Topic: topic, |
| 152 | + Partition: part, // samples in this batch are split between N partitions |
| 153 | + } |
| 154 | + produceResult := kafkaClient.ProduceSync(ctx, rec) |
| 155 | + require.NoError(t, produceResult.FirstErr()) |
| 156 | + return produceResult |
| 157 | +} |
| 158 | + |
| 159 | +func commitOffset(ctx context.Context, t *testing.T, kafkaClient *kgo.Client, group string, offset kadm.Offset) { |
| 160 | + offsets := make(kadm.Offsets) |
| 161 | + offsets.Add(offset) |
| 162 | + err := kadm.NewClient(kafkaClient).CommitAllOffsets(ctx, group, offsets) |
| 163 | + require.NoError(t, err) |
| 164 | +} |
0 commit comments