Files
seaweedfs/seaweed-block/core/rpc/lifecycleclient/reason.go
T
Ping QiuandClaude Opus 5.5 a5686e7fa7 Sync seaweed-block v1.1.1 into seaweed-block/ (#13411)
Brings seaweed-block main (v1.1.1 plus the docs and a test fix after it)
into seaweed-block/ as one commit. Only files under seaweed-block/ change,
and the subtree equals seaweed-block main exactly. The module's full
history stays in the seaweed-block repository.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 19:04:53 -08:00

85 lines
3.3 KiB
Go

package lifecycleclient
import (
"errors"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/status"
)
// Typed refusal reasons the master attaches to a gRPC status (an ErrorInfo
// detail in ErrorDomain), so a client decides by the reason, never by the
// message text.
const (
ErrorDomain = "seaweed-block"
// ReasonRestoreSourceNotOwn: a restore was refused because the snapshot
// names chunks of a volume other than its source (a restore target's
// snapshot cannot be restored again).
ReasonRestoreSourceNotOwn = "RESTORE_SOURCE_NOT_OWN"
// ReasonBinaryTooOld: the caller's binary does not read the cluster's
// format level (its heartbeat's reads_level is below it), so the master
// refuses it and gives it no work.
ReasonBinaryTooOld = "BINARY_TOO_OLD"
// ReasonFormatHostsTooOld: the format finalize was refused because a
// process that holds a volume reports that it reads below the new level.
ReasonFormatHostsTooOld = "FORMAT_HOSTS_TOO_OLD"
// ReasonHostHoldsWork: a host retire was refused because the host still
// holds a lease or an assignment for a volume.
ReasonHostHoldsWork = "HOST_HOLDS_WORK"
// ReasonSizeRequiresFormatLevel1: a new volume above 256 GiB was refused
// because the cluster's format level is 0 (finalize the v1.1 format).
ReasonSizeRequiresFormatLevel1 = "SIZE_REQUIRES_FORMAT_LEVEL_1"
// ReasonHolderNotReleased: a volume delete was refused because the
// volume's authority line has no recorded clean release (its holder has
// not released it).
ReasonHolderNotReleased = "HOLDER_NOT_RELEASED"
)
// WithReason returns st with an ErrorInfo detail carrying reason in
// ErrorDomain; st unchanged if the detail cannot be attached.
func WithReason(st *status.Status, reason string) *status.Status {
return WithReasonMetadata(st, reason, nil)
}
// WithReasonMetadata is WithReason with typed fields in the detail's
// metadata (for example one entry per refused host).
func WithReasonMetadata(st *status.Status, reason string, md map[string]string) *status.Status {
if d, err := st.WithDetails(&errdetails.ErrorInfo{Reason: reason, Domain: ErrorDomain, Metadata: md}); err == nil {
return d
}
return st
}
// Reason is the typed refusal reason in err's gRPC status (an ErrorInfo in
// ErrorDomain), or "" when it carries none. err may be wrapped.
func Reason(err error) string {
reason, _ := ReasonDetail(err)
return reason
}
// ReasonDetail is Reason with the detail's metadata (nil when none).
func ReasonDetail(err error) (string, map[string]string) {
var se interface{ GRPCStatus() *status.Status }
if !errors.As(err, &se) {
return "", nil
}
for _, d := range se.GRPCStatus().Details() {
if info, ok := d.(*errdetails.ErrorInfo); ok && info.GetDomain() == ErrorDomain {
return info.GetReason(), info.GetMetadata()
}
}
return "", nil
}
// HostMetadataPrefix keys one refused host in a reason's metadata:
// "host.<server id>" maps to what that host reported.
const HostMetadataPrefix = "host."
// UnconfirmedMetadataPrefix keys one host a format finalize could not
// confirm and did not block on: "unconfirmed.<server id>" maps to why.
const UnconfirmedMetadataPrefix = "unconfirmed."
// VolumeMetadataPrefix keys one volume in a reason's metadata:
// "volume.<volume id>" maps to the host that holds it.
const VolumeMetadataPrefix = "volume."