From 08b2c8bcb3f2cb91831ce0927ce9fdf95da7df16 Mon Sep 17 00:00:00 2001 From: Dirkjan Bussink Date: Fri, 8 Dec 2023 17:04:04 +0100 Subject: [PATCH] build: Allow compilation on Windows (#14718) Signed-off-by: Dirkjan Bussink --- go.mod | 10 +-- go.sum | 20 ++--- go/cmd/vtctl/vtctl.go | 7 +- go/cmd/vtctl/vtctl_unix.go | 33 +++++++ go/cmd/vtctl/vtctl_windows.go | 27 ++++++ go/cmd/vttablet/cli/plugin_sysloglogger.go | 2 + go/event/syslogger/syslogger.go | 2 + go/event/syslogger/syslogger_test.go | 2 + go/mysql/conn.go | 39 --------- go/mysql/conn_unix.go | 65 ++++++++++++++ go/mysql/conn_windows.go | 24 +++++ go/streamlog/streamlog.go | 4 +- go/streamlog/streamlog_unix.go | 29 +++++++ go/streamlog/streamlog_windows.go | 29 +++++++ go/syscallutil/kill_unix.go | 27 ++++++ go/syscallutil/kill_windows.go | 28 ++++++ go/test/endtoend/cluster/cluster_process.go | 3 +- go/test/endtoend/cluster/mysqlctl_process.go | 5 +- go/test/endtoend/cluster/vttablet_process.go | 5 -- .../endtoend/cluster/vttablet_process_unix.go | 26 ++++++ .../cluster/vttablet_process_windows.go | 28 ++++++ go/test/endtoend/filelock/filelock.go | 20 +---- go/test/endtoend/filelock/filelock_unix.go | 6 +- go/test/endtoend/filelock/filelock_windows.go | 26 ++++++ go/vt/servenv/pprof.go | 41 --------- go/vt/servenv/pprof_unix.go | 66 ++++++++++++++ go/vt/servenv/pprof_windows.go | 27 ++++++ go/vt/servenv/servenv.go | 60 ------------- go/vt/servenv/servenv_unix.go | 87 +++++++++++++++++++ go/vt/servenv/servenv_windows.go | 21 +++++ go/vt/topo/events/keyspace_change_syslog.go | 2 + .../events/keyspace_change_syslog_test.go | 2 + go/vt/topo/events/shard_change_syslog.go | 2 + go/vt/topo/events/shard_change_syslog_test.go | 2 + go/vt/topo/events/tablet_change_syslog.go | 2 + .../topo/events/tablet_change_syslog_test.go | 2 + go/vt/topotools/events/reparent_syslog.go | 2 + .../topotools/events/reparent_syslog_test.go | 2 + go/vt/vtgate/plugin_mysql_server.go | 9 +- go/vt/vtgate/plugin_mysql_server_unix.go | 40 +++++++++ go/vt/vtgate/plugin_mysql_server_windows.go | 29 +++++++ go/vt/vtorc/inst/audit_dao.go | 21 +---- go/vt/vtorc/inst/audit_dao_nosyslog.go | 32 +++++++ go/vt/vtorc/inst/audit_dao_syslog.go | 43 +++++++++ go/vt/vttablet/onlineddl/executor.go | 5 +- go/vt/vttablet/sysloglogger/sysloglogger.go | 2 + .../sysloglogger/sysloglogger_test.go | 2 + go/vt/zkctl/zkctl.go | 5 +- 48 files changed, 748 insertions(+), 225 deletions(-) create mode 100644 go/cmd/vtctl/vtctl_unix.go create mode 100644 go/cmd/vtctl/vtctl_windows.go create mode 100644 go/mysql/conn_unix.go create mode 100644 go/mysql/conn_windows.go create mode 100644 go/streamlog/streamlog_unix.go create mode 100644 go/streamlog/streamlog_windows.go create mode 100644 go/syscallutil/kill_unix.go create mode 100644 go/syscallutil/kill_windows.go create mode 100644 go/test/endtoend/cluster/vttablet_process_unix.go create mode 100644 go/test/endtoend/cluster/vttablet_process_windows.go create mode 100644 go/test/endtoend/filelock/filelock_windows.go create mode 100644 go/vt/servenv/pprof_unix.go create mode 100644 go/vt/servenv/pprof_windows.go create mode 100644 go/vt/servenv/servenv_unix.go create mode 100644 go/vt/servenv/servenv_windows.go create mode 100644 go/vt/vtgate/plugin_mysql_server_unix.go create mode 100644 go/vt/vtgate/plugin_mysql_server_windows.go create mode 100644 go/vt/vtorc/inst/audit_dao_nosyslog.go create mode 100644 go/vt/vtorc/inst/audit_dao_syslog.go diff --git a/go.mod b/go.mod index 7a396338352..af1ae0a7488 100644 --- a/go.mod +++ b/go.mod @@ -71,12 +71,12 @@ require ( go.etcd.io/etcd/client/pkg/v3 v3.5.8 go.etcd.io/etcd/client/v3 v3.5.8 go.uber.org/mock v0.2.0 - golang.org/x/crypto v0.14.0 // indirect + golang.org/x/crypto v0.16.0 // indirect golang.org/x/mod v0.12.0 // indirect - golang.org/x/net v0.17.0 + golang.org/x/net v0.19.0 golang.org/x/oauth2 v0.11.0 - golang.org/x/sys v0.14.0 - golang.org/x/term v0.13.0 + golang.org/x/sys v0.15.0 + golang.org/x/term v0.15.0 golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.3.0 golang.org/x/tools v0.12.1-0.20230815132531-74c255bcf846 @@ -149,7 +149,7 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-ieproxy v0.0.10 // indirect + github.com/mattn/go-ieproxy v0.0.11 // indirect github.com/mattn/go-isatty v0.0.18 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect diff --git a/go.sum b/go.sum index 395f8439090..1df92514455 100644 --- a/go.sum +++ b/go.sum @@ -406,8 +406,8 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-ieproxy v0.0.1/go.mod h1:pYabZ6IHcRpFh7vIaLfK7rdcWgFEb3SFJ6/gNWuh88E= -github.com/mattn/go-ieproxy v0.0.10 h1:P+2QihaKCLgbs/32dhFLbxXlqsy8tIG1LUXHIoPaQPo= -github.com/mattn/go-ieproxy v0.0.10/go.mod h1:/NsJd+kxZBmjMc5hrJCKMbP57B84rvq9BiDRbtO9AS0= +github.com/mattn/go-ieproxy v0.0.11 h1:MQ/5BuGSgDAHZOJe6YY80IF2UVCfGkwfo6AeD7HtHYo= +github.com/mattn/go-ieproxy v0.0.11/go.mod h1:/NsJd+kxZBmjMc5hrJCKMbP57B84rvq9BiDRbtO9AS0= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= @@ -663,8 +663,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= -golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= +golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -756,8 +756,8 @@ golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -854,14 +854,14 @@ golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= -golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= -golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= +golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/go/cmd/vtctl/vtctl.go b/go/cmd/vtctl/vtctl.go index 175e49c6831..e95f484cf4f 100644 --- a/go/cmd/vtctl/vtctl.go +++ b/go/cmd/vtctl/vtctl.go @@ -19,7 +19,6 @@ package main import ( "context" "fmt" - "log/syslog" "os" "os/signal" "strings" @@ -118,11 +117,7 @@ func main() { startMsg := fmt.Sprintf("USER=%v SUDO_USER=%v %v", os.Getenv("USER"), os.Getenv("SUDO_USER"), strings.Join(os.Args, " ")) - if syslogger, err := syslog.New(syslog.LOG_INFO, "vtctl "); err == nil { - syslogger.Info(startMsg) // nolint:errcheck - } else { - log.Warningf("cannot connect to syslog: %v", err) - } + logSyslog(startMsg) closer := trace.StartTracing("vtctl") defer trace.LogErrorsWhenClosing(closer) diff --git a/go/cmd/vtctl/vtctl_unix.go b/go/cmd/vtctl/vtctl_unix.go new file mode 100644 index 00000000000..bee0be238d7 --- /dev/null +++ b/go/cmd/vtctl/vtctl_unix.go @@ -0,0 +1,33 @@ +//go:build !windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "log/syslog" + + "vitess.io/vitess/go/vt/log" +) + +func logSyslog(msg string) { + if syslogger, err := syslog.New(syslog.LOG_INFO, "vtctl "); err == nil { + syslogger.Info(msg) // nolint:errcheck + } else { + log.Warningf("cannot connect to syslog: %v", err) + } +} diff --git a/go/cmd/vtctl/vtctl_windows.go b/go/cmd/vtctl/vtctl_windows.go new file mode 100644 index 00000000000..63c5cceb63b --- /dev/null +++ b/go/cmd/vtctl/vtctl_windows.go @@ -0,0 +1,27 @@ +//go:build windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "vitess.io/vitess/go/vt/log" +) + +func logSyslog(msg string) { + log.Warningf("windows does not have syslog support") +} diff --git a/go/cmd/vttablet/cli/plugin_sysloglogger.go b/go/cmd/vttablet/cli/plugin_sysloglogger.go index 303bf014d83..90860abe826 100644 --- a/go/cmd/vttablet/cli/plugin_sysloglogger.go +++ b/go/cmd/vttablet/cli/plugin_sysloglogger.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/event/syslogger/syslogger.go b/go/event/syslogger/syslogger.go index 1c8ff22136b..eef3257e518 100644 --- a/go/event/syslogger/syslogger.go +++ b/go/event/syslogger/syslogger.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/event/syslogger/syslogger_test.go b/go/event/syslogger/syslogger_test.go index fddeef12b27..8217b945baa 100644 --- a/go/event/syslogger/syslogger_test.go +++ b/go/event/syslogger/syslogger_test.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/mysql/conn.go b/go/mysql/conn.go index 9cac35ea01f..1908875db49 100644 --- a/go/mysql/conn.go +++ b/go/mysql/conn.go @@ -28,7 +28,6 @@ import ( "strings" "sync" "sync/atomic" - "syscall" "time" "vitess.io/vitess/go/bucketpool" @@ -1699,41 +1698,3 @@ func (c *Conn) IsMarkedForClose() bool { func (c *Conn) IsShuttingDown() bool { return c.listener.shutdown.Load() } - -// ConnCheck ensures that this connection to the MySQL server hasn't been broken. -// This is a fast, non-blocking check. For details on its implementation, please read -// "Three Bugs in the Go MySQL Driver" (Vicent Marti, GitHub, 2020) -// https://github.blog/2020-05-20-three-bugs-in-the-go-mysql-driver/ -func (c *Conn) ConnCheck() error { - conn := c.conn - if tlsconn, ok := conn.(*tls.Conn); ok { - conn = tlsconn.NetConn() - } - if conn, ok := conn.(syscall.Conn); ok { - rc, err := conn.SyscallConn() - if err != nil { - return err - } - - var n int - var buff [1]byte - rerr := rc.Read(func(fd uintptr) bool { - n, err = syscall.Read(int(fd), buff[:]) - return true - }) - - switch { - case rerr != nil: - return rerr - case n == 0 && err == nil: - return io.EOF - case n > 0: - return sqlerror.NewSQLError(sqlerror.CRUnknownError, sqlerror.SSUnknownSQLState, "unexpected read from conn") - case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK: - return nil - default: - return err - } - } - return nil -} diff --git a/go/mysql/conn_unix.go b/go/mysql/conn_unix.go new file mode 100644 index 00000000000..54e883efcd9 --- /dev/null +++ b/go/mysql/conn_unix.go @@ -0,0 +1,65 @@ +//go:build !windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package mysql + +import ( + "crypto/tls" + "io" + "syscall" + + "vitess.io/vitess/go/mysql/sqlerror" +) + +// ConnCheck ensures that this connection to the MySQL server hasn't been broken. +// This is a fast, non-blocking check. For details on its implementation, please read +// "Three Bugs in the Go MySQL Driver" (Vicent Marti, GitHub, 2020) +// https://github.blog/2020-05-20-three-bugs-in-the-go-mysql-driver/ +func (c *Conn) ConnCheck() error { + conn := c.conn + if tlsconn, ok := conn.(*tls.Conn); ok { + conn = tlsconn.NetConn() + } + if conn, ok := conn.(syscall.Conn); ok { + rc, err := conn.SyscallConn() + if err != nil { + return err + } + + var n int + var buff [1]byte + rerr := rc.Read(func(fd uintptr) bool { + n, err = syscall.Read(int(fd), buff[:]) + return true + }) + + switch { + case rerr != nil: + return rerr + case n == 0 && err == nil: + return io.EOF + case n > 0: + return sqlerror.NewSQLError(sqlerror.CRUnknownError, sqlerror.SSUnknownSQLState, "unexpected read from conn") + case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK: + return nil + default: + return err + } + } + return nil +} diff --git a/go/mysql/conn_windows.go b/go/mysql/conn_windows.go new file mode 100644 index 00000000000..695c5703cdb --- /dev/null +++ b/go/mysql/conn_windows.go @@ -0,0 +1,24 @@ +//go:build windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package mysql + +// ConnCheck is not implemented for Windows. +func (c *Conn) ConnCheck() error { + return nil +} diff --git a/go/streamlog/streamlog.go b/go/streamlog/streamlog.go index 26248fcd1b1..25e60182c4a 100644 --- a/go/streamlog/streamlog.go +++ b/go/streamlog/streamlog.go @@ -23,10 +23,8 @@ import ( "net/http" "net/url" "os" - "os/signal" "strings" "sync" - "syscall" "github.com/spf13/pflag" @@ -215,7 +213,7 @@ func (logger *StreamLogger[T]) ServeLogs(url string, logf LogFormatter) { // it. func (logger *StreamLogger[T]) LogToFile(path string, logf LogFormatter) (chan T, error) { rotateChan := make(chan os.Signal, 1) - signal.Notify(rotateChan, syscall.SIGUSR2) + setupRotate(rotateChan) logChan := logger.Subscribe("FileLog") formatParams := map[string][]string{"full": {}} diff --git a/go/streamlog/streamlog_unix.go b/go/streamlog/streamlog_unix.go new file mode 100644 index 00000000000..0cfa4b9e6bc --- /dev/null +++ b/go/streamlog/streamlog_unix.go @@ -0,0 +1,29 @@ +//go:build !windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package streamlog + +import ( + "os" + "os/signal" + "syscall" +) + +func setupRotate(ch chan os.Signal) { + signal.Notify(ch, syscall.SIGUSR2) +} diff --git a/go/streamlog/streamlog_windows.go b/go/streamlog/streamlog_windows.go new file mode 100644 index 00000000000..ef69058b97c --- /dev/null +++ b/go/streamlog/streamlog_windows.go @@ -0,0 +1,29 @@ +//go:build windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package streamlog + +import ( + "os" + + "vitess.io/vitess/go/vt/log" +) + +func setupRotate(ch chan os.Signal) { + log.Warningf("signal based log rotation is not supported on Windows") +} diff --git a/go/syscallutil/kill_unix.go b/go/syscallutil/kill_unix.go new file mode 100644 index 00000000000..d0b1776ae3c --- /dev/null +++ b/go/syscallutil/kill_unix.go @@ -0,0 +1,27 @@ +//go:build !windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package syscallutil + +import ( + "syscall" +) + +func Kill(pid int, signum syscall.Signal) (err error) { + return syscall.Kill(pid, signum) +} diff --git a/go/syscallutil/kill_windows.go b/go/syscallutil/kill_windows.go new file mode 100644 index 00000000000..091fcdf759d --- /dev/null +++ b/go/syscallutil/kill_windows.go @@ -0,0 +1,28 @@ +//go:build windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package syscallutil + +import ( + "errors" + "syscall" +) + +func Kill(pid int, signum syscall.Signal) (err error) { + return errors.New("kill is not supported on windows") +} diff --git a/go/test/endtoend/cluster/cluster_process.go b/go/test/endtoend/cluster/cluster_process.go index 28a8807cf08..3a1390eec0c 100644 --- a/go/test/endtoend/cluster/cluster_process.go +++ b/go/test/endtoend/cluster/cluster_process.go @@ -41,6 +41,7 @@ import ( "vitess.io/vitess/go/json2" "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/sqltypes" + "vitess.io/vitess/go/syscallutil" "vitess.io/vitess/go/test/endtoend/filelock" "vitess.io/vitess/go/vt/grpcclient" "vitess.io/vitess/go/vt/log" @@ -1091,7 +1092,7 @@ func (cluster *LocalProcessCluster) waitForMySQLProcessToExit(mysqlctlProcessLis log.Errorf("Error in conversion to integer: %v", err) return } - err = syscall.Kill(pid, syscall.SIGKILL) + err = syscallutil.Kill(pid, syscall.SIGKILL) if err != nil { log.Errorf("Error in killing process: %v", err) } diff --git a/go/test/endtoend/cluster/mysqlctl_process.go b/go/test/endtoend/cluster/mysqlctl_process.go index 06808627254..79248b6d9a7 100644 --- a/go/test/endtoend/cluster/mysqlctl_process.go +++ b/go/test/endtoend/cluster/mysqlctl_process.go @@ -30,6 +30,7 @@ import ( "github.com/google/safehtml/template" "vitess.io/vitess/go/mysql" + "vitess.io/vitess/go/syscallutil" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/mysqlctl" "vitess.io/vitess/go/vt/tlstest" @@ -215,11 +216,11 @@ func (mysqlctl *MysqlctlProcess) Stop() (err error) { // If we found a valid associated mysqld_safe process then let's kill // it first. if err == nil && mysqldSafePID > 0 { - if err = syscall.Kill(mysqldSafePID, syscall.SIGKILL); err != nil { + if err = syscallutil.Kill(mysqldSafePID, syscall.SIGKILL); err != nil { return err } } - return syscall.Kill(pid, syscall.SIGKILL) + return syscallutil.Kill(pid, syscall.SIGKILL) } // StopProcess executes mysqlctl command to stop mysql instance and returns process reference diff --git a/go/test/endtoend/cluster/vttablet_process.go b/go/test/endtoend/cluster/vttablet_process.go index f51e1838a5b..96bec7c624b 100644 --- a/go/test/endtoend/cluster/vttablet_process.go +++ b/go/test/endtoend/cluster/vttablet_process.go @@ -556,11 +556,6 @@ func (vttablet *VttabletProcess) getDBSystemValues(placeholder string, value str return "", nil } -// ToggleProfiling enables or disables the configured CPU profiler on this vttablet -func (vttablet *VttabletProcess) ToggleProfiling() error { - return vttablet.proc.Process.Signal(syscall.SIGUSR1) -} - // WaitForVReplicationToCatchup waits for "workflow" to finish copying func (vttablet *VttabletProcess) WaitForVReplicationToCatchup(t testing.TB, workflow, database string, sidecarDBName string, duration time.Duration) { if sidecarDBName == "" { diff --git a/go/test/endtoend/cluster/vttablet_process_unix.go b/go/test/endtoend/cluster/vttablet_process_unix.go new file mode 100644 index 00000000000..3f5c76e9988 --- /dev/null +++ b/go/test/endtoend/cluster/vttablet_process_unix.go @@ -0,0 +1,26 @@ +//go:build !windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cluster + +import "syscall" + +// ToggleProfiling enables or disables the configured CPU profiler on this vttablet +func (vttablet *VttabletProcess) ToggleProfiling() error { + return vttablet.proc.Process.Signal(syscall.SIGUSR1) +} diff --git a/go/test/endtoend/cluster/vttablet_process_windows.go b/go/test/endtoend/cluster/vttablet_process_windows.go new file mode 100644 index 00000000000..6c233746e8a --- /dev/null +++ b/go/test/endtoend/cluster/vttablet_process_windows.go @@ -0,0 +1,28 @@ +//go:build windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cluster + +import ( + "errors" +) + +// ToggleProfiling enables or disables the configured CPU profiler on this vttablet. +func (vttablet *VttabletProcess) ToggleProfiling() error { + return errors.New("not implemented") +} diff --git a/go/test/endtoend/filelock/filelock.go b/go/test/endtoend/filelock/filelock.go index 05f27c321a8..d37331892d1 100644 --- a/go/test/endtoend/filelock/filelock.go +++ b/go/test/endtoend/filelock/filelock.go @@ -10,7 +10,6 @@ package filelock import ( "errors" "io/fs" - "os" ) // A File provides the minimal set of methods required to lock an open file. @@ -78,22 +77,7 @@ func (lt lockType) String() string { // IsNotSupported returns a boolean indicating whether the error is known to // report that a function is not supported (possibly for a specific input). -// It is satisfied by ErrNotSupported as well as some syscall errors. +// It is satisfied by errors.ErrUnsupported as well as some syscall errors. func IsNotSupported(err error) bool { - return isNotSupported(underlyingError(err)) -} - -var ErrNotSupported = errors.New("operation not supported") - -// underlyingError returns the underlying error for known os error types. -func underlyingError(err error) error { - switch err := err.(type) { - case *fs.PathError: - return err.Err - case *os.LinkError: - return err.Err - case *os.SyscallError: - return err.Err - } - return err + return errors.Is(err, errors.ErrUnsupported) } diff --git a/go/test/endtoend/filelock/filelock_unix.go b/go/test/endtoend/filelock/filelock_unix.go index 23064dae0be..6f73b1bfeea 100644 --- a/go/test/endtoend/filelock/filelock_unix.go +++ b/go/test/endtoend/filelock/filelock_unix.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build darwin || dragonfly || freebsd || illumos || linux || netbsd || openbsd + package filelock import ( @@ -36,7 +38,3 @@ func lock(f File, lt lockType) (err error) { func unlock(f File) error { return lock(f, syscall.LOCK_UN) } - -func isNotSupported(err error) bool { - return err == syscall.ENOSYS || err == syscall.ENOTSUP || err == syscall.EOPNOTSUPP || err == ErrNotSupported -} diff --git a/go/test/endtoend/filelock/filelock_windows.go b/go/test/endtoend/filelock/filelock_windows.go new file mode 100644 index 00000000000..34df039b96b --- /dev/null +++ b/go/test/endtoend/filelock/filelock_windows.go @@ -0,0 +1,26 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build windows + +package filelock + +import ( + "errors" +) + +type lockType uint32 + +const ( + readLock lockType = 0 + writeLock lockType = 1 +) + +func lock(f File, lt lockType) error { + return errors.New("filelock: not implemented on windows") +} + +func unlock(f File) error { + return errors.New("filelock: not implemented on windows") +} diff --git a/go/vt/servenv/pprof.go b/go/vt/servenv/pprof.go index d1d8e99588f..7aff18ca05a 100644 --- a/go/vt/servenv/pprof.go +++ b/go/vt/servenv/pprof.go @@ -20,7 +20,6 @@ import ( "fmt" "io" "os" - "os/signal" "path/filepath" "runtime" "runtime/pprof" @@ -28,7 +27,6 @@ import ( "strconv" "strings" "sync/atomic" - "syscall" "github.com/spf13/pflag" @@ -298,45 +296,6 @@ func (prof *profile) init() (start func(), stop func()) { } } -func pprofInit() { - prof, err := parseProfileFlag(pprofFlag) - if err != nil { - log.Fatal(err) - } - if prof != nil { - start, stop := prof.init() - startSignal := make(chan os.Signal, 1) - stopSignal := make(chan os.Signal, 1) - - if prof.waitSig { - signal.Notify(startSignal, syscall.SIGUSR1) - } else { - start() - signal.Notify(stopSignal, syscall.SIGUSR1) - } - - go func() { - for { - <-startSignal - start() - signal.Reset(syscall.SIGUSR1) - signal.Notify(stopSignal, syscall.SIGUSR1) - } - }() - - go func() { - for { - <-stopSignal - stop() - signal.Reset(syscall.SIGUSR1) - signal.Notify(startSignal, syscall.SIGUSR1) - } - }() - - OnTerm(stop) - } -} - func init() { OnParse(func(fs *pflag.FlagSet) { fs.StringSliceVar(&pprofFlag, "pprof", pprofFlag, "enable profiling") diff --git a/go/vt/servenv/pprof_unix.go b/go/vt/servenv/pprof_unix.go new file mode 100644 index 00000000000..097abc08720 --- /dev/null +++ b/go/vt/servenv/pprof_unix.go @@ -0,0 +1,66 @@ +//go:build !windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package servenv + +import ( + "os" + "os/signal" + "syscall" + + "vitess.io/vitess/go/vt/log" +) + +func pprofInit() { + prof, err := parseProfileFlag(pprofFlag) + if err != nil { + log.Fatal(err) + } + if prof != nil { + start, stop := prof.init() + startSignal := make(chan os.Signal, 1) + stopSignal := make(chan os.Signal, 1) + + if prof.waitSig { + signal.Notify(startSignal, syscall.SIGUSR1) + } else { + start() + signal.Notify(stopSignal, syscall.SIGUSR1) + } + + go func() { + for { + <-startSignal + start() + signal.Reset(syscall.SIGUSR1) + signal.Notify(stopSignal, syscall.SIGUSR1) + } + }() + + go func() { + for { + <-stopSignal + stop() + signal.Reset(syscall.SIGUSR1) + signal.Notify(startSignal, syscall.SIGUSR1) + } + }() + + OnTerm(stop) + } +} diff --git a/go/vt/servenv/pprof_windows.go b/go/vt/servenv/pprof_windows.go new file mode 100644 index 00000000000..7ec4be816df --- /dev/null +++ b/go/vt/servenv/pprof_windows.go @@ -0,0 +1,27 @@ +//go:build windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package servenv + +import ( + "vitess.io/vitess/go/vt/log" +) + +func pprofInit() { + log.Warningf("pprof is not supported on Windows") +} diff --git a/go/vt/servenv/servenv.go b/go/vt/servenv/servenv.go index e7c28855997..6a7898501f8 100644 --- a/go/vt/servenv/servenv.go +++ b/go/vt/servenv/servenv.go @@ -33,11 +33,8 @@ import ( "fmt" "net/url" "os" - "os/signal" - "runtime/debug" "strings" "sync" - "syscall" "time" "github.com/spf13/cobra" @@ -111,63 +108,6 @@ func GetInitStartTime() time.Time { return initStartTime } -// Init is the first phase of the server startup. -func Init() { - mu.Lock() - defer mu.Unlock() - initStartTime = time.Now() - - // Uptime metric - _ = stats.NewGaugeFunc("Uptime", "Uptime in nanoseconds", func() int64 { - return int64(time.Since(serverStart).Nanoseconds()) - }) - - // Ignore SIGPIPE if specified - // The Go runtime catches SIGPIPE for us on all fds except stdout/stderr - // See https://golang.org/pkg/os/signal/#hdr-SIGPIPE - if catchSigpipe { - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, syscall.SIGPIPE) - go func() { - <-sigChan - log.Warning("Caught SIGPIPE (ignoring all future SIGPIPEs)") - signal.Ignore(syscall.SIGPIPE) - }() - } - - // Add version tag to every info log - log.Infof(AppVersion.String()) - if inited { - log.Fatal("servenv.Init called second time") - } - inited = true - - // Once you run as root, you pretty much destroy the chances of a - // non-privileged user starting the program correctly. - if uid := os.Getuid(); uid == 0 { - log.Exitf("servenv.Init: running this as root makes no sense") - } - - // We used to set this limit directly, but you pretty much have to - // use a root account to allow increasing a limit reliably. Dropping - // privileges is also tricky. The best strategy is to make a shell - // script set up the limits as root and switch users before starting - // the server. - fdLimit := &syscall.Rlimit{} - if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, fdLimit); err != nil { - log.Errorf("max-open-fds failed: %v", err) - } - fdl := stats.NewGauge("MaxFds", "File descriptor limit") - fdl.Set(int64(fdLimit.Cur)) - - // Limit the stack size. We don't need huge stacks and smaller limits mean - // any infinite recursion fires earlier and on low memory systems avoids - // out of memory issues in favor of a stack overflow error. - debug.SetMaxStack(maxStackSize) - - onInitHooks.Fire() -} - func populateListeningURL(port int32) { host, err := netutil.FullyQualifiedHostname() if err != nil { diff --git a/go/vt/servenv/servenv_unix.go b/go/vt/servenv/servenv_unix.go new file mode 100644 index 00000000000..17fa85c4167 --- /dev/null +++ b/go/vt/servenv/servenv_unix.go @@ -0,0 +1,87 @@ +//go:build !windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package servenv + +import ( + "os" + "os/signal" + "runtime/debug" + "syscall" + "time" + + "vitess.io/vitess/go/stats" + "vitess.io/vitess/go/vt/log" +) + +// Init is the first phase of the server startup. +func Init() { + mu.Lock() + defer mu.Unlock() + initStartTime = time.Now() + + // Uptime metric + _ = stats.NewGaugeFunc("Uptime", "Uptime in nanoseconds", func() int64 { + return int64(time.Since(serverStart).Nanoseconds()) + }) + + // Ignore SIGPIPE if specified + // The Go runtime catches SIGPIPE for us on all fds except stdout/stderr + // See https://golang.org/pkg/os/signal/#hdr-SIGPIPE + if catchSigpipe { + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGPIPE) + go func() { + <-sigChan + log.Warning("Caught SIGPIPE (ignoring all future SIGPIPEs)") + signal.Ignore(syscall.SIGPIPE) + }() + } + + // Add version tag to every info log + log.Infof(AppVersion.String()) + if inited { + log.Fatal("servenv.Init called second time") + } + inited = true + + // Once you run as root, you pretty much destroy the chances of a + // non-privileged user starting the program correctly. + if uid := os.Getuid(); uid == 0 { + log.Exitf("servenv.Init: running this as root makes no sense") + } + + // We used to set this limit directly, but you pretty much have to + // use a root account to allow increasing a limit reliably. Dropping + // privileges is also tricky. The best strategy is to make a shell + // script set up the limits as root and switch users before starting + // the server. + fdLimit := &syscall.Rlimit{} + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, fdLimit); err != nil { + log.Errorf("max-open-fds failed: %v", err) + } + fdl := stats.NewGauge("MaxFds", "File descriptor limit") + fdl.Set(int64(fdLimit.Cur)) + + // Limit the stack size. We don't need huge stacks and smaller limits mean + // any infinite recursion fires earlier and on low memory systems avoids + // out of memory issues in favor of a stack overflow error. + debug.SetMaxStack(maxStackSize) + + onInitHooks.Fire() +} diff --git a/go/vt/servenv/servenv_windows.go b/go/vt/servenv/servenv_windows.go new file mode 100644 index 00000000000..bd610b1f245 --- /dev/null +++ b/go/vt/servenv/servenv_windows.go @@ -0,0 +1,21 @@ +//go:build windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package servenv + +func Init() {} diff --git a/go/vt/topo/events/keyspace_change_syslog.go b/go/vt/topo/events/keyspace_change_syslog.go index d7f456ae6b8..7404c3ca882 100644 --- a/go/vt/topo/events/keyspace_change_syslog.go +++ b/go/vt/topo/events/keyspace_change_syslog.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/topo/events/keyspace_change_syslog_test.go b/go/vt/topo/events/keyspace_change_syslog_test.go index 1367cf27b23..8ba7225a025 100644 --- a/go/vt/topo/events/keyspace_change_syslog_test.go +++ b/go/vt/topo/events/keyspace_change_syslog_test.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/topo/events/shard_change_syslog.go b/go/vt/topo/events/shard_change_syslog.go index 3f6422a9175..2055e4268ec 100644 --- a/go/vt/topo/events/shard_change_syslog.go +++ b/go/vt/topo/events/shard_change_syslog.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/topo/events/shard_change_syslog_test.go b/go/vt/topo/events/shard_change_syslog_test.go index fc721bae923..bdac457853e 100644 --- a/go/vt/topo/events/shard_change_syslog_test.go +++ b/go/vt/topo/events/shard_change_syslog_test.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/topo/events/tablet_change_syslog.go b/go/vt/topo/events/tablet_change_syslog.go index e2dae020c8e..55de46674dc 100644 --- a/go/vt/topo/events/tablet_change_syslog.go +++ b/go/vt/topo/events/tablet_change_syslog.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/topo/events/tablet_change_syslog_test.go b/go/vt/topo/events/tablet_change_syslog_test.go index 4a5bb4d7ea9..7ecabf3f7fb 100644 --- a/go/vt/topo/events/tablet_change_syslog_test.go +++ b/go/vt/topo/events/tablet_change_syslog_test.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/topotools/events/reparent_syslog.go b/go/vt/topotools/events/reparent_syslog.go index dae22467d1f..dd995f34d73 100644 --- a/go/vt/topotools/events/reparent_syslog.go +++ b/go/vt/topotools/events/reparent_syslog.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/topotools/events/reparent_syslog_test.go b/go/vt/topotools/events/reparent_syslog_test.go index 93a9b860fe2..f4ba39f602b 100644 --- a/go/vt/topotools/events/reparent_syslog_test.go +++ b/go/vt/topotools/events/reparent_syslog_test.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/vtgate/plugin_mysql_server.go b/go/vt/vtgate/plugin_mysql_server.go index 273592b5bf7..00eb5e1b605 100644 --- a/go/vt/vtgate/plugin_mysql_server.go +++ b/go/vt/vtgate/plugin_mysql_server.go @@ -550,17 +550,10 @@ func initMySQLProtocol(vtgate *VTGate) *mysqlServer { } if mysqlServerSocketPath != "" { - // Let's create this unix socket with permissions to all users. In this way, - // clients can connect to vtgate mysql server without being vtgate user - oldMask := syscall.Umask(000) - srv.unixListener, err = newMysqlUnixSocket(mysqlServerSocketPath, authServer, srv.vtgateHandle) - _ = syscall.Umask(oldMask) + err = setupUnixSocket(srv, authServer, mysqlServerSocketPath) if err != nil { log.Exitf("mysql.NewListener failed: %v", err) - return nil } - // Listen for unix socket - go srv.unixListener.Accept() } return srv } diff --git a/go/vt/vtgate/plugin_mysql_server_unix.go b/go/vt/vtgate/plugin_mysql_server_unix.go new file mode 100644 index 00000000000..95c9731fccc --- /dev/null +++ b/go/vt/vtgate/plugin_mysql_server_unix.go @@ -0,0 +1,40 @@ +//go:build !windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package vtgate + +import ( + "syscall" + + "vitess.io/vitess/go/mysql" +) + +func setupUnixSocket(srv *mysqlServer, authServer mysql.AuthServer, path string) error { + // Let's create this unix socket with permissions to all users. In this way, + // clients can connect to vtgate mysql server without being vtgate user + var err error + oldMask := syscall.Umask(000) + srv.unixListener, err = newMysqlUnixSocket(path, authServer, srv.vtgateHandle) + _ = syscall.Umask(oldMask) + if err != nil { + return err + } + // Listen for unix socket + go srv.unixListener.Accept() + return nil +} diff --git a/go/vt/vtgate/plugin_mysql_server_windows.go b/go/vt/vtgate/plugin_mysql_server_windows.go new file mode 100644 index 00000000000..0502cadf863 --- /dev/null +++ b/go/vt/vtgate/plugin_mysql_server_windows.go @@ -0,0 +1,29 @@ +//go:build windows + +/* +Copyright 2023 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package vtgate + +import ( + "errors" + + "vitess.io/vitess/go/mysql" +) + +func setupUnixSocket(srv *mysqlServer, authServer mysql.AuthServer, path string) error { + return errors.New("unix sockets are not supported on windows") +} diff --git a/go/vt/vtorc/inst/audit_dao.go b/go/vt/vtorc/inst/audit_dao.go index 96db7f32ccf..fc1528c9640 100644 --- a/go/vt/vtorc/inst/audit_dao.go +++ b/go/vt/vtorc/inst/audit_dao.go @@ -18,36 +18,22 @@ package inst import ( "fmt" - "log/syslog" "os" "time" - "vitess.io/vitess/go/vt/log" - "github.com/rcrowley/go-metrics" + "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/vtorc/config" "vitess.io/vitess/go/vt/vtorc/db" ) -// syslogWriter is optional, and defaults to nil (disabled) -var syslogWriter *syslog.Writer - var auditOperationCounter = metrics.NewCounter() func init() { _ = metrics.Register("audit.write", auditOperationCounter) } -// EnableSyslogWriter enables, if possible, writes to syslog. These will execute _in addition_ to normal logging -func EnableAuditSyslog() (err error) { - syslogWriter, err = syslog.New(syslog.LOG_ERR, "vtorc") - if err != nil { - syslogWriter = nil - } - return err -} - // AuditOperation creates and writes a new audit entry by given params func AuditOperation(auditType string, tabletAlias string, message string) error { keyspace := "" @@ -94,11 +80,8 @@ func AuditOperation(auditType string, tabletAlias string, message string) error } } logMessage := fmt.Sprintf("auditType:%s alias:%s keyspace:%s shard:%s message:%s", auditType, tabletAlias, keyspace, shard, message) - if syslogWriter != nil { + if syslogMessage(logMessage) { auditWrittenToFile = true - go func() { - _ = syslogWriter.Info(logMessage) - }() } if !auditWrittenToFile { log.Infof(logMessage) diff --git a/go/vt/vtorc/inst/audit_dao_nosyslog.go b/go/vt/vtorc/inst/audit_dao_nosyslog.go new file mode 100644 index 00000000000..a61b3eb8f42 --- /dev/null +++ b/go/vt/vtorc/inst/audit_dao_nosyslog.go @@ -0,0 +1,32 @@ +//go:build windows + +/* + Copyright 2014 Outbrain Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package inst + +import ( + "errors" +) + +// EnableAuditSyslog enables, if possible, writes to syslog. These will execute _in addition_ to normal logging +func EnableAuditSyslog() (err error) { + return errors.New("syslog is not supported on windows") +} + +func syslogMessage(logMessage string) bool { + return false +} diff --git a/go/vt/vtorc/inst/audit_dao_syslog.go b/go/vt/vtorc/inst/audit_dao_syslog.go new file mode 100644 index 00000000000..2567409f03e --- /dev/null +++ b/go/vt/vtorc/inst/audit_dao_syslog.go @@ -0,0 +1,43 @@ +//go:build !windows + +/* + Copyright 2014 Outbrain Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package inst + +import "log/syslog" + +// syslogWriter is optional, and defaults to nil (disabled) +var syslogWriter *syslog.Writer + +// EnableAuditSyslog enables, if possible, writes to syslog. These will execute _in addition_ to normal logging +func EnableAuditSyslog() (err error) { + syslogWriter, err = syslog.New(syslog.LOG_ERR, "vtorc") + if err != nil { + syslogWriter = nil + } + return err +} + +func syslogMessage(logMessage string) bool { + if syslogWriter == nil { + return false + } + go func() { + _ = syslogWriter.Info(logMessage) + }() + return true +} diff --git a/go/vt/vttablet/onlineddl/executor.go b/go/vt/vttablet/onlineddl/executor.go index 3b2dd30c9f6..cdd890ef5d7 100644 --- a/go/vt/vttablet/onlineddl/executor.go +++ b/go/vt/vttablet/onlineddl/executor.go @@ -43,6 +43,7 @@ import ( "vitess.io/vitess/go/mysql/sqlerror" "vitess.io/vitess/go/sqlescape" "vitess.io/vitess/go/sqltypes" + "vitess.io/vitess/go/syscallutil" "vitess.io/vitess/go/textutil" "vitess.io/vitess/go/timer" "vitess.io/vitess/go/vt/binlog/binlogplayer" @@ -2044,10 +2045,10 @@ func (e *Executor) terminateMigration(ctx context.Context, onlineDDL *schema.Onl foundRunning = true // Because pt-osc doesn't offer much control, we take a brute force approach to killing it, // revoking its privileges, and cleaning up its triggers. - if err := syscall.Kill(pid, syscall.SIGTERM); err != nil { + if err := syscallutil.Kill(pid, syscall.SIGTERM); err != nil { return foundRunning, nil } - if err := syscall.Kill(pid, syscall.SIGKILL); err != nil { + if err := syscallutil.Kill(pid, syscall.SIGKILL); err != nil { return foundRunning, nil } if err := e.dropOnlineDDLUser(ctx); err != nil { diff --git a/go/vt/vttablet/sysloglogger/sysloglogger.go b/go/vt/vttablet/sysloglogger/sysloglogger.go index 87601884d5c..37672911e23 100644 --- a/go/vt/vttablet/sysloglogger/sysloglogger.go +++ b/go/vt/vttablet/sysloglogger/sysloglogger.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/vttablet/sysloglogger/sysloglogger_test.go b/go/vt/vttablet/sysloglogger/sysloglogger_test.go index c62a4396ac6..7a1678638ca 100644 --- a/go/vt/vttablet/sysloglogger/sysloglogger_test.go +++ b/go/vt/vttablet/sysloglogger/sysloglogger_test.go @@ -1,3 +1,5 @@ +//go:build !windows + /* Copyright 2019 The Vitess Authors. diff --git a/go/vt/zkctl/zkctl.go b/go/vt/zkctl/zkctl.go index acb8dba6356..60102d1bbf9 100644 --- a/go/vt/zkctl/zkctl.go +++ b/go/vt/zkctl/zkctl.go @@ -33,6 +33,7 @@ import ( zookeeper "github.com/z-division/go-zookeeper/zk" + "vitess.io/vitess/go/syscallutil" "vitess.io/vitess/go/vt/env" "vitess.io/vitess/go/vt/log" ) @@ -137,13 +138,13 @@ func (zkd *Zkd) Shutdown() error { if err != nil { return err } - err = syscall.Kill(pid, syscall.SIGKILL) + err = syscallutil.Kill(pid, syscall.SIGKILL) if err != nil && err != syscall.ESRCH { return err } timeout := time.Now().Add(shutdownWaitTime) for time.Now().Before(timeout) { - if syscall.Kill(pid, syscall.SIGKILL) == syscall.ESRCH { + if syscallutil.Kill(pid, syscall.SIGKILL) == syscall.ESRCH { return nil } time.Sleep(time.Second)