mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* mount: refuse a merge whose local truncate cuts chunks a peer wrote A flush refused by IF_ENTRY_EQUAL replays the handle's truncate onto the stored entry. The truncate was made over the handle's base, so a chunk a peer committed after that base was dropped or clipped silently, losing data the peer had already fsynced. Like a stale whole-file rewrite over a peer's chunk change, it now fails the merge. A base chunk the peer only shortened carries no peer data and is still cut. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep the later mtime and ctime when merging a delayed flush The merge took every attribute the handle changed from the handle, so a flush that waited behind a peer's commit moved mtime and ctime back to the handle's older write. Each time is now merged as one value (seconds and nanoseconds were merged field by field, so a moved second could take the peer's nanoseconds): the handle's time stands unless a peer moved it too and later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: let a truncate merge pass the handle's own committed chunks A flush whose commit answer was lost finds its own chunks in the stored entry while its base is still the older one. Those chunks are the handle's writes, not a peer's, so they do not refuse the merge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: do not keep a stored copy of a chunk the merge re-adds from the handle A stored chunk the base lacks but the handle holds is the handle's own commit whose answer was lost. The merge appends the handle's chunks after the stored ones, so keeping the stored copy referenced the chunk twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: take the manifests of a failed flush's fold for the handle's own chunks A flush can commit its folded manifests and lose the answer. The handle reverts to the member chunks and keeps its old base, so the next merge found the stored manifest, knew neither its file id nor its key, and refused it as a peer write across the handle's truncate. The handle now remembers the folds of flushes that failed over its base, and a merge unfolds them in the stored entry before comparing, so the members match the handle's chunks. A peer's manifest is still refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: take a retried flush's own stored fold for its own chunks The client retries a request whose connection dropped, and the first try may have stored the flush's folded manifests. The retry is then refused over them before the fold is recorded as failed, and the merge took the manifest for a peer's write across the handle's truncate. The merge now unfolds the flush's own fold in the stored entry as well; the condition still names the entry as stored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep every fold the filer may hold instead of the last four Dropping the oldest of four could forget the fold the filer committed, and the merge then refused that manifest again. A fold is now kept only when the flush failed without the filer's answer, or when the refusal lookup shows it stored, so a refused flush no longer adds one, and the list ends with the base. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep a flush's fold when its first request may have been stored A precondition verdict on a retried send does not prove the first send stored nothing, and a failed refusal lookup then hid that it had. The create now counts the sends of the request that carries the fold and keeps the fold whenever the flush fails and that request was sent more than once or never got the filer's answer. That covers both flush paths, so revert is back to only restoring the chunks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: drop the failed folds a refused create shows were not stored A refusal means the stored entry moved off the handle's base, so no request guarded by that base can store another fold. After the refusal lookup the handle keeps only the folds whose manifests the stored entry holds, at most one, which bounds the list over a base. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: bound the failed folds a handle keeps over one base Flushes that keep failing with no filer answer each saved a new fold the filer may hold, so the list grew for as long as the outage lasted. Once a handle keeps maxFailedFolds folds over its base, its flushes send their chunks unfolded for the filer to fold, and no kept fold is dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep the failed folds a refusal lookup does not show The refusal comes from the file's write owner, but the lookup after it can read a filer that lags behind and still shows the base. Pruning the folds against that read forgot a fold the owner had stored, and once its manifest showed up a pending truncate took it for a peer's chunk on every flush. The maxFailedFolds limit bounds the list, so the folds stay until the base moves. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: drop the chunks a failed flush sent that the handle has cut since A flush that commits with its answer lost, then a truncate of the handle, left the next merge with the handle's own committed chunks in the stored entry, outside both base and the handle. The truncate check took them for a peer's writes and refused every later flush over the base. A failed flush now records the file ids of the chunks it sent that base lacks, and the merge drops those the handle no longer holds: it cut or replaced them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: copy the failed flush's sent set with a loop Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep folding past maxFailedFolds instead of sending chunks unfolded The filer folds a flat chunk list itself, into manifests the handle does not know: a lost answer then left one of them across a pending truncate, and an acknowledged one gave the handle flat chunks under a folded base. Flushes fold as before, and the handle stops keeping folds past the limit while it still records the chunks they sent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: read the stored manifests a refused merge cannot name A fold changes no content, but a manifest the handle does not know sat in the stored entry as one chunk: across a pending truncate it refused the merge, though it can hold the handle's own chunks (a fold it did not keep, or one the filer made). When the merge is refused, the flush reads those manifests, replaces them with the chunks they hold and merges again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: hold the chunks the filer stored after a plain acknowledged flush The filer folds a flat chunk list (the mount's own fold failed), and the acknowledgement makes the stored entry the base while the handle kept the flat chunks. A later merge took them for writes of the handle, which can bring back chunks a peer's truncate removed. As after a merge, the handle now takes the stored chunks, keeping those attached since the request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep a bounded record per failed flush instead of a growing set The set of chunk ids failed flushes sent grew with every overwrite while commits failed, and each failure copied it whole. Each failed flush the filer may have stored (a send got no verdict) now keeps one record, its fold and the ids it sent, built once and at most maxFailedFlushes over a base. Past that, a merge refuses the chunks of a flush it did not keep that the handle has cut since, as a peer's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: register the chunks a rebase or install gives the handle for reads A handle that takes the stored chunks after a flush (or a peer's, after a merge) can get manifests the filer made, and resolving them needs the file's read token. rebaseEntry and installRewrittenChunks now record the chunks under the file's path before the read view resolves them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep every chunk id a failed flush sent, cap only the folds Past four failed flushes over one base the handle stopped recording what a later flush sent. If that flush was stored with its answer lost, a truncate of its chunks was refused as a peer's write and kept failing after the filer came back. The ids now go into one set shared by the base's marks, each kept once and never copied, until the base moves. Only the folds stay capped: a stored manifest past the cap is read when a refused merge cannot place it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: fail a flush over a peer's chunk change unless -concurrentWriteMerge A content flush refused because another mount committed first used to merge the handle's writes onto the stored entry. That merge now runs only with the new -concurrentWriteMerge mount option (off by default). By default a stored chunk change that neither the base nor the handle holds fails the flush with EIO, and a peer's chunk event no longer rebases a dirty handle. Attribute-only changes and the handle's own commits (a lost answer, its own truncate) still merge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: recognize the shortened references a failed flush sent A truncate shrinks a base chunk in place, keeping its file id. A failed flush that may have stored such a reference recorded only file ids base lacks, so a later truncate saw its own stored reference as a peer's change and failed with EIO by default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
417 lines
15 KiB
Go
417 lines
15 KiB
Go
//go:build linux || darwin || freebsd || windows
|
|
|
|
package command
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"runtime"
|
|
"runtime/debug"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/reflection"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/mount"
|
|
"github.com/seaweedfs/seaweedfs/weed/mount/meta_cache"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mount_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/seaweedfs/seaweedfs/weed/util/grace"
|
|
)
|
|
|
|
func runMount(cmd *Command, args []string) bool {
|
|
|
|
if *mountOptions.debug {
|
|
go http.ListenAndServe(fmt.Sprintf(":%d", *mountOptions.debugPort), nil)
|
|
}
|
|
|
|
*mountCpuProfile = util.ResolvePath(*mountCpuProfile)
|
|
*mountMemProfile = util.ResolvePath(*mountMemProfile)
|
|
grace.SetupProfiling(*mountCpuProfile, *mountMemProfile)
|
|
if *mountReadRetryTime < time.Second {
|
|
*mountReadRetryTime = time.Second
|
|
}
|
|
util.RetryWaitTime = *mountReadRetryTime
|
|
|
|
// 32 bits, not 64: os.FileMode is uint32, so a wider parse would let a
|
|
// nonsense umask truncate silently instead of being rejected here.
|
|
umask, umaskErr := strconv.ParseUint(*mountOptions.umaskString, 8, 32)
|
|
if umaskErr != nil {
|
|
fmt.Printf("can not parse umask %s", *mountOptions.umaskString)
|
|
return false
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
return false
|
|
}
|
|
|
|
return RunMount(&mountOptions, os.FileMode(umask))
|
|
}
|
|
|
|
func ensureBucketAllowEmptyFolders(ctx context.Context, filerClient filer_pb.FilerClient, mountRoot, bucketRootPath string) error {
|
|
bucketPath, isBucketRootMount := bucketPathForMountRoot(mountRoot, bucketRootPath)
|
|
if !isBucketRootMount {
|
|
return nil
|
|
}
|
|
|
|
bucketFullPath := util.FullPath(bucketPath)
|
|
parent, _ := bucketFullPath.DirAndName()
|
|
|
|
for attempt := 0; attempt < 5; attempt++ {
|
|
entry, _, _, err := filer_pb.GetEntry(ctx, filerClient, util.FullPath(bucketPath))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if entry == nil {
|
|
return fmt.Errorf("bucket %s not found", bucketPath)
|
|
}
|
|
|
|
if value := strings.TrimSpace(string(entry.Extended[s3_constants.ExtAllowEmptyFolders])); value != "" {
|
|
return nil
|
|
}
|
|
|
|
expected := filer_pb.SnapshotExtended(entry.Extended, s3_constants.ExtAllowEmptyFolders)
|
|
if entry.Extended == nil {
|
|
entry.Extended = make(map[string][]byte)
|
|
}
|
|
entry.Extended[s3_constants.ExtAllowEmptyFolders] = []byte("true")
|
|
|
|
err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
_, err := client.UpdateEntry(ctx, &filer_pb.UpdateEntryRequest{
|
|
Directory: parent,
|
|
Entry: entry,
|
|
ExpectedExtended: expected,
|
|
})
|
|
return err
|
|
})
|
|
if err == nil {
|
|
glog.V(3).Infof("RunMount: set bucket %s %s=true", bucketPath, s3_constants.ExtAllowEmptyFolders)
|
|
return nil
|
|
}
|
|
if status.Code(err) != codes.FailedPrecondition {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return fmt.Errorf("bucket %s attributes keep changing", bucketPath)
|
|
}
|
|
|
|
func bucketPathForMountRoot(mountRoot, bucketRootPath string) (string, bool) {
|
|
cleanPath := path.Clean("/" + strings.TrimPrefix(mountRoot, "/"))
|
|
cleanBucketRoot := path.Clean("/" + strings.TrimPrefix(bucketRootPath, "/"))
|
|
if cleanBucketRoot == "/" {
|
|
return "", false
|
|
}
|
|
prefix := cleanBucketRoot + "/"
|
|
if !strings.HasPrefix(cleanPath, prefix) {
|
|
return "", false
|
|
}
|
|
rest := strings.TrimPrefix(cleanPath, prefix)
|
|
|
|
bucketParts := strings.Split(rest, "/")
|
|
if len(bucketParts) != 1 || bucketParts[0] == "" {
|
|
return "", false
|
|
}
|
|
return cleanBucketRoot + "/" + bucketParts[0], true
|
|
}
|
|
|
|
func peerStringOrEmpty(p *string) string {
|
|
if p == nil {
|
|
return ""
|
|
}
|
|
return *p
|
|
}
|
|
|
|
// connectToFiler retries the filer handshake, returning the cluster's cipher
|
|
// setting and bucket root, and adopts the filer's digest.
|
|
func connectToFiler(option *MountOptions) (filerAddresses []pb.ServerAddress, grpcDialOption grpc.DialOption, cipher bool, bucketRootPath string, ok bool) {
|
|
// try to connect to filer
|
|
filerAddresses = pb.ServerAddresses(*option.filer).ToAddresses()
|
|
util.LoadSecurityConfiguration()
|
|
grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
|
|
var err error
|
|
var digest string
|
|
for i := 0; i < 10; i++ {
|
|
err = pb.WithOneOfGrpcFilerClients(false, filerAddresses, grpcDialOption, nil, func(client filer_pb.SeaweedFilerClient) error {
|
|
resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
|
|
if err != nil {
|
|
return fmt.Errorf("get filer grpc address %v configuration: %w", filerAddresses, err)
|
|
}
|
|
cipher = resp.Cipher
|
|
bucketRootPath = resp.DirBuckets
|
|
digest = resp.Digest
|
|
return nil
|
|
})
|
|
if err == nil {
|
|
break
|
|
}
|
|
glog.V(0).Infof("failed to talk to filer %v: %v", filerAddresses, err)
|
|
glog.V(0).Infof("wait for %d seconds ...", i+1)
|
|
time.Sleep(time.Duration(i+1) * time.Second)
|
|
}
|
|
if err != nil {
|
|
glog.Errorf("failed to talk to filer %v: %v", filerAddresses, err)
|
|
return nil, nil, false, "", false
|
|
}
|
|
if err = util.AdoptFilerDigest(*option.filer, digest); err != nil {
|
|
glog.Errorf("%v", err)
|
|
return nil, nil, false, "", false
|
|
}
|
|
if bucketRootPath == "" {
|
|
bucketRootPath = "/buckets"
|
|
}
|
|
return filerAddresses, grpcDialOption, cipher, bucketRootPath, true
|
|
}
|
|
|
|
// fileSystemParams are the pieces of a mount that each platform works out for
|
|
// itself: where it is attached, and whose identity the entries carry.
|
|
type fileSystemParams struct {
|
|
sseS3Reader *mount.SSES3Reader
|
|
dir string
|
|
mountRoot string
|
|
filerAddresses []pb.ServerAddress
|
|
grpcDialOption grpc.DialOption
|
|
cipher bool
|
|
uidGidMapper *meta_cache.UidGidMapper
|
|
uid uint32
|
|
gid uint32
|
|
mountMode os.FileMode
|
|
mountCtime time.Time
|
|
umask os.FileMode
|
|
chunkSizeLimitMB int
|
|
cacheDirForRead string
|
|
cacheDirForWrite string
|
|
|
|
// eagerFilerCreate persists a created file's entry at create time rather
|
|
// than at flush. A platform sets it when it cannot run the flush before
|
|
// the application's close returns, so nothing that reads through the
|
|
// filer can race an unflushed close.
|
|
eagerFilerCreate bool
|
|
}
|
|
|
|
func buildSeaweedFileSystem(option *MountOptions, p fileSystemParams) *mount.WFS {
|
|
readerCacheSizeMB := int64(256)
|
|
if option.readerCacheSizeMB != nil {
|
|
readerCacheSizeMB = *option.readerCacheSizeMB
|
|
}
|
|
return mount.NewSeaweedFileSystem(&mount.Option{
|
|
SSES3Reader: p.sseS3Reader,
|
|
MountDirectory: p.dir,
|
|
FilerAddresses: p.filerAddresses,
|
|
GrpcDialOption: p.grpcDialOption,
|
|
FilerSigningKey: security.SigningKey(util.GetViper().GetString("jwt.filer_signing.key")),
|
|
FilerSigningExpiresAfterSec: util.GetViper().GetInt("jwt.filer_signing.expires_after_seconds"),
|
|
FilerMountRootPath: p.mountRoot,
|
|
VolumeReadAuth: option.volumeReadAuth != nil && *option.volumeReadAuth,
|
|
Collection: *option.collection,
|
|
Replication: *option.replication,
|
|
TtlSec: int32(*option.ttlSec),
|
|
DiskType: types.ToDiskType(*option.diskType),
|
|
ChunkSizeLimit: int64(p.chunkSizeLimitMB) * 1024 * 1024,
|
|
ConcurrentWriters: *option.concurrentWriters,
|
|
ConcurrentReaders: *option.concurrentReaders,
|
|
ReaderCacheSizeMB: readerCacheSizeMB,
|
|
CacheDirForRead: p.cacheDirForRead,
|
|
CacheSizeMBForRead: *option.cacheSizeMBForRead,
|
|
CacheDirForWrite: p.cacheDirForWrite,
|
|
WriteBufferSizeMB: *option.writeBufferSizeMB,
|
|
CacheMetaTTlSec: *option.cacheMetaTtlSec,
|
|
CacheDirMaxEntries: *option.cacheDirMaxEntries,
|
|
DataCenter: *option.dataCenter,
|
|
Quota: int64(*option.collectionQuota) * 1024 * 1024,
|
|
LogicalDiskUsage: *option.logicalDiskUsage,
|
|
MountUid: p.uid,
|
|
MountGid: p.gid,
|
|
MountMode: p.mountMode,
|
|
MountCtime: p.mountCtime,
|
|
MountMtime: time.Now(),
|
|
Umask: p.umask,
|
|
VolumeServerAccess: *mountOptions.volumeServerAccess,
|
|
Cipher: p.cipher,
|
|
UidGidMapper: p.uidGidMapper,
|
|
IncludeSystemEntries: *option.includeSystemEntries,
|
|
DefaultPermissions: *option.defaultPermissions,
|
|
DisableXAttr: *option.disableXAttr,
|
|
IsMacOs: runtime.GOOS == "darwin",
|
|
MetadataFlushSeconds: *option.metadataFlushSeconds,
|
|
DirIdleEvictSec: *option.dirIdleEvictSec,
|
|
EnableDistributedLock: option.distributedLock != nil && *option.distributedLock,
|
|
ConcurrentWriteMerge: option.concurrentWriteMerge != nil && *option.concurrentWriteMerge,
|
|
WritebackCache: option.writebackCache != nil && *option.writebackCache,
|
|
NfsExport: option.nfsExport != nil && *option.nfsExport,
|
|
EagerFilerCreate: p.eagerFilerCreate,
|
|
PosixDirNlink: option.posixDirNlink != nil && *option.posixDirNlink,
|
|
AccessReport: option.accessReport != nil && *option.accessReport,
|
|
AccessReportInterval: *option.accessReportInterval,
|
|
// Peer chunk sharing
|
|
PeerEnabled: option.peerEnabled != nil && *option.peerEnabled,
|
|
PeerListen: peerStringOrEmpty(option.peerListen),
|
|
PeerAdvertise: peerStringOrEmpty(option.peerAdvertise),
|
|
PeerDataCenter: peerStringOrEmpty(option.peerDataCenter),
|
|
PeerRack: peerStringOrEmpty(option.peerRack),
|
|
})
|
|
}
|
|
|
|
// createMountRoot makes the filer-side directory the mount is rooted at.
|
|
func createMountRoot(wfs *mount.WFS, mountRoot, bucketRootPath string, filerAddresses []pb.ServerAddress) bool {
|
|
if err := ensureMountRootDirectory(context.Background(), wfs, mountRoot); err != nil {
|
|
fmt.Printf("failed to create dir %s on filer %s: %v\n", mountRoot, filerAddresses, err)
|
|
return false
|
|
}
|
|
if err := ensureBucketAllowEmptyFolders(context.Background(), wfs, mountRoot, bucketRootPath); err != nil {
|
|
fmt.Printf("failed to set bucket auto-remove-empty-folders policy for %s: %v\n", mountRoot, err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ensureMountRootDirectory creates the mount root only when it is missing. The
|
|
// filer answers a non-exclusive create of an existing path by replacing the
|
|
// entry, keeping only its crtime and inode, so creating the root again at every
|
|
// mount start would reset its mode and owner and drop what only the filer
|
|
// holds: its extended attributes and, when the root is a bucket, the bucket's
|
|
// quota and the settings S3 keeps on it.
|
|
func ensureMountRootDirectory(ctx context.Context, filerClient filer_pb.FilerClient, mountRoot string) error {
|
|
mountRootPath := util.FullPath(mountRoot)
|
|
if mountRootPath == "/" {
|
|
return nil
|
|
}
|
|
entry, _, _, err := filer_pb.GetEntry(ctx, filerClient, mountRootPath)
|
|
if err == nil {
|
|
return mountRootMustBeDirectory(mountRoot, entry)
|
|
}
|
|
if !errors.Is(err, filer_pb.ErrNotFound) {
|
|
return err
|
|
}
|
|
parent, name := mountRootPath.DirAndName()
|
|
if err := filer_pb.MkdirExclusive(ctx, filerClient, parent, name, nil); err != nil {
|
|
// Another client may have created it since the lookup. A filer that
|
|
// predates error codes reports that only as text, so look again rather
|
|
// than match the error.
|
|
entry, _, _, lookupErr := filer_pb.GetEntry(ctx, filerClient, mountRootPath)
|
|
if lookupErr != nil {
|
|
if errors.Is(lookupErr, filer_pb.ErrNotFound) {
|
|
return fmt.Errorf("mount root %s does not exist and could not be created: %w", mountRoot, err)
|
|
}
|
|
return err
|
|
}
|
|
return mountRootMustBeDirectory(mountRoot, entry)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func mountRootMustBeDirectory(mountRoot string, entry *filer_pb.Entry) error {
|
|
if !entry.GetIsDirectory() {
|
|
return fmt.Errorf("%s: %w", mountRoot, filer_pb.ErrExistingIsFile)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// mountSocketCleanup returns a func that removes the control socket this mount
|
|
// listens on, unless a later mount of the same directory has replaced it.
|
|
func mountSocketCleanup(socketPath string) func() {
|
|
listened, err := os.Lstat(socketPath)
|
|
if err != nil {
|
|
return func() {}
|
|
}
|
|
var once sync.Once
|
|
return func() {
|
|
once.Do(func() {
|
|
current, err := os.Lstat(socketPath)
|
|
if err != nil || !os.SameFile(current, listened) {
|
|
return
|
|
}
|
|
if err := os.Remove(socketPath); err != nil && !os.IsNotExist(err) {
|
|
glog.Warningf("remove mount socket %s: %v", socketPath, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// serveMountGrpc exposes the local control socket used by "weed mount.stats".
|
|
func serveMountGrpc(wfs *mount.WFS, listener net.Listener) {
|
|
grpcS := pb.NewGrpcServer()
|
|
mount_pb.RegisterSeaweedMountServer(grpcS, wfs)
|
|
reflection.Register(grpcS)
|
|
go grpcS.Serve(listener)
|
|
}
|
|
|
|
// resolveMountRoot trims the trailing slash the filer path must not carry.
|
|
func resolveMountRoot(filerMountRootPath string) string {
|
|
mountRoot := filerMountRootPath
|
|
if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
|
|
mountRoot = mountRoot[0 : len(mountRoot)-1]
|
|
}
|
|
return mountRoot
|
|
}
|
|
|
|
// resolveCacheDirs falls back to the read cache when no write cache is set.
|
|
func resolveCacheDirs(option *MountOptions) (string, string) {
|
|
cacheDirForRead := util.ResolvePath(*option.cacheDirForRead)
|
|
cacheDirForWrite := util.ResolvePath(*option.cacheDirForWrite)
|
|
if cacheDirForWrite == "" {
|
|
cacheDirForWrite = cacheDirForRead
|
|
}
|
|
return cacheDirForRead, cacheDirForWrite
|
|
}
|
|
|
|
// volumeName labels the mount where the platform shows one, in Finder and in
|
|
// Explorer. An explicit override always wins; failing that, the mounted path
|
|
// names the disk, then the mount point; the filer address, which every mount
|
|
// from one filer shares, is the last resort.
|
|
func volumeName(filer, filerMountRootPath, dir, override string) string {
|
|
name := override
|
|
if name == "" {
|
|
name = lastSegment(filerMountRootPath)
|
|
}
|
|
if name == "" {
|
|
name = lastSegment(dir)
|
|
}
|
|
if name == "" {
|
|
name = filer
|
|
}
|
|
return strings.ReplaceAll(name, ",", "+")
|
|
}
|
|
|
|
// lastSegment is the name a path ends with, and "" for the ones that name no
|
|
// disk: the root, "." and a bare Windows drive letter.
|
|
func lastSegment(p string) string {
|
|
name := path.Base(strings.ReplaceAll(p, `\`, "/"))
|
|
if name == "/" || name == "." || strings.HasSuffix(name, ":") {
|
|
return ""
|
|
}
|
|
return name
|
|
}
|
|
|
|
func configureMountMemory(option *MountOptions) error {
|
|
if option.readerCacheSizeMB != nil && (*option.readerCacheSizeMB <= 0 || *option.readerCacheSizeMB > math.MaxInt64>>20) {
|
|
return fmt.Errorf("readerCacheSizeMB must be positive and fit in an int64 byte budget")
|
|
}
|
|
if option.memoryLimitMB != nil {
|
|
if *option.memoryLimitMB < 0 || *option.memoryLimitMB > math.MaxInt64>>20 {
|
|
return fmt.Errorf("memoryLimitMB must be non-negative and fit in an int64 byte limit")
|
|
}
|
|
if *option.memoryLimitMB > 0 {
|
|
debug.SetMemoryLimit(*option.memoryLimitMB << 20)
|
|
}
|
|
}
|
|
return nil
|
|
}
|