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

chore(processor): support multiple jobsdb writers when source isolation is enabled #3428

Merged
merged 1 commit into from
Jun 6, 2023
Merged
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
93 changes: 58 additions & 35 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/rudderlabs/rudder-go-kit/logger"
"github.com/rudderlabs/rudder-go-kit/ro"
"github.com/rudderlabs/rudder-go-kit/stats"
kit_sync "github.com/rudderlabs/rudder-go-kit/sync"
backendconfig "github.com/rudderlabs/rudder-server/backend-config"
eventschema "github.com/rudderlabs/rudder-server/event-schema"
"github.com/rudderlabs/rudder-server/jobsdb"
Expand Down Expand Up @@ -139,6 +140,7 @@ type Handle struct {
}

adaptiveLimit func(int64) int64
storePlocker kit_sync.PartitionLocker
}
type processorStats struct {
statGatewayDBR stats.Measurement
Expand Down Expand Up @@ -348,6 +350,7 @@ func (proc *Handle) Setup(
if proc.adaptiveLimit == nil {
proc.adaptiveLimit = func(limit int64) int64 { return limit }
}
proc.storePlocker = *kit_sync.NewPartitionLocker()

// Stats
proc.statsFactory = stats.Default
Expand Down Expand Up @@ -1733,6 +1736,7 @@ func (proc *Handle) transformations(partition string, in *transformationMessage)
procErrorJobsByDestID := make(map[string][]*jobsdb.JobT)
var batchDestJobs []*jobsdb.JobT
var destJobs []*jobsdb.JobT
routerDestIDs := make(map[string]struct{})

destProcStart := time.Now()

Expand Down Expand Up @@ -1763,7 +1767,7 @@ func (proc *Handle) transformations(partition string, in *transformationMessage)
for o := range chOut {
destJobs = append(destJobs, o.destJobs...)
batchDestJobs = append(batchDestJobs, o.batchDestJobs...)

routerDestIDs = lo.Assign(routerDestIDs, o.routerDestIDs)
in.reportMetrics = append(in.reportMetrics, o.reportMetrics...)
for k, v := range o.errorsPerDestID {
procErrorJobsByDestID[k] = append(procErrorJobsByDestID[k], v...)
Expand All @@ -1776,13 +1780,15 @@ func (proc *Handle) transformations(partition string, in *transformationMessage)
// this tells us how many transformations we are doing per second.
transformationsThroughput := throughputPerSecond(in.totalEvents, destProcTime)
proc.stats.transformationsThroughput.Count(transformationsThroughput)

return &storeMessage{
in.statusList,
destJobs,
batchDestJobs,

procErrorJobsByDestID,
in.procErrorJobs,
lo.Keys(routerDestIDs),

in.reportMetrics,
in.sourceDupStats,
Expand All @@ -1801,6 +1807,7 @@ type storeMessage struct {

procErrorJobsByDestID map[string][]*jobsdb.JobT
procErrorJobs []*jobsdb.JobT
routerDestIDs []string

reportMetrics []*types.PUReportedMetric
sourceDupStats map[dupStatKey]int
Expand Down Expand Up @@ -1852,6 +1859,7 @@ func (proc *Handle) Store(partition string, in *storeMessage) {
if proc.limiter.store != nil {
defer proc.limiter.store.BeginWithPriority(partition, proc.getLimiterPriority(partition))()
}

statusList, destJobs, batchDestJobs := in.statusList, in.destJobs, in.batchDestJobs
beforeStoreStatus := time.Now()
// XX: Need to do this in a transaction
Expand Down Expand Up @@ -1894,41 +1902,50 @@ func (proc *Handle) Store(partition string, in *storeMessage) {
}

if len(destJobs) > 0 {
err := misc.RetryWithNotify(
context.Background(),
proc.jobsDBCommandTimeout,
proc.jobdDBMaxRetries,
func(ctx context.Context) error {
return proc.routerDB.WithStoreSafeTx(
ctx,
func(tx jobsdb.StoreSafeTx) error {
err := proc.routerDB.StoreInTx(ctx, tx, destJobs)
if err != nil {
return fmt.Errorf("storing router jobs: %w", err)
}

// rsources stats
err = proc.updateRudderSourcesStats(ctx, tx, destJobs)
if err != nil {
return fmt.Errorf("publishing rsources stats for router: %w", err)
}
return nil
})
}, proc.sendRetryStoreStats)
if err != nil {
panic(err)
}
proc.logger.Debug("[Processor] Total jobs written to router : ", len(destJobs))
func() {
// Only one goroutine can store to a router destination at a time, otherwise we may have different transactions
// committing at different timestamps which can cause events with lower jobIDs to appear after events with higher ones.
// For that purpose, before storing, we lock the relevant destination IDs (in sorted order to avoid deadlocks).
if len(in.routerDestIDs) > 0 {
destIDs := lo.Uniq(in.routerDestIDs)
slices.Sort(destIDs)
for _, destID := range destIDs {
proc.storePlocker.Lock(destID)
defer proc.storePlocker.Unlock(destID)
}
}
Comment on lines +1906 to +1916
Copy link
Member

Choose a reason for hiding this comment

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

Would it be simpler if we had just one lock?

I haven't any benchmarks, but concurrent writes on the same table are not beneficial from a performance standpoint.

Copy link
Contributor Author

@atzoum atzoum Jun 2, 2023

Choose a reason for hiding this comment

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

We have observed major improvements in wait times after increasing the number of writers, for some scenarios

err := misc.RetryWithNotify(
context.Background(),
proc.jobsDBCommandTimeout,
proc.jobdDBMaxRetries,
func(ctx context.Context) error {
return proc.routerDB.WithStoreSafeTx(
ctx,
func(tx jobsdb.StoreSafeTx) error {
err := proc.routerDB.StoreInTx(ctx, tx, destJobs)
if err != nil {
return fmt.Errorf("storing router jobs: %w", err)
}

proc.multitenantI.ReportProcLoopAddStats(
getJobCountsByWorkspaceDestType(destJobs),
"rt",
)
proc.stats.statDestNumOutputEvents.Count(len(destJobs))
proc.stats.statDBWriteRouterEvents.Observe(float64(len(destJobs)))
proc.stats.statDBWriteRouterPayloadBytes.Observe(
float64(lo.SumBy(destJobs, func(j *jobsdb.JobT) int { return len(j.EventPayload) })),
)
// rsources stats
err = proc.updateRudderSourcesStats(ctx, tx, destJobs)
if err != nil {
return fmt.Errorf("publishing rsources stats for router: %w", err)
}
return nil
})
}, proc.sendRetryStoreStats)
if err != nil {
panic(err)
}
proc.logger.Debug("[Processor] Total jobs written to router : ", len(destJobs))
proc.multitenantI.ReportProcLoopAddStats(getJobCountsByWorkspaceDestType(destJobs), "rt")
proc.stats.statDestNumOutputEvents.Count(len(destJobs))
proc.stats.statDBWriteRouterEvents.Observe(float64(len(destJobs)))
proc.stats.statDBWriteRouterPayloadBytes.Observe(
float64(lo.SumBy(destJobs, func(j *jobsdb.JobT) int { return len(j.EventPayload) })),
)
}()
}

for _, jobs := range in.procErrorJobsByDestID {
Expand Down Expand Up @@ -2019,6 +2036,7 @@ type transformSrcDestOutput struct {
destJobs []*jobsdb.JobT
batchDestJobs []*jobsdb.JobT
errorsPerDestID map[string][]*jobsdb.JobT
routerDestIDs map[string]struct{}
}

func (proc *Handle) transformSrcDest(
Expand Down Expand Up @@ -2051,6 +2069,7 @@ func (proc *Handle) transformSrcDest(
reportMetrics := make([]*types.PUReportedMetric, 0)
batchDestJobs := make([]*jobsdb.JobT, 0)
destJobs := make([]*jobsdb.JobT, 0)
routerDestIDs := make(map[string]struct{})
procErrorJobsByDestID := make(map[string][]*jobsdb.JobT)

proc.config.configSubscriberLock.RLock()
Expand Down Expand Up @@ -2164,6 +2183,7 @@ func (proc *Handle) transformSrcDest(
batchDestJobs: batchDestJobs,
errorsPerDestID: procErrorJobsByDestID,
reportMetrics: reportMetrics,
routerDestIDs: routerDestIDs,
}
}

Expand Down Expand Up @@ -2228,6 +2248,7 @@ func (proc *Handle) transformSrcDest(
batchDestJobs: batchDestJobs,
errorsPerDestID: procErrorJobsByDestID,
reportMetrics: reportMetrics,
routerDestIDs: routerDestIDs,
}
}

Expand Down Expand Up @@ -2374,6 +2395,7 @@ func (proc *Handle) transformSrcDest(
batchDestJobs = append(batchDestJobs, &newJob)
} else {
destJobs = append(destJobs, &newJob)
routerDestIDs[destID] = struct{}{}
}
}
})
Expand All @@ -2382,6 +2404,7 @@ func (proc *Handle) transformSrcDest(
batchDestJobs: batchDestJobs,
errorsPerDestID: procErrorJobsByDestID,
reportMetrics: reportMetrics,
routerDestIDs: routerDestIDs,
}
}

Expand Down