Skip to content

Commit ffa4211

Browse files
committed
Fix race condition in message tracker
1 parent a082b4e commit ffa4211

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

e2e/endtoend_message.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ type EndToEndMessage struct {
1414
Timestamp int64 `json:"createdUtcNs"` // when the message was created, unix nanoseconds
1515

1616
// The following properties are only used within the message tracker
17-
partition int
18-
state int
17+
partition int
18+
state int
19+
produceLatency float64
1920
}
2021

2122
func (m *EndToEndMessage) creationTime() time.Time {

e2e/message_tracker.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ func (t *messageTracker) updateItemIfExists(msg *EndToEndMessage) error {
6666
return ttlcache.ErrNotFound
6767
}
6868

69-
t.cache.SetWithTTL(msg.MessageID, msg, remainingTTL)
69+
err = t.cache.SetWithTTL(msg.MessageID, msg, remainingTTL)
70+
if err != nil {
71+
panic(err)
72+
}
73+
7074
return nil
7175
}
7276

@@ -129,5 +133,6 @@ func (t *messageTracker) onMessageExpired(_ string, reason ttlcache.EvictionReas
129133
zap.Int("partition", msg.partition),
130134
zap.String("message_id", msg.MessageID),
131135
zap.Bool("successfully_produced", msg.state == EndToEndMessageStateProducedSuccessfully),
136+
zap.Float64("produce_latency_seconds", msg.produceLatency),
132137
)
133138
}

e2e/producer.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ func (s *Service) produceMessage(ctx context.Context, partition int) {
5858
// We need to use updateItemIfExists() because it's possible that the message has already been consumed
5959
// before we have received the message here (because we were awaiting the produce ack).
6060
msg.state = EndToEndMessageStateProducedSuccessfully
61-
s.messageTracker.updateItemIfExists(msg)
61+
msg.produceLatency = ackDuration.Seconds()
62+
63+
// TODO: Enable again as soon as https://github.com/ReneKroon/ttlcache/issues/60 is fixed
64+
// Because we cannot update cache items in an atomic fashion we currently can't use this method
65+
// as this would cause a race condition which ends up in records being reported as lost/expired.
66+
// s.messageTracker.updateItemIfExists(msg)
6267
}
6368

6469
s.produceLatency.WithLabelValues(pID).Observe(ackDuration.Seconds())

0 commit comments

Comments
 (0)