use of org.apache.ignite.events.IgfsEvent in project ignite by apache.
the class IgfsInputStreamImpl method close.
/**
* {@inheritDoc}
*/
@Override
public synchronized void close() throws IOException {
if (!closed) {
try {
if (secReader != null) {
// Close secondary input stream.
secReader.close();
// Ensuring local cache futures completion.
for (IgniteInternalFuture<byte[]> fut : locCache.values()) {
try {
fut.get();
} catch (IgniteCheckedException ignore) {
// No-op.
}
}
// Ensuring pending evicted futures completion.
while (!pendingFuts.isEmpty()) {
pendingFutsLock.lock();
try {
pendingFutsCond.await(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
// No-op.
} finally {
pendingFutsLock.unlock();
}
}
}
} catch (Exception e) {
throw new IOException("File to close the file: " + path, e);
} finally {
closed = true;
IgfsLocalMetrics metrics = igfsCtx.metrics();
metrics.addReadBytesTime(bytes, time);
metrics.decrementFilesOpenedForRead();
locCache.clear();
GridEventStorageManager evts = igfsCtx.kernalContext().event();
if (evts.isRecordable(EVT_IGFS_FILE_CLOSED_READ))
evts.record(new IgfsEvent(path, igfsCtx.localNode(), EVT_IGFS_FILE_CLOSED_READ, bytes()));
}
}
}
use of org.apache.ignite.events.IgfsEvent 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