Files
seaweedfs/seaweed-block/core/rpc/answer/policy.go
T

207 lines
11 KiB
Go

// Package answer owns the operator failure policy shared by CLI and RPC
// adapters. Transport adapters supply facts; they do not decide retry safety.
package answer
import (
"fmt"
"google.golang.org/grpc/codes"
"time"
)
// Phase is admission evidence, not a guess based on an error message.
type Phase string
const (
Unsent Phase = "unsent"
PossiblyAdmitted Phase = "possibly_admitted"
)
// Contract is the existing output protocol, not a retry decision. It is
// part of the error kind so the same pure registry preserves wire exits.
type Contract string
const (
Chunk Contract = "chunk"
ChunkVerify Contract = "chunk_verify"
Workspace Contract = "workspace"
VolumeCreate Contract = "volume_create"
VolumeResolve Contract = "volume_resolve"
Cluster Contract = "cluster"
Restore Contract = "restore"
Failover Contract = "failover"
VolumeJob Contract = "volume_job"
VolumeStop Contract = "volume_stop"
VolumePlacement Contract = "volume_placement"
Shell Contract = "shell"
Ops Contract = "ops"
)
type Kind struct {
Name string
Contract Contract
ServerCode string
ServerRetryable bool
ServerMessage string
}
func (kind Kind) String() string {
name := string(kind.Contract) + "/" + kind.Name
if kind.ServerCode != "" && len(kind.ServerCode) <= 64 {
name += "/" + kind.ServerCode
}
return name
}
func ForContract(contract Contract, kind Kind) Kind { kind.Contract = contract; return kind }
var (
Connection = Kind{Name: "connection"}
PlaintextConnection = Kind{Name: "plaintext_connection"}
Certificate = Kind{Name: "certificate"}
Trust = Kind{Name: "trust"}
Name = Kind{Name: "name"}
DNS = Kind{Name: "dns"}
Timeout = Kind{Name: "timeout"}
Canceled = Kind{Name: "canceled"}
Credential = Kind{Name: "local_credential"}
Configuration = Kind{Name: "local_configuration"}
CallerDeadline = Kind{Name: "caller_deadline"}
)
// RetryRule describes recovery of this operation, never a new operation.
type RetryRule string
const (
Resend RetryRule = "resend_same_operation"
Recover RetryRule = "replay_key_or_poll"
Never RetryRule = "fix_before_retry"
)
type Policy struct {
Code string
Retryable bool
Retry RetryRule
Cure string
Exit int
}
type key struct {
phase Phase
kind Kind
}
// The registry is the sole owner of classification, cure and exit policy.
var registry = buildRegistry()
func buildRegistry() map[key]Policy {
base := map[key]Policy{
{Unsent, Credential}: {"bad_request", false, Never, "fix the local credential files (certificate, key, CA or token) before dialing again", 0},
{Unsent, Configuration}: {"bad_request", false, Never, "correct the local address or transport configuration before dialing again", 0},
{PossiblyAdmitted, Configuration}: {"bad_request", false, Never, "correct the local address or transport configuration, then recover the same operation", 0},
{Unsent, CallerDeadline}: {"deadline_exceeded", true, Resend, "the caller's deadline passed before sending; retry the same operation with a sufficient caller window", 0},
{PossiblyAdmitted, CallerDeadline}: {"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0},
{PossiblyAdmitted, Credential}: {"bad_request", false, Never, "fix the local credential files (certificate, key, CA or token), then recover the same operation", 0},
{Unsent, Connection}: {"remote_unavailable", true, Resend, "retry the same operation when the remote is reachable", 0},
{PossiblyAdmitted, Connection}: {"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0},
{Unsent, DNS}: {"remote_unavailable", true, Resend, "check name resolution, then retry the same operation", 0},
{PossiblyAdmitted, DNS}: {"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0},
{Unsent, Certificate}: {"not_authorized", false, Never, "fix the client certificate, its key and the peer's certificate policy before retrying", 0},
{PossiblyAdmitted, Certificate}: {"not_authorized", false, Never, "fix the client certificate, its key and the peer's certificate policy before retrying", 0},
{Unsent, Trust}: {"not_authorized", false, Never, "fix the trusted CA before retrying", 0},
{PossiblyAdmitted, Trust}: {"not_authorized", false, Never, "fix the trusted CA before retrying", 0},
{Unsent, Name}: {"not_authorized", false, Never, "fix the peer address or its certificate names before retrying", 0},
{PossiblyAdmitted, Name}: {"not_authorized", false, Never, "fix the peer address or its certificate names before retrying", 0},
{Unsent, Timeout}: {"remote_unavailable", true, Resend, "retry the same operation when the remote is reachable", 0},
{PossiblyAdmitted, Timeout}: {"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0},
{Unsent, Canceled}: {"canceled", false, Never, "operation interrupted by its caller", 0},
{PossiblyAdmitted, Canceled}: {"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0},
}
base[key{Unsent, PlaintextConnection}] = base[key{Unsent, Connection}]
base[key{PossiblyAdmitted, PlaintextConnection}] = base[key{PossiblyAdmitted, Connection}]
for _, row := range []struct {
code codes.Code
policy Policy
}{
{codes.InvalidArgument, Policy{"bad_request", false, Never, "correct the server's stated argument error before retrying", 0}},
{codes.FailedPrecondition, Policy{"not_admitted", false, Never, "resolve the server's stated precondition before retrying", 0}},
{codes.NotFound, Policy{"not_found", false, Never, "check the requested identity; do not create a replacement as a retry", 0}},
{codes.AlreadyExists, Policy{"target_exists", false, Never, "read the existing target and reconcile the original operation", 0}},
{codes.PermissionDenied, Policy{"not_authorized", false, Never, "check the certificate's role in the server's role file before retrying", 0}},
{codes.Unauthenticated, Policy{"not_authorized", false, Never, "check the certificate and the server's trusted CA and role file before retrying", 0}},
{codes.Unimplemented, Policy{"unsupported", false, Never, "use a server that supports this operation", 0}},
{codes.DataLoss, Policy{"readout_unproven", false, Never, "inspect the stored operation and its loss or integrity evidence before proceeding", 0}},
{codes.Internal, Policy{"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0}},
{codes.Unknown, Policy{"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0}},
{codes.Aborted, Policy{"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0}},
{codes.OutOfRange, Policy{"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0}},
{codes.ResourceExhausted, Policy{"outcome_unknown", true, Recover, "recover the same operation by its replay key or poll its existing handle; do not start another operation", 0}},
} {
for _, phase := range []Phase{Unsent, PossiblyAdmitted} {
base[key{phase, rpcKindKey(row.code)}] = row.policy
}
}
addDomainCells(base)
for _, contract := range []Contract{Chunk, ChunkVerify, Workspace, VolumeCreate, VolumeResolve, Cluster, Restore, Failover, VolumeJob, VolumeStop, VolumePlacement, Shell, Ops} {
for cell, policy := range base {
if cell.kind.Contract != "" {
continue
}
if cell.kind == rpcKindKey(codes.NotFound) && (contract == Restore || contract == VolumeJob) {
policy.Code = "snapshot_not_found"
}
if contract == Failover && policy.Retryable {
policy.Retry = Recover
policy.Cure = "read the current authority line before another move; do not blindly repeat an uncertain move"
}
if contract == VolumeCreate && policy.Retryable {
policy.Cure = "earlier steps may remain recorded; retry identical name, geometry and placement"
}
if contract == Workspace && (cell.kind == rpcKindKey(codes.PermissionDenied) || cell.kind == rpcKindKey(codes.Unauthenticated)) {
policy.Cure = "not authorized by blockmaster: " + policy.Cure
}
base[key{cell.phase, ForContract(contract, cell.kind)}] = policy
}
}
addHTTPStatusCells(base)
return base
}
// Lookup refuses unlisted combinations. Callers must not invent a retry
// policy when an adapter supplies a new error kind or lacks phase evidence.
func Lookup(phase Phase, kind Kind) (Policy, error) {
lookup := kind
if kind.Name == "server_domain" {
if kind.ServerCode == "" || len(kind.ServerCode) > 64 {
return Policy{}, fmt.Errorf("answer: server domain code must have 1..64 bytes")
}
lookup.ServerMessage, lookup.ServerRetryable = "", false
}
if kind.Name == "delete_lease_barrier" {
if len(kind.ServerMessage) > 64 {
return Policy{}, fmt.Errorf("answer: invalid lease barrier stamp")
}
if _, err := time.Parse(time.RFC3339Nano, kind.ServerMessage); err != nil {
return Policy{}, fmt.Errorf("answer: invalid lease barrier stamp: %w", err)
}
lookup.ServerMessage = ""
}
policy, ok := registry[key{phase, lookup}]
if !ok {
return Policy{}, fmt.Errorf("answer: unlisted phase %q, error kind %q", phase, kind)
}
if kind.Name == "server_domain" {
policy = projectDomain(policy, kind)
}
if kind.Name == "delete_lease_barrier" {
policy.Cure = "retry the same --request-id after " + kind.ServerMessage
}
if kind.Contract == Shell && kind.Name != "server_domain" && kind.Name != "delete_lease_barrier" && kind.Name != CallerDeadline.Name && policy.Retryable {
policy.Cure = "recover this read-only inspection by polling the same record; this inspection does not mutate the volume"
}
if kind.Name == PlaintextConnection.Name && phase == Unsent {
policy.Cure += "; this client is configured for plaintext: check whether the endpoint requires a TLS-capable client (for blockmaster, configure SW_BLOCK_TLS_CERT, SW_BLOCK_TLS_KEY and SW_BLOCK_TLS_CA); a closed preface alone does not prove a TLS requirement"
}
policy.Exit = projectExit(kind, policy)
return policy, nil
}