github.com/m3db/m3@v1.5.0/src/cmd/tools/dtest/tests/replace_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 replaceUpNodeRemoveTestCmd = &cobra.Command{ 33 Use: "replace_up_node_remove", 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) 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) Replace any node in the cluster with the unused node in the cluster placement. 41 (5) Start the joining node's process. 42 (6) Wait until any shard on the joining node is marked as available. 43 (7) Remove the joining node from the cluster placement. 44 `, 45 Example: `./dtest replace_up_node_remove --m3db-build path/to/m3dbnode --m3db-config path/to/m3dbnode.yaml --dtest-config path/to/dtest.yaml`, 46 Run: replaceUpNodeRemoveDTest, 47 } 48 ) 49 50 func replaceUpNodeRemoveDTest(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 // pick first node from cluster 85 nodeToReplace := setupNodes[0] 86 logger.Infof("replacing node: %v", nodeToReplace.ID()) 87 replacementNodes, err := testCluster.ReplaceNode(nodeToReplace) 88 panicIfErr(err, "unable to replace node") 89 panicIf(len(replacementNodes) < 1, "no replacement nodes returned") 90 logger.Infof("replaced node with: %+v", replacementNodes) 91 92 logger.Infof("starting replacement nodes") 93 // starting replacement nodes 94 for _, n := range replacementNodes { 95 panicIfErr(n.Start(), "unable to start node") 96 } 97 logger.Infof("started replacement nodes") 98 99 // NB(prateek): ideally we'd like to wait until the node begins bootstrapping, but we don't 100 // have a way to capture that node status. The rpc endpoint in m3dbnode only captures bootstrap 101 // status at the database level, and m3kv only captures state once a shard is marked as bootstrapped. 102 // So here we wait until any shard is marked as bootstrapped before continuing. 103 104 // wait until any shard is bootstrapped (i.e. marked available on new node) 105 replacementNode := replacementNodes[0] 106 logger.Infof("waiting till any shards are bootstrapped on node: %v", replacementNode.ID()) 107 timeout := dt.BootstrapTimeout() 108 anyBootstrapped := xclock.WaitUntil(func() bool { return dt.AnyInstanceShardHasState(replacementNode.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 replacement nodes") 113 for _, n := range replacementNodes { 114 panicIfErr(testCluster.RemoveNode(n), "unable to remove node") 115 } 116 logger.Infof("removed replacement nodes") 117 }