Skip to content

Commit

Permalink
Refactoring protobuf (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuka authored Mar 31, 2020
1 parent 1042058 commit 4603138
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 240 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CGO_ENABLED ?= 0
CGO_CFLAGS ?=
CGO_LDFLAGS ?=
BUILD_TAGS ?=
VERSION ?= $(shell git tag -l --sort=-v:refname | head -1)
VERSION ?=
BIN_EXT ?=
DOCKER_REPOSITORY ?= mosuka

Expand Down
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ The result of the above command is:
```json
{
"node": {
"bind_addr": ":7000",
"raft_address": ":7000",
"metadata": {
"grpc_addr": ":9000",
"http_addr": ":8000"
"grpc_address": ":9000",
"http_address": ":8000"
},
"state": "Leader"
}
Expand Down Expand Up @@ -231,26 +231,26 @@ You can see the result in JSON format. The result of the above command is:
"cluster": {
"nodes": {
"node1": {
"bind_addr": ":7000",
"raft_address": ":7000",
"metadata": {
"grpc_addr": ":9000",
"http_addr": ":8000"
"grpc_address": ":9000",
"http_address": ":8000"
},
"state": "Leader"
},
"node2": {
"bind_addr": ":7001",
"raft_address": ":7001",
"metadata": {
"grpc_addr": ":9001",
"http_addr": ":8001"
"grpc_address": ":9001",
"http_address": ":8001"
},
"state": "Follower"
},
"node3": {
"bind_addr": ":7002",
"raft_address": ":7002",
"metadata": {
"grpc_addr": ":9002",
"http_addr": ":8002"
"grpc_address": ":9002",
"http_address": ":8002"
},
"state": "Follower"
}
Expand All @@ -273,10 +273,10 @@ or, you can use the RESTful API as follows:
```bash
$ curl -X PUT 'http://127.0.0.1:8000/v1/cluster/node2' --data-binary '
{
"bind_addr": ":7001",
"raft_address": ":7001",
"metadata": {
"grpc_addr": ":9001",
"http_addr": ":8001"
"grpc_address": ":9001",
"http_address": ":8001"
}
}
'
Expand All @@ -297,13 +297,13 @@ $ curl -X DELETE 'http://127.0.0.1:8000/v1/cluster/node2'
The following command indexes documents to any node in the cluster:

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

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

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

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

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

You can see the result. The result of the above command is:
Expand Down Expand Up @@ -400,15 +400,15 @@ writing new private key to 'key.pem'
Starting a node with HTTPS enabled, node-to-node encryption, and with the above configuration file. It is assumed the HTTPS X.509 certificate and key are at the paths server.crt and key.pem respectively.

```bash
$ ./bin/cete start --id=node1 --bind-addr=:7000 --grpc-addr=:9000 --http-addr=:8000 --data-dir=/tmp/cete/node1 --peer-grpc-addr=:9000 --cert-file=./etc/cert.pem --key-file=./etc/key.pem --cert-hostname=localhost
$ ./bin/cete start --id=node2 --bind-addr=:7001 --grpc-addr=:9001 --http-addr=:8001 --data-dir=/tmp/cete/node2 --peer-grpc-addr=:9000 --cert-file=./etc/cert.pem --key-file=./etc/key.pem --cert-hostname=localhost
$ ./bin/cete start --id=node3 --bind-addr=:7002 --grpc-addr=:9002 --http-addr=:8002 --data-dir=/tmp/cete/node3 --peer-grpc-addr=:9000 --cert-file=./etc/cert.pem --key-file=./etc/key.pem --cert-hostname=localhost
$ ./bin/cete start --id=node1 --raft-address=:7000 --grpc-address=:9000 --http-address=:8000 --data-directory=/tmp/cete/node1 --peer-grpc-address=:9000 --certificate-file=./etc/cert.pem --key-file=./etc/key.pem --common-name=localhost
$ ./bin/cete start --id=node2 --raft-address=:7001 --grpc-address=:9001 --http-address=:8001 --data-directory=/tmp/cete/node2 --peer-grpc-address=:9000 --certificate-file=./etc/cert.pem --key-file=./etc/key.pem --common-name=localhost
$ ./bin/cete start --id=node3 --raft-address=:7002 --grpc-address=:9002 --http-address=:8002 --data-directory=/tmp/cete/node3 --peer-grpc-address=:9000 --certificate-file=./etc/cert.pem --key-file=./etc/key.pem --common-name=localhost
```

You can access the cluster by adding a flag, such as the following command:

```bash
$ ./bin/cete cluster --grpc-addr=:9000 --cert-file=./cert.pem --cert-hostname=localhost | jq .
$ ./bin/cete cluster --grpc-address=:9000 --certificate-file=./cert.pem --common-name=localhost | jq .
```

or
Expand Down
16 changes: 8 additions & 8 deletions client/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ type GRPCClient struct {
logger *log.Logger
}

func NewGRPCClient(address string) (*GRPCClient, error) {
return NewGRPCClientWithContext(address, context.Background())
func NewGRPCClient(grpc_address string) (*GRPCClient, error) {
return NewGRPCClientWithContext(grpc_address, context.Background())
}

func NewGRPCClientWithContext(address string, baseCtx context.Context) (*GRPCClient, error) {
return NewGRPCClientWithContextTLS(address, baseCtx, "", "")
func NewGRPCClientWithContext(grpc_address string, baseCtx context.Context) (*GRPCClient, error) {
return NewGRPCClientWithContextTLS(grpc_address, baseCtx, "", "")
}

func NewGRPCClientWithContextTLS(address string, baseCtx context.Context, certFile string, certHostname string) (*GRPCClient, error) {
func NewGRPCClientWithContextTLS(grpcAddress string, baseCtx context.Context, certificateFile string, commonName string) (*GRPCClient, error) {
dialOpts := []grpc.DialOption{
grpc.WithDefaultCallOptions(
grpc.MaxCallSendMsgSize(math.MaxInt64),
Expand All @@ -50,17 +50,17 @@ func NewGRPCClientWithContextTLS(address string, baseCtx context.Context, certFi

ctx, cancel := context.WithCancel(baseCtx)

if certFile == "" {
if certificateFile == "" {
dialOpts = append(dialOpts, grpc.WithInsecure())
} else {
creds, err := credentials.NewClientTLSFromFile(certFile, certHostname)
creds, err := credentials.NewClientTLSFromFile(certificateFile, commonName)
if err != nil {
return nil, err
}
dialOpts = append(dialOpts, grpc.WithTransportCredentials(creds))
}

conn, err := grpc.DialContext(ctx, address, dialOpts...)
conn, err := grpc.DialContext(ctx, grpcAddress, dialOpts...)
if err != nil {
cancel()
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ var (
joinRequest := &protobuf.JoinRequest{
Id: id,
Node: &protobuf.Node{
BindAddr: raftAddress,
RaftAddress: raftAddress,
Metadata: &protobuf.Metadata{
GrpcAddr: grpcAddress,
HttpAddr: httpAddress,
GrpcAddress: grpcAddress,
HttpAddress: httpAddress,
},
},
}
Expand Down
24 changes: 12 additions & 12 deletions marshaler/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func TestMarshalAny(t *testing.T) {

// test kvs.Node
node := &protobuf.Node{
BindAddr: ":7000",
State: "Leader",
RaftAddress: ":7000",
State: "Leader",
Metadata: &protobuf.Metadata{
GrpcAddr: ":9000",
HttpAddr: ":8000",
GrpcAddress: ":9000",
HttpAddress: ":8000",
},
}

Expand All @@ -52,7 +52,7 @@ func TestMarshalAny(t *testing.T) {
t.Errorf("expected content to see %s, saw %s", expectedType, actualType)
}

expectedValue = []byte(`{"bind_addr":":7000","metadata":{"grpc_addr":":9000","http_addr":":8000"},"state":"Leader"}`)
expectedValue = []byte(`{"raft_address":":7000","metadata":{"grpc_address":":9000","http_address":":8000"},"state":"Leader"}`)
actualValue = nodeAny.Value
if !bytes.Equal(expectedValue, actualValue) {
t.Errorf("expected content to see %v, saw %v", expectedValue, actualValue)
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestUnmarshalAny(t *testing.T) {
// raft.Node
dataAny = &any.Any{
TypeUrl: "protobuf.Node",
Value: []byte(`{"bind_addr":":7000","metadata":{"grpc_addr":":9000","http_addr":":8000"},"state":"Leader"}`),
Value: []byte(`{"raft_address":":7000","metadata":{"grpc_address":":9000","http_address":":8000"},"state":"Leader"}`),
}

data, err = MarshalAny(dataAny)
Expand All @@ -94,14 +94,14 @@ func TestUnmarshalAny(t *testing.T) {
}
node := data.(*protobuf.Node)

if node.BindAddr != ":7000" {
t.Errorf("expected content to see %v, saw %v", ":6060", node.BindAddr)
if node.RaftAddress != ":7000" {
t.Errorf("expected content to see %v, saw %v", ":7000", node.RaftAddress)
}
if node.Metadata.GrpcAddr != ":9000" {
t.Errorf("expected content to see %v, saw %v", ":5050", node.BindAddr)
if node.Metadata.GrpcAddress != ":9000" {
t.Errorf("expected content to see %v, saw %v", ":9000", node.Metadata.GrpcAddress)
}
if node.Metadata.HttpAddr != ":8000" {
t.Errorf("expected content to see %v, saw %v", ":5050", node.BindAddr)
if node.Metadata.HttpAddress != ":8000" {
t.Errorf("expected content to see %v, saw %v", ":8000", node.Metadata.HttpAddress)
}
if node.State != "Leader" {
t.Errorf("expected content to see %v, saw %v", "Leader", node.State)
Expand Down
Loading

0 comments on commit 4603138

Please sign in to comment.