From 0db54dae0294444a223d2d1053a3af9258dee807 Mon Sep 17 00:00:00 2001 From: hzliangbin Date: Sun, 22 May 2022 22:04:41 +0800 Subject: [PATCH] feat: add readiness and liveness probe (#52) Signed-off-by: hzliangbin --- cmd/cmd.go | 17 ++++++---- cmd/healthcheck.go | 55 ++++++++++++++++++++++++++++++++ cmd/metrics.go | 29 +++++++++++++++++ cmd/routes.go | 34 ++++++++++++++++++++ go.mod | 4 +-- go.sum | 24 +------------- makefile | 1 + pkg/dt/api/api.pb.go | 3 +- pkg/dt/api/api_grpc.pb.go | 1 + pkg/dt/undolog/undo_log.pb.go | 3 +- pkg/resource/data_source.go | 12 +++++++ pkg/sql/db.go | 11 ++++++- testdata/mock_db_manager.go | 3 +- third_party/parser/ast/misc.go | 3 +- third_party/parser/hintparser.go | 3 +- third_party/parser/parser.go | 3 +- third_party/types/time.go | 5 +-- 17 files changed, 167 insertions(+), 44 deletions(-) create mode 100644 cmd/healthcheck.go create mode 100644 cmd/metrics.go create mode 100644 cmd/routes.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 86b6592a..4f307f32 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -28,7 +28,6 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/pkg/errors" - "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/spf13/cobra" "github.com/cectc/dbpack/pkg/config" @@ -179,7 +178,7 @@ var ( if lisErr != nil { log.Fatalf("unable init metrics server: %+v", lisErr) } - go initMetrics(ctx, lis) + go initServer(ctx, lis) dbpack.Start(ctx) }, @@ -192,21 +191,25 @@ func init() { rootCommand.AddCommand(startCommand) } -func initMetrics(ctx context.Context, lis net.Listener) { +func initServer(ctx context.Context, lis net.Listener) { go func() { <-ctx.Done() lis.Close() }() - mux := http.NewServeMux() - mux.Handle("/metrics", promhttp.Handler()) + handler, err := configServerHandler() + if err != nil { + log.Fatalf("failed to init handler: %+v", err) + return + } httpS := &http.Server{ - Handler: mux, + Handler: handler, } - err := httpS.Serve(lis) + err = httpS.Serve(lis) if err != nil { log.Fatalf("unable create status server: %+v", err) return } + log.Infof("start api server : %s", lis.Addr()) } //func initHolmes() *holmes.Holmes { diff --git a/cmd/healthcheck.go b/cmd/healthcheck.go new file mode 100644 index 00000000..b6075153 --- /dev/null +++ b/cmd/healthcheck.go @@ -0,0 +1,55 @@ +/* + * Copyright 2022 CECTC, 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 main + +import ( + "net/http" + + "github.com/gorilla/mux" + + "github.com/cectc/dbpack/pkg/resource" +) + +const ( + healthCheckLivenessPath = "/live" + healthCheckReadinessPath = "/ready" +) + +func registerHealthCheckRouter(router *mux.Router) { + router.Methods(http.MethodGet).Path(healthCheckReadinessPath).HandlerFunc(readinessHandler) + + router.Methods(http.MethodGet).Path(healthCheckLivenessPath).HandlerFunc(livenessHandler) + +} + +func readinessHandler(w http.ResponseWriter, r *http.Request) { + dbm := resource.GetDBManager() + if dbm == nil { + w.WriteHeader(http.StatusServiceUnavailable) + return + } + err := dbm.(*resource.DBManager).GetResoucePoolStatus() + if err != nil { + w.WriteHeader(http.StatusServiceUnavailable) + return + } + w.WriteHeader(http.StatusOK) +} + +func livenessHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) +} diff --git a/cmd/metrics.go b/cmd/metrics.go new file mode 100644 index 00000000..aafbc31d --- /dev/null +++ b/cmd/metrics.go @@ -0,0 +1,29 @@ +/* + * Copyright 2022 CECTC, 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 main + +import ( + "github.com/gorilla/mux" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +// registerMetricsRouter - add handler functions for metrics. +func registerMetricsRouter(router *mux.Router) { + // metrics router + router.Path("/metrics").Handler(promhttp.Handler()) + +} diff --git a/cmd/routes.go b/cmd/routes.go new file mode 100644 index 00000000..925a56d1 --- /dev/null +++ b/cmd/routes.go @@ -0,0 +1,34 @@ +/* + * Copyright 2022 CECTC, 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 main + +import ( + "net/http" + + "github.com/gorilla/mux" +) + +func configServerHandler() (http.Handler, error) { + router := mux.NewRouter().SkipClean(true).UseEncodedPath() + // Add healthcheck router + registerHealthCheckRouter(router) + + // Add server metrics router + registerMetricsRouter(router) + + return router, nil +} diff --git a/go.mod b/go.mod index 7ce6d2f3..aa8074c6 100644 --- a/go.mod +++ b/go.mod @@ -47,11 +47,9 @@ require ( github.com/BurntSushi/toml v1.1.0 // indirect github.com/benbjohnson/clock v1.3.0 // indirect github.com/go-logr/logr v1.2.3 // indirect + github.com/gorilla/mux v1.8.0 github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 // indirect github.com/jonboulle/clockwork v0.2.2 // indirect - github.com/mattn/go-colorable v0.1.12 // indirect - github.com/mattn/go-runewidth v0.0.13 // indirect - github.com/mgechev/revive v1.2.1 // indirect github.com/soheilhy/cmux v0.1.5-0.20210205191134-5ec6847320e5 // indirect github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect golang.org/x/sys v0.0.0-20220519141025-dcacdad47464 // indirect diff --git a/go.sum b/go.sum index 1ddcb37b..18a2a387 100644 --- a/go.sum +++ b/go.sum @@ -119,8 +119,6 @@ github.com/cespare/xxhash/v2 v2.1.0/go.mod h1:dgIUBU3pDso/gPgZ1osOZ0iQf77oPR28Tj github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/chavacava/garif v0.0.0-20220316182200-5cad0b5181d4 h1:tFXjAxje9thrTF4h57Ckik+scJjTWdwAtZqZPtOT48M= -github.com/chavacava/garif v0.0.0-20220316182200-5cad0b5181d4/go.mod h1:W8EnPSQ8Nv4fUjc/v1/8tHFqhuOJXnRub0dTfuAQktU= github.com/cheggaaa/pb/v3 v3.0.8/go.mod h1:UICbiLec/XO6Hw6k+BHEtHeQFzzBH4i2/qk/ow1EJTA= github.com/cheynewallace/tabby v1.1.1/go.mod h1:Pba/6cUL8uYqvOc9RkyvFbHGrQ9wShyrn6/S/1OYVys= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= @@ -227,10 +225,7 @@ github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= -github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= -github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= -github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4= github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94= github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA= @@ -423,6 +418,7 @@ github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/ github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= @@ -564,9 +560,6 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= -github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= @@ -574,14 +567,10 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= -github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= -github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= -github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o= github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= @@ -591,17 +580,12 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5 github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed/go.mod h1:dSsfyI2zABAdhcbvkXqgxOxrCsbYeHCPgrZkku60dSg= github.com/mediocregopher/radix/v3 v3.3.0/go.mod h1:EmfVyvspXz1uZEyPBMyGK+kjWiKQGvsUt6O3Pj+LDCQ= github.com/mgechev/dots v0.0.0-20190921121421-c36f7dcfbb81/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= -github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0= -github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= github.com/mgechev/revive v1.0.2/go.mod h1:rb0dQy1LVAxW9SWy5R3LPUjevzUbUS316U5MFySA2lo= -github.com/mgechev/revive v1.2.1 h1:GjFml7ZsoR0IrQ2E2YIvWFNS5GPDV7xNwvA5GM1HZC4= -github.com/mgechev/revive v1.2.1/go.mod h1:+Ro3wqY4vakcYNtkBWdZC7dBg1xSB6sp054wWwmeFm0= github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/minio/sio v0.3.0/go.mod h1:8b0yPp2avGThviy/+OCJBI6OMpvxoUuiLvE6F1lebhw= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= @@ -639,8 +623,6 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn github.com/oleiade/reflections v1.0.1/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60= github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA= -github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= -github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -766,7 +748,6 @@ github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qq github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= @@ -1190,13 +1171,10 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60= -golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220519141025-dcacdad47464 h1:MpIuURY70f0iKp/oooEFtB2oENcHITo/z1b6u41pKCw= golang.org/x/sys v0.0.0-20220519141025-dcacdad47464/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/makefile b/makefile index fab831b6..207e4f22 100644 --- a/makefile +++ b/makefile @@ -19,6 +19,7 @@ default: fmt errcheck ######################################################## fmt: ## Format the files @gofmt -l -w $(GO_FILES) + @goimports -w --local $(PKG) . ######################################################## fmtcheck: ## Check and format the files diff --git a/pkg/dt/api/api.pb.go b/pkg/dt/api/api.pb.go index c819cf6b..54b94865 100644 --- a/pkg/dt/api/api.pb.go +++ b/pkg/dt/api/api.pb.go @@ -6,13 +6,14 @@ package api import ( bytes "bytes" fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" reflect "reflect" strconv "strconv" strings "strings" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/dt/api/api_grpc.pb.go b/pkg/dt/api/api_grpc.pb.go index c0c8ef99..16db8258 100644 --- a/pkg/dt/api/api_grpc.pb.go +++ b/pkg/dt/api/api_grpc.pb.go @@ -4,6 +4,7 @@ package api import ( context "context" + grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" diff --git a/pkg/dt/undolog/undo_log.pb.go b/pkg/dt/undolog/undo_log.pb.go index a121003e..9ac85514 100644 --- a/pkg/dt/undolog/undo_log.pb.go +++ b/pkg/dt/undolog/undo_log.pb.go @@ -5,8 +5,9 @@ package undolog import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" math "math" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/resource/data_source.go b/pkg/resource/data_source.go index c269550e..0efddb72 100644 --- a/pkg/resource/data_source.go +++ b/pkg/resource/data_source.go @@ -17,6 +17,8 @@ package resource import ( + "fmt" + "github.com/cectc/dbpack/pkg/config" "github.com/cectc/dbpack/pkg/filter" "github.com/cectc/dbpack/pkg/proto" @@ -84,3 +86,13 @@ func SetDBManager(manager proto.DBManager) { func (manager *DBManager) GetDB(name string) proto.DB { return manager.resourcePools[name] } + +func (manager *DBManager) GetResoucePoolStatus() error { + for _, dataSource := range manager.resourcePools { + db := dataSource.(*sql.DB) + if err := db.TestConn(); err != nil { + return fmt.Errorf("datasource %s is not ready, err: %+v", db.Name(), err) + } + } + return nil +} diff --git a/pkg/sql/db.go b/pkg/sql/db.go index 9e083267..22de1851 100644 --- a/pkg/sql/db.go +++ b/pkg/sql/db.go @@ -335,7 +335,7 @@ func (db *DB) ping() (err error) { if currentCount%int64(db.pingTimesForChangeStatus) == 0 { db.pingCount.Swap(0) if currentCount > 0 { - db.status |= db.status + db.status = ^db.status & 1 } } }() @@ -359,3 +359,12 @@ func (db *DB) Close() { func (db *DB) IsClosed() (closed bool) { return db.pool.IsClosed() } + +func (db *DB) TestConn() error { + r, err := db.pool.Get(context.Background()) + if err != nil { + return err + } + conn := r.(*driver.BackendConnection) + return conn.Ping(context.Background()) +} diff --git a/testdata/mock_db_manager.go b/testdata/mock_db_manager.go index 52435dd7..6ba1193c 100644 --- a/testdata/mock_db_manager.go +++ b/testdata/mock_db_manager.go @@ -7,8 +7,9 @@ package testdata import ( reflect "reflect" - proto "github.com/cectc/dbpack/pkg/proto" gomock "github.com/golang/mock/gomock" + + proto "github.com/cectc/dbpack/pkg/proto" ) // MockDBManager is a mock of DBManager interface. diff --git a/third_party/parser/ast/misc.go b/third_party/parser/ast/misc.go index ae0788f5..7b7ff156 100644 --- a/third_party/parser/ast/misc.go +++ b/third_party/parser/ast/misc.go @@ -20,11 +20,12 @@ import ( "strconv" "strings" + "github.com/pingcap/errors" + "github.com/cectc/dbpack/third_party/parser/auth" "github.com/cectc/dbpack/third_party/parser/format" "github.com/cectc/dbpack/third_party/parser/model" "github.com/cectc/dbpack/third_party/parser/mysql" - "github.com/pingcap/errors" ) var ( diff --git a/third_party/parser/hintparser.go b/third_party/parser/hintparser.go index 3f1a72a2..3293ec13 100644 --- a/third_party/parser/hintparser.go +++ b/third_party/parser/hintparser.go @@ -31,9 +31,8 @@ package parser -import __yyfmt__ "fmt" - import ( + __yyfmt__ "fmt" "strconv" "github.com/cectc/dbpack/third_party/parser/ast" diff --git a/third_party/parser/parser.go b/third_party/parser/parser.go index 102a3915..1149f3ca 100644 --- a/third_party/parser/parser.go +++ b/third_party/parser/parser.go @@ -26,9 +26,8 @@ package parser -import __yyfmt__ "fmt" - import ( + __yyfmt__ "fmt" "strings" "github.com/cectc/dbpack/third_party/parser/ast" diff --git a/third_party/types/time.go b/third_party/types/time.go index 36c32685..b41767de 100644 --- a/third_party/types/time.go +++ b/third_party/types/time.go @@ -26,12 +26,13 @@ import ( "github.com/pingcap/log" - "github.com/cectc/dbpack/third_party/parser/mysql" - "github.com/cectc/dbpack/third_party/parser/terror" "github.com/pingcap/errors" "github.com/pingcap/tidb/sessionctx/stmtctx" tidbMath "github.com/pingcap/tidb/util/math" "github.com/pingcap/tidb/util/parser" + + "github.com/cectc/dbpack/third_party/parser/mysql" + "github.com/cectc/dbpack/third_party/parser/terror" ) // Time format without fractional seconds precision.