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

[-] fix connection usage for replicas and locked sessions, fixes #565 #568

Merged
merged 1 commit into from
May 23, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
"program": "${workspaceFolder}",
"env": {},
"args": ["postgresql://scheduler@localhost:5432/timetable",
"--clientname=loader",
"--log-level=debug"]
"--clientname=worker001",
"--log-level=debug",
"--timeout=-1"]
}
]
}
29 changes: 16 additions & 13 deletions internal/pgengine/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,22 @@ var sqlNames = []string{"Schema Init", "Cron Functions", "Tables and Views", "JS

// New opens connection and creates schema
func New(ctx context.Context, cmdOpts config.CmdOptions, logger log.LoggerHookerIface) (*PgEngine, error) {
var err error
var (
err error
connctx = ctx
conncancel context.CancelFunc
)
pge := &PgEngine{
l: logger,
ConfigDb: nil,
CmdOptions: cmdOpts,
chainSignalChan: make(chan ChainSignal, 64),
}
pge.l.WithField("sid", pge.Getsid()).Info("Starting new session... ")
connctx, conncancel := context.WithTimeout(ctx, time.Duration(cmdOpts.Connection.Timeout)*time.Second)
defer conncancel()
if cmdOpts.Connection.Timeout > 0 { // Timeout less than 0 allows endless connection attempts
connctx, conncancel = context.WithTimeout(ctx, time.Duration(cmdOpts.Connection.Timeout)*time.Second)
defer conncancel()
}

config := pge.getPgxConnConfig()
if err = retry.Do(connctx, backoff, func(ctx context.Context) error {
Expand Down Expand Up @@ -200,16 +206,13 @@ func (pge *PgEngine) TryLockClientName(ctx context.Context, conn QueryRowIface)
return nil
}
sql = "SELECT timetable.try_lock_client_name($1, $2)"
return retry.Do(ctx, backoff, func(ctx context.Context) error {
var locked bool
if e := conn.QueryRow(ctx, sql, pge.Getsid(), pge.ClientName).Scan(&locked); e != nil {
return e
} else if !locked {
pge.l.Info("Cannot obtain lock for a session")
return retry.RetryableError(errors.New("Cannot obtain lock for a session"))
}
return nil
})
var locked bool
if e := conn.QueryRow(ctx, sql, pge.Getsid(), pge.ClientName).Scan(&locked); e != nil {
return e
} else if !locked {
return errors.New("Cannot obtain lock for a session")
}
return nil
}

// ExecuteCustomScripts executes SQL scripts in files
Expand Down
13 changes: 0 additions & 13 deletions internal/pgengine/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,4 @@ func TestTryLockClientName(t *testing.T) {
m := mockpgconn{r}
assert.NoError(t, pge.TryLockClientName(context.Background(), m))
})

t.Run("retry locking", func(t *testing.T) {
r := &mockpgrow{results: []interface{}{
1, //procoid
false, //locked
false, //locked
false, //locked
}}
m := mockpgconn{r}
ctx, cancel := context.WithTimeout(context.Background(), pgengine.WaitTime*2)
defer cancel()
assert.ErrorIs(t, pge.TryLockClientName(ctx, m), ctx.Err())
})
}