Skip to content

Commit 889c977

Browse files
authored
feat(store): describe block (#604)
Signed-off-by: James Yin <[email protected]>
1 parent 44bbb30 commit 889c977

File tree

12 files changed

+390
-346
lines changed

12 files changed

+390
-346
lines changed

internal/store/segment/api.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,17 @@ func (s *segmentServer) RemoveBlock(ctx context.Context, req *segpb.RemoveBlockR
7878
return &emptypb.Empty{}, nil
7979
}
8080

81-
func (s *segmentServer) GetBlockInfo(
82-
_ context.Context, _ *segpb.GetBlockInfoRequest,
83-
) (*segpb.GetBlockInfoResponse, error) {
84-
// TODO(james.yin): implements GetBlockInfo()
85-
// if err := s.srv.GetBlockInfo(ctx, 0); err != nil {
86-
// return nil, err
87-
// }
88-
89-
return &segpb.GetBlockInfoResponse{}, nil
81+
func (s *segmentServer) DescribeBlock(
82+
ctx context.Context, req *segpb.DescribeBlockRequest,
83+
) (*segpb.DescribeBlockResponse, error) {
84+
blockID := vanus.NewIDFromUint64(req.Id)
85+
86+
info, err := s.srv.DescribeBlock(ctx, blockID)
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
return &segpb.DescribeBlockResponse{Info: info}, nil
9092
}
9193

9294
func (s *segmentServer) ActivateSegment(

internal/store/segment/api_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
// first-party libraries.
2828
"github.com/vanus-labs/vanus/pkg/errors"
2929
cepb "github.com/vanus-labs/vanus/proto/pkg/cloudevents"
30+
metapb "github.com/vanus-labs/vanus/proto/pkg/meta"
3031
segpb "github.com/vanus-labs/vanus/proto/pkg/segment"
3132

3233
// this project.
@@ -117,12 +118,24 @@ func TestSegmentServer(t *testing.T) {
117118
So(err, ShouldEqual, errors.ErrInvalidRequest)
118119
})
119120

120-
Convey("GetBlockInfo()", func() {
121-
// FIXME: not implement.
122-
req := &segpb.GetBlockInfoRequest{}
123-
resp, err := ss.GetBlockInfo(context.Background(), req)
121+
Convey("DescribeBlock()", func() {
122+
srv.EXPECT().DescribeBlock(Any(), Not(vanus.EmptyID())).DoAndReturn(func(
123+
_ context.Context, id vanus.ID,
124+
) (*metapb.SegmentHealthInfo, error) {
125+
return &metapb.SegmentHealthInfo{Id: id.Uint64()}, nil
126+
})
127+
srv.EXPECT().DescribeBlock(Any(), Eq(vanus.EmptyID())).Return(nil, errors.ErrInvalidRequest)
128+
129+
req := &segpb.DescribeBlockRequest{
130+
Id: vanus.NewTestID().Uint64(),
131+
}
132+
resp, err := ss.DescribeBlock(context.Background(), req)
124133
So(err, ShouldBeNil)
125-
So(resp, ShouldNotBeNil)
134+
So(resp.Info.Id, ShouldEqual, req.Id)
135+
136+
req = &segpb.DescribeBlockRequest{}
137+
_, err = ss.DescribeBlock(context.Background(), req)
138+
So(err, ShouldEqual, errors.ErrInvalidRequest)
126139
})
127140

128141
Convey("ActivateSegment()", func() {

internal/store/segment/mock_replica.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/segment/mock_server.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/segment/server.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type Server interface {
8181

8282
CreateBlock(ctx context.Context, id vanus.ID, size int64) error
8383
RemoveBlock(ctx context.Context, id vanus.ID) error
84-
// GetBlockInfo(ctx context.Context, id vanus.ID) error
84+
DescribeBlock(ctx context.Context, id vanus.ID) (*metapb.SegmentHealthInfo, error)
8585

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

448448
// FIXME(james.yin): more info.
449-
log.Info(ctx).
450-
Stringer("block_id", b.ID()).
451-
Msg("The block has been deleted.")
449+
log.Info(ctx).Stringer("block_id", b.ID()).Msg("The block has been deleted.")
452450

453451
return nil
454452
}
455453

456-
// TODO(james.yin): implements GetBlockInfo.
457-
// func (s *server) GetBlockInfo(ctx context.Context, id vanus.ID) error {
458-
// if err := s.checkState(); err != nil {
459-
// return err
460-
// }
461-
// return nil
462-
// }
454+
func (s *server) DescribeBlock(_ context.Context, id vanus.ID) (*metapb.SegmentHealthInfo, error) {
455+
if err := s.checkState(); err != nil {
456+
return nil, err
457+
}
458+
459+
var b Replica
460+
if v, ok := s.replicas.Load(id); ok {
461+
b, _ = v.(Replica)
462+
} else {
463+
return nil, errors.ErrResourceNotFound.WithMessage("the block doesn't exist")
464+
}
465+
466+
return b.Status(), nil
467+
}
463468

464469
// ActivateSegment mark a block ready to using and preparing to initializing a replica group.
465470
func (s *server) ActivateSegment(

proto/Makefile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ endif
88
generate-pb: check-package-env
99
mkdir -p ${VSPROTO_ROOT}/pkg/${package}
1010
protoc -I=${VSPROTO_ROOT}/include \
11-
-I=${VSPROTO_ROOT}/proto \
12-
--go_out=${VSPROTO_ROOT}/pkg/${package} \
13-
--go_opt=paths=source_relative \
14-
--go-grpc_out=${VSPROTO_ROOT}/pkg/${package} \
15-
--go-grpc_opt=paths=source_relative,require_unimplemented_servers=false \
16-
${VSPROTO_ROOT}/proto/${package}.proto
11+
-I=${VSPROTO_ROOT}/proto \
12+
--go_out=${VSPROTO_ROOT}/pkg/${package} \
13+
--go_opt=paths=source_relative \
14+
--go-grpc_out=${VSPROTO_ROOT}/pkg/${package} \
15+
--go-grpc_opt=paths=source_relative,require_unimplemented_servers=false \
16+
--go-grpc-mock_out=${VSPROTO_ROOT}/pkg/${package} \
17+
--go-grpc-mock_opt=paths=source_relative \
18+
${VSPROTO_ROOT}/proto/${package}.proto

proto/README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Protobuf files for Vanus
66

77
### Setup IDE
88

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

@@ -17,12 +17,13 @@ Protobuf files for Vanus
1717
brew install protobuf
1818

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

23-
# 2. Install protoc-gen-go and protoc-gen-go-grpc
23+
# 2. Install protoc-gen-go, protoc-gen-go-grpc and protoc-gen-go-grpc-mock
2424
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
2525
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
26+
go install github.com/sorcererxw/protoc-gen-go-grpc-mock@latest
2627
```
2728

2829
### debug
@@ -47,6 +48,12 @@ package=controller make generate-pb
4748
brew install bufbuild/buf/buf
4849
```
4950

51+
### Install `protoc-gen-go-grpc-mock`
52+
53+
```bash
54+
go install github.com/sorcererxw/protoc-gen-go-grpc-mock@latest
55+
```
56+
5057
### Generate code
5158

5259
```bash

0 commit comments

Comments
 (0)