Search in sources :

Example 1 with ComponentCommandAckAlloc

use of com.cosylab.acs.maci.manager.recovery.ComponentCommandAckAlloc in project ACS by ACS-Community.

the class ManagerImpl method internalNoSyncRequestComponentPhase2.

private ComponentInfo internalNoSyncRequestComponentPhase2(int requestor, String name, String type, String code, String containerName, int keepAliveTime, StatusHolder status, AcsJCannotGetComponentEx bcex, boolean isOtherDomainComponent, boolean isDynamicComponent, int h, boolean reactivate, ComponentInfo componentInfo, Container container, ContainerInfo containerInfo, long executionId, long activationTime, boolean timeoutError) throws AcsJCannotGetComponentEx {
    // remove component from client component list, will be added later (first lots of checks has to be done)
    if ((requestor & TYPE_MASK) == COMPONENT_MASK) {
        removeComponentOwner(h | COMPONENT_MASK, requestor);
    }
    // remove from pending activation list
    synchronized (pendingActivations) {
        pendingActivations.remove(name);
    }
    // failed to activate
    if (componentInfo == null || componentInfo.getHandle() == 0 || componentInfo.getComponent() == null) {
        if (bcex == null)
            logger.log(Level.SEVERE, "Failed to activate component '" + name + "' (" + HandleHelper.toString(h | COMPONENT_MASK) + ").");
        else
            bcex.setReason("Failed to activate component '" + name + "' (" + HandleHelper.toString(h | COMPONENT_MASK) + ").");
        componentsLock.lock();
        try {
            // !!! ACID 3
            if (!reactivate)
                executeCommand(new ComponentCommandDeallocate(h, h | COMPONENT_MASK, timeoutError ? WhyUnloadedReason.TIMEOUT : WhyUnloadedReason.REMOVED, true));
        //components.deallocate(h, true);
        } finally {
            componentsLock.unlock();
        }
        status.setStatus(ComponentStatus.COMPONENT_NOT_ACTIVATED);
        if (bcex == null)
            return null;
        else
            throw bcex;
    }
    // log info
    logger.log(Level.INFO, "Component '" + name + "' (" + HandleHelper.toString(h | COMPONENT_MASK) + ") activated successfully.");
    //
    if (!isOtherDomainComponent && !componentInfo.getComponent().doesImplement(type)) {
        // just output SEVERE message
        logger.log(Level.SEVERE, "Activated component '" + name + "' does not implement specified type '" + type + "'.");
    }
    // component info (to get type and code, container - prefix name)
    if (isOtherDomainComponent) {
        // @todo MF tmp (for testing)
        componentInfo.setHandle(h | COMPONENT_MASK);
        componentInfo.setClients(new IntArray());
        componentInfo.setComponents(new IntArray(0));
        componentInfo.setContainer(0);
        // set variables
        type = componentInfo.getType();
        code = componentInfo.getCode();
    }
    int[] clients;
    componentsLock.lock();
    try {
        //
        if (componentInfo.getHandle() != (h | COMPONENT_MASK)) {
            // container returned different handle
            // (it seems it has already activated this Component)
            // check if we can accept it
            int componentHandle = componentInfo.getHandle() & HANDLE_MASK;
            if (components.isAllocated(componentHandle)) {
                // cancel preallocation
                if (!reactivate)
                    executeCommand(new ComponentCommandDeallocate(h, componentInfo.getHandle(), WhyUnloadedReason.REMOVED, true));
                //components.deallocate(h, true);
                bcex = new AcsJCannotGetComponentEx();
                bcex.setReason("Container '" + containerName + "' returned another '" + name + "' component handle than given, failed to fix handle since returned handle is already allocated.");
                // log this anyway, since it's an error of a global nature
                logger.log(Level.SEVERE, "Container '" + containerName + "' returned another '" + name + "' component handle than given, failed to fix handle since returned handle is already allocated.", bcex);
                // component is activated, but cannot be managed by the Manager
                status.setStatus(ComponentStatus.COMPONENT_ACTIVATED);
                throw bcex;
            } else {
                // handle is free, relocate handle
                ComponentInfo existingData = null;
                // deallocate old
                if (!reactivate)
                    executeCommand(new ComponentCommandDeallocate(h, componentInfo.getHandle(), WhyUnloadedReason.REPLACED, true));
                else //components.deallocate(h, true);
                {
                    // !!! ACID 3
                    existingData = (ComponentInfo) components.get(h);
                    executeCommand(new ComponentCommandDeallocate(h, componentInfo.getHandle(), WhyUnloadedReason.REPLACED));
                //components.deallocate(h);
                }
                // !!! ACID 3
                // preallocate new
                Integer objHandle = (Integer) executeCommand(new ComponentCommandAllocateHandle(componentHandle, true));
                //h = components.allocate(componentHandle, true);
                if (objHandle == null || (h = objHandle.intValue()) == 0) {
                    // failed to allocate new
                    bcex = new AcsJCannotGetComponentEx();
                    bcex.setReason("Container '" + containerName + "' returned another '" + name + "' component handle than given, failed to fix handle due to handle relocation failure.");
                    // log this anyway, since it's an error of a global nature
                    logger.log(Level.SEVERE, "Container '" + containerName + "' returned another '" + name + "' component handle than given, failed to fix handle due to handle relocation failure.", bcex);
                    // Component is activated, but cannot be managed by the Manager
                    status.setStatus(ComponentStatus.COMPONENT_ACTIVATED);
                    throw bcex;
                } else // !!! ACID 3
                if (existingData != null)
                    executeCommand(new ComponentCommandSet(h, existingData));
                //components.set(h, existingData);
                logger.log(Level.WARNING, "Container '" + containerName + "' returned another '" + name + "' component handle than given, handle fixed.");
            }
        }
        //
        // pre-store data
        //
        ComponentInfo existingData = (ComponentInfo) components.get(h);
        // create a new ComponentInfo - do not trust containers
        ComponentInfo data = new ComponentInfo(componentInfo.getHandle(), name, type, code, componentInfo.getComponent());
        if (existingData != null) {
            data.setClients(existingData.getClients());
            data.setComponents(existingData.getComponents());
        }
        if (requestor != 0)
            if (// hierarchical components need this check
            !data.getClients().contains(requestor))
                data.getClients().add(requestor);
        if (keepAliveTime <= RELEASE_NEVER)
            if (// make component immortal
            !data.getClients().contains(this.getHandle()))
                data.getClients().add(this.getHandle());
        // remember keep alive time
        data.setKeepAliveTime(keepAliveTime);
        if (isOtherDomainComponent) {
            data.setContainer(0);
            data.setContainerName(componentInfo.getContainerName());
        } else {
            data.setContainer(containerInfo.getHandle());
            data.setContainerName(containerInfo.getName());
        }
        data.setAccessRights(0);
        data.setInterfaces(componentInfo.getInterfaces());
        // mark as dynamic component and store its container
        if (isDynamicComponent) {
            data.setDynamic(true);
            data.setDynamicContainerName(containerName);
        }
        // !!! ACID
        executeCommand(new ComponentCommandSet(h, data));
        // acknowledge allocation
        if (!reactivate)
            // !!! ACID 2
            executeCommand(new ComponentCommandAckAlloc(h));
        //components.ackAllocation(h);
        componentInfo = data;
        clients = componentInfo.getClients().toArray();
    } finally {
        componentsLock.unlock();
    }
    if (!isOtherDomainComponent) {
        // add component to client component list to allow dependency checks
        if ((requestor & TYPE_MASK) == COMPONENT_MASK)
            addComponentOwner(componentInfo.getHandle(), requestor);
        //
        // call construct
        //
        boolean constructed = false;
        try {
            componentInfo.getComponent().construct();
            constructed = true;
        } catch (Throwable ex) {
            bcex = new AcsJCannotGetComponentEx(ex);
            bcex.setReason("Failed to construct component '" + name + "', exception caught when invoking 'construct()' method.");
        }
        // remove component from client component list, will be added later
        if ((requestor & TYPE_MASK) == COMPONENT_MASK)
            removeComponentOwner(componentInfo.getHandle(), requestor);
        if (!constructed) {
            // release Component
            componentsLock.lock();
            try {
                // !!! ACID 3
                if (!reactivate)
                    executeCommand(new ComponentCommandDeallocate(h, componentInfo.getHandle(), WhyUnloadedReason.REMOVED));
                // deactivate
                try {
                    container.deactivate_component(componentInfo.getHandle());
                } catch (Exception ex) {
                    bcex = new AcsJCannotGetComponentEx(ex);
                    bcex.setReason("Failed to deactivate component '" + name + "' on container '" + containerName + "'.");
                }
                status.setStatus(ComponentStatus.COMPONENT_NOT_ACTIVATED);
                throw bcex;
            } finally {
                componentsLock.unlock();
            }
        }
    }
    // add component to client component list
    if (requestor != this.getHandle() && requestor != 0)
        addComponentOwner(componentInfo.getHandle(), requestor);
    // add component to container component list
    if (!isOtherDomainComponent)
        synchronized (containerInfo.getComponents()) {
            if (!containerInfo.getComponents().contains(componentInfo.getHandle()))
                executeCommand(new ContainerInfoCommandComponentAdd(containerInfo.getHandle() & HANDLE_MASK, componentInfo.getHandle()));
        //containerInfo.getComponents().add(componentInfo.getHandle());
        }
    // remove component from unavailable list
    if (reactivate) {
        synchronized (unavailableComponents) {
            executeCommand(new UnavailableComponentCommandRemove(name));
        //unavailableComponents.remove(name);
        }
    }
    //
    if (!isOtherDomainComponent)
        notifyComponentActivated(componentInfo, activationTime, executionId);
    //
    if (reactivate) {
        notifyComponentAvailable(requestor, clients, new ComponentInfo[] { componentInfo });
    }
    //
    // notify administrators about the activation
    //
    notifyComponentRequested(new int[] { requestor }, new int[] { componentInfo.getHandle() }, activationTime);
    if (reactivate)
        logger.log(Level.FINE, "Component '" + name + "' (" + HandleHelper.toString(componentInfo.getHandle()) + ") reactivated.");
    else
        logger.log(Level.FINE, "Component '" + name + "' (" + HandleHelper.toString(componentInfo.getHandle()) + ") activated.");
    // notify about the change (only this-domain container which activated the component)...
    if (containerInfo != null)
        topologySortManager.notifyTopologyChange(containerInfo.getHandle());
    status.setStatus(ComponentStatus.COMPONENT_ACTIVATED);
    return componentInfo;
}
Also used : ComponentCommandAllocateHandle(com.cosylab.acs.maci.manager.recovery.ComponentCommandAllocateHandle) UnavailableComponentCommandRemove(com.cosylab.acs.maci.manager.recovery.UnavailableComponentCommandRemove) AcsJCannotGetComponentEx(alma.maciErrType.wrappers.AcsJCannotGetComponentEx) ComponentCommandAckAlloc(com.cosylab.acs.maci.manager.recovery.ComponentCommandAckAlloc) AcsJException(alma.acs.exceptions.AcsJException) NoDefaultComponentException(com.cosylab.acs.maci.NoDefaultComponentException) RemoteTransientException(com.cosylab.acs.maci.RemoteTransientException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) NoResourcesException(com.cosylab.acs.maci.NoResourcesException) URISyntaxException(java.net.URISyntaxException) NamingException(javax.naming.NamingException) BadParametersException(com.cosylab.acs.maci.BadParametersException) CoreException(com.cosylab.acs.maci.CoreException) RemoteException(com.cosylab.acs.maci.RemoteException) TimeoutRemoteException(com.cosylab.acs.maci.TimeoutRemoteException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContainerInfoCommandComponentAdd(com.cosylab.acs.maci.manager.recovery.ContainerInfoCommandComponentAdd) IntArray(com.cosylab.acs.maci.IntArray) ComponentCommandDeallocate(com.cosylab.acs.maci.manager.recovery.ComponentCommandDeallocate) ComponentCommandSet(com.cosylab.acs.maci.manager.recovery.ComponentCommandSet) ComponentInfo(com.cosylab.acs.maci.ComponentInfo)

Aggregations

AcsJException (alma.acs.exceptions.AcsJException)1 AcsJCannotGetComponentEx (alma.maciErrType.wrappers.AcsJCannotGetComponentEx)1 BadParametersException (com.cosylab.acs.maci.BadParametersException)1 ComponentInfo (com.cosylab.acs.maci.ComponentInfo)1 CoreException (com.cosylab.acs.maci.CoreException)1 IntArray (com.cosylab.acs.maci.IntArray)1 NoDefaultComponentException (com.cosylab.acs.maci.NoDefaultComponentException)1 NoResourcesException (com.cosylab.acs.maci.NoResourcesException)1 RemoteException (com.cosylab.acs.maci.RemoteException)1 RemoteTransientException (com.cosylab.acs.maci.RemoteTransientException)1 TimeoutRemoteException (com.cosylab.acs.maci.TimeoutRemoteException)1 ComponentCommandAckAlloc (com.cosylab.acs.maci.manager.recovery.ComponentCommandAckAlloc)1 ComponentCommandAllocateHandle (com.cosylab.acs.maci.manager.recovery.ComponentCommandAllocateHandle)1 ComponentCommandDeallocate (com.cosylab.acs.maci.manager.recovery.ComponentCommandDeallocate)1 ComponentCommandSet (com.cosylab.acs.maci.manager.recovery.ComponentCommandSet)1 ContainerInfoCommandComponentAdd (com.cosylab.acs.maci.manager.recovery.ContainerInfoCommandComponentAdd)1 UnavailableComponentCommandRemove (com.cosylab.acs.maci.manager.recovery.UnavailableComponentCommandRemove)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1