vitess.io/vitess@v0.16.2/go/vt/vtexplain/vtexplain_topo.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 vtexplain
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"sync"
    23  
    24  	"vitess.io/vitess/go/vt/topo"
    25  
    26  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    27  	vschemapb "vitess.io/vitess/go/vt/proto/vschema"
    28  )
    29  
    30  // ExplainTopo satisfies the srvtopo.Server interface.
    31  // Modeled after the vtgate test sandboxTopo
    32  type ExplainTopo struct {
    33  	// Map of keyspace name to vschema
    34  	Keyspaces map[string]*vschemapb.Keyspace
    35  
    36  	// Map of ks/shard to test tablet connection
    37  	TabletConns map[string]*explainTablet
    38  
    39  	KeyspaceShards map[string]map[string]*topodatapb.ShardReference
    40  
    41  	// Synchronization lock
    42  	Lock sync.Mutex
    43  
    44  	// Number of shards for sharded keyspaces
    45  	NumShards int
    46  
    47  	TopoServer *topo.Server
    48  }
    49  
    50  func (et *ExplainTopo) getSrvVSchema() *vschemapb.SrvVSchema {
    51  	et.Lock.Lock()
    52  	defer et.Lock.Unlock()
    53  
    54  	return &vschemapb.SrvVSchema{
    55  		Keyspaces: et.Keyspaces,
    56  	}
    57  }
    58  
    59  // GetTopoServer is part of the srvtopo.Server interface
    60  func (et *ExplainTopo) GetTopoServer() (*topo.Server, error) {
    61  	return et.TopoServer, nil
    62  }
    63  
    64  // GetSrvKeyspaceNames is part of the srvtopo.Server interface.
    65  func (et *ExplainTopo) GetSrvKeyspaceNames(ctx context.Context, cell string, staleOK bool) ([]string, error) {
    66  	et.Lock.Lock()
    67  	defer et.Lock.Unlock()
    68  
    69  	keyspaces := make([]string, 0, 1)
    70  	for k := range et.Keyspaces {
    71  		keyspaces = append(keyspaces, k)
    72  	}
    73  	return keyspaces, nil
    74  }
    75  
    76  // GetSrvKeyspace is part of the srvtopo.Server interface.
    77  func (et *ExplainTopo) GetSrvKeyspace(ctx context.Context, cell, keyspace string) (*topodatapb.SrvKeyspace, error) {
    78  	et.Lock.Lock()
    79  	defer et.Lock.Unlock()
    80  
    81  	vschema := et.Keyspaces[keyspace]
    82  	if vschema == nil {
    83  		return nil, fmt.Errorf("no vschema for keyspace %s", keyspace)
    84  	}
    85  
    86  	shards := make([]*topodatapb.ShardReference, 0, len(et.KeyspaceShards[keyspace]))
    87  	for _, shard := range et.KeyspaceShards[keyspace] {
    88  		shards = append(shards, shard)
    89  	}
    90  
    91  	srvKeyspace := &topodatapb.SrvKeyspace{
    92  		Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{
    93  			{
    94  				ServedType:      topodatapb.TabletType_PRIMARY,
    95  				ShardReferences: shards,
    96  			},
    97  			{
    98  				ServedType:      topodatapb.TabletType_REPLICA,
    99  				ShardReferences: shards,
   100  			},
   101  			{
   102  				ServedType:      topodatapb.TabletType_RDONLY,
   103  				ShardReferences: shards,
   104  			},
   105  		},
   106  	}
   107  
   108  	return srvKeyspace, nil
   109  }
   110  
   111  func (et *ExplainTopo) WatchSrvKeyspace(ctx context.Context, cell, keyspace string, callback func(*topodatapb.SrvKeyspace, error) bool) {
   112  	ks, err := et.GetSrvKeyspace(ctx, cell, keyspace)
   113  	callback(ks, err)
   114  }
   115  
   116  // WatchSrvVSchema is part of the srvtopo.Server interface.
   117  func (et *ExplainTopo) WatchSrvVSchema(ctx context.Context, cell string, callback func(*vschemapb.SrvVSchema, error) bool) {
   118  	callback(et.getSrvVSchema(), nil)
   119  }