You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
6.1 KiB
212 lines
6.1 KiB
9 years ago
|
/*
|
||
4 years ago
|
* MinIO Cloud Storage, (C) 2016-2020 MinIO, Inc.
|
||
9 years ago
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
9 years ago
|
"errors"
|
||
6 years ago
|
"reflect"
|
||
9 years ago
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// Global lookup timeout.
|
||
|
const (
|
||
5 years ago
|
globalLookupTimeout = time.Minute * 30 // 30minutes.
|
||
|
treeWalkEntryLimit = 50
|
||
|
treeWalkSameEntryLimit = 4
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
// listParams - list object params used for list object map
|
||
|
type listParams struct {
|
||
|
bucket string
|
||
|
recursive bool
|
||
|
marker string
|
||
|
prefix string
|
||
|
}
|
||
|
|
||
|
// errWalkAbort - returned by doTreeWalk() if it returns prematurely.
|
||
|
// doTreeWalk() can return prematurely if
|
||
|
// 1) treeWalk is timed out by the timer go-routine.
|
||
|
// 2) there is an error during tree walk.
|
||
9 years ago
|
var errWalkAbort = errors.New("treeWalk abort")
|
||
|
|
||
9 years ago
|
// treeWalk - represents the go routine that does the file tree walk.
|
||
|
type treeWalk struct {
|
||
5 years ago
|
added time.Time
|
||
6 years ago
|
resultCh chan TreeWalkResult
|
||
9 years ago
|
endWalkCh chan struct{} // To signal when treeWalk go-routine should end.
|
||
|
endTimerCh chan<- struct{} // To signal when timer go-routine should end.
|
||
9 years ago
|
}
|
||
|
|
||
6 years ago
|
// TreeWalkPool - pool of treeWalk go routines.
|
||
9 years ago
|
// A treeWalk is added to the pool by Set() and removed either by
|
||
|
// doing a Release() or if the concerned timer goes off.
|
||
|
// treeWalkPool's purpose is to maintain active treeWalk go-routines in a map so that
|
||
|
// it can be looked up across related list calls.
|
||
6 years ago
|
type TreeWalkPool struct {
|
||
4 years ago
|
mu sync.Mutex
|
||
9 years ago
|
pool map[listParams][]treeWalk
|
||
9 years ago
|
timeOut time.Duration
|
||
|
}
|
||
|
|
||
6 years ago
|
// NewTreeWalkPool - initialize new tree walk pool.
|
||
|
func NewTreeWalkPool(timeout time.Duration) *TreeWalkPool {
|
||
|
tPool := &TreeWalkPool{
|
||
9 years ago
|
pool: make(map[listParams][]treeWalk),
|
||
9 years ago
|
timeOut: timeout,
|
||
|
}
|
||
|
return tPool
|
||
|
}
|
||
|
|
||
9 years ago
|
// Release - selects a treeWalk from the pool based on the input
|
||
6 years ago
|
// listParams, removes it from the pool, and returns the TreeWalkResult
|
||
9 years ago
|
// channel.
|
||
5 years ago
|
// Returns nil if listParams does not have an associated treeWalk.
|
||
5 years ago
|
func (t *TreeWalkPool) Release(params listParams) (resultCh chan TreeWalkResult, endWalkCh chan struct{}) {
|
||
4 years ago
|
t.mu.Lock()
|
||
|
defer t.mu.Unlock()
|
||
9 years ago
|
walks, ok := t.pool[params] // Pick the valid walks.
|
||
4 years ago
|
if !ok || len(walks) == 0 {
|
||
|
// Release return nil if params not found.
|
||
|
return nil, nil
|
||
9 years ago
|
}
|
||
4 years ago
|
|
||
|
// Pop out the first valid walk entry.
|
||
|
walk := walks[0]
|
||
|
walks = walks[1:]
|
||
|
if len(walks) > 0 {
|
||
|
t.pool[params] = walks
|
||
|
} else {
|
||
|
delete(t.pool, params)
|
||
|
}
|
||
|
walk.endTimerCh <- struct{}{}
|
||
|
return walk.resultCh, walk.endWalkCh
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Set - adds a treeWalk to the treeWalkPool.
|
||
|
// Also starts a timer go-routine that ends when:
|
||
|
// 1) time.After() expires after t.timeOut seconds.
|
||
|
// The expiration is needed so that the treeWalk go-routine resources are freed after a timeout
|
||
|
// if the S3 client does only partial listing of objects.
|
||
5 years ago
|
// 2) Release() signals the timer go-routine to end on endTimerCh.
|
||
9 years ago
|
// During listing the timer should not timeout and end the treeWalk go-routine, hence the
|
||
|
// timer go-routine should be ended.
|
||
5 years ago
|
func (t *TreeWalkPool) Set(params listParams, resultCh chan TreeWalkResult, endWalkCh chan struct{}) {
|
||
4 years ago
|
t.mu.Lock()
|
||
|
defer t.mu.Unlock()
|
||
5 years ago
|
// If we are above the limit delete at least one entry from the pool.
|
||
|
if len(t.pool) > treeWalkEntryLimit {
|
||
|
age := time.Now()
|
||
|
var oldest listParams
|
||
|
for k, v := range t.pool {
|
||
|
if len(v) == 0 {
|
||
|
delete(t.pool, k)
|
||
|
continue
|
||
|
}
|
||
|
// The first element is the oldest, so we only check that.
|
||
4 years ago
|
e := v[0]
|
||
|
if e.added.Before(age) {
|
||
5 years ago
|
oldest = k
|
||
4 years ago
|
age = e.added
|
||
5 years ago
|
}
|
||
|
}
|
||
|
// Invalidate and delete oldest.
|
||
4 years ago
|
if walks, ok := t.pool[oldest]; ok && len(walks) > 0 {
|
||
|
endCh := walks[0].endTimerCh
|
||
4 years ago
|
endWalkCh := walks[0].endWalkCh
|
||
4 years ago
|
if len(walks) > 1 {
|
||
|
// Move walks forward
|
||
|
copy(walks, walks[1:])
|
||
|
walks = walks[:len(walks)-1]
|
||
|
t.pool[oldest] = walks
|
||
5 years ago
|
} else {
|
||
4 years ago
|
// Only entry, just delete.
|
||
|
delete(t.pool, oldest)
|
||
5 years ago
|
}
|
||
4 years ago
|
select {
|
||
|
case endCh <- struct{}{}:
|
||
4 years ago
|
close(endWalkCh)
|
||
4 years ago
|
default:
|
||
|
}
|
||
|
} else {
|
||
|
// Shouldn't happen, but just in case.
|
||
|
delete(t.pool, oldest)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// Should be a buffered channel so that Release() never blocks.
|
||
9 years ago
|
endTimerCh := make(chan struct{}, 1)
|
||
|
walkInfo := treeWalk{
|
||
5 years ago
|
added: UTCNow(),
|
||
9 years ago
|
resultCh: resultCh,
|
||
|
endWalkCh: endWalkCh,
|
||
|
endTimerCh: endTimerCh,
|
||
9 years ago
|
}
|
||
4 years ago
|
|
||
9 years ago
|
// Append new walk info.
|
||
5 years ago
|
walks := t.pool[params]
|
||
|
if len(walks) < treeWalkSameEntryLimit {
|
||
|
t.pool[params] = append(walks, walkInfo)
|
||
|
} else {
|
||
|
// We are at limit, invalidate oldest, move list down and add new as last.
|
||
4 years ago
|
select {
|
||
|
case walks[0].endTimerCh <- struct{}{}:
|
||
4 years ago
|
close(walks[0].endWalkCh)
|
||
4 years ago
|
default:
|
||
|
}
|
||
5 years ago
|
copy(walks, walks[1:])
|
||
|
walks[len(walks)-1] = walkInfo
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Timer go-routine which times out after t.timeOut seconds.
|
||
4 years ago
|
go func(endTimerCh <-chan struct{}, walkInfo treeWalk) {
|
||
9 years ago
|
select {
|
||
|
// Wait until timeOut
|
||
|
case <-time.After(t.timeOut):
|
||
9 years ago
|
// Timeout has expired. Remove the treeWalk from treeWalkPool and
|
||
|
// end the treeWalk go-routine.
|
||
4 years ago
|
t.mu.Lock()
|
||
|
defer t.mu.Unlock()
|
||
9 years ago
|
walks, ok := t.pool[params]
|
||
9 years ago
|
if ok {
|
||
6 years ago
|
// Trick of filtering without allocating
|
||
|
// https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
|
||
|
nwalks := walks[:0]
|
||
9 years ago
|
// Look for walkInfo, remove it from the walks list.
|
||
6 years ago
|
for _, walk := range walks {
|
||
|
if !reflect.DeepEqual(walk, walkInfo) {
|
||
|
nwalks = append(nwalks, walk)
|
||
9 years ago
|
}
|
||
|
}
|
||
6 years ago
|
if len(nwalks) == 0 {
|
||
9 years ago
|
// No more treeWalk go-routines associated with listParams
|
||
|
// hence remove map entry.
|
||
9 years ago
|
delete(t.pool, params)
|
||
9 years ago
|
} else {
|
||
|
// There are more treeWalk go-routines associated with listParams
|
||
|
// hence save the list in the map.
|
||
6 years ago
|
t.pool[params] = nwalks
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
// Signal the treeWalk go-routine to die.
|
||
|
close(endWalkCh)
|
||
|
case <-endTimerCh:
|
||
9 years ago
|
return
|
||
|
}
|
||
4 years ago
|
}(endTimerCh, walkInfo)
|
||
9 years ago
|
}
|