Package edu.emory.mathcs.backport.java.util.concurrent.locks

Examples of edu.emory.mathcs.backport.java.util.concurrent.locks.Lock


            throw new ManageException("id may not be null");
        }

        final InstanceResource resource;

        final Lock lock = this.lockManager.getLock(id);
        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            throw new ManageException(e.getMessage(), e);
        }

        try {

            final Element el = this.cache.get(id);
            if (el == null) {
                resource = this.newInstance(id);
                resource.load(id); // throws DoesNotExistException if not in db

                final Calendar currTime = Calendar.getInstance();
                final Calendar termTime = resource.getTerminationTime();
                if (termTime != null && termTime.before(currTime)) {
                    boolean destroyed = this.destroy(id);
                    if (destroyed) {
                      throw new DoesNotExistException(Lager.id(id) + " expired");
                    }
                }

            } else {
                resource = (InstanceResource) el.getObjectValue();
            }

        } catch (DoesNotExistException e) {
            this.cache.remove(id);
            throw e;
        } catch (CreationException e) {
            throw new ManageException(e.getMessage(), e); // ...
        } finally {
            lock.unlock();
        }

        return resource;
    }
View Full Code Here


        this.cache.remove(id);
    }

    public void _cleanup(int id)
            throws ManageException, DoesNotExistException {
        final Lock destroy_lock = this.lockManager.getLock("destroy_" + id);
        final Lock lock = this.lockManager.getLock(id);
        try {
            destroy_lock.lockInterruptibly();
        } catch (InterruptedException e) {
            throw new ManageException(e.getMessage(), e);
        }

        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            destroy_lock.unlock();
            throw new ManageException(e.getMessage(), e);
        }

        try {
            final InstanceResource resource = this.find(id);
            this.scheduler.cleanup(id);
            resource.cleanup();
        } finally {
            lock.unlock();
            destroy_lock.unlock();
        }
    }
View Full Code Here

        if (id == null) {
            throw new IllegalArgumentException("id may not be null");
        }

        final Lock destroy_lock = this.lockManager.getLock("destroy_" + id);
        final Lock lock = this.lockManager.getLock(id);
        try {
            destroy_lock.lockInterruptibly();
        } catch (InterruptedException e) {
            throw new ManageException(e.getMessage(), e);
        }

        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            destroy_lock.unlock();
            throw new ManageException(e.getMessage(), e);
        }

        try {
            final InstanceResource resource = this.find(id);
            destroyed = resource.remove();
            if (destroyed) {
                this.cache.remove(id);
            }

        } finally {
            lock.unlock();
            destroy_lock.unlock();
        }

        return destroyed;
    }
