Skip to content

Commit 6c69073

Browse files
authoredDec 2, 2024
minor DefaultRouteBehaviour refactoring (#845)
1 parent 31d4dd9 commit 6c69073

File tree

5 files changed

+21
-10
lines changed

5 files changed

+21
-10
lines changed
 

‎cmd/router/main.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"runtime"
1212
"runtime/pprof"
1313
"strconv"
14+
"strings"
1415
"sync"
1516
"syscall"
1617

@@ -83,7 +84,7 @@ func init() {
8384
rootCmd.PersistentFlags().IntVarP(&adminPort, "admin-port", "", 0, "router Metadata PostgreSQL interface admin port")
8485
rootCmd.PersistentFlags().IntVarP(&grpcPort, "grpc-port", "", 0, "router Metadata GRPC interface admin port")
8586

86-
rootCmd.PersistentFlags().StringVarP(&default_route_behaviour, "default-route-behaviour", "", "", "router block or scatters-out failed to route statements")
87+
rootCmd.PersistentFlags().StringVarP(&default_route_behaviour, "default-route-behaviour", "", "", "The router will either block or scatter-out multishard queries by default")
8788

8889
rootCmd.PersistentFlags().BoolVarP(&pgprotoDebug, "proto-debug", "", false, "reply router notice, warning, etc")
8990
rootCmd.AddCommand(runCmd)
@@ -218,7 +219,11 @@ var runCmd = &cobra.Command{
218219
}
219220

220221
if default_route_behaviour != "" {
221-
rcfg.Qr.DefaultRouteBehaviour = default_route_behaviour
222+
if strings.ToLower(default_route_behaviour) == "block" {
223+
rcfg.Qr.DefaultRouteBehaviour = config.DefaultRouteBehaviourBlock
224+
} else {
225+
rcfg.Qr.DefaultRouteBehaviour = config.DefaultRouteBehaviourAllow
226+
}
222227
}
223228

224229
router, err := instance.NewRouter(ctx, rcfg, os.Getenv("NOTIFY_SOCKET"), persist)

‎docs/configuration/router.mdx

+6-6
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ Map of string to Shard objects. Refer to the `Shard` struct in the [pkg/config/r
8383

8484
## Feature Flags
8585

86-
| Setting | Description | Possible Values |
87-
|-----------------------------------------|-------------------------------------------------|-----------------|
88-
| `maintain_params` | Whether to maintain parameters flag. | `true`, `false` |
89-
| `query_routing.default_route_behaviour` | Whether to explicitly block multishard queries. | `BLOCK`, `` |
90-
| `with_jaeger` | Whether to integrate with Jaeger for tracing. | `true`, `false` |
91-
| `world_shard_fallback` | Whether to enable fallback to world shard. | `true`, `false` |
86+
| Setting | Description | Possible Values |
87+
|-----------------------------------------|-------------------------------------------------|------------------|
88+
| `maintain_params` | Whether to maintain parameters flag. | `true`, `false` |
89+
| `query_routing.default_route_behaviour` | Whether to explicitly block multishard queries. | `BLOCK`, `ALLOW` |
90+
| `with_jaeger` | Whether to integrate with Jaeger for tracing. | `true`, `false` |
91+
| `world_shard_fallback` | Whether to enable fallback to world shard. | `true`, `false` |
9292

9393

9494
## Mode Settings

‎pkg/config/router.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
type PoolMode string
1818
type ShardType string
1919
type RouterMode string
20+
type DefaultRouteBehaviour string
2021

2122
const (
2223
PoolModeSession = PoolMode("SESSION")
@@ -27,6 +28,9 @@ const (
2728

2829
LocalMode = RouterMode("LOCAL")
2930
ProxyMode = RouterMode("PROXY")
31+
32+
DefaultRouteBehaviourBlock = DefaultRouteBehaviour("BLOCK")
33+
DefaultRouteBehaviourAllow = DefaultRouteBehaviour("ALLOW")
3034
)
3135

3236
var cfgRouter Router
@@ -86,7 +90,7 @@ type Router struct {
8690
}
8791

8892
type QRouter struct {
89-
DefaultRouteBehaviour string `json:"default_route_behaviour" toml:"default_route_behaviour" yaml:"default_route_behaviour"`
93+
DefaultRouteBehaviour DefaultRouteBehaviour `json:"default_route_behaviour" toml:"default_route_behaviour" yaml:"default_route_behaviour"`
9094
}
9195

9296
type BackendRule struct {

‎router/qrouter/proxy_routing.go

+2
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,8 @@ func (qr *ProxyQrouter) Route(ctx context.Context, stmt lyx.Node, sph session.Se
12031203
switch sph.DefaultRouteBehaviour() {
12041204
case "BLOCK":
12051205
return routingstate.SkipRoutingState{}, spqrerror.NewByCode(spqrerror.SPQR_NO_DATASHARD)
1206+
case "ALLOW":
1207+
return v, nil
12061208
default:
12071209
return v, nil
12081210
}

‎router/rulerouter/rulerouter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func NewRouter(tlsconfig *tls.Config, rcfg *config.Router, notifier *notifier.No
147147

148148
// TODO : unit tests
149149
func (r *RuleRouterImpl) PreRoute(conn net.Conn, pt port.RouterPortType) (rclient.RouterClient, error) {
150-
cl := rclient.NewPsqlClient(conn, pt, r.Config().Qr.DefaultRouteBehaviour, r.Config().ShowNoticeMessages, r.Config().DefaultTSA)
150+
cl := rclient.NewPsqlClient(conn, pt, string(r.Config().Qr.DefaultRouteBehaviour), r.Config().ShowNoticeMessages, r.Config().DefaultTSA)
151151

152152
tlsConfig := r.tlsconfig
153153
if pt == port.UnixSocketPortType {

0 commit comments

Comments
 (0)