brought over agent ping uses the same db connection

This commit is contained in:
Alex Huang 2011-07-22 11:23:41 -07:00
parent 10ac7753ed
commit 1b56808be5
3 changed files with 58 additions and 51 deletions

View File

@ -18,6 +18,7 @@
package com.cloud.agent.manager; package com.cloud.agent.manager;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException;
import java.util.List; import java.util.List;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -59,13 +60,13 @@ public class AgentMonitor extends Thread implements Listener {
private AlertManager _alertMgr; private AlertManager _alertMgr;
private long _msId; private long _msId;
private ConnectionConcierge _concierge; private ConnectionConcierge _concierge;
protected AgentMonitor() { protected AgentMonitor() {
} }
public AgentMonitor(long msId, HostDao hostDao, VMInstanceDao vmDao, DataCenterDao dcDao, HostPodDao podDao, AgentManagerImpl agentMgr, AlertManager alertMgr, long pingTimeout) { public AgentMonitor(long msId, HostDao hostDao, VMInstanceDao vmDao, DataCenterDao dcDao, HostPodDao podDao, AgentManagerImpl agentMgr, AlertManager alertMgr, long pingTimeout) {
super("AgentMonitor"); super("AgentMonitor");
_msId = msId; _msId = msId;
_pingTimeout = pingTimeout; _pingTimeout = pingTimeout;
_hostDao = hostDao; _hostDao = hostDao;
_agentMgr = agentMgr; _agentMgr = agentMgr;
@ -74,19 +75,22 @@ public class AgentMonitor extends Thread implements Listener {
_dcDao = dcDao; _dcDao = dcDao;
_podDao = podDao; _podDao = podDao;
_alertMgr = alertMgr; _alertMgr = alertMgr;
Connection conn = Transaction.getStandaloneConnection(); try {
if (conn == null) { Connection conn = Transaction.getStandaloneConnectionWithException();
throw new CloudRuntimeException("Unable to get a db connection."); conn.setAutoCommit(true);
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
_concierge = new ConnectionConcierge("AgentMonitor", conn, true);
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to get a db connection", e);
} }
_concierge = new ConnectionConcierge("AgentMonitor", conn, true, true);
} }
// TODO : use host machine time is not safe in clustering environment // TODO : use host machine time is not safe in clustering environment
@Override @Override
public void run() { public void run() {
s_logger.info("Agent Monitor is started."); s_logger.info("Agent Monitor is started.");
while (!_stop) { while (!_stop) {
try { try {
// check every 60 seconds // check every 60 seconds
@ -94,41 +98,41 @@ public class AgentMonitor extends Thread implements Listener {
} catch (InterruptedException e) { } catch (InterruptedException e) {
s_logger.info("Who woke me from my slumber?"); s_logger.info("Who woke me from my slumber?");
} }
GlobalLock lock = GlobalLock.getInternLock("AgentMonitorLock"); GlobalLock lock = GlobalLock.getInternLock("AgentMonitorLock");
if (lock == null) { if (lock == null) {
s_logger.error("Unable to acquire lock. Better luck next time?"); s_logger.error("Unable to acquire lock. Better luck next time?");
continue; continue;
} }
if (!lock.lock(10)) { if (!lock.lock(10)) {
s_logger.info("Someone else is already working on the agents. Skipping my turn"); s_logger.info("Someone else is already working on the agents. Skipping my turn");
continue; continue;
} }
try { try {
long time = (System.currentTimeMillis() >> 10) - _pingTimeout; long time = (System.currentTimeMillis() >> 10) - _pingTimeout;
List<HostVO> hosts = _hostDao.findLostHosts(time); List<HostVO> hosts = _hostDao.findLostHosts(time);
if (s_logger.isInfoEnabled()) { if (s_logger.isInfoEnabled()) {
s_logger.info("Found " + hosts.size() + " hosts behind on ping. pingTimeout : " + _pingTimeout + ", mark time : " + time); s_logger.info("Found " + hosts.size() + " hosts behind on ping. pingTimeout : " + _pingTimeout + ", mark time : " + time);
} }
for (HostVO host : hosts) { for (HostVO host : hosts) {
if (host.getType().equals(Host.Type.ExternalFirewall) || if (host.getType().equals(Host.Type.ExternalFirewall) ||
host.getType().equals(Host.Type.ExternalLoadBalancer) || host.getType().equals(Host.Type.ExternalLoadBalancer) ||
host.getType().equals(Host.Type.TrafficMonitor) || host.getType().equals(Host.Type.TrafficMonitor) ||
host.getType().equals(Host.Type.SecondaryStorage)) { host.getType().equals(Host.Type.SecondaryStorage)) {
continue; continue;
} }
if (host.getManagementServerId() == null || host.getManagementServerId() == _msId) { if (host.getManagementServerId() == null || host.getManagementServerId() == _msId) {
if (s_logger.isInfoEnabled()) { if (s_logger.isInfoEnabled()) {
s_logger.info("Asking agent mgr to investgate why host " + host.getId() + " is behind on ping. last ping time: " + host.getLastPinged()); s_logger.info("Asking agent mgr to investgate why host " + host.getId() + " is behind on ping. last ping time: " + host.getLastPinged());
} }
_agentMgr.disconnect(host.getId(), Event.PingTimeout, true); _agentMgr.disconnect(host.getId(), Event.PingTimeout, true);
} }
} }
hosts = _hostDao.listByStatus(Status.PrepareForMaintenance, Status.ErrorInMaintenance); hosts = _hostDao.listByStatus(Status.PrepareForMaintenance, Status.ErrorInMaintenance);
for (HostVO host : hosts) { for (HostVO host : hosts) {
long hostId = host.getId(); long hostId = host.getId();
@ -147,13 +151,13 @@ public class AgentMonitor extends Thread implements Listener {
} catch (Throwable th) { } catch (Throwable th) {
s_logger.error("Caught the following exception: ", th); s_logger.error("Caught the following exception: ", th);
} finally { } finally {
lock.unlock(); lock.unlock();
} }
} }
s_logger.info("Agent Monitor is leaving the building!"); s_logger.info("Agent Monitor is leaving the building!");
} }
public void signalStop() { public void signalStop() {
_stop = true; _stop = true;
interrupt(); interrupt();
@ -193,10 +197,10 @@ public class AgentMonitor extends Thread implements Listener {
} }
return processed; return processed;
} }
@Override @Override
public AgentControlAnswer processControlCommand(long agentId, AgentControlCommand cmd) { public AgentControlAnswer processControlCommand(long agentId, AgentControlCommand cmd) {
return null; return null;
} }
@Override @Override
@ -207,15 +211,15 @@ public class AgentMonitor extends Thread implements Listener {
public boolean processDisconnect(long agentId, Status state) { public boolean processDisconnect(long agentId, Status state) {
return true; return true;
} }
@Override @Override
public boolean processTimeout(long agentId, long seq) { public boolean processTimeout(long agentId, long seq) {
return true; return true;
} }
@Override @Override
public int getTimeout() { public int getTimeout() {
return -1; return -1;
} }
} }

View File

@ -160,6 +160,7 @@ ALTER TABLE `cloud`.`disk_offering` ADD INDEX `i_disk_offering__removed`(`remove
ALTER TABLE `cloud`.`storage_pool` ADD INDEX `i_storage_pool__removed`(`removed`); ALTER TABLE `cloud`.`storage_pool` ADD INDEX `i_storage_pool__removed`(`removed`);
ALTER TABLE `cloud`.`instance_group` ADD INDEX `i_instance_group__removed`(`removed`); ALTER TABLE `cloud`.`instance_group` ADD INDEX `i_instance_group__removed`(`removed`);
ALTER TABLE `cloud`.`sync_queue_item__queue_proc_number` ADD INDEX `i_sync_queue_item__queue_proc_number`(`queue_proc_number`);
ALTER TABLE `cloud`.`sync_queue_item__queue_proc_number` ADD INDEX `i_sync_queue_item__queue_proc_msid`(`queue_proc_msid`);
ALTER TABLE `cloud`.`op_nwgrp_work` ADD INDEX `i_op_nwgrp_work__taken`(`taken`);

View File

@ -2,5 +2,7 @@
<classpath> <classpath>
<classpathentry exported="true" kind="lib" path="jnetpcap.jar"/> <classpathentry exported="true" kind="lib" path="jnetpcap.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="selenium-java-client-driver.jar"/>
<classpathentry exported="true" kind="lib" path="selenium-server.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>