Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a9bf9d

Browse files
server-writer-fixes (#540)
Summary: - Add mutex protection for handler context clone. - Add mutex protection for handler context error stream. - Added robot test `PG Session Debug Behaviour Canonical`. - Added robot test `PG Session Debug Off Behaviour Canonical`. - Version differences `psql` accounted for.
1 parent 783e018 commit 6a9bf9d

32 files changed

+31057
-284
lines changed

.vscode/launch.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,16 @@
355355
"true",
356356
"false"
357357
]
358+
},
359+
{
360+
"type": "pickString",
361+
"id": "serverDebugPublish",
362+
"description": "postgres server publish debug messages",
363+
"default": "true",
364+
"options": [
365+
"true",
366+
"false"
367+
]
358368
}
359369
],
360370
"configurations": [
@@ -436,7 +446,8 @@
436446
"--namespaces=${input:namespaceString}",
437447
"--sqlBackend=${input:sqlBackendString}",
438448
"--dbInternal=${input:dbInternalString}",
439-
"--export.alias=${input:exportAliasString}"
449+
"--export.alias=${input:exportAliasString}",
450+
"--pgsrv.debug.enable=${input:serverDebugPublish}",
440451
],
441452
},
442453
{
@@ -456,7 +467,8 @@
456467
"--sqlBackend=${input:sqlBackendString}",
457468
"--dbInternal=${input:dbInternalString}",
458469
"--pgsrv.tls=${input:pgsrvTlsString}",
459-
"--export.alias=${input:exportAliasString}"
470+
"--export.alias=${input:exportAliasString}",
471+
"--pgsrv.debug.enable=${input:serverDebugPublish}",
460472
],
461473
},
462474
{

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ require (
1919
github.com/spf13/cobra v1.4.0
2020
github.com/spf13/pflag v1.0.5
2121
github.com/spf13/viper v1.10.1
22-
github.com/stackql/any-sdk v0.1.2-alpha37
22+
github.com/stackql/any-sdk v0.1.2-beta01
2323
github.com/stackql/go-suffix-map v0.0.1-alpha01
24-
github.com/stackql/psql-wire v0.1.1-alpha07
24+
github.com/stackql/psql-wire v0.1.1-beta23
2525
github.com/stackql/stackql-parser v0.0.14-alpha05
2626
github.com/stretchr/testify v1.10.0
2727
golang.org/x/sync v0.10.0

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,12 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
482482
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
483483
github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk=
484484
github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU=
485-
github.com/stackql/any-sdk v0.1.2-alpha37 h1:bvU576YCwqtjztMD4wSpgj//StNH8qGoBRBnN6uOH1I=
486-
github.com/stackql/any-sdk v0.1.2-alpha37/go.mod h1:4jYKpPoX2GWEK+qBnlGLvr8SUfndiYwHMIkg1dn9tFM=
485+
github.com/stackql/any-sdk v0.1.2-beta01 h1:fmvsPGmjrcRRdgNsXJrp6TL7nDzPub0LmkIhPsW0tnc=
486+
github.com/stackql/any-sdk v0.1.2-beta01/go.mod h1:4jYKpPoX2GWEK+qBnlGLvr8SUfndiYwHMIkg1dn9tFM=
487487
github.com/stackql/go-suffix-map v0.0.1-alpha01 h1:TDUDS8bySu41Oo9p0eniUeCm43mnRM6zFEd6j6VUaz8=
488488
github.com/stackql/go-suffix-map v0.0.1-alpha01/go.mod h1:QAi+SKukOyf4dBtWy8UMy+hsXXV+yyEE4vmBkji2V7g=
489-
github.com/stackql/psql-wire v0.1.1-alpha07 h1:LQWVUlx4Bougk6dztDNG5tmXxpIVeeTSsInTj801xCs=
490-
github.com/stackql/psql-wire v0.1.1-alpha07/go.mod h1:a44Wd8kDC3irFLpGutarKDBqhJ/aqXlj1aMzO5bVJYg=
489+
github.com/stackql/psql-wire v0.1.1-beta23 h1:1ayYMjZArfDcIMyEOKnm+Bp1zRCISw8pguvTFuUhhVQ=
490+
github.com/stackql/psql-wire v0.1.1-beta23/go.mod h1:a44Wd8kDC3irFLpGutarKDBqhJ/aqXlj1aMzO5bVJYg=
491491
github.com/stackql/readline v0.0.2-alpha05 h1:ID4QzGdplFBsrSnTuz8pvKzWw96JbrJg8fsLry2UriU=
492492
github.com/stackql/readline v0.0.2-alpha05/go.mod h1:OFAYOdXk/X4+5GYiDXFfaGrk+bCN6Qv0SYY5HNzD2E0=
493493
github.com/stackql/stackql-go-sqlite3 v1.0.3-stackql h1:j0yt6T5thZuz5+HIr81PXz2AClAtCko0vzr5tm8C1g8=

internal/stackql/bundle/bundle.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bundle
22

33
import (
4+
"io"
5+
46
"github.com/stackql/any-sdk/pkg/dto"
57
"github.com/stackql/stackql-parser/go/vt/sqlparser"
68
"github.com/stackql/stackql/internal/stackql/acid/txn_context"
@@ -30,6 +32,10 @@ type Bundle interface {
3032
GetTxnCoordinatorContext() txn_context.ITransactionCoordinatorContext
3133
GetTypingConfig() typing.Config
3234
GetSessionContext() dto.SessionContext
35+
GetStdOut() (io.Writer, bool)
36+
GetStdErr() (io.Writer, bool)
37+
WithStdOut(io.Writer) Bundle
38+
WithStdErr(io.Writer) Bundle
3339
}
3440

3541
func NewBundle(
@@ -80,6 +86,26 @@ type simpleBundle struct {
8086
authContexts map[string]*dto.AuthCtx
8187
txnCoordintatorContext txn_context.ITransactionCoordinatorContext
8288
sessionCtx dto.SessionContext
89+
stdOut io.Writer
90+
stdErr io.Writer
91+
}
92+
93+
func (sb *simpleBundle) WithStdOut(w io.Writer) Bundle {
94+
sb.stdOut = w
95+
return sb
96+
}
97+
98+
func (sb *simpleBundle) WithStdErr(w io.Writer) Bundle {
99+
sb.stdErr = w
100+
return sb
101+
}
102+
103+
func (sb *simpleBundle) GetStdOut() (io.Writer, bool) {
104+
return sb.stdOut, sb.stdOut != nil
105+
}
106+
107+
func (sb *simpleBundle) GetStdErr() (io.Writer, bool) {
108+
return sb.stdErr, sb.stdErr != nil
83109
}
84110

85111
func (sb *simpleBundle) GetSessionContext() dto.SessionContext {

internal/stackql/cmd/exec.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/stackql/stackql/internal/stackql/entryutil"
2828
"github.com/stackql/stackql/internal/stackql/handler"
2929
"github.com/stackql/stackql/internal/stackql/iqlerror"
30-
"github.com/stackql/stackql/internal/stackql/writer"
3130
)
3231

3332
// execCmd represents the exec command.
@@ -78,27 +77,16 @@ stackql exec -i iqlscripts/create-disk.iql --credentialsfilepath /mnt/c/tmp/stac
7877
}
7978
inputBundle, err := entryutil.BuildInputBundle(runtimeCtx)
8079
iqlerror.PrintErrorAndExitOneIfError(err)
81-
handlerCtx, err := entryutil.BuildHandlerContext(runtimeCtx, rdr, queryCache, inputBundle)
80+
handlerCtx, err := entryutil.BuildHandlerContext(runtimeCtx, rdr, queryCache, inputBundle, true)
8281
iqlerror.PrintErrorAndExitOneIfError(err)
8382
iqlerror.PrintErrorAndExitOneIfNil(handlerCtx, "Handler context error")
8483
cr := newCommandRunner()
85-
cr.RunCommand(handlerCtx, nil, nil)
84+
cr.RunCommand(handlerCtx)
8685
},
8786
}
8887

89-
func getOutputFile(filename string) (*os.File, error) {
90-
switch filename {
91-
case "stdout":
92-
return os.Stdout, nil
93-
case "stderr":
94-
return os.Stderr, nil
95-
default:
96-
return os.Create(filename)
97-
}
98-
}
99-
10088
type commandRunner interface {
101-
RunCommand(handlerCtx handler.HandlerContext, outfile io.Writer, outErrFile io.Writer)
89+
RunCommand(handlerCtx handler.HandlerContext)
10290
}
10391

10492
func newCommandRunner() commandRunner {
@@ -108,16 +96,7 @@ func newCommandRunner() commandRunner {
10896
type commandRunnerImpl struct {
10997
}
11098

111-
func (cr *commandRunnerImpl) RunCommand(handlerCtx handler.HandlerContext, outfile io.Writer, outErrFile io.Writer) {
112-
defer iqlerror.HandlePanic(outErrFile)
113-
if outfile == nil {
114-
outfile, _ = getOutputFile(handlerCtx.GetRuntimeContext().OutfilePath)
115-
}
116-
if outErrFile == nil {
117-
outErrFile, _ = getOutputFile(writer.StdErrStr)
118-
}
119-
handlerCtx.SetOutfile(outfile)
120-
handlerCtx.SetOutErrFile(outErrFile)
99+
func (cr *commandRunnerImpl) RunCommand(handlerCtx handler.HandlerContext) {
121100
stackqlDriver, err := driver.NewStackQLDriver(handlerCtx)
122101
iqlerror.PrintErrorAndExitOneIfError(err)
123102
if handlerCtx.GetRuntimeContext().DryRunFlag {

internal/stackql/cmd/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ var registryCmd = &cobra.Command{
8787

8888
inputBundle, err := entryutil.BuildInputBundle(runtimeCtx)
8989
iqlerror.PrintErrorAndExitOneIfError(err)
90-
handlerCtx, err := entryutil.BuildHandlerContext(runtimeCtx, rdr, queryCache, inputBundle)
90+
handlerCtx, err := entryutil.BuildHandlerContext(runtimeCtx, rdr, queryCache, inputBundle, true)
9191
iqlerror.PrintErrorAndExitOneIfError(err)
9292
iqlerror.PrintErrorAndExitOneIfNil(handlerCtx, "Handler context error")
9393
cr := newCommandRunner()
94-
cr.RunCommand(handlerCtx, nil, nil)
94+
cr.RunCommand(handlerCtx)
9595

9696
},
9797
}

internal/stackql/cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func init() {
156156
rootCmd.PersistentFlags().StringVar(&runtimeCtx.HTTPProxyUser, dto.HTTPProxyUserKey, "", "http proxy user")
157157
rootCmd.PersistentFlags().StringVar(&runtimeCtx.ProviderStr, dto.ProviderStrKey, "", "stackql provider")
158158
rootCmd.PersistentFlags().StringVar(&runtimeCtx.ExportAlias, dto.ExportAliasKey, "", "export alias prefix (namespace or schema)")
159+
rootCmd.PersistentFlags().BoolVar(&runtimeCtx.PGSrvIsDebugNoticesEnabled, dto.PgSrvIsDebugNoticesEnabledKey, false, "Publish debug notices to clients, for server mode only")
159160
rootCmd.PersistentFlags().BoolVar(&runtimeCtx.WorkOffline, dto.WorkOfflineKey, false, "Work offline, using cached data")
160161
rootCmd.PersistentFlags().BoolVarP(&runtimeCtx.VerboseFlag, dto.VerboseFlagKey, "v", false, "Verbose flag")
161162
rootCmd.PersistentFlags().BoolVar(&runtimeCtx.DryRunFlag, dto.DryRunFlagKey, false, "dryrun flag; preprocessor only will run and output returned")
@@ -176,6 +177,7 @@ func init() {
176177
rootCmd.PersistentFlags().StringVar(&runtimeCtx.PGSrvAddress, dto.PgSrvAddressKey, "0.0.0.0", "server address, for server mode only")
177178
rootCmd.PersistentFlags().StringVar(&runtimeCtx.PGSrvLogLevel, dto.PgSrvLogLevelKey, "WARN", "Log level, for server mode only")
178179
rootCmd.PersistentFlags().StringVar(&runtimeCtx.PGSrvRawTLSCfg, dto.PgSrvRawTLSCfgKey, "", "tls config for server, for server mode only")
180+
rootCmd.PersistentFlags().StringVar(&runtimeCtx.PGSrvRawSrvCfg, dto.PgSrvRawSrvCfgKey, "{}", "miscellaneous config for server, for server mode only")
179181
rootCmd.PersistentFlags().IntVar(&runtimeCtx.PGSrvPort, dto.PgSrvPortKey, 5466, "TCP server port, for server mode only") //nolint:mnd // TODO: investigate
180182

181183
rootCmd.PersistentFlags().StringSliceVar(&runtimeCtx.VarList, dto.VarListKey, []string{}, "list of variables to be used in queries")

internal/stackql/cmd/shell.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ var shellCmd = &cobra.Command{
125125

126126
inputBundle, err := entryutil.BuildInputBundle(runtimeCtx)
127127
iqlerror.PrintErrorAndExitOneIfError(err)
128+
inputBundle.WithStdOut(outfile).WithStdErr(outErrFile)
128129

129-
handlerCtx, handlerrErr := handler.GetHandlerCtx("", runtimeCtx, queryCache, inputBundle)
130+
handlerCtx, handlerrErr := handler.NewHandlerCtx("", runtimeCtx, queryCache, inputBundle)
130131
if handlerrErr != nil {
131132
fmt.Fprintln( //nolint:gosimple // legacy
132133
outErrFile,
@@ -260,21 +261,6 @@ func newSessionRunner(
260261
outfile io.Writer,
261262
outErrFile io.Writer,
262263
) (sessionRunner, error) {
263-
var err error
264-
if outfile == nil {
265-
outfile, err = getOutputFile(handlerCtx.GetRuntimeContext().OutfilePath)
266-
if err != nil {
267-
return nil, err
268-
}
269-
}
270-
if outErrFile == nil {
271-
outErrFile, err = getOutputFile(writer.StdErrStr)
272-
if err != nil {
273-
return nil, err
274-
}
275-
}
276-
handlerCtx.SetOutfile(outfile)
277-
handlerCtx.SetOutErrFile(outErrFile)
278264
stackqlDriver, driverErr := driver.NewStackQLDriver(handlerCtx)
279265
if driverErr != nil {
280266
return nil, driverErr

internal/stackql/cmd/srv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ var srvCmd = &cobra.Command{
4343
iqlerror.PrintErrorAndExitOneIfError(flagErr)
4444
inputBundle, err := entryutil.BuildInputBundle(runtimeCtx)
4545
iqlerror.PrintErrorAndExitOneIfError(err)
46-
handlerCtx, err := entryutil.BuildHandlerContextNoPreProcess(runtimeCtx, queryCache, inputBundle)
46+
handlerCtx, err := entryutil.BuildHandlerContext(runtimeCtx, nil, queryCache, inputBundle, false)
4747
iqlerror.PrintErrorAndExitOneIfError(err)
48-
sbe := driver.NewStackQLDriverFactory(handlerCtx)
48+
sbe := driver.NewStackQLDriverFactory(handlerCtx, runtimeCtx.PGSrvIsDebugNoticesEnabled)
4949
server, err := psqlwire.MakeWireServer(sbe, runtimeCtx)
5050
iqlerror.PrintErrorAndExitOneIfError(err)
5151
server.Serve() //nolint:errcheck // TODO: investigate

internal/stackql/driver/aggregation_compute_disks_integration_test.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ func TestSelectComputeDisksOrderByCrtTmstpAsc(t *testing.T) {
3535
}
3636

3737
testSubject := func(t *testing.T, outFile *bufio.Writer) {
38-
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle)
39-
handlerCtx.SetOutfile(os.Stdout)
40-
handlerCtx.SetOutErrFile(os.Stderr)
38+
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle.WithStdOut(outFile), true)
4139
if err != nil {
4240
t.Fatalf("Test failed: %v", err)
4341
}
@@ -54,7 +52,6 @@ func TestSelectComputeDisksOrderByCrtTmstpAsc(t *testing.T) {
5452
t.Fatalf("Test failed: %v", prepareErr)
5553
}
5654
response := querySubmitter.SubmitQuery()
57-
handlerCtx.SetOutfile(outFile)
5855
responsehandler.HandleResponse(handlerCtx, response)
5956

6057
dr.ProcessQuery(handlerCtx.GetRawQuery())
@@ -76,9 +73,7 @@ func TestSelectComputeDisksAggOrderBySizeAsc(t *testing.T) {
7673
}
7774

7875
testSubject := func(t *testing.T, outFile *bufio.Writer) {
79-
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle)
80-
handlerCtx.SetOutfile(os.Stdout)
81-
handlerCtx.SetOutErrFile(os.Stderr)
76+
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle.WithStdOut(outFile), true)
8277
if err != nil {
8378
t.Fatalf("Test failed: %v", err)
8479
}
@@ -95,7 +90,6 @@ func TestSelectComputeDisksAggOrderBySizeAsc(t *testing.T) {
9590
t.Fatalf("Test failed: %v", prepareErr)
9691
}
9792
response := querySubmitter.SubmitQuery()
98-
handlerCtx.SetOutfile(outFile)
9993
responsehandler.HandleResponse(handlerCtx, response)
10094

10195
dr.ProcessQuery(handlerCtx.GetRawQuery())
@@ -117,9 +111,7 @@ func TestSelectComputeDisksAggOrderBySizeDesc(t *testing.T) {
117111
}
118112

119113
testSubject := func(t *testing.T, outFile *bufio.Writer) {
120-
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle)
121-
handlerCtx.SetOutfile(os.Stdout)
122-
handlerCtx.SetOutErrFile(os.Stderr)
114+
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle.WithStdOut(outFile), true)
123115
if err != nil {
124116
t.Fatalf("Test failed: %v", err)
125117
}
@@ -136,7 +128,6 @@ func TestSelectComputeDisksAggOrderBySizeDesc(t *testing.T) {
136128
t.Fatalf("Test failed: %v", prepareErr)
137129
}
138130
response := querySubmitter.SubmitQuery()
139-
handlerCtx.SetOutfile(outFile)
140131
responsehandler.HandleResponse(handlerCtx, response)
141132

142133
dr.ProcessQuery(handlerCtx.GetRawQuery())
@@ -158,9 +149,7 @@ func TestSelectComputeDisksAggTotalSize(t *testing.T) {
158149
}
159150

160151
testSubject := func(t *testing.T, outFile *bufio.Writer) {
161-
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle)
162-
handlerCtx.SetOutfile(os.Stdout)
163-
handlerCtx.SetOutErrFile(os.Stderr)
152+
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle.WithStdOut(outFile), true)
164153
if err != nil {
165154
t.Fatalf("Test failed: %v", err)
166155
}
@@ -177,7 +166,6 @@ func TestSelectComputeDisksAggTotalSize(t *testing.T) {
177166
t.Fatalf("Test failed: %v", prepareErr)
178167
}
179168
response := querySubmitter.SubmitQuery()
180-
handlerCtx.SetOutfile(outFile)
181169
responsehandler.HandleResponse(handlerCtx, response)
182170

183171
dr.ProcessQuery(handlerCtx.GetRawQuery())
@@ -199,9 +187,7 @@ func TestSelectComputeDisksAggTotalString(t *testing.T) {
199187
}
200188

201189
testSubject := func(t *testing.T, outFile *bufio.Writer) {
202-
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle)
203-
handlerCtx.SetOutfile(os.Stdout)
204-
handlerCtx.SetOutErrFile(os.Stderr)
190+
handlerCtx, err := entryutil.BuildHandlerContext(*runtimeCtx, strings.NewReader(""), lrucache.NewLRUCache(int64(runtimeCtx.QueryCacheSize)), inputBundle.WithStdOut(outFile), true)
205191
if err != nil {
206192
t.Fatalf("Test failed: %v", err)
207193
}
@@ -218,7 +204,6 @@ func TestSelectComputeDisksAggTotalString(t *testing.T) {
218204
t.Fatalf("Test failed: %v", prepareErr)
219205
}
220206
response := querySubmitter.SubmitQuery()
221-
handlerCtx.SetOutfile(outFile)
222207
responsehandler.HandleResponse(handlerCtx, response)
223208

224209
dr.ProcessQuery(handlerCtx.GetRawQuery())

0 commit comments

Comments
 (0)