Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log unprocessed items after dynamo batch put operation #333

Merged
merged 1 commit into from
Sep 18, 2024
Merged
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
31 changes: 25 additions & 6 deletions core/models/channel_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"slices"
"time"

"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/mailroom/runtime"
Expand Down Expand Up @@ -84,12 +88,27 @@ type dbChannelLog struct {
// InsertChannelLogs writes the given channel logs to the db
func InsertChannelLogs(ctx context.Context, rt *runtime.Runtime, logs []*ChannelLog) error {
// write all logs to DynamoDB
cls := make([]*clogs.Log, len(logs))
for i, l := range logs {
cls[i] = l.Log
}
if err := clogs.BatchPut(ctx, rt.Dynamo, "ChannelLogs", cls); err != nil {
return fmt.Errorf("error writing channel logs: %w", err)
for batch := range slices.Chunk(logs, 25) {
writeReqs := make([]types.WriteRequest, len(batch))

for i, l := range batch {
d, err := l.MarshalDynamo()
if err != nil {
return fmt.Errorf("error marshalling log: %w", err)
}
writeReqs[i] = types.WriteRequest{PutRequest: &types.PutRequest{Item: d}}
}

resp, err := rt.Dynamo.Client.BatchWriteItem(ctx, &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]types.WriteRequest{rt.Dynamo.TableName("ChannelLogs"): writeReqs},
})
if err != nil {
return fmt.Errorf("error writing logs to dynamo: %w", err)
}
if len(resp.UnprocessedItems) > 0 {
// TODO shouldn't happend.. but need to figure out how we would retry these
slog.Error("unprocessed items writing logs to dynamo", "count", len(resp.UnprocessedItems))
}
}

unattached := make([]*dbChannelLog, 0, len(logs))
Expand Down
36 changes: 0 additions & 36 deletions utils/clogs/batch.go

This file was deleted.

4 changes: 3 additions & 1 deletion utils/clogs/clog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func TestLogs(t *testing.T) {
l2.Error(clogs.NewLogError("code2", "ext", "message"))

// write both logs to db
err = clogs.BatchPut(ctx, ds, "ChannelLogs", []*clogs.Log{l1, l2})
err = ds.PutItem(ctx, "ChannelLogs", l1)
assert.NoError(t, err)
err = ds.PutItem(ctx, "ChannelLogs", l2)
assert.NoError(t, err)

// read log 1 back from db
Expand Down
Loading