Skip to content

Commit

Permalink
synthetic wait time generator of fs calls
Browse files Browse the repository at this point in the history
Useful for testing how the system reacts on random wait times on fs
calls.
  • Loading branch information
andreaceccanti committed Mar 18, 2018
1 parent 02243e8 commit a9b92af
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package it.grid.storm.filesystem;

import static java.lang.Integer.parseInt;
import static java.lang.System.getProperty;
import static java.util.Objects.isNull;

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import it.grid.storm.griduser.LocalUser;

public class RandomWaitFilesystemAdapter implements FilesystemIF {

public static final Logger LOG = LoggerFactory.getLogger(RandomWaitFilesystemAdapter.class);

public static final String RANDOM_SLEEP_ENABLED = "storm.fs.sleep.enabled";
public static final String MAX_WAIT_TIME_MSEC = "storm.fs.sleep.maxTimeMs";
public static final String MIN_WAIT_TIME_MSEC = "storm.fs.sleep.minTimeMs";

final FilesystemIF delegate;
final Random random = new Random();
int maxWaitTime = 1000;
int minWaitTime = 10;

private long randomTimeinMsec() {

int time = random.nextInt(maxWaitTime);
if (time < minWaitTime) {
time = minWaitTime;
}

return time;
}

private RandomWaitFilesystemAdapter(FilesystemIF delegate) {

LOG.warn("RandomWaitFilesystemAdapter is ENABLED ({} property is defined)",
RANDOM_SLEEP_ENABLED);

LOG.warn("This adapter introduces synthentic sleep time on each fs call!! Not advisable "
+ "in production");

this.delegate = delegate;
maxWaitTime = parseInt(getProperty(MAX_WAIT_TIME_MSEC, "1000"));
minWaitTime = parseInt(getProperty(MIN_WAIT_TIME_MSEC, "10"));
}


public static FilesystemIF maybeWrapFilesystem(FilesystemIF delegate) {
if (!isNull(getProperty(RANDOM_SLEEP_ENABLED))) {
return new RandomWaitFilesystemAdapter(delegate);
}

return delegate;
}

protected void sleepSomeTime() {
try {
long sleepTime = randomTimeinMsec();
Thread.sleep(sleepTime);
LOG.debug("Slept {} msec", sleepTime);
} catch (InterruptedException e) {
LOG.warn("Interrupted", e);
Thread.currentThread().interrupt();
}
}


@Override
public long getSize(String file) {
sleepSomeTime();
return delegate.getSize(file);
}

public long getLastModifiedTime(String fileOrDirectory) {
sleepSomeTime();
return delegate.getLastModifiedTime(fileOrDirectory);
}

public long getExactSize(String file) {
sleepSomeTime();
return delegate.getExactSize(file);
}

public long getExactLastModifiedTime(String fileOrDirectory) {
sleepSomeTime();
return delegate.getExactLastModifiedTime(fileOrDirectory);
}

public int truncateFile(String filename, long desired_size) {
sleepSomeTime();
return delegate.truncateFile(filename, desired_size);
}

public boolean isFileOnDisk(String filename) {
sleepSomeTime();
return delegate.isFileOnDisk(filename);
}

public long getFileBlockSize(String filename) {
sleepSomeTime();
return delegate.getFileBlockSize(filename);
}

public void changeFileGroupOwnership(String filename, String groupName) {
sleepSomeTime();
delegate.changeFileGroupOwnership(filename, groupName);
}

public long getFreeSpace() {
sleepSomeTime();
return delegate.getFreeSpace();
}

public boolean canAccess(LocalUser u, String fileOrDirectory, FilesystemPermission accessMode) {
sleepSomeTime();
return delegate.canAccess(u, fileOrDirectory, accessMode);
}

public FilesystemPermission getEffectiveGroupPermission(LocalUser u, String fileOrDirectory) {
sleepSomeTime();
return delegate.getEffectiveGroupPermission(u, fileOrDirectory);
}

public FilesystemPermission getEffectiveUserPermission(LocalUser u, String fileOrDirectory) {
sleepSomeTime();
return delegate.getEffectiveUserPermission(u, fileOrDirectory);
}

public FilesystemPermission getGroupPermission(LocalUser u, String fileOrDirectory) {
sleepSomeTime();
return delegate.getGroupPermission(u, fileOrDirectory);
}

public FilesystemPermission getUserPermission(LocalUser u, String fileOrDirectory) {
sleepSomeTime();
return delegate.getUserPermission(u, fileOrDirectory);
}

public FilesystemPermission grantGroupPermission(LocalUser u, String fileOrDirectory,
FilesystemPermission permission) {
sleepSomeTime();
return delegate.grantGroupPermission(u, fileOrDirectory, permission);
}

public FilesystemPermission grantUserPermission(LocalUser u, String fileOrDirectory,
FilesystemPermission permission) {
sleepSomeTime();
return delegate.grantUserPermission(u, fileOrDirectory, permission);
}

public FilesystemPermission removeGroupPermission(LocalUser u, String fileOrDirectory) {
sleepSomeTime();
return delegate.removeGroupPermission(u, fileOrDirectory);
}

public FilesystemPermission removeUserPermission(LocalUser u, String fileOrDirectory) {
sleepSomeTime();
return delegate.removeUserPermission(u, fileOrDirectory);
}

public FilesystemPermission revokeGroupPermission(LocalUser u, String fileOrDirectory,
FilesystemPermission permission) {
sleepSomeTime();
return delegate.revokeGroupPermission(u, fileOrDirectory, permission);
}

public FilesystemPermission revokeUserPermission(LocalUser u, String fileOrDirectory,
FilesystemPermission permission) {
sleepSomeTime();
return delegate.revokeUserPermission(u, fileOrDirectory, permission);
}

public FilesystemPermission setGroupPermission(LocalUser u, String fileOrDirectory,
FilesystemPermission permission) {
sleepSomeTime();
return delegate.setGroupPermission(u, fileOrDirectory, permission);
}

public FilesystemPermission setUserPermission(LocalUser u, String fileOrDirectory,
FilesystemPermission permission) {
sleepSomeTime();
return delegate.setUserPermission(u, fileOrDirectory, permission);
}

}
25 changes: 14 additions & 11 deletions src/main/java/it/grid/storm/namespace/model/VirtualFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@

