mirror of
https://github.com/milvus-io/milvus.git
synced 2026-07-21 10:15:43 +00:00
## Summary In CDC replication mode, a 2PC bulk import (`auto_commit=false`) runs **independently** on the primary (source) and standby clusters against the same files. Only the source can broadcast `Commit`/`Rollback`; the standby replays them via CDC. When the import fails on **one** cluster but the other holds a replicated `Uncommitted` job, that job had no recovery path: - **Mode 1 (source fails, standby `Uncommitted`):** the failure never broadcast a `RollbackImport`, and `AbortImport` on a terminal-`Failed` source was rejected — so the standby stayed `Uncommitted` indefinitely, holding invisible imported segments. (Observed in production: standby stuck 2+ days.) - **Mode 2 (standby fails, source `Uncommitted`):** if the platform commits the source before confirming the standby is prepared, the source commits while the standby's commit ack no-ops on its `Failed` job → silent divergence. issue: #51070 ## What this changes (datacoord) - **Abort a failed source (`AbortImport`):** a `Failed` source (its own import failed) is now abortable — it broadcasts `RollbackImport` to release the peer's `Uncommitted` job. `Committing`/`Completed` stay rejected. Lets an operator recover **immediately**, instead of waiting for GC. - **GC self-heal (`importChecker.checkGC`):** before GC removes a `Failed` replicate-source job, it broadcasts `RollbackImport` so the peer is released **automatically at retention expiry**. Transient broadcast failure keeps the job and retries on the next tick; a standby's `ErrNotPrimary` proceeds; **job removal itself is the idempotency guard** (once gone, never re-broadcast). This closes the recovery-window gap without a persisted flag or proto change. - **Divergence signal (`CommitImport` ack):** a commit ack landing on a `Failed` replica now logs at WARN (Mode 2). - `isReplicatingClusterNow`: the GC decision now **propagates** replication-check errors instead of tolerating them — an indeterminate status (transient balancer error, `OnShutdownError`) retains the job and retries next tick, since a false "not replicating" would irreversibly strand the replicating peer; only permanent broadcast errors (`ErrNotPrimary` from a standby, `ErrCollectionNotFound` after a replicated drop) proceed to GC. The balancer access is bounded by a 10s timeout so an unregistered balancer cannot park the checker loop. - Minor: the coordinator callbacks the checker needs are bundled into an `importCheckerHooks` struct instead of a growing positional-arg list. ## Recovery guarantee The peer's `Uncommitted` job is released either **immediately** via `AbortImport`, or **automatically** when the failed source reaches GC retention — no permanent stranding, and no dependence on a manual abort within the 3h retention window. ## Not in scope (follow-ups) - **Idempotent re-broadcast dedup:** repeated `AbortImport` on a real-failure source re-broadcasts each time (harmless — flusher/standby-ack no-op). A persisted `rollback_broadcasted` flag would dedup this. - **Eager self-heal:** broadcasting the rollback at *failure* time (in `checkFailedJob`) rather than at GC time, to shorten peer-stranding to seconds. - **Mode 2 prevention:** a standby→coordinator prepared-vote so the source refuses to commit unless all replicas are prepared. ## Test plan - `checkGC` self-heal: failed replicate-source broadcasts-then-removes with transient-retry; `ErrNotPrimary` (standby) proceeds; non-replicating skips the broadcast. - `AbortImport`: failed-source broadcasts rollback; `Committing` and `Completed` rejected; previously-user-aborted is idempotent. - CI `ci-v2/ut-go` compiles and runs these tests. (Local datacoord UT was not run for this revision due to cgo core drift in the worktree.) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: bigsheeper <yihao.dai@zilliz.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
317 lines
12 KiB
Go
317 lines
12 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 datacoord
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v3/commonpb"
|
|
"github.com/milvus-io/milvus-proto/go-api/v3/msgpb"
|
|
"github.com/milvus-io/milvus-proto/go-api/v3/schemapb"
|
|
"github.com/milvus-io/milvus/internal/streamingcoord/server/balancer/balance"
|
|
"github.com/milvus-io/milvus/internal/streamingcoord/server/broadcaster/registry"
|
|
"github.com/milvus-io/milvus/internal/util/importutilv2"
|
|
"github.com/milvus-io/milvus/pkg/v3/mlog"
|
|
"github.com/milvus-io/milvus/pkg/v3/proto/internalpb"
|
|
"github.com/milvus-io/milvus/pkg/v3/streaming/util/message"
|
|
"github.com/milvus-io/milvus/pkg/v3/util/funcutil"
|
|
"github.com/milvus-io/milvus/pkg/v3/util/merr"
|
|
"github.com/milvus-io/milvus/pkg/v3/util/paramtable"
|
|
)
|
|
|
|
// importV1AckCallback handles the ack callback for import messages.
|
|
func (c *DDLCallbacks) importV1AckCallback(ctx context.Context, result message.BroadcastResultImportMessageV1) error {
|
|
body := result.Message.MustBody()
|
|
|
|
// Ensure Schema.DbName is populated from the broadcast message's DbName,
|
|
// matching the behavior in master where this was set before calling ImportV2.
|
|
if body.Schema != nil {
|
|
body.Schema.DbName = body.DbName
|
|
}
|
|
|
|
// Process each vchannel with its own TimeTick (not deprecated MsgBase)
|
|
// Each vchannel gets its own import job with the corresponding TimeTick
|
|
vchannels := make([]string, 0, len(result.Results))
|
|
for vchannel := range result.Results {
|
|
if funcutil.IsControlChannel(vchannel) {
|
|
continue
|
|
}
|
|
vchannels = append(vchannels, vchannel)
|
|
}
|
|
|
|
// Call createImportJobFromAck directly instead of ImportV2
|
|
// ImportV2 is only for proxy broadcast, not for ack callback
|
|
importResp, err := c.createImportJobFromAck(ctx, &internalpb.ImportRequestInternal{
|
|
DbID: 0, // already deprecated.
|
|
CollectionID: body.GetCollectionID(),
|
|
CollectionName: body.GetCollectionName(),
|
|
PartitionIDs: body.GetPartitionIDs(),
|
|
ChannelNames: vchannels,
|
|
Schema: body.GetSchema(),
|
|
Files: lo.Map(body.GetFiles(), func(file *msgpb.ImportFile, _ int) *internalpb.ImportFile {
|
|
return &internalpb.ImportFile{
|
|
Id: file.GetId(),
|
|
Paths: file.GetPaths(),
|
|
}
|
|
}),
|
|
Options: funcutil.Map2KeyValuePair(body.GetOptions()),
|
|
DataTimestamp: result.GetMaxTimeTick(), // TODO: use per-vchannel TimeTick in future, must be supported for CDC.
|
|
JobID: body.GetJobID(),
|
|
})
|
|
|
|
err = merr.CheckRPCCall(importResp, err)
|
|
if errors.Is(err, merr.ErrCollectionNotFound) {
|
|
mlog.Warn(ctx, "import job creation failed because of collection not found, skip it",
|
|
mlog.Strings("vchannels", vchannels),
|
|
mlog.String("job_id", importResp.GetJobID()), mlog.Err(err))
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
// validateImportRequest validates the import request before broadcasting.
|
|
// This includes all validation logic previously done in CheckCallback and Proxy.
|
|
func (s *Server) validateImportRequest(ctx context.Context, files []*msgpb.ImportFile, options []*commonpb.KeyValuePair) error {
|
|
// Validate timeout
|
|
_, err := importutilv2.GetTimeoutTs(options)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Validate binlog import files if it's a backup
|
|
if importutilv2.IsBackup(options) {
|
|
err = ValidateBinlogImportRequest(ctx, s.meta.chunkManager, files, options)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Validate max import job count
|
|
err = ValidateMaxImportJobExceed(ctx, s.importMeta)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.validateImportReplication(ctx, options); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) validateImportReplication(ctx context.Context, options []*commonpb.KeyValuePair) error {
|
|
balancer, err := balance.GetWithContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
assignment, err := balancer.GetLatestChannelAssignment()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if assignment == nil {
|
|
return nil
|
|
}
|
|
if !isReplicatingCluster(assignment.ReplicateConfiguration) {
|
|
return nil
|
|
}
|
|
|
|
if !paramtable.Get().DataCoordCfg.ImportInReplicatingCluster.GetAsBool() {
|
|
return merr.WrapErrOperationNotSupportedMsg("import in replicating cluster is not supported yet")
|
|
}
|
|
if importutilv2.IsAutoCommit(options) {
|
|
return merr.WrapErrOperationNotSupportedMsg("auto_commit=true import in replicating cluster is not supported")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func isReplicatingCluster(cfg *commonpb.ReplicateConfiguration) bool {
|
|
return cfg != nil && (len(cfg.GetCrossClusterTopology()) > 0 || len(cfg.GetClusters()) > 1)
|
|
}
|
|
|
|
// isReplicatingClusterNow reports whether this cluster is currently part of a CDC
|
|
// replication topology. A non-nil error means the status could not be determined (e.g. a
|
|
// transient balancer error, or OnShutdownError while streamingcoord is stopping before
|
|
// datacoord); the caller must treat that as indeterminate rather than "not replicating",
|
|
// because at GC time a false "not replicating" would irreversibly drop a replicating job
|
|
// without releasing the peer. A nil assignment is an unambiguous "not replicating".
|
|
func (s *Server) isReplicatingClusterNow(ctx context.Context) (bool, error) {
|
|
balancer, err := balance.GetWithContext(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
assignment, err := balancer.GetLatestChannelAssignment()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if assignment == nil {
|
|
return false, nil
|
|
}
|
|
return isReplicatingCluster(assignment.ReplicateConfiguration), nil
|
|
}
|
|
|
|
// broadcastImport broadcasts the import message to all vchannels.
|
|
// This method is called from the new ImportV2 flow where proxy calls DataCoord directly.
|
|
func (s *Server) broadcastImport(ctx context.Context,
|
|
collectionName string,
|
|
collectionID int64,
|
|
partitionIDs []int64,
|
|
files []*internalpb.ImportFile,
|
|
options []*commonpb.KeyValuePair,
|
|
schema *schemapb.CollectionSchema,
|
|
jobID int64,
|
|
vchannels []string,
|
|
) error {
|
|
// Convert files to msgpb format for validation
|
|
msgFiles := lo.Map(files, func(file *internalpb.ImportFile, _ int) *msgpb.ImportFile {
|
|
return &msgpb.ImportFile{
|
|
Id: file.GetId(),
|
|
Paths: file.GetPaths(),
|
|
}
|
|
})
|
|
|
|
// Validate the request before broadcasting
|
|
if err := s.validateImportRequest(ctx, msgFiles, options); err != nil {
|
|
return merr.Wrap(err, "failed to validate import request")
|
|
}
|
|
|
|
// Get database name from collection metadata via broker
|
|
// This is safer than extracting from schema which may be stale
|
|
broadcaster, err := s.startBroadcastWithCollectionID(ctx, collectionID)
|
|
if err != nil {
|
|
return merr.Wrap(err, "failed to start broadcast with collection id")
|
|
}
|
|
defer broadcaster.Close()
|
|
|
|
coll, err := s.broker.DescribeCollectionInternal(ctx, collectionID)
|
|
if err := merr.CheckRPCCall(coll, err); err != nil {
|
|
return err
|
|
}
|
|
// Build import message without deprecated MsgBase
|
|
msg := message.NewImportMessageBuilderV1().
|
|
WithHeader(&message.ImportMessageHeader{}).
|
|
WithBody(&msgpb.ImportMsg{
|
|
Base: &commonpb.MsgBase{
|
|
MsgType: commonpb.MsgType_Import,
|
|
Timestamp: 0,
|
|
},
|
|
DbName: coll.DbName,
|
|
CollectionName: collectionName,
|
|
CollectionID: collectionID,
|
|
PartitionIDs: partitionIDs,
|
|
Options: funcutil.KeyValuePair2Map(options),
|
|
Files: msgFiles,
|
|
Schema: schema, // TODO: should we use the schema from the collection?
|
|
JobID: jobID,
|
|
}).
|
|
WithBroadcast(vchannels).
|
|
MustBuildBroadcast()
|
|
|
|
// Broadcast the message
|
|
_, err = broadcaster.Broadcast(ctx, msg)
|
|
return err
|
|
}
|
|
|
|
func (c *DDLCallbacks) registerImportCallbacks() {
|
|
registry.RegisterImportV1AckCallback(c.importV1AckCallback)
|
|
registry.RegisterCommitImportV2AckCallback(c.commitImportV2AckCallback)
|
|
registry.RegisterRollbackImportV2AckCallback(c.rollbackImportV2AckCallback)
|
|
}
|
|
|
|
// commitImportV2AckCallback handles the ack callback for CommitImport WAL message.
|
|
// It transitions the import job from Uncommitted → Committing state.
|
|
// Concurrency safety is guaranteed by the broadcaster framework's resource key lock
|
|
// (exclusive collection-level lock), so no CAS is needed here.
|
|
func (c *DDLCallbacks) commitImportV2AckCallback(ctx context.Context, result message.BroadcastResultCommitImportMessageV2) error {
|
|
header := result.Message.Header()
|
|
jobID := header.GetJobId()
|
|
mlog.Info(ctx, "CommitImport broadcast ack received", mlog.FieldJobID(jobID))
|
|
|
|
job := c.importMeta.GetJob(ctx, jobID)
|
|
if job == nil {
|
|
mlog.Warn(ctx, "CommitImport: job not found, skipping", mlog.FieldJobID(jobID))
|
|
return nil
|
|
}
|
|
switch job.GetState() {
|
|
case internalpb.ImportJobState_Uncommitted:
|
|
// proceed
|
|
case internalpb.ImportJobState_Committing, internalpb.ImportJobState_Completed:
|
|
mlog.Info(ctx, "CommitImport: job already committing or completed, no-op",
|
|
mlog.FieldJobID(jobID), mlog.String("state", job.GetState().String()))
|
|
return nil
|
|
case internalpb.ImportJobState_Failed:
|
|
// Divergence signal: the source committed but this replica already failed, so
|
|
// this replica will NOT make the data visible. Left as a no-op here; surfaced
|
|
// at WARN for alerting.
|
|
mlog.Warn(ctx, "CommitImport ack landed on a Failed import job; this replica will NOT commit while the source commits — potential primary/standby divergence",
|
|
mlog.FieldJobID(jobID), mlog.String("reason", job.GetReason()))
|
|
return nil
|
|
default:
|
|
// CommitImport may be replicated before the local import task reaches
|
|
// Uncommitted. Returning an error keeps the broadcast task alive so the
|
|
// callback can retry after the import task finishes writing local meta.
|
|
mlog.Info(ctx, "CommitImport: job is not ready, retry later",
|
|
mlog.FieldJobID(jobID), mlog.String("state", job.GetState().String()))
|
|
return merr.WrapErrImportSysFailedMsg("job %d is in state %s, waiting for Uncommitted", jobID, job.GetState())
|
|
}
|
|
|
|
if err := c.importMeta.UpdateJob(ctx, jobID,
|
|
UpdateJobState(internalpb.ImportJobState_Committing),
|
|
); err != nil {
|
|
return err
|
|
}
|
|
|
|
uncommittedDuration := job.GetTR().RecordSpan()
|
|
mlog.Info(ctx, "import job uncommitted stage done",
|
|
mlog.FieldJobID(jobID),
|
|
mlog.Duration("jobTimeCost/uncommitted", uncommittedDuration))
|
|
return nil
|
|
}
|
|
|
|
// rollbackImportV2AckCallback handles the ack callback for RollbackImport WAL message.
|
|
// It transitions the import job to Failed state and records that the failure
|
|
// was user-initiated so AbortImport retries can be idempotent.
|
|
// Concurrency safety is guaranteed by the broadcaster framework's resource key lock
|
|
// (exclusive collection-level lock), so no CAS is needed here.
|
|
// Segment cleanup is handled by the import inspector (processFailed), not here.
|
|
func (c *DDLCallbacks) rollbackImportV2AckCallback(ctx context.Context, result message.BroadcastResultRollbackImportMessageV2) error {
|
|
header := result.Message.Header()
|
|
jobID := header.GetJobId()
|
|
mlog.Info(ctx, "RollbackImport broadcast ack received", mlog.FieldJobID(jobID))
|
|
|
|
job := c.importMeta.GetJob(ctx, jobID)
|
|
if job == nil {
|
|
mlog.Warn(ctx, "RollbackImport: job not found, skipping", mlog.FieldJobID(jobID))
|
|
return nil
|
|
}
|
|
state := job.GetState()
|
|
if state == internalpb.ImportJobState_Committing ||
|
|
state == internalpb.ImportJobState_Completed ||
|
|
state == internalpb.ImportJobState_Failed {
|
|
mlog.Info(ctx, "RollbackImport: job already in terminal/committed state, no-op",
|
|
mlog.FieldJobID(jobID), mlog.String("state", state.String()))
|
|
return nil
|
|
}
|
|
|
|
return c.importMeta.UpdateJob(ctx, jobID,
|
|
UpdateJobState(internalpb.ImportJobState_Failed),
|
|
UpdateJobReason(importJobReasonAbortedByUser),
|
|
)
|
|
}
|