Search in sources :

Example 16 with ContainerInfo

use of com.cosylab.acs.maci.ContainerInfo in project ACS by ACS-Community.

the class ComponentInfoTopologicalSortManager method run.

/**
	 * (Sync is clean since activationPendingRWLock is acquired and all
	 * notify request is issued only from its reader lock).
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    while (!destroyed) {
        synchronized (this) {
            int dirtyContainersCount;
            synchronized (dirtyContainerMap) {
                dirtyContainersCount = dirtyContainerMap.size();
            }
            if (dirtyContainersCount == 0) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    return;
                }
            }
        }
        if (destroyed)
            return;
        // aquire writer lock to prevent activation/deactivation
        activationPendingRWLock.writeLock().lock();
        try {
            Integer[] conts;
            synchronized (dirtyContainerMap) {
                conts = new Integer[dirtyContainerMap.size()];
                dirtyContainerMap.toArray(conts);
                dirtyContainerMap.clear();
            }
            ComponentInfo[] orderedList;
            synchronized (components) {
                List list = ComponentInfoTopologicalSort.sort(components);
                orderedList = new ComponentInfo[list.size()];
                list.toArray(orderedList);
            }
            synchronized (listLock) {
                currentTSList = orderedList;
            }
            for (int i = 0; i < conts.length; i++) {
                int handle = conts[i].intValue() & HandleConstants.HANDLE_MASK;
                ContainerInfo containerInfo = null;
                synchronized (containers) {
                    if (containers.isAllocated(handle))
                        containerInfo = (ContainerInfo) containers.get(handle);
                }
                if (containerInfo == null || containerInfo.getName() == null)
                    break;
                String containerName = containerInfo.getName();
                // check if shutdown state
                if (pendingContainerShutdown.contains(containerName))
                    break;
                IntArray containerOrderdList = new IntArray(10);
                for (int j = 0; j < orderedList.length; j++) {
                    // if component already deactivated, skip it
                    if (orderedList[j].getContainer() == 0)
                        continue;
                    // this is null proof
                    if (containerName.equals(orderedList[j].getContainerName())) {
                        containerOrderdList.add(orderedList[j].getHandle());
                    }
                }
                // optimization
                if (containerOrderdList.size() > 1)
                    notifyContainerShutdownOrder(containerInfo, containerOrderdList.toArray());
            }
        } finally {
            activationPendingRWLock.writeLock().unlock();
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            logger.log(Level.WARNING, "Exception caught while waiting in run() method");
            ex.printStackTrace();
        }
    }
}
Also used : IntArray(com.cosylab.acs.maci.IntArray) ContainerInfo(com.cosylab.acs.maci.ContainerInfo) ArrayList(java.util.ArrayList) List(java.util.List) ComponentInfo(com.cosylab.acs.maci.ComponentInfo)

Example 17 with ContainerInfo

use of com.cosylab.acs.maci.ContainerInfo in project ACS by ACS-Community.

the class ManagerImpl method getRequestorName.

/*****************************************************************************/
/*************************** [ Utility methods ] *****************************/
/*****************************************************************************/
/**
	 * Returns human-readable and meaningful name of handle.
	 *
	 * @param	id	handle to stringifys
	 * @return	human-readable and meaningful name of handle.
	 */
