|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/aerospike/aerolab/parallelize" |
| 12 | + aeroconf "github.com/rglonek/aerospike-config-file-parser" |
| 13 | +) |
| 14 | + |
| 15 | +type confSCCmd struct { |
| 16 | + ClusterName TypeClusterName `short:"n" long:"name" description:"Cluster name" default:"mydc"` |
| 17 | + Namespace string `short:"m" long:"namespace" description:"Namespace to change" default:"test"` |
| 18 | + Path string `short:"p" long:"path" description:"Path to aerospike.conf" default:"/etc/aerospike/aerospike.conf"` |
| 19 | + Force bool `short:"f" long:"force" description:"If set, will zero out the devices even if strong-consistency was already configured"` |
| 20 | + parallelThreadsCmd |
| 21 | +} |
| 22 | + |
| 23 | +func (c *confSCCmd) Execute(args []string) error { |
| 24 | + if earlyProcessV2(nil, true) { |
| 25 | + return nil |
| 26 | + } |
| 27 | + log.Println("Running conf.sc") |
| 28 | + // stop aerospike |
| 29 | + log.Println("conf.sc: Stopping aerospike") |
| 30 | + a.opts.Aerospike.Stop.ClusterName = c.ClusterName |
| 31 | + a.opts.Aerospike.Stop.ParallelThreads = c.ParallelThreads |
| 32 | + err := a.opts.Aerospike.Stop.run(nil, "stop", os.Stdout) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + // get node count |
| 37 | + log.Println("conf.sc: Getting cluster size") |
| 38 | + nodes, err := b.NodeListInCluster(c.ClusterName.String()) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + // patch aerospike.conf |
| 43 | + log.Println("conf.sc: Patching aerospike.conf") |
| 44 | + returns := parallelize.MapLimit(nodes, c.ParallelThreads, func(node int) error { |
| 45 | + // read config file |
| 46 | + out, err := b.RunCommands(c.ClusterName.String(), [][]string{{"cat", c.Path}}, []int{node}) |
| 47 | + if err != nil { |
| 48 | + nout := "" |
| 49 | + for _, n := range out { |
| 50 | + nout = nout + "\n" + string(n) |
| 51 | + } |
| 52 | + return fmt.Errorf("error on cluster %s: %s: %s", c.ClusterName, nout, err) |
| 53 | + } |
| 54 | + fileContents := bytes.NewReader(out[0]) |
| 55 | + // edit actual file contents |
| 56 | + s, err := aeroconf.Parse(fileContents) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + if s.Type("namespace "+c.Namespace) != aeroconf.ValueStanza { |
| 61 | + return errors.New("namespace not found") |
| 62 | + } |
| 63 | + changes := false |
| 64 | + x := s.Stanza("namespace " + c.Namespace) |
| 65 | + // check RF |
| 66 | + if x.Type("replication-factor") == aeroconf.ValueString { |
| 67 | + vals, err := x.GetValues("replication-factor") |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + if len(vals) != 1 { |
| 72 | + return errors.New("replication-factor parameter error") |
| 73 | + } |
| 74 | + rf, err := strconv.Atoi(*vals[0]) |
| 75 | + if err != nil { |
| 76 | + return errors.New("replication-factor parameter invalid value found") |
| 77 | + } |
| 78 | + if rf > len(nodes) { |
| 79 | + x.SetValue("replication-factor", strconv.Itoa(len(nodes))) |
| 80 | + changes = true |
| 81 | + } |
| 82 | + } else if len(nodes) == 1 { |
| 83 | + x.SetValue("replication-factor", "1") |
| 84 | + changes = true |
| 85 | + } |
| 86 | + // get SC |
| 87 | + rmFiles := false |
| 88 | + if x.Type("strong-consistency") != aeroconf.ValueString { |
| 89 | + x.SetValue("strong-consistency", "true") |
| 90 | + changes = true |
| 91 | + rmFiles = true |
| 92 | + } else { |
| 93 | + vals, err := x.GetValues("strong-consistency") |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + if len(vals) != 1 { |
| 98 | + return errors.New("strong-consistency parameter error") |
| 99 | + } |
| 100 | + if *vals[0] != "true" { |
| 101 | + x.SetValue("strong-consistency", "true") |
| 102 | + changes = true |
| 103 | + rmFiles = true |
| 104 | + } |
| 105 | + } |
| 106 | + // remove storage files |
| 107 | + if rmFiles || c.Force { |
| 108 | + if x.Type("storage-engine device") == aeroconf.ValueStanza { |
| 109 | + x = x.Stanza("storage-engine device") |
| 110 | + if x.Type("file") == aeroconf.ValueString { |
| 111 | + files, err := x.GetValues("file") |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + cmd := []string{"rm", "-f"} |
| 116 | + for _, file := range files { |
| 117 | + cmd = append(cmd, *file) |
| 118 | + } |
| 119 | + data, err := b.RunCommands(string(c.ClusterName), [][]string{cmd}, []int{node}) |
| 120 | + if len(data) == 0 { |
| 121 | + data = [][]byte{{'-'}} |
| 122 | + } |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("%s: %s", err, string(data[0])) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + // store changes back |
| 130 | + if changes { |
| 131 | + var buf bytes.Buffer |
| 132 | + err = s.Write(&buf, "", " ", true) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + contents := buf.Bytes() |
| 137 | + fileContents = bytes.NewReader(contents) |
| 138 | + // edit end |
| 139 | + err = b.CopyFilesToClusterReader(c.ClusterName.String(), []fileListReader{{filePath: c.Path, fileContents: fileContents, fileSize: len(contents)}}, []int{node}) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + } |
| 144 | + return nil |
| 145 | + }) |
| 146 | + isError := false |
| 147 | + for i, ret := range returns { |
| 148 | + if ret != nil { |
| 149 | + log.Printf("Node %d returned %s", nodes[i], ret) |
| 150 | + isError = true |
| 151 | + } |
| 152 | + } |
| 153 | + if isError { |
| 154 | + return errors.New("some nodes returned errors") |
| 155 | + } |
| 156 | + // restart aerospike |
| 157 | + log.Println("conf.sc: Cold-Starting aerospike") |
| 158 | + a.opts.Aerospike.ColdStart.ClusterName = c.ClusterName |
| 159 | + a.opts.Aerospike.ColdStart.ParallelThreads = c.ParallelThreads |
| 160 | + err = a.opts.Aerospike.ColdStart.run(nil, "cold-start", os.Stdout) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + // wait for cluster to be stable |
| 165 | + log.Println("conf.sc: Waiting for cluster to be stable") |
| 166 | + a.opts.Aerospike.IsStable.ClusterName = c.ClusterName |
| 167 | + a.opts.Aerospike.IsStable.ParallelThreads = c.ParallelThreads |
| 168 | + a.opts.Aerospike.IsStable.Wait = true |
| 169 | + a.opts.Aerospike.IsStable.IgnoreMigrations = true |
| 170 | + a.opts.Aerospike.IsStable.Namespace = c.Namespace |
| 171 | + err = a.opts.Aerospike.IsStable.Execute(nil) |
| 172 | + if err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + // apply roster |
| 176 | + log.Println("conf.sc: Applying roster") |
| 177 | + a.opts.Roster.Apply.ClusterName = c.ClusterName |
| 178 | + a.opts.Roster.Apply.Namespace = c.Namespace |
| 179 | + a.opts.Roster.Apply.ParallelThreads = c.ParallelThreads |
| 180 | + a.opts.Roster.Apply.Quiet = true |
| 181 | + err = a.opts.Roster.Apply.Execute(nil) |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + log.Println("conf.sc: Done") |
| 186 | + return nil |
| 187 | +} |
0 commit comments