Skip to content
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
11 changes: 10 additions & 1 deletion gateway/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,9 +1142,18 @@ func (gw *Handle) storeJobs(ctx context.Context, jobs []*jobsdb.JobT) error {
// rsources stats
rsourcesStats := rsources.NewStatsCollector(
gw.rsourcesService,
"gw",
gw.stats,
rsources.IgnoreDestinationID(),
)
rsourcesStats.JobsStoredWithErrors(jobs, nil)
return rsourcesStats.Publish(ctx, tx.SqlTx())
err := rsourcesStats.Publish(ctx, tx.SqlTx())
if err != nil {
gw.logger.Errorn("Failed to publish rsources stats",
obskit.Error(err),
)
return err
}
return nil
})
}
2 changes: 1 addition & 1 deletion gateway/handle_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func (gw *Handle) dbWriterWorkerProcess() {
}

// rsources stats
rsourcesStats := rsources.NewStatsCollector(gw.rsourcesService, rsources.IgnoreDestinationID())
rsourcesStats := rsources.NewStatsCollector(gw.rsourcesService, "gw", gw.stats, rsources.IgnoreDestinationID())
rsourcesStats.JobsStoredWithErrors(lo.Flatten(jobBatches), errorMessagesMap)
return rsourcesStats.Publish(ctx, tx.SqlTx())
})
Expand Down
3 changes: 2 additions & 1 deletion jobsdb/jobsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,7 @@ func (jd *Handle) WithTx(f func(tx *Tx) error) error {
err = f(tx)
if err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
return fmt.Errorf("%w; %s", err, rollbackErr)
return fmt.Errorf("%w; rollback error: %s", err, rollbackErr)
}
return err
}
Expand Down Expand Up @@ -2449,6 +2449,7 @@ func (jd *Handle) addNewDSLoop(ctx context.Context) {
if err != nil {
return err
}
jd.logger.Infon("Acquired lock", logger.NewField("ds", latestDS), logger.NewStringField("jobsdb", jd.tablePrefix))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How often would you say we would get these printed out? I'm asking because I'm a bit afraid of noise/pollution.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not very often we are printing this everytime we create a new table. we have a log here as well

if _, err = tx.Exec(fmt.Sprintf(`LOCK TABLE %q IN EXCLUSIVE MODE;`, latestDS.JobTable)); err != nil {
return fmt.Errorf("error locking table %s: %w", latestDS.JobTable, err)
}
Expand Down
28 changes: 15 additions & 13 deletions processor/partition_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ import (
)

type partitionWorker struct {
partition string
pipelines []*pipelineWorker
logger logger.Logger
stats *processorStats
tracer *tracing.Tracer
handle workerHandle
partition string
pipelines []*pipelineWorker
logger logger.Logger
stats *processorStats
tracer *tracing.Tracer
handle workerHandle
statsFactory stats.Stats
}

// newPartitionWorker creates a new worker for the specified partition
func newPartitionWorker(partition string, h workerHandle, t stats.Tracer) *partitionWorker {
func newPartitionWorker(partition string, h workerHandle, t stats.Tracer, statsFactory stats.Stats) *partitionWorker {
w := &partitionWorker{
partition: partition,
logger: h.logger().Child(partition),
stats: h.stats(),
tracer: tracing.New(t, tracing.WithNamePrefix("partitionWorker")),
handle: h,
partition: partition,
logger: h.logger().Child(partition),
stats: h.stats(),
tracer: tracing.New(t, tracing.WithNamePrefix("partitionWorker")),
handle: h,
statsFactory: statsFactory,
}
// Create workers for each pipeline
pipelinesPerPartition := h.config().pipelinesPerPartition
Expand Down Expand Up @@ -131,7 +133,7 @@ func (w *partitionWorker) sendToPreProcess(ctx context.Context, jobsByPipeline m
partition := pipelineIdx
jobs := pipelineJobs
// Initialize rsources stats
rsourcesStats := rsources.NewStatsCollector(w.handle.rsourcesService(), rsources.IgnoreDestinationID())
rsourcesStats := rsources.NewStatsCollector(w.handle.rsourcesService(), "processor", w.statsFactory, rsources.IgnoreDestinationID())
rsourcesStats.BeginProcessing(jobs)

g.Go(func() error {
Expand Down
6 changes: 3 additions & 3 deletions processor/partition_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestWorkerPool(t *testing.T) {

// create a worker pool
wp := workerpool.New(poolCtx, func(partition string) workerpool.Worker {
return newPartitionWorker(partition, wh, stats.NOP.NewTracer(""))
return newPartitionWorker(partition, wh, stats.NOP.NewTracer(""), stats.NOP)
}, logger.NOP)

// start pinging for work for 100 partitions
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestWorkerPoolIdle(t *testing.T) {
// create a worker pool
wp := workerpool.New(poolCtx,
func(partition string) workerpool.Worker {
return newPartitionWorker(partition, wh, stats.NOP.NewTracer(""))
return newPartitionWorker(partition, wh, stats.NOP.NewTracer(""), stats.NOP)
},
logger.NOP,
workerpool.WithCleanupPeriod(200*time.Millisecond),
Expand Down Expand Up @@ -222,7 +222,7 @@ func (m *mockWorkerHandle) handlePendingGatewayJobs(partition string) bool {
if len(jobs.Jobs) > 0 {
_ = m.markExecuting(ctx, partition, jobs.Jobs)
}
rsourcesStats := rsources.NewStatsCollector(m.rsourcesService(), rsources.IgnoreDestinationID())
rsourcesStats := rsources.NewStatsCollector(m.rsourcesService(), "test", stats.NOP, rsources.IgnoreDestinationID())
for _, subJob := range m.jobSplitter(ctx, jobs.Jobs, rsourcesStats) {
var dest *transformationMessage
var err error
Expand Down
8 changes: 5 additions & 3 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ func (proc *Handle) Start(ctx context.Context) error {

h := &workerHandleAdapter{proc}
pool := workerpool.New(ctx, func(partition string) workerpool.Worker {
return newPartitionWorker(partition, h, proc.statsFactory.NewTracer("partitionWorker"))
return newPartitionWorker(partition, h, proc.statsFactory.NewTracer("partitionWorker"), proc.statsFactory)
}, proc.logger)
defer pool.Shutdown()
for {
Expand Down Expand Up @@ -3547,6 +3547,8 @@ func (proc *Handle) saveDroppedJobs(ctx context.Context, droppedJobs []*jobsdb.J
}
rsourcesStats := rsources.NewDroppedJobsCollector(
proc.rsourcesService,
"processor",
proc.statsFactory,
rsources.IgnoreDestinationID(),
)
rsourcesStats.JobsDropped(droppedJobs)
Expand Down Expand Up @@ -3786,7 +3788,7 @@ func (proc *Handle) handlePendingGatewayJobs(partition string) bool {
return false
}

rsourcesStats := rsources.NewStatsCollector(proc.rsourcesService, rsources.IgnoreDestinationID())
rsourcesStats := rsources.NewStatsCollector(proc.rsourcesService, "processor", proc.statsFactory, rsources.IgnoreDestinationID())
rsourcesStats.BeginProcessing(unprocessedList.Jobs)

var transMessage *transformationMessage
Expand Down Expand Up @@ -3858,7 +3860,7 @@ func (proc *Handle) isReportingEnabled() bool {
}

func (proc *Handle) updateRudderSourcesStats(ctx context.Context, tx jobsdb.StoreSafeTx, jobs []*jobsdb.JobT) error {
rsourcesStats := rsources.NewStatsCollector(proc.rsourcesService)
rsourcesStats := rsources.NewStatsCollector(proc.rsourcesService, "processor", proc.statsFactory)
rsourcesStats.JobsStored(jobs)
err := rsourcesStats.Publish(ctx, tx.SqlTx())
return err
Expand Down
2 changes: 1 addition & 1 deletion router/batchrouter/handle_observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (brt *Handle) updateRudderSourcesStats(
jobs []*jobsdb.JobT,
jobStatuses []*jobsdb.JobStatusT,
) error {
rsourcesStats := rsources.NewStatsCollector(brt.rsourcesService)
rsourcesStats := rsources.NewStatsCollector(brt.rsourcesService, "brt", stats.Default)
rsourcesStats.BeginProcessing(jobs)
rsourcesStats.CollectStats(jobStatuses)
rsourcesStats.CollectFailedRecords(jobStatuses)
Expand Down
2 changes: 1 addition & 1 deletion router/handle_observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (rt *Handle) updateRudderSourcesStats(
jobs []*jobsdb.JobT,
jobStatuses []*jobsdb.JobStatusT,
) error {
rsourcesStats := rsources.NewStatsCollector(rt.rsourcesService)
rsourcesStats := rsources.NewStatsCollector(rt.rsourcesService, "rt", stats.Default)
rsourcesStats.BeginProcessing(jobs)
rsourcesStats.CollectStats(jobStatuses)
rsourcesStats.CollectFailedRecords(jobStatuses)
Expand Down
3 changes: 3 additions & 0 deletions runner/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,8 @@ var (
"warehouse_dest_transform_output_failed_events": {
1, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 12500, 15000, 30000, 50000, 75000, 100000,
},
"rsources_publish_time_second": {
0.1, 0.5, 1, 5, 10, 30, 60,
},
}
)
16 changes: 13 additions & 3 deletions services/rsources/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

obskit "github.com/rudderlabs/rudder-observability-kit/go/labels"

"github.com/lib/pq"
"github.com/segmentio/ksuid"

Expand Down Expand Up @@ -107,8 +109,10 @@ func (*sourcesHandler) IncrementStats(ctx context.Context, tx *sql.Tx, jobRunId
sqlStatement,
jobRunId, key.TaskRunID, key.SourceID, key.DestinationID,
stats.In, stats.Out, stats.Failed)

return err
if err != nil {
return fmt.Errorf("inserting into rsources_stats: %w", err)
}
return nil
}

func (sh *sourcesHandler) AddFailedRecords(ctx context.Context, tx *sql.Tx, jobRunId string, key JobTargetKey, records []FailedRecord) error {
Expand Down Expand Up @@ -417,13 +421,15 @@ func (sh *sourcesHandler) CleanupLoop(ctx context.Context) error {
case <-sh.cleanupTrigger():
err := sh.doCleanupTables(ctx)
if err != nil {
sh.log.Errorn("Failed to cleanup rsources tables", obskit.Error(err))
return err
}
}
}
}

func (sh *sourcesHandler) doCleanupTables(ctx context.Context) error {
sh.log.Infon("Cleaning up rsources tables")
tx, err := sh.localDB.Begin()
if err != nil {
return err
Expand Down Expand Up @@ -453,7 +459,11 @@ func (sh *sourcesHandler) doCleanupTables(ctx context.Context) error {
_ = tx.Rollback()
return err
}
return tx.Commit()
if commitErr := tx.Commit(); commitErr != nil {
return commitErr
}
sh.log.Infon("Finished cleaning up rsources tables")
return nil
}

func (sh *sourcesHandler) readDB() *sql.DB {
Expand Down
21 changes: 17 additions & 4 deletions services/rsources/stats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/rudderlabs/rudder-go-kit/stats"

"github.com/google/uuid"
"github.com/tidwall/gjson"

"github.com/rudderlabs/rudder-server/jobsdb"
)

const rsourcesPublishTime = "rsources_publish_time_second"

// StatsPublisher publishes stats
type StatsPublisher interface {
// Publish publishes statistics
Expand Down Expand Up @@ -55,7 +60,7 @@ type FailedJobsStatsCollector interface {
}

// NewStatsCollector creates a new stats collector
func NewStatsCollector(jobservice JobService, opts ...OptFunc) StatsCollector {
func NewStatsCollector(jobservice JobService, component string, statFactory stats.Stats, opts ...OptFunc) StatsCollector {
sc := &statsCollector{
jobService: jobservice,
jobIdsToStatKeyIndex: map[int64]statKey{},
Expand All @@ -67,11 +72,12 @@ func NewStatsCollector(jobservice JobService, opts ...OptFunc) StatsCollector {
for _, opt := range opts {
opt(sc)
}
sc.stats.publishTime = statFactory.NewTaggedStat(rsourcesPublishTime, stats.TimerType, stats.Tags{"module": component})

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this a new metric? Do we have defined buckets for it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes its a new metric, I will add buckets for this

return sc
}

// NewDroppedJobsCollector creates a new stats collector for publishing failed job stats and records
func NewDroppedJobsCollector(jobservice JobService, opts ...OptFunc) FailedJobsStatsCollector {
func NewDroppedJobsCollector(jobservice JobService, component string, statFactory stats.Stats, opts ...OptFunc) FailedJobsStatsCollector {
sc := &statsCollector{
jobService: jobservice,
jobIdsToStatKeyIndex: map[int64]statKey{},
Expand All @@ -83,6 +89,7 @@ func NewDroppedJobsCollector(jobservice JobService, opts ...OptFunc) FailedJobsS
for _, opt := range opts {
opt(sc)
}
sc.stats.publishTime = statFactory.NewTaggedStat(rsourcesPublishTime, stats.TimerType, stats.Tags{"module": component})
return sc
}

Expand All @@ -105,6 +112,9 @@ type statsCollector struct {
statsIndex map[statKey]*Stats
failedRecordsIndex map[statKey][]FailedRecord
parametersParser parametersParser
stats struct {
publishTime stats.Timer
}
}

func (r *statsCollector) orderedStatMapKeys() []statKey {
Expand Down Expand Up @@ -206,6 +216,8 @@ func (r *statsCollector) Publish(ctx context.Context, tx *sql.Tx) error {
if r.jobService == nil {
return fmt.Errorf("no JobService provided during initialization")
}
startTime := time.Now()

// sort the maps to avoid deadlocks
statKeys := r.orderedStatMapKeys()
for i := range statKeys {
Expand All @@ -216,7 +228,7 @@ func (r *statsCollector) Publish(ctx context.Context, tx *sql.Tx) error {
}
err := r.jobService.IncrementStats(ctx, tx, k.jobRunId, k.JobTargetKey, *v)
if err != nil {
return err
return fmt.Errorf("failed to increment stats for job %s, idx %d,: %w", k.String(), i, err)
}
}
failedRecordsKeys := r.orderedFailedRecordsKeys()
Expand All @@ -229,9 +241,10 @@ func (r *statsCollector) Publish(ctx context.Context, tx *sql.Tx) error {
})
err := r.jobService.AddFailedRecords(ctx, tx, k.jobRunId, k.JobTargetKey, v)
if err != nil {
return err
return fmt.Errorf("failed to add failed records for job %s: %w", k.String(), err)
}
}
r.stats.publishTime.SendTiming(time.Since(startTime))

return nil
}
Expand Down
Loading