mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-18 17:55:26 +01:00
* ec: mount shards against the index .vif when the local one has no EC config A worker encode can send shards to a disk that still holds the source volume's .vif while the encode's .vif lands with the index on a sibling disk. The shard disk mounted through the source's config-free .vif, so it reported encode time 0 and fell back to the sidecar layout, and reads carrying the encode time were rejected as a different encode run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): mount shards against the index .vif when the local one has no EC config Mirrors the Go mount: a data-dir .vif that records no EC config yields to the idx dir's when that one records it, and load_vif_info_across_dirs reports the directory the .vif actually came from. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * storage: do not mirror EC sidecars onto a disk whose .vif has no EC config Startup mirroring copied .ecx/.ecj next to a source volume's leftover config-free .vif and kept that .vif, so after a restart the shard disk mounted self-contained with encode time 0. Leave such a disk on the cross-disk mount, which reads the owner's .vif. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): do not mirror EC sidecars onto a disk whose .vif has no EC config Mirrors the Go startup mirror: a shard disk holding a source volume's config-free .vif stays on the cross-disk mount, which reads the owner's .vif, instead of mounting self-contained with encode time 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ec: take the encode .vif from a sibling data dir when the index dir is shared With a separate -dir.idx the .ecx is shared by every disk, but the encode's .vif lands in the data dir of the disk that received it. A disk still holding a source volume's config-free .vif mounted through it at generation 0. NewEcVolume now accepts the server's data dirs and takes a sibling's EC .vif when the index is not local and every sibling EC .vif names the same run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * storage: give EC mounts the data dir of every disk on the server NewStore records every data dir on each DiskLocation before loading, so the startup scan, cross-disk reconcile and MountEcShards all pass them to NewEcVolume. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): take the encode .vif from a sibling data dir when the index dir is shared Mirrors the Go mount: EcVolume::new_with_vif_dirs takes a sibling's EC .vif when this disk's own records no EC config, the index is not local, and every sibling EC .vif names the same run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): give EC mounts the data dir of every disk on the server main sets Store.store_dirs before the first add_location, so a disk loaded before its siblings still sees them; each DiskLocation passes them to EcVolume::new_with_vif_dirs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * storage: size startup EC validation by the sibling encode .vif With a shared -dir.idx, a disk that still holds the source .dat and its config-free .vif read the default data-shard count, judged custom-ratio shards an interrupted encode, and removed them along with the shared .ecx before the mount could take the sibling's .vif. The startup lookup now falls back to the same sibling .vif the mount uses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): size startup EC validation by the sibling encode .vif Mirrors the Go change: validate_ec_volume falls back to the sibling data dir's encode .vif before judging shards beside a source .dat. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * storage: keep EC shards when sibling .vif files disagree on the encode SiblingEcConfigVif reports when a sibling .vif cannot be read or the siblings name different encode runs. The startup data-shard lookup then returns 0 instead of the default ratio, and validateEcVolume keeps the files rather than sizing custom-ratio shards by a ratio they do not use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): keep EC shards when sibling .vif files disagree on the encode Mirrors the Go change: sibling_ec_config_vif returns Err for an unreadable sibling .vif or disagreeing runs, ec_data_shards_from_vif returns 0 then, and validate_ec_volume keeps the files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * storage: release the opened EC shard when its volume cannot be built Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
189 lines
5.9 KiB
Go
189 lines
5.9 KiB
Go
package storage
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
)
|
|
|
|
// Listed in the order NewEcVolume opens them.
|
|
var ecMirroredSidecars = []string{".ecx", ".ecj", ".vif"}
|
|
|
|
// mirrorEcMetadataToShardDisks physically copies .ecx / .ecj / .vif
|
|
// onto every disk that holds EC shards but lacks the matching
|
|
// sidecars, so each shard-bearing disk mounts self-contained instead
|
|
// of reaching across to a sibling. Runs before
|
|
// reconcileEcShardsAcrossDisks; the cross-disk virtual mount stays
|
|
// as the fallback when mirroring fails.
|
|
func (s *Store) mirrorEcMetadataToShardDisks() {
|
|
if len(s.Locations) < 2 {
|
|
return
|
|
}
|
|
|
|
ecxOwners := s.indexEcxOwners()
|
|
if len(ecxOwners) == 0 {
|
|
return
|
|
}
|
|
|
|
for _, loc := range s.Locations {
|
|
orphans := loc.collectOrphanEcShards()
|
|
if len(orphans) == 0 {
|
|
continue
|
|
}
|
|
for key := range orphans {
|
|
owner, ok := ecxOwners[key]
|
|
if !ok {
|
|
continue
|
|
}
|
|
if owner.location == loc {
|
|
continue
|
|
}
|
|
if loc.hasAllEcSidecarsLocally(key.collection, key.vid) {
|
|
continue
|
|
}
|
|
copied, err := loc.mirrorEcSidecarsFrom(owner, key.collection, key.vid)
|
|
if err != nil {
|
|
glog.Warningf("ec volume %d (collection=%q): mirror sidecars from %s to %s failed after %d files: %v; cross-disk fallback will handle this volume",
|
|
key.vid, key.collection, owner.location.Directory, loc.Directory, copied, err)
|
|
continue
|
|
}
|
|
if copied > 0 {
|
|
glog.V(0).Infof("ec volume %d (collection=%q): mirrored %d sidecar(s) from %s to %s for same-disk invariant",
|
|
key.vid, key.collection, copied, owner.location.Directory, loc.Directory)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// hasAllEcSidecarsLocally checks both the modern routing and the
|
|
// opposite directory (legacy pre-`-dir.idx` layout) — without the
|
|
// fallback, a destination that still has .ecx in its data dir would
|
|
// be re-mirrored into IdxDirectory.
|
|
func (l *DiskLocation) hasAllEcSidecarsLocally(collection string, vid needle.VolumeId) bool {
|
|
for _, ext := range ecMirroredSidecars {
|
|
if statRegular(l.ecSidecarDestPath(collection, vid, ext)) {
|
|
continue
|
|
}
|
|
if l.IdxDirectory != l.Directory {
|
|
fallbackDir := l.Directory
|
|
if ext == ".vif" {
|
|
fallbackDir = l.IdxDirectory
|
|
}
|
|
if statRegular(erasure_coding.EcShardFileName(collection, fallbackDir, int(vid)) + ext) {
|
|
continue
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func statRegular(path string) bool {
|
|
info, err := os.Stat(path)
|
|
return err == nil && !info.IsDir()
|
|
}
|
|
|
|
// ecSidecarDestPath routes .ecx/.ecj to IdxDirectory and .vif to
|
|
// Directory, matching NewEcVolume's open order.
|
|
func (l *DiskLocation) ecSidecarDestPath(collection string, vid needle.VolumeId, ext string) string {
|
|
if ext == ".vif" {
|
|
return erasure_coding.EcShardFileName(collection, l.Directory, int(vid)) + ext
|
|
}
|
|
return erasure_coding.EcShardFileName(collection, l.IdxDirectory, int(vid)) + ext
|
|
}
|
|
|
|
func (l *DiskLocation) mirrorEcSidecarsFrom(owner ecxOwnerInfo, collection string, vid needle.VolumeId) (int, error) {
|
|
srcIdxBase := erasure_coding.EcShardFileName(collection, owner.idxDir, int(vid))
|
|
srcDataBase := erasure_coding.EcShardFileName(collection, owner.location.Directory, int(vid))
|
|
|
|
// A local .vif with no EC config (a source volume's own) would shadow the
|
|
// owner's once the index is copied next to it; keep mounting across disks.
|
|
if dstVif := l.ecSidecarDestPath(collection, vid, ".vif"); statRegular(dstVif) && !vifIsEcVolume(dstVif) &&
|
|
(vifIsEcVolume(srcIdxBase+".vif") || vifIsEcVolume(srcDataBase+".vif")) {
|
|
glog.V(0).Infof("ec volume %d (collection=%q): %s records no EC config; not mirroring sidecars from %s",
|
|
vid, collection, dstVif, owner.location.Directory)
|
|
return 0, nil
|
|
}
|
|
|
|
copied := 0
|
|
for _, ext := range ecMirroredSidecars {
|
|
dst := l.ecSidecarDestPath(collection, vid, ext)
|
|
// An existing local copy is authoritative — it may be newer
|
|
// than the owner's after a delete journal append.
|
|
if _, err := os.Stat(dst); err == nil {
|
|
continue
|
|
} else if !os.IsNotExist(err) {
|
|
return copied, fmt.Errorf("stat %s: %w", dst, err)
|
|
}
|
|
|
|
var src string
|
|
for _, candidate := range []string{srcIdxBase + ext, srcDataBase + ext} {
|
|
if info, err := os.Stat(candidate); err == nil && !info.IsDir() {
|
|
src = candidate
|
|
break
|
|
}
|
|
}
|
|
if src == "" {
|
|
glog.V(1).Infof("ec volume %d (collection=%q): sidecar %s not found on owner %s; skipping mirror",
|
|
vid, collection, ext, owner.location.Directory)
|
|
continue
|
|
}
|
|
|
|
if err := copyEcSidecarAtomic(src, dst); err != nil {
|
|
return copied, fmt.Errorf("copy %s: %w", ext, err)
|
|
}
|
|
copied++
|
|
}
|
|
return copied, nil
|
|
}
|
|
|
|
// copyEcSidecarAtomic writes to <dst>.mirror.tmp, fsyncs, renames.
|
|
// Crash-safe: a partial write leaves the tmp orphaned and the
|
|
// canonical dst absent so retries recognise the file still needs
|
|
// copying.
|
|
func copyEcSidecarAtomic(src, dst string) error {
|
|
srcFile, err := os.Open(src)
|
|
if err != nil {
|
|
return fmt.Errorf("open source %s: %w", src, err)
|
|
}
|
|
defer srcFile.Close()
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
|
return fmt.Errorf("mkdir %s: %w", filepath.Dir(dst), err)
|
|
}
|
|
|
|
tmpDst := dst + ".mirror.tmp"
|
|
_ = os.Remove(tmpDst)
|
|
dstFile, err := os.OpenFile(tmpDst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
|
|
if err != nil {
|
|
return fmt.Errorf("create %s: %w", tmpDst, err)
|
|
}
|
|
cleanup := func() {
|
|
_ = dstFile.Close()
|
|
_ = os.Remove(tmpDst)
|
|
}
|
|
|
|
if _, err := io.Copy(dstFile, srcFile); err != nil {
|
|
cleanup()
|
|
return fmt.Errorf("copy bytes %s -> %s: %w", src, tmpDst, err)
|
|
}
|
|
if err := dstFile.Sync(); err != nil {
|
|
cleanup()
|
|
return fmt.Errorf("fsync %s: %w", tmpDst, err)
|
|
}
|
|
if err := dstFile.Close(); err != nil {
|
|
_ = os.Remove(tmpDst)
|
|
return fmt.Errorf("close %s: %w", tmpDst, err)
|
|
}
|
|
if err := os.Rename(tmpDst, dst); err != nil {
|
|
_ = os.Remove(tmpDst)
|
|
return fmt.Errorf("rename %s -> %s: %w", tmpDst, dst, err)
|
|
}
|
|
return nil
|
|
}
|