private String getRequestorName(int id) {
    // parse handle part
    int reqHandle = id & HANDLE_MASK;
    boolean invalidHandle = true;
    StringBuffer name = new StringBuffer(30);
    switch(id & TYPE_MASK) {
        case CONTAINER_MASK:
            //name.append("Container ");
            synchronized (containers) {
                if (containers.isAllocated(reqHandle)) {
                    ContainerInfo info = (ContainerInfo) containers.get(reqHandle);
                    if (info.getHandle() == id) {
                        invalidHandle = false;
                        name.append(info.getName());
                    }
                }
            }
            break;
        case CLIENT_MASK:
            //name.append("Client ");
            synchronized (clients) {
                if (clients.isAllocated(reqHandle)) {
                    ClientInfo info = (ClientInfo) clients.get(reqHandle);
                    if (info.getHandle() == id) {
                        invalidHandle = false;
                        name.append(info.getName());
                    }
                }
            }
            break;
        case ADMINISTRATOR_MASK:
            //name.append("Administrator ");
            synchronized (administrators) {
                if (administrators.isAllocated(reqHandle)) {
                    ClientInfo info = (ClientInfo) administrators.get(reqHandle);
                    if (info.getHandle() == id) {
                        invalidHandle = false;
                        name.append(info.getName());
                    }
                }
            }
            break;
        case COMPONENT_MASK:
            //name.append("Component ");
            componentsLock.lock();
            try {
                if (components.isAllocated(reqHandle)) {
                    ComponentInfo info = (ComponentInfo) components.get(reqHandle);
                    // do additional preallocation check
                    if (info != null && info.getHandle() == id) {
                        invalidHandle = false;
                        name.append(info.getName());
                    }
                }
            } finally {
                componentsLock.unlock();
            }
            break;
        case MANAGER_MASK:
            //name.append("Manager ");
            name.append("Manager");
            invalidHandle = false;
            break;
    }
    if (invalidHandle)
        name.append("<unknown>");
    return name.toString();
}
Also used : ContainerInfo(com.cosylab.acs.maci.ContainerInfo) ClientInfo(com.cosylab.acs.maci.ClientInfo) ComponentInfo(com.cosylab.acs.maci.ComponentInfo)

Example 18 with ContainerInfo

use of com.cosylab.acs.maci.ContainerInfo in project ACS by ACS-Community.

the class ManagerImpl method securityCheck.

/**
	 * Performs security check on given handle and if check if owner has <code>rights</code> permissions granted.
	 *
	 * Validating means checking key part (KEY_MASK) of the handle.
	 *
	 * @param	id	handle to be checked.
	 * @param	rights	checks if owner of the handle has this permissions granted, can be 0.
	 * @throws	AcsJNoPermissionEx	thrown if handle is not valid or handle owner has not enough permissions
	 */
private void securityCheck(int id, int requiredRights) throws AcsJNoPermissionEx {
    try {
        // check if already shutdown
        if (id != this.getHandle() && shutdown.get()) {
            // already shutdown
            AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
            npe.setID(HandleHelper.toString(id));
            npe.setReason("Manager in shutdown state.");
            throw npe;
        }
        // parse handle part
        int handle = id & HANDLE_MASK;
        int grantedRights = 0;
        boolean invalidHandle = true;
        switch(id & TYPE_MASK) {
            case CONTAINER_MASK:
                synchronized (containers) {
                    if (containers.isAllocated(handle)) {
                        ContainerInfo info = (ContainerInfo) containers.get(handle);
                        if (info.getHandle() == id)
                            invalidHandle = false;
                        grantedRights = CONTAINER_RIGHTS;
                    }
                }
                break;
            case CLIENT_MASK:
                synchronized (clients) {
                    if (clients.isAllocated(handle)) {
                        ClientInfo info = (ClientInfo) clients.get(handle);
                        if (info.getHandle() == id)
                            invalidHandle = false;
                        grantedRights = info.getAccessRights();
                    }
                }
                break;
            case ADMINISTRATOR_MASK:
                synchronized (administrators) {
                    if (administrators.isAllocated(handle)) {
                        ClientInfo info = (ClientInfo) administrators.get(handle);
                        if (info.getHandle() == id)
                            invalidHandle = false;
                        grantedRights = info.getAccessRights();
                    }
                }
                break;
            case COMPONENT_MASK:
                componentsLock.lock();
                try {
                    if (components.isAllocated(handle)) {
                        ComponentInfo info = (ComponentInfo) components.get(handle);
                        if (info != null && info.getHandle() == id)
                            invalidHandle = false;
                        grantedRights = AccessRights.REGISTER_COMPONENT;
                    }
                } finally {
                    componentsLock.unlock();
                }
                break;
            case MANAGER_MASK:
                invalidHandle = false;
                grantedRights = AccessRights.REGISTER_COMPONENT | AccessRights.SHUTDOWN_SYSTEM | AccessRights.INTROSPECT_MANAGER;
                break;
        }
        if (invalidHandle) {
            // NO_PERMISSION
            AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
            npe.setID(HandleHelper.toString(id));
            HandleMonitorEntry hme = getHandleReleaseLog(id);
            if (hme != null) {
                final String timeISO = IsoDateFormat.formatDate(new Date(hme.timestamp));
                switch(hme.reason) {
                    case REMOVED:
                        npe.setReason("Invalid handle, handle was properly removed at " + timeISO + ".");
                        break;
                    case TIMEOUT:
                        npe.setReason("Invalid handle, handle was removed due to timeout at " + timeISO + ".");
                        break;
                    case DISAPPEARED:
                        npe.setReason("Invalid handle, handle was removed due to client/container/component disappearing at " + timeISO + ".");
                        break;
                }
            } else {
                if (enableHandleMonitoringDurationMins <= 0)
                    npe.setReason("Invalid handle, handle was never known.");
                else
                    npe.setReason("Invalid handle, handle is not known for the last " + enableHandleMonitoringDurationMins + " minutes.");
            }
            throw npe;
        }
        if ((grantedRights & requiredRights) != requiredRights) {
            // NO_PERMISSION
            AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
            npe.setID(HandleHelper.toString(id));
            npe.setReason("Insufficient rights.");
            throw npe;
        }
    } catch (AcsJNoPermissionEx ex) {
        logger.log(AcsLogLevel.DELOUSE, "securityCheck fails with AcsJNoPermissionEx:", ex);
        throw ex;
    }
}
Also used : AcsJNoPermissionEx(alma.maciErrType.wrappers.AcsJNoPermissionEx) ContainerInfo(com.cosylab.acs.maci.ContainerInfo) ClientInfo(com.cosylab.acs.maci.ClientInfo) ComponentInfo(com.cosylab.acs.maci.ComponentInfo) Date(java.util.Date)

