Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to grpc-gateway #15

Merged
merged 1 commit into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ PROTOBUFS = $(shell find . -name '*.proto' -print0 | xargs -0 -n1 dirname | sort

TARGET_PACKAGES = $(shell find . -name 'main.go' -print0 | xargs -0 -n1 dirname | sort | uniq | grep -v /vendor/)

GRPC_GATEWAY_PATH = $(shell $(GO) list -m -f "{{.Dir}}" github.com/grpc-ecosystem/grpc-gateway)

ifeq ($(GOOS),)
GOOS = $(shell go version | awk -F ' ' '{print $$NF}' | awk -F '/' '{print $$1}')
endif
Expand All @@ -53,7 +55,8 @@ GO := GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) CGO_CFLAGS=$(CGO_
.PHONY: protoc
protoc:
@echo ">> generating proto3 code"
@for proto_dir in $(PROTOBUFS); do echo $$proto_dir; protoc --proto_path=. --proto_path=$$proto_dir --go_out=plugins=grpc:$(GOPATH)/src $$proto_dir/*.proto || exit 1; done
@for proto_dir in $(PROTOBUFS); do echo $$proto_dir; protoc --proto_path=. --proto_path=$$proto_dir --proto_path=${GRPC_GATEWAY_PATH} --proto_path=${GRPC_GATEWAY_PATH}/third_party/googleapis --go_out=plugins=grpc:$(GOPATH)/src $$proto_dir/*.proto || exit 1; done
@for proto_dir in $(PROTOBUFS); do echo $$proto_dir; protoc --proto_path=. --proto_path=$$proto_dir --proto_path=${GRPC_GATEWAY_PATH} --proto_path=${GRPC_GATEWAY_PATH}/third_party/googleapis --grpc-gateway_out=logtostderr=true,allow_delete_body=true:$(GOPATH)/src $$proto_dir/*.proto || exit 1; done

.PHONY: format
format:
Expand Down
90 changes: 41 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ Cete implements [Raft consensus algorithm](https://raft.github.io/) by [hashicor
Cete makes it easy bringing up a cluster of BadgerDB (a cete of badgers) .




## Features

- Easy deployment
Expand Down Expand Up @@ -55,7 +53,6 @@ If you want to build for other platform, set `GOOS`, `GOARCH` environment variab
$ make GOOS=darwin build
```


### Binaries

You can see the binary file when build successful like so:
Expand Down Expand Up @@ -83,15 +80,13 @@ $ make test
$ make GOOS=linux dist
```


#### macOS
### macOS

```bash
$ make GOOS=darwin dist
```



## Starting Cete node

Starting cete is easy as follows:
Expand All @@ -100,10 +95,16 @@ Starting cete is easy as follows:
$ ./bin/cete start --id=node1 --bind-addr=:7000 --grpc-addr=:9000 --http-addr=:8000 --data-dir=/tmp/cete/node1
```

You can get node info as follows:
You can get the node information with the following command:

```bash
$ ./bin/cete node --grpc-addr=:9000
$ ./bin/cete node --grpc-addr=:9000 | jq .
```

or the following URL:

```bash
$ curl -X GET http://localhost:8000/v1/node | jq .
```

The result of the above command is:
Expand All @@ -119,68 +120,53 @@ The result of the above command is:
}
```

### Setting a value by key via CLI
### Putting a key-value

Setting a value by key, execute the following command:
To put a key-value, execute the following command:

```bash
$ ./bin/cete set --grpc-addr=:9000 --key=key1 value1
```


### Getting a value by key via CLI

Getting a value by key, execute the following command:
or, you can use the RESTful API as follows:

```bash
$ ./bin/cete get --grpc-addr=:9000 --key=key1
```

You can see the result. The result of the above command is:

```text
value1
$ curl -X PUT 'http://127.0.0.1:8000/v1/data/1' --data-binary value1
$ curl -X PUT 'http://127.0.0.1:8000/v1/data/2' -H "Content-Type: image/jpeg" --data-binary @/path/to/photo.jpg
```

### Getting a key-value

### Deleting a value by key via CLI

Deleting a value by key, execute the following command:
To get a key-value, execute the following command:

```bash
$ ./bin/cete delete --grpc-addr=:9000 --key=key1
$ ./bin/cete get --grpc-addr=:9000 1
```


## Using HTTP REST API

Also you can do above commands via HTTP REST API that listened port 8000.


### Indexing a value by key via HTTP REST API

Indexing a value by key via HTTP is as following:
or, you can use the RESTful API as follows:

```bash
$ curl -s -X PUT 'http://127.0.0.1:8000/store/key1' -d value1
$ curl -X GET 'http://127.0.0.1:8000/v1/data/1'
```

You can see the result. The result of the above command is:

### Getting a value by key via HTTP REST API
```text
value1
```

Getting a value by key via HTTP is as following:
### Deleting a value by key via CLI

Deleting a value by key, execute the following command:

```bash
$ curl -s -X GET 'http://127.0.0.1:8000/store/key1'
$ ./bin/cete delete --grpc-addr=:9000 1
```


### Deleting a value by key via HTTP REST API

Deleting a value by key via HTTP is as following:
or, you can use the RESTful API as follows:

```bash
$ curl -X DELETE 'http://127.0.0.1:8000/store/key1'
$ curl -X DELETE 'http://127.0.0.1:8000/v1/data/1'
```


Expand All @@ -199,7 +185,13 @@ This instructs each new node to join an existing node, each node recognizes the
So you have a 3-node cluster. That way you can tolerate the failure of 1 node. You can check the cluster with the following command:

```bash
$ ./bin/cete cluster --grpc-addr=:9000
$ ./bin/cete cluster --grpc-addr=:9000 | jq .
```

or, you can use the RESTful API as follows:

```bash
$ curl -X GET 'http://127.0.0.1:8000/v1/cluster' | jq .
```

You can see the result in JSON format. The result of the above command is:
Expand Down Expand Up @@ -234,13 +226,13 @@ Recommend 3 or more odd number of nodes in the cluster. In failure scenarios, da
The following command indexes documents to any node in the cluster:

```bash
$ ./bin/cete set --grpc-addr=:9000 --key=key1 value1
$ ./bin/cete set --grpc-addr=:9000 1 value1
```

So, you can get the document from the node specified by the above command as follows:

```bash
$ ./bin/cete get --grpc-addr=:9000 --key=key1
$ ./bin/cete get --grpc-addr=:9000 1
```

You can see the result. The result of the above command is:
Expand All @@ -252,8 +244,8 @@ value1
You can also get the same document from other nodes in the cluster as follows:

```bash
$ ./bin/cete get --grpc-addr=:9001 --key=key1
$ ./bin/cete get --grpc-addr=:9002 --key=key1
$ ./bin/cete get --grpc-addr=:9001 1
$ ./bin/cete get --grpc-addr=:9002 1
```

You can see the result. The result of the above command is:
Expand Down Expand Up @@ -303,7 +295,7 @@ $ docker run --rm --name cete-node1 \
-p 8000:8000 \
-p 9000:9000 \
mosuka/cete:latest cete start \
--node-id=node1 \
--id=node1 \
--bind-addr=:7000 \
--grpc-addr=:9000 \
--http-addr=:8000 \
Expand Down
9 changes: 3 additions & 6 deletions cmd/cete/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,20 @@ func execCluster(c *cli.Context) error {
return err
}
defer func() {
err := client.Close()
if err != nil {
_, err = fmt.Fprintln(os.Stderr, err)
}
_ = client.Close()
}()

resp, err := client.Cluster()
if err != nil {
return err
}

clusterBytes, err := json.MarshalIndent(resp, "", " ")
clusterBytes, err := json.Marshal(resp)
if err != nil {
return err
}

_, _ = fmt.Fprintln(os.Stdout, fmt.Sprintf("%v\n", string(clusterBytes)))
_, _ = fmt.Fprintln(os.Stdout, fmt.Sprintf("%s", string(clusterBytes)))

return nil
}
9 changes: 2 additions & 7 deletions cmd/cete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package main

import (
"errors"
"fmt"
"os"

"github.com/mosuka/cete/kvs"
pbkvs "github.com/mosuka/cete/protobuf/kvs"
Expand All @@ -34,18 +32,15 @@ func execDelete(c *cli.Context) error {
}

req := &pbkvs.DeleteRequest{
Key: []byte(key),
Key: key,
}

client, err := kvs.NewGRPCClient(grpcAddr)
if err != nil {
return err
}
defer func() {
err := client.Close()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
_ = client.Close()
}()

err = client.Delete(req)
Expand Down
9 changes: 3 additions & 6 deletions cmd/cete/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ func execGet(c *cli.Context) error {
}

req := &pbkvs.GetRequest{
Key: []byte(key),
Key: key,
}

client, err := kvs.NewGRPCClient(grpcAddr)
if err != nil {
return err
}
defer func() {
err := client.Close()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
_ = client.Close()
}()

resp, err := client.Get(req)
Expand All @@ -58,7 +55,7 @@ func execGet(c *cli.Context) error {
return nil
}

_, _ = fmt.Fprintln(os.Stdout, fmt.Sprintf("%v", string(resp.Value)))
_, _ = fmt.Fprintln(os.Stdout, fmt.Sprintf("%s", string(resp.Value)))

return nil
}
12 changes: 2 additions & 10 deletions cmd/cete/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package main

import (
"errors"
"fmt"
"os"

"github.com/mosuka/cete/kvs"
pbkvs "github.com/mosuka/cete/protobuf/kvs"
Expand All @@ -44,10 +42,7 @@ func execJoin(c *cli.Context) error {
return err
}
defer func() {
err := targetClient.Close()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
_ = targetClient.Close()
}()

nodeResp, err := targetClient.Node()
Expand All @@ -67,10 +62,7 @@ func execJoin(c *cli.Context) error {
return err
}
defer func() {
err := client.Close()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
_ = client.Close()
}()

err = client.Join(req)
Expand Down
7 changes: 1 addition & 6 deletions cmd/cete/leave.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package main

import (
"errors"
"fmt"
"os"

"github.com/mosuka/cete/kvs"
pbkvs "github.com/mosuka/cete/protobuf/kvs"
Expand All @@ -42,10 +40,7 @@ func execLeave(c *cli.Context) error {
return err
}
defer func() {
err := client.Close()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
_ = client.Close()
}()

err = client.Leave(req)
Expand Down
5 changes: 1 addition & 4 deletions cmd/cete/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ func execMetrics(c *cli.Context) error {
return err
}
defer func() {
err := client.Close()
if err != nil {
_, err = fmt.Fprintln(os.Stderr, err)
}
_ = client.Close()
}()

resp, err := client.Metrics()
Expand Down
9 changes: 3 additions & 6 deletions cmd/cete/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,20 @@ func execNode(c *cli.Context) error {
return err
}
defer func() {
err := client.Close()
if err != nil {
_, err = fmt.Fprintln(os.Stderr, err)
}
_ = client.Close()
}()

resp, err := client.Node()
if err != nil {
return err
}

clusterBytes, err := json.MarshalIndent(resp, "", " ")
clusterBytes, err := json.Marshal(resp)
if err != nil {
return err
}

_, _ = fmt.Fprintln(os.Stdout, fmt.Sprintf("%v\n", string(clusterBytes)))
_, _ = fmt.Fprintln(os.Stdout, fmt.Sprintf("%s", string(clusterBytes)))

return nil
}
Loading