Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Add KVS HTTP handlers #46

Merged
merged 2 commits into from
Mar 23, 2019
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Add KVS HTTP handlers #46

### Changed


Expand Down
2 changes: 1 addition & 1 deletion cmd/blast-kvs/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/urfave/cli"
)

func delete(c *cli.Context) error {
func execDelete(c *cli.Context) error {
grpcAddr := c.String("grpc-addr")

key := c.String("key")
Expand Down
2 changes: 1 addition & 1 deletion cmd/blast-kvs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/urfave/cli"
)

func get(c *cli.Context) error {
func execGet(c *cli.Context) error {
grpcAddr := c.String("grpc-addr")

key := c.String("key")
Expand Down
2 changes: 1 addition & 1 deletion cmd/blast-kvs/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/urfave/cli"
)

func join(c *cli.Context) error {
func execJoin(c *cli.Context) error {
grpcAddr := c.String("grpc-addr")

id := c.Args().Get(0)
Expand Down
2 changes: 1 addition & 1 deletion cmd/blast-kvs/leave.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/urfave/cli"
)

func leave(c *cli.Context) error {
func execLeave(c *cli.Context) error {
grpcAddr := c.String("grpc-addr")

id := c.Args().Get(0)
Expand Down
14 changes: 7 additions & 7 deletions cmd/blast-kvs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func main() {
Usage: "Compress a HTTP access log",
},
},
Action: start,
Action: execStart,
},
{
Name: "join",
Expand All @@ -136,7 +136,7 @@ func main() {
},
},
ArgsUsage: "[id] [addr]",
Action: join,
Action: execJoin,
},
{
Name: "leave",
Expand All @@ -149,7 +149,7 @@ func main() {
},
},
ArgsUsage: "[id]",
Action: leave,
Action: execLeave,
},
{
Name: "snapshot",
Expand All @@ -161,7 +161,7 @@ func main() {
Usage: "address to connect to",
},
},
Action: snapshot,
Action: execSnapshot,
},
{
Name: "get",
Expand All @@ -178,7 +178,7 @@ func main() {
Usage: "key",
},
},
Action: get,
Action: execGet,
},
{
Name: "put",
Expand All @@ -196,7 +196,7 @@ func main() {
},
},
ArgsUsage: "[value]",
Action: put,
Action: execPut,
},
{
Name: "delete",
Expand All @@ -213,7 +213,7 @@ func main() {
Usage: "key",
},
},
Action: delete,
Action: execDelete,
},
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/blast-kvs/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/mosuka/blast/kvs"
pbkvs "github.com/mosuka/blast/protobuf/kvs"
"github.com/urfave/cli"
)

func put(c *cli.Context) error {
func execPut(c *cli.Context) error {
grpcAddr := c.String("grpc-addr")

key := c.String("key")
if key == "" {
err := errors.New("key argument must be set")
return err
} else if key[:1] != "/" {
err := errors.New("key argument must start \"/\"")
return err
}

key, err := filepath.Abs(key)

value := c.Args().Get(0)
if value == "" {
err := errors.New("value argument must be set")
Expand Down
2 changes: 1 addition & 1 deletion cmd/blast-kvs/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/urfave/cli"
)

func snapshot(c *cli.Context) error {
func execSnapshot(c *cli.Context) error {
grpcAddr := c.String("grpc-addr")

client, err := kvs.NewGRPCClient(grpcAddr)
Expand Down
2 changes: 1 addition & 1 deletion cmd/blast-kvs/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/urfave/cli"
)

func start(c *cli.Context) error {
func execStart(c *cli.Context) error {
nodeId := c.String("node-id")
bindAddr := c.String("bind-addr")
grpcAddr := c.String("grpc-addr")
Expand Down
25 changes: 21 additions & 4 deletions kvs/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ package kvs

import (
"context"
"errors"
"log"
"math"

"github.com/golang/protobuf/ptypes/empty"
blasterrors "github.com/mosuka/blast/errors"
"github.com/mosuka/blast/protobuf/kvs"
"github.com/mosuka/blast/protobuf/raft"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type GRPCClient struct {
Expand Down Expand Up @@ -90,7 +94,9 @@ func (c *GRPCClient) Leave(req *raft.Node, opts ...grpc.CallOption) error {
func (c *GRPCClient) Snapshot(opts ...grpc.CallOption) error {
_, err := c.client.Snapshot(c.ctx, &empty.Empty{})
if err != nil {
return err
st, _ := status.FromError(err)

return errors.New(st.Message())
}

return nil
Expand All @@ -99,7 +105,14 @@ func (c *GRPCClient) Snapshot(opts ...grpc.CallOption) error {
func (c *GRPCClient) Get(req *kvs.KeyValuePair, opts ...grpc.CallOption) (*kvs.KeyValuePair, error) {
resp, err := c.client.Get(c.ctx, req, opts...)
if err != nil {
return nil, err
st, _ := status.FromError(err)

switch st.Code() {
case codes.NotFound:
return nil, blasterrors.ErrNotFound
default:
return nil, errors.New(st.Message())
}
}

return resp, nil
Expand All @@ -108,7 +121,9 @@ func (c *GRPCClient) Get(req *kvs.KeyValuePair, opts ...grpc.CallOption) (*kvs.K
func (c *GRPCClient) Put(req *kvs.KeyValuePair, opts ...grpc.CallOption) error {
_, err := c.client.Put(c.ctx, req, opts...)
if err != nil {
return err
st, _ := status.FromError(err)

return errors.New(st.Message())
}

return nil
Expand All @@ -117,7 +132,9 @@ func (c *GRPCClient) Put(req *kvs.KeyValuePair, opts ...grpc.CallOption) error {
func (c *GRPCClient) Delete(req *kvs.KeyValuePair, opts ...grpc.CallOption) error {
_, err := c.client.Delete(c.ctx, req, opts...)
if err != nil {
return err
st, _ := status.FromError(err)

return errors.New(st.Message())
}

return nil
Expand Down
Loading