Files
seaweedfs/weed/shell/command_volume_merge.go
T
Chris LuandClaude Opus 5.5 c8cd1a7d77 volume: refuse a batch delete while a replica is offline (#12311)
* volume: refuse a batch delete while a replica is offline

BatchDelete now applies the replica check the HTTP delete path already
uses: when the master lists fewer replicas than the volume's copy count,
the delete is refused with 503 instead of applied to the replicas that
remain. The caller retries, so the replica that was offline still gets
the delete once it returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: retry deletes refused for an offline replica

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rust): refuse a batch delete while a replica is offline

Mirrors the Go volume server: batch_delete checks that the master lists
every replica of a replicated volume before deleting, and answers 503
with the replica count error otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: move exhausted deletion retries to dead letter queues

A deletion that still fails after the backed-off retries is no longer
dropped. It moves to a dead letter queue retried after 24 hours, then to a
second one retried after 48 hours, and is dropped only if that fails too.
The id stays in the durable deletion ledger the whole time, so a restart
replays it instead of losing it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume: report a low-disk volume as read-only but deletable

A volume is read-only while its disk is low on space, but it still
applies deletes; only noWriteOrDelete refuses them. The heartbeat set
readOnlyCanDelete from the operator mark alone, so the master listed a
low-disk replica as refusing deletes and the replica count check
rejected every delete on its volumes, on the healthy replicas too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rust): report a low-disk volume as read-only but deletable

Mirrors the Go heartbeat: readOnlyCanDelete is set whenever the volume
is read-only without no_write_or_delete, which includes low disk space.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: retry deletes a low-disk replica could not record

A low-disk volume now counts as deletable, but appending the tombstone
can still fail with no space left on device. Treat that as retryable so
the deletion stays queued instead of being dropped after the other
replicas applied it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* master: keep under-replicated volumes out of empty-volume cleanup

The cleanup only looked at the copies the master lists. With a replica
offline the listed copies can be empty and quiet while the missing one
still holds data, and deleting them would leave that copy as the only
one when it returns. Low-disk replicas now report readOnlyCanDelete and
pass the eligibility check, which made this reachable for more volumes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* master: finish a partly deleted empty volume in the next sweep

When one copy's VolumeDelete succeeded and another failed, the vid was
left with fewer copies than its replica placement, so every later sweep
skipped it as under-replicated and the empty copy kept its slot. The vid
now stays marked deleting after a partial delete, and the next sweep
finishes it. The mark is cleared once the vid is gone or stops being an
empty, quiet candidate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* master: keep the cleanup mark while a survivor is only read-only

A partly deleted empty volume lost its deleting mark when the next sweep
found a surviving copy read-only. Later sweeps then skipped the vid as
under-replicated, so the empty copy kept its slot after it became
deletable again. Only a copy that gained data or was written recently
clears the mark now; a read-only or missing copy just postpones the
cleanup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: delete an empty volume only when every copy is empty

volume.deleteEmpty judged each copy on its own, so it removed an empty
copy while another copy still held data or was offline, leaving that
one as the volume's only copy. Low-disk copies now report
readOnlyCanDelete and passed the check as well. Apply the master
sweep's rule: every copy is listed and every copy is empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: name the copies a partial empty-volume delete leaves behind

Once some copies of a volume are deleted, a rerun of volume.deleteEmpty
skips the rest as under-replicated, and the shell keeps no state to
finish them. Log each copy whose delete failed with the volume.delete
command that removes it once it is empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: keep the gRPC port in the printed volume.delete command

ServerAddress.String() drops a custom gRPC port, so the printed -node
would dial the HTTP port plus 10000. Print the full address.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: let manifest retries pass through the dead letter queues

The manifest retry queue reuses DeletionRetryQueue, so a manifest that
does not resolve now waits through both dead letter queues before it is
deleted alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rust): look up a batch delete's replicas over the master's gRPC port

lookup_volume now dials the master's gRPC port, as Go's LookupVolumeIds
does; the replica check and its test follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume: let a batch delete through on a volume grown degraded