Example 19 with ContainerInfo

use of com.cosylab.acs.maci.ContainerInfo in project ACS by ACS-Community.

the class ManagerImpl method internalNoSyncStartUpContainer.

/**
	 * Start-up container (if it has a deploy info).
	 * @param containerName	name of the container to start up.
	 * @return container info of container, <code>null</code> if failed to start.
	 */
private ContainerInfo internalNoSyncStartUpContainer(String containerName) {
    DAOProxy dao = getContainersDAOProxy();
    if (dao == null)
        return null;
    //
    // read DeployInfo and initiate start-up
    //
    String startOnDemand = readStringCharacteristics(dao, containerName + "/DeployInfo/StartOnDemand", true);
    if (startOnDemand == null || !startOnDemand.equalsIgnoreCase("TRUE"))
        return null;
    String host = readStringCharacteristics(dao, containerName + "/DeployInfo/Host", true);
    if (host == null)
        return null;
    String flags = readStringCharacteristics(dao, containerName + "/DeployInfo/Flags", true);
    if (flags == null)
        flags = "";
    String impLang = readStringCharacteristics(dao, containerName + "/ImplLang", true);
    if (impLang == null)
        impLang = "";
    // add itself as manager reference
    flags += " -m " + transport.getManagerReference();
    short instance = (short) ACSPorts.getBasePort();
    try {
        Daemon daemon = transport.getDaemon(host);
        if (daemon != null)
            daemon.startContainer(impLang, containerName, instance, flags);
        else
            throw new RuntimeException("Failed to get daemon.");
    } catch (Throwable th) {
        RemoteException re = new RemoteException("Failed to connect to ACS daemon on host '" + host + "' to start container '" + containerName + "'.", th);
        logger.log(Level.SEVERE, re.getMessage(), re);
        return null;
    }
    //
    // wait for login
    //
    // HSO: raised timeout from 15 sec to 2 min because of increased usage of autostart containers,
    //      where container start times get extremely long when started in parallel on one machine.
    //      TODO: Refactor manager interface to use callbacks for component getter methods, 
    //            to not block the manager ORB threads with long-lasting container starts.
    final int CONTAINER_STARTUP_TIMEOUT = 120000;
    // notify about new container login
    synchronized (containerLoggedInMonitor) {
        int waitTime = CONTAINER_STARTUP_TIMEOUT;
        while (waitTime > 0) {
            long start = System.currentTimeMillis();
            try {
                containerLoggedInMonitor.wait(waitTime);
            } catch (InterruptedException e) {
                return null;
            }
            // check if container has logged in
            ContainerInfo info = getContainerInfo(containerName);
            if (info != null) {
                return info;
            }
            waitTime = waitTime - (int) (System.currentTimeMillis() - start);
        }
        // container did not logged in within CONTAINER_STARTUP_TIMEOUT ms
        return null;
    }
}
Also used : Daemon(com.cosylab.acs.maci.Daemon) ServiceDaemon(com.cosylab.acs.maci.ServiceDaemon) ContainerInfo(com.cosylab.acs.maci.ContainerInfo) DAOProxy(com.cosylab.cdb.client.DAOProxy) RemoteException(com.cosylab.acs.maci.RemoteException) TimeoutRemoteException(com.cosylab.acs.maci.TimeoutRemoteException)

