use of com.cosylab.acs.maci.Container in project ACS by ACS-Community.
the class ManagerImpl method containerLogout.
/**
* Container specific logout method
* @param id handle of the container.
* @param pingFailed flag indicating that ping has failed (i.e. is the reason of this logout).
*/
private void containerLogout(int id, boolean pingFailed) {
TimerTaskContainerInfo containerInfo = null;
synchronized (containers) {
int handle = id & HANDLE_MASK;
// already logged out
if (!containers.isAllocated(handle))
return;
containerInfo = (TimerTaskContainerInfo) containers.get(handle);
// cancel all "old" container async request
AcsJCannotGetComponentEx acgcex = new AcsJCannotGetComponentEx();
acgcex.setReason("Request canceled due to container logout.");
cancelPendingContainerAsyncRequestWithException(containerInfo.getName(), acgcex);
// !!! ACID - RemoveContainerCommand
executeCommand(new ContainerCommandDeallocate(handle, id, pingFailed ? WhyUnloadedReason.DISAPPEARED : WhyUnloadedReason.REMOVED));
// remove
//containers.deallocate(handle);
}
// deregister container from the heartbeat manager
containerInfo.getTask().cancel();
// make all container components unavailable
markContainersComponentsUnavailable(containerInfo);
/// TODO !!!!!!!!!!!!!! no more handle -> componentInfo data
// notify administrators about the logout
notifyContainerLogout(containerInfo, System.currentTimeMillis());
Container container = containerInfo.getContainer();
if (container != null)
container.release();
logger.log(Level.INFO, "Container '" + containerInfo.getName() + "' logged out.");
}
use of com.cosylab.acs.maci.Container in project ACS by ACS-Community.
the class ManagerImpl method login.
/**
* @see com.cosylab.acs.maci.Manager#login(Client)
*/
public ClientInfo login(Client reference) throws AcsJNoPermissionEx {
// check if already shutdown
if (shutdown.get()) {
// already shutdown
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setReason("Manager in shutdown state.");
throw npe;
}
if (reference == null) {
// BAD_PARAM
BadParametersException af = new BadParametersException("Non-null 'reference' expected.");
throw af;
}
/****************************************************************/
ClientInfo info = null;
try {
long executionId = generateExecutionId();
AuthenticationData reply = reference.authenticate(executionId, "Identify yourself");
if (reply == null) {
// BAD_PARAM
BadParametersException af = new BadParametersException("Invalid response to 'Client::authenticate()' method - non-null structure expected.");
throw af;
} else if (reply.getClientType() == null) {
// BAD_PARAM
BadParametersException af = new BadParametersException("Invalid response to 'Client::authenticate()' method - non-null client type expected.");
throw af;
} else if (reply.getImplLang() == null) {
// BAD_PARAM
BadParametersException af = new BadParametersException("Invalid response to 'Client::authenticate()' method - no-null implementation language expected.");
throw af;
}
// get client's name
String name = reference.name();
if (name == null) {
// BAD_PARAM
BadParametersException af = new BadParametersException("Invalid response to 'Client::name()' method - non-null string expected.");
throw af;
}
logger.log(Level.FINE, "'" + name + "' is logging in.");
final long timeStamp = reply.getTimeStamp() > 0 ? reply.getTimeStamp() : System.currentTimeMillis();
if (reply.getExecutionId() != 0)
executionId = generateExecutionId();
// delegate
switch(reply.getClientType()) {
// container
case CONTAINER:
if (reference instanceof Container) {
info = containerLogin(name, reply, (Container) reference, timeStamp, executionId);
} else {
// NO_PERMISSION
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setReason("Given reply to 'Client::authenticate()' method indicated container login, but given reference does not implement 'maci::Container' interface.");
npe.setID(name);
throw npe;
}
break;
// client
case CLIENT:
info = clientLogin(name, reply, reference, timeStamp, executionId);
break;
// supervisor (administrator)
case ADMINISTRATOR:
if (reference instanceof Administrator) {
info = administratorLogin(name, reply, (Administrator) reference, timeStamp, executionId);
} else {
// NO_PERMISSION
AcsJNoPermissionEx npe = new AcsJNoPermissionEx();
npe.setReason("Given reply to 'Client::authenticate()' method indicated administrator login, but given reference does not implement 'maci::Administrator' interface.");
npe.setID(name);
throw npe;
}
break;
default:
assert (false);
}
} catch (AcsJNoPermissionEx npe) {
throw npe;
} catch (BadParametersException bpe) {
throw bpe;
} catch (NoResourcesException nre) {
throw nre;
} catch (RemoteException re) {
// TODO @todo exception
RuntimeException rt = new RuntimeException("Exception caught while examining the client. Login rejected.", re);
throw rt;
} catch (Throwable ex) {
// TODO @todo exception
RuntimeException rt = new RuntimeException("Unexpected exception during login. Login rejected.", ex);
throw rt;
}
/****************************************************************/
logger.log(Level.FINE, "Client with handle '" + HandleHelper.toString(info.getHandle()) + "' has logged in.");
return info;
}
use of com.cosylab.acs.maci.Container in project ACS by ACS-Community.
the class ManagerImpl method shutdownContainer.
/**
* @see com.cosylab.acs.maci.Manager#shutdownContainer(int, java.lang.String, int)
*/
public void shutdownContainer(int id, String containerName, int action) throws AcsJNoPermissionEx {
// check if null
if (containerName == null) {
// BAD_PARAM
BadParametersException af = new BadParametersException("Non-null 'containerName' expected.");
throw af;
}
/****************************************************************/
// the caller must have SHUTDOWN_SYSTEM access rights,
securityCheck(id, AccessRights.SHUTDOWN_SYSTEM);
Container container;
ContainerInfo containerInfo = getContainerInfo(containerName);
if (containerInfo == null || (container = containerInfo.getContainer()) == null) {
// NO_RESOURCES
NoResourcesException nre = new NoResourcesException("Container '" + containerName + "' not logged in.");
throw nre;
}
pendingContainerShutdown.add(containerInfo.getName());
try {
// release components
try {
// get shutdown order
ComponentInfo[] infos = topologySortManager.getComponentShutdownOrder(containerInfo);
releaseComponents(infos);
} catch (Throwable th) {
CoreException ce = new CoreException("Failed to release components on container '" + containerName + "'.", th);
reportException(ce);
}
// shutdown (or disconnect)
try {
if (action == 0)
container.disconnect();
else
container.shutdown(action);
} catch (Throwable th) {
// NO_RESOURCES
NoResourcesException nre = new NoResourcesException("Failed to shutdown container '" + containerName + "'.", th);
throw nre;
}
} finally {
pendingContainerShutdown.remove(containerInfo.getName());
}
/****************************************************************/
}
use of com.cosylab.acs.maci.Container in project ACS by ACS-Community.
the class ManagerImpl method internalNoSyncDeactivateComponent.
/**
* Deactivate component, issue deactivate reeust to container (or other manager).
* @param componentInfo info about component to be deactivated.
*/
private void internalNoSyncDeactivateComponent(ComponentInfo componentInfo) throws Throwable {
// unbind from remote directory
//unbind(convertToHiearachical(componentInfo.getName()), "O");
int handle = componentInfo.getHandle() & HANDLE_MASK;
int owners = componentInfo.getClients().size();
try {
//
// get container/remote manager
//
String name = componentInfo.getName();
boolean isOtherDomainComponent = name.startsWith(CURL_URI_SCHEMA);
if (isOtherDomainComponent) {
Manager remoteManager = null;
// @todo MF do the login?
try {
String domainName = CURLHelper.createURI(name).getAuthority();
remoteManager = getManagerForDomain(domainName);
if (remoteManager == null)
throw new CoreException("Failed to obtain manager for domain '" + domainName + "'.");
} catch (Throwable th) {
logger.log(Level.WARNING, "Failed to obtain non-local manager required by component '" + name + "'.", th);
throw th;
}
// release component
try {
URI curlName = CURLHelper.createURI(name);
// @todo MF tmp (handle)
remoteManager.releaseComponent(INTERDOMAIN_MANAGER_HANDLE, curlName);
} catch (Throwable th) {
logger.log(Level.WARNING, "Failed to release component '" + componentInfo.getName() + "' on remote manager.'", th);
throw th;
}
} else {
//
// search for container by its name
//
Container container = null;
ContainerInfo containerInfo = null;
int containerHandle = componentInfo.getContainer();
// if containerHandle equals 0, we have unavailable or registered component
if (containerHandle != 0) {
containerInfo = getContainerInfo(containerHandle);
if (containerInfo != null) {
// remove component from container component list
synchronized (containerInfo.getComponents()) {
// !!! ACID
if (containerInfo.getComponents().contains(componentInfo.getHandle()))
executeCommand(new ContainerInfoCommandComponentRemove(containerInfo.getHandle() & HANDLE_MASK, componentInfo.getHandle()));
//containerInfo.getComponents().remove(componentInfo.getHandle());
}
// we allow this (since releasing components is part of container shutdown procedure)
//checkContainerState(containerInfo);
container = containerInfo.getContainer();
}
// required container is not logged in
if (container == null) {
// then simply do not do the deactivation
String containerName;
if (containerInfo != null)
containerName = containerInfo.getName();
else
containerName = HandleHelper.toString(componentInfo.getContainer());
logger.log(Level.WARNING, "Container '" + containerName + "' required by component '" + componentInfo.getName() + "' is not logged in.");
}
}
if (container != null) {
// log info
logger.log(Level.INFO, "Deactivating component '" + componentInfo.getName() + "' (" + HandleHelper.toString(componentInfo.getHandle()) + ") on container '" + containerInfo.getName() + "'.");
// destruct
try {
componentInfo.getComponent().destruct();
} catch (Throwable ex) {
RemoteException re = new RemoteException("Failed to destruct component '" + componentInfo.getName() + "', exception caught when invoking 'destruct()' method.", ex);
logger.log(Level.SEVERE, re.getMessage(), re);
throw ex;
}
long deactivationTime = 0;
// deactivate component in anycase
try {
container.deactivate_component(componentInfo.getHandle());
deactivationTime = System.currentTimeMillis();
} catch (AcsJException aex) {
logger.log(Level.SEVERE, aex.getMessage(), aex);
throw aex;
} catch (Throwable ex) {
RemoteException re = new RemoteException("Failed to deactivate component '" + componentInfo.getName() + "' (" + HandleHelper.toString(componentInfo.getHandle()) + ") on container '" + containerInfo.getName() + "'.", ex);
logger.log(Level.SEVERE, re.getMessage(), re);
throw ex;
}
// notify administrators about deactivation, but not if failed
if (deactivationTime != 0)
notifyComponentDeactivated(componentInfo.getHandle(), deactivationTime);
// shutdown container if required (and necessary)
conditionalShutdownContainer(containerInfo);
}
}
} finally {
if (owners == 0) {
// deallocate Component
componentsLock.lock();
try {
executeCommand(new ComponentCommandDeallocate(handle, componentInfo.getHandle(), WhyUnloadedReason.REMOVED));
//components.deallocate(handle);
} finally {
componentsLock.unlock();
}
}
}
// log info
logger.log(Level.INFO, "Component '" + componentInfo.getName() + "' (" + HandleHelper.toString(componentInfo.getHandle()) + ") deactivated.");
// release all subcomponents (just like client logoff)
// component should have already done this by itself, but take care of clean cleanup
// what about that: if subcomponent becomes unavailable, does component also becomes?!
// no, it is notified and it handles situation by its own way (e.g. changes component state).
// Just like it already handles activation (manager does not care for dependecy trees).
int[] subcomponents = null;
// no not hold the lock
synchronized (componentInfo.getComponents()) {
if (componentInfo.getComponents().size() > 0) {
IntArray toCleanupList = new IntArray();
IntArray comps = componentInfo.getComponents();
for (int i = 0; i < comps.size(); i++) if (components.isAllocated(comps.get(i) & HANDLE_MASK))
toCleanupList.add(comps.get(i));
if (toCleanupList.size() > 0)
subcomponents = toCleanupList.toArray();
}
//subcomponents = componentInfo.getComponents().toArray();
}
if (subcomponents != null && subcomponents.length > 0)
new ReleaseComponentTask(componentInfo.getHandle(), subcomponents).run();
// make unavailable (deactivation was forced)
if (owners > 0)
makeUnavailable(componentInfo);
}
use of com.cosylab.acs.maci.Container in project ACS by ACS-Community.
the class ManagerImplTest method testConcurrentContainersLogin.
public void testConcurrentContainersLogin() {
final Object sync = new Object();
final AtomicBoolean done = new AtomicBoolean(false);
final AtomicInteger counter = new AtomicInteger(0);
final AtomicInteger loginCounter = new AtomicInteger(0);
final int CONCURRENT_LOGINS = 100;
final CyclicBarrier barrier = new CyclicBarrier(CONCURRENT_LOGINS);
class LoginWorker implements Runnable {
public void run() {
final String containerName = "Container" + counter.incrementAndGet();
Container container = new TestContainer(containerName);
try {
barrier.await();
} catch (Throwable th) {
return;
}
ClientInfo info = null;
try {
info = manager.login(container);
} catch (AcsJNoPermissionEx e) {
fail("No permission");
}
// note that if this fails, tearDown will be called (and manager shutdown)
assertTrue(containerName + " failed to login.", info != null && info.getHandle() != 0);
if (loginCounter.incrementAndGet() == CONCURRENT_LOGINS) {
synchronized (sync) {
sync.notifyAll();
}
}
}
}
synchronized (sync) {
// spawn threads
for (int i = 0; i < CONCURRENT_LOGINS; ++i) new Thread(new LoginWorker()).start();
try {
sync.wait(CONCURRENT_LOGINS * 500);
} catch (InterruptedException e) {
}
}
assertTrue("All containers failed to login successfully in time.", loginCounter.get() == CONCURRENT_LOGINS);
}
Aggregations