/** * Get a referenced table layout monitor for a table from the cache. * @param tableName table whose layout monitor should be returned. * @param cache of table layout monitors. */ public ReferencedTableLayoutMonitor( String tableName, ReferenceCountedCache<String, TableLayoutMonitor> cache) { mTableName = tableName; mCache = cache; mTableLayoutMonitor = mCache.get(mTableName); }
/** {@inheritDoc} */ @Override public void close() throws IOException { mCache.release(mTableName); }
/** * Make a best effort at closing all the cached values. This method is *not* guaranteed to close * every cached value if there are concurrent users of the cache. As a result, this method * should only be relied upon if only a single thread is using this cache while {@code #close} is * called. * * @throws IOException if any entry throws an IOException while closing. An entry throwing an * IOException will prevent any further entries from being closed. */ @Override public void close() throws IOException { mCache.close(); }
/** * Private default constructor. * * @param iface The interface class of values in this cache. * @param cacheLoader A function to compute cached values on demand. Should never return null. * @param trackResources Whether the cached values should be registered with the resource tracker. * @param trackProxies Whether the created proxy values should be registered with the resource * tracker. */ public ReferenceCountedProxyCache( final Class<V> iface, final Function<? super K, ? extends V> cacheLoader, final boolean trackResources, final boolean trackProxies ) { if (trackResources) { mCache = ReferenceCountedCache.createWithResourceTracking(cacheLoader); } else { mCache = ReferenceCountedCache.create(cacheLoader); } mIface = new Class[] { iface }; mTrackProxies = trackProxies; }
mTableLayoutMonitors = ReferenceCountedCache.create(new TableLayoutMonitorFactoryFn());
/** * Create a {@code ReferenceCountedCache} using the supplied function to create cached values * on demand. * * <p> * The function may be evaluated with the same key multiple times in the case that the key * becomes invalidated due to the reference count falling to 0. A function need not handle the * case of a null key, but it should never return a null value for any key. * </p> * * @param cacheLoader A function to compute cached values on demand. Should never return null. * @return A new {@link ReferenceCountedCache}. * @param <K> The key type of the cache. * @param <V> The value type (must implement {@link Closeable}) of the cache. */ public static <K, V extends Closeable> ReferenceCountedCache<K, V> create( final Function<? super K, ? extends V> cacheLoader ) { return new ReferenceCountedCache<>(cacheLoader, false); }
/** * Returns whether this cache contains <i>or</i> contained a cached entry for the provided key. * * @param key to check for a cached entry. * @return whether this cache contains <i>or</i> contained a cached entry for the key. */ public boolean containsKey(final K key) { return mCache.containsKey(key); }
/** * Create a {@code ReferenceCountedCache} using the supplied function to create cached values * on demand with resource tracking of cached values. * * <p> * The function may be evaluated with the same key multiple times in the case that the key * becomes invalidated due to the reference count falling to 0. A function need not handle the * case of a null key, but it should never return a null value for any key. * </p> * * <p> * The cached values will be registered with the {@link ResourceTracker}, and automatically * unregistered when their reference count falls to 0 and they are removed from the cache. The * values will be registered once upon creation, not every time the value is borrowed. If the * value objects already perform resource tracking, then prefer using {@link #create} to avoid * double registration. * </p> * * @param cacheLoader A function to compute cached values on demand. Should never return null. * @return A new {@link ReferenceCountedCache}. * @param <K> The key type of the cache. * @param <V> The value type (must implement {@link Closeable}) of the cache. */ public static <K, V extends Closeable> ReferenceCountedCache<K, V> createWithResourceTracking( final Function<? super K, ? extends V> cacheLoader ) { return new ReferenceCountedCache<>(cacheLoader, true); }
/** {@inheritDoc} */ @Override public CloseFuture closeAsync() { try { CACHE.release(mKey); // CloseFuture#immediateFuture is package private, so reflection is used to call it. final Method method = CloseFuture.class.getDeclaredMethod("immediateFuture"); AccessController.doPrivileged( new PrivilegedAction<Object>() { @Override public Object run() { method.setAccessible(true); return null; } }); return (CloseFuture) method.invoke(null); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
/** * Get an open session to the Cassandra cluster with the given cluster key. * * @param key The Cassandra cluster information. * @return An open session. */ private static Session getSession(final ClusterKey key) { LOG.debug("Retrieving cached session with key: {}.", key); return new CachedSession(key, CACHE.get(key).getSession()); }
/** * Close all cached ZooKeeper connections. * * <p> * This method closes all ZooKeeper connections that have been returned from * {@link #getZooKeeperClient}. Clients who continue to use these connections will receive * {@code IOExceptions}. This method should only be called when Fiji objects will no longer be * used in the JVM. * </p> * * @throws java.io.IOException on I/O Exception */ public static void closeAllZooKeeperConnections() throws IOException { ZK_CLIENT_CACHE.close(); }
/** {@inheritDoc} */ @Override public Object invoke( final Object proxy, final Method method, final Object[] args ) throws Throwable { if (method.getName().equals("close") && args == null) { if (mTrackedProxy) { ResourceTracker.get().unregisterResource(this); } mCache.release(mKey); return this; } else { return method.invoke(mValue, args); } } }
/** * Returns the value associated with {@code key} in this cache, first creating that value if * necessary. No observable state associated with this cache is modified until loading completes. * * @param key for which to retrieve cached value. * @return cached value associated with the key. */ @SuppressWarnings("unchecked") public V get(final K key) { final V value = mCache.get(key); final ReferenceCountedProxy<K, V> proxy = new ReferenceCountedProxy<>(value, key, mCache, mTrackProxies); if (mTrackProxies) { ResourceTracker.get().registerResource(proxy); } return (V) Proxy.newProxyInstance(value.getClass().getClassLoader(), mIface, proxy); }
/** * Close this InstanceMonitor. Should be called when this instance monitor is no longer needed. * All table monitor objects owned by this instance monitor will also be closed. * * @throws IOException on unrecoverable ZooKeeper exception. */ @Override public void close() throws IOException { mState.set(State.CLOSED); LOG.debug("Closing InstanceMonitor for instance {} with userID {}.", mInstanceURI, mUserID); if (mUserRegistration != null) { mUserRegistration.close(); } mTableLayoutMonitors.close(); }
/** {@inheritDoc} */ @Override public void close() { try { mCache.release(mCacheKey); } catch (IOException e) { // Impossible since {@link CuratorFramework#close()} does not throw IOException. throw new InternalFijiError(e); } finally { ResourceTracker.get().unregisterResource(this); } } }
/** * Create a new cached Curator Framework with the provided cache, key and namespace. * * @param cache holding delegate CuratorFramework instances. * @param key key in cache. * @param namespace to constrain client to, or null for root. * @return a new CuratorFramework with a shared ZooKeeper connection and resource tracking. * @param <K> type of key in cache. */ public static <K> CuratorFramework create( final ReferenceCountedCache<K, CuratorFramework> cache, final K key, final String namespace ) { // Cast is safe; all implementations of CuratorFramework subclass CuratorFrameworkImpl final CuratorFrameworkImpl delegate = (CuratorFrameworkImpl) cache.get(key); if (namespace == null) { return new CachedCuratorFrameworkImpl<K>(cache, key, delegate); } else { return new CachedNamespacedCuratorFramework<K>(cache, key, delegate, namespace); } }
/** {@inheritDoc} */ @Override public void close() { try { mCache.release(mCacheKey); } catch (IOException e) { // Impossible since {@link CuratorFramework#close()} does not throw IOException. throw new InternalFijiError(e); } finally { ResourceTracker.get().unregisterResource(this); } } }