use of org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystemPositionedReadable in project ignite by apache.
the class IgfsImpl method open.
/**
* {@inheritDoc}
*/
@Override
public IgfsInputStream open(final IgfsPath path, final int bufSize, final int seqReadsBeforePrefetch) {
A.notNull(path, "path");
A.ensure(bufSize >= 0, "bufSize >= 0");
A.ensure(seqReadsBeforePrefetch >= 0, "seqReadsBeforePrefetch >= 0");
return safeOp(new Callable<IgfsInputStream>() {
@Override
public IgfsInputStream call() throws Exception {
if (log.isDebugEnabled())
log.debug("Open file for reading [path=" + path + ", bufSize=" + bufSize + ']');
int bufSize0 = bufSize == 0 ? cfg.getBufferSize() : bufSize;
IgfsMode mode = resolveMode(path);
switch(mode) {
case PRIMARY:
{
IgfsEntryInfo info = meta.infoForPath(path);
if (info == null)
throw new IgfsPathNotFoundException("File not found: " + path);
if (!info.isFile())
throw new IgfsPathIsDirectoryException("Failed to open file (not a file): " + path);
// Input stream to read data from grid cache with separate blocks.
IgfsInputStreamImpl os = new IgfsInputStreamImpl(igfsCtx, path, info, cfg.getPrefetchBlocks(), seqReadsBeforePrefetch, null, info.length(), info.blockSize(), info.blocksCount(), false);
IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EVT_IGFS_FILE_OPENED_READ);
return os;
}
case DUAL_ASYNC:
case DUAL_SYNC:
{
assert IgfsUtils.isDualMode(mode);
IgfsSecondaryInputStreamDescriptor desc = meta.openDual(secondaryFs, path, bufSize0);
IgfsEntryInfo info = desc.info();
IgfsInputStreamImpl os = new IgfsInputStreamImpl(igfsCtx, path, info, cfg.getPrefetchBlocks(), seqReadsBeforePrefetch, desc.reader(), info.length(), info.blockSize(), info.blocksCount(), false);
IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EVT_IGFS_FILE_OPENED_READ);
return os;
}
case PROXY:
{
assert secondaryFs != null;
IgfsFile info = info(path);
if (info == null)
throw new IgfsPathNotFoundException("File not found: " + path);
if (!info.isFile())
throw new IgfsPathIsDirectoryException("Failed to open file (not a file): " + path);
IgfsSecondaryFileSystemPositionedReadable secReader = new IgfsLazySecondaryFileSystemPositionedReadable(secondaryFs, path, bufSize);
long len = info.length();
int blockSize = info.blockSize() > 0 ? info.blockSize() : cfg.getBlockSize();
long blockCnt = len / blockSize;
if (len % blockSize != 0)
blockCnt++;
IgfsInputStream os = new IgfsInputStreamImpl(igfsCtx, path, null, cfg.getPrefetchBlocks(), seqReadsBeforePrefetch, secReader, info.length(), blockSize, blockCnt, true);
IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EVT_IGFS_FILE_OPENED_READ);
return os;
}
default:
assert false : "Unexpected mode " + mode;
return null;
}
}
});
}
use of org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystemPositionedReadable in project ignite by apache.
the class IgfsMetaManager method appendDual.
/**
* Append to a file in DUAL mode.
*
* @param fs File system.
* @param path Path.
* @param bufSize Buffer size.
* @param create Create flag.
* @return Output stream descriptor.
* @throws IgniteCheckedException If output stream open for append has failed.
*/
public IgfsCreateResult appendDual(final IgfsSecondaryFileSystem fs, final IgfsPath path, final int bufSize, final boolean create) throws IgniteCheckedException {
if (busyLock.enterBusy()) {
try {
assert fs != null;
assert path != null;
// Events to fire (can be done outside of a transaction).
final Deque<IgfsEvent> pendingEvts = new LinkedList<>();
SynchronizationTask<IgfsCreateResult> task = new SynchronizationTask<IgfsCreateResult>() {
/**
* Container for the secondary file system output stream.
*/
private final T1<OutputStream> outT1 = new T1<>(null);
@Override
public IgfsCreateResult onSuccess(Map<IgfsPath, IgfsEntryInfo> infos) throws Exception {
validTxState(true);
final IgfsEntryInfo info = infos.get(path);
final IgfsEntryInfo lockedInfo;
if (info == null)
return onSuccessCreate(fs, path, true, /*simpleCreate*/
null, false, /*overwrite*/
bufSize, (short) 0, 0, null, infos, pendingEvts, outT1);
else {
if (info.isDirectory())
throw fsException("Failed to open output stream to the file in the " + "secondary file system because the path points to a directory: " + path);
outT1.set(fs.append(path, bufSize, false, null));
// Synchronize file ending.
long len = info.length();
int blockSize = info.blockSize();
int remainder = (int) (len % blockSize);
if (remainder > 0) {
int blockIdx = (int) (len / blockSize);
try (IgfsSecondaryFileSystemPositionedReadable reader = fs.open(path, bufSize)) {
IgniteInternalFuture<byte[]> fut = igfsCtx.data().dataBlock(info, path, blockIdx, reader);
assert fut != null;
fut.get();
}
}
if (info.lockId() != null) {
throw fsException("Failed to open file (file is opened for writing) [path=" + path + ", fileId=" + info.id() + ", lockId=" + info.lockId() + ']');
}
// Set lock and return.
lockedInfo = invokeLock(info.id(), false);
}
if (evts.isRecordable(EventType.EVT_IGFS_FILE_OPENED_WRITE))
pendingEvts.add(new IgfsEvent(path, locNode, EventType.EVT_IGFS_FILE_OPENED_WRITE));
return new IgfsCreateResult(lockedInfo, outT1.get());
}
@Override
public IgfsCreateResult onFailure(@Nullable Exception err) throws IgniteCheckedException {
U.closeQuiet(outT1.get());
U.error(log, "File append in DUAL mode failed [path=" + path + ", bufferSize=" + bufSize + ']', err);
throw new IgniteCheckedException("Failed to append to the file due to secondary file " + "system exception: " + path, err);
}
};
try {
return synchronizeAndExecute(task, fs, !create, /*strict*/
path);
} finally {
for (IgfsEvent evt : pendingEvts) evts.record(evt);
}
} finally {
busyLock.leaveBusy();
}
} else
throw new IllegalStateException("Failed to append to file in DUAL mode because Grid is stopping: " + path);
}
Aggregations