Skip to content

Commit

Permalink
fix: fallback to standar lib json and report
Browse files Browse the repository at this point in the history
  • Loading branch information
lvrach committed Sep 23, 2022
1 parent 9aefaa4 commit 4a1b100
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
23 changes: 22 additions & 1 deletion router/batchrouter/batchrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ func (brt *HandleT) setJobStatus(batchJobs *BatchJobsT, isWarehouse bool, errOcc
}

var parameters JobParametersT
err = json.Unmarshal(job.Parameters, &parameters)
err = TryUnmarshalJSON(job.Parameters, &parameters)
if err != nil {
brt.logger.Error("Unmarshal of job parameters failed. ", string(job.Parameters))
}
Expand Down Expand Up @@ -2410,3 +2410,24 @@ func (brt *HandleT) updateProcessedEventsMetrics(statusList []*jobsdb.JobStatusT
}).Count(count)
}
}

func TryUnmarshalJSON(data []byte, v interface{}) (err error) {
c := cap(data)
l := len(data)

startingData := make([]byte, l)
copy(startingData, data)

defer func() {
if r := recover(); r != nil {
pkgLogger.Warnf("Panic while unmarshalling json: %v", r)
pkgLogger.Warnf(" starting slice: [%d:%d]: %q", l, c, string(startingData))
pkgLogger.Warnf(" leading slice: [%d:%d]: %q", len(data), cap(data), string(data))

err = stdjson.Unmarshal(data, v)
}
}()

err = json.Unmarshal(data, v)
return
}
16 changes: 9 additions & 7 deletions router/batchrouter/batchrouterBenchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
backendconfig "github.com/rudderlabs/rudder-server/config/backend-config"
"github.com/rudderlabs/rudder-server/jobsdb"
mocksFileManager "github.com/rudderlabs/rudder-server/mocks/services/filemanager"
"github.com/rudderlabs/rudder-server/utils/logger"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -46,14 +47,18 @@ func randomString() string {

func Benchmark_JSONUmarshal(b *testing.B) {

config.Load()
logger.Init()
Init()

for i := 0; i < b.N; i++ {

jobs := []*jobsdb.JobT{}
for i := 0; i < 100; i++ {
params := JobParametersT{
EventName: "test",
EventType: "track",
MessageID: string(uuid.Must(uuid.NewV4()).String()),
MessageID: uuid.Must(uuid.NewV4()).String(),

ReceivedAt: "2021-01-01T00:00:00Z",
}
Expand All @@ -69,7 +74,6 @@ func Benchmark_JSONUmarshal(b *testing.B) {

g := errgroup.Group{}
g.Go(func() error {

params := JobParametersT{
EventName: "test",
EventType: "track",
Expand All @@ -91,10 +95,9 @@ func Benchmark_JSONUmarshal(b *testing.B) {
})

g.Go(func() error {

for i, _ := range jobs {
for i := range jobs {
var params JobParametersT
json.Unmarshal(jobs[i].Parameters, &params)
_ = TryUnmarshalJSON(jobs[i].Parameters, &params)
// if err != nil {
// b.Fatal(err)
// }
Expand All @@ -104,8 +107,7 @@ func Benchmark_JSONUmarshal(b *testing.B) {
return nil
})

g.Wait()

_ = g.Wait()
}

}

0 comments on commit 4a1b100

Please sign in to comment.