1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
package shell
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/cowsql/go-cowsql"
"github.com/cowsql/go-cowsql/client"
"github.com/cowsql/go-cowsql/driver"
)
// Shell can be used to implement interactive prompts for inspecting a cowsql
// database.
type Shell struct {
store client.NodeStore
dial client.DialFunc
db *sql.DB
format string
}
// New creates a new Shell connected to the given database.
func New(database string, store client.NodeStore, options ...Option) (*Shell, error) {
o := defaultOptions()
for _, option := range options {
option(o)
}
switch o.Format {
case formatTabular:
case formatJson:
default:
return nil, fmt.Errorf("unknown format %s", o.Format)
}
driver, err := driver.New(store, driver.WithDialFunc(o.Dial))
if err != nil {
return nil, err
}
sql.Register(o.DriverName, driver)
db, err := sql.Open(o.DriverName, database)
if err != nil {
return nil, err
}
shell := &Shell{
store: store,
dial: o.Dial,
db: db,
format: o.Format,
}
return shell, nil
}
// Process a single input line.
func (s *Shell) Process(ctx context.Context, line string) (string, error) {
switch line {
case ".cluster":
return s.processCluster(ctx, line)
case ".leader":
return s.processLeader(ctx, line)
case ".help":
return s.processHelp(), nil
}
if strings.HasPrefix(strings.ToLower(strings.TrimLeft(line, " ")), ".remove") {
return s.processRemove(ctx, line)
}
if strings.HasPrefix(strings.ToLower(strings.TrimLeft(line, " ")), ".describe") {
return s.processDescribe(ctx, line)
}
if strings.HasPrefix(strings.ToLower(strings.TrimLeft(line, " ")), ".weight") {
return s.processWeight(ctx, line)
}
if strings.HasPrefix(strings.ToLower(strings.TrimLeft(line, " ")), ".dump") {
return s.processDump(ctx, line)
}
if strings.HasPrefix(strings.ToLower(strings.TrimLeft(line, " ")), ".reconfigure") {
return s.processReconfigure(ctx, line)
}
return s.processQuery(ctx, line)
}
func (s *Shell) processHelp() string {
return `
Cowsql shell is a simple interactive prompt for inspecting a cowsql database.
Enter a SQL statement to execute it, or one of the following built-in commands:
.cluster Show the cluster membership
.leader Show the current leader
.remove <address> Remove a node from the cluster
.describe <address> Show the details of a node
.weight <address> <weight> Set the weight of a node
.dump <address> [<database>] Dump the database
.reconfigure <dir> <clusteryaml> Reconfigure the cluster
`[1:]
}
func (s *Shell) processCluster(ctx context.Context, line string) (string, error) {
cli, err := client.FindLeader(ctx, s.store, client.WithDialFunc(s.dial))
if err != nil {
return "", err
}
cluster, err := cli.Cluster(ctx)
if err != nil {
return "", err
}
result := ""
switch s.format {
case formatTabular:
for i, server := range cluster {
if i > 0 {
result += "\n"
}
result += fmt.Sprintf("%x|%s|%s", server.ID, server.Address, server.Role)
}
case formatJson:
data, err := json.Marshal(cluster)
if err != nil {
return "", err
}
var indented bytes.Buffer
json.Indent(&indented, data, "", "\t")
result = string(indented.Bytes())
}
return result, nil
}
func (s *Shell) processLeader(ctx context.Context, line string) (string, error) {
cli, err := client.FindLeader(ctx, s.store, client.WithDialFunc(s.dial))
if err != nil {
return "", err
}
leader, err := cli.Leader(ctx)
if err != nil {
return "", err
}
if leader == nil {
return "", nil
}
return leader.Address, nil
}
func (s *Shell) processRemove(ctx context.Context, line string) (string, error) {
parts := strings.Split(line, " ")
if len(parts) != 2 {
return "", fmt.Errorf("bad command format, should be: .remove <address>")
}
address := parts[1]
cli, err := client.FindLeader(ctx, s.store, client.WithDialFunc(s.dial))
if err != nil {
return "", err
}
cluster, err := cli.Cluster(ctx)
if err != nil {
return "", err
}
for _, node := range cluster {
if node.Address != address {
continue
}
if err := cli.Remove(ctx, node.ID); err != nil {
return "", fmt.Errorf("remove node %q: %w", address, err)
}
return "", nil
}
return "", fmt.Errorf("no node has address %q", address)
}
func (s *Shell) processDescribe(ctx context.Context, line string) (string, error) {
parts := strings.Split(line, " ")
if len(parts) != 2 {
return "", fmt.Errorf("bad command format, should be: .describe <address>")
}
address := parts[1]
cli, err := client.New(ctx, address, client.WithDialFunc(s.dial))
if err != nil {
return "", err
}
metadata, err := cli.Describe(ctx)
if err != nil {
return "", err
}
result := ""
switch s.format {
case formatTabular:
result += fmt.Sprintf("%s|%d|%d", address, metadata.FailureDomain, metadata.Weight)
case formatJson:
data, err := json.Marshal(metadata)
if err != nil {
return "", err
}
var indented bytes.Buffer
json.Indent(&indented, data, "", "\t")
result = string(indented.Bytes())
}
return result, nil
}
func (s *Shell) processDump(ctx context.Context, line string) (string, error) {
parts := strings.Split(line, " ")
if len(parts) < 2 || len(parts) > 3 {
return "NOK", fmt.Errorf("bad command format, should be: .dump <address> [<database>]")
}
address := parts[1]
cli, err := client.New(ctx, address, client.WithDialFunc(s.dial))
if err != nil {
return "NOK", fmt.Errorf("dial failed")
}
database := "db.bin"
if len(parts) == 3 {
database = parts[2]
}
files, err := cli.Dump(ctx, database)
if err != nil {
return "NOK", fmt.Errorf("dump failed")
}
dir, err := os.Getwd()
if err != nil {
return "NOK", fmt.Errorf("os.Getwd() failed")
}
for _, file := range files {
path := filepath.Join(dir, file.Name)
err := ioutil.WriteFile(path, file.Data, 0600)
if err != nil {
return "NOK", fmt.Errorf("WriteFile failed on path %s", path)
}
}
return "OK", nil
}
func (s *Shell) processReconfigure(ctx context.Context, line string) (string, error) {
parts := strings.Split(line, " ")
if len(parts) != 3 {
return "NOK", fmt.Errorf("bad command format, should be: .reconfigure <dir> <clusteryaml>\n" +
"Args:\n" +
"\tdir - Directory of node with up to date data\n" +
"\tclusteryaml - Path to a .yaml file containing the desired cluster configuration\n\n" +
"Help:\n" +
"\tUse this command when trying to preserve the data from your cluster while changing the\n" +
"\tconfiguration of the cluster because e.g. your cluster is broken due to unreachablee nodes.\n" +
"\t0. BACKUP ALL YOUR NODE DATA DIRECTORIES BEFORE PROCEEDING!\n" +
"\t1. Stop all cowsql nodes.\n" +
"\t2. Identify the dir of the node with the most up to date raft term and log, this will be the <dir> argument.\n" +
"\t3. Create a .yaml file with the same format as cluster.yaml (or use/adapt an existing cluster.yaml) with the\n " +
"\t desired cluster configuration. This will be the <clusteryaml> argument.\n" +
"\t Don't forget to make sure the ID's in the file line up with the ID's in the info.yaml files.\n" +
"\t4. Run the .reconfigure <dir> <clusteryaml> command, it should return \"OK\".\n" +
"\t5. Copy the snapshot-xxx-xxx-xxx, snapshot-xxx-xxx-xxx.meta, segment files (00000xxxxx-000000xxxxx), desired cluster.yaml\n" +
"\t from <dir> over to the directories of the other nodes identified in <clusteryaml>, deleting any leftover snapshot-xxx-xxx-xxx, snapshot-xxx-xxx-xxx.meta,\n" +
"\t segment (00000xxxxx-000000xxxxx, open-xxx) and metadata{1,2} files that it contains.\n" +
"\t Make sure an info.yaml is also present that is in line with cluster.yaml.\n" +
"\t6. Start all the cowsql nodes.\n" +
"\t7. If, for some reason, this fails or gives undesired results, try again with data from another node (you should still have this from step 0).\n")
}
dir := parts[1]
clusteryamlpath := parts[2]
store, err := client.NewYamlNodeStore(clusteryamlpath)
if err != nil {
return "NOK", fmt.Errorf("failed to create YamlNodeStore from file at %s :%v", clusteryamlpath, err)
}
servers, err := store.Get(ctx)
if err != nil {
return "NOK", fmt.Errorf("failed to retrieve NodeInfo list :%v", err)
}
err = cowsql.ReconfigureMembershipExt(dir, servers)
if err != nil {
return "NOK", fmt.Errorf("failed to reconfigure membership :%v", err)
}
return "OK", nil
}
func (s *Shell) processWeight(ctx context.Context, line string) (string, error) {
parts := strings.Split(line, " ")
if len(parts) != 3 {
return "", fmt.Errorf("bad command format, should be: .weight <address> <n>")
}
address := parts[1]
weight, err := strconv.Atoi(parts[2])
if err != nil || weight < 0 {
return "", fmt.Errorf("bad weight %q", parts[2])
}
cli, err := client.New(ctx, address, client.WithDialFunc(s.dial))
if err != nil {
return "", err
}
if err := cli.Weight(ctx, uint64(weight)); err != nil {
return "", err
}
return "", nil
}
func (s *Shell) processQuery(ctx context.Context, line string) (string, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return "", fmt.Errorf("begin transaction: %w", err)
}
rows, err := tx.Query(line)
if err != nil {
err = fmt.Errorf("query: %w", err)
if rbErr := tx.Rollback(); rbErr != nil {
return "", fmt.Errorf("unable to rollback: %v", err)
}
return "", err
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
err = fmt.Errorf("columns: %w", err)
if rbErr := tx.Rollback(); rbErr != nil {
return "", fmt.Errorf("unable to rollback: %v", err)
}
return "", err
}
n := len(columns)
var sb strings.Builder
for rows.Next() {
row := make([]interface{}, n)
rowPointers := make([]interface{}, n)
for i := range row {
rowPointers[i] = &row[i]
}
if err := rows.Scan(rowPointers...); err != nil {
err = fmt.Errorf("scan: %w", err)
if rbErr := tx.Rollback(); rbErr != nil {
return "", fmt.Errorf("unable to rollback: %v", err)
}
return "", err
}
for i, column := range row {
if i == 0 {
fmt.Fprintf(&sb, "%v", column)
} else {
fmt.Fprintf(&sb, "|%v", column)
}
}
sb.WriteByte('\n')
}
if err := rows.Err(); err != nil {
err = fmt.Errorf("rows: %w", err)
if rbErr := tx.Rollback(); rbErr != nil {
return "", fmt.Errorf("unable to rollback: %v", err)
}
return "", err
}
if err := tx.Commit(); err != nil {
return "", fmt.Errorf("commit: %w", err)
}
return strings.TrimRight(sb.String(), "\n"), nil
}
func (s *Shell) processExec(ctx context.Context, line string) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
if _, err := tx.Exec(line); err != nil {
err = fmt.Errorf("exec: %w", err)
if rbErr := tx.Rollback(); rbErr != nil {
return fmt.Errorf("unable to rollback: %v", err)
}
return err
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit: %w", err)
}
return nil
}
|