package it.grid.storm.namespace.model;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.slf4j.Logger;

import it.grid.storm.balancer.BalancingStrategy;
import it.grid.storm.balancer.Node;
import it.grid.storm.catalogs.ReservedSpaceCatalog;
Expand All @@ -31,6 +41,7 @@
import it.grid.storm.filesystem.LocalFile;
import it.grid.storm.filesystem.MetricsFilesystemAdapter;
import it.grid.storm.filesystem.MockSpaceSystem;
import it.grid.storm.filesystem.RandomWaitFilesystemAdapter;
import it.grid.storm.filesystem.ReservationException;
import it.grid.storm.filesystem.Space;
import it.grid.storm.filesystem.SpaceSystem;
Expand Down Expand Up @@ -59,16 +70,6 @@
import it.grid.storm.srm.types.TSpaceToken;
import it.grid.storm.srm.types.TSpaceType;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.slf4j.Logger;

/**
* <p> Title: </p>
*
Expand Down Expand Up @@ -140,7 +141,7 @@ public void setFSDriver(Class fsDriver) throws NamespaceException {
} else {
this.genericFS = makeFSInstance();
}

fsWrapper = RandomWaitFilesystemAdapter.maybeWrapFilesystem(fsWrapper);
this.fsWrapper = new MetricsFilesystemAdapter(
new Filesystem(getFSDriverInstance()),
StormMetricRegistry.INSTANCE.getRegistry());
Expand Down Expand Up @@ -417,6 +418,8 @@ public FilesystemIF getFilesystem() throws NamespaceException {

FilesystemIF fs = new Filesystem(getFSDriverInstance());

fs = RandomWaitFilesystemAdapter.maybeWrapFilesystem(fs);

fsWrapper = new MetricsFilesystemAdapter(fs,
StormMetricRegistry.INSTANCE.getRegistry());

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/it/grid/storm/synchcall/FileSystemUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package it.grid.storm.synchcall;

import static it.grid.storm.filesystem.RandomWaitFilesystemAdapter.maybeWrapFilesystem;

import org.slf4j.Logger;

import it.grid.storm.filesystem.Filesystem;
import it.grid.storm.filesystem.FilesystemIF;
import it.grid.storm.filesystem.LocalFile;
Expand All @@ -27,8 +31,6 @@
import it.grid.storm.namespace.NamespaceException;
import it.grid.storm.namespace.VirtualFSInterface;

import org.slf4j.Logger;

/**
* <p>
* Title:
Expand Down Expand Up @@ -74,7 +76,10 @@ public static LocalFile getLocalFileByAbsolutePath(String absolutePath)
try {
fsDriver = (genericfs) (vfs.getFSDriver()).newInstance();

FilesystemIF wrappedFs = new Filesystem(fsDriver);
FilesystemIF wrappedFs = new Filesystem(fsDriver);

wrappedFs = maybeWrapFilesystem(wrappedFs);

fs = new MetricsFilesystemAdapter(wrappedFs,
StormMetricRegistry.INSTANCE.getRegistry());

Expand Down

0 comments on commit a9b92af

Please sign in to comment.