mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
master: /col/delete requires an explicit collection name (#13055)
* master: /col/delete requires an explicit collection name A /col/delete request without a collection parameter, or with an empty one, read as the empty-named default collection and deleted it. Return 400 instead, and accept collection=_default (the keyword weed shell uses) to delete the default collection deliberately. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: keep a collection literally named _default addressable by /col/delete collection=_default now names the empty-named collection. A collection created with that literal name would have been unreachable, and a request meant for it would have deleted the default collection instead. Resolve the keyword to the literal collection when one exists, and refuse the request when both exist. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: /col/delete accepts only the DELETE method A GET is expected to be safe, yet a bare GET or a link prefetch deleted the named collection. Any other method now gets 405 with an Allow header, and the docs, benchmark help and test helpers send DELETE. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: /col/delete accepts _default_ for the default collection When a collection literally named _default exists next to the default collection, collection=_default is refused, which left the default collection with no way to be named. Accept the _default_ spelling that weed shell collection.delete already takes; it always means the default collection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * master: /col/delete keeps a literal _default_ collection addressable _default_ now resolves like _default: to the literal collection when one exists, to the default collection otherwise, and refuses the request when both exist. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * REST_API: /col/delete when both keyword names are literal collections Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
9 files changed
+170
-9
No files matched your search
+15
-2
@@ -278,11 +278,24 @@ automatic vacuum or another request is running does nothing and gets
|
||||
### Delete a collection
|
||||
|
||||
```bash
|
||||
curl "http://localhost:9333/col/delete?collection=benchmark"
|
||||
curl -X DELETE "http://localhost:9333/col/delete?collection=benchmark"
|
||||
```
|
||||
|
||||
Deletes all volumes of the collection, including erasure-coded shards.
|
||||
`204 No Content` on success.
|
||||
`204 No Content` on success. Only the `DELETE` method is accepted; any other
|
||||
method gets `405 Method Not Allowed`.
|
||||
|
||||
The `collection` parameter is required: a request without it, or with an
|
||||
empty value, gets `400 Bad Request`. To delete the default (empty-named)
|
||||
collection, name it explicitly with `collection=_default` or
|
||||
`collection=_default_`, the keywords `weed shell collection.delete` accepts.
|
||||
If a collection is literally named a keyword, that name deletes it, and when
|
||||
it exists next to the default collection the request gets
|
||||
`400 Bad Request`; use the other keyword for the default collection. If both
|
||||
keyword names are literal collections next to the default collection, both
|
||||
keywords get `400 Bad Request`; delete the default collection with
|
||||
`weed shell` `collection.delete -collection _default -apply`, where the
|
||||
keywords always mean the default collection.
|
||||
|
||||
### Health
|
||||
|
||||
|
||||
@@ -186,7 +186,7 @@ func forceDeleteCollection(t *testing.T, bucketName string) {
|
||||
return
|
||||
}
|
||||
endpoint := strings.TrimRight(defaultConfig.MasterEndpoint, "/") + "/col/delete?collection=" + url.QueryEscape(bucketName)
|
||||
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
||||
req, err := http.NewRequest(http.MethodDelete, endpoint, nil)
|
||||
if err != nil {
|
||||
t.Logf("Note: building collection delete request for %s failed: %v", bucketName, err)
|
||||
return
|
||||
|
||||
@@ -166,7 +166,7 @@ func forceDeleteCollection(t *testing.T, bucketName string) {
|
||||
return
|
||||
}
|
||||
endpoint := strings.TrimRight(masterEndpoint, "/") + "/col/delete?collection=" + url.QueryEscape(bucketName)
|
||||
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
||||
req, err := http.NewRequest(http.MethodDelete, endpoint, nil)
|
||||
if err != nil {
|
||||
t.Logf("Note: building collection delete request for %s failed: %v", bucketName, err)
|
||||
return
|
||||
|
||||
@@ -156,7 +156,7 @@ func forceDeleteCollection(t *testing.T, bucketName string) {
|
||||
return
|
||||
}
|
||||
endpoint := strings.TrimRight(defaultConfig.MasterEndpoint, "/") + "/col/delete?collection=" + url.QueryEscape(bucketName)
|
||||
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
||||
req, err := http.NewRequest(http.MethodDelete, endpoint, nil)
|
||||
if err != nil {
|
||||
t.Logf("Note: building collection delete request for %s failed: %v", bucketName, err)
|
||||
return
|
||||
|
||||
@@ -193,7 +193,7 @@ func forceDeleteCollection(t *testing.T, bucketName string) {
|
||||
return
|
||||
}
|
||||
endpoint := strings.TrimRight(masterEndpoint, "/") + "/col/delete?collection=" + url.QueryEscape(bucketName)
|
||||
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
||||
req, err := http.NewRequest(http.MethodDelete, endpoint, nil)
|
||||
if err != nil {
|
||||
t.Logf("Note: building collection delete request for %s failed: %v", bucketName, err)
|
||||
return
|
||||
|
||||
@@ -180,7 +180,7 @@ func forceDeleteCollection(t *testing.T, bucketName string) {
|
||||
return
|
||||
}
|
||||
endpoint := strings.TrimRight(defaultConfig.MasterEndpoint, "/") + "/col/delete?collection=" + url.QueryEscape(bucketName)
|
||||
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
||||
req, err := http.NewRequest(http.MethodDelete, endpoint, nil)
|
||||
if err != nil {
|
||||
t.Logf("Note: building collection delete request for %s failed: %v", bucketName, err)
|
||||
return
|
||||
|
||||
@@ -102,7 +102,7 @@ var cmdBenchmark = &Command{
|
||||
http://localhost:9333/vol/grow?collection=benchmark&count=5
|
||||
|
||||
After benchmarking, you can clean up the written data by deleting the benchmark collection
|
||||
http://localhost:9333/col/delete?collection=benchmark
|
||||
curl -X DELETE "http://localhost:9333/col/delete?collection=benchmark"
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
@@ -19,10 +19,20 @@ import (
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
||||
"github.com/seaweedfs/seaweedfs/weed/topology"
|
||||
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util/wildcard"
|
||||
)
|
||||
|
||||
func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
collectionName := r.FormValue("collection")
|
||||
if r.Method != http.MethodDelete {
|
||||
w.Header().Set("Allow", http.MethodDelete)
|
||||
writeJsonError(w, r, http.StatusMethodNotAllowed, fmt.Errorf("/col/delete requires the DELETE method"))
|
||||
return
|
||||
}
|
||||
collectionName, err := ms.collectionToDelete(r)
|
||||
if err != nil {
|
||||
writeJsonError(w, r, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if _, ok := ms.Topo.FindCollection(collectionName); !ok {
|
||||
writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", collectionName))
|
||||
return
|
||||
@@ -40,6 +50,36 @@ func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.R
|
||||
return
|
||||
}
|
||||
|
||||
// collectionToDelete reads the collection a /col/delete request names. The
|
||||
// default collection is the empty-named one, and an absent or empty parameter
|
||||
// also reads as "", so a bare /col/delete would delete the default collection.
|
||||
// Require it to be named explicitly, with the keywords weed shell uses for it.
|
||||
// A collection literally named a keyword keeps its name, and when both exist
|
||||
// the request is refused rather than guessing which one to delete.
|
||||
func (ms *MasterServer) collectionToDelete(r *http.Request) (string, error) {
|
||||
name := r.FormValue("collection")
|
||||
if name == "" {
|
||||
return "", fmt.Errorf("collection parameter is required; use collection=%s to delete the default (empty-named) collection", wildcard.CollectionDefault)
|
||||
}
|
||||
keywords := []string{wildcard.CollectionDefault, wildcard.CollectionDefault + "_"}
|
||||
if name != keywords[0] && name != keywords[1] {
|
||||
return name, nil
|
||||
}
|
||||
if _, literal := ms.Topo.FindCollection(name); !literal {
|
||||
return "", nil
|
||||
}
|
||||
if _, hasDefault := ms.Topo.FindCollection(""); !hasDefault {
|
||||
return name, nil
|
||||
}
|
||||
hint := "use weed shell collection.delete to delete the default collection"
|
||||
for _, other := range keywords {
|
||||
if _, taken := ms.Topo.FindCollection(other); !taken {
|
||||
hint = fmt.Sprintf("use collection=%s to delete the default collection", other)
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("collection=%s is ambiguous: both a collection named %s and the default (empty-named) collection exist; %s", name, name, hint)
|
||||
}
|
||||
|
||||
func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
m := make(map[string]interface{})
|
||||
m["Version"] = version.Version()
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package weed_server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
||||
)
|
||||
|
||||
// A /col/delete that does not name a collection must not be read as a request
|
||||
// to delete the default (empty-named) collection.
|
||||
func TestCollectionDeleteHandlerRequiresCollection(t *testing.T) {
|
||||
ms := newLeaderMaster()
|
||||
addCollectionVolume(t, ms, 1, "")
|
||||
_, ok := ms.Topo.FindCollection("")
|
||||
require.True(t, ok, "test setup: the default collection should exist")
|
||||
|
||||
requests := map[string]*http.Request{
|
||||
"DELETE without parameter": httptest.NewRequest(http.MethodDelete, "/col/delete", nil),
|
||||
"DELETE with empty value": httptest.NewRequest(http.MethodDelete, "/col/delete?collection=", nil),
|
||||
}
|
||||
for name, r := range requests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
ms.collectionDeleteHandler(w, r)
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body.String())
|
||||
assert.Contains(t, w.Body.String(), "collection parameter is required")
|
||||
_, ok := ms.Topo.FindCollection("")
|
||||
assert.True(t, ok, "the default collection must survive")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectionDeleteHandlerRequiresDeleteMethod(t *testing.T) {
|
||||
ms := newLeaderMaster()
|
||||
addCollectionVolume(t, ms, 1, "photos")
|
||||
for _, method := range []string{http.MethodGet, http.MethodPost, http.MethodPut} {
|
||||
t.Run(method, func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
ms.collectionDeleteHandler(w, httptest.NewRequest(method, "/col/delete?collection=photos", nil))
|
||||
assert.Equal(t, http.StatusMethodNotAllowed, w.Code, w.Body.String())
|
||||
assert.Equal(t, http.MethodDelete, w.Header().Get("Allow"))
|
||||
_, ok := ms.Topo.FindCollection("photos")
|
||||
assert.True(t, ok, "the collection must survive")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectionToDelete(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
collections []string
|
||||
query string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "missing", collections: []string{""}, query: "", wantErr: true},
|
||||
{name: "empty", collections: []string{""}, query: "collection=", wantErr: true},
|
||||
{name: "default keyword", collections: []string{""}, query: "collection=_default", want: ""},
|
||||
{name: "named", collections: []string{"", "photos"}, query: "collection=photos", want: "photos"},
|
||||
{name: "literal keyword name", collections: []string{"_default"}, query: "collection=_default", want: "_default"},
|
||||
{name: "literal keyword name and default", collections: []string{"", "_default"}, query: "collection=_default", wantErr: true},
|
||||
{name: "legacy default keyword", collections: []string{""}, query: "collection=_default_", want: ""},
|
||||
{name: "legacy default keyword next to literal keyword name", collections: []string{"", "_default"}, query: "collection=_default_", want: ""},
|
||||
{name: "literal legacy keyword name", collections: []string{"_default_"}, query: "collection=_default_", want: "_default_"},
|
||||
{name: "literal legacy keyword name and default", collections: []string{"", "_default_"}, query: "collection=_default_", wantErr: true},
|
||||
{name: "default keyword next to literal legacy keyword name", collections: []string{"", "_default_"}, query: "collection=_default", want: ""},
|
||||
{name: "both literal keyword names and default", collections: []string{"", "_default", "_default_"}, query: "collection=_default", wantErr: true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
ms := newLeaderMaster()
|
||||
for i, collection := range c.collections {
|
||||
addCollectionVolume(t, ms, needle.VolumeId(i+1), collection)
|
||||
}
|
||||
got, err := ms.collectionToDelete(httptest.NewRequest(http.MethodGet, "/col/delete?"+c.query, nil))
|
||||
if c.wantErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, c.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func addCollectionVolume(t *testing.T, ms *MasterServer, id needle.VolumeId, collection string) {
|
||||
t.Helper()
|
||||
rp, err := super_block.NewReplicaPlacementFromString("000")
|
||||
require.NoError(t, err)
|
||||
dn := ms.Topo.GetOrCreateDataCenter("dc1").GetOrCreateRack("rack1").
|
||||
GetOrCreateDataNode("127.0.0.1", 8080, 18080, "127.0.0.1", "dn1", map[string]uint32{"": 10})
|
||||
v := storage.VolumeInfo{
|
||||
Id: id,
|
||||
Collection: collection,
|
||||
ReplicaPlacement: rp,
|
||||
Ttl: needle.EMPTY_TTL,
|
||||
Version: needle.GetCurrentVersion(),
|
||||
}
|
||||
dn.AddOrUpdateVolume(v)
|
||||
ms.Topo.RegisterVolumeLayout(v, dn)
|
||||
}
|
||||
Reference in new issue
Block a user