use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.
the class ClusterManagerImpl method getNotificationTask.
private Runnable getNotificationTask() {
return new ManagedContextRunnable() {
@Override
protected void runInContext() {
while (true) {
synchronized (_notificationMsgs) {
try {
_notificationMsgs.wait(1000);
} catch (final InterruptedException e) {
}
}
ClusterManagerMessage msg = null;
while ((msg = getNextNotificationMessage()) != null) {
try {
switch(msg.getMessageType()) {
case nodeAdded:
if (msg.getNodes() != null && msg.getNodes().size() > 0) {
final Profiler profiler = new Profiler();
profiler.start();
notifyNodeJoined(msg.getNodes());
profiler.stop();
if (profiler.getDurationInMillis() > 1000) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Notifying management server join event took " + profiler.getDurationInMillis() + " ms");
}
} else {
s_logger.warn("Notifying management server join event took " + profiler.getDurationInMillis() + " ms");
}
}
break;
case nodeRemoved:
if (msg.getNodes() != null && msg.getNodes().size() > 0) {
final Profiler profiler = new Profiler();
profiler.start();
notifyNodeLeft(msg.getNodes());
profiler.stop();
if (profiler.getDurationInMillis() > 1000) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Notifying management server leave event took " + profiler.getDurationInMillis() + " ms");
}
} else {
s_logger.warn("Notifying management server leave event took " + profiler.getDurationInMillis() + " ms");
}
}
break;
case nodeIsolated:
notifyNodeIsolated();
break;
default:
assert false;
break;
}
} catch (final Throwable e) {
s_logger.warn("Unexpected exception during cluster notification. ", e);
}
}
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
}
}
}
};
}
use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.
the class ClientTransportProvider method initialize.
public void initialize(String serverAddress, int serverPort) {
_serverAddress = serverAddress;
_serverPort = serverPort;
_executor = Executors.newFixedThreadPool(_poolSize, new NamedThreadFactory("Transport-Worker"));
_connection = new ClientTransportConnection(this);
_executor.execute(new ManagedContextRunnable() {
@Override
protected void runInContext() {
try {
_connection.connect(_serverAddress, _serverPort);
} catch (Throwable e) {
s_logger.info("[ignored]" + "error during ipc client initialization: " + e.getLocalizedMessage());
}
}
});
}
use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.
the class ClusteredAgentManagerImpl method getTransferScanTask.
private Runnable getTransferScanTask() {
return new ManagedContextRunnable() {
@Override
protected void runInContext() {
try {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Clustered agent transfer scan check, management server id:" + _nodeId);
}
synchronized (_agentToTransferIds) {
if (_agentToTransferIds.size() > 0) {
s_logger.debug("Found " + _agentToTransferIds.size() + " agents to transfer");
// for (Long hostId : _agentToTransferIds) {
for (final Iterator<Long> iterator = _agentToTransferIds.iterator(); iterator.hasNext(); ) {
final Long hostId = iterator.next();
final AgentAttache attache = findAttache(hostId);
// if the thread:
// 1) timed out waiting for the host to reconnect
// 2) recipient management server is not active any more
// 3) if the management server doesn't own the host any more
// remove the host from re-balance list and delete from op_host_transfer DB
// no need to do anything with the real attache as we haven't modified it yet
final Date cutTime = DateUtil.currentGMTTime();
final HostTransferMapVO transferMap = _hostTransferDao.findActiveHostTransferMapByHostId(hostId, new Date(cutTime.getTime() - rebalanceTimeOut));
if (transferMap == null) {
s_logger.debug("Timed out waiting for the host id=" + hostId + " to be ready to transfer, skipping rebalance for the host");
iterator.remove();
_hostTransferDao.completeAgentTransfer(hostId);
continue;
}
if (transferMap.getInitialOwner() != _nodeId || attache == null || attache.forForward()) {
s_logger.debug("Management server " + _nodeId + " doesn't own host id=" + hostId + " any more, skipping rebalance for the host");
iterator.remove();
_hostTransferDao.completeAgentTransfer(hostId);
continue;
}
final ManagementServerHostVO ms = _mshostDao.findByMsid(transferMap.getFutureOwner());
if (ms != null && ms.getState() != ManagementServerHost.State.Up) {
s_logger.debug("Can't transfer host " + hostId + " as it's future owner is not in UP state: " + ms + ", skipping rebalance for the host");
iterator.remove();
_hostTransferDao.completeAgentTransfer(hostId);
continue;
}
if (attache.getQueueSize() == 0 && attache.getNonRecurringListenersSize() == 0) {
iterator.remove();
try {
_executor.execute(new RebalanceTask(hostId, transferMap.getInitialOwner(), transferMap.getFutureOwner()));
} catch (final RejectedExecutionException ex) {
s_logger.warn("Failed to submit rebalance task for host id=" + hostId + "; postponing the execution");
continue;
}
} else {
s_logger.debug("Agent " + hostId + " can't be transfered yet as its request queue size is " + attache.getQueueSize() + " and listener queue size is " + attache.getNonRecurringListenersSize());
}
}
} else {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Found no agents to be transfered by the management server " + _nodeId);
}
}
}
} catch (final Throwable e) {
s_logger.error("Problem with the clustered agent transfer scan check!", e);
}
}
};
}
use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.
the class AgentResourceBase method executeRequest.
@Override
public final Answer executeRequest(final Command cmd) {
final AtomicReference<Answer> result = new AtomicReference<Answer>();
new ManagedContextRunnable() {
@Override
protected void runInContext() {
result.set(executeRequestInContext(cmd));
}
}.run();
return result.get();
}
use of org.apache.cloudstack.managed.context.ManagedContextRunnable in project cloudstack by apache.
the class SystemVmLoadScanner method getCapacityScanTask.
private Runnable getCapacityScanTask() {
return new ManagedContextRunnable() {
@Override
protected void runInContext() {
try {
CallContext callContext = CallContext.current();
assert (callContext != null);
AsyncJobExecutionContext.registerPseudoExecutionContext(callContext.getCallingAccountId(), callContext.getCallingUserId());
reallyRun();
AsyncJobExecutionContext.unregister();
} catch (Throwable e) {
s_logger.warn("Unexpected exception " + e.getMessage(), e);
}
}
private void reallyRun() {
loadScan();
}
};
}
Aggregations