@ -21,10 +21,10 @@ import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
@ -228,6 +228,10 @@ func (c *diskCache) toClear() uint64 {
return bytesToClear ( int64 ( di . Total ) , int64 ( di . Free ) , uint64 ( c . quotaPct ) , uint64 ( c . lowWatermark ) )
}
var (
errDoneForNow = errors . New ( "done for now" )
)
// Purge cache entries that were not accessed.
func ( c * diskCache ) purge ( ctx context . Context ) {
if c . diskUsageLow ( ) {
@ -263,25 +267,24 @@ func (c *diskCache) purge(ctx context.Context) {
}
return fm
}
objDirs , err := ioutil . ReadDir ( c . dir )
if err != nil {
log . Fatal ( err )
}
for _ , obj := range objDirs {
if obj . Name ( ) == minioMetaBucket {
continue
filterFn := func ( name string , typ os . FileMode ) error {
if name == minioMetaBucket {
// Proceed to next file.
return nil
}
cacheDir := pathJoin ( c . dir , obj . N ame( ) )
cacheDir := pathJoin ( c . dir , n ame)
meta , _ , numHits , err := c . statCachedMeta ( ctx , cacheDir )
if err != nil {
// delete any partially filled cache entry left behind.
removeAll ( cacheDir )
continue
// Proceed to next file.
return nil
}
// stat all cached file ranges and cacheDataFile.
cachedFiles := fiStatFn ( meta . Ranges , cacheDataFile , pathJoin ( c . dir , obj . N ame( ) ) )
cachedFiles := fiStatFn ( meta . Ranges , cacheDataFile , pathJoin ( c . dir , n ame) )
objInfo := meta . ToObjectInfo ( "" , "" )
cc := cacheControlOpts ( objInfo )
for fname , fi := range cachedFiles {
@ -291,9 +294,11 @@ func (c *diskCache) purge(ctx context.Context) {
logger . LogIf ( ctx , err )
}
scorer . adjustSaveBytes ( - fi . Size ( ) )
// break early if sufficient disk space reclaimed.
if c . diskUsageLow ( ) {
return
// if we found disk usage is already low, we return nil filtering is complete.
return errDoneForNow
}
}
continue
@ -305,12 +310,24 @@ func (c *diskCache) purge(ctx context.Context) {
if err != nil || ( fi . ModTime ( ) . Before ( expiry ) && len ( cachedFiles ) == 0 ) {
removeAll ( cacheDir )
scorer . adjustSaveBytes ( - fi . Size ( ) )
continue
// Proceed to next file.
return nil
}
// if we found disk usage is already low, we return nil filtering is complete.
if c . diskUsageLow ( ) {
return
return errDoneForNow
}
// Proceed to next file.
return nil
}
if err := readDirFilterFn ( c . dir , filterFn ) ; err != nil {
logger . LogIf ( ctx , err )
return
}
for _ , path := range scorer . fileNames ( ) {
removeAll ( path )
slashIdx := strings . LastIndex ( path , SlashSeparator )