Fixing a bug where the readme snippet doesn't work - the main issue is that the memory persister was returnning an error when the checkpoint didn't exist. The other stores all treat this as a "go ahead and initialize a new checkpoint" so this is only an issue with the memory persister.
I've added some tests in to get better confidence around it (and to document what filters you do end up with). For the most part we can fall back to just using the same non-inclusive filter for everything offset based since offsets can't be negative Also, the tf file doesn't work with the latest terraforms so I've adjusted it like the service bus one.
This commit is contained in:
Родитель
0eb7b61636
Коммит
2b1e9f8aee
|
@ -144,7 +144,7 @@ output "AZURE_CLIENT_ID" {
|
|||
value = compact(
|
||||
concat(
|
||||
azuread_application.test.*.application_id,
|
||||
list(data.azurerm_client_config.current.client_id)
|
||||
[data.azurerm_client_config.current.client_id]
|
||||
)
|
||||
)[0]
|
||||
}
|
||||
|
@ -153,7 +153,7 @@ output "AZURE_CLIENT_SECRET" {
|
|||
value = compact(
|
||||
concat(
|
||||
azuread_service_principal_password.test.*.value,
|
||||
list(var.azure_client_secret)
|
||||
[var.azure_client_secret]
|
||||
)
|
||||
)[0]
|
||||
sensitive = true
|
||||
|
|
|
@ -1,30 +1,7 @@
|
|||
// Package persist provides abstract structures for checkpoint persistence.
|
||||
package persist
|
||||
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"sync"
|
||||
)
|
||||
|
@ -73,7 +50,7 @@ func (p *MemoryPersister) Read(namespace, name, consumerGroup, partitionID strin
|
|||
if offset, ok := p.values[key]; ok {
|
||||
return offset, nil
|
||||
}
|
||||
return NewCheckpointFromStartOfStream(), fmt.Errorf("could not read the offset for the key %s", key)
|
||||
return NewCheckpointFromStartOfStream(), nil
|
||||
}
|
||||
|
||||
func getPersistenceKey(namespace, name, consumerGroup, partitionID string) string {
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package persist
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMemoryPersister(t *testing.T) {
|
||||
p := NewMemoryPersister()
|
||||
|
||||
// read a checkpoint before it's been saved will default to "beginning of stream"
|
||||
actualCheckpoint, err := p.Read("namespace", "name", "consumerGroup", "0")
|
||||
assert.NoError(t, err, "Not an error to get a checkpoint for a partition for the first time")
|
||||
assert.Equal(t, NewCheckpointFromStartOfStream(), actualCheckpoint, "If we've never stored a checkpoint, default to beginning of stream")
|
||||
|
||||
now := time.Now()
|
||||
|
||||
// now write one and read it back
|
||||
err = p.Write("namespace", "name", "consumerGroup", "0", Checkpoint{
|
||||
Offset: "100",
|
||||
SequenceNumber: 2,
|
||||
EnqueueTime: now,
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
actualCheckpoint, err = p.Read("namespace", "name", "consumerGroup", "0")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, Checkpoint{
|
||||
Offset: "100",
|
||||
SequenceNumber: 2,
|
||||
EnqueueTime: now,
|
||||
}, actualCheckpoint)
|
||||
}
|
32
receiver.go
32
receiver.go
|
@ -382,12 +382,15 @@ func (r *receiver) newSessionAndLink(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
offsetExpression, err := r.getOffsetExpression()
|
||||
checkpoint, err := r.getLastReceivedCheckpoint()
|
||||
|
||||
if err != nil {
|
||||
tab.For(ctx).Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
offsetExpression := getOffsetExpression(checkpoint)
|
||||
|
||||
r.session, err = newSession(amqpSession)
|
||||
if err != nil {
|
||||
tab.For(ctx).Error(err)
|
||||
|
@ -423,20 +426,6 @@ func (r *receiver) storeLastReceivedCheckpoint(checkpoint persist.Checkpoint) er
|
|||
return r.offsetPersister().Write(r.namespaceName(), r.hubName(), r.consumerGroup, r.partitionID, checkpoint)
|
||||
}
|
||||
|
||||
func (r *receiver) getOffsetExpression() (string, error) {
|
||||
checkpoint, err := r.getLastReceivedCheckpoint()
|
||||
if err != nil {
|
||||
// assume err read is due to not having an offset -- probably want to change this as it's ambiguous
|
||||
return fmt.Sprintf(amqpAnnotationFormat, offsetAnnotationName, "=", persist.StartOfStream), nil
|
||||
}
|
||||
|
||||
if checkpoint.Offset == "" {
|
||||
return fmt.Sprintf(amqpAnnotationFormat, enqueuedTimeAnnotationName, "", checkpoint.EnqueueTime.UnixNano()/int64(time.Millisecond)), nil
|
||||
}
|
||||
|
||||
return fmt.Sprintf(amqpAnnotationFormat, offsetAnnotationName, "", checkpoint.Offset), nil
|
||||
}
|
||||
|
||||
func (r *receiver) getAddress() string {
|
||||
return fmt.Sprintf("%s/ConsumerGroups/%s/Partitions/%s", r.hubName(), r.consumerGroup, r.partitionID)
|
||||
}
|
||||
|
@ -489,3 +478,16 @@ func (lc *ListenerHandle) Err() error {
|
|||
}
|
||||
return lc.ctx.Err()
|
||||
}
|
||||
|
||||
// getOffsetExpression calculates a selector expression based on the Offset or EnqueueTime of a Checkpoint.
|
||||
func getOffsetExpression(checkpoint persist.Checkpoint) string {
|
||||
if checkpoint.Offset == "" {
|
||||
// time-based, non-inclusive
|
||||
// ex: amqp.annotation.x-opt-enqueued-time > '165805323000'
|
||||
return fmt.Sprintf(amqpAnnotationFormat, enqueuedTimeAnnotationName, "", checkpoint.EnqueueTime.UnixNano()/int64(time.Millisecond))
|
||||
}
|
||||
|
||||
// offset based, non-inclusive
|
||||
// ex: "amqp.annotation.x-opt-offset > '100'"
|
||||
return fmt.Sprintf(amqpAnnotationFormat, offsetAnnotationName, "", checkpoint.Offset)
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package eventhub
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Azure/azure-event-hubs-go/v3/persist"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetOffsetExpression(t * testing.T) {
|
||||
expr := getOffsetExpression(persist.NewCheckpointFromStartOfStream())
|
||||
assert.EqualValues(t, "amqp.annotation.x-opt-offset > '-1'", expr)
|
||||
|
||||
expr = getOffsetExpression(persist.NewCheckpointFromEndOfStream())
|
||||
assert.EqualValues(t, "amqp.annotation.x-opt-offset > '@latest'", expr)
|
||||
|
||||
expr = getOffsetExpression(persist.Checkpoint{Offset: "100"})
|
||||
assert.EqualValues(t, "amqp.annotation.x-opt-offset > '100'", expr)
|
||||
|
||||
// offset wins - the time is ignored if they've specified an offset in the checkpoint.t
|
||||
now, err := time.Parse(time.RFC3339, "1975-04-04T01:02:03Z")
|
||||
assert.NoError(t, err)
|
||||
|
||||
// now's ignored here - the offset will win.
|
||||
checkpoint := persist.NewCheckpoint("100", 1, now)
|
||||
expr = getOffsetExpression(checkpoint)
|
||||
assert.EqualValues(t, "amqp.annotation.x-opt-offset > '100'", expr)
|
||||
|
||||
// no offset this time, date will be used
|
||||
checkpoint = persist.NewCheckpoint("", 1, now)
|
||||
expr = getOffsetExpression(checkpoint)
|
||||
assert.EqualValues(t, "amqp.annotation.x-opt-enqueued-time > '165805323000'", expr)
|
||||
}
|
Загрузка…
Ссылка в новой задаче