-
Notifications
You must be signed in to change notification settings - Fork 319
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
feat: implement backoff for snowpipe streaming authorization errors #5399
base: master
Are you sure you want to change the base?
Changes from all commits
141b31f
9df59d0
e268f73
e6a2122
b5a1417
6ca236f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"bufio" | ||
"context" | ||
stdjson "encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
@@ -12,6 +13,7 @@ | |
"sync" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/hashicorp/go-retryablehttp" | ||
jsoniter "github.com/json-iterator/go" | ||
"github.com/samber/lo" | ||
|
@@ -31,20 +33,21 @@ | |
"github.com/rudderlabs/rudder-server/router/batchrouter/asyncdestinationmanager/snowpipestreaming/internal/model" | ||
"github.com/rudderlabs/rudder-server/utils/misc" | ||
"github.com/rudderlabs/rudder-server/utils/timeutil" | ||
"github.com/rudderlabs/rudder-server/warehouse/integrations/manager" | ||
whutils "github.com/rudderlabs/rudder-server/warehouse/utils" | ||
) | ||
|
||
var json = jsoniter.ConfigCompatibleWithStandardLibrary | ||
|
||
func New( | ||
conf *config.Config, | ||
logger logger.Logger, | ||
log logger.Logger, | ||
statsFactory stats.Stats, | ||
destination *backendconfig.DestinationT, | ||
) *Manager { | ||
m := &Manager{ | ||
appConfig: conf, | ||
logger: logger.Child("snowpipestreaming").Withn( | ||
logger: log.Child("snowpipestreaming").Withn( | ||
obskit.WorkspaceID(destination.WorkspaceID), | ||
obskit.DestinationID(destination.ID), | ||
obskit.DestinationType(destination.DestinationDefinition.Name), | ||
|
@@ -67,6 +70,9 @@ | |
m.config.client.retryMax = conf.GetInt("SnowpipeStreaming.Client.retryMax", 5) | ||
m.config.instanceID = conf.GetString("INSTANCE_ID", "1") | ||
m.config.maxBufferCapacity = conf.GetReloadableInt64Var(512*bytesize.KB, bytesize.B, "SnowpipeStreaming.maxBufferCapacity") | ||
m.config.backoff.initialInterval = conf.GetReloadableDurationVar(1, time.Second, "SnowpipeStreaming.backoffInitialIntervalInSeconds") | ||
m.config.backoff.multiplier = conf.GetReloadableFloat64Var(2.0, "SnowpipeStreaming.backoffMultiplier") | ||
m.config.backoff.maxInterval = conf.GetReloadableDurationVar(1, time.Hour, "SnowpipeStreaming.backoffMaxIntervalInHours") | ||
|
||
tags := stats.Tags{ | ||
"module": "batch_router", | ||
|
@@ -100,6 +106,17 @@ | |
snowpipeapi.New(m.appConfig, m.statsFactory, m.config.client.url, m.requestDoer), | ||
destination, | ||
) | ||
m.managerCreator = func(ctx context.Context, modelWarehouse whutils.ModelWarehouse, conf *config.Config, logger logger.Logger, statsFactory stats.Stats) (manager.Manager, error) { | ||
sf, err := manager.New(whutils.SnowpipeStreaming, conf, logger, statsFactory) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating snowflake manager: %w", err) | ||
} | ||
Check warning on line 113 in router/batchrouter/asyncdestinationmanager/snowpipestreaming/snowpipestreaming.go Codecov / codecov/patchrouter/batchrouter/asyncdestinationmanager/snowpipestreaming/snowpipestreaming.go#L112-L113
|
||
err = sf.Setup(ctx, modelWarehouse, whutils.NewNoOpUploader()) | ||
if err != nil { | ||
return nil, fmt.Errorf("setting up snowflake manager: %w", err) | ||
} | ||
Check warning on line 117 in router/batchrouter/asyncdestinationmanager/snowpipestreaming/snowpipestreaming.go Codecov / codecov/patchrouter/batchrouter/asyncdestinationmanager/snowpipestreaming/snowpipestreaming.go#L116-L117
|
||
return sf, nil | ||
} | ||
return m | ||
} | ||
|
||
|
@@ -121,6 +138,10 @@ | |
return client | ||
} | ||
|
||
func (m *Manager) Now() time.Time { | ||
return m.now() | ||
} | ||
|
||
func (m *Manager) Transform(job *jobsdb.JobT) (string, error) { | ||
return common.GetMarshalledData(string(job.EventPayload), job.JobID) | ||
} | ||
|
@@ -152,6 +173,12 @@ | |
|
||
discardsChannel, err := m.initializeChannelWithSchema(ctx, asyncDest.Destination.ID, &destConf, discardsTable(), discardsSchema()) | ||
if err != nil { | ||
if errors.Is(err, errAuthz) || errors.Is(err, errBackoff) { | ||
if errors.Is(err, errAuthz) { | ||
m.setBackOff() | ||
} | ||
return m.failedJobs(asyncDest, err.Error()) | ||
} | ||
return m.abortJobs(asyncDest, fmt.Errorf("failed to prepare discards channel: %w", err).Error()) | ||
} | ||
m.logger.Infon("Prepared discards channel") | ||
|
@@ -184,9 +211,17 @@ | |
importInfos []*importInfo | ||
discardImportInfo *importInfo | ||
) | ||
shouldResetBackoff := true // backoff should be reset if authz error is not encountered for any of the tables | ||
isBackoffSet := false // should not be set again if already set | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this? It's not being set anywhere? |
||
for _, info := range uploadInfos { | ||
imInfo, discardImInfo, err := m.sendEventsToSnowpipe(ctx, asyncDest.Destination.ID, &destConf, info) | ||
if err != nil { | ||
if errors.Is(err, errAuthz) || errors.Is(err, errBackoff) { | ||
shouldResetBackoff = false | ||
if errors.Is(err, errAuthz) && !isBackoffSet { | ||
m.setBackOff() | ||
} | ||
} | ||
m.logger.Warnn("Failed to send events to Snowpipe", | ||
logger.NewStringField("table", info.tableName), | ||
obskit.Error(err), | ||
|
@@ -206,6 +241,9 @@ | |
discardImportInfo.Offset = discardImInfo.Offset | ||
} | ||
} | ||
if shouldResetBackoff { | ||
m.resetBackoff() | ||
} | ||
if discardImportInfo != nil { | ||
importInfos = append(importInfos, discardImportInfo) | ||
} | ||
|
@@ -245,7 +283,7 @@ | |
|
||
events := make([]*event, 0, eventsCount) | ||
|
||
formattedTS := m.now().Format(misc.RFC3339Milli) | ||
formattedTS := m.Now().Format(misc.RFC3339Milli) | ||
scanner := bufio.NewScanner(file) | ||
scanner.Buffer(nil, int(m.config.maxBufferCapacity.Load())) | ||
|
||
|
@@ -289,7 +327,7 @@ | |
} | ||
log.Infon("Prepared channel", logger.NewStringField("channelID", channelResponse.ChannelID)) | ||
|
||
formattedTS := m.now().Format(misc.RFC3339Milli) | ||
formattedTS := m.Now().Format(misc.RFC3339Milli) | ||
var discardInfos []discardInfo | ||
for _, tableEvent := range info.events { | ||
discardInfos = append(discardInfos, getDiscardedRecordsFromEvent(tableEvent, channelResponse.SnowpipeSchema, info.tableName, formattedTS)...) | ||
|
@@ -362,6 +400,16 @@ | |
} | ||
} | ||
|
||
func (m *Manager) failedJobs(asyncDest *common.AsyncDestinationStruct, failedReason string) common.AsyncUploadOutput { | ||
m.stats.jobs.failed.Count(len(asyncDest.ImportingJobIDs)) | ||
return common.AsyncUploadOutput{ | ||
FailedJobIDs: asyncDest.ImportingJobIDs, | ||
FailedCount: len(asyncDest.ImportingJobIDs), | ||
FailedReason: failedReason, | ||
DestinationID: asyncDest.Destination.ID, | ||
} | ||
} | ||
|
||
// Poll checks the status of multiple imports using the import ID from pollInput. | ||
// For the once which have reached the terminal state (success or failure), it caches the import infos in polledImportInfoMap. Later if Poll is called again, it does not need to do the status check again. | ||
// Once all the imports have reached the terminal state, if any imports have failed, it deletes the channels for those imports. | ||
|
@@ -549,3 +597,34 @@ | |
}, | ||
} | ||
} | ||
|
||
func (m *Manager) isInBackoff() bool { | ||
if m.backoff.next.IsZero() { | ||
return false | ||
} | ||
return m.Now().Before(m.backoff.next) | ||
} | ||
|
||
func (m *Manager) resetBackoff() { | ||
m.backoff.next = time.Time{} | ||
m.backoff.attempts = 0 | ||
} | ||
|
||
func (m *Manager) setBackOff() { | ||
b := backoff.NewExponentialBackOff( | ||
backoff.WithInitialInterval(m.config.backoff.initialInterval.Load()), | ||
backoff.WithMultiplier(m.config.backoff.multiplier.Load()), | ||
backoff.WithClockProvider(m), | ||
backoff.WithRandomizationFactor(0), | ||
backoff.WithMaxElapsedTime(0), | ||
backoff.WithMaxInterval(m.config.backoff.maxInterval.Load()), | ||
) | ||
b.Reset() | ||
m.backoff.attempts++ | ||
|
||
var d time.Duration | ||
for index := int64(0); index < int64(m.backoff.attempts); index++ { | ||
d = b.NextBackOff() | ||
} | ||
m.backoff.next = m.Now().Add(d) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably avoid doing
errors.Is
twice.