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(jobsdb): use a different advisory lock for different table prefixes #2575

Merged
merged 1 commit into from
Oct 15, 2022
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
14 changes: 12 additions & 2 deletions jobsdb/jobsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ package jobsdb
import (
"bytes"
"context"
"crypto/sha256"
"database/sql"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -2913,6 +2915,7 @@ Other functions are impacted by movement of data across DS in background
so take both the list and data lock
*/
func (jd *HandleT) addNewDSLoop(ctx context.Context) {
advisoryLock := jd.getAdvisoryLockForOperation("add_ds")
for {
select {
case <-ctx.Done():
Expand All @@ -2926,10 +2929,10 @@ func (jd *HandleT) addNewDSLoop(ctx context.Context) {
// start a transaction
err := jd.WithTx(func(tx *Tx) error {
// acquire a advisory transaction level blocking lock, which is released once the transaction ends.
sqlStatement := fmt.Sprintf(`SELECT pg_advisory_xact_lock(%d);`, misc.JobsDBAddDsAdvisoryLock)
sqlStatement := fmt.Sprintf(`SELECT pg_advisory_xact_lock(%d);`, advisoryLock)
_, err := tx.ExecContext(context.TODO(), sqlStatement)
if err != nil {
return fmt.Errorf("error while acquiring advisory lock %d: %w", misc.JobsDBAddDsAdvisoryLock, err)
return fmt.Errorf("error while acquiring advisory lock %d: %w", advisoryLock, err)
}

// We acquire the list lock only after we have acquired the advisory lock.
Expand Down Expand Up @@ -2975,6 +2978,13 @@ func (jd *HandleT) addNewDSLoop(ctx context.Context) {
}
}

func (jd *HandleT) getAdvisoryLockForOperation(operation string) int64 {
key := fmt.Sprintf("%s_%s", jd.tablePrefix, operation)
h := sha256.New()
h.Write([]byte(key))
return int64(binary.BigEndian.Uint32(h.Sum(nil)))
}

func setReadonlyDsInTx(tx *Tx, latestDS dataSetT) error {
sqlStatement := fmt.Sprintf(
`CREATE TRIGGER readonlyTableTrg
Expand Down
15 changes: 15 additions & 0 deletions jobsdb/jobsdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,21 @@ func Test_SortDnumList(t *testing.T) {
require.Equal(t, []string{"-2", "0_1", "0_1_1", "1"}, l)
}

func Test_GetAdvisoryLockForOperation_Unique(t *testing.T) {
calculated := map[int64]string{}
for _, operation := range []string{"add_ds", "migrate_ds"} {
for _, prefix := range []string{"gw", "rt", "batch_rt", "proc_error"} {
h := &HandleT{tablePrefix: prefix}
key := fmt.Sprintf("%s_%s", prefix, operation)
advLock := h.getAdvisoryLockForOperation(operation)
if dupKey, ok := calculated[advLock]; ok {
t.Errorf("Duplicate advisory lock calculated for different keys %s and %s: %d", key, dupKey, advLock)
}
calculated[advLock] = key
}
}
}

type testingT interface {
Errorf(format string, args ...interface{})
FailNow()
Expand Down
6 changes: 0 additions & 6 deletions utils/misc/dbutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import (
"github.com/rudderlabs/rudder-server/config"
)

type AdvisoryLock int

const (
JobsDBAddDsAdvisoryLock AdvisoryLock = 11
)

// GetConnectionString Returns Jobs DB connection configuration
func GetConnectionString() string {
host := config.GetString("DB.host", "localhost")
Expand Down