这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
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
32 changes: 32 additions & 0 deletions .github/workflows/PR.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on:
pull_request:
branches:
- master

jobs:
build:
name: build
runs-on: ubuntu-latest
container:
image: golang:1.14-buster
volumes:
- "/home/runner/work/$GITHUB_REPOSITORY:/go/src/github.com/$GITHUB_REPOSITORY"
steps:
- uses: actions/checkout@v1

- name: restore from cache
uses: actions/cache@v1
with:
path: /go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

- name: download dependencies if not cached
run: |
if [ ! -d "/go/pkg/mod" ]; then
go mod tidy
fi

- name: test
run: go test -race -covermode=atomic -coverprofile coverage.out -v $(go list ./...)
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- adds scan endpoint #35 @vniche
- Fix bug in getting leader ID #34 @mosuka

## [v0.3.1] 2020-04-01
Expand Down
272 changes: 197 additions & 75 deletions protobuf/kvs.pb.go

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions protobuf/kvs.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions protobuf/kvs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,20 @@ service KVS {
get: "/v1/data/{key=**}"
};
}

rpc Scan (ScanRequest) returns (ScanResponse) {
option (google.api.http) = {
get: "/v1/data/{prefix=**}"
};
}

rpc Set (SetRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
put: "/v1/data/{key=**}"
body: "*"
};
}

rpc Delete (DeleteRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/v1/data/{key=**}"
Expand Down Expand Up @@ -125,6 +133,14 @@ message GetResponse {
bytes value = 1;
}

message ScanRequest {
string prefix = 1;
}

message ScanResponse {
repeated bytes values = 1;
}

message SetRequest {
string key = 1;
bytes value = 2;
Expand Down
17 changes: 17 additions & 0 deletions server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,23 @@ func (s *GRPCService) Get(ctx context.Context, req *protobuf.GetRequest) (*proto
return resp, nil
}

func (s *GRPCService) Scan(ctx context.Context, req *protobuf.ScanRequest) (*protobuf.ScanResponse, error) {
resp := &protobuf.ScanResponse{}

var err error

resp, err = s.raftServer.Scan(req)
if err != nil {
switch err {
default:
s.logger.Debug("failed to scan data", zap.String("prefix", req.Prefix), zap.String("err", err.Error()))
return resp, status.Error(codes.Internal, err.Error())
}
}

return resp, nil
}

func (s *GRPCService) Set(ctx context.Context, req *protobuf.SetRequest) (*empty.Empty, error) {
resp := &empty.Empty{}

Expand Down
10 changes: 10 additions & 0 deletions server/raft_fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ func (f *RaftFSM) Get(key string) ([]byte, error) {
return value, nil
}

func (f *RaftFSM) Scan(prefix string) ([][]byte, error) {
values, err := f.kvs.Scan(prefix)
if err != nil {
f.logger.Error("failed to scan values", zap.String("prefix", prefix), zap.Error(err))
return nil, err
}

return values, nil
}

func (f *RaftFSM) applySet(key string, value []byte) interface{} {
err := f.kvs.Set(key, value)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions server/raft_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,20 @@ func (s *RaftServer) Get(req *protobuf.GetRequest) (*protobuf.GetResponse, error
return resp, nil
}

func (s *RaftServer) Scan(req *protobuf.ScanRequest) (*protobuf.ScanResponse, error) {
values, err := s.fsm.Scan(req.Prefix)
if err != nil {
s.logger.Error("failed to scan", zap.Any("prefix", req.Prefix), zap.Error(err))
return nil, err
}

resp := &protobuf.ScanResponse{
Values: values,
}

return resp, nil
}

func (s *RaftServer) Set(req *protobuf.SetRequest) error {
kvpAny := &any.Any{}
if err := marshaler.UnmarshalAny(req, kvpAny); err != nil {
Expand Down
28 changes: 28 additions & 0 deletions storage/kvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ func (k *KVS) Get(key string) ([]byte, error) {
return value, nil
}

func (k *KVS) Scan(prefix string) ([][]byte, error) {
start := time.Now()

var value [][]byte
if err := k.db.View(func(txn *badger.Txn) error {
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
prefixBytes := []byte(prefix)
for it.Seek(prefixBytes); it.ValidForPrefix(prefixBytes); it.Next() {
item := it.Item()
err := item.Value(func(val []byte) error {
value = append(value, append([]byte{}, val...))
return nil
})
if err != nil {
return err
}
}
return nil
}); err != nil {
k.logger.Error("failed to scan value", zap.String("prefix", prefix), zap.Error(err))
return nil, err
}

k.logger.Debug("scan", zap.String("prefix", prefix), zap.Float64("time", float64(time.Since(start))/float64(time.Second)))
return value, nil
}

func (k *KVS) Set(key string, value []byte) error {
start := time.Now()

Expand Down