Apply quota usage cache invalidation per second (#10127)

Allow faster lookups for quota check enforcement
master
Harshavardhana 4 years ago committed by GitHub
parent 4752323e1c
commit 57ff9abca2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 43
      cmd/bucket-quota.go
  2. 3
      cmd/object-handlers.go

@ -68,43 +68,36 @@ func (sys *BucketQuotaSys) check(ctx context.Context, bucket string, size int64)
return errServerNotInitialized return errServerNotInitialized
} }
q, err := sys.Get(bucket)
if err != nil {
return nil
}
if q.Type == madmin.FIFOQuota {
return nil
}
if q.Quota == 0 {
// No quota set return quickly.
return nil
}
sys.bucketStorageCache.Once.Do(func() { sys.bucketStorageCache.Once.Do(func() {
sys.bucketStorageCache.TTL = 10 * time.Second sys.bucketStorageCache.TTL = 1 * time.Second
sys.bucketStorageCache.Update = func() (interface{}, error) { sys.bucketStorageCache.Update = func() (interface{}, error) {
return loadDataUsageFromBackend(ctx, objAPI) return loadDataUsageFromBackend(ctx, objAPI)
} }
}) })
v, err := sys.bucketStorageCache.Get() q, err := sys.Get(bucket)
if err != nil { if err != nil {
return err return err
} }
dui := v.(DataUsageInfo) if q != nil && q.Type == madmin.HardQuota && q.Quota > 0 {
v, err := sys.bucketStorageCache.Get()
if err != nil {
return err
}
bui, ok := dui.BucketsUsage[bucket] dui := v.(DataUsageInfo)
if !ok {
// bucket not found, cannot enforce quota bui, ok := dui.BucketsUsage[bucket]
// call will fail anyways later. if !ok {
return nil // bucket not found, cannot enforce quota
} // call will fail anyways later.
return nil
}
if (bui.Size + uint64(size)) > q.Quota { if (bui.Size + uint64(size)) >= q.Quota {
return BucketQuotaExceeded{Bucket: bucket} return BucketQuotaExceeded{Bucket: bucket}
}
} }
return nil return nil

@ -980,6 +980,7 @@ func (api objectAPIHandlers) CopyObjectHandler(w http.ResponseWriter, r *http.Re
} }
length = actualSize length = actualSize
} }
if !cpSrcDstSame { if !cpSrcDstSame {
if err := enforceBucketQuota(ctx, dstBucket, actualSize); err != nil { if err := enforceBucketQuota(ctx, dstBucket, actualSize); err != nil {
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r)) writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
@ -1893,10 +1894,12 @@ func (api objectAPIHandlers) CopyObjectPartHandler(w http.ResponseWriter, r *htt
return return
} }
} }
if err := enforceBucketQuota(ctx, dstBucket, actualPartSize); err != nil { if err := enforceBucketQuota(ctx, dstBucket, actualPartSize); err != nil {
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r)) writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
return return
} }
// Special care for CopyObjectPart // Special care for CopyObjectPart
if partRangeErr := checkCopyPartRangeWithSize(rs, actualPartSize); partRangeErr != nil { if partRangeErr := checkCopyPartRangeWithSize(rs, actualPartSize); partRangeErr != nil {
writeCopyPartErr(ctx, w, partRangeErr, r.URL, guessIsBrowserReq(r)) writeCopyPartErr(ctx, w, partRangeErr, r.URL, guessIsBrowserReq(r))

Loading…
Cancel
Save