Skip to content

Commit

Permalink
Add --set-locality flag to set region and zone info to each node (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kota2and3kan authored May 6, 2024
1 parent cb53b05 commit 0a80235
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,111 @@ go install
### Create a cluster

You can create a CockroachDB local cluster as follows. By default, it creates 3 node cluster.

```shell
goki create
```

If you want to create 5 node cluster, you can specify the number of node using `--node (-n)` flag.

```shell
goki create -n 5
```

You can specify the version of CockroachDB usint `--crdb-version` flag.

```shell
goki create -n 5 --crdb-version v22.1.11
```

### Set region and zone information to each node

You can set region and zone information for each node by specifying the `--set-locality (-l)` flag. Mainly, this is for the testing of Table Localities.

```shell
goki create --set-locality
```

If you set the `--set-locality (-l)` flag, goki adds region and zone information as follows:

```sql
root@goki-1:26257/defaultdb> SELECT node_id, address, locality FROM crdb_internal.gossip_nodes;
node_id | address | locality
----------+--------------+------------------------------
1 | goki-1:26257 | region=region-1,zone=zone-1
2 | goki-2:26257 | region=region-1,zone=zone-2
3 | goki-3:26257 | region=region-1,zone=zone-3
4 | goki-4:26257 | region=region-2,zone=zone-1
5 | goki-5:26257 | region=region-2,zone=zone-2
6 | goki-6:26257 | region=region-2,zone=zone-3
7 | goki-7:26257 | region=region-3,zone=zone-1
8 | goki-8:26257 | region=region-3,zone=zone-2
9 | goki-9:26257 | region=region-3,zone=zone-3
(9 rows)
```

```console
+---[Region 1]-------------------------------------------+ +---[Region 2]-------------------------------------------+ +---[Region 3]-------------------------------------------+
| | | | | |
| +---[Zone 1]---+ +---[Zone 2]---+ +---[Zone 3]---+ | | +---[Zone 1]---+ +---[Zone 2]---+ +---[Zone 3]---+ | | +---[Zone 1]---+ +---[Zone 2]---+ +---[Zone 3]---+ |
| | | | | | | | | | | | | | | | | | | | | | | |
| | +--------+ | | +--------+ | | +--------+ | | | | +--------+ | | +--------+ | | +--------+ | | | | +--------+ | | +--------+ | | +--------+ | |
| | | goki-1 | | | | goki-2 | | | | goki-3 | | | | | | goki-4 | | | | goki-5 | | | | goki-6 | | | | | | goki-7 | | | | goki-8 | | | | goki-9 | | |
| | +--------+ | | +--------+ | | +--------+ | | | | +--------+ | | +--------+ | | +--------+ | | | | +--------+ | | +--------+ | | +--------+ | |
| | | | | | | | | | | | | | | | | | | | | | | |
| +--------------+ +--------------+ +--------------+ | | +--------------+ +--------------+ +--------------+ | | +--------------+ +--------------+ +--------------+ |
| | | | | |
+--------------------------------------------------------+ +--------------------------------------------------------+ +--------------------------------------------------------+
```

### Connect to the cluster using built-in SQL shell

After creating your CockroachDB local cluster, you can access to it using built-in SQL shell as follows. By default, it access to the first node `goki-1` as a `root` user.

```shell
goki sql
```

If you want to access other node, you can specify the node number using `--goki (-g)` flag.

```shell
goki sql -g 3
```

You can use default non-root user (User name is `goki`) using `--non-root` flag.

```shell
goki sql --non-root
```

If you create your own user, you can specify the user using `--user (-u)` flag and `--password (-p)` flag.

```shell
goki sql -u foo -p foopass
```

`goki sql` command uses the CockroachDB's built-in SQL shell. To exit buitl-in SQL shell, you can use `\q`, `quit`, `exit`, or `Ctrl-d`.

```shell
root@goki-1:26257/defaultdb> \q
```

### Delete the cluster

You can delete the CockroachDB local cluster as follows. By default, it deletes docker containers and docker network only. The docker volumes that include CockroachDB's data are not deleted.

```shell
goki delete
```

In this case, you can restart your CockroachDB local cluster using the existing data in the docker volume by `goki create` command.

If you want to all component includes docker volume, you can specify the `--volume (-v)` flag as follows.

```shell
goki delete -v
```

## License

Please refer to the [LICENSE](https://github.com/kota2and3kan/goki/blob/main/LICENSE) for the details on the license of the files in this repository.
35 changes: 33 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ import (
)

var (
gokiVolumeAlreadyExist bool // If Goki's docker volume already exist, re-use it and skip cluster initializing.
gokiVolumeAlreadyExist bool // If Goki's docker volume already exist, re-use it and skip cluster initializing.
gokiRegionId int = 0 // The ID of a region that a node will be deployed. Goki uses region-1, region-2, or region-3.
gokiZoneId int = 0 // The ID of a zone that a node will be deployed. Goki uses zone-1, zone-2, or zone-3.
)

// Flag value of create command.
var createCmdFlags struct {
node int // Number of node (container).
crdbVersion string // Version of CockroachDB that specified to tag of container image.
locality bool // Whether set --locality flag or not.
}

// createCmd represents the create command
Expand Down Expand Up @@ -77,7 +80,10 @@ var createCmd = &cobra.Command{
}

// Create CockroachDB Local Cluster.
fmt.Println("INFO: The number of cockroaches in the cluster is", createCmdFlags.node)
fmt.Println("INFO: The number of cockroaches in the cluster is", createCmdFlags.node, ".")
if createCmdFlags.locality {
fmt.Println("INFO: The --set-locality is true. Set region and zone information in each node.")
}
fmt.Println("INFO: *** Start Creating CockroachDB Local Cluster ***")

// Create Docker Network that each cockroach and client will join.
Expand Down Expand Up @@ -377,6 +383,11 @@ func createFirstGoki() error {
// Create first node.
fmt.Println("INFO: Creating First node start.")

if createCmdFlags.locality {
gokiRegionId = 1
gokiZoneId = 1
}

c := exec.Command("docker", "run", "-d",
"--name="+gokiResourceName+"-1",
"--hostname="+gokiResourceName+"-1",
Expand All @@ -390,6 +401,7 @@ func createFirstGoki() error {
"start",
"--certs-dir=certs/node-certs/"+gokiResourceName+"-1",
"--join="+gokiResourceName+"-1,"+gokiResourceName+"-2,"+gokiResourceName+"-3",
"--locality=region=region-"+strconv.Itoa(gokiRegionId)+",zone=zone-"+strconv.Itoa(gokiZoneId),
)

if output, err := c.CombinedOutput(); err != nil {
Expand Down Expand Up @@ -520,6 +532,23 @@ func createGokiCluster() error {
// Run the second and later node.
for i := 2; i <= createCmdFlags.node; i++ {

if createCmdFlags.locality {
switch i {
case 1, 2, 3:
gokiRegionId = 1
case 4, 5, 6:
gokiRegionId = 2
case 7, 8, 9:
gokiRegionId = 3
}

if i%3 == 0 {
gokiZoneId = 3
} else {
gokiZoneId = i % 3
}
}

c := exec.Command("docker", "run", "-d",
"--name="+gokiResourceName+"-"+strconv.Itoa(i),
"--hostname="+gokiResourceName+"-"+strconv.Itoa(i),
Expand All @@ -531,6 +560,7 @@ func createGokiCluster() error {
"start",
"--certs-dir=certs/node-certs/"+gokiResourceName+"-"+strconv.Itoa(i),
"--join="+gokiResourceName+"-1,"+gokiResourceName+"-2,"+gokiResourceName+"-3",
"--locality=region=region-"+strconv.Itoa(gokiRegionId)+",zone=zone-"+strconv.Itoa(gokiZoneId),
)

if output, err := c.CombinedOutput(); err != nil {
Expand Down Expand Up @@ -657,4 +687,5 @@ func init() {
// Flags of goki create.
createCmd.Flags().IntVarP(&createCmdFlags.node, "node", "n", 3, "The number of cockroaches.")
createCmd.Flags().StringVar(&createCmdFlags.crdbVersion, "crdb-version", crdbVersion, "Version of CockroachDB (Tag of container image).")
createCmd.Flags().BoolVarP(&createCmdFlags.locality, "set-locality", "l", false, "Set --locality flag (region and zone value) to all nodes.")
}

0 comments on commit 0a80235

Please sign in to comment.