mirror of
https://github.com/milvus-io/milvus.git
synced 2026-07-21 18:25:44 +00:00
issue: #50742 ## What this PR does Optimizes protobuf decoding on Milvus RPC read/write hot paths with a hand-written decoder in `pkg/util/fastpb`. The final change contains no gRPC connection-pool or connection-count changes. ## Fast decoder coverage The gRPC `releaseCodec.Unmarshal` path dispatches these top-level messages to fastpb: - `internalpb.RetrieveResults` - `milvuspb.InsertRequest` - `milvuspb.UpsertRequest` `schemapb.SearchResultData` is not a top-level codec-dispatched message. It is decoded directly from `SlicedBlob` at the search/reduce hot paths in: - `internal/proxy/search_reduce_util.go` - `internal/querynodev2/segments/result.go` - `internal/querynodev2/local_worker.go` - `internal/querynodev2/tasks/search_task_go_reduce.go` Nested hot messages such as `FieldData`, scalar/vector arrays, and IDs are decoded by the same package. Main optimizations: - varchar string arrays use one backing byte arena, reducing per-string allocations - packed float vectors use a single memcpy into self-owned memory - cold, complex, unknown, and mismatched fields delegate or merge through the official protobuf codec - no new external dependency or source-buffer aliasing ## Compatibility boundary The compatibility contract depends on the data path: - **Untrusted client ingress (`InsertRequest` and `UpsertRequest`)** validates proto3 strings as UTF-8 and is tested differentially against `proto.Unmarshal`, including malformed input behavior. - **Trusted internal result paths (`SearchResultData`, `RetrieveResults`, and direct `FieldData` decoding)** assume canonical Milvus/segcore protobuf output and intentionally skip UTF-8 validation for performance. Therefore these paths do not claim equivalence for arbitrary adversarial wire containing invalid UTF-8. Within that boundary, the decoder preserves the protobuf behaviors exercised by the differential tests: - proto2 groups fall back to the official codec - known fields with mismatched wire types fall back to the official codec - unknown and future fields are preserved through an official merge pass - repeated singular messages merge from the raw wire, including explicitly encoded proto3 default values - repeated message-valued oneof members merge correctly, while different variants remain last-wins - mixed packed and unpacked scalar encodings preserve wire order - descriptor field-set tripwires force decoder review when the protobuf schema changes The unsafe packed-float copy assumes the currently supported little-endian Milvus targets such as x86-64 and arm64. ## Benchmarks fastpb vs official `proto.Unmarshal`, using 1000-row payloads: | path | payload | speedup | allocations/op | |---|---|---:|---:| | search / query | varchar | about 2x | about 1000-2035 to about 10-17 | | search / query | float vector, dim 768 | about 6x | unchanged | | insert | varchar with UTF-8 validation | about 1.8x | 1019 to about 10 | ## Verification - unit and differential tests cover field descriptors, UTF-8 contracts, wire-type mismatches, proto2 groups, unknown fields, packed/unpacked ordering, oneof behavior, and repeated-message merge semantics - randomized canonical-message equivalence tests cover search, retrieve, insert, and upsert payloads - `go test ./util/fastpb ./util/resource` passes - `go vet ./util/fastpb ./util/resource` passes - current full CI and end-to-end status is tracked by the PR checks 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: xiaofanluan <xiaofan.luan@zilliz.com> Signed-off-by: xiaofanluan <xf@hjjaq.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: xiaofanluan <xf@hjjaq.com>
115 lines
4.2 KiB
Go
115 lines
4.2 KiB
Go
// Licensed to the LF AI & Data foundation under one
|
|
// or more contributor license agreements. See the NOTICE file
|
|
// distributed with this work for additional information
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
// to you under the Apache License, Version 2.0 (the
|
|
// "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package querynodev2
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v3/commonpb"
|
|
"github.com/milvus-io/milvus-proto/go-api/v3/schemapb"
|
|
"github.com/milvus-io/milvus/internal/querynodev2/cluster"
|
|
"github.com/milvus-io/milvus/internal/util/streamrpc"
|
|
"github.com/milvus-io/milvus/pkg/v3/proto/internalpb"
|
|
"github.com/milvus-io/milvus/pkg/v3/proto/querypb"
|
|
"github.com/milvus-io/milvus/pkg/v3/util/fastpb"
|
|
"github.com/milvus-io/milvus/pkg/v3/util/merr"
|
|
"github.com/milvus-io/milvus/pkg/v3/util/resource"
|
|
)
|
|
|
|
var _ cluster.Worker = &LocalWorker{}
|
|
|
|
type LocalWorker struct {
|
|
node *QueryNode
|
|
}
|
|
|
|
func NewLocalWorker(node *QueryNode) *LocalWorker {
|
|
return &LocalWorker{
|
|
node: node,
|
|
}
|
|
}
|
|
|
|
func (w *LocalWorker) LoadSegments(ctx context.Context, req *querypb.LoadSegmentsRequest) error {
|
|
status, err := w.node.LoadSegments(ctx, req)
|
|
return merr.CheckRPCCall(status, err)
|
|
}
|
|
|
|
func (w *LocalWorker) ReleaseSegments(ctx context.Context, req *querypb.ReleaseSegmentsRequest) error {
|
|
status, err := w.node.ReleaseSegments(ctx, req)
|
|
return merr.CheckRPCCall(status, err)
|
|
}
|
|
|
|
func (w *LocalWorker) Delete(ctx context.Context, req *querypb.DeleteRequest) error {
|
|
status, err := w.node.Delete(ctx, req)
|
|
return merr.CheckRPCCall(status, err)
|
|
}
|
|
|
|
func (w *LocalWorker) DeleteBatch(ctx context.Context, req *querypb.DeleteBatchRequest) (*querypb.DeleteBatchResponse, error) {
|
|
return w.node.DeleteBatch(ctx, req)
|
|
}
|
|
|
|
func (w *LocalWorker) SearchSegments(ctx context.Context, req *querypb.SearchRequest) (*internalpb.SearchResults, error) {
|
|
resp, err := w.node.SearchSegments(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// The gRPC codec (releaseCodec) never runs for in-process calls, so any C
|
|
// memory pinned in MsgPins will never be triggered by Marshal. We must
|
|
// consume it here: unmarshal SlicedBlob → ResultData (so the delegator can
|
|
// use it directly), then release any pinned C memory.
|
|
if blob := resp.GetSlicedBlob(); len(blob) > 0 {
|
|
var resultData schemapb.SearchResultData
|
|
// fastpb: wire-equivalent fast decoder for the local (in-process) search
|
|
// hot path (~2x varchar / ~6x vector vs proto.Unmarshal).
|
|
if unmarshalErr := fastpb.UnmarshalSearchResultData(blob, &resultData); unmarshalErr != nil {
|
|
resource.MsgPins.Release(resp) // still release to avoid leak
|
|
return nil, merr.WrapErrServiceInternal("unmarshal SearchResultData from SlicedBlob", unmarshalErr.Error())
|
|
}
|
|
resp.ResultData = &resultData
|
|
resp.SlicedBlob = nil
|
|
resource.MsgPins.Release(resp) // no-op if not pinned
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (w *LocalWorker) QueryStreamSegments(ctx context.Context, req *querypb.QueryRequest, srv streamrpc.QueryStreamServer) error {
|
|
return w.node.queryStreamSegments(ctx, req, srv)
|
|
}
|
|
|
|
func (w *LocalWorker) QuerySegments(ctx context.Context, req *querypb.QueryRequest) (*internalpb.RetrieveResults, error) {
|
|
return w.node.QuerySegments(ctx, req)
|
|
}
|
|
|
|
func (w *LocalWorker) GetStatistics(ctx context.Context, req *querypb.GetStatisticsRequest) (*internalpb.GetStatisticsResponse, error) {
|
|
return w.node.GetStatistics(ctx, req)
|
|
}
|
|
|
|
func (w *LocalWorker) UpdateSchema(ctx context.Context, req *querypb.UpdateSchemaRequest) (*commonpb.Status, error) {
|
|
return w.node.UpdateSchema(ctx, req)
|
|
}
|
|
|
|
func (w *LocalWorker) IsHealthy() bool {
|
|
return true
|
|
}
|
|
|
|
func (w *LocalWorker) DropIndex(ctx context.Context, req *querypb.DropIndexRequest) error {
|
|
status, err := w.node.DropIndex(ctx, req)
|
|
return merr.CheckRPCCall(status, err)
|
|
}
|
|
|
|
func (w *LocalWorker) Stop() {
|
|
}
|