View Full Code Here

                           final String tarStr)

            throws ManageException {

        final LockManager lockManager = resource.getLockManager();
        final Lock lock = lockManager.getLock(id);
        final Lock destroy_lock = lockManager.getLock("destroy_" + id);

        if (current >= STATE_CANCELLING_STAGING_IN
                        && current <= STATE_CANCELLING_STAGING_OUT) {


            // since setState(STATE_DESTROYING) is called before any
            // invocation to a CANCELLING command, this should never be
            // the case (because setState doesn't call StateTransition.run()
            // if target has already been set to DESTROYING  (target =
            // STATE_DESTROYING is only the case here in this handler when
            // setTargetState first changes it to DESTROYING

            logger.fatal("programming error, stopping state transition");
            // stop processing
            return true;


            // TODO: In the future an action should be allowed to be
            //       cancelled without its target being set to DESTROYING
            //       first.  When that happens, this assumption above will
            //       need to change (add another handler before this remove
            //       handler to handle resources with a cancelling state or
            //       cancel target state.  A smart scheduler could for
            //       example just want cancelPropagate to execute because
            //       it got a priority request and needs the network, i.e.,
            //       postpone functionality.
        }


        if (target != STATE_DESTROYING) {
            return false;
        }

        if (this.trace) {
            logger.trace("\n\n   ***** ST--remove: processing " +
                    idStr + ", current = " + curStr + ", target = " +
                    tarStr + "\n");
        }

        final WorkspaceRequestContext requestContext =
                new WorkspaceRequestContext(id, resource.getName(),
                                            this.locator, this.lager);
       
        requestContext.setGroupID(resource.getGroupId());
        requestContext.setGroupSize(resource.getGroupSize());
        if (resource.isLastInGroup()) {
            requestContext.setLastInGroup(true);
            resource.setLastInGroup(false);
        }
        requestContext.setPartOfGroupRequest(resource.isPartOfGroupRequest());

        WorkspaceRequest req = null;
        int nextstate = STATE_INVALID;

        switch (current) {
            case STATE_DESTROY_SUCCEEDED:
                break;
            case STATE_DESTROY_FAILED:
                req = reqFactory.cancelAllAtVMM();
                nextstate = STATE_CANCELLING_AT_VMM;
                requestContext.setVm(resource.getVM());
                break;
            case STATE_STAGING_IN: // now unused
            case STATE_UNPROPAGATED:
                req = reqFactory.cancelUnpropagated();
                nextstate = STATE_CANCELLING_UNPROPAGATED;
                requestContext.setVm(resource.getVM());
                break;
            case STATE_PROPAGATING:
                req = reqFactory.cancelPropagating();
                nextstate = STATE_CANCELLING_PROPAGATING;
                requestContext.setVm(resource.getVM());
                break;
            case STATE_PROPAGATING_TO_START:
                req = reqFactory.cancelPropagatingToStart();
                nextstate = STATE_CANCELLING_PROPAGATING_TO_START;
                requestContext.setVm(resource.getVM());
                break;
            case STATE_PROPAGATING_TO_PAUSE:   
                req = reqFactory.cancelPropagatingToPause();
                nextstate = STATE_CANCELLING_PROPAGATING_TO_PAUSE;
                requestContext.setVm(resource.getVM());
                break;
            case STATE_PROPAGATED:
            case STATE_STARTING:
            case STATE_STARTED:
            case STATE_SERIALIZING:
            case STATE_SERIALIZED:
            case STATE_PAUSING:
            case STATE_PAUSED:
            case STATE_SHUTTING_DOWN:
                req = reqFactory.cancelAllAtVMM();
                nextstate = STATE_CANCELLING_AT_VMM;
                requestContext.setVm(resource.getVM());
                break;
            case STATE_READYING_FOR_TRANSPORT:
                req = reqFactory.cancelReadyingForTransport();
                nextstate = STATE_CANCELLING_READYING_FOR_TRANSPORT;
                requestContext.setVm(resource.getVM());
                break;
            case STATE_READY_FOR_TRANSPORT:
                req = reqFactory.cancelReadyForTransport();
                nextstate = STATE_CANCELLING_READY_FOR_TRANSPORT;
                requestContext.setVm(resource.getVM());
                break;
            case STATE_STAGING_OUT: // now unused
            default:
        }

        if (current >= STATE_CORRUPTED) {

            // currently we will try to do something about a workspace
            // corrupted at times that may have left image files or state
            // at the backend node, we do not handle other corrupt-*
            // situations now.

            final int oldstate = current - STATE_CORRUPTED;

            if (oldstate >= STATE_PROPAGATING
                    && oldstate < STATE_READYING_FOR_TRANSPORT) {

                req = reqFactory.cancelAllAtVMM();
                nextstate = STATE_CANCELLING_AT_VMM;
                requestContext.setVm(resource.getVM());

            } else {

                // candidate for admin log/trigger of severe issues

                final String err = "Destroying a corrupted " +
                            "resource in state '" + curStr +
                            "'. That state does not indicate files or " +
                            "cruft may be on VMM node, not doing anything" +
                            " (but there may be stray staged files off-VMM).";

                if (this.event) {
                    logger.info(Lager.ev(id) + err);
                } else if (this.trace) {
                    logger.trace(idStr + err);
                }
            }
        }

        if (req != null) {

            resource.setStateUnderLock(nextstate, null);
            requestContext.setNotify(STATE_DESTROYING);
            req.setRequestContext(requestContext);

            if (this.trace) {
                logger.trace("\n\n   ***** ST--remove: " + idStr
                                    + ", executing " + req.toString() + "\n");
            }

            try {
                destroy_lock.lockInterruptibly();
            } catch (InterruptedException e) {
                throw new ManageException(e.getMessage(), e);
            }

            lock.unlock();

            // TODO: add a timeout
            try {
                req.execute(); // could block
            } catch (Throwable t) {
                // candidate for admin log/trigger of severe issues
                logger.error("",t);
            } finally {
              try {
                  lock.lockInterruptibly();
                  destroy_lock.unlock();
              } catch (InterruptedException e) {
                  throw new ManageException(e.getMessage(), e);
              }
            }

View Full Code Here

    // evaluate flag allows setState calls from within StateTransition to
    // bypass running StateTransition again
    private void setState(int newstate, Throwable t, boolean evaluate)
                                    throws LockAcquisitionFailure {

        Lock lock = null;
        Lock destroy_lock = null;
        if (evaluate) {
            destroy_lock = lockMgr.getLock("destroy_" + this.id);
            lock = lockMgr.getLock(this.id);

            try {
                destroy_lock.lockInterruptibly();
            } catch (InterruptedException e) {
                throw new LockAcquisitionFailure(e);
            }

            try {
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                destroy_lock.unlock();
                throw new LockAcquisitionFailure(e);
            }

            if (lager.traceLog) {
                logger.trace(Lager.id(this.id) + ": acquired lock");
            }
        }

        try {
            setStateImpl(newstate, t, evaluate);
        } finally {
            if (lock != null) {
                if (lager.traceLog) {
                    logger.trace(Lager.id(this.id) + ": releasing lock");
                }
                lock.unlock();
                destroy_lock.unlock();
            }
        }
    }
View Full Code Here

    // see activateOverride(), the only valid use of force==true
    private void setTargetState(int targetState, boolean force)
                                            throws ManageException {

        final Lock lock = this.lockMgr.getLock(this.id);

        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            throw new LockAcquisitionFailure(Lager.id(this.id), e);
        }

        if (lager.traceLog) {
            logger.trace(Lager.id(this.id) + ": acquired lock");
        }

        try {
            setTargetStateImpl(targetState, force, true);
        } catch (ManageException e) {
            if (logger.isDebugEnabled()) {
                logger.error(e.getMessage(), e);
            } else {
                logger.error(e.getMessage());
            }
            throw e;
        } finally {
            if (lager.traceLog) {
                logger.trace(Lager.id(this.id) + ": releasing lock");
            }
            lock.unlock();
        }
    }
View Full Code Here

       
        final CoschedResource resource = this.newEmptyResource();

        final String idStr = this.uuidGen.generateRandomBasedUUID().toString();

        final Lock lock = this.lockManager.getLock(idStr);
        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            throw new ManageException(e.getMessage(), e);
        }

        try {

            resource.setID(idStr);
            resource.setCreatorID(creatorID);
            this.persistence.addEnsemble(resource);

            final Element el = this.cache.get(idStr);
            if (el == null) {
                this.cache.put(new Element(idStr, resource));
            } else {
                throw new ManageException("UUID collision in groups cache, " +
                        "ID '" + idStr + "' already exists (seriously?)");
            }

        } finally {
            lock.unlock();
        }

        if (this.lager.eventLog) {
            logger.info(Lager.ensembleev(idStr) +
                    " created on behalf of '" + creatorID + "'");
View Full Code Here

            throw new ManageException("coschedid may not be null");
        }

        final CoschedResource resource;

        final Lock lock = this.lockManager.getLock(coschedid);
        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            throw new ManageException(e.getMessage(), e);
        }

        try {

            final Element el = this.cache.get(coschedid);
            if (el == null) {
                resource = this.newEmptyResource();
                // throws DoesNotExistException if not in db
                this.persistence.loadEnsemble(coschedid, resource);

            } else {
                resource = (CoschedResource) el.getObjectValue();
            }

        } finally {
            lock.unlock();
        }

        return resource;
       
    }
