/** * Checks whether the resource is healthy. If not retry. When this called, the resource * is not in mAvailableResources. * * @param resource the resource to check * @param endTimeMs the end time to wait till * @return the resource * @throws TimeoutException if it times out to wait for a resource */ private T checkHealthyAndRetry(T resource, long endTimeMs) throws TimeoutException, IOException { if (isHealthy(resource)) { return resource; } else { LOG.info("Clearing unhealthy resource {}.", resource); remove(resource); closeResource(resource); return acquire(endTimeMs - mClock.millis(), TimeUnit.MILLISECONDS); } }
ResourceInternal<T> resource = poll(); if (resource != null) { return checkHealthyAndRetry(resource.mResource, endTimeMs); if (!isFull()) { T newResource = createNewResource(); ResourceInternal<T> resourceInternal = new ResourceInternal<>(newResource); if (add(resourceInternal)) { return newResource; } else { closeResource(newResource); mLock.lock(); while (true) { resource = poll(); if (resource != null) { break; return checkHealthyAndRetry(resource.mResource, endTimeMs);
while (iterator.hasNext()) { ResourceInternal<T> next = iterator.next(); if (shouldGc(next)) { resourcesToGc.add(next.mResource); iterator.remove(); LOG.info("Resource {} is garbage collected.", resource); try { closeResource(resource); } catch (IOException e) { LOG.warn("Failed to close resource {}.", resource, e);
/** * Acquires a resource of type {code T} from the pool. * * @return the acquired resource */ @Override public T acquire() throws IOException { try { return acquire(100 /* no timeout */, TimeUnit.DAYS); } catch (TimeoutException e) { // Never should timeout in acquire(). throw new RuntimeException(e); } }
/** * Closes the pool and clears all the resources. The resource pool should not be used after this. */ @Override public void close() throws IOException { try { mLock.lock(); if (mAvailableResources.size() != mResources.size()) { LOG.warn("{} resources are not released when closing the resource pool.", mResources.size() - mAvailableResources.size()); } for (ResourceInternal<T> resourceInternal : mAvailableResources) { closeResource(resourceInternal.mResource); } mAvailableResources.clear(); } finally { mLock.unlock(); } mGcFuture.cancel(true); }
/** * Acquire a resource of type {code T} from the pool. * * @return the acquired resource */ @Override public T acquire() throws IOException { try { return acquire(100 /* no timeout */, TimeUnit.DAYS); } catch (TimeoutException e) { // Never should timeout in acquire(). throw new RuntimeException(e); } }
/** * Closes the pool and clears all the resources. The resource pool should not be used after this. */ @Override public void close() { try { mLock.lock(); if (mAvailableResources.size() != mResources.size()) { LOG.warn("{} resources are not released when closing the resource pool.", mResources.size() - mAvailableResources.size()); } for (ResourceInternal<T> resourceInternal : mAvailableResources) { closeResource(resourceInternal.mResource); } mAvailableResources.clear(); } finally { mLock.unlock(); } mGcFuture.cancel(true); }
ResourceInternal<T> resource = poll(); if (resource != null) { return checkHealthyAndRetry(resource.mResource, endTimeMs); if (!isFull()) { T newResource = createNewResource(); ResourceInternal<T> resourceInternal = new ResourceInternal<>(newResource); if (add(resourceInternal)) { return newResource; } else { closeResource(newResource); mLock.lock(); while (true) { resource = poll(); if (resource != null) { break; return checkHealthyAndRetry(resource.mResource, endTimeMs);
/** * Checks whether the resource is healthy. If not retry. When this called, the resource * is not in mAvailableResources. * * @param resource the resource to check * @param endTimeMs the end time to wait till * @return the resource * @throws TimeoutException if it times out to wait for a resource */ private T checkHealthyAndRetry(T resource, long endTimeMs) throws TimeoutException, IOException { if (isHealthy(resource)) { return resource; } else { LOG.info("Clearing unhealthy resource {}.", resource); remove(resource); closeResource(resource); return acquire(endTimeMs - mClock.millis(), TimeUnit.MILLISECONDS); } }
while (iterator.hasNext()) { ResourceInternal<T> next = iterator.next(); if (shouldGc(next)) { resourcesToGc.add(next.mResource); iterator.remove(); closeResource(resource);