vitess.io/vitess@v0.16.2/go/vt/topotools/routing_rules.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package topotools
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"vitess.io/vitess/go/vt/log"
    25  	"vitess.io/vitess/go/vt/topo"
    26  
    27  	vschemapb "vitess.io/vitess/go/vt/proto/vschema"
    28  )
    29  
    30  // GetRoutingRules fetches routing rules from the topology server and returns a
    31  // mapping of fromTable=>[]toTables.
    32  func GetRoutingRules(ctx context.Context, ts *topo.Server) (map[string][]string, error) {
    33  	rrs, err := ts.GetRoutingRules(ctx)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	rules := make(map[string][]string, len(rrs.Rules))
    39  	for _, rr := range rrs.Rules {
    40  		rules[rr.FromTable] = rr.ToTables
    41  	}
    42  
    43  	return rules, nil
    44  }
    45  
    46  // SaveRoutingRules converts a mapping of fromTable=>[]toTables into a
    47  // vschemapb.RoutingRules protobuf message and saves it in the topology.
    48  func SaveRoutingRules(ctx context.Context, ts *topo.Server, rules map[string][]string) error {
    49  	log.Infof("Saving routing rules %v\n", rules)
    50  
    51  	rrs := &vschemapb.RoutingRules{Rules: make([]*vschemapb.RoutingRule, 0, len(rules))}
    52  	for from, to := range rules {
    53  		rrs.Rules = append(rrs.Rules, &vschemapb.RoutingRule{
    54  			FromTable: from,
    55  			ToTables:  to,
    56  		})
    57  	}
    58  
    59  	return ts.SaveRoutingRules(ctx, rrs)
    60  }
    61  
    62  // GetShardRoutingRules fetches shard routing rules from the topology server and returns a
    63  // mapping of fromKeyspace.Shard=>toKeyspace.
    64  func GetShardRoutingRules(ctx context.Context, ts *topo.Server) (map[string]string, error) {
    65  	rrs, err := ts.GetShardRoutingRules(ctx)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	rules := make(map[string]string, len(rrs.Rules))
    71  	for _, rr := range rrs.Rules {
    72  		rules[fmt.Sprintf("%s.%s", rr.FromKeyspace, rr.Shard)] = rr.ToKeyspace
    73  	}
    74  
    75  	return rules, nil
    76  }
    77  
    78  // SaveShardRoutingRules converts a mapping of fromKeyspace.Shard=>toKeyspace into a
    79  // vschemapb.ShardRoutingRules protobuf message and saves it in the topology.
    80  func SaveShardRoutingRules(ctx context.Context, ts *topo.Server, srr map[string]string) error {
    81  	log.Infof("Saving shard routing rules %v\n", srr)
    82  
    83  	srs := &vschemapb.ShardRoutingRules{Rules: make([]*vschemapb.ShardRoutingRule, 0, len(srr))}
    84  	for from, to := range srr {
    85  		arr := strings.Split(from, ".")
    86  		fromKeyspace := arr[0]
    87  		shard := arr[1]
    88  		srs.Rules = append(srs.Rules, &vschemapb.ShardRoutingRule{
    89  			FromKeyspace: fromKeyspace,
    90  			ToKeyspace:   to,
    91  			Shard:        shard,
    92  		})
    93  	}
    94  
    95  	return ts.SaveShardRoutingRules(ctx, srs)
    96  }