Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions state/aws/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type StateStore struct {
table string
ttlAttributeName string
partitionKey string
ttlInSeconds *int

dynamodbClient *dynamodb.Client
}
Expand All @@ -64,6 +65,7 @@ type dynamoDBMetadata struct {
Table string `json:"table"`
TTLAttributeName string `json:"ttlAttributeName"`
PartitionKey string `json:"partitionKey"`
TTLInSeconds *int `json:"ttlInSeconds" mapstructure:"ttlInSeconds"`
}

type putData struct {
Expand Down Expand Up @@ -117,6 +119,7 @@ func (d *StateStore) Init(ctx context.Context, metadata state.Metadata) error {
d.table = meta.Table
d.ttlAttributeName = meta.TTLAttributeName
d.partitionKey = meta.PartitionKey
d.ttlInSeconds = meta.TTLInSeconds

if err := d.validateTableAccess(ctx); err != nil {
return fmt.Errorf("error validating DynamoDB table '%s' access: %w", d.table, err)
Expand Down Expand Up @@ -425,6 +428,11 @@ func (d *StateStore) parseTTL(req *state.SetRequest) (*int64, error) {

return &expirationTime, nil
}
// apply global TTL if no explicit TTL in request metadata
if d.ttlInSeconds != nil {
expirationTime := time.Now().Unix() + int64(*d.ttlInSeconds)
return &expirationTime, nil
}
}

return nil, nil
Expand Down
167 changes: 167 additions & 0 deletions state/aws/dynamodb/dynamodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1206,3 +1206,170 @@ func TestMultiTx(t *testing.T) {
require.NoError(t, err)
})
}

func TestParseTTLWithDefault(t *testing.T) {
t.Run("Use explicit TTL from request metadata", func(t *testing.T) {
defaultTTL := 600
s := StateStore{
ttlAttributeName: "expiresAt",
ttlInSeconds: &defaultTTL,
}

req := &state.SetRequest{
Key: "test-key",
Metadata: map[string]string{
"ttlInSeconds": "300",
},
}

ttl, err := s.parseTTL(req)
require.NoError(t, err)
require.NotNil(t, ttl)

// Should use explicit value (300), not default (600)
expectedTime := time.Now().Unix() + 300
assert.InDelta(t, expectedTime, *ttl, 2) // Allow 2 second tolerance
})

t.Run("Use default TTL when no explicit TTL in request", func(t *testing.T) {
defaultTTL := 600
s := StateStore{
ttlAttributeName: "expiresAt",
ttlInSeconds: &defaultTTL,
}

req := &state.SetRequest{
Key: "test-key",
Metadata: map[string]string{},
}

ttl, err := s.parseTTL(req)
require.NoError(t, err)
require.NotNil(t, ttl)

// Should use default value (600)
expectedTime := time.Now().Unix() + 600
assert.InDelta(t, expectedTime, *ttl, 2) // Allow 2 second tolerance
})

t.Run("No TTL when no default and no explicit TTL", func(t *testing.T) {
s := StateStore{
ttlAttributeName: "expiresAt",
ttlInSeconds: nil, // No default configured
}

req := &state.SetRequest{
Key: "test-key",
Metadata: map[string]string{},
}

ttl, err := s.parseTTL(req)
require.NoError(t, err)
assert.Nil(t, ttl)
})

t.Run("No TTL when ttlAttributeName is not set", func(t *testing.T) {
defaultTTL := 600
s := StateStore{
ttlAttributeName: "", // TTL not enabled in component
ttlInSeconds: &defaultTTL,
}

req := &state.SetRequest{
Key: "test-key",
Metadata: map[string]string{
"ttlInSeconds": "300",
},
}

ttl, err := s.parseTTL(req)
require.NoError(t, err)
assert.Nil(t, ttl) // Should return nil when TTL not enabled
})

t.Run("Explicit TTL with value -1", func(t *testing.T) {
defaultTTL := 600
s := StateStore{
ttlAttributeName: "expiresAt",
ttlInSeconds: &defaultTTL,
}

req := &state.SetRequest{
Key: "test-key",
Metadata: map[string]string{
"ttlInSeconds": "-1",
},
}

ttl, err := s.parseTTL(req)
require.NoError(t, err)
require.NotNil(t, ttl)

// -1 should result in immediate expiration (now + -1)
expectedTime := time.Now().Unix() - 1
assert.InDelta(t, expectedTime, *ttl, 2)
})

t.Run("Default TTL with large value", func(t *testing.T) {
defaultTTL := 86400 // 24 hours
s := StateStore{
ttlAttributeName: "expiresAt",
ttlInSeconds: &defaultTTL,
}

req := &state.SetRequest{
Key: "test-key",
Metadata: map[string]string{},
}

ttl, err := s.parseTTL(req)
require.NoError(t, err)
require.NotNil(t, ttl)

expectedTime := time.Now().Unix() + 86400
assert.InDelta(t, expectedTime, *ttl, 2)
})

t.Run("Error on invalid TTL value", func(t *testing.T) {
defaultTTL := 600
s := StateStore{
ttlAttributeName: "expiresAt",
ttlInSeconds: &defaultTTL,
}

req := &state.SetRequest{
Key: "test-key",
Metadata: map[string]string{
"ttlInSeconds": "invalid",
},
}

ttl, err := s.parseTTL(req)
require.Error(t, err)
assert.Nil(t, ttl)
assert.Contains(t, err.Error(), "invalid syntax")
})

t.Run("Explicit TTL overrides default in request with empty metadata", func(t *testing.T) {
defaultTTL := 1200
s := StateStore{
ttlAttributeName: "expiresAt",
ttlInSeconds: &defaultTTL,
}

req := &state.SetRequest{
Key: "test-key",
Metadata: map[string]string{
"ttlInSeconds": "0",
},
}

ttl, err := s.parseTTL(req)
require.NoError(t, err)
require.NotNil(t, ttl)

// Should use explicit value 0, not default
expectedTime := time.Now().Unix()
assert.InDelta(t, expectedTime, *ttl, 2)
})
Comment on lines +1353 to +1374
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case demonstrates that the behavior for dynamodb would make the setting of this metadata field to 0 to expire immediately, and the same for a negative value. However, if I look at the SET implementation of the redis implementation a value of 0 for the ttl means the key never expires, as does a negative value. I think we should have the same behavior on dynamodb that we have for redis please 🙏 If you can also create a docs PR to add this PR to the docs too that would be greatly appreciated! Feel free to tag me when this is ready for re-review :)

https://github.com/berndverst/components-contrib/blob/master/state/redis/redis.go#L363

}
6 changes: 6 additions & 0 deletions state/aws/dynamodb/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ metadata:
The table attribute name which should be used for TTL.
example: '"expiresAt"'
type: string
- name: ttlInSeconds
required: false
description: |
Allows specifying a default Time-to-live (TTL) in seconds that will be applied to every state store request unless TTL is explicitly defined via the request metadata.
Comment on lines +41 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: |
Allows specifying a default Time-to-live (TTL) in seconds that will be applied to every state store request unless TTL is explicitly defined via the request metadata.
description: |
Allows specifying a Time-to-live (TTL) in seconds that will be applied to every state store request unless TTL is explicitly defined via the request metadata.

example: '"600"'
type: number
- name: partitionKey
required: false
description: |
Expand Down