View Full Code Here

       
        if (coschedid == null) {
            throw new ManageException("id may not be null");
        }

        final Lock lock = this.lockManager.getLock(coschedid);
        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            throw new ManageException(e.getMessage(), e);
        }

        try {
            final CoschedResource resource = this.find(coschedid);
            resource.remove();
            this.cache.remove(coschedid);
            this.persistence.removeEnsemble(coschedid);

        } finally {
            lock.unlock();
        }
    }
View Full Code Here

        final GroupResource resource = this.newEmptyResource();
       
        final String idStr = this.uuidGen.generateRandomBasedUUID().toString();

        final Lock lock = this.lockManager.getLock(idStr);
        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            throw new ManageException(e.getMessage(), e);
        }

        try {

            resource.setID(idStr);
            resource.setCreatorID(creatorID);
            this.persistence.addGroup(resource);

            final Element el = this.cache.get(idStr);
            if (el == null) {
                this.cache.put(new Element(idStr, resource));
            } else {
                throw new ManageException("UUID collision in groups cache, " +
                        "ID '" + idStr + "' already exists (seriously?)");
            }

        } finally {
            lock.unlock();
        }

        if (this.lager.eventLog) {
            logger.info(Lager.groupev(idStr) +
                    " created on behalf of '" + creatorID + "'");
View Full Code Here

TOP

Related Classes of edu.emory.mathcs.backport.java.util.concurrent.locks.Lock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.