mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
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>
163 lines
8.2 KiB
Go
163 lines
8.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
|
|
control "github.com/seaweedfs/seaweed-block/core/rpc/control"
|
|
"github.com/seaweedfs/seaweed-block/core/rpc/link"
|
|
)
|
|
|
|
// workspaceRevoke is the master's RevokeWorkspace.
|
|
var workspaceRevoke = func(ctx context.Context, master string, id *control.WorkspaceIdentity) (*control.WorkspaceLeaseFact, error) {
|
|
conn, err := link.DialMasterFromEnv(master)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer conn.Close()
|
|
return control.NewWorkspaceServiceClient(conn).RevokeWorkspace(ctx, id)
|
|
}
|
|
|
|
// releaseQuery reads the master's lease record for stop and start (the
|
|
// master's QueryWorkspace; a test replaces it).
|
|
var releaseQuery = func(ctx context.Context, master, volume string) (*control.WorkspaceStatus, error) {
|
|
return workspaceQuery(ctx, master, volume)
|
|
}
|
|
|
|
// volumeReleaseSetup bounds start's lease/move setup and the stop reads/revoke before the release
|
|
// wait (a test shortens it).
|
|
var volumeReleaseSetup = 10 * time.Second
|
|
|
|
// volumeReleaseSlack is added to one lease validity before a release that
|
|
// has not been recorded is reported not released (a test shortens it).
|
|
var volumeReleaseSlack = 5 * time.Second
|
|
|
|
// releaseResult is a stop's answer about the volume's lease. Released is
|
|
// exactly what cluster status reads as stopped: the master recorded this
|
|
// host's clean release (its revocation ACK) for the current line.
|
|
type releaseResult struct {
|
|
Legacy bool // no binding: use the v1.1.0 cut-and-unit-stop path, without revoke
|
|
Released bool
|
|
Final bool // no later run changes the answer (moved, or the lease ended without a release)
|
|
Message string // the line printed
|
|
}
|
|
|
|
// leaseVerdict reads the master's lease record for this host's volume.
|
|
// holderEpoch is the running holder's own epoch (0 when it is not running).
|
|
func leaseVerdict(ws *control.WorkspaceStatus, volume, replica, server string, holderEpoch uint64) (releaseResult, bool) {
|
|
l, a := ws.GetLease(), ws.GetRevocationAck().GetIdentity()
|
|
if l == nil {
|
|
return releaseResult{Final: true, Message: "not released: the master holds no lease for " + volume + " (it moved or was deleted)"}, true
|
|
}
|
|
if a != nil && a.GetVolumeId() == volume && a.GetReplicaId() == replica && a.GetServerId() == server && a.GetEpoch() == l.GetEpoch() && l.GetReplicaId() == replica {
|
|
return releaseResult{Released: true, Final: true, Message: fmt.Sprintf("released: the master recorded %s/%s@%d's clean release; cluster status reads %s stopped; volume start obtains epoch+1 through a same-host clean move before starting the unit", server, replica, l.GetEpoch(), volume)}, true
|
|
}
|
|
if l.GetReplicaId() != replica || (holderEpoch != 0 && l.GetEpoch() != holderEpoch) {
|
|
return releaseResult{Final: true, Message: fmt.Sprintf("not released: %s moved (the master's line is %s@%d); nothing to release here", volume, l.GetReplicaId(), l.GetEpoch())}, true
|
|
}
|
|
if l.GetRevoking() && l.GetValidForNs() <= 0 {
|
|
return releaseResult{Final: true, Message: fmt.Sprintf("not released: %s@%d's lease ended without a clean release; volume start checks the current owner and refuses this revoked epoch; force advice is given only for a proven local owner", replica, l.GetEpoch())}, true
|
|
}
|
|
return releaseResult{}, false
|
|
}
|
|
|
|
// releaseLease ends a stop of a lease-holding (walstore-chunk) volume: it
|
|
// revokes the running holder's own epoch and waits, bounded by one lease
|
|
// validity, until the master records the holder's clean release (the holder
|
|
// drains, detaches HEAD and acknowledges at its next renewal). Only the
|
|
// master's recorded ACK counts as released; the holder's exit status does
|
|
// not. With the unit stopped (holderRunning false) it never reads the
|
|
// holder or revokes; it only reads the master's record, so a
|
|
// rerun converges: released, moved, or ended without a release.
|
|
func releaseLease(ctx context.Context, env verbEnv, e map[string]string, holderRunning bool, out io.Writer) releaseResult {
|
|
master, volume, replica, server := e["SWB_MASTER_ADDR"], e["SWB_VOLUME_ID"], e["SWB_REPLICA_ID"], e["SWB_SERVER_ID"]
|
|
// Every call is bounded: the reads before the wait by volumeReleaseSetup,
|
|
// and the whole wait (each call in it) by one lease validity plus slack.
|
|
// A stalled master ends the release unconfirmed; the unit still stops.
|
|
setup, cancelSetup := context.WithTimeout(ctx, volumeReleaseSetup)
|
|
defer cancelSetup()
|
|
ws, err := releaseQuery(setup, master, volume)
|
|
if err != nil {
|
|
return releaseResult{Message: fmt.Sprintf("release not confirmed: the master's lease record could not be read (%v); whether it holds a release is unknown", err)}
|
|
}
|
|
if ws.GetOwnerUnreadable() {
|
|
return releaseResult{Final: true, Message: "not released: owner unknown; the master cannot prove the current authority holder; no revoke was sent"}
|
|
}
|
|
if legacyWorkspaceMaster(ws) {
|
|
return releaseResult{Legacy: true, Final: true, Message: "legacy stop: master predates owner observation; cut and unit stop, no revoke; volume start uses the existing authority path"}
|
|
}
|
|
if volumeHostBinding(ws, volume, server) == nil {
|
|
return releaseResult{Legacy: true, Final: true, Message: "legacy stop: no binding for this host; cut and unit stop, no revoke; volume start uses the existing line"}
|
|
}
|
|
fmt.Fprintln(out, "clean-release mode: this host holds the volume binding; release retains it for the next same-host start")
|
|
var holderEpoch uint64
|
|
if holderRunning {
|
|
var h struct {
|
|
VolumeID, ReplicaID string
|
|
Epoch uint64
|
|
}
|
|
err := getJSON(setup, env, "http://"+e["SWB_STATUS_ADDR"]+"/status?volume="+volume, &h)
|
|
if err == nil && (h.VolumeID != volume || h.ReplicaID != replica || h.Epoch == 0) {
|
|
err = fmt.Errorf("its status names %s/%s@%d", h.VolumeID, h.ReplicaID, h.Epoch)
|
|
}
|
|
if err != nil {
|
|
return releaseResult{Message: fmt.Sprintf("release not confirmed: the running holder's status could not be read (%v), so its epoch to revoke is unknown; whether the lease is released is unknown", err)}
|
|
}
|
|
holderEpoch = h.Epoch
|
|
}
|
|
if r, done := leaseVerdict(ws, volume, replica, server, holderEpoch); done {
|
|
return r
|
|
}
|
|
if !holderRunning {
|
|
next := "the line was never revoked; volume start uses the existing line"
|
|
if ws.GetLease().GetRevoking() {
|
|
next = "the line is revoked without a recorded release; volume start checks ownership and refuses, with no force advice for an unknown or different owner"
|
|
}
|
|
return releaseResult{Final: true, Message: fmt.Sprintf("not released: the unit is stopped and its holder did not release %s's lease; %s", volume, next)}
|
|
}
|
|
fmt.Fprintln(out, fmt.Sprintf("+ release %s/%s@%d's lease (revoke, then wait for the holder's recorded release)", server, replica, holderEpoch))
|
|
lease := ws.GetLease()
|
|
if !lease.GetRevoking() {
|
|
lease, err = workspaceRevoke(setup, master, &control.WorkspaceIdentity{VolumeId: volume, ReplicaId: replica, Epoch: holderEpoch, ServerId: server})
|
|
if err != nil {
|
|
return releaseResult{Message: fmt.Sprintf("release not confirmed: the revoke was refused or not answered (%v); a later volume stop reads the master's record", err)}
|
|
}
|
|
}
|
|
bound := time.Duration(lease.GetValidForNs()) + volumeReleaseSlack
|
|
wait, cancelWait := context.WithTimeout(ctx, bound)
|
|
defer cancelWait()
|
|
for {
|
|
ws, err = releaseQuery(wait, master, volume)
|
|
if err == nil {
|
|
if r, done := leaseVerdict(ws, volume, replica, server, holderEpoch); done {
|
|
return r
|
|
}
|
|
}
|
|
if wait.Err() != nil {
|
|
return releaseResult{Message: fmt.Sprintf("release not confirmed: the master showed no recorded release of %s@%d within one lease (%s); a later volume stop reads the record again", replica, holderEpoch, bound)}
|
|
}
|
|
select {
|
|
case <-wait.Done():
|
|
case <-time.After(env.Poll):
|
|
}
|
|
}
|
|
}
|
|
|
|
// A binding selects the CLI stop mode, never the current authority owner.
|
|
func volumeHostBinding(ws *control.WorkspaceStatus, volume, server string) *control.WorkspaceBinding {
|
|
for _, b := range ws.GetBindings() {
|
|
if b.GetVolumeId() == volume && b.GetServerId() == server && !b.GetRetired() {
|
|
return b
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Modern masters return a holder, positive first-launch proof, or an
|
|
// unreadable-owner marker. Absence of all three identifies the old protocol.
|
|
func legacyWorkspaceMaster(ws *control.WorkspaceStatus) bool {
|
|
return ws != nil && ws.GetCurrentHolder() == nil && !ws.GetNeverPublished() && !ws.GetOwnerUnreadable()
|
|
}
|