@Test public void testDelegateOutStreamMethods() throws IOException { // mock the output stream final FSDataOutputStream mockOut = mock(FSDataOutputStream.class); final long outPos = 46651L; when(mockOut.getPos()).thenReturn(outPos); final FileSystem fs = mock(FileSystem.class); when(fs.create(any(Path.class), any(WriteMode.class))).thenReturn(mockOut); final LimitedConnectionsFileSystem lfs = new LimitedConnectionsFileSystem(fs, 100); final FSDataOutputStream out = lfs.create(mock(Path.class), WriteMode.OVERWRITE); // validate the output stream out.write(77); verify(mockOut).write(77); { byte[] bytes = new byte[1786]; out.write(bytes, 100, 111); verify(mockOut).write(bytes, 100, 111); } assertEquals(outPos, out.getPos()); out.flush(); verify(mockOut).flush(); out.sync(); verify(mockOut).sync(); out.close(); verify(mockOut).close(); }
@Override public void write(int b) throws IOException { out.write(b); }
@Override public void close() throws IOException { final FSDataOutputStream s = this.stream; if (s != null) { this.stream = null; s.close(); } }
@Override public long getPos() throws IOException { return out.getPos(); }
@Override public void flush() throws IOException { outputStream.flush(); }
@Override public void sync() throws IOException { outputStream.sync(); }
@Test public void testWithSafetyNet() throws Exception { final String entropyKey = "__ekey__"; final String entropyValue = "abc"; final File folder = TMP_FOLDER.newFolder(); final Path path = new Path(Path.fromLocalFile(folder), entropyKey + "/path/"); final Path pathWithEntropy = new Path(Path.fromLocalFile(folder), entropyValue + "/path/"); TestEntropyInjectingFs efs = new TestEntropyInjectingFs(entropyKey, entropyValue); FSDataOutputStream out; FileSystemSafetyNet.initializeSafetyNetForThread(); FileSystem fs = FileSystemSafetyNet.wrapWithSafetyNetWhenActivated(efs); try { OutputStreamAndPath streamAndPath = EntropyInjector.createEntropyAware( fs, path, WriteMode.NO_OVERWRITE); out = streamAndPath.stream(); assertEquals(pathWithEntropy, streamAndPath.path()); } finally { FileSystemSafetyNet.closeSafetyNetAndGuardedResourcesForThread(); } // check that the safety net closed the stream try { out.write(42); out.flush(); fail("stream should be already close and hence fail with an exception"); } catch (IOException ignored) {} }
@Override public void write(byte[] buffer, int off, int len) throws IOException { out.write(buffer, off, len); }
@Override public void close() throws IOException { outputStream.close(); }
@Override public long getPos() throws IOException { return outputStream.getPos(); }
@Override public void flush() throws IOException { out.flush(); }
@Override public void sync() throws IOException { try { originalStream.sync(); } catch (IOException e) { handleIOException(e); } }
@Override public void write(int b) throws IOException { outputStream.write(b); }
@Override public void close() throws IOException { if (!closed) { closed = true; registry.unregisterCloseable(this); outputStream.close(); } }
@Override public void putJobGraph(SubmittedJobGraph jobGraph) throws Exception { checkNotNull(jobGraph, "Job graph"); String path = getPathForJob(jobGraph.getJobId()); LOG.debug("Adding job graph {} to {}{}.", jobGraph.getJobId(), basePath, path); synchronized (cacheLock) { verifyIsRunning(); Path jobGraphPath = new Path(basePath + path); try { FSDataOutputStream outputStream = jobsFileSystem.create(jobGraphPath, true); outputStream.write(InstantiationUtil.serializeObject(jobGraph)); outputStream.close(); } catch (Exception e) { throw new RuntimeException("Storing job graph " + jobGraph.getJobId() + " failed", e); } } LOG.info("Added job graph {} to {}{}.", jobGraph, basePath, path); }
@Override public long getPos() throws IOException { try { return originalStream.getPos(); } catch (IOException e) { handleIOException(e); return -1; // silence the compiler } }
@Override public void flush() throws IOException { try { originalStream.flush(); } catch (IOException e) { handleIOException(e); } }
@Override public void sync() throws IOException { outputStream.sync(); }