Skip to content

Commit 6c88e9a

Browse files
authored
Dont add host back after agent service restart (#4228)
1 parent d6db476 commit 6c88e9a

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

agent/src/main/java/com/cloud/agent/Agent.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,19 @@ protected void cancelTasks() {
420420
}
421421
}
422422

423+
/**
424+
* Cleanup agent zone properties.
425+
*
426+
* Unset zone, cluster and pod values so that host is not added back
427+
* when service is restarted. This will be set to proper values
428+
* when host is added back
429+
*/
430+
protected void cleanupAgentZoneProperties() {
431+
_shell.setPersistentProperty(null, "zone", "");
432+
_shell.setPersistentProperty(null, "cluster", "");
433+
_shell.setPersistentProperty(null, "pod", "");
434+
}
435+
423436
public synchronized void lockStartupTask(final Link link) {
424437
_startup = new StartupTask(link);
425438
_timer.schedule(_startup, _startupWait);
@@ -603,6 +616,9 @@ protected void processRequest(final Request request, final Link link) {
603616
final ShutdownCommand shutdown = (ShutdownCommand)cmd;
604617
s_logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
605618
cancelTasks();
619+
if (shutdown.isRemoveHost()) {
620+
cleanupAgentZoneProperties();
621+
}
606622
_reconnectAllowed = false;
607623
answer = new Answer(cmd, true, null);
608624
} else if (cmd instanceof ReadyCommand && ((ReadyCommand)cmd).getDetails() != null) {

core/src/main/java/com/cloud/agent/api/ShutdownCommand.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class ShutdownCommand extends Command {
3030

3131
private String reason;
3232
private String detail;
33+
private boolean removeHost;
3334

3435
protected ShutdownCommand() {
3536
super();
@@ -41,6 +42,13 @@ public ShutdownCommand(String reason, String detail) {
4142
this.detail = detail;
4243
}
4344

45+
public ShutdownCommand(String reason, String detail, boolean removeHost) {
46+
super();
47+
this.reason = reason;
48+
this.detail = detail;
49+
this.removeHost = removeHost;
50+
}
51+
4452
/**
4553
* @return return the reason the agent shutdown. If Unknown, call getDetail() for any details.
4654
*/
@@ -52,6 +60,10 @@ public String getDetail() {
5260
return detail;
5361
}
5462

63+
public boolean isRemoveHost() {
64+
return removeHost;
65+
}
66+
5567
@Override
5668
public boolean executeInSequence() {
5769
return true;

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
411411
public final static ConfigKey<Long> IOPS_MAX_WRITE_LENGTH = new ConfigKey<Long>(Long.class, "vm.disk.iops.maximum.write.length", "Advanced", "0",
412412
"Maximum IOPS write burst duration (seconds). If '0' (zero) then does not check for maximum burst length.", true, ConfigKey.Scope.Global, null);
413413

414+
public static final ConfigKey<Boolean> ADD_HOST_ON_SERVICE_RESTART_KVM = new ConfigKey<Boolean>(Boolean.class, "add.host.on.service.restart.kvm", "Advanced", "true",
415+
"Indicates whether the host will be added back to cloudstack after restarting agent service on host. If false it wont be added back even after service restart",
416+
true, ConfigKey.Scope.Global, null);
417+
414418
private static final String IOPS_READ_RATE = "IOPS Read";
415419
private static final String IOPS_WRITE_RATE = "IOPS Write";
416420
private static final String BYTES_READ_RATE = "Bytes Read";
@@ -6381,6 +6385,7 @@ public String getConfigComponentName() {
63816385

63826386
@Override
63836387
public ConfigKey<?>[] getConfigKeys() {
6384-
return new ConfigKey<?>[] {SystemVMUseLocalStorage, IOPS_MAX_READ_LENGTH, IOPS_MAX_WRITE_LENGTH, BYTES_MAX_READ_LENGTH, BYTES_MAX_WRITE_LENGTH};
6388+
return new ConfigKey<?>[] {SystemVMUseLocalStorage, IOPS_MAX_READ_LENGTH, IOPS_MAX_WRITE_LENGTH,
6389+
BYTES_MAX_READ_LENGTH, BYTES_MAX_WRITE_LENGTH, ADD_HOST_ON_SERVICE_RESTART_KVM};
63856390
}
63866391
}

server/src/main/java/com/cloud/hypervisor/kvm/discoverer/LibvirtServerDiscoverer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
import com.cloud.utils.ssh.SSHCmdHelper;
6868
import com.trilead.ssh2.Connection;
6969

70+
import static com.cloud.configuration.ConfigurationManagerImpl.ADD_HOST_ON_SERVICE_RESTART_KVM;
71+
7072
public abstract class LibvirtServerDiscoverer extends DiscovererBase implements Discoverer, Listener, ResourceStateAdapter {
7173
private static final Logger s_logger = Logger.getLogger(LibvirtServerDiscoverer.class);
7274
private final int _waitTime = 5; /* wait for 5 minutes */
@@ -348,6 +350,7 @@ private void setupAgentSecurity(final Connection sshConnection, final String age
348350
_hostDao.saveDetails(connectedHost);
349351
return resources;
350352
} catch (DiscoveredWithErrorException e) {
353+
s_logger.error("DiscoveredWithErrorException caught and rethrowing, message: "+ e.getMessage());
351354
throw e;
352355
} catch (Exception e) {
353356
String msg = " can't setup agent, due to " + e.toString() + " - " + e.getMessage();
@@ -474,7 +477,7 @@ public DeleteHostAnswer deleteHost(HostVO host, boolean isForced, boolean isForc
474477

475478
_resourceMgr.deleteRoutingHost(host, isForced, isForceDeleteStorage);
476479
try {
477-
ShutdownCommand cmd = new ShutdownCommand(ShutdownCommand.DeleteHost, null);
480+
ShutdownCommand cmd = new ShutdownCommand(ShutdownCommand.DeleteHost, null, !ADD_HOST_ON_SERVICE_RESTART_KVM.value());
478481
agentMgr.send(host.getId(), cmd);
479482
} catch (AgentUnavailableException e) {
480483
s_logger.warn("Sending ShutdownCommand failed: ", e);

0 commit comments

Comments
 (0)