Search in sources :

Example 6 with Client

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

the class ManagerImpl method initialize.

/**
	 * Initializes Manager.
	 * @param	prevayler			implementation of prevayler system
	 * @param	context				remote directory implementation
	 */
public void initialize(Prevayler prevayler, CDBAccess cdbAccess, Context context, final Logger logger, ManagerContainerServices managerContainerServices) {
    this.prevayler = prevayler;
    this.remoteDirectory = context;
    this.logger = logger;
    // needs to be done here, since deserialization is used
    initializeDefaultConfiguration();
    if (cdbAccess != null)
        setCDBAccess(cdbAccess);
    readManagerConfiguration();
    componentsLock = (ProfilingReentrantLock.isProfilingEnabled ? new ProfilingReentrantLock("componentsLock") : new ReentrantLock());
    random = new Random();
    heartbeatTask = new Timer(true);
    delayedDeactivationTask = new Timer(true);
    containerLoggedInMonitor = new Object();
    activationSynchronization = new HashMap<String, ReferenceCountingLock>();
    activationPendingRWLock = new ReaderPreferenceReadWriteLock();
    shutdown = new AtomicBoolean(false);
    threadPool = new ThreadPoolExecutor(poolThreads, poolThreads, Long.MAX_VALUE, TimeUnit.NANOSECONDS, new LinkedBlockingQueue(), new DaemonThreadFactory("managerThreadPool"));
    managerCache = new HashMap<String, Manager>();
    pendingActivations = new HashMap<String, ComponentInfo>();
    pendingContainerShutdown = Collections.synchronizedSet(new HashSet<String>());
    pendingContainerAsyncRequests = new HashMap<String, Deque<ComponentInfoCompletionCallback>>();
    clientMessageQueue = new HashMap<Client, LinkedList<ClientMessageTask>>();
    groupedNotifyTaskMap = new HashMap<Object, GroupedNotifyTask>();
    threadsUsedPercentage = new AtomicInteger(0);
    // create threads
    threadPool.prestartAllCoreThreads();
    // read CDB startup
    try {
        String componentSpec = System.getProperty(NAME_CDB_COMPONENTSPEC);
        if (componentSpec != null) {
            cdbActivation = new ComponentSpec(componentSpec);
            logger.log(Level.INFO, "Using CDB component specification: '" + cdbActivation + "'.");
        }
    } catch (Throwable t) {
        logger.log(Level.WARNING, "Failed to parse '" + NAME_CDB_COMPONENTSPEC + "' variable, " + t.getMessage(), t);
    }
    // check load balancing strategy
    checkLoadBalancingStrategy();
    // establish connect to the alarm system
    try {
        alarmSource = new AlarmSourceImpl(managerContainerServices);
        alarmSource.start();
    } catch (Throwable ex) {
        logger.log(Level.SEVERE, "Failed to initialize Alarm System Interface " + ex.getMessage(), ex);
        alarmSource = null;
    }
    // register ping tasks
    initializePingTasks();
    // handle monitoring removal task
    final long timeInMs = enableHandleMonitoringDurationMins * 60L * 1000;
    if (enableHandleMonitoring && enableHandleMonitoringDurationMins > 0) {
        heartbeatTask.schedule(new TimerTask() {

            @Override
            public void run() {
                try {
                    logHandleCleanup(timeInMs);
                } catch (Throwable th) {
                    logger.log(Level.SEVERE, "Unexpected exception in handle log cleanup task.", th);
                }
            }
        }, 0, timeInMs);
    }
    // start topology sort manager
    topologySortManager = new ComponentInfoTopologicalSortManager(components, containers, activationPendingRWLock, pendingContainerShutdown, threadPool, logger);
    if (prevayler == null)
        statePersitenceFlag.set(false);
    String enDis = statePersitenceFlag.get() ? "enabled" : "disabled";
    logger.info("Manager initialized with state persistence " + enDis + ".");
}
Also used : DaemonThreadFactory(alma.acs.concurrent.DaemonThreadFactory) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Manager(com.cosylab.acs.maci.Manager) Random(java.util.Random) TimerTask(java.util.TimerTask) AlarmSourceImpl(alma.acs.alarmsystem.source.AlarmSourceImpl) Client(com.cosylab.acs.maci.Client) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ComponentSpec(com.cosylab.acs.maci.ComponentSpec) ArrayDeque(java.util.ArrayDeque) Deque(java.util.Deque) LinkedList(java.util.LinkedList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Timer(java.util.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ComponentInfo(com.cosylab.acs.maci.ComponentInfo)

Example 7 with Client

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

the class ManagerImpl method administratorLogout.

/**
	 * Administrator specific logout method
	 * @param	id	handle of the administrators.
	 * @param	pingFailed	flag indicating that ping has failed (i.e. is the reason of this logout).
	 */
private void administratorLogout(int id, boolean pingFailed) {
    TimerTaskClientInfo clientInfo = null;
    int[] componentsArray = null;
    synchronized (administrators) {
        int handle = id & HANDLE_MASK;
        // already logged out
        if (!administrators.isAllocated(handle))
            return;
        clientInfo = (TimerTaskClientInfo) administrators.get(handle);
        // !!! ACID - RemoveAdministratorCommand
        executeCommand(new AdministratorCommandDeallocate(handle, id, pingFailed ? WhyUnloadedReason.DISAPPEARED : WhyUnloadedReason.REMOVED));
        // remove
        //administrators.deallocate(handle);
        componentsArray = clientInfo.getComponents().toArray();
    }
    // deregister client from the heartbeat manager
    clientInfo.getTask().cancel();
    // spawn another task which will release all clientInfo.getComponents()
    threadPool.execute(new ReleaseComponentTask(clientInfo.getHandle(), componentsArray));
    /// TODO !!!!!!!!!!!!!! no more handle -> componentInfo data
    // notify administrators about the logout
    notifyClientLogout(clientInfo, System.currentTimeMillis());
    Client client = clientInfo.getClient();
    if (client != null)
        client.release();
    logger.log(Level.INFO, "Administrator '" + clientInfo.getName() + "' logged out.");
}
Also used : Client(com.cosylab.acs.maci.Client) AdministratorCommandDeallocate(com.cosylab.acs.maci.manager.recovery.AdministratorCommandDeallocate)

Example 8 with Client

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

the class ManagerImpl method clientLogout.

/**
	 * Client specific logout method
	 * @param	id	handle of the client.
	 * @param	pingFailed	flag indicating that ping has failed (i.e. is the reason of this logout).
	 */
private void clientLogout(int id, boolean pingFailed) {
    TimerTaskClientInfo clientInfo = null;
    int[] componentsArray = null;
    synchronized (clients) {
        int handle = id & HANDLE_MASK;
        // already logged out
        if (!clients.isAllocated(handle))
            return;
        clientInfo = (TimerTaskClientInfo) clients.get(handle);
        // !!! ACID - RemoveClientCommand
        executeCommand(new ClientCommandDeallocate(handle, id, pingFailed ? WhyUnloadedReason.DISAPPEARED : WhyUnloadedReason.REMOVED));
        // remove
        //clients.deallocate(handle);
        componentsArray = clientInfo.getComponents().toArray();
    }
    // deregister client from the heartbeat manager
    clientInfo.getTask().cancel();
    // spawn another task which will release all clientInfo.getComponents()
    threadPool.execute(new ReleaseComponentTask(clientInfo.getHandle(), componentsArray));
    //internalReleaseComponents(components to be released, owners IntArray)
    //internalReleaseComponents(clientInfo.getComponents(), clientInfo.getComponents())
    /// TODO !!!!!!!!!!!!!! no more handle -> componentInfo data
    // notify administrators about the logout
    notifyClientLogout(clientInfo, System.currentTimeMillis());
    Client client = clientInfo.getClient();
    if (client != null)
        client.release();
    logger.log(Level.INFO, "Client '" + clientInfo.getName() + "' logged out.");
}
Also used : ClientCommandDeallocate(com.cosylab.acs.maci.manager.recovery.ClientCommandDeallocate) Client(com.cosylab.acs.maci.Client)

Example 9 with Client

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

the class ManagerImplTest method testLogin.

public void testLogin() {
    // test null
    try {
        manager.login(null);
        fail();
    } catch (AcsJNoPermissionEx npe) {
        fail("No permission");
    } catch (BadParametersException bpe) {
        System.out.println("This is OK: " + bpe.getMessage());
    }
    // test null name
    try {
        manager.login(new TestClient(null));
        fail();
    } catch (AcsJNoPermissionEx npe) {
        fail("No permission");
    } catch (BadParametersException bpe) {
        System.out.println("This is OK: " + bpe.getMessage());
    }
    // test null autheticate
    try {
        manager.login(new TestClient("null-auth", null));
        fail();
    } catch (AcsJNoPermissionEx npe) {
        fail("No permission");
    } catch (BadParametersException bpe) {
        System.out.println("This is OK: " + bpe.getMessage());
    }
    // test invalid autheticate
    try {
        manager.login(new TestClient("container-invalid-auth", ClientType.ADMINISTRATOR));
        fail();
    } catch (AcsJNoPermissionEx npe) {
        System.out.println("This is OK: " + npe.toString());
    }
    // test wrong container ImplLang (CDB vs reported by authenticate method)
    try {
        manager.login(new TestContainer("PyContainer", ClientType.CONTAINER, ImplLang.cpp, false));
        fail();
    } catch (AcsJNoPermissionEx npe) {
        System.out.println("This is OK: " + npe.toString());
    }
    //test client login
    Client client = new TestClient(clientName);
    ClientInfo info = null;
    try {
        info = manager.login(client);
    } catch (AcsJNoPermissionEx e) {
        fail("No permission");
    }
    assertNotNull(info);
    assertTrue((info.getHandle() & HandleConstants.CLIENT_MASK) == HandleConstants.CLIENT_MASK);
    assertEquals(info.getClient(), client);
    //test duplicate login
    ClientInfo info2 = null;
    try {
        info2 = manager.login(client);
    } catch (AcsJNoPermissionEx e) {
        fail("No permission");
    }
    assertNotNull(info2);
    assertEquals(info, info2);
    /* 
		// THIS TAKES WAY TOO MUCH TIME
		// DoS attack, there should be no handle left...
		try
		{
			Client differentClient = new TestAlwaysNotEqualClient("different");
			for (int i=0; i<HandleConstants.HANDLE_MASK-1; i++)
			{
				System.out.println(i);
				manager.login(differentClient);			
			}
			
			fail();
		}
		catch (NoResourcesException nre)
		{

			System.out.println("This is OK: "+nre.getMessage());
		}
		*/
    //test administrator login
    Administrator administrator = new TestAdministrator(administratorName);
    try {
        info = manager.login(administrator);
    } catch (AcsJNoPermissionEx e) {
        fail("No permission");
    }
    assertNotNull(info);
    assertTrue((info.getHandle() & HandleConstants.ADMINISTRATOR_MASK) == HandleConstants.ADMINISTRATOR_MASK);
    assertEquals(info.getClient(), administrator);
    //test duplicate login
    try {
        info2 = manager.login(administrator);
    } catch (AcsJNoPermissionEx e) {
        fail("No permission");
    }
    assertNotNull(info2);
    assertEquals(info, info2);
    //test container login
    TestContainer container = new TestContainer(containerName);
    try {
        info = manager.login(container);
    } catch (AcsJNoPermissionEx e) {
        fail("No permission");
    }
    assertNotNull(info);
    assertTrue((info.getHandle() & HandleConstants.CONTAINER_MASK) == HandleConstants.CONTAINER_MASK);
    assertEquals(info.getClient(), container);
    //test duplicate login (same instance) - allowed
    try {
        info2 = manager.login(container);
    } catch (AcsJNoPermissionEx e) {
        fail("No permission");
    }
    assertNotNull(info2);
    assertEquals(info.getHandle(), info2.getHandle());
    container.setHandle(info2.getHandle());
    //test duplicate login (same instance name, previous container alive) - reject
    TestContainer containerSameName = new TestContainer(containerName);
    try {
        info2 = manager.login(containerSameName);
        fail("No permission expected");
    } catch (AcsJNoPermissionEx e) {
        System.out.println("This is OK: " + e.toString());
    }
    // ... now make first instance return handle 0
    // this should allow the login
    container.setHandle(0);
    try {
        info2 = manager.login(containerSameName);
    } catch (AcsJNoPermissionEx e) {
        fail("No permission");
    }
    assertNotNull(info2);
    assertEquals(info.getHandle(), info2.getHandle());
    // wait for some time, so that manager reports passes postponed start-up component activation
    try {
        Thread.sleep(STARTUP_COBS_SLEEP_TIME_MS);
    } catch (InterruptedException ie) {
    }
}
Also used : Administrator(com.cosylab.acs.maci.Administrator) AcsJNoPermissionEx(alma.maciErrType.wrappers.AcsJNoPermissionEx) ClientInfo(com.cosylab.acs.maci.ClientInfo) Client(com.cosylab.acs.maci.Client) BadParametersException(com.cosylab.acs.maci.BadParametersException)

Aggregations

Client (com.cosylab.acs.maci.Client)9 AcsJNoPermissionEx (alma.maciErrType.wrappers.AcsJNoPermissionEx)6 ClientInfo (com.cosylab.acs.maci.ClientInfo)6 Administrator (com.cosylab.acs.maci.Administrator)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 BadParametersException (com.cosylab.acs.maci.BadParametersException)2 ComponentInfo (com.cosylab.acs.maci.ComponentInfo)2 AlarmSourceImpl (alma.acs.alarmsystem.source.AlarmSourceImpl)1 DaemonThreadFactory (alma.acs.concurrent.DaemonThreadFactory)1 ComponentSpec (com.cosylab.acs.maci.ComponentSpec)1 Container (com.cosylab.acs.maci.Container)1 Manager (com.cosylab.acs.maci.Manager)1 NoResourcesException (com.cosylab.acs.maci.NoResourcesException)1 AdministratorCommandDeallocate (com.cosylab.acs.maci.manager.recovery.AdministratorCommandDeallocate)1 ClientCommandDeallocate (com.cosylab.acs.maci.manager.recovery.ClientCommandDeallocate)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1