vitess.io/vitess@v0.16.2/go/vt/vtgate/resolver.go (about)

     1  /*
     2  Copyright 2019 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 vtgate
    18  
    19  import (
    20  	"context"
    21  
    22  	"vitess.io/vitess/go/sqltypes"
    23  	"vitess.io/vitess/go/vt/key"
    24  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    25  	"vitess.io/vitess/go/vt/srvtopo"
    26  )
    27  
    28  // Resolver is the layer to resolve KeyspaceIds and KeyRanges
    29  // to shards. It will try to re-resolve shards if ScatterConn
    30  // returns retryable error, which may imply horizontal or vertical
    31  // resharding happened. It is implemented using a srvtopo.Resolver.
    32  type Resolver struct {
    33  	scatterConn *ScatterConn
    34  	resolver    *srvtopo.Resolver
    35  	toposerv    srvtopo.Server
    36  	cell        string
    37  }
    38  
    39  // NewResolver creates a new Resolver.
    40  func NewResolver(resolver *srvtopo.Resolver, serv srvtopo.Server, cell string, sc *ScatterConn) *Resolver {
    41  	return &Resolver{
    42  		scatterConn: sc,
    43  		resolver:    resolver,
    44  		toposerv:    serv,
    45  		cell:        cell,
    46  	}
    47  }
    48  
    49  // MessageStream streams messages.
    50  func (res *Resolver) MessageStream(ctx context.Context, keyspace string, shard string, keyRange *topodatapb.KeyRange, name string, callback func(*sqltypes.Result) error) error {
    51  	var destination key.Destination
    52  	if shard != "" {
    53  		// If we pass in a shard, resolve the keyspace/shard
    54  		// following redirects.
    55  		destination = key.DestinationShard(shard)
    56  	} else {
    57  		// If we pass in a KeyRange, resolve it to the proper shards.
    58  		// Note we support multiple shards here, we will just aggregate
    59  		// the message streams.
    60  		destination = key.DestinationExactKeyRange{KeyRange: keyRange}
    61  	}
    62  	rss, err := res.resolver.ResolveDestination(ctx, keyspace, topodatapb.TabletType_PRIMARY, destination)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	return res.scatterConn.MessageStream(ctx, rss, name, callback)
    67  }
    68  
    69  // GetGatewayCacheStatus returns a displayable version of the Gateway cache.
    70  func (res *Resolver) GetGatewayCacheStatus() TabletCacheStatusList {
    71  	return res.scatterConn.GetGatewayCacheStatus()
    72  }