Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public interface StorageServiceConfig
@WithName( "bucket.name" )
@WithDefault( "test" )
String bucketName();

@WithName( "filesystemsCacheTtlSeconds" )
@WithDefault( "600" )
int filesystemsCacheTtlSeconds();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.quarkus.runtime.Startup;
import org.apache.commons.io.IOUtils;
import org.commonjava.service.storage.config.StorageServiceConfig;
import org.commonjava.service.storage.dto.*;
import org.commonjava.storage.pathmapped.core.PathMappedFileManager;
import org.commonjava.storage.pathmapped.model.FileChecksum;
Expand Down Expand Up @@ -61,10 +62,16 @@ public class StorageController
@Inject
PathMappedFileManager fileManager;

@Inject
StorageServiceConfig config;

private ObjectMapper objectMapper = new ObjectMapper();

private static final int DEFAULT_RECURSIVE_LIST_LIMIT = 5000;

// Cache for filesystems list
private volatile CachedFilesystems cachedFilesystems;

public InputStream openInputStream( String fileSystem, String path) throws IOException
{
return fileManager.openInputStream( fileSystem, path );
Expand Down Expand Up @@ -248,16 +255,36 @@ public BatchDeleteResult purgeFilesystem(String filesystem)
{
fileManager.purgeFilesystem( statistics );
}
invalidateFilesystemsCache();
return ret;
}

public Collection<String> getFilesystems()
{
CachedFilesystems cached = cachedFilesystems;
long now = System.currentTimeMillis();

// Check if cache is valid
if ( cached != null && ( now - cached.timestamp ) < TimeUnit.SECONDS.toMillis( config.filesystemsCacheTtlSeconds() ) )
{
logger.debug( "Returning cached filesystems list (age: {}ms)", now - cached.timestamp );
return cached.filesystems;
}

// Cache miss or expired - fetch from database
logger.debug( "Cache miss or expired, fetching filesystems from database" );
Collection<? extends Filesystem> filesystems = fileManager.getFilesystems();
Collection<String> result;
if ( filesystems != null ) {
return filesystems.stream().map(filesystem -> filesystem.getFilesystem()).sorted().collect(Collectors.toList());
result = filesystems.stream().map(filesystem -> filesystem.getFilesystem()).sorted().collect(Collectors.toList());
} else {
result = emptyList();
}
return emptyList();

// Update cache
cachedFilesystems = new CachedFilesystems( result, now );
logger.debug( "Cached filesystems list (size: {})", result.size() );
return result;
}

public Collection<? extends Filesystem> getEmptyFilesystems()
Expand All @@ -271,6 +298,7 @@ public void purgeEmptyFilesystems()
{
Collection<? extends Filesystem> ret = getEmptyFilesystems();
ret.forEach( filesystem -> fileManager.purgeFilesystem( filesystem ));
invalidateFilesystemsCache();
}

/**
Expand Down Expand Up @@ -426,4 +454,28 @@ public FileCopyResult copy(FileCopyRequest request)

return new FileCopyResult( true, completed, skipped );
}

/**
* Invalidates the filesystems cache. Should be called whenever filesystems are added or removed.
*/
private void invalidateFilesystemsCache()
{
logger.debug( "Invalidating filesystems cache" );
cachedFilesystems = null;
}

/**
* Simple cache entry for filesystems list.
*/
private static class CachedFilesystems
{
final Collection<String> filesystems;
final long timestamp;

CachedFilesystems( Collection<String> filesystems, long timestamp )
{
this.filesystems = filesystems;
this.timestamp = timestamp;
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ storage:
readonly: false
deduplicatePattern: "(generic-http|npm).+"
removableFilesystemPattern: ".+:(remote|group):.+"
filesystemsCacheTtlSeconds: 600
#type: s3
#bucket:
# name: test
Expand Down