From 8beca8309002db87aa6e6921b1f5cf7dfb8ab4c4 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 26 Apr 2015 21:15:43 -0700 Subject: [PATCH] Limit memory mode at 100 buckets and return error appropriately ~~~ mc: Failed to create bucket for URL [http://localhost:9000/newbucket-101]. Reason: [You have attempted to create more buckets than allowed.]. ~~~ Response from server --- pkg/api/api_bucket_handlers.go | 4 ++++ pkg/api/api_object_handlers.go | 4 ++++ pkg/api/api_router.go | 3 ++- pkg/storage/drivers/errors.go | 8 ++++++++ pkg/storage/drivers/memory/memory.go | 8 ++++++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/api/api_bucket_handlers.go b/pkg/api/api_bucket_handlers.go index 3fbba0303..3950101b9 100644 --- a/pkg/api/api_bucket_handlers.go +++ b/pkg/api/api_bucket_handlers.go @@ -171,6 +171,10 @@ func (server *minioAPI) putBucketHandler(w http.ResponseWriter, req *http.Reques w.Header().Set("Connection", "close") w.WriteHeader(http.StatusOK) } + case drivers.TooManyBuckets: + { + writeErrorResponse(w, req, TooManyBuckets, acceptsContentType, req.URL.Path) + } case drivers.BucketNameInvalid: { writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) diff --git a/pkg/api/api_object_handlers.go b/pkg/api/api_object_handlers.go index 0c0900429..d4dbb4c8b 100644 --- a/pkg/api/api_object_handlers.go +++ b/pkg/api/api_object_handlers.go @@ -163,6 +163,10 @@ func (server *minioAPI) putObjectHandler(w http.ResponseWriter, req *http.Reques w.WriteHeader(http.StatusOK) case drivers.ObjectExists: { + // we need to debate about this, if this is the right message to send back + // https://github.com/minio-io/minio/issues/505 + + // Ideally we can use 405 Method No Allowed writeErrorResponse(w, req, NotImplemented, acceptsContentType, req.URL.Path) } case drivers.BadDigest: diff --git a/pkg/api/api_router.go b/pkg/api/api_router.go index 08ee0711f..c218e6a8a 100644 --- a/pkg/api/api_router.go +++ b/pkg/api/api_router.go @@ -20,12 +20,13 @@ import ( "log" "net/http" + "time" + router "github.com/gorilla/mux" "github.com/minio-io/minio/pkg/api/config" "github.com/minio-io/minio/pkg/api/quota" "github.com/minio-io/minio/pkg/iodine" "github.com/minio-io/minio/pkg/storage/drivers" - "time" ) // private use diff --git a/pkg/storage/drivers/errors.go b/pkg/storage/drivers/errors.go index cdcea484c..fcba09774 100644 --- a/pkg/storage/drivers/errors.go +++ b/pkg/storage/drivers/errors.go @@ -78,6 +78,9 @@ type BucketExists GenericBucketError // BucketNotFound - requested bucket not found type BucketNotFound GenericBucketError +// TooManyBuckets - total buckets exceeded +type TooManyBuckets GenericBucketError + /// Object related errors // ObjectNotFound - requested object not found @@ -149,6 +152,11 @@ func (e BucketExists) Error() string { return "Bucket exists: " + e.Bucket } +// Return string an error formatted as the given text +func (e TooManyBuckets) Error() string { + return "Bucket limit exceeded beyond 100, cannot create bucket: " + e.Bucket +} + // Return string an error formatted as the given text func (e BucketNotFound) Error() string { return "Bucket not Found: " + e.Bucket diff --git a/pkg/storage/drivers/memory/memory.go b/pkg/storage/drivers/memory/memory.go index 37ae429e5..a7c2ca99b 100644 --- a/pkg/storage/drivers/memory/memory.go +++ b/pkg/storage/drivers/memory/memory.go @@ -60,6 +60,10 @@ type storedObject struct { metadata drivers.ObjectMetadata } +const ( + totalBuckets = 100 +) + // Start memory object server func Start(maxSize uint64) (chan<- string, <-chan error, drivers.Driver) { ctrlChannel := make(chan string) @@ -263,6 +267,10 @@ func (memory *memoryDriver) CreateObject(bucket, key, contentType, expectedMD5Su // CreateBucket - create bucket in memory func (memory *memoryDriver) CreateBucket(bucketName, acl string) error { memory.lock.RLock() + if len(memory.bucketMetadata) == totalBuckets { + memory.lock.RLock() + return iodine.New(drivers.TooManyBuckets{Bucket: bucketName}, nil) + } if !drivers.IsValidBucket(bucketName) { memory.lock.RUnlock() return iodine.New(drivers.BucketNameInvalid{Bucket: bucketName}, nil)