diff --git a/REST_API.md b/REST_API.md index 97df87757c..bf0e0c57b7 100644 --- a/REST_API.md +++ b/REST_API.md @@ -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 diff --git a/test/s3/copying/s3_copying_test.go b/test/s3/copying/s3_copying_test.go index bfa8ba7d41..7acee1cdc9 100644 --- a/test/s3/copying/s3_copying_test.go +++ b/test/s3/copying/s3_copying_test.go @@ -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 diff --git a/test/s3/cors/s3_cors_test.go b/test/s3/cors/s3_cors_test.go index e4c35d442b..fdb8ed0f1e 100644 --- a/test/s3/cors/s3_cors_test.go +++ b/test/s3/cors/s3_cors_test.go @@ -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 diff --git a/test/s3/retention/s3_retention_test.go b/test/s3/retention/s3_retention_test.go index 953db09a52..42ebd0a26a 100644 --- a/test/s3/retention/s3_retention_test.go +++ b/test/s3/retention/s3_retention_test.go @@ -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 diff --git a/test/s3/tagging/s3_tagging_test.go b/test/s3/tagging/s3_tagging_test.go index 28db4ddeda..d4e4eec3f1 100644 --- a/test/s3/tagging/s3_tagging_test.go +++ b/test/s3/tagging/s3_tagging_test.go @@ -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 diff --git a/test/s3/versioning/s3_versioning_test.go b/test/s3/versioning/s3_versioning_test.go index ba42e04b50..ec31b239ae 100644 --- a/test/s3/versioning/s3_versioning_test.go +++ b/test/s3/versioning/s3_versioning_test.go @@ -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 diff --git a/weed/command/benchmark.go b/weed/command/benchmark.go index 0ee3f03136..cd9975eb96 100644 --- a/weed/command/benchmark.go +++ b/weed/command/benchmark.go @@ -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" `, } diff --git a/weed/server/master_server_handlers_admin.go b/weed/server/master_server_handlers_admin.go index 3bd8d4c863..4777ba3610 100644 --- a/weed/server/master_server_handlers_admin.go +++ b/weed/server/master_server_handlers_admin.go @@ -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() diff --git a/weed/server/master_server_handlers_admin_test.go b/weed/server/master_server_handlers_admin_test.go new file mode 100644 index 0000000000..66e78d7905 --- /dev/null +++ b/weed/server/master_server_handlers_admin_test.go @@ -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) +}