Skip to content

Commit 3df46fb

Browse files
committed
feat: add message timestamp to view update context
1 parent 85b1720 commit 3df46fb

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type UpdateContext interface {
2828
// It is recommended to lazily evaluate the headers to reduce overhead per message
2929
// when headers are not used.
3030
Headers() Headers
31+
32+
// Timestamp returns the timestamp of the input message.
33+
Timestamp() time.Time
3134
}
3235

3336
// UpdateCallback is invoked upon arrival of a message for a table partition.
@@ -95,6 +98,7 @@ type DefaultUpdateContext struct {
9598
partition int32
9699
offset int64
97100
headers []*sarama.RecordHeader
101+
timestamp time.Time
98102
}
99103

100104
// Topic returns the topic of input message.
@@ -117,6 +121,11 @@ func (ctx DefaultUpdateContext) Headers() Headers {
117121
return HeadersFromSarama(ctx.headers)
118122
}
119123

124+
// Timestamp returns the timestamp of the input message.
125+
func (ctx DefaultUpdateContext) Timestamp() time.Time {
126+
return ctx.timestamp
127+
}
128+
120129
///////////////////////////////////////////////////////////////////////////////
121130
// processor options
122131
///////////////////////////////////////////////////////////////////////////////

partition_table.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ func (p *PartitionTable) loadMessages(ctx context.Context, cons sarama.Partition
462462
}
463463

464464
lastMessage = time.Now()
465-
if err := p.storeEvent(string(msg.Key), msg.Value, msg.Offset, msg.Headers); err != nil {
465+
if err := p.storeEvent(string(msg.Key), msg.Value, msg.Offset, msg.Headers, msg.Timestamp); err != nil {
466466
return fmt.Errorf("load: error updating storage: %v", err)
467467
}
468468

@@ -534,12 +534,13 @@ func (p *PartitionTable) TrackMessageWrite(ctx context.Context, length int) {
534534
})
535535
}
536536

537-
func (p *PartitionTable) storeEvent(key string, value []byte, offset int64, headers []*sarama.RecordHeader) error {
537+
func (p *PartitionTable) storeEvent(key string, value []byte, offset int64, headers []*sarama.RecordHeader, timestamp time.Time) error {
538538
err := p.st.Update(&DefaultUpdateContext{
539539
topic: p.st.topic,
540540
partition: p.st.partition,
541541
offset: offset,
542542
headers: headers,
543+
timestamp: timestamp,
543544
}, key, value)
544545
if err != nil {
545546
return fmt.Errorf("Error from the update callback while recovering from the log: %v", err)

partition_table_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -708,16 +708,19 @@ func TestPT_loadMessages(t *testing.T) {
708708
func TestPT_storeEvent(t *testing.T) {
709709
t.Run("succeed", func(t *testing.T) {
710710
var (
711-
localOffset int64
712-
partition int32
713-
topic = "some-topic"
714-
key = "some-key"
715-
value = []byte("some-vale")
716-
actualKey string
717-
actualValue []byte
718-
updateCB UpdateCallback = func(ctx UpdateContext, s storage.Storage, k string, v []byte) error {
711+
localOffset int64
712+
partition int32
713+
topic = "some-topic"
714+
key = "some-key"
715+
value = []byte("some-vale")
716+
timestamp = time.Now()
717+
actualKey string
718+
actualValue []byte
719+
actualTimestamp time.Time
720+
updateCB UpdateCallback = func(ctx UpdateContext, s storage.Storage, k string, v []byte) error {
719721
actualKey = k
720722
actualValue = v
723+
actualTimestamp = ctx.Timestamp()
721724
return nil
722725
}
723726
)
@@ -735,9 +738,10 @@ func TestPT_storeEvent(t *testing.T) {
735738
defer cancel()
736739
err := pt.setup(ctx)
737740
require.NoError(t, err)
738-
err = pt.storeEvent(key, value, localOffset, nil)
741+
err = pt.storeEvent(key, value, localOffset, nil, timestamp)
739742
require.Equal(t, actualKey, key)
740743
require.Equal(t, actualValue, value)
744+
require.Equal(t, actualTimestamp, timestamp)
741745
require.NoError(t, err)
742746
})
743747
t.Run("fail", func(t *testing.T) {
@@ -747,6 +751,7 @@ func TestPT_storeEvent(t *testing.T) {
747751
topic = "some-topic"
748752
key = "some-key"
749753
value = []byte("some-vale")
754+
timestamp = time.Now()
750755
updateCB UpdateCallback = updateCallbackNoop
751756
retErr error = fmt.Errorf("storage err")
752757
)
@@ -764,7 +769,7 @@ func TestPT_storeEvent(t *testing.T) {
764769
defer cancel()
765770
err := pt.setup(ctx)
766771
require.NoError(t, err)
767-
err = pt.storeEvent(key, value, localOffset, nil)
772+
err = pt.storeEvent(key, value, localOffset, nil, timestamp)
768773
require.Error(t, err)
769774
})
770775
}

0 commit comments

Comments
 (0)