Skip to content

Commit 9d7aae6

Browse files
Refactor internal shell package (#9)
2 parents 3d2395f + d6a5cf5 commit 9d7aae6

2 files changed

Lines changed: 34 additions & 43 deletions

File tree

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
.sqlite
22
cmd/cowsql/cowsql
33
cmd/cowsql-demo/cowsql-demo
4-
cowsql
5-
cowsql-demo
4+
cmd/cowsql-benchmark/cowsql-benchmark
5+
./cowsql
6+
./cowsql-demo
7+
./cowsql-benchmark
68
profile.coverprofile
79
overalls.coverprofile

internal/shell/shell.go

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ func (s *Shell) processCluster(ctx context.Context, line string) (string, error)
129129
}
130130
var indented bytes.Buffer
131131
json.Indent(&indented, data, "", "\t")
132-
result = string(indented.Bytes())
132+
result = indented.String()
133133
}
134134

135135
return result, nil
136136
}
137137

138-
func (s *Shell) processLeader(ctx context.Context, line string) (string, error) {
138+
func (s *Shell) processLeader(ctx context.Context, _ string) (string, error) {
139139
cli, err := client.FindLeader(ctx, s.store, client.WithDialFunc(s.dial))
140140
if err != nil {
141141
return "", err
@@ -203,7 +203,7 @@ func (s *Shell) processDescribe(ctx context.Context, line string) (string, error
203203
}
204204
var indented bytes.Buffer
205205
json.Indent(&indented, data, "", "\t")
206-
result = string(indented.Bytes())
206+
result = indented.String()
207207
}
208208

209209
return result, nil
@@ -248,26 +248,30 @@ func (s *Shell) processDump(ctx context.Context, line string) (string, error) {
248248
func (s *Shell) processReconfigure(ctx context.Context, line string) (string, error) {
249249
parts := strings.Split(line, " ")
250250
if len(parts) != 3 {
251-
return "NOK", fmt.Errorf("bad command format, should be: .reconfigure <dir> <clusteryaml>\n" +
252-
"Args:\n" +
253-
"\tdir - Directory of node with up to date data\n" +
254-
"\tclusteryaml - Path to a .yaml file containing the desired cluster configuration\n\n" +
255-
"Help:\n" +
256-
"\tUse this command when trying to preserve the data from your cluster while changing the\n" +
257-
"\tconfiguration of the cluster because e.g. your cluster is broken due to unreachablee nodes.\n" +
258-
"\t0. BACKUP ALL YOUR NODE DATA DIRECTORIES BEFORE PROCEEDING!\n" +
259-
"\t1. Stop all cowsql nodes.\n" +
260-
"\t2. Identify the dir of the node with the most up to date raft term and log, this will be the <dir> argument.\n" +
261-
"\t3. Create a .yaml file with the same format as cluster.yaml (or use/adapt an existing cluster.yaml) with the\n " +
262-
"\t desired cluster configuration. This will be the <clusteryaml> argument.\n" +
263-
"\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" +
264-
"\t4. Run the .reconfigure <dir> <clusteryaml> command, it should return \"OK\".\n" +
265-
"\t5. Copy the snapshot-xxx-xxx-xxx, snapshot-xxx-xxx-xxx.meta, segment files (00000xxxxx-000000xxxxx), desired cluster.yaml\n" +
266-
"\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" +
267-
"\t segment (00000xxxxx-000000xxxxx, open-xxx) and metadata{1,2} files that it contains.\n" +
268-
"\t Make sure an info.yaml is also present that is in line with cluster.yaml.\n" +
269-
"\t6. Start all the cowsql nodes.\n" +
270-
"\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")
251+
msg := `bad command format, should be: .reconfigure <dir> <clusteryaml>
252+
Args:
253+
dir - Directory of node with up to date data\n" +
254+
clusteryaml - Path to a .yaml file containing the desired cluster configuration
255+
256+
257+
Help:
258+
Use this command when trying to preserve the data from your cluster while changing the
259+
configuration of the cluster because e.g. your cluster is broken due to unreachablee nodes.
260+
0. BACKUP ALL YOUR NODE DATA DIRECTORIES BEFORE PROCEEDING!
261+
1. Stop all cowsql nodes.
262+
2. Identify the dir of the node with the most up to date raft term and log, this will be the <dir> argument.
263+
3. Create a .yaml file with the same format as cluster.yaml (or use/adapt an existing cluster.yaml) with the
264+
desired cluster configuration. This will be the <clusteryaml> argument.
265+
Don't forget to make sure the ID's in the file line up with the ID's in the info.yaml files.
266+
4. Run the .reconfigure <dir> <clusteryaml> command, it should return "OK".
267+
5. Copy the snapshot-xxx-xxx-xxx, snapshot-xxx-xxx-xxx.meta, segment files (00000xxxxx-000000xxxxx), desired cluster.yaml
268+
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,
269+
segment (00000xxxxx-000000xxxxx, open-xxx) and metadata{1,2} files that it contains.
270+
Make sure an info.yaml is also present that is in line with cluster.yaml.
271+
6. Start all the cowsql nodes.
272+
7. 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).
273+
`
274+
return "NOK", shellError(msg)
271275
}
272276
dir := parts[1]
273277
clusteryamlpath := parts[2]
@@ -379,23 +383,8 @@ func (s *Shell) processQuery(ctx context.Context, line string) (string, error) {
379383
return strings.TrimRight(sb.String(), "\n"), nil
380384
}
381385

382-
func (s *Shell) processExec(ctx context.Context, line string) error {
383-
tx, err := s.db.BeginTx(ctx, nil)
384-
if err != nil {
385-
return err
386-
}
387-
388-
if _, err := tx.Exec(line); err != nil {
389-
err = fmt.Errorf("exec: %w", err)
390-
if rbErr := tx.Rollback(); rbErr != nil {
391-
return fmt.Errorf("unable to rollback: %v", err)
392-
}
393-
return err
394-
}
395-
396-
if err := tx.Commit(); err != nil {
397-
return fmt.Errorf("commit: %w", err)
398-
}
386+
type shellError string
399387

400-
return nil
388+
func (s shellError) Error() string {
389+
return string(s)
401390
}

0 commit comments

Comments
 (0)