Search in sources :

Example 1 with AssocWithThreadResourceHandle

use of com.sun.enterprise.resource.AssocWithThreadResourceHandle in project Payara by payara.

the class AssocWithThreadPoolResizer method removeIdleAndInvalidResources.

/**
 * Get the free connections list from the pool, remove idle-timed-out resources
 * and then invalid resources.
 *
 * @return int number of resources removed
 */
@Override
protected int removeIdleAndInvalidResources() {
    int poolSizeBeforeRemoval = ds.getResourcesSize();
    int noOfResourcesRemoved = 0;
    // let's cache the current time since precision is not required here.
    long currentTime = System.currentTimeMillis();
    int validConnectionsCounter = 0;
    int idleConnKeptInSteadyCounter = 0;
    ResourceState state;
    Set<ResourceHandle> resourcesToValidate = new HashSet<ResourceHandle>();
    Set<ResourceHandle> resourcesToRemove = new HashSet<ResourceHandle>();
    try {
        // iterate through all the resources to find idle-time lapsed ones.
        for (ResourceHandle h : ds.getAllResources()) {
            synchronized (h.lock) {
                state = h.getResourceState();
                if (!state.isBusy()) {
                    if (currentTime - state.getTimestamp() < pool.getIdleTimeout()) {
                        // Should be added for validation.
                        if (state.isUnenlisted() && state.isFree()) {
                            if (((AssocWithThreadResourceHandle) h).isAssociated()) {
                                ((AssocWithThreadResourceHandle) h).setAssociated(false);
                                validConnectionsCounter++;
                                resourcesToValidate.add(h);
                            }
                        }
                    } else {
                        boolean isResourceEligibleForRemoval = isResourceEligibleForRemoval(h, validConnectionsCounter);
                        if (!isResourceEligibleForRemoval) {
                            // preferValidateOverrecreate true and connection is valid within SPS
                            validConnectionsCounter++;
                            idleConnKeptInSteadyCounter++;
                            debug("PreferValidateOverRecreate: Keeping idle resource " + h + " in the steady part of the free pool " + "as the RA reports it to be valid (" + validConnectionsCounter + " <= " + pool.getSteadyPoolSize() + ")");
                        } else {
                            // Add this to remove later
                            resourcesToRemove.add(h);
                            ((AssocWithThreadResourceHandle) h).setDirty();
                        }
                    }
                }
            }
        }
    } finally {
        for (ResourceHandle resourceToRemove : resourcesToRemove) {
            if (ds.getAllResources().contains(resourceToRemove)) {
                ds.removeResource(resourceToRemove);
            }
        }
    }
    // remove invalid resources from the free (active) resources list.
    // Since the whole pool is not locked, it may happen that some of these
    // resources may be given to applications.
    int noOfInvalidResources = removeInvalidResources(resourcesToValidate);
    // locks the pool throughout its operations.
    if (preferValidateOverRecreate) {
        debug("Idle resources validated and kept in the steady pool for pool [ " + poolInfo + " ] - " + idleConnKeptInSteadyCounter);
        debug("Number of Idle resources freed for pool [ " + poolInfo + " ] - " + (resourcesToRemove.size()));
        debug("Number of Invalid resources removed for pool [ " + poolInfo + " ] - " + noOfInvalidResources);
    } else {
        debug("Number of Idle resources freed for pool [ " + poolInfo + " ] - " + resourcesToRemove.size());
        debug("Number of Invalid resources removed for pool [ " + poolInfo + " ] - " + noOfInvalidResources);
    }
    noOfResourcesRemoved = poolSizeBeforeRemoval - ds.getResourcesSize();
    return noOfResourcesRemoved;
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle) AssocWithThreadResourceHandle(com.sun.enterprise.resource.AssocWithThreadResourceHandle) ResourceState(com.sun.enterprise.resource.ResourceState) HashSet(java.util.HashSet) AssocWithThreadResourceHandle(com.sun.enterprise.resource.AssocWithThreadResourceHandle)

Example 2 with AssocWithThreadResourceHandle

use of com.sun.enterprise.resource.AssocWithThreadResourceHandle in project Payara by payara.

the class AssocWithThreadResourcePool method prefetch.

/**
 * Prefetch is called to check whether there there is a free resource is already associated with the thread
 * Only when prefetch is unable to find a resource, normal routine (getUnenlistedResource) will happen.
 * @param spec ResourceSpec
 * @param alloc ResourceAllocator
 * @param tran Transaction
 * @return ResourceHandle resource associated with the thread, if any
 */
