/** {@inheritDoc} */ @Override public void writeData(HadoopIgfsStreamDelegate delegate, byte[] data, int off, int len) throws IOException { try { IgfsOutputStream stream = delegate.target(); stream.write(data, off, len); } catch (IllegalStateException | IOException e) { HadoopIgfsStreamEventListener lsnr = lsnrs.get(delegate); if (lsnr != null) lsnr.onError(e.getMessage()); if (e instanceof IllegalStateException) throw new IOException("Failed to write data to IGFS stream because Grid is stopping.", e); else throw e; } }
final AtomicReference<Exception> err = new AtomicReference<>(); igfs.create(FILE, false).close();
/** {@inheritDoc} */ @Override public void flush(HadoopIgfsStreamDelegate delegate) throws IOException { try { IgfsOutputStream stream = delegate.target(); stream.flush(); } catch (IllegalStateException | IOException e) { HadoopIgfsStreamEventListener lsnr = lsnrs.get(delegate); if (lsnr != null) lsnr.onError(e.getMessage()); if (e instanceof IllegalStateException) throw new IOException("Failed to flush data to IGFS stream because Grid is stopping.", e); else throw e; } }
out.transferFrom(in, writeLen);
@Override public void run() { try { int lvl = rand.nextInt(lvlCnt) + 1; IgfsPath parentPath = dirPaths.get(lvl).get(rand.nextInt(dirPaths.get(lvl).size())); IgfsPath path = new IgfsPath(parentPath, "newFile-" + fileCtr.incrementAndGet()); U.awaitQuiet(barrier); IgfsOutputStream os = null; try { os = igfs.create(path, true); os.write(chunk); } finally { U.closeQuiet(os); } } catch (IOException | IgniteException ignore) { // No-op. } } };
final AtomicReference<Exception> err = new AtomicReference<>(); igfs.create(FILE, false).close();
/** * Writes data to the file of the specified index and closes the output stream. * * @param igfs0 IGFS. * @param ios The output stream * @param fileIdx Th eindex of the file. * @throws IOException On error. */ void writeChunks0(IgfsEx igfs0, IgfsOutputStream ios, int fileIdx) throws IOException { try { byte[] data = createChunk(fileSize, fileIdx); writeFileChunks(ios, data); } finally { ios.flush(); U.closeQuiet(ios); awaitFileClose(igfs0, filePath(fileIdx)); } }
out.transferFrom(in, writeLen);
/** * @throws Exception If failed. */ @Test public void testReadFragmentizing() throws Exception { IgniteFileSystem igfs = grid(0).fileSystem("igfs"); IgfsPath path = new IgfsPath("/someFile"); try (IgfsOutputStream out = igfs.create(path, true)) { // Write 10 groups. for (int i = 0; i < 10 * IGFS_GROUP_SIZE; i++) { byte[] data = new byte[IGFS_BLOCK_SIZE]; Arrays.fill(data, (byte)i); out.write(data); } } long start = System.currentTimeMillis(); do { try (IgfsInputStream in = igfs.open(path)) { for (int i = 0; i < 10 * IGFS_GROUP_SIZE; i++) { for (int j = 0; j < IGFS_BLOCK_SIZE; j++) assertEquals(i & 0xFF, in.read()); } assertEquals(-1, in.read()); } } while (System.currentTimeMillis() - start < 7000); }
/** @throws Exception If failed. */ @Test public void testMultipleClose() throws Exception { IgniteFileSystem fs = igfsPrimary[0]; IgfsOutputStream out = fs.create(new IgfsPath("/primary/file"), false); out.close(); out.close(); IgfsInputStream in = fs.open(new IgfsPath("/primary/file")); in.close(); in.close(); IgfsMetrics m = fs.metrics(); assertEquals(0, m.filesOpenedForWrite()); assertEquals(0, m.filesOpenedForRead()); }
@SuppressWarnings("ThrowFromFinallyBlock") @Override public void run() { while (!stop.get() && err.get() == null) { IgfsOutputStream os = null; try { os = igfs.append(FILE, false); os.write(chunk); os.close(); chunksCtr.incrementAndGet(); } catch (IgniteException ignore) { // No-op. } catch (IOException e) { err.compareAndSet(null, e); } finally { if (os != null) try { os.close(); } catch (IOException ioe) { throw new IgniteException(ioe); } } } } }, threadCnt);
/** * Ensure that IGFS is able to stop in case not closed output stream exist. * * @throws Exception If failed. */ @Test public void testStop() throws Exception { create(igfs, paths(DIR, SUBDIR), null); IgfsOutputStream os = igfs.create(FILE, true); os.write(chunk); igfs.stop(true); // Reset test state. afterTestsStopped(); beforeTestsStarted(); }
/** * Test rename on the file when it was opened for write(create) and is not closed yet. * * @throws Exception If failed. */ @Test public void testCreateRenameNoClose() throws Exception { if (dual) return; create(igfs, paths(DIR, SUBDIR), null); IgfsOutputStream os = null; try { os = igfs.create(FILE, true); igfs.rename(FILE, FILE2); os.close(); } finally { U.closeQuiet(os); } }
@Override public Object call() throws Exception { IgfsOutputStream osErr = igfs(0).append(path, false); try { for (int i = 0; i < maxSize / writeChunkSize * GRID_CNT; ++i) osErr.write(chunk(writeChunkSize)); osErr.close(); return null; } catch (IOException e) { Throwable e0 = e; while (e0.getCause() != null) e0 = e0.getCause(); throw (Exception)e0; } finally { U.closeQuiet(osErr); } } }, IgniteOutOfMemoryException.class, "Not enough memory allocated");