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

handle context cancellation eventhub #1151

Merged
merged 3 commits into from
Jan 25, 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
14 changes: 9 additions & 5 deletions flow/activities/flowable.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,12 @@ func (a *FlowableActivity) StartFlow(ctx context.Context,
numRecords := res.NumRecordsSynced
syncDuration := time.Since(syncStartTime)

slog.InfoContext(ctx, fmt.Sprintf("pushed %d records in %d seconds\n",
numRecords, int(syncDuration.Seconds())),
)
slog.InfoContext(ctx, fmt.Sprintf("pushed %d records in %d seconds", numRecords, int(syncDuration.Seconds())))
activity.RecordHeartbeat(ctx, fmt.Sprintf("pushed %d records", numRecords))

lastCheckpoint, err := recordBatch.GetLastCheckpoint()
if err != nil {
slog.ErrorContext(ctx, "failed to get last checkpoint", slog.Any("error", err))
a.Alerter.LogFlowError(ctx, flowName, err)
return nil, fmt.Errorf("failed to get last checkpoint: %w", err)
}
Expand All @@ -334,6 +334,7 @@ func (a *FlowableActivity) StartFlow(ctx context.Context,
pglogrepl.LSN(lastCheckpoint),
)
if err != nil {
slog.ErrorContext(ctx, "failed to update num rows and end lsn for cdc batch", slog.Any("error", err))
a.Alerter.LogFlowError(ctx, flowName, err)
return nil, err
}
Expand All @@ -345,6 +346,7 @@ func (a *FlowableActivity) StartFlow(ctx context.Context,
pglogrepl.LSN(lastCheckpoint),
)
if err != nil {
slog.ErrorContext(ctx, "failed to update latest lsn at target for cdc flow", slog.Any("error", err))
a.Alerter.LogFlowError(ctx, flowName, err)
return nil, err
}
Expand All @@ -356,6 +358,7 @@ func (a *FlowableActivity) StartFlow(ctx context.Context,
}
}
if err != nil {
slog.ErrorContext(ctx, "failed to update latest lsn at target for cdc flow", slog.Any("error", err))
a.Alerter.LogFlowError(ctx, flowName, err)
return nil, err
}
Expand Down Expand Up @@ -627,7 +630,7 @@ func (a *FlowableActivity) replicateQRepPartition(ctx context.Context,
return err
}

slog.InfoContext(ctx, fmt.Sprintf("pushed %d records\n", rowsSynced))
slog.InfoContext(ctx, fmt.Sprintf("pushed %d records", rowsSynced))
}

err = monitoring.UpdateEndTimeForPartition(ctx, a.CatalogPool, runUUID, partition)
Expand Down Expand Up @@ -779,7 +782,8 @@ func (a *FlowableActivity) SendWALHeartbeat(ctx context.Context) error {
slog.InfoContext(ctx, fmt.Sprintf("sent walheartbeat to peer %v", pgPeer.Name))
}
}
ticker.Reset(sendTimeout)
ticker.Stop()
ticker = time.NewTicker(sendTimeout)
}
}

Expand Down
3 changes: 2 additions & 1 deletion flow/activities/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (a *FlowableActivity) recordSlotSizePeriodically(
case <-ctx.Done():
return
}
ticker.Reset(timeout)
ticker.Stop()
ticker = time.NewTicker(timeout)
}
}
20 changes: 13 additions & 7 deletions flow/connectors/eventhub/eventhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *EventHubConnector) Close() error {
allErrors = errors.Join(allErrors, err)
}

err = c.hubManager.Close(context.Background())
err = c.hubManager.Close(c.ctx)
if err != nil {
c.logger.Error("failed to close event hub manager", slog.Any("error", err))
allErrors = errors.Join(allErrors, err)
Expand Down Expand Up @@ -119,7 +119,6 @@ func (c *EventHubConnector) processBatch(
flowJobName string,
batch *model.CDCRecordStream,
) (uint32, error) {
ctx := context.Background()
batchPerTopic := NewHubBatches(c.hubManager)
toJSONOpts := model.NewToJSONOptions(c.config.UnnestColumns)

Expand All @@ -145,7 +144,7 @@ func (c *EventHubConnector) processBatch(
case record, ok := <-batch.GetRecords():
if !ok {
c.logger.Info("flushing batches because no more records")
err := batchPerTopic.flushAllBatches(ctx, flowJobName)
err := batchPerTopic.flushAllBatches(c.ctx, flowJobName)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -175,7 +174,7 @@ func (c *EventHubConnector) processBatch(
return 0, err
}

numPartitions, err := c.hubManager.GetNumPartitions(ctx, destination)
numPartitions, err := c.hubManager.GetNumPartitions(c.ctx, destination)
if err != nil {
c.logger.Error("failed to get number of partitions", slog.Any("error", err))
return 0, err
Expand All @@ -194,7 +193,7 @@ func (c *EventHubConnector) processBatch(
}
partitionKey = utils.HashedPartitionKey(partitionKey, uint32(numPartitions))
destination.SetPartitionValue(partitionKey)
err = batchPerTopic.AddEvent(ctx, destination, json, false)
err = batchPerTopic.AddEvent(c.ctx, destination, json, false)
if err != nil {
c.logger.Error("failed to add event to batch", slog.Any("error", err))
return 0, err
Expand All @@ -205,8 +204,13 @@ func (c *EventHubConnector) processBatch(
c.logger.Info("processBatch", slog.Int("number of records processed for sending", int(curNumRecords)))
}

case <-c.ctx.Done():
c.logger.Error("context cancelled", slog.Any("error", c.ctx.Err()))
return 0, fmt.Errorf("[eventhub] context cancelled %v", c.ctx.Err())

case <-ticker.C:
err := batchPerTopic.flushAllBatches(ctx, flowJobName)
c.logger.Info("flushing batches because of timeout")
err := batchPerTopic.flushAllBatches(c.ctx, flowJobName)
if err != nil {
return 0, err
}
Expand All @@ -220,7 +224,8 @@ func (c *EventHubConnector) processBatch(
}
}

ticker.Reset(eventHubFlushTimeout)
ticker.Stop()
ticker = time.NewTicker(eventHubFlushTimeout)
}
}
}
Expand All @@ -245,6 +250,7 @@ func (c *EventHubConnector) SyncRecords(req *model.SyncRecordsRequest) (*model.S
c.logger.Error("failed to update last offset", slog.Any("error", err))
return nil, err
}

err = c.pgMetadata.IncrementID(req.FlowJobName)
if err != nil {
c.logger.Error("failed to increment id", slog.Any("error", err))
Expand Down
8 changes: 8 additions & 0 deletions flow/connectors/eventhub/hubmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
Expand Down Expand Up @@ -126,6 +127,12 @@ func (m *EventHubManager) closeProducerClient(ctx context.Context, pc *azeventhu
}

func (m *EventHubManager) Close(ctx context.Context) error {
numHubsClosed := atomic.Uint32{}
shutdown := utils.HeartbeatRoutine(ctx, 10*time.Second, func() string {
return fmt.Sprintf("closed %d eventhub clients", numHubsClosed.Load())
})
defer shutdown()

var allErrors error

m.hubs.Range(func(key any, value any) bool {
Expand All @@ -136,6 +143,7 @@ func (m *EventHubManager) Close(ctx context.Context) error {
slog.Error(fmt.Sprintf("failed to close eventhub client for %v", name), slog.Any("error", err))
allErrors = errors.Join(allErrors, err)
}
numHubsClosed.Add(1)
return true
})

Expand Down
Loading