Skip to content

Commit

Permalink
tools: normalize covermerger db
Browse files Browse the repository at this point in the history
  • Loading branch information
tarasmadan committed Jul 7, 2024
1 parent bc4ebbb commit fd2c629
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
20 changes: 10 additions & 10 deletions tools/syz-bq.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,20 @@ then
exit
fi

db="coverage"

# it also allows to early check gcloud credentials
echo "making sure spanner table 'files' exists"
create_table=$( echo -n '
CREATE TABLE IF NOT EXISTS
files (
"namespace" text,
"repo" text,
"commit" text,
"session" text,
"filepath" text,
"duration" bigint,
"dateto" date,
"instrumented" bigint,
"covered" bigint,
PRIMARY KEY
(duration, dateto, commit, filepath) );')
gcloud spanner databases ddl update coverage --instance=syzbot --project=syzkaller \
(session, filepath) );')
gcloud spanner databases ddl update $db --instance=syzbot --project=syzkaller \
--ddl="$create_table"

echo "making sure spanner table 'merge_history' exists"
Expand All @@ -73,13 +71,15 @@ CREATE TABLE IF NOT EXISTS
merge_history (
"namespace" text,
"repo" text,
"commit" text,
"duration" bigint,
"dateto" date,
"session" text,
"time" timestamptz,
"commit" text,
"totalrows" bigint,
PRIMARY KEY
(duration, dateto, commit) );')
gcloud spanner databases ddl update coverage --instance=syzbot --project=syzkaller \
(namespace, repo, duration, dateto) );')
gcloud spanner databases ddl update $db --instance=syzbot --project=syzkaller \
--ddl="$create_table"

echo "Workdir: $workdir"
Expand Down
29 changes: 14 additions & 15 deletions tools/syz-covermerger/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ package main
import (
"context"
"fmt"
"time"

"cloud.google.com/go/civil"
"cloud.google.com/go/spanner"
"github.com/google/uuid"
)

// TODO: move to dashAPI once tested? I'm not sure we'll benefit.

type DBRecord struct {
Namespace string
Repo string
Commit string
Duration int64
DateTo civil.Date
type DBFilesRecord struct {
Session string
FilePath string
Instrumented int64
Covered int64
}

type DBHistoryRecord struct {
Session string
Time time.Time
Namespace string
Repo string
Commit string
Expand All @@ -34,33 +34,30 @@ type DBHistoryRecord struct {
}

func saveToSpanner(ctx context.Context, projectID string, coverage map[string]*Coverage,
template *DBRecord, totalRows int64) {
template *DBHistoryRecord, totalRows int64) {
client, err := spanner.NewClient(ctx, "projects/"+projectID+"/instances/syzbot/databases/coverage")
if err != nil {
panic(fmt.Sprintf("spanner.NewClient() failed: %s", err.Error()))
}
defer client.Close()

session := uuid.New().String()
mutations := []*spanner.Mutation{}
for filePath, record := range coverage {
var insert *spanner.Mutation
if insert, err = spanner.InsertOrUpdateStruct("files", &DBRecord{
Namespace: template.Namespace,
Repo: template.Repo,
Commit: template.Commit,
Duration: template.Duration,
DateTo: template.DateTo,
if insert, err = spanner.InsertOrUpdateStruct("files", &DBFilesRecord{
Session: session,
FilePath: filePath,
Instrumented: record.Instrumented,
Covered: record.Covered,
}); err != nil {
panic(fmt.Sprintf("failed to spanner.InsertStruct(): %s", err.Error()))
}
mutations = append(mutations, insert)
// 80k mutations is a DB limit. 7 fields * 1k records is apx 7k mutations
// 80k mutations is a DB limit. 4 fields * 2k records is apx 8k mutations
// let keep this value 10x lower to have a room for indexes
// indexes update are also counted
if len(mutations) > 1000 {
if len(mutations) > 2000 {
if _, err = client.Apply(ctx, mutations); err != nil {
panic(fmt.Sprintf("failed to spanner.Apply(inserts): %s", err.Error()))
}
Expand All @@ -70,6 +67,8 @@ func saveToSpanner(ctx context.Context, projectID string, coverage map[string]*C

var historyInsert *spanner.Mutation
if historyInsert, err = spanner.InsertOrUpdateStruct("merge_history", &DBHistoryRecord{
Session: session,
Time: time.Now(),
Namespace: template.Namespace,
Repo: template.Repo,
Commit: template.Commit,
Expand Down
2 changes: 1 addition & 1 deletion tools/syz-covermerger/syz_covermerger.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func main() {
}
coverage, _, _ := mergeResultsToCoverage(mergeResult)
saveToSpanner(context.Background(), *flagProjectID, coverage,
&DBRecord{
&DBHistoryRecord{
Namespace: *flagNamespace,
Repo: *flagRepo,
Commit: *flagCommit,
Expand Down

0 comments on commit fd2c629

Please sign in to comment.