Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

5.1 KB
Raw Blame
package topology

import (
	"math/rand/v2"
	"time"

	"github.com/seaweedfs/seaweedfs/weed/stats"
	"github.com/seaweedfs/seaweedfs/weed/storage/types"
	"google.golang.org/grpc"

	"github.com/seaweedfs/seaweedfs/weed/glog"
	"github.com/seaweedfs/seaweedfs/weed/storage"
)

func (t *Topology) StartRefreshWritableVolumes(grpcDialOption grpc.DialOption, garbageThreshold float64, concurrentVacuumLimitPerVolumeServer int, concurrentVacuumLimitPerRack int, growThreshold float64, preallocate int64, vacuumInterval time.Duration, deleteEmptyAfter time.Duration) {
	if vacuumInterval <= 0 {
		vacuumInterval = 14 * time.Minute
	}
	go func() {
		for {
			if t.IsLeader() {
				freshThreshHold := time.Now().Unix() - 3*t.pulse //3 times of sleep interval
				t.CollectDeadNodeAndFullVolumes(freshThreshHold, t.volumeSizeLimit, growThreshold)
				t.DecayQuietVolumeSizes()
				t.retryPendingQuarantineReleases()
			}
			time.Sleep(time.Duration(float32(t.pulse*1e3)*(1+rand.Float32())) * time.Millisecond)
		}
	}()
	go func(garbageThreshold float64) {
		for {
			if t.IsLeader() {
				// Safety net: if vacuum was disabled by the plugin monitor but the
				// admin server is no longer connected, automatically re-enable.
				// This handles the case where the admin server crashes without
				// cleanup. Does NOT override an operator's intentional disable.
				if t.IsVacuumDisabledByPlugin() && t.adminServerConnectedFunc != nil && !t.adminServerConnectedFunc() {
					glog.V(0).Infof("Admin server disconnected while vacuum was disabled by plugin, clearing plugin disable")
					t.EnableVacuumByPlugin()
				}
				if !t.IsVacuumDisabled() {
					t.Vacuum(grpcDialOption, garbageThreshold, concurrentVacuumLimitPerVolumeServer, concurrentVacuumLimitPerRack, 0, "", preallocate, true, deleteEmptyAfter)
				}
			} else {
				stats.MasterReplicaPlacementMismatch.Reset()
			}
			time.Sleep(vacuumInterval + time.Duration(120*rand.Float32())*time.Second)
		}
	}(garbageThreshold)
	go func() {
		for {
			select {
			case fv := <-t.chanFullVolumes:
				t.SetVolumeCapacityFull(fv)
			case cv := <-t.chanCrowdedVolumes:
				t.SetVolumeCrowded(cv)
			}
		}
	}()
}
func (t *Topology) SetVolumeCapacityFull(volumeInfo storage.VolumeInfo) bool {
	diskType := types.ToDiskType(volumeInfo.DiskType)
	vl := t.GetVolumeLayout(volumeInfo.Collection, volumeInfo.ReplicaPlacement, volumeInfo.Ttl, diskType)
	if !vl.SetVolumeCapacityFull(volumeInfo.Id) {
		return false
	}

	vl.accessLock.RLock()
	defer vl.accessLock.RUnlock()

	vidLocations, found := vl.vid2location[volumeInfo.Id]
	if !found {
		return false
	}

	for _, dn := range vidLocations.list {
		if !volumeInfo.ReadOnly {

			disk := dn.getOrCreateDiskWithLock(volumeInfo.DiskType)
			disk.UpAdjustDiskUsageDelta(types.ToDiskType(volumeInfo.DiskType), &DiskUsageCounts{
				activeVolumeCount: -1,
			})

		}
	}
	return true
}

func (t *Topology) SetVolumeCrowded(volumeInfo storage.VolumeInfo) {
	diskType := types.ToDiskType(volumeInfo.DiskType)
	vl := t.GetVolumeLayout(volumeInfo.Collection, volumeInfo.ReplicaPlacement, volumeInfo.Ttl, diskType)
	vl.SetVolumeCrowded(volumeInfo.Id)
}

// SetDataNodeMaintenanceMode records the maintenance flag a volume server
// reported and returns whether it changed. On a change every volume the node
// holds is re-evaluated: while the flag is on, a volume with a replica there
// leaves the writable list, so no writes are assigned to it and the server can
// be drained; once the flag is off the volumes are writable again. Reads are
// untouched, and volume growth skips the node through AvailableSpaceFor.
func (t *Topology) SetDataNodeMaintenanceMode(dn *DataNode, on bool) (changed bool) {
	if !dn.SetMaintenanceMode(on) {
		return false
	}
	glog.V(0).Infof("volume server %s maintenance mode: %v", dn.Id(), on)
	for _, v := range dn.GetVolumes() {
		diskType := types.ToDiskType(v.DiskType)
		vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
		vl.EnsureCorrectWritables(&v)
	}
	return true
}

func (t *Topology) UnRegisterDataNode(dn *DataNode) {
	dn.IsTerminating = true
	for _, v := range dn.GetVolumes() {
		glog.V(0).Infoln("Removing Volume", v.Id, "from the dead volume server", dn.Id())
		diskType := types.ToDiskType(v.DiskType)
		vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
		vl.SetVolumeUnavailable(dn, v.Id)
		// this node may have been the conflicting copy pinning a quarantine
		defer t.releaseResolvedQuarantine(v.Id)
	}

	// unregister ec shards when volume server disconnected. The node's own
	// records clear first so the still-held check cannot skip the drop, and
	// each unregister gets a copy — DeleteEcShard drains the shared
	// ShardsInfo.
	for _, s := range dn.GetEcShards() {
		held := *s
		held.ShardsInfo = s.ShardsInfo.Copy()
		dn.DeleteEcShard(s)
		t.UnRegisterEcShards(&held, dn)
	}

	negativeUsages := dn.GetDiskUsages().negative()
	for dt, du := range negativeUsages.usages {
		dn.UpAdjustDiskUsageDelta(dt, du)
	}
	dn.DeltaUpdateVolumes([]storage.VolumeInfo{}, dn.GetVolumes())
	t.unregisterDataNodeAddress(dn.ServerAddress(), dn)
	if dn.Parent() != nil {
		dn.Parent().UnlinkChildNode(dn.Id())
	}
}