Skip to content

Commit 113341e

Browse files
authoredFeb 15, 2024
Refactoring (#502)
* Refactoring * Fixed lint * Fixed unittests & Removed incorrect one
1 parent f35eea0 commit 113341e

File tree

6 files changed

+57
-217
lines changed

6 files changed

+57
-217
lines changed
 

‎router/mock/qrouter/mock_qrouter.go

+7-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎router/qrouter/explain.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// TODO : unit tests
1313
func (qr *ProxyQrouter) Explain(ctx context.Context, stmt *lyx.Explain, cli *clientinteractor.PSQLInteractor) error {
14-
meta := NewRoutingMetadataContext(nil, nil, cli.GetDistribution(), nil, nil)
14+
meta := NewRoutingMetadataContext(cli.GetDistribution(), nil, nil)
1515

1616
switch node := stmt.Stmt.(type) {
1717
case *lyx.VariableSetStmt:

‎router/qrouter/proxy_routing.go

+31-44
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/pg-sharding/spqr/pkg/config"
99
"github.com/pg-sharding/spqr/pkg/models/hashfunction"
1010
"github.com/pg-sharding/spqr/pkg/models/kr"
11-
"github.com/pg-sharding/spqr/pkg/models/shrule"
1211
"github.com/pg-sharding/spqr/pkg/session"
1312
"github.com/pg-sharding/spqr/pkg/spqrlog"
1413
"github.com/pg-sharding/spqr/qdb"
@@ -64,39 +63,20 @@ type RoutingMetadataContext struct {
6463
// INSERT INTO x (...) SELECT 7
6564
TargetList []lyx.Node
6665

67-
rls []*shrule.ShardingRule
68-
krs []*kr.KeyRange
6966
distribution string
7067

7168
params [][]byte
7269
paramsFormatCodes []int16
7370
// TODO: include client ops and metadata here
7471
}
7572

76-
func (m *RoutingMetadataContext) CheckColumnRls(colname string) bool {
77-
for i := range m.rls {
78-
for _, c := range m.rls[i].Entries() {
79-
if c.Column == colname {
80-
return true
81-
}
82-
}
83-
}
84-
return false
85-
}
86-
87-
func NewRoutingMetadataContext(
88-
krs []*kr.KeyRange,
89-
rls []*shrule.ShardingRule,
90-
ds string,
91-
params [][]byte, paramsFormatCodes []int16) *RoutingMetadataContext {
73+
func NewRoutingMetadataContext(ds string, params [][]byte, paramsFormatCodes []int16) *RoutingMetadataContext {
9274

9375
meta := &RoutingMetadataContext{
9476
rels: map[RelationFQN][]string{},
9577
tableAliases: map[string]RelationFQN{},
9678
exprs: map[RelationFQN]map[string]string{},
9779
unparsed_columns: map[string]struct{}{},
98-
krs: krs,
99-
rls: rls,
10080
distribution: ds,
10181
params: params,
10282
}
@@ -171,19 +151,19 @@ func (qr *ProxyQrouter) DeparseExprShardingEntries(expr lyx.Node, meta *RoutingM
171151
}
172152

173153
// TODO : unit tests
174-
func (qr *ProxyQrouter) DeparseKeyWithRangesInternal(ctx context.Context, key string, meta *RoutingMetadataContext) (*routingstate.DataShardRoute, error) {
154+
func (qr *ProxyQrouter) DeparseKeyWithRangesInternal(_ context.Context, key string, krs []*kr.KeyRange) (*routingstate.DataShardRoute, error) {
175155
spqrlog.Zero.Debug().
176156
Str("key", key).
177157
Msg("checking key")
178158

179159
spqrlog.Zero.Debug().
180160
Str("key", key).
181-
Int("key-ranges-count", len(meta.krs)).
161+
Int("key-ranges-count", len(krs)).
182162
Msg("checking key with key ranges")
183163

184164
var matched_krkey *kr.KeyRange = nil
185165

186-
for _, krkey := range meta.krs {
166+
for _, krkey := range krs {
187167
if kr.CmpRangesLessEqual(krkey.LowerBound, []byte(key)) &&
188168
(matched_krkey == nil || kr.CmpRangesLessEqual(matched_krkey.LowerBound, krkey.LowerBound)) {
189169
matched_krkey = krkey
@@ -205,7 +185,7 @@ func (qr *ProxyQrouter) DeparseKeyWithRangesInternal(ctx context.Context, key st
205185
}
206186

207187
// TODO : unit tests
208-
func (qr *ProxyQrouter) RouteKeyWithRanges(ctx context.Context, expr lyx.Node, meta *RoutingMetadataContext, hf hashfunction.HashFunctionType) (*routingstate.DataShardRoute, error) {
188+
func (qr *ProxyQrouter) RouteKeyWithRanges(ctx context.Context, expr lyx.Node, meta *RoutingMetadataContext, krs []*kr.KeyRange, hf hashfunction.HashFunctionType) (*routingstate.DataShardRoute, error) {
209189
switch e := expr.(type) {
210190
case *lyx.ParamRef:
211191
if e.Number > len(meta.params) {
@@ -233,15 +213,15 @@ func (qr *ProxyQrouter) RouteKeyWithRanges(ctx context.Context, expr lyx.Node, m
233213
}
234214
spqrlog.Zero.Debug().Str("key", string(routeParam)).Str("hashed key", string(hashedKey)).Msg("applying hash function on key")
235215

236-
return qr.DeparseKeyWithRangesInternal(ctx, string(hashedKey), meta)
216+
return qr.DeparseKeyWithRangesInternal(ctx, string(hashedKey), krs)
237217
case *lyx.AExprSConst:
238218
hashedKey, err := hashfunction.ApplyHashFunction([]byte(e.Value), hf)
239219
if err != nil {
240220
return nil, err
241221
}
242222

243223
spqrlog.Zero.Debug().Str("key", e.Value).Str("hashed key", string(hashedKey)).Msg("applying hash function on key")
244-
return qr.DeparseKeyWithRangesInternal(ctx, string(hashedKey), meta)
224+
return qr.DeparseKeyWithRangesInternal(ctx, string(hashedKey), krs)
245225
case *lyx.AExprIConst:
246226
val := fmt.Sprintf("%d", e.Value)
247227
hashedKey, err := hashfunction.ApplyHashFunction([]byte(val), hf)
@@ -250,7 +230,7 @@ func (qr *ProxyQrouter) RouteKeyWithRanges(ctx context.Context, expr lyx.Node, m
250230
}
251231

252232
spqrlog.Zero.Debug().Int("key", e.Value).Str("hashed key", string(hashedKey)).Msg("applying hash function on key")
253-
return qr.DeparseKeyWithRangesInternal(ctx, string(hashedKey), meta)
233+
return qr.DeparseKeyWithRangesInternal(ctx, string(hashedKey), krs)
254234
default:
255235
return nil, ComplexQuery
256236
}
@@ -746,17 +726,8 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
746726
}
747727

748728
/* TODO: delay this until step 2. */
749-
krs, err := qr.mgr.ListKeyRanges(ctx, queryDistribution)
750-
if err != nil {
751-
return nil, err
752-
}
753729

754-
rls, err := qr.mgr.ListShardingRules(ctx, queryDistribution)
755-
if err != nil {
756-
return nil, err
757-
}
758-
759-
meta := NewRoutingMetadataContext(krs, rls, queryDistribution, sph.BindParams(), sph.BindParamFormatCodes())
730+
meta := NewRoutingMetadataContext(queryDistribution, sph.BindParams(), sph.BindParamFormatCodes())
760731

761732
tsa := config.TargetSessionAttrsAny
762733

@@ -931,9 +902,16 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
931902
var route_err error
932903
for rfqn, cols := range meta.rels {
933904

934-
/*
935-
*
936-
*/
905+
// TODO: check by whole RFQN
906+
ds, err := qr.mgr.GetRelationDistribution(ctx, rfqn.RelationName)
907+
if err != nil {
908+
return nil, err
909+
}
910+
911+
krs, err := qr.mgr.ListKeyRanges(ctx, ds.Id)
912+
if err != nil {
913+
return nil, err
914+
}
937915

938916
if rule, err := MatchShardingRule(ctx, rfqn.RelationName, cols, qr.mgr.QDB()); err != nil {
939917
for _, col := range cols {
@@ -953,7 +931,7 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
953931
continue
954932
}
955933

956-
currroute, err := qr.DeparseKeyWithRangesInternal(ctx, string(hashedKey), meta)
934+
currroute, err := qr.DeparseKeyWithRangesInternal(ctx, string(hashedKey), krs)
957935
if err != nil {
958936
route_err = err
959937
spqrlog.Zero.Debug().Err(route_err).Msg("temporarily skip the route error")
@@ -996,6 +974,15 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
996974
}
997975
}
998976

977+
ds, err := qr.mgr.GetRelationDistribution(ctx, meta.InsertStmtRel)
978+
if err != nil {
979+
return nil, err
980+
}
981+
krs, err := qr.mgr.ListKeyRanges(ctx, ds.Id)
982+
if err != nil {
983+
return nil, err
984+
}
985+
999986
hf, err := hashfunction.HashFunctionByName(rule.Entries[0].HashFunction)
1000987
if err != nil {
1001988
/* failed to resolve hash function */
@@ -1005,7 +992,7 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
1005992
meta.offsets = offsets
1006993
routed := false
1007994
if len(meta.offsets) != 0 && len(meta.TargetList) > meta.offsets[0] {
1008-
currroute, err := qr.RouteKeyWithRanges(ctx, meta.TargetList[meta.offsets[0]], meta, hf)
995+
currroute, err := qr.RouteKeyWithRanges(ctx, meta.TargetList[meta.offsets[0]], meta, krs, hf)
1009996
if err == nil {
1010997
/* else failed, ignore */
1011998
spqrlog.Zero.Debug().
@@ -1023,7 +1010,7 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
10231010
if len(meta.offsets) != 0 && len(meta.ValuesLists) > meta.offsets[0] && !routed && meta.ValuesLists != nil {
10241011
// only first value from value list
10251012

1026-
currroute, err := qr.RouteKeyWithRanges(ctx, meta.ValuesLists[meta.offsets[0]], meta, hf)
1013+
currroute, err := qr.RouteKeyWithRanges(ctx, meta.ValuesLists[meta.offsets[0]], meta, krs, hf)
10271014
if err == nil { /* else failed, ignore */
10281015
spqrlog.Zero.Debug().
10291016
Interface("current-route", currroute).

0 commit comments

Comments
 (0)