Search in sources :

Example 1 with ResourceState

use of com.sun.enterprise.resource.ResourceState 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 ResourceState

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

the class ConnectionPool method getResourceFromTransaction.

/**
 * Try to get a resource from current transaction if it is shareable<br>
 * @param tran Current Transaction
 * @param alloc ResourceAllocator
 * @param spec ResourceSpec
 * @return result ResourceHandle
 */
private ResourceHandle getResourceFromTransaction(Transaction tran, ResourceAllocator alloc, ResourceSpec spec) {
    ResourceHandle result = null;
    try {
        // shareable, so abort right here if that's not the case
        if (tran != null && alloc.shareableWithinComponent()) {
            // TODO should be handled by PoolTxHelper
            JavaEETransaction j2eetran = (JavaEETransaction) tran;
            // case 1. look for free and enlisted in same tx
            Set set = j2eetran.getResources(poolInfo);
            if (set != null) {
                Iterator iter = set.iterator();
                while (iter.hasNext()) {
                    ResourceHandle h = (ResourceHandle) iter.next();
                    if (h.hasConnectionErrorOccurred()) {
                        iter.remove();
                        continue;
                    }
                    ResourceState state = h.getResourceState();
                    /*
                         * One can share a resource only for the following conditions:
                         * 1. The caller resource is shareable (look at the outermost
                         *    if marked comment-1
                         * 2. The resource enlisted inside the transaction is shareable
                         * 3. We are dealing with XA resources OR
                         *    We are dealing with a non-XA resource that's not in use
                         *    Note that sharing a non-xa resource that's in use involves
                         *    associating physical connections.
                         * 4. The credentials of the resources match
                         */
                    if (h.getResourceAllocator().shareableWithinComponent()) {
                        if (spec.isXA() || poolTxHelper.isNonXAResourceAndFree(j2eetran, h)) {
                            if (matchConnections) {
                                if (!alloc.matchConnection(h)) {
                                    if (poolLifeCycleListener != null) {
                                        poolLifeCycleListener.connectionNotMatched();
                                    }
                                    continue;
                                }
                                if (h.hasConnectionErrorOccurred()) {
                                    if (failAllConnections) {
                                        // if failAllConnections has happened, we flushed the
                                        // pool, so we don't have to do iter.remove else we
                                        // will get a ConncurrentModificationException
                                        result = null;
                                        break;
                                    }
                                    iter.remove();
                                    continue;
                                }
                                if (poolLifeCycleListener != null) {
                                    poolLifeCycleListener.connectionMatched();
                                }
                            }
                            if (state.isFree())
                                setResourceStateToBusy(h);
                            result = h;
                            break;
                        }
                    }
                }
            }
        }
    } catch (ClassCastException e) {
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "Pool: getResource : " + "transaction is not JavaEETransaction but a " + tran.getClass().getName(), e);
        }
    }
    return result;
}
Also used : JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) ResourceHandle(com.sun.enterprise.resource.ResourceHandle) ResourceState(com.sun.enterprise.resource.ResourceState)

Example 3 with ResourceState

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

the class ConnectionPool method resourceClosed.

/**
 * this method is called to indicate that the resource is
 * not used by a bean/application anymore
 */
public void resourceClosed(ResourceHandle h) throws IllegalStateException {
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "Pool: resourceClosed: " + h);
    }
    ResourceState state = getResourceState(h);
    if (state == null) {
        throw new IllegalStateException("State is null");
    }
    if (!state.isBusy()) {
        throw new IllegalStateException("state.isBusy() : false");
    }
    // mark as not busy
    setResourceStateToFree(h);
    state.touchTimestamp();
    if (state.isUnenlisted() || (poolTxHelper.isNonXAResource(h) && poolTxHelper.isLocalTransactionInProgress() && poolTxHelper.isLocalResourceEligibleForReuse(h))) {
        freeUnenlistedResource(h);
    }
    if (poolLifeCycleListener != null) {
        poolLifeCycleListener.connectionReleased(h.getId());
    }
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "Pool: resourceFreed: " + h);
    }
}
Also used : ResourceState(com.sun.enterprise.resource.ResourceState)

Example 4 with ResourceState

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

the class ConnectionPool method resourceErrorOccurred.

public void resourceErrorOccurred(ResourceHandle h) throws IllegalStateException {
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "Pool: resourceErrorOccurred: " + h);
    }
    if (failAllConnections) {
        doFailAllConnectionsProcessing();
        return;
    }
    ResourceState state = getResourceState(h);
    if (state == null) {
        throw new IllegalStateException();
    }
    // changed order of commands
    // Commenting resources.remove() out since we will call an iter.remove()
    // in the getUnenlistedResource method in the if check after
    // matchManagedConnections or in the internalGetResource method
    // If we were to call remove directly here there is always the danger
    // of a ConcurrentModificationExceptionbeing thrown when we return
    // 
    // In case of this method being called asynchronously, since
    // the resource has been marked as "errorOccured", it will get
    // removed in the next iteration of getUnenlistedResource
    // or internalGetResource
    ds.removeResource(h);
}
Also used : ResourceState(com.sun.enterprise.resource.ResourceState)

Example 5 with ResourceState

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

the class UnpooledResource method getUnenlistedResource.

@Override
protected ResourceHandle getUnenlistedResource(ResourceSpec spec, ResourceAllocator alloc, Transaction tran) throws PoolingException {
    ResourceHandle handle = null;
    if (incrementPoolSize()) {
        try {
            handle = createSingleResource(alloc);
        } catch (PoolingException ex) {
            decrementPoolSize();
            throw ex;
        }
        ResourceState state = new ResourceState();
        handle.setResourceState(state);
        state.setEnlisted(false);
        setResourceStateToBusy(handle);
        return handle;
    }
    String msg = localStrings.getStringWithDefault("poolmgr.max.pool.size.reached", "In-use connections equal max-pool-size therefore cannot allocate any more connections.");
    throw new PoolingException(msg);
}
Also used : PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ResourceHandle(com.sun.enterprise.resource.ResourceHandle) ResourceState(com.sun.enterprise.resource.ResourceState)

Aggregations

ResourceState (com.sun.enterprise.resource.ResourceState)10 ResourceHandle (com.sun.enterprise.resource.ResourceHandle)6 JavaEETransaction (com.sun.enterprise.transaction.api.JavaEETransaction)3 HashSet (java.util.HashSet)2 PoolingException (com.sun.appserv.connectors.internal.api.PoolingException)1 AssocWithThreadResourceHandle (com.sun.enterprise.resource.AssocWithThreadResourceHandle)1 ResourceAllocator (com.sun.enterprise.resource.allocator.ResourceAllocator)1