A volume the master grew with fewer copies than its placement reports
min_writable_copies in the lookup. Go's replica check already uses it;
Rust now does too, so deletes on such a volume are not refused forever.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume, shell: let volume.merge write deletes into its merged copy

volume.merge marks the source copies read-only and writes the merged
records, deletes included, into a temporary copy that replaces them. The
batch delete replica check counts only the temporary copy there and
refused every delete, so the merge stopped at its first tombstone.
BatchDeleteRequest.skip_replica_check lets a caller that updates the
other copies itself skip the check; volume.merge sets it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 13:14:33 -08:00

676 lines
23 KiB
Go

package shell
import (
"bytes"
"container/heap"
"context"
"flag"
"fmt"
"io"
"net/http"
"sync"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/operation"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
"google.golang.org/grpc"
)
// mergeIdleTimeoutSeconds is the timeout for idle streams during needle tailing.
// This ensures that slow or stalled streams don't block the merge indefinitely.
// Set to 5 seconds to handle network congestion and avoid premature stream termination.
// Can be made configurable in the future if needed for different deployment scenarios.
const mergeIdleTimeoutSeconds = 5
// mergeDeduplicationWindowNs defines the time window for deduplication across replicas.
// Since the same needle ID can have different timestamps on different servers due to
// clock skew and replication lag, we deduplicate needles with the same ID within this window.
// Set to 5 seconds in nanoseconds to handle typical server clock differences.
const mergeDeduplicationWindowNs = 5 * time.Second
func init() {
Commands = append(Commands, &commandVolumeMerge{})
}
type commandVolumeMerge struct{}
func (c *commandVolumeMerge) Name() string {
return "volume.merge"
}
func (c *commandVolumeMerge) Help() string {
return `merge replicas for a volume id in timestamp order into a fresh copy
volume.merge -volumeId <volume id>
This command:
1) marks the volume readonly on replicas (if not already)
2) allocates a temporary copy on a third location
3) merges replicas in append timestamp order, skipping duplicates
4) verifies the merged copy is not short before touching the replicas
5) replaces the original replicas with the merged volume
6) restores writable state if it was writable before
`
}
func (c *commandVolumeMerge) HasTag(CommandTag) bool {
return false
}
func (c *commandVolumeMerge) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
mergeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
volumeIdInt := mergeCommand.Int("volumeId", 0, "the volume id")
targetNodeStr := mergeCommand.String("target", "", "optional target volume server <host>:<port> for temporary merge output")
noLock := mergeCommand.Bool("noLock", false, "do not lock the admin shell at one's own risk")
if err = mergeCommand.Parse(args); err != nil {
return err
}
if *volumeIdInt == 0 {
return fmt.Errorf("volumeId is required")
}
if *noLock {
commandEnv.noLock = true
} else if err = commandEnv.confirmIsLocked(args); err != nil {
return err
}
volumeId := needle.VolumeId(*volumeIdInt)
topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
if err != nil {
return err
}
volumeReplicas, allLocations := collectVolumeReplicaLocations(topologyInfo)
replicas := volumeReplicas[uint32(volumeId)]
if len(replicas) < 2 {
return fmt.Errorf("volume %d has %d replica(s); merge requires at least two", volumeId, len(replicas))
}
if len(replicas) > mergeMaxReplicas {
return fmt.Errorf("volume %d has %d replicas; merge supports at most %d", volumeId, len(replicas), mergeMaxReplicas)
}
volumeInfo := replicas[0].info
replicaPlacement, err := super_block.NewReplicaPlacementFromByte(byte(volumeInfo.ReplicaPlacement))
if err != nil {
return fmt.Errorf("parse replica placement for volume %d: %w", volumeId, err)
}
var targetServer pb.ServerAddress
if *targetNodeStr != "" {
targetServer = pb.ServerAddress(*targetNodeStr)
if isReplicaServer(targetServer, replicas) {
return fmt.Errorf("target %s already hosts volume %d", *targetNodeStr, volumeId)
}
if err = allocateMergeVolume(commandEnv.option.GrpcDialOption, targetServer, volumeInfo, replicaPlacement); err != nil {
return err
}
} else {
targetServer, err = allocateMergeVolumeOnThirdLocation(commandEnv.option.GrpcDialOption, allLocations, replicas, volumeInfo, replicaPlacement)
if err != nil {
return err
}
}
cleanupTarget := true
defer func() {
if !cleanupTarget {
return
}
if delErr := deleteVolume(context.Background(), commandEnv.option.GrpcDialOption, volumeId, targetServer, false, false, false); delErr != nil {
glog.Warningf("failed to clean up temporary merge volume %d on %s: %v", volumeId, targetServer, delErr)
}
}()
writableReplicaIndices, err := ensureVolumeReadonly(commandEnv, replicas)
if err != nil {
return err
}
if len(writableReplicaIndices) > 0 {
defer func() {
// Only restore writable state for replicas that were originally writable
writableReplicas := make([]*VolumeReplica, 0, len(writableReplicaIndices))
for _, idx := range writableReplicaIndices {
writableReplicas = append(writableReplicas, replicas[idx])
}
if restoreErr := markReplicasWritable(commandEnv.option.GrpcDialOption, writableReplicas, true, false); restoreErr != nil {
glog.Warningf("failed to restore writable state for volume %d: %v", volumeId, restoreErr)
}
}()
}
done := make(chan struct{})
defer close(done)
keys := make(mergedKeys)
histories := make([]*keyHistoryStream, 0, len(replicas))
sources := make([]needleStream, 0, len(replicas))
for i, replica := range replicas {
server := pb.NewServerAddressFromDataNode(replica.location.dataNode)
history := &keyHistoryStream{needleStream: startTailNeedleStream(commandEnv.option.GrpcDialOption, volumeId, server, done), bit: 1 << i, keys: keys}
histories = append(histories, history)
sources = append(sources, history)
}
mergeErr := operation.WithVolumeServerClient(false, targetServer, commandEnv.option.GrpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
version := needle.Version(volumeInfo.Version)
if version == 0 {
version = needle.GetCurrentVersion()
}
return mergeNeedleStreams(sources, func(streamIndex int, n *needle.Needle) error {
return keys.writeMergedNeedle(client, volumeId, version, n, histories[streamIndex].hadKey)
})
})
if mergeErr != nil {
return mergeErr
}
// Verify the merged copy before overwriting any replica. A short or empty
// merge stamped over every replica at once is unrecoverable; on failure the
// originals are left intact and the bad merged copy is cleaned up.
if err = verifyMergedVolume(commandEnv.option.GrpcDialOption, volumeId, targetServer, replicas, keys.deletedCount()); err != nil {
return err
}
// The merged copy is now the only authoritative copy. Keep it until every
// replica is rebuilt from it so a mid-loop failure can still be finished.
cleanupTarget = false
for i, replica := range replicas {
sourceServer := pb.NewServerAddressFromDataNode(replica.location.dataNode)
if _, err = copyVolume(context.Background(), commandEnv.option.GrpcDialOption, writer, volumeId, targetServer, sourceServer, "", 0, false); err != nil {
return fmt.Errorf("rebuild replica %d/%d on %s from merged volume %d: %w; merged copy kept on %s, re-run to finish", i+1, len(replicas), sourceServer, volumeId, err, targetServer)
}
}
if err = deleteVolume(context.Background(), commandEnv.option.GrpcDialOption, volumeId, targetServer, false, false, false); err != nil {
return err
}
fmt.Fprintf(writer, "merged volume %d from %d replicas via %s\n", volumeId, len(replicas), targetServer)
return nil
}
// mergedKeys holds, per key, a bit for each source stream that has returned a
// record for it, and mergedKeyDeleted once the merged copy holds it deleted.
type mergedKeys map[types.NeedleId]uint64
const mergeMaxReplicas = 63
const mergedKeyDeleted = uint64(1) << mergeMaxReplicas
// keyHistoryStream records the keys its source returns. hadKey reports whether
// the key of the record returned last appeared earlier in the same source.
type keyHistoryStream struct {
needleStream
bit uint64
keys mergedKeys
hadKey bool
}
func (s *keyHistoryStream) Next() (*needle.Needle, bool) {
n, ok := s.needleStream.Next()
if ok {
s.hadKey = s.keys[n.Id]&s.bit != 0
s.keys[n.Id] |= s.bit
}
return n, ok
}
// writeMergedNeedle copies one merged record to the target. A delete and an
// empty write both arrive as a size-0 record; it is a delete when its source
// already had a record for the key, and an empty write otherwise. A size-0
// record never revives a key the merged copy already deleted.
func (keys mergedKeys) writeMergedNeedle(client volume_server_pb.VolumeServerClient, volumeId needle.VolumeId, version needle.Version, n *needle.Needle, sourceHadKey bool) error {
if n.Size == 0 && keys[n.Id]&mergedKeyDeleted != 0 {
return nil
}
if n.Size == 0 && sourceHadKey {
fid := needle.NewFileId(volumeId, uint64(n.Id), 0).String()
// the source copies are read-only and get replaced by this copy
resp, err := client.BatchDelete(context.Background(), &volume_server_pb.BatchDeleteRequest{
FileIds: []string{fid},
SkipCookieCheck: true,
SkipReplicaCheck: true,
})
if err != nil {
return fmt.Errorf("delete merged needle %s: %w", fid, err)
}
for _, result := range resp.Results {
if result.Status != http.StatusAccepted && result.Status != http.StatusNotModified {
return fmt.Errorf("delete merged needle %s: status %d: %s", fid, result.Status, result.Error)
}
}
keys[n.Id] |= mergedKeyDeleted
return nil
}
blob, size, err := needleBlobFromNeedle(n, version)
if err != nil {
return err
}
if _, err = client.WriteNeedleBlob(context.Background(), &volume_server_pb.WriteNeedleBlobRequest{
VolumeId: uint32(volumeId),
NeedleId: uint64(n.Id),
Size: int32(size),
NeedleBlob: blob,
}); err != nil {
return err
}
keys[n.Id] &^= mergedKeyDeleted
return nil
}
func (keys mergedKeys) deletedCount() (count uint64) {
for _, state := range keys {
if state&mergedKeyDeleted != 0 {
count++
}
}
return count
}
type needleStream interface {
Next() (*needle.Needle, bool)
Err() error
}
type tailNeedleStream struct {
ch <-chan *needle.Needle
errMu sync.Mutex
err error
}
func (s *tailNeedleStream) Next() (*needle.Needle, bool) {
n, ok := <-s.ch
return n, ok
}
func (s *tailNeedleStream) Err() error {
s.errMu.Lock()
defer s.errMu.Unlock()
return s.err
}
func (s *tailNeedleStream) setErr(err error) {
s.errMu.Lock()
s.err = err
s.errMu.Unlock()
}
func startTailNeedleStream(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, server pb.ServerAddress, done <-chan struct{}) *tailNeedleStream {
ch := make(chan *needle.Needle, 32)
stream := &tailNeedleStream{ch: ch}
go func() {
err := operation.TailVolumeFromSource(server, volumeId, 0, mergeIdleTimeoutSeconds, func(n *needle.Needle) error {
select {
case ch <- n:
case <-done:
return fmt.Errorf("merge cancelled")
}
return nil
}, grpcDialOption)
close(ch)
stream.setErr(err)
}()
return stream
}
type needleMergeItem struct {
streamIndex int
needle *needle.Needle
timestamp uint64
}
type needleMergeHeap []needleMergeItem
func (h needleMergeHeap) Len() int { return len(h) }
func (h needleMergeHeap) Less(i, j int) bool {
if h[i].timestamp == h[j].timestamp {
return h[i].needle.Id < h[j].needle.Id
}
return h[i].timestamp < h[j].timestamp
}
func (h needleMergeHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *needleMergeHeap) Push(x any) {
*h = append(*h, x.(needleMergeItem))
}
func (h *needleMergeHeap) Pop() any {
old := *h
n := len(old)
item := old[n-1]
*h = old[:n-1]
return item
}
func mergeNeedleStreams(streams []needleStream, consume func(int, *needle.Needle) error) error {
h := &needleMergeHeap{}
heap.Init(h)
for i, stream := range streams {
if n, ok := stream.Next(); ok {
heap.Push(h, needleMergeItem{streamIndex: i, needle: n, timestamp: needleTimestamp(n)})
}
}
// Track seen needle IDs (by stream) within a time window to skip cross-stream duplicates only.
// Needles with the same ID from different streams within mergeDeduplicationWindowNs are considered
// cross-stream duplicates and skipped. Same-stream duplicates (overwrites) are kept.
// Map: needleId -> streamIndex that first processed it in this window
seenAtTimestamp := make(map[types.NeedleId]int)
var windowStartTimestamp uint64
windowInitialized := false
for h.Len() > 0 {
item := heap.Pop(h).(needleMergeItem)
ts := item.timestamp
n := item.needle
// Initialize window on first timestamp, or move to new window when outside current window
if !windowInitialized {
windowStartTimestamp = ts
windowInitialized = true
} else if ts > windowStartTimestamp+uint64(mergeDeduplicationWindowNs) {
// Moving to a new window: clear the watermark to reduce memory usage.
// This is safe because we only skip duplicates within the same time window.
seenAtTimestamp = make(map[types.NeedleId]int)
windowStartTimestamp = ts
}
// Skip cross-stream duplicates: if we've already seen this needle ID from a DIFFERENT stream
// within this time window, skip it. Same-stream duplicates (overwrites) are kept.
if seenStreamIdx, exists := seenAtTimestamp[n.Id]; exists && seenStreamIdx != item.streamIndex {
// Cross-stream duplicate from different stream - skip this occurrence
if nextN, ok := streams[item.streamIndex].Next(); ok {
heap.Push(h, needleMergeItem{streamIndex: item.streamIndex, needle: nextN, timestamp: needleTimestamp(nextN)})
}
continue
}
// Record this stream's occurrence of this needle ID in this window
// (overwrite if from same stream, since we process in timestamp order)
seenAtTimestamp[n.Id] = item.streamIndex
if err := consume(item.streamIndex, n); err != nil {
return err
}
if nextN, ok := streams[item.streamIndex].Next(); ok {
heap.Push(h, needleMergeItem{streamIndex: item.streamIndex, needle: nextN, timestamp: needleTimestamp(nextN)})
}
}
for _, stream := range streams {
if err := stream.Err(); err != nil {
return err
}
}
return nil
}
func needleTimestamp(n *needle.Needle) uint64 {
if n.AppendAtNs != 0 {
return n.AppendAtNs
}
if n.LastModified != 0 {
return uint64(time.Unix(int64(n.LastModified), 0).UnixNano())
}
return 0
}
// memoryBackendFile implements backend.BackendStorageFile using an in-memory buffer
type memoryBackendFile struct {
buf *bytes.Buffer
}
func (m *memoryBackendFile) ReadAt(p []byte, off int64) (n int, err error) {
data := m.buf.Bytes()
if off >= int64(len(data)) {
return 0, io.EOF
}
n = copy(p, data[off:])
if off+int64(n) < int64(len(data)) {
return n, nil
}
return n, io.EOF
}
func (m *memoryBackendFile) WriteAt(p []byte, off int64) (n int, err error) {
data := m.buf.Bytes()
if off > int64(len(data)) {
// Pad with zeros
m.buf.Write(make([]byte, off-int64(len(data))))
// Refresh data snapshot after padding to see the padded length
data = m.buf.Bytes()
}
if off == int64(len(data)) {
return m.buf.Write(p)
}
// Overwrite existing data: preserve any trailing bytes beyond the write range
newLen := off + int64(len(p))
if newLen < int64(len(data)) {
newLen = int64(len(data))
}
newData := make([]byte, newLen)
copy(newData, data)
copy(newData[off:], p)
m.buf = bytes.NewBuffer(newData)
return len(p), nil
}
func (m *memoryBackendFile) Truncate(off int64) error {
data := m.buf.Bytes()
if off > int64(len(data)) {
m.buf.Write(make([]byte, off-int64(len(data))))
} else {
m.buf = bytes.NewBuffer(data[:off])
}
return nil
}
func (m *memoryBackendFile) Close() error {
return nil
}
func (m *memoryBackendFile) GetStat() (datSize int64, modTime time.Time, err error) {
return int64(m.buf.Len()), time.Now(), nil
}
func (m *memoryBackendFile) Name() string {
return "memory"
}
func (m *memoryBackendFile) Sync() error {
return nil
}
func newMemoryBackendFile() *memoryBackendFile {
return &memoryBackendFile{
buf: &bytes.Buffer{},
}
}
func needleBlobFromNeedle(n *needle.Needle, version needle.Version) ([]byte, types.Size, error) {
// Use in-memory buffer for serialization to avoid expensive temporary file I/O
memFile := newMemoryBackendFile()
defer memFile.Close()
// Append reports Size(n.DataSize); the .dat header and the needle map both use
// n.Size, which Append fills in as it serializes.
_, _, actualSize, err := n.Append(memFile, version)
if err != nil {
return nil, 0, err
}
buf := make([]byte, actualSize)
read, err := memFile.ReadAt(buf, 0)
if err != nil && err != io.EOF {
return nil, 0, err
}
return buf[:read], n.Size, nil
}
func allocateMergeVolumeOnThirdLocation(grpcDialOption grpc.DialOption, allLocations []location, replicas []*VolumeReplica, info *master_pb.VolumeInformationMessage, replicaPlacement *super_block.ReplicaPlacement) (pb.ServerAddress, error) {
replicaNodes := map[string]struct{}{}
for _, replica := range replicas {
replicaNodes[replica.location.dataNode.Id] = struct{}{}
}
for _, loc := range allLocations {
if _, exists := replicaNodes[loc.dataNode.Id]; exists {
continue
}
if !locationHasDiskType(loc, info.DiskType) {
continue
}
server := pb.NewServerAddressFromDataNode(loc.dataNode)
if err := allocateMergeVolume(grpcDialOption, server, info, replicaPlacement); err != nil {
glog.V(1).Infof("failed to allocate merge volume on %s with replication %s: %v", server, replicaPlacement.String(), err)
continue
}
return server, nil
}
return "", fmt.Errorf("no third location available to merge volume %d", info.Id)
}
func allocateMergeVolume(grpcDialOption grpc.DialOption, server pb.ServerAddress, info *master_pb.VolumeInformationMessage, replicaPlacement *super_block.ReplicaPlacement) error {
return operation.WithVolumeServerClient(false, server, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
_, err := client.AllocateVolume(context.Background(), &volume_server_pb.AllocateVolumeRequest{
VolumeId: info.Id,
Collection: info.Collection,
Preallocate: 0,
Replication: replicaPlacement.String(),
Ttl: needle.LoadTTLFromUint32(info.Ttl).String(),
DiskType: info.DiskType,
Version: info.Version,
})
return err
})
}
// ensureVolumeReadonly marks all replicas as readonly and returns the indices of replicas that were writable
func ensureVolumeReadonly(commandEnv *CommandEnv, replicas []*VolumeReplica) ([]int, error) {
var writableReplicaIndices []int
for i, replica := range replicas {
server := pb.NewServerAddressFromDataNode(replica.location.dataNode)
status, err := readVolumeStatus(commandEnv.option.GrpcDialOption, server, needle.VolumeId(replica.info.Id))
if err != nil {
return nil, err
}
if !status.IsReadOnly {
writableReplicaIndices = append(writableReplicaIndices, i)
}
}
if len(writableReplicaIndices) > 0 {
if err := markReplicasWritable(commandEnv.option.GrpcDialOption, replicas, false, false); err != nil {
return nil, err
}
}
return writableReplicaIndices, nil
}
// verifyMergedVolume checks the freshly merged copy is at least as complete as the
// most complete source replica before the originals are overwritten.
func verifyMergedVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, targetServer pb.ServerAddress, replicas []*VolumeReplica, mergedDeleted uint64) error {
merged, err := readVolumeStatus(grpcDialOption, targetServer, volumeId)
if err != nil {
return fmt.Errorf("read merged volume %d on %s: %w", volumeId, targetServer, err)
}
replicaLive := make(map[pb.ServerAddress]uint64, len(replicas))
for _, replica := range replicas {
server := pb.NewServerAddressFromDataNode(replica.location.dataNode)
status, err := readVolumeStatus(grpcDialOption, server, volumeId)
if err != nil {
return fmt.Errorf("read replica volume %d on %s: %w", volumeId, server, err)
}
replicaLive[server] = liveNeedleCount(status)
}
return evaluateMergedVolume(volumeId, liveNeedleCount(merged), mergedDeleted, replicaLive)
}
// evaluateMergedVolume reports why a merged copy with mergedLive live needles is
// unsafe to overwrite replicas whose live counts are replicaLive. The merge is the
// union of every replica's needles, so the merged copy must be non-empty, and every
// key live on the most complete replica must be live or deleted in it: mergedLive
// plus the mergedDeleted keys the merge deleted is at least that replica's count.
func evaluateMergedVolume(volumeId needle.VolumeId, mergedLive, mergedDeleted uint64, replicaLive map[pb.ServerAddress]uint64) error {
if mergedLive == 0 {
return fmt.Errorf("merged volume %d is empty; keeping original replicas", volumeId)
}
var maxLive uint64
var maxFrom pb.ServerAddress
for server, live := range replicaLive {
if live > maxLive {
maxLive, maxFrom = live, server
}
}
if mergedLive+mergedDeleted < maxLive {
return fmt.Errorf("merged volume %d has %d live and %d deleted needles, fewer than replica %s with %d live; refusing to overwrite replicas", volumeId, mergedLive, mergedDeleted, maxFrom, maxLive)
}
return nil
}
func liveNeedleCount(status *volume_server_pb.VolumeStatusResponse) uint64 {
if status == nil || status.FileCount < status.FileDeletedCount {
return 0
}
return status.FileCount - status.FileDeletedCount
}
func readVolumeStatus(grpcDialOption grpc.DialOption, server pb.ServerAddress, volumeId needle.VolumeId) (*volume_server_pb.VolumeStatusResponse, error) {
var resp *volume_server_pb.VolumeStatusResponse
err := operation.WithVolumeServerClient(false, server, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
r, statusErr := client.VolumeStatus(context.Background(), &volume_server_pb.VolumeStatusRequest{VolumeId: uint32(volumeId)})
if statusErr != nil {
return statusErr
}
resp = r
return nil
})
if err == nil && resp == nil {
return nil, fmt.Errorf("empty volume status response from %s", server)
}
return resp, err
}
func isReplicaServer(target pb.ServerAddress, replicas []*VolumeReplica) bool {
for _, replica := range replicas {
if pb.NewServerAddressFromDataNode(replica.location.dataNode).Equals(target) {
return true
}
}
return false
}
func locationHasDiskType(loc location, diskType string) bool {
for _, diskInfo := range loc.dataNode.DiskInfos {
if diskInfo.Type == diskType {
return true
}
}
return false
}
func markReplicasWritable(grpcDialOption grpc.DialOption, replicas []*VolumeReplica, writable bool, persist bool) error {
for _, replica := range replicas {
server := pb.NewServerAddressFromDataNode(replica.location.dataNode)
err := operation.WithVolumeServerClient(false, server, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
if writable {
_, err := client.VolumeMarkWritable(context.Background(), &volume_server_pb.VolumeMarkWritableRequest{VolumeId: replica.info.Id})
return err
}
_, err := client.VolumeMarkReadonly(context.Background(), &volume_server_pb.VolumeMarkReadonlyRequest{VolumeId: replica.info.Id, Persist: persist})
return err
})
if err != nil {
return err
}
}
return nil
}