Skip to content

Commit

Permalink
#1 nio plugin development
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-conway committed Jun 20, 2017
1 parent 4c93f1b commit d3b11ec
Show file tree
Hide file tree
Showing 21 changed files with 470 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
package org.irods.jargon2.core.pub.io.plugin.nio;

import java.io.IOException;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Path;

import org.irods.jargon2.core.context.IOContext;
import org.irods.jargon2.core.context.monitor.IOMonitor;
import org.irods.jargon2.core.context.monitor.IOMonitorStatusReport;
import org.irods.jargon2.core.context.monitor.IOMonitorStatusReport.IOOperType;
import org.irods.jargon2.core.ioplugin.AbstractFileIOOperations;
import org.irods.jargon2.core.ioplugin.FileIOOperations;
import org.irods.jargon2.exception.io.JargonIOException;
import org.irods.jargon2.utils.exception.bundle.ExceptionResourceKeys;
Expand All @@ -25,38 +28,61 @@
* @author mconway
*
*/
public class NIOFileIOOperationsImpl implements FileIOOperations {
public class NIOFileIOOperationsImpl extends AbstractFileIOOperations implements FileIOOperations {

public static final Logger log = LoggerFactory.getLogger(NIOFileIOOperationsImpl.class);

/*
* (non-Javadoc)
/**
* Constructor taking a context
*
* @see org.irods.jargon2.core.ioplugin.FileIOOperations#
* transferLocalFileToIrodsSingleBuffer(java.nio.file.Path,
* java.nio.channels.Channel, org.irods.jargon2.core.ioplugin.IOMonitor,
* org.irods.jargon2.core.ioplugin.IOContext)
* @param ioContext
*/
public NIOFileIOOperationsImpl(IOContext ioContext) {
super(ioContext);
}

@Override
public void transferLocalFileToIrodsSingleBuffer(Path path, Channel channel, IOMonitor ioMonitor,
IOContext ioContext) throws JargonIOException {
public void transferLocalFileToIrodsSingleBuffer(Path path, WritableByteChannel channel, IOMonitor ioMonitor)
throws JargonIOException {
log.info("transferLocalFileToIrodsSingleBuffer()");
if (path == null) {
throw new IllegalArgumentException("null path");
throw new IllegalArgumentException(
MessageUtil.formatMessage(ExceptionResourceKeys.NULL_OR_EMPTY_PARAMETER, "path"));
}

if (channel == null) {
throw new IllegalArgumentException("null channel");
throw new IllegalArgumentException(
MessageUtil.formatMessage(ExceptionResourceKeys.NULL_OR_EMPTY_PARAMETER, "channel"));
}

log.info("path:{}", path);
log.info("channel:{}", channel);
log.info("ioMonitor:{}", ioMonitor);
log.info("ioContext:{}", ioContext);

FileChannel fileChannel;
try {
FileChannel.open(path);
fileChannel = FileChannel.open(path);

long startTime = System.currentTimeMillis();
if (getIoContext().getJargonProperties().isUseFastChannelCopy()) {
log.info("fast channel copy for file");
fileChannel.transferTo(0, fileChannel.size(), channel);
long endTime = System.currentTimeMillis();
log.info("successfully transferred");
if (getIoContext().getJargonProperties().isInstrument()) {
IOMonitorStatusReport statusReport = new IOMonitorStatusReport();
statusReport.setCompleteTimeInMillisthisOper(endTime);
statusReport.setIoOperType(IOOperType.OUTPUT_WRITE);
statusReport.setSizeThisOperation(fileChannel.size());
statusReport.setStartInMillisThisOper(startTime);
statusReport.setTotalCompleted(statusReport.getSizeThisOperation());
statusReport.setTotalSize(statusReport.getSizeThisOperation());
getIoContext().getInstrumentationSink().reportObservation(statusReport);
}
} else {
log.error("unsupported transfer method");
throw new UnsupportedOperationException();
}
} catch (IOException e) {
log.error("I/O Exception opening path:{}", path, e);
throw new JargonIOException(MessageUtil.formatMessage(ExceptionResourceKeys.EXCEPT_lOCAL_IO, path), e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.irods.jargon2.core.pub.io.plugin.nio;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ NIOFileIOOperationsImplTest.class })
public class AllTests {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.irods.jargon2.core.pub.io.plugin.nio;

import static org.junit.Assert.fail;

import org.junit.Test;

public class NIOFileIOOperationsImplTest {

@Test
public void testTransferLocalFileToIrodsSingleBuffer() {
fail("Not yet implemented");
}

@Test(expected = IllegalArgumentException.class)
public void testNIOFileIOOperationsImpl() {
// IOContext ioContext = new IOContext();
new NIOFileIOOperationsImpl(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
public class CachedJargonProperties implements JargonProperties {

public static final String CONNECTION_TIMEOUT_IN_SECONDS = "connection.timeout";
public static final String INSTRUMENTED = "instrumented";
public static final String IO_STYLE = "io.style";
public static final String IO_FAST_CHANNEL_COPY = "io.fast.channel.copy";
public static final String IO_NIO_DIRECT = "io.nio.direct";

private ConcurrentHashMap<String, String> propertiesCache = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -46,37 +49,47 @@ public void setConnectionTimeoutInSeconds(int timeout) {

@Override
public EnumIoStyle getEnumIoStyle() {
// TODO Auto-generated method stub
return null;
return EnumIoStyle.valueOf(propertiesCache.getOrDefault(IO_STYLE, EnumIoStyle.NIO.toString()));
}

@Override
public void setEnumIoStyle(EnumIoStyle enumIoStyle) {
// TODO Auto-generated method stub
propertiesCache.put(IO_STYLE, enumIoStyle.toString());

}

@Override
public boolean isUseNioDirect() {
// TODO Auto-generated method stub
return false;
return valAsBooleanOrFalse(IO_NIO_DIRECT);

}

@Override
public void setUseNioDirect(boolean useNioDirect) {
// TODO Auto-generated method stub
propertiesCache.put(IO_NIO_DIRECT, String.valueOf(useNioDirect));

}

@Override
public boolean isUseFastChannelCopy() {
// TODO Auto-generated method stub
return false;
return valAsBooleanOrFalse(IO_FAST_CHANNEL_COPY);

}

@Override
public void setUseFastChannelCopy(boolean useFastChannelCopy) {
// TODO Auto-generated method stub
propertiesCache.put(IO_FAST_CHANNEL_COPY, String.valueOf(useFastChannelCopy));

}

@Override
public boolean isInstrument() {
return valAsBooleanOrFalse(INSTRUMENTED);
}

@Override
public void setInstrument(boolean instrument) {
propertiesCache.put(INSTRUMENTED, String.valueOf(instrument));

}

Expand All @@ -89,4 +102,13 @@ private int valAsIntOrZero(final String key) {
}
}

private boolean valAsBooleanOrFalse(final String key) {
String stringVal = propertiesCache.get(key);
if (stringVal == null) {
return false;
} else {
return Boolean.valueOf(stringVal);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,37 @@
public interface JargonProperties {

/*
* iRODS connection properties
* -------------------------------------------------------------------------
* ------------------------- General behavior properties
*/

/**
* Is performance logging turned on?
*
* @return <code>boolean</code> if logging is turned on
*/
boolean isInstrument();

/**
*
* Turn insrumentation logging on or off, <code>true</code> turns on logging
* to the configured {@link IOMonitor}
*
* @param instrument
*/
void setInstrument(boolean instrument);

/*
* -------------------------------------------------------------------------
* ------------------------- iRODS connection properties
*/
int getConnectionTimeoutInSeconds();

void setConnectionTimeoutInSeconds(int connectionTimeoutInSeconds);

/*
* General i/o properties
* -------------------------------------------------------------------------
* ------------------------- General i/o properties
*/

/**
Expand All @@ -31,7 +54,8 @@ public interface JargonProperties {
void setEnumIoStyle(EnumIoStyle enumIoStyle);

/*
* local file system i/o properties
* -------------------------------------------------------------------------
* ------------------------- local file system i/o properties
*/

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
*
*/
package org.irods.jargon2.common.confg.props;

/**
* Describes a source to produce a configured <code>JargonProperties</code>.
* This can load from a properties file, database, etc
*
* @author mcc
*
*/
public interface JargonPropertiesLoader {

JargonProperties loadJargonProperties();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
*
*/
package org.irods.jargon2.common.confg.props;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.irods.jargon2.common.confg.enumz.EnumIoStyle;
import org.irods.jargon2.exception.configuration.JargonConfigurationRuntimeException;
import org.irods.jargon2.utils.exception.bundle.ExceptionResourceKeys;
import org.irods.jargon2.utils.exception.bundle.MessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Load <code>JargonProperties</code> via a provided java
* <code>Properties</code> file
*
* @author mcc
*
*/
public class PropertiesFileJargonPropertiesLoader implements JargonPropertiesLoader {

private final String resourcePath;
public static final Logger log = LoggerFactory.getLogger(PropertiesFileJargonPropertiesLoader.class);

/**
*
*/
public PropertiesFileJargonPropertiesLoader(final String resourcePath) {
if (resourcePath == null || resourcePath.isEmpty()) {
throw new IllegalArgumentException(
MessageUtil.formatMessage(ExceptionResourceKeys.NULL_OR_EMPTY_PARAMETER, "resourcePath"));
}
this.resourcePath = resourcePath;
}

/*
* (non-Javadoc)
*
* @see org.irods.jargon2.common.confg.props.JargonPropertiesLoader#
* loadJargonProperties()
*/
@Override
public JargonProperties loadJargonProperties() {
log.info("loadJargonProperties() using:{}", resourcePath);
final Properties properties = new Properties();
try (final InputStream stream = this.getClass().getResourceAsStream(resourcePath)) {
properties.load(stream);
} catch (IOException e) {
log.error("exception loading properties from:{}", resourcePath, e);
throw new JargonConfigurationRuntimeException(
MessageUtil.formatMessage(ExceptionResourceKeys.CONFIG_EXCEPT_LOAD_CONFIG), e);
}

JargonProperties jargonProperties = new CachedJargonProperties();
jargonProperties.setConnectionTimeoutInSeconds(
Integer.parseInt(properties.getProperty(CachedJargonProperties.CONNECTION_TIMEOUT_IN_SECONDS)));
jargonProperties.setEnumIoStyle(EnumIoStyle.valueOf(properties.getProperty(CachedJargonProperties.IO_STYLE)));
jargonProperties.setInstrument(Boolean.valueOf(properties.getProperty(CachedJargonProperties.INSTRUMENTED)));
jargonProperties.setUseFastChannelCopy(
Boolean.valueOf(properties.getProperty(CachedJargonProperties.IO_FAST_CHANNEL_COPY)));
jargonProperties.setUseNioDirect(Boolean.valueOf(properties.getProperty(CachedJargonProperties.IO_NIO_DIRECT)));
return jargonProperties;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

/**
* Various utilities to monitor performance and activity
*
* @author mcc
*
*/
package org.irods.jargon2.context.monitors;
Loading

0 comments on commit d3b11ec

Please sign in to comment.