@Override protected AbstractCloseableRegistry< WrappingProxyCloseable<? extends Closeable>, SafetyNetCloseableRegistry.PhantomDelegatingCloseableRef> createRegistry() { return new SafetyNetCloseableRegistry(); }
@Override protected boolean doUnRegister( @Nonnull WrappingProxyCloseable<? extends Closeable> closeable, @Nonnull Map<Closeable, PhantomDelegatingCloseableRef> closeableMap) { assert Thread.holdsLock(getSynchronizationLock()); Closeable innerCloseable = WrappingProxyUtil.stripProxy(closeable); return null != innerCloseable && closeableMap.remove(innerCloseable) != null; }
public static ClosingFSDataOutputStream wrapSafe( FSDataOutputStream delegate, SafetyNetCloseableRegistry registry, String debugInfo) throws IOException { ClosingFSDataOutputStream inputStream = new ClosingFSDataOutputStream(delegate, registry, debugInfo); registry.registerCloseable(inputStream); return inputStream; } }
@Test public void testReaperThreadSpawnAndStop() throws Exception { Assert.assertFalse(SafetyNetCloseableRegistry.isReaperThreadRunning()); try (SafetyNetCloseableRegistry ignored = new SafetyNetCloseableRegistry()) { Assert.assertTrue(SafetyNetCloseableRegistry.isReaperThreadRunning()); try (SafetyNetCloseableRegistry ignored2 = new SafetyNetCloseableRegistry()) { Assert.assertTrue(SafetyNetCloseableRegistry.isReaperThreadRunning()); } Assert.assertTrue(SafetyNetCloseableRegistry.isReaperThreadRunning()); } Assert.assertFalse(SafetyNetCloseableRegistry.isReaperThreadRunning()); } }
@Override public void close() throws IOException { if (!closed) { closed = true; registry.unregisterCloseable(this); inputStream.close(); } }
@After public void tearDown() { Assert.assertFalse(SafetyNetCloseableRegistry.isReaperThreadRunning()); }
@Override public void close() throws IOException { if (!closed) { closed = true; registry.unregisterCloseable(this); outputStream.close(); } }
public void setup(int maxStreams) { Assert.assertFalse(SafetyNetCloseableRegistry.isReaperThreadRunning()); this.closeableRegistry = createRegistry(); this.unclosedCounter = new AtomicInteger(0); this.streamOpenThreads = new ProducerThread[10]; for (int i = 0; i < streamOpenThreads.length; ++i) { streamOpenThreads[i] = createProducerThread(closeableRegistry, unclosedCounter, maxStreams); } }
/** * Activates the safety net for a thread. {@link FileSystem} instances obtained by the thread * that called this method will be guarded, meaning that their created streams are tracked and can * be closed via the safety net closing hook. * * <p>This method should be called at the beginning of a thread that should be guarded. * * @throws IllegalStateException Thrown, if a safety net was already registered for the thread. */ @Internal public static void initializeSafetyNetForThread() { SafetyNetCloseableRegistry oldRegistry = REGISTRIES.get(); checkState(null == oldRegistry, "Found an existing FileSystem safety net for this thread: %s " + "This may indicate an accidental repeated initialization, or a leak of the" + "(Inheritable)ThreadLocal through a ThreadPool.", oldRegistry); SafetyNetCloseableRegistry newRegistry = new SafetyNetCloseableRegistry(); REGISTRIES.set(newRegistry); }
@Override public void close() throws IOException { if (!closed) { closed = true; registry.unregisterCloseable(this); inputStream.close(); } }
public static ClosingFSDataInputStream wrapSafe( FSDataInputStream delegate, SafetyNetCloseableRegistry registry, String debugInfo) throws IOException{ ClosingFSDataInputStream inputStream = new ClosingFSDataInputStream(delegate, registry, debugInfo); registry.registerCloseable(inputStream); return inputStream; } }
@Override protected void doRegister( @Nonnull WrappingProxyCloseable<? extends Closeable> wrappingProxyCloseable, @Nonnull Map<Closeable, PhantomDelegatingCloseableRef> closeableMap) { assert Thread.holdsLock(getSynchronizationLock()); Closeable innerCloseable = WrappingProxyUtil.stripProxy(wrappingProxyCloseable); if (null == innerCloseable) { return; } PhantomDelegatingCloseableRef phantomRef = new PhantomDelegatingCloseableRef( wrappingProxyCloseable, this, REAPER_THREAD.referenceQueue); closeableMap.put(innerCloseable, phantomRef); }
/** * Activates the safety net for a thread. {@link FileSystem} instances obtained by the thread * that called this method will be guarded, meaning that their created streams are tracked and can * be closed via the safety net closing hook. * * <p>This method should be called at the beginning of a thread that should be guarded. * * @throws IllegalStateException Thrown, if a safety net was already registered for the thread. */ @Internal public static void initializeSafetyNetForThread() { SafetyNetCloseableRegistry oldRegistry = REGISTRIES.get(); checkState(null == oldRegistry, "Found an existing FileSystem safety net for this thread: %s " + "This may indicate an accidental repeated initialization, or a leak of the" + "(Inheritable)ThreadLocal through a ThreadPool.", oldRegistry); SafetyNetCloseableRegistry newRegistry = new SafetyNetCloseableRegistry(); REGISTRIES.set(newRegistry); }
@Override public void close() throws IOException { if (!closed) { closed = true; registry.unregisterCloseable(this); inputStream.close(); } }
public static ClosingFSDataOutputStream wrapSafe( FSDataOutputStream delegate, SafetyNetCloseableRegistry registry, String debugInfo) throws IOException { ClosingFSDataOutputStream inputStream = new ClosingFSDataOutputStream(delegate, registry, debugInfo); registry.registerCloseable(inputStream); return inputStream; } }
@Override protected boolean doUnRegister( @Nonnull WrappingProxyCloseable<? extends Closeable> closeable, @Nonnull Map<Closeable, PhantomDelegatingCloseableRef> closeableMap) { assert Thread.holdsLock(getSynchronizationLock()); Closeable innerCloseable = WrappingProxyUtil.stripProxy(closeable.getWrappedDelegate()); return null != innerCloseable && closeableMap.remove(innerCloseable) != null; }
/** * Activates the safety net for a thread. {@link FileSystem} instances obtained by the thread * that called this method will be guarded, meaning that their created streams are tracked and can * be closed via the safety net closing hook. * * <p>This method should be called at the beginning of a thread that should be guarded. * * @throws IllegalStateException Thrown, if a safety net was already registered for the thread. */ @Internal public static void initializeSafetyNetForThread() { SafetyNetCloseableRegistry oldRegistry = REGISTRIES.get(); checkState(null == oldRegistry, "Found an existing FileSystem safety net for this thread: %s " + "This may indicate an accidental repeated initialization, or a leak of the" + "(Inheritable)ThreadLocal through a ThreadPool.", oldRegistry); SafetyNetCloseableRegistry newRegistry = new SafetyNetCloseableRegistry(); REGISTRIES.set(newRegistry); }
@Override public void close() throws IOException { if (!closed) { closed = true; registry.unregisterCloseable(this); outputStream.close(); } }
public static ClosingFSDataOutputStream wrapSafe( FSDataOutputStream delegate, SafetyNetCloseableRegistry registry, String debugInfo) throws IOException { ClosingFSDataOutputStream inputStream = new ClosingFSDataOutputStream(delegate, registry, debugInfo); registry.registerCloseable(inputStream); return inputStream; } }
@Override protected boolean doUnRegister( @Nonnull WrappingProxyCloseable<? extends Closeable> closeable, @Nonnull Map<Closeable, PhantomDelegatingCloseableRef> closeableMap) { assert Thread.holdsLock(getSynchronizationLock()); Closeable innerCloseable = WrappingProxyUtil.stripProxy(closeable.getWrappedDelegate()); return null != innerCloseable && closeableMap.remove(innerCloseable) != null; }