shell: make every -collection flag parse the same way (#11794)

* shell: make every -collection flag parse the same way

The filter vocabulary (comma-separated names, wildcards, regexes,
"_default" for the empty-named collection) already existed in
wildcard.CompileCollectionMatcher, but several commands still did their
own exact-match comparison or sent the raw flag value to servers.

fs.mergeVolumes used a literal string compare, so -collection=_default
silently produced an empty merge plan; it now uses the shared matcher
while keeping "*" as the match-all default. volume.fsck and volume.mark
filter through the matcher too, and volume.vacuum resolves the filter
client-side into per-collection and per-volume requests, since no
Collection value scopes the VacuumVolume RPC to the unnamed collection
alone.

Commands that put one concrete collection name on the wire normalize
"_default" to "" (volume.grow, fs.configure, mq.topic.compact,
collection.delete, ec.rebuild, ec.balance), and the tier -volumeId
commands resolve the volume's actual collection from topology first so
patterns and named-collection requests pass the volume server's
collection check.

* shell: spell the default-collection keyword via CollectionDefault

* shell: address review findings on collection filter handling

volume.fsck keeps a set of every volume the master knows before any
filtering, so a filer entry whose volume was excluded by -collection or
-volumeId is skipped instead of reported "volume not found" and deleted
by -reallyDeleteFilerEntries. The collection filter also no longer
scopes the filer walk to /buckets/<collection>: fs.configure can bind a
collection to any path prefix, and a narrower walk could mark live
chunks as orphans under -reallyDeleteFromVolume.

ec.rebuild and ec.balance resolve -collection through the new
wildcard.ExpandCollectionFilter, which passes literal names through even
when the master does not list them — ec.rebuild must be able to recover
a collection whose shards all lost their index and never registered.

fs.configure and mq.topic.compact now reject _default outright: an empty
Collection means "unset" in both the path-rule merge and the filer's
storage-option fallback chain, so the keyword would have been silently
ignored.

The unnamed-collection vacuum sweep keeps the master's low-disk-space
exception: a volume whose every replica is read-only is probed with
VacuumVolumeCheck and stays eligible when a replica reports
DiskSpaceLow, instead of being dropped outright.
This commit is contained in:
Chris Lu authored and GitHub committed 2026-10-16 21:07:51 -07:00
1 parent 268424a5e1
commit b0ab4c4e1e
23 files changed
+593 -71

No files matched your search

+177 -10
View File
@@ -5,11 +5,18 @@ import (
"flag"
"fmt"
"io"
"slices"
"sort"
"strconv"
"strings"
"time"
"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/util/wildcard"
"google.golang.org/grpc"
)
func init() {
@@ -42,12 +49,17 @@ func (c *commandVacuum) Do(args []string, commandEnv *CommandEnv, writer io.Writ
volumeVacuumCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
garbageThreshold := volumeVacuumCommand.Float64("garbageThreshold", 0.3, "vacuum when garbage is more than this limit")
collection := volumeVacuumCommand.String("collection", "", "vacuum this collection")
collection := volumeVacuumCommand.String("collection", "", "comma-separated collection names, wildcards, or regex patterns; empty or \"*\" matches all, \""+CollectionDefault+"\" the collection with no name")
volumeIds := volumeVacuumCommand.String("volumeId", "", "comma-separated list of volume IDs")
if err = volumeVacuumCommand.Parse(args); err != nil {
return nil
}
collectionMatcher, err := wildcard.CompileCollectionMatcher(*collection)
if err != nil {
return fmt.Errorf("invalid collection pattern %q: %w", *collection, err)
}
if err = commandEnv.confirmIsLocked(args); err != nil {
return
}
@@ -107,19 +119,41 @@ func (c *commandVacuum) Do(args []string, commandEnv *CommandEnv, writer io.Writ
} else if topo != nil {
// The sweep says nothing about the volumes it leaves alone, so an
// operator on a full disk sees the command return and nothing change.
if skipped := readOnlyVolumesAboveThreshold(topo, *collection, *garbageThreshold); len(skipped) > 0 {
if skipped := readOnlyVolumesAboveThreshold(topo, collectionMatcher, *garbageThreshold); len(skipped) > 0 {
fmt.Fprintf(writer, "%d read-only volume(s) hold garbage above %g and are skipped by the sweep: %v\n", len(skipped), *garbageThreshold, skipped)
fmt.Fprintf(writer, "vacuum them explicitly with -volumeId\n")
}
}
for _, volumeId := range volumeIdInts {
err = commandEnv.MasterClient.WithClient(context.Background(), false, func(client master_pb.SeaweedClient) error {
_, err = client.VacuumVolume(context.Background(), &master_pb.VacuumVolumeRequest{
GarbageThreshold: float32(*garbageThreshold),
VolumeId: volumeId,
Collection: *collection,
// The VacuumVolume RPC takes one exact collection name per call and reads
// "" as "all collections", so a filter has to be resolved here: one request
// per matched named collection, and per-volume requests for the unnamed
// collection, which no Collection value selects on its own.
var requests []*master_pb.VacuumVolumeRequest
if collectionMatcher == nil {
for _, volumeId := range volumeIdInts {
requests = append(requests, &master_pb.VacuumVolumeRequest{
VolumeId: volumeId,
})
}
} else {
if topo == nil {
return fmt.Errorf("cannot resolve collection pattern %q: topology unavailable", *collection)
}
if *volumeIds != "" {
requests, err = vacuumRequestsForVolumeIds(topo, collectionMatcher, volumeIdInts)
} else {
requests, err = vacuumRequestsForPattern(commandEnv.option.GrpcDialOption, writer, topo, collectionMatcher)
}
if err != nil {
return err
}
}
for _, req := range requests {
req.GarbageThreshold = float32(*garbageThreshold)
err = commandEnv.MasterClient.WithClient(context.Background(), false, func(client master_pb.SeaweedClient) error {
_, err = client.VacuumVolume(context.Background(), req)
return err
})
if err != nil {
@@ -130,6 +164,139 @@ func (c *commandVacuum) Do(args []string, commandEnv *CommandEnv, writer io.Writ
return nil
}
// vidReplica pairs a volume replica's read-only state with the server that
// reports it, so the unnamed-collection sweep can reproduce the master's
// low-disk-space exception client-side.
type vidReplica struct {
server pb.ServerAddress
readOnly bool
}
// vacuumRequestsForPattern resolves a collection filter to the sweep requests
// that cover it: one per matched named collection, which keeps the server's
// read-only skip, plus per-volume requests for the unnamed collection — no
// Collection value scopes the RPC to it alone.
func vacuumRequestsForPattern(grpcDialOption grpc.DialOption, writer io.Writer, topo *master_pb.TopologyInfo, collectionMatcher *wildcard.CollectionMatcher) ([]*master_pb.VacuumVolumeRequest, error) {
collections := make(map[string]bool)
defaultReplicas := make(map[uint32][]vidReplica)
eachDataNode(topo, func(_ DataCenterId, _ RackId, dn *master_pb.DataNodeInfo) {
for _, disk := range dn.DiskInfos {
for _, v := range disk.VolumeInfos {
if !collectionMatcher.Matches(v.Collection) {
continue
}
collections[v.Collection] = true
if v.Collection == "" {
defaultReplicas[v.Id] = append(defaultReplicas[v.Id], vidReplica{
server: pb.NewServerAddressFromDataNode(dn),
readOnly: v.ReadOnly,
})
}
}
}
})
if len(collections) == 0 {
return nil, fmt.Errorf("no volumes found in collections matching the filter")
}
var requests []*master_pb.VacuumVolumeRequest
for name := range collections {
if name != "" {
requests = append(requests, &master_pb.VacuumVolumeRequest{Collection: name})
}
}
// The unnamed collection has no scoping name, so each of its volumes gets
// a per-volume request — which also vacuums any other collection sharing
// the id. A volume whose every replica is read-only is skipped unless a
// replica reports low disk space, the sweep's own exception.
var defaultVids []uint32
for vid, replicas := range defaultReplicas {
writable := false
for _, r := range replicas {
if !r.readOnly {
writable = true
break
}
}
if !writable && !anyReplicaDiskSpaceLow(grpcDialOption, replicas, vid, writer) {
continue
}
defaultVids = append(defaultVids, vid)
}
sort.Slice(defaultVids, func(i, j int) bool { return defaultVids[i] < defaultVids[j] })
for _, vid := range defaultVids {
requests = append(requests, &master_pb.VacuumVolumeRequest{VolumeId: vid})
}
return requests, nil
}
// anyReplicaDiskSpaceLow probes read-only replicas for the low-disk condition
// under which the master's sweep still vacuums a read-only volume.
func anyReplicaDiskSpaceLow(grpcDialOption grpc.DialOption, replicas []vidReplica, vid uint32, writer io.Writer) bool {
for _, r := range replicas {
diskSpaceLow := false
err := operation.WithVolumeServerClient(false, r.server, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, checkErr := client.VacuumVolumeCheck(ctx, &volume_server_pb.VacuumVolumeCheckRequest{VolumeId: vid})
if checkErr != nil {
return checkErr
}
diskSpaceLow = resp.DiskSpaceLow
return nil
})
if err != nil {
fmt.Fprintf(writer, "volume %d on %s: cannot check disk space: %v\n", vid, r.server, err)
continue
}
if diskSpaceLow {
return true
}
}
return false
}
// vacuumRequestsForVolumeIds scopes each requested volume to the collections
// it lives in that the filter matches, so -volumeId and a pattern narrow each
// other the same way the server narrows an exact -collection name.
func vacuumRequestsForVolumeIds(topo *master_pb.TopologyInfo, collectionMatcher *wildcard.CollectionMatcher, volumeIdInts []uint32) ([]*master_pb.VacuumVolumeRequest, error) {
collectionsByVid := make(map[uint32]map[string]bool)
eachDataNode(topo, func(_ DataCenterId, _ RackId, dn *master_pb.DataNodeInfo) {
for _, disk := range dn.DiskInfos {
for _, v := range disk.VolumeInfos {
if collectionsByVid[v.Id] == nil {
collectionsByVid[v.Id] = make(map[string]bool)
}
collectionsByVid[v.Id][v.Collection] = true
}
}
})
var requests []*master_pb.VacuumVolumeRequest
for _, vid := range volumeIdInts {
var matched []string
for name := range collectionsByVid[vid] {
if collectionMatcher.Matches(name) {
matched = append(matched, name)
}
}
if len(matched) == 0 {
return nil, fmt.Errorf("volume %d is not in a collection matching the filter", vid)
}
// The unnamed collection has no scoping name; a volume-id-only request
// covers it (and any other collection the id is shared into).
if slices.Contains(matched, "") {
requests = append(requests, &master_pb.VacuumVolumeRequest{VolumeId: vid})
continue
}
sort.Strings(matched)
for _, name := range matched {
requests = append(requests, &master_pb.VacuumVolumeRequest{VolumeId: vid, Collection: name})
}
}
return requests, nil
}
// readOnlyVolumesAboveThreshold lists the volumes a sweep leaves alone: any
// replica read-only (that is what the sweep checks), in the collection when one
// is given, and some replica with a garbage ratio at or above the threshold.
@@ -138,13 +305,13 @@ func (c *commandVacuum) Do(args []string, commandEnv *CommandEnv, writer io.Writ
// it can only understate, and a converted index that reports deletes without
// their sizes is left out because its ratio is not knowable here. This is a
// hint; the volume server's own check decides.
func readOnlyVolumesAboveThreshold(topo *master_pb.TopologyInfo, collection string, garbageThreshold float64) []uint32 {
func readOnlyVolumesAboveThreshold(topo *master_pb.TopologyInfo, collectionMatcher *wildcard.CollectionMatcher, garbageThreshold float64) []uint32 {
readOnly := make(map[uint32]bool)
garbage := make(map[uint32]float64) // the highest ratio any replica reports
eachDataNode(topo, func(_ DataCenterId, _ RackId, dn *master_pb.DataNodeInfo) {
for _, disk := range dn.DiskInfos {
for _, v := range disk.VolumeInfos {
if collection != "" && v.Collection != collection {
if !collectionMatcher.Matches(v.Collection) {
continue
}
if v.ReadOnly {