Skip to content

Commit

Permalink
feat(store): describe block
Browse files Browse the repository at this point in the history
Signed-off-by: James Yin <[email protected]>
  • Loading branch information
ifplusor committed Jun 1, 2023
1 parent 44bbb30 commit 54617b3
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 346 deletions.
20 changes: 11 additions & 9 deletions internal/store/segment/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ func (s *segmentServer) RemoveBlock(ctx context.Context, req *segpb.RemoveBlockR
return &emptypb.Empty{}, nil
}

func (s *segmentServer) GetBlockInfo(
_ context.Context, _ *segpb.GetBlockInfoRequest,
) (*segpb.GetBlockInfoResponse, error) {
// TODO(james.yin): implements GetBlockInfo()
// if err := s.srv.GetBlockInfo(ctx, 0); err != nil {
// return nil, err
// }

return &segpb.GetBlockInfoResponse{}, nil
func (s *segmentServer) DescribeBlock(
ctx context.Context, req *segpb.DescribeBlockRequest,
) (*segpb.DescribeBlockResponse, error) {
blockID := vanus.NewIDFromUint64(req.Id)

info, err := s.srv.DescribeBlock(ctx, blockID)
if err != nil {
return nil, err
}

return &segpb.DescribeBlockResponse{Info: info}, nil
}

func (s *segmentServer) ActivateSegment(
Expand Down
23 changes: 18 additions & 5 deletions internal/store/segment/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
// first-party libraries.
"github.com/vanus-labs/vanus/pkg/errors"
cepb "github.com/vanus-labs/vanus/proto/pkg/cloudevents"
metapb "github.com/vanus-labs/vanus/proto/pkg/meta"
segpb "github.com/vanus-labs/vanus/proto/pkg/segment"

// this project.
Expand Down Expand Up @@ -117,12 +118,24 @@ func TestSegmentServer(t *testing.T) {
So(err, ShouldEqual, errors.ErrInvalidRequest)
})

Convey("GetBlockInfo()", func() {
// FIXME: not implement.
req := &segpb.GetBlockInfoRequest{}
resp, err := ss.GetBlockInfo(context.Background(), req)
Convey("DescribeBlock()", func() {
srv.EXPECT().DescribeBlock(Any(), Not(vanus.EmptyID())).DoAndReturn(func(
_ context.Context, id vanus.ID,
) (*metapb.SegmentHealthInfo, error) {
return &metapb.SegmentHealthInfo{Id: id.Uint64()}, nil
})
srv.EXPECT().DescribeBlock(Any(), Eq(vanus.EmptyID())).Return(nil, errors.ErrInvalidRequest)

req := &segpb.DescribeBlockRequest{
Id: vanus.NewTestID().Uint64(),
}
resp, err := ss.DescribeBlock(context.Background(), req)
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.Info.Id, ShouldEqual, req.Id)

req = &segpb.DescribeBlockRequest{}
_, err = ss.DescribeBlock(context.Background(), req)
So(err, ShouldEqual, errors.ErrInvalidRequest)
})

Convey("ActivateSegment()", func() {
Expand Down
4 changes: 2 additions & 2 deletions internal/store/segment/mock_replica.go

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

16 changes: 16 additions & 0 deletions internal/store/segment/mock_server.go

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

27 changes: 16 additions & 11 deletions internal/store/segment/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type Server interface {

CreateBlock(ctx context.Context, id vanus.ID, size int64) error
RemoveBlock(ctx context.Context, id vanus.ID) error
// GetBlockInfo(ctx context.Context, id vanus.ID) error
DescribeBlock(ctx context.Context, id vanus.ID) (*metapb.SegmentHealthInfo, error)

ActivateSegment(ctx context.Context, logID vanus.ID, segID vanus.ID, replicas map[vanus.ID]string) error
InactivateSegment(ctx context.Context) error
Expand Down Expand Up @@ -446,20 +446,25 @@ func (s *server) RemoveBlock(ctx context.Context, blockID vanus.ID) error {
}

// FIXME(james.yin): more info.
log.Info(ctx).
Stringer("block_id", b.ID()).
Msg("The block has been deleted.")
log.Info(ctx).Stringer("block_id", b.ID()).Msg("The block has been deleted.")

return nil
}

// TODO(james.yin): implements GetBlockInfo.
// func (s *server) GetBlockInfo(ctx context.Context, id vanus.ID) error {
// if err := s.checkState(); err != nil {
// return err
// }
// return nil
// }
func (s *server) DescribeBlock(_ context.Context, id vanus.ID) (*metapb.SegmentHealthInfo, error) {
if err := s.checkState(); err != nil {
return nil, err
}

var b Replica
if v, ok := s.replicas.Load(id); ok {
b, _ = v.(Replica)
} else {
return nil, errors.ErrResourceNotFound.WithMessage("the block doesn't exist")
}

return b.Status(), nil
}

// ActivateSegment mark a block ready to using and preparing to initializing a replica group.
func (s *server) ActivateSegment(
Expand Down
14 changes: 8 additions & 6 deletions proto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ endif
generate-pb: check-package-env
mkdir -p ${VSPROTO_ROOT}/pkg/${package}
protoc -I=${VSPROTO_ROOT}/include \
-I=${VSPROTO_ROOT}/proto \
--go_out=${VSPROTO_ROOT}/pkg/${package} \
--go_opt=paths=source_relative \
--go-grpc_out=${VSPROTO_ROOT}/pkg/${package} \
--go-grpc_opt=paths=source_relative,require_unimplemented_servers=false \
${VSPROTO_ROOT}/proto/${package}.proto
-I=${VSPROTO_ROOT}/proto \
--go_out=${VSPROTO_ROOT}/pkg/${package} \
--go_opt=paths=source_relative \
--go-grpc_out=${VSPROTO_ROOT}/pkg/${package} \
--go-grpc_opt=paths=source_relative,require_unimplemented_servers=false \
--go-grpc-mock_out=${VSPROTO_ROOT}/pkg/${package} \
--go-grpc-mock_opt=paths=source_relative \
${VSPROTO_ROOT}/proto/${package}.proto
15 changes: 11 additions & 4 deletions proto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Protobuf files for Vanus

### Setup IDE

1. install plugin 'Proto Buffer' in marketplace
1. install plugin 'Protocol Buffer' in marketplace
2. config path: Performance -> Language&Framework -> Proto Buffers -> Import paths;
and add `include` and `proto` directory to

Expand All @@ -17,12 +17,13 @@ Protobuf files for Vanus
brew install protobuf

# or download https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-osx-x86_64.zip
unzip protoc-3.19.4-osx-x86_64.zip
mv bin/protoc $GOPATH/bin/
#unzip protoc-3.19.4-osx-x86_64.zip
#mv bin/protoc $GOPATH/bin/

# 2. Install protoc-gen-go and protoc-gen-go-grpc
# 2. Install protoc-gen-go, protoc-gen-go-grpc and protoc-gen-go-grpc-mock
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/sorcererxw/protoc-gen-go-grpc-mock@latest
```

### debug
Expand All @@ -47,6 +48,12 @@ package=controller make generate-pb
brew install bufbuild/buf/buf
```

### Install `protoc-gen-go-grpc-mock`

```bash
go install github.com/sorcererxw/protoc-gen-go-grpc-mock@latest
```

### Generate code

```bash
Expand Down
Loading

0 comments on commit 54617b3

Please sign in to comment.