github.com/m3db/m3@v1.5.0/src/cmd/tools/dtest/tests/replace_up_node_remove_unseeded.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 replaceUpNodeRemoveUnseededTestCmd = &cobra.Command{ 33 Use: "replace_up_node_remove_unseeded", 34 Short: "Run a dtest where a node that is UP is replaced from the cluster. The replacing 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) Start the nodes from (1), and wait until they are bootstrapped. 39 (3) Replace any node in the cluster with the unused node in the cluster placement. 40 (4) Start the joining node's process. 41 (5) Wait until any shard on the joining node is marked as available. 42 (6) Remove the joining node from the cluster placement. 43 `, 44 Example: `./dtest replace_up_node_remove_unseeded --m3db-build path/to/m3dbnode --m3db-config path/to/m3dbnode.yaml --dtest-config path/to/dtest.yaml`, 45 Run: replaceUpNodeRemoveUnseededDTest, 46 } 47 ) 48 49 func replaceUpNodeRemoveUnseededDTest(cmd *cobra.Command, args []string) { 50 if err := globalArgs.Validate(); err != nil { 51 printUsage(cmd) 52 return 53 } 54 55 rawLogger := newLogger(cmd) 56 defer rawLogger.Sync() 57 logger := rawLogger.Sugar() 58 59 dt := harness.New(globalArgs, rawLogger) 60 defer dt.Close() 61 62 nodes := dt.Nodes() 63 numNodes := len(nodes) - 1 // leaving one spare 64 testCluster := dt.Cluster() 65 66 logger.Infof("setting up cluster") 67 setupNodes, err := testCluster.Setup(numNodes) 68 panicIfErr(err, "unable to setup cluster") 69 logger.Infof("setup cluster with %d nodes", numNodes) 70 71 logger.Infof("starting cluster") 72 panicIfErr(testCluster.Start(), "unable to start nodes") 73 logger.Infof("started cluster with %d nodes", numNodes) 74 75 logger.Infof("waiting until all instances are bootstrapped") 76 panicIfErr(dt.WaitUntilAllBootstrapped(setupNodes), "unable to bootstrap all nodes") 77 logger.Infof("all nodes bootstrapped successfully!") 78 79 // pick first node from cluster 80 nodeToReplace := setupNodes[0] 81 logger.Infof("replacing node: %v", nodeToReplace.ID()) 82 replacementNodes, err := testCluster.ReplaceNode(nodeToReplace) 83 panicIfErr(err, "unable to replace node") 84 panicIf(len(replacementNodes) < 1, "no replacement nodes returned") 85 logger.Infof("replaced node with: %+v", replacementNodes) 86 87 logger.Infof("starting replacement nodes") 88 // starting replacement nodes 89 for _, n := range replacementNodes { 90 panicIfErr(n.Start(), "unable to start node") 91 } 92 logger.Infof("started replacement nodes") 93 94 // NB(prateek): ideally we'd like to wait until the node begins bootstrapping, but we don't 95 // have a way to capture that node status. The rpc endpoint in m3dbnode only captures bootstrap 96 // status at the database level, and m3kv only captures state once a shard is marked as bootstrapped. 97 // So here we wait until any shard is marked as bootstrapped before continuing. 98 99 // wait until any shard is bootstrapped (i.e. marked available on new node) 100 replacementNode := replacementNodes[0] 101 logger.Infof("waiting till any shards are bootstrapped on node: %v", replacementNode.ID()) 102 timeout := dt.BootstrapTimeout() 103 anyBootstrapped := xclock.WaitUntil(func() bool { return dt.AnyInstanceShardHasState(replacementNode.ID(), shard.Available) }, timeout) 104 panicIf(!anyBootstrapped, "all shards not available") 105 106 // remove the node once it has a shard available 107 logger.Infof("node has at least 1 shard available. removing replacement nodes") 108 for _, n := range replacementNodes { 109 panicIfErr(testCluster.RemoveNode(n), "unable to remove node") 110 } 111 logger.Infof("removed replacement nodes") 112 }