Example 20 with ContainerInfo

use of com.cosylab.acs.maci.ContainerInfo in project ACS by ACS-Community.

the class ManagerImplTest method testOnDemandContainerStartupComponents.

public void testOnDemandContainerStartupComponents() throws Throwable {
    TestDaemon daemon = new TestDaemon(manager, false);
    transport.registerDeamon("test", daemon);
    // this one starts startup components on auto-start containers
    manager.initializationDone();
    try {
        Thread.sleep(STARTUP_COBS_SLEEP_TIME_MS * 3);
    } catch (InterruptedException ie) {
    }
    TestAdministrator client = new TestAdministrator(administratorName);
    ClientInfo info = manager.login(client);
    assertTrue(info.getHandle() != 0);
    // there should be one Component activated
    ComponentInfo[] infos = manager.getComponentInfo(info.getHandle(), new int[0], "*", "*", true);
    assertEquals(1, infos.length);
    assertEquals("DEMANDER2", infos[0].getName());
    // check if container is logged in
    ContainerInfo[] infos2 = manager.getContainerInfo(info.getHandle(), new int[0], "OnDemandContainer2");
    assertNotNull(infos2);
    assertEquals(1, infos2.length);
}
Also used : ContainerInfo(com.cosylab.acs.maci.ContainerInfo) ClientInfo(com.cosylab.acs.maci.ClientInfo) ComponentInfo(com.cosylab.acs.maci.ComponentInfo)

Aggregations

ContainerInfo (com.cosylab.acs.maci.ContainerInfo)24 ComponentInfo (com.cosylab.acs.maci.ComponentInfo)9 ClientInfo (com.cosylab.acs.maci.ClientInfo)7 AcsJNoPermissionEx (alma.maciErrType.wrappers.AcsJNoPermissionEx)5 Container (com.cosylab.acs.maci.Container)5 DAOProxy (com.cosylab.cdb.client.DAOProxy)5 BadParametersException (com.cosylab.acs.maci.BadParametersException)4 RemoteException (com.cosylab.acs.maci.RemoteException)4 TimeoutRemoteException (com.cosylab.acs.maci.TimeoutRemoteException)4 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 AcsJCannotGetComponentEx (alma.maciErrType.wrappers.AcsJCannotGetComponentEx)3 Component (com.cosylab.acs.maci.Component)3 CoreException (com.cosylab.acs.maci.CoreException)3 NoResourcesException (com.cosylab.acs.maci.NoResourcesException)3 StatusHolder (com.cosylab.acs.maci.StatusHolder)3 ManagerImpl (com.cosylab.acs.maci.manager.ManagerImpl)3 ImplLang (com.cosylab.acs.maci.ImplLang)2 IntArray (com.cosylab.acs.maci.IntArray)2 Manager (com.cosylab.acs.maci.Manager)2