Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add disk full protection #41

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
branches: [develop, master]
pull_request:
branches: [develop]
workflow_dispatch:

jobs:
all:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## START MAVEN

target/
.flattened-pom.xml
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
Expand Down
9 changes: 1 addition & 8 deletions grida-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>fr.insalyon.creatis</groupId>
<artifactId>grida</artifactId>
<version>2.3.0</version>
<version>${revision}${sha1}${changelist}</version>
</parent>

<artifactId>grida-client</artifactId>
Expand Down Expand Up @@ -39,13 +39,6 @@
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>fr.insalyon.creatis</groupId>
<artifactId>grida-common</artifactId>
Expand Down
11 changes: 1 addition & 10 deletions grida-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,8 @@
<parent>
<groupId>fr.insalyon.creatis</groupId>
<artifactId>grida</artifactId>
<version>2.3.0</version>
<version>${revision}${sha1}${changelist}</version>
</parent>

<artifactId>grida-common</artifactId>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
42 changes: 10 additions & 32 deletions grida-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>fr.insalyon.creatis</groupId>
<artifactId>grida</artifactId>
<version>2.3.0</version>
<version>${revision}${sha1}${changelist}</version>
</parent>

<artifactId>grida-server</artifactId>
Expand Down Expand Up @@ -38,26 +38,14 @@
</plugin>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</build>

<dependencies>

<!-- internal dependencies -->
<dependency>
<groupId>fr.insalyon.creatis</groupId>
Expand Down Expand Up @@ -85,33 +73,23 @@
</dependency>

<!-- END internal dependencies -->

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.13.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
-->
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.insalyon.creatis.grida.server.business;

import java.io.File;

import fr.insalyon.creatis.grida.server.Configuration;

public class DiskspaceManager {

public long getFreeSpace() {
return new File(".").getFreeSpace();
}

public long getTotalSpace() {
return new File(".").getTotalSpace();
}

public double getMinAvailableDiskSpace() {
return Configuration.getInstance().getMinAvailableDiskSpace();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,22 @@ public class OperationBusiness {

private static final Logger logger =
Logger.getLogger(OperationBusiness.class);
private Configuration configuration;
private String proxy;
private Operations operations;
private DiskspaceManager diskManager;

public OperationBusiness(String proxy) {
public OperationBusiness(String proxy, DiskspaceManager manager, Operations operations) {
this.proxy = proxy;
configuration = Configuration.getInstance();
operations = configuration.getOperations();
this.diskManager = manager;
this.operations = operations;
}

public OperationBusiness(String proxy) {
this(proxy, new DiskspaceManager(), Configuration.getInstance().getOperations());
}

public void setDiskManager(DiskspaceManager manager) {
this.diskManager = manager;
}

/**
Expand Down Expand Up @@ -356,14 +364,28 @@ public boolean isFolder(String path) throws BusinessException {
* @throws BusinessException
*/
public long getDataSize(String path) throws BusinessException {

try {
return operations.getDataSize(proxy, path);
} catch (OperationException ex) {
throw new BusinessException(ex);
}
}

/**
* This will check is there is enought of place on the grida server to transfer the file !
* @param pathFile (can be null if just want to check if there is enought of place, ex: folder creation)
* @throws BusinessException
*/
public void isTransferPossible(String pathFile) throws BusinessException {
axlbonnet marked this conversation as resolved.
Show resolved Hide resolved
long freeSpace = diskManager.getFreeSpace();
long totalSpace = diskManager.getTotalSpace();
long fileSize = pathFile != null ? getDataSize(pathFile) : 0;

if (freeSpace - fileSize < totalSpace * diskManager.getMinAvailableDiskSpace()) {
throw new BusinessException("Unable to download " + pathFile + "' due to disk space limits. Size: " + ((int) fileSize / 1024 / 1024) + " MB.");
}
}

/**
*
* @param errorFiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public class PoolBusiness {

private static final Logger logger = Logger.getLogger(PoolBusiness.class);
private PoolDAO poolDAO;
private DiskspaceManager diskManager;

public PoolBusiness() {

diskManager = new DiskspaceManager();
poolDAO = DAOFactory.getDAOFactory().getPoolDAO();
}

Expand Down Expand Up @@ -93,8 +94,8 @@ public String addOperation(String proxyFileName, String source, String dest,
Operation op = new Operation(id, source, dest, type, user,
proxyFileName, size);

long freeSpace = new File(".").getFreeSpace();
long totalSpace = new File(".").getTotalSpace();
long freeSpace = diskManager.getFreeSpace();
long totalSpace = diskManager.getTotalSpace();
if (freeSpace - size < totalSpace * Configuration.getInstance().getMinAvailableDiskSpace()) {
op.setStatus(Operation.Status.Failed);
logger.error("Unable to download '" + source + "' due to disk space limits. Size: " + ((int) size / 1024 / 1024) + " MB.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public CreateFolderCommand(Communication communication, String proxyFileName, St
public void execute() {

try {
operationBusiness.isTransferPossible(null);

operationBusiness.createFolder(newDir);
communication.sendSuccessMessage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void execute() {
* @throws Exception
*/
private String downloadFile(String fileName) throws BusinessException {
operationBusiness.isTransferPossible(remoteFilePath);

String destPath = operationBusiness.downloadFile(null, localDirPath,
fileName, remoteFilePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public GetRemoteFolderCommand(Communication communication,
public void execute() {

try {
operationBusiness.isTransferPossible(remoteDirPath);

String destPath = operationBusiness.downloadFolder(
null, localDirPath, remoteDirPath, zipResult);
communication.sendMessage(destPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

import fr.insalyon.creatis.grida.common.Communication;
import fr.insalyon.creatis.grida.common.bean.Operation;
import fr.insalyon.creatis.grida.server.Configuration;
import fr.insalyon.creatis.grida.server.business.BusinessException;
import fr.insalyon.creatis.grida.server.business.OperationBusiness;
import fr.insalyon.creatis.grida.server.business.PoolBusiness;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ public String uploadFile(String operationID, String proxy, String localFilePath,
logger.error("Cannot copy file to " + dest, e);
throw new OperationException(e);
}
try {
Files.delete(Paths.get(localFilePath));
} catch (AccessDeniedException e) {
logger.warn("cannot delete uploaded file " + localFilePath + " : access denied");
} catch (IOException e) {
logger.error("Cannot copy file to " + dest, e);
throw new OperationException(e);
}
FileUtils.deleteQuietly(new File(localFilePath));
axlbonnet marked this conversation as resolved.
Show resolved Hide resolved
return dest.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package fr.insalyon.creatis.server.business;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import fr.insalyon.creatis.grida.server.business.BusinessException;
import fr.insalyon.creatis.grida.server.business.DiskspaceManager;
import fr.insalyon.creatis.grida.server.business.OperationBusiness;
import fr.insalyon.creatis.grida.server.operation.Operations;

public class OperationBusinessTest {

private OperationBusiness business;

@Mock
private DiskspaceManager manager;

@Mock
private Operations operations;

@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
business = new OperationBusiness("", manager, operations);
}

@Test
public void transferImpossibleLarge() throws Exception {
when(operations.getDataSize("","")).thenReturn(101L);
when(manager.getTotalSpace()).thenReturn(200L);
when(manager.getFreeSpace()).thenReturn(100L);
when(manager.getMinAvailableDiskSpace()).thenReturn(Double.valueOf(0.1));

assertThrows(BusinessException.class, () -> business.isTransferPossible(""));
verify(operations, times(1)).getDataSize("", "");
}

@Test
public void transferImpossibleSmall() throws Exception {
when(operations.getDataSize("","")).thenReturn(1L);
when(manager.getTotalSpace()).thenReturn(200L);
when(manager.getFreeSpace()).thenReturn(20L);
when(manager.getMinAvailableDiskSpace()).thenReturn(Double.valueOf(0.1));

assertThrows(BusinessException.class, () -> business.isTransferPossible(""));
verify(operations, times(1)).getDataSize("", "");
}

@Test
public void transferPossibleLarge() throws Exception {
when(operations.getDataSize("","")).thenReturn(50L);

when(manager.getTotalSpace()).thenReturn(200L);
when(manager.getFreeSpace()).thenReturn(100L);
when(manager.getMinAvailableDiskSpace()).thenReturn(Double.valueOf(0.1));

assertDoesNotThrow(() -> business.isTransferPossible(""));
verify(operations, times(1)).getDataSize("", "");
}

@Test
public void transferPossibleSmall() throws Exception {
when(operations.getDataSize("","")).thenReturn(1L);
when(manager.getTotalSpace()).thenReturn(200L);
when(manager.getFreeSpace()).thenReturn(102L);
when(manager.getMinAvailableDiskSpace()).thenReturn(Double.valueOf(0.5));

assertDoesNotThrow(() -> business.isTransferPossible(""));
verify(operations, times(1)).getDataSize("", "");
}
}
2 changes: 1 addition & 1 deletion grida-standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>fr.insalyon.creatis</groupId>
<artifactId>grida</artifactId>
<version>2.3.0</version>
<version>${revision}${sha1}${changelist}</version>
</parent>

<artifactId>grida-standalone</artifactId>
Expand Down
Loading
Loading