# Handle duplicate restore cluster creation error in workflow action

**Date**: 2026-06-09
**Module**: `github.com/tidbcloud/dataflow-service`
**Domain**: `BR / Restore`

---

## Background

`CheckAndCreateCluster.createCluster` calls mgmt-service `RestoreDedicatedCluster` using persisted `RequestArgsV2`.
After mgmt-service change (`tidb-management-service` PR #6316), repeated create attempts may return:

`rpc error: code = Code(49900005) desc = failed to set cluster custom status for restore task <id>`.

This should be treated as an idempotent duplicate-create signal in dataflow-service restore workflow handling.

---

## Analysis

### Impact Table

| Area | File | Change | Verification | Success Criteria |
|------|------|--------|--------------|------------------|
| Restore workflow action | `app/workflow/action/restore/create/create_cluster.go` | Add helper to detect duplicate-create rpc error and idempotent handling branch | `go test ./app/workflow/action/restore/create -run 'TestCheckAndCreateCluster_CreateCluster'` | New tests pass and existing behavior remains unchanged for non-duplicate errors |
| Restore workflow tests | `app/workflow/action/restore/create/create_cluster_test.go` | Add unit tests for duplicate and non-duplicate paths | Same as above | All asserted branches are covered |

### Symbols To Introduce

- `isDuplicateRestoreClusterError(err error) bool`

---

## Execution Steps

1. Add duplicate error detection helper in restore create action.
- Verification: `go test ./app/workflow/action/restore/create -run 'TestIsDuplicateRestoreClusterError'`
- Success criteria: helper test passes for positive and negative inputs.

2. Apply idempotent handling when duplicate error is returned by mgmt-service.
- Verification: `go test ./app/workflow/action/restore/create -run 'TestCheckAndCreateCluster_CreateCluster'`
- Success criteria: duplicate path is ignored only when restore entity already has cluster id; other errors are unchanged.

3. Run focused package test.
- Verification: `go test ./app/workflow/action/restore/create`
- Success criteria: package tests pass.

---

## Risk Assessment

| Risk | Likelihood | Impact | Mitigation |
|------|-----------|--------|------------|
| Duplicate detection matches unintended errors | Low | Medium | Require both error code token (`Code(49900005)`) and restore custom-status message fragment |
| Duplicate error ignored too early before cluster id persistence | Medium | Medium | Re-load restore entity before ignoring; if cluster id is still empty, keep existing error path |
| Regression in non-duplicate error handling | Low | Medium | Add explicit non-duplicate branch test |
