From 45febac298d4654a5444e756ed48eefc572d717f Mon Sep 17 00:00:00 2001 From: bigsheeper Date: Mon, 6 Jun 2022 15:56:05 +0800 Subject: [PATCH] Extract lease of cProto in QueryNode (#17373) Signed-off-by: bigsheeper --- internal/querynode/cgo_helper.go | 19 ++++++++++++++++++- internal/querynode/segment.go | 12 ------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/internal/querynode/cgo_helper.go b/internal/querynode/cgo_helper.go index 202b1e5086..f1e2fb425b 100644 --- a/internal/querynode/cgo_helper.go +++ b/internal/querynode/cgo_helper.go @@ -32,6 +32,8 @@ import ( "fmt" "unsafe" + "github.com/golang/protobuf/proto" + "github.com/milvus-io/milvus/internal/log" "github.com/milvus-io/milvus/internal/proto/commonpb" "github.com/milvus-io/milvus/internal/util/cgoconverter" @@ -56,12 +58,27 @@ func HandleCStatus(status *C.CStatus, extraInfo string) error { return errors.New(finalMsg) } +// HandleCProto deal with the result proto returned from CGO +func HandleCProto(cRes *C.CProto, msg proto.Message) error { + // Standalone CProto is protobuf created by C side, + // Passed from c side + // memory is managed manually + lease, blob := cgoconverter.UnsafeGoBytes(&cRes.proto_blob, int(cRes.proto_size)) + defer cgoconverter.Release(lease) + + return proto.Unmarshal(blob, msg) +} + +// CopyCProtoBlob returns the copy of C memory func CopyCProtoBlob(cProto *C.CProto) []byte { blob := C.GoBytes(unsafe.Pointer(cProto.proto_blob), C.int32_t(cProto.proto_size)) + C.free(unsafe.Pointer(cProto.proto_blob)) return blob } +// GetCProtoBlob returns the raw C memory, invoker should release it itself func GetCProtoBlob(cProto *C.CProto) []byte { - _, blob := cgoconverter.UnsafeGoBytes(&cProto.proto_blob, int(cProto.proto_size)) + lease, blob := cgoconverter.UnsafeGoBytes(&cProto.proto_blob, int(cProto.proto_size)) + cgoconverter.Extract(lease) return blob } diff --git a/internal/querynode/segment.go b/internal/querynode/segment.go index 43cd51d131..385640b4ee 100644 --- a/internal/querynode/segment.go +++ b/internal/querynode/segment.go @@ -53,7 +53,6 @@ import ( "github.com/milvus-io/milvus/internal/proto/schemapb" "github.com/milvus-io/milvus/internal/proto/segcorepb" "github.com/milvus-io/milvus/internal/storage" - "github.com/milvus-io/milvus/internal/util/cgoconverter" ) type segmentType = commonpb.SegmentState @@ -292,17 +291,6 @@ func (s *Segment) search(searchReq *searchRequest) (*SearchResult, error) { return &searchResult, nil } -// HandleCProto deal with the result proto returned from CGO -func HandleCProto(cRes *C.CProto, msg proto.Message) error { - // Standalone CProto is protobuf created by C side, - // Passed from c side - // memory is managed manually - lease, blob := cgoconverter.UnsafeGoBytes(&cRes.proto_blob, int(cRes.proto_size)) - defer cgoconverter.Release(lease) - - return proto.Unmarshal(blob, msg) -} - func (s *Segment) retrieve(plan *RetrievePlan) (*segcorepb.RetrieveResults, error) { if s.segmentPtr == nil { return nil, errors.New("null seg core pointer")