protected ResourceHandle prefetch(ResourceSpec spec, ResourceAllocator alloc, Transaction tran) {
    AssocWithThreadResourceHandle ar = localResource.get();
    if (ar != null) {
        // resource is usable at all
        synchronized (ar.lock) {
            if ((ar.getThreadId() != Thread.currentThread().getId()) || ar.hasConnectionErrorOccurred() || ar.isDirty() || !ar.isAssociated()) {
                // we were associated with someone else or resource error
                // occurred or resource was disassociated and used by some one else. So evict
                // NOTE: We do not setAssociated to false here since someone
                // else has associated this resource to themself. Also, if
                // the eviction is because of a resourceError, the resource is
                // not going to be used anyway.
                localResource.remove();
                return null;
            }
            if (ar.getResourceState().isFree() && ar.getResourceState().isUnenlisted()) {
                if (matchConnections) {
                    if (!alloc.matchConnection(ar)) {
                        // again, since the credentials of the caller don't match
                        // evict from ThreadLocal
                        // also, mark the resource as unassociated and make this resource
                        // potentially usable
                        localResource.remove();
                        ar.setAssociated(false);
                        if (poolLifeCycleListener != null) {
                            poolLifeCycleListener.connectionNotMatched();
                        }
                        return null;
                    }
                    if (poolLifeCycleListener != null) {
                        poolLifeCycleListener.connectionMatched();
                    }
                }
                if (!isConnectionValid(ar, alloc)) {
                    localResource.remove();
                    ar.setAssociated(false);
                    // from the pool.
                    return null;
                }
                setResourceStateToBusy(ar);
                if (maxConnectionUsage_ > 0) {
                    ar.incrementUsageCount();
                }
                if (poolLifeCycleListener != null) {
                    poolLifeCycleListener.connectionUsed(ar.getId());
                    // Decrement numConnFree
                    poolLifeCycleListener.decrementNumConnFree();
                }
                return ar;
            }
        }
    }
    return null;
}
Also used : AssocWithThreadResourceHandle(com.sun.enterprise.resource.AssocWithThreadResourceHandle)

Example 3 with AssocWithThreadResourceHandle

use of com.sun.enterprise.resource.AssocWithThreadResourceHandle in project Payara by payara.

the class AssocWithThreadResourcePool method getUnenlistedResource.

// this is the RI getResource() with some modifications
/**
 * return resource in free list. If none is found, returns null
 */
protected ResourceHandle getUnenlistedResource(ResourceSpec spec, ResourceAllocator alloc, Transaction tran) throws PoolingException {
    ResourceHandle result;
    result = super.getUnenlistedResource(spec, alloc, tran);
    // and hence we should not use this resource.
    if (result != null) {
        synchronized (result.lock) {
            if (ds.getAllResources().contains(result) && ((AssocWithThreadResourceHandle) result).isDirty()) {
                // Remove the resource and set to null
                ds.removeResource(result);
                result = null;
            }
        }
    }
    // DISASSOCIATE
    if (result == null) {
        synchronized (this) {
            for (ResourceHandle resource : ds.getAllResources()) {
                synchronized (resource.lock) {
                    if (resource.getResourceState().isUnenlisted() && resource.getResourceState().isFree() && !(((AssocWithThreadResourceHandle) resource).isDirty())) {
                        if (!matchConnection(resource, alloc)) {
                            continue;
                        }
                        if (resource.hasConnectionErrorOccurred()) {
                            continue;
                        }
                        result = resource;
                        setResourceStateToBusy(result);
                        ((AssocWithThreadResourceHandle) result).setAssociated(false);
                        break;
                    }
                }
            }
        }
    }
    if (localResource.get() == null) {
        setInThreadLocal((AssocWithThreadResourceHandle) result);
    }
    return result;
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle) AssocWithThreadResourceHandle(com.sun.enterprise.resource.AssocWithThreadResourceHandle) AssocWithThreadResourceHandle(com.sun.enterprise.resource.AssocWithThreadResourceHandle)

Example 4 with AssocWithThreadResourceHandle

use of com.sun.enterprise.resource.AssocWithThreadResourceHandle in project Payara by payara.

the class AssocWithThreadPoolResizer method scaleDownPool.

/**
 * Scale down pool by a <code>size &lt;= pool-resize-quantity</code>
 *
 * @param forced            scale-down only when forced
 * @param scaleDownQuantity no. of resources to remove
 */
@Override
protected void scaleDownPool(int scaleDownQuantity, boolean forced) {
    if (pool.getResizeQuantity() > 0 && forced) {
        scaleDownQuantity = (scaleDownQuantity <= (ds.getResourcesSize() - pool.getSteadyPoolSize())) ? scaleDownQuantity : 0;
        debug("Scaling down pool by quantity : " + scaleDownQuantity);
        Set<ResourceHandle> resourcesToRemove = new HashSet<ResourceHandle>();
        try {
            for (ResourceHandle h : ds.getAllResources()) {
                if (scaleDownQuantity > 0) {
                    synchronized (h.lock) {
                        if (!h.isBusy()) {
                            resourcesToRemove.add(h);
                            ((AssocWithThreadResourceHandle) h).setDirty();
                            scaleDownQuantity--;
                        }
                    }
                }
            }
        } finally {
            for (ResourceHandle resourceToRemove : resourcesToRemove) {
                if (ds.getAllResources().contains(resourceToRemove)) {
                    ds.removeResource(resourceToRemove);
                }
            }
        }
    }
}
Also used : ResourceHandle(com.sun.enterprise.resource.ResourceHandle) AssocWithThreadResourceHandle(com.sun.enterprise.resource.AssocWithThreadResourceHandle) HashSet(java.util.HashSet) AssocWithThreadResourceHandle(com.sun.enterprise.resource.AssocWithThreadResourceHandle)

Aggregations

AssocWithThreadResourceHandle (com.sun.enterprise.resource.AssocWithThreadResourceHandle)4 ResourceHandle (com.sun.enterprise.resource.ResourceHandle)3 HashSet (java.util.HashSet)2 ResourceState (com.sun.enterprise.resource.ResourceState)1