vitess.io/vitess@v0.16.2/go/vt/vttest/topoctl.go (about)

     1  package vttest
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"vitess.io/vitess/go/vt/log"
     8  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
     9  	"vitess.io/vitess/go/vt/proto/vttest"
    10  	"vitess.io/vitess/go/vt/topo"
    11  )
    12  
    13  // TopoManager is an interface to manage things in a remote topology server for local cluster
    14  type TopoManager interface {
    15  	Setup() error
    16  }
    17  
    18  // Topoctl implements TopoManager. For now it only sets up cells in remote topology server if any of them doesn't exist.
    19  type Topoctl struct {
    20  	TopoImplementation      string
    21  	TopoGlobalServerAddress string
    22  	TopoGlobalRoot          string
    23  	Topology                *vttest.VTTestTopology
    24  }
    25  
    26  func (ctl *Topoctl) Setup() error {
    27  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    28  	defer cancel()
    29  
    30  	topoServer, err := topo.OpenServer(ctl.TopoImplementation, ctl.TopoGlobalServerAddress, ctl.TopoGlobalRoot)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	log.Infof("Creating cells if they don't exist in the provided topo server %s %s %s", ctl.TopoImplementation, ctl.TopoGlobalServerAddress, ctl.TopoGlobalRoot)
    36  	// Create cells if it doesn't exist to be idempotent. Should work when we share the same topo server across multiple local clusters.
    37  	for _, cell := range ctl.Topology.Cells {
    38  		_, err := topoServer.GetCellInfo(ctx, cell, true)
    39  		// Cell info already exists. no-op
    40  		if err == nil {
    41  			continue
    42  		}
    43  
    44  		// Return any error that's not NoNode
    45  		if !topo.IsErrType(err, topo.NoNode) {
    46  			return err
    47  		}
    48  
    49  		// Use the same topo server address in cell info, or else it would cause error when talking to local cell topo
    50  		// Use dummy (empty) cell root till we have a use case to set up local cells properly
    51  		cellInfo := &topodatapb.CellInfo{ServerAddress: ctl.TopoGlobalServerAddress}
    52  
    53  		err = topoServer.CreateCellInfo(ctx, cell, cellInfo)
    54  		if err != nil {
    55  			return err
    56  		}
    57  		log.Infof("Created cell info for %s in the topo server %s %s %s", cell, ctl.TopoImplementation, ctl.TopoGlobalServerAddress, ctl.TopoGlobalRoot)
    58  	}
    59  
    60  	return nil
    61  }