github.com/m3db/m3@v1.5.0/src/cmd/tools/dtest/tests/add_up_node_remove.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package dtests
    22  
    23  import (
    24  	"github.com/m3db/m3/src/cluster/shard"
    25  	"github.com/m3db/m3/src/cmd/tools/dtest/harness"
    26  	xclock "github.com/m3db/m3/src/x/clock"
    27  
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  var (
    32  	addUpNodeRemoveTestCmd = &cobra.Command{
    33  		Use:   "add_up_node_remove",
    34  		Short: "Run a dtest where a node that is UP, is added to the cluster. Node is removed as it begins bootstrapping.",
    35  		Long: `
    36  		Perform the following operations on the provided set of nodes:
    37  		(1) Create a new cluster placement using all but one of the provided nodes.
    38  		(2) Seed the nodes used in (1), with initial data on their respective file-systems.
    39  		(3) Start the nodes from (1), and wait until they are bootstrapped.
    40  		(4) Start the one unused node's process.
    41  		(5) Add the node from (4) to the cluster placement.
    42  		(6) Wait until any shard on the node is marked as available.
    43  		(7) Remove the node from the cluster placement.
    44  `,
    45  		Example: `./dtest add_up_node_remove --m3db-build path/to/m3dbnode --m3db-config path/to/m3dbnode.yaml --dtest-config path/to/dtest.yaml`,
    46  		Run:     addUpNodeRemoveDTest,
    47  	}
    48  )
    49  
    50  func addUpNodeRemoveDTest(cmd *cobra.Command, args []string) {
    51  	if err := globalArgs.Validate(); err != nil {
    52  		printUsage(cmd)
    53  		return
    54  	}
    55  
    56  	rawLogger := newLogger(cmd)
    57  	defer rawLogger.Sync()
    58  	logger := rawLogger.Sugar()
    59  
    60  	dt := harness.New(globalArgs, rawLogger)
    61  	defer dt.Close()
    62  
    63  	nodes := dt.Nodes()
    64  	numNodes := len(nodes) - 1 // leaving one spare
    65  	testCluster := dt.Cluster()
    66  
    67  	logger.Infof("setting up cluster")
    68  	setupNodes, err := testCluster.Setup(numNodes)
    69  	panicIfErr(err, "unable to setup cluster")
    70  	logger.Infof("setup cluster with %d nodes", numNodes)
    71  
    72  	logger.Infof("seeding nodes with initial data")
    73  	panicIfErr(dt.Seed(setupNodes), "unable to seed nodes")
    74  	logger.Infof("seeded nodes")
    75  
    76  	logger.Infof("starting cluster")
    77  	panicIfErr(testCluster.Start(), "unable to start nodes")
    78  	logger.Infof("started cluster with %d nodes", numNodes)
    79  
    80  	logger.Infof("waiting until all instances are bootstrapped")
    81  	panicIfErr(dt.WaitUntilAllBootstrapped(setupNodes), "unable to bootstrap all nodes")
    82  	logger.Infof("all nodes bootstrapped successfully!")
    83  
    84  	// get a spare, ensure it's up and add to the cluster
    85  	logger.Infof("adding spare to the cluster")
    86  	spares := testCluster.SpareNodes()
    87  	panicIf(len(spares) < 1, "no spares to add to the cluster")
    88  	spare := spares[0]
    89  
    90  	// start node
    91  	logger.Infof("starting new node: %v", spare.ID())
    92  	panicIfErr(spare.Start(), "unable to start node")
    93  	logger.Infof("started node")
    94  
    95  	// add to placement
    96  	logger.Infof("adding node")
    97  	panicIfErr(testCluster.AddSpecifiedNode(spare), "unable to add node")
    98  	logger.Infof("added node")
    99  
   100  	// NB(prateek): ideally we'd like to wait until the node begins bootstrapping, but we don't
   101  	// have a way to capture that node status. The rpc endpoint in m3dbnode only captures bootstrap
   102  	// status at the database level, and m3kv only captures state once a shard is marked as bootstrapped.
   103  	// So here we wait until any shard is marked as bootstrapped before continuing.
   104  
   105  	// wait until any shard is bootstrapped (i.e. marked available on new node)
   106  	logger.Infof("waiting till any shards are bootstrapped on new node")
   107  	timeout := dt.BootstrapTimeout()
   108  	anyBootstrapped := xclock.WaitUntil(func() bool { return dt.AnyInstanceShardHasState(spare.ID(), shard.Available) }, timeout)
   109  	panicIf(!anyBootstrapped, "all shards not available")
   110  
   111  	// remove the node once it has a shard available
   112  	logger.Infof("node has at least 1 shard available. removing node")
   113  	panicIfErr(testCluster.RemoveNode(spare), "unable to remove node")
   114  	logger.Infof("removed node")
   115  }