Skip to content

Commit 3c5a1f0

Browse files
committed
Merge branch 'main' into ExternalDeploymentIntegration
2 parents 31e35ff + 3e3a0c0 commit 3c5a1f0

File tree

41 files changed

+837
-326
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+837
-326
lines changed

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -453,22 +453,30 @@ private void scheduleCertificateRenewalTask() {
453453
certExecutor.schedule(new PostCertificateRenewalTask(this), 5, TimeUnit.SECONDS);
454454
}
455455

456-
private void scheduleHostLBCheckerTask(final long checkInterval) {
456+
private void scheduleHostLBCheckerTask(final String lbAlgorithm, final long checkInterval) {
457457
String name = "HostLBCheckerTask";
458458
if (hostLbCheckExecutor != null && !hostLbCheckExecutor.isShutdown()) {
459+
logger.info("Shutting down the preferred host checker task {}", name);
459460
hostLbCheckExecutor.shutdown();
460461
try {
461462
if (!hostLbCheckExecutor.awaitTermination(1, TimeUnit.SECONDS)) {
462463
hostLbCheckExecutor.shutdownNow();
463464
}
464465
} catch (InterruptedException e) {
465-
logger.debug("Forcing {} shutdown as it did not shutdown in the desired time due to: {}",
466+
logger.debug("Forcing the preferred host checker task {} shutdown as it did not shutdown in the desired time due to: {}",
466467
name, e.getMessage());
467468
hostLbCheckExecutor.shutdownNow();
468469
}
469470
}
470471
if (checkInterval > 0L) {
471-
logger.info("Scheduling preferred host task with host.lb.interval={}ms", checkInterval);
472+
if ("shuffle".equalsIgnoreCase(lbAlgorithm)) {
473+
logger.info("Scheduling the preferred host checker task to trigger once (to apply lb algorithm '{}') after host.lb.interval={} ms", lbAlgorithm, checkInterval);
474+
hostLbCheckExecutor = Executors.newSingleThreadScheduledExecutor((new NamedThreadFactory(name)));
475+
hostLbCheckExecutor.schedule(new PreferredHostCheckerTask(), checkInterval, TimeUnit.MILLISECONDS);
476+
return;
477+
}
478+
479+
logger.info("Scheduling a recurring preferred host checker task with lb algorithm '{}' and host.lb.interval={} ms", lbAlgorithm, checkInterval);
472480
hostLbCheckExecutor = Executors.newSingleThreadScheduledExecutor((new NamedThreadFactory(name)));
473481
hostLbCheckExecutor.scheduleAtFixedRate(new PreferredHostCheckerTask(), checkInterval, checkInterval,
474482
TimeUnit.MILLISECONDS);
@@ -928,7 +936,7 @@ private Answer setupAgentCertificate(final SetupCertificateCommand cmd) {
928936
return new SetupCertificateAnswer(true);
929937
}
930938

931-
private void processManagementServerList(final List<String> msList, final List<String> avoidMsList, final String lbAlgorithm, final Long lbCheckInterval) {
939+
private void processManagementServerList(final List<String> msList, final List<String> avoidMsList, final String lbAlgorithm, final Long lbCheckInterval, final boolean triggerHostLB) {
932940
if (CollectionUtils.isNotEmpty(msList) && StringUtils.isNotEmpty(lbAlgorithm)) {
933941
try {
934942
final String newMSHosts = String.format("%s%s%s", com.cloud.utils.StringUtils.toCSVList(msList), IAgentShell.hostLbAlgorithmSeparator, lbAlgorithm);
@@ -941,22 +949,24 @@ private void processManagementServerList(final List<String> msList, final List<S
941949
}
942950
}
943951
shell.setAvoidHosts(avoidMsList);
944-
if ("shuffle".equals(lbAlgorithm)) {
945-
scheduleHostLBCheckerTask(0);
946-
} else {
947-
scheduleHostLBCheckerTask(shell.getLbCheckerInterval(lbCheckInterval));
952+
if (triggerHostLB) {
953+
logger.info("Triggering the preferred host checker task now");
954+
ScheduledExecutorService hostLbExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("HostLB-Executor"));
955+
hostLbExecutor.schedule(new PreferredHostCheckerTask(), 0, TimeUnit.MILLISECONDS);
956+
hostLbExecutor.shutdown();
948957
}
958+
scheduleHostLBCheckerTask(lbAlgorithm, shell.getLbCheckerInterval(lbCheckInterval));
949959
}
950960

951961
private Answer setupManagementServerList(final SetupMSListCommand cmd) {
952-
processManagementServerList(cmd.getMsList(), cmd.getAvoidMsList(), cmd.getLbAlgorithm(), cmd.getLbCheckInterval());
962+
processManagementServerList(cmd.getMsList(), cmd.getAvoidMsList(), cmd.getLbAlgorithm(), cmd.getLbCheckInterval(), cmd.getTriggerHostLb());
953963
return new SetupMSListAnswer(true);
954964
}
955965

956966
private Answer migrateAgentToOtherMS(final MigrateAgentConnectionCommand cmd) {
957967
try {
958968
if (CollectionUtils.isNotEmpty(cmd.getMsList())) {
959-
processManagementServerList(cmd.getMsList(), cmd.getAvoidMsList(), cmd.getLbAlgorithm(), cmd.getLbCheckInterval());
969+
processManagementServerList(cmd.getMsList(), cmd.getAvoidMsList(), cmd.getLbAlgorithm(), cmd.getLbCheckInterval(), false);
960970
}
961971
Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("MigrateAgentConnection-Job")).schedule(() -> {
962972
migrateAgentConnection(cmd.getAvoidMsList());
@@ -1046,7 +1056,7 @@ public void processReadyCommand(final Command cmd) {
10461056
}
10471057

10481058
verifyAgentArch(ready.getArch());
1049-
processManagementServerList(ready.getMsHostList(), ready.getAvoidMsHostList(), ready.getLbAlgorithm(), ready.getLbCheckInterval());
1059+
processManagementServerList(ready.getMsHostList(), ready.getAvoidMsHostList(), ready.getLbAlgorithm(), ready.getLbCheckInterval(), false);
10501060

10511061
logger.info("Ready command is processed for agent [id: {}, uuid: {}, name: {}]", getId(), getUuid(), getName());
10521062
}

api/src/main/java/com/cloud/exception/OperationTimedoutException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class OperationTimedoutException extends CloudException {
4040
boolean _isActive;
4141

4242
public OperationTimedoutException(Command[] cmds, long agentId, long seqId, int time, boolean isActive) {
43-
super("Commands " + seqId + " to Host " + agentId + " timed out after " + time);
43+
super("Commands " + seqId + " to Host " + agentId + " timed out after " + time + " secs");
4444
_agentId = agentId;
4545
_seqId = seqId;
4646
_time = time;

api/src/main/java/com/cloud/host/Host.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ public static String[] toStrings(Host.Type... types) {
5353
return strs;
5454
}
5555
}
56-
public static final String HOST_UEFI_ENABLE = "host.uefi.enable";
57-
public static final String HOST_VOLUME_ENCRYPTION = "host.volume.encryption";
58-
public static final String HOST_INSTANCE_CONVERSION = "host.instance.conversion";
56+
57+
String HOST_UEFI_ENABLE = "host.uefi.enable";
58+
String HOST_VOLUME_ENCRYPTION = "host.volume.encryption";
59+
String HOST_INSTANCE_CONVERSION = "host.instance.conversion";
60+
String HOST_OVFTOOL_VERSION = "host.ovftool.version";
61+
String HOST_VIRTV2V_VERSION = "host.virtv2v.version";
5962

6063
/**
6164
* @return name of the machine.

api/src/main/java/com/cloud/resource/ResourceState.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public static Event toEvent(String e) {
7676
}
7777
}
7878

79+
public static List<ResourceState> s_maintenanceStates = List.of(ResourceState.Maintenance,
80+
ResourceState.ErrorInMaintenance, ResourceState.PrepareForMaintenance,
81+
ResourceState.ErrorInPrepareForMaintenance);
82+
7983
public ResourceState getNextState(Event a) {
8084
return s_fsm.getNextState(this, a);
8185
}
@@ -98,8 +102,7 @@ public static String[] toString(ResourceState... states) {
98102
}
99103

100104
public static boolean isMaintenanceState(ResourceState state) {
101-
return Arrays.asList(ResourceState.Maintenance, ResourceState.ErrorInMaintenance,
102-
ResourceState.PrepareForMaintenance, ResourceState.ErrorInPrepareForMaintenance).contains(state);
105+
return s_maintenanceStates.contains(state);
103106
}
104107

105108
public static boolean canAttemptMaintenance(ResourceState state) {

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ public class ApiConstants {
454454
public static final String PUBLIC_END_PORT = "publicendport";
455455
public static final String PUBLIC_ZONE = "publiczone";
456456
public static final String PURGE_RESOURCES = "purgeresources";
457+
public static final String REBALANCE = "rebalance";
457458
public static final String RECEIVED_BYTES = "receivedbytes";
458459
public static final String RECONNECT = "reconnect";
459460
public static final String RECOVER = "recover";

api/src/main/java/org/apache/cloudstack/api/command/admin/systemvm/PatchSystemVMCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class PatchSystemVMCmd extends BaseAsyncCmd {
4646
@Parameter(name = ApiConstants.FORCED, type = CommandType.BOOLEAN,
4747
description = "If true, initiates copy of scripts and restart of the agent, even if the scripts version matches." +
4848
"To be used with ID parameter only")
49-
private Boolean force;
49+
private Boolean forced;
5050

5151
/////////////////////////////////////////////////////
5252
/////////////////// Accessors ///////////////////////
@@ -58,7 +58,7 @@ public Long getId() {
5858
}
5959

6060
public boolean isForced() {
61-
return force != null && force;
61+
return forced != null && forced;
6262
}
6363

6464
/////////////////////////////////////////////////////

core/src/main/java/org/apache/cloudstack/agent/lb/SetupMSListCommand.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ public class SetupMSListCommand extends Command {
2929
private List<String> avoidMsList;
3030
private String lbAlgorithm;
3131
private Long lbCheckInterval;
32+
private Boolean triggerHostLb;
3233

33-
public SetupMSListCommand(final List<String> msList, final List<String> avoidMsList, final String lbAlgorithm, final Long lbCheckInterval) {
34+
public SetupMSListCommand(final List<String> msList, final List<String> avoidMsList, final String lbAlgorithm, final Long lbCheckInterval, final Boolean triggerHostLb) {
3435
super();
3536
this.msList = msList;
3637
this.avoidMsList = avoidMsList;
3738
this.lbAlgorithm = lbAlgorithm;
3839
this.lbCheckInterval = lbCheckInterval;
40+
this.triggerHostLb = triggerHostLb;
3941
}
4042

4143
public List<String> getMsList() {
@@ -54,9 +56,12 @@ public Long getLbCheckInterval() {
5456
return lbCheckInterval;
5557
}
5658

59+
public boolean getTriggerHostLb() {
60+
return triggerHostLb;
61+
}
62+
5763
@Override
5864
public boolean executeInSequence() {
5965
return false;
6066
}
61-
6267
}

engine/components-api/src/main/java/com/cloud/agent/AgentManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,5 @@ enum TapAgentsAction {
171171

172172
void propagateChangeToAgents(Map<String, String> params);
173173

174-
boolean transferDirectAgentsFromMS(String fromMsUuid, long fromMsId, long timeoutDurationInMs);
174+
boolean transferDirectAgentsFromMS(String fromMsUuid, long fromMsId, long timeoutDurationInMs, boolean excludeHostsInMaintenance);
175175
}

engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
6363
import org.apache.commons.collections.MapUtils;
6464
import org.apache.commons.lang3.BooleanUtils;
65+
import org.apache.commons.lang3.ObjectUtils;
6566
import org.apache.commons.lang3.StringUtils;
6667
import org.apache.logging.log4j.ThreadContext;
6768

@@ -273,8 +274,6 @@ public boolean configure(final String name, final Map<String, Object> params) th
273274

274275
_executor = new ThreadPoolExecutor(agentTaskThreads, agentTaskThreads, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new NamedThreadFactory("AgentTaskPool"));
275276

276-
initConnectExecutor();
277-
278277
maxConcurrentNewAgentConnections = RemoteAgentMaxConcurrentNewConnections.value();
279278

280279
_connection = new NioServer("AgentManager", Port.value(), Workers.value() + 10,
@@ -803,11 +802,25 @@ protected AgentAttache notifyMonitorsOfConnection(final AgentAttache attache, fi
803802
Map<String, String> detailsMap = readyAnswer.getDetailsMap();
804803
if (detailsMap != null) {
805804
String uefiEnabled = detailsMap.get(Host.HOST_UEFI_ENABLE);
805+
String virtv2vVersion = detailsMap.get(Host.HOST_VIRTV2V_VERSION);
806+
String ovftoolVersion = detailsMap.get(Host.HOST_OVFTOOL_VERSION);
806807
logger.debug("Got HOST_UEFI_ENABLE [{}] for host [{}]:", uefiEnabled, host);
807-
if (uefiEnabled != null) {
808+
if (ObjectUtils.anyNotNull(uefiEnabled, virtv2vVersion, ovftoolVersion)) {
808809
_hostDao.loadDetails(host);
810+
boolean updateNeeded = false;
809811
if (!uefiEnabled.equals(host.getDetails().get(Host.HOST_UEFI_ENABLE))) {
810812
host.getDetails().put(Host.HOST_UEFI_ENABLE, uefiEnabled);
813+
updateNeeded = true;
814+
}
815+
if (StringUtils.isNotBlank(virtv2vVersion) && !virtv2vVersion.equals(host.getDetails().get(Host.HOST_VIRTV2V_VERSION))) {
816+
host.getDetails().put(Host.HOST_VIRTV2V_VERSION, virtv2vVersion);
817+
updateNeeded = true;
818+
}
819+
if (StringUtils.isNotBlank(ovftoolVersion) && !ovftoolVersion.equals(host.getDetails().get(Host.HOST_OVFTOOL_VERSION))) {
820+
host.getDetails().put(Host.HOST_OVFTOOL_VERSION, ovftoolVersion);
821+
updateNeeded = true;
822+
}
823+
if (updateNeeded) {
811824
_hostDao.saveDetails(host);
812825
}
813826
}
@@ -828,6 +841,7 @@ public boolean start() {
828841
return true;
829842
}
830843

844+
initConnectExecutor();
831845
startDirectlyConnectedHosts(false);
832846

833847
if (_connection != null) {
@@ -2193,7 +2207,7 @@ public void propagateChangeToAgents(Map<String, String> params) {
21932207
}
21942208

21952209
@Override
2196-
public boolean transferDirectAgentsFromMS(String fromMsUuid, long fromMsId, long timeoutDurationInMs) {
2210+
public boolean transferDirectAgentsFromMS(String fromMsUuid, long fromMsId, long timeoutDurationInMs, boolean excludeHostsInMaintenance) {
21972211
return true;
21982212
}
21992213

engine/orchestration/src/main/java/com/cloud/agent/manager/ClusteredAgentManagerImpl.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import javax.net.ssl.SSLContext;
4343
import javax.net.ssl.SSLEngine;
4444

45+
import com.cloud.resource.ResourceState;
4546
import org.apache.cloudstack.ca.CAManager;
4647
import org.apache.cloudstack.framework.config.ConfigDepot;
4748
import org.apache.cloudstack.framework.config.ConfigKey;
@@ -434,10 +435,10 @@ public boolean routeToPeer(final String peer, final byte[] bytes) {
434435
ch = connectToPeer(peer, ch);
435436
if (ch == null) {
436437
try {
437-
logD(bytes, "Unable to route to peer: " + Request.parse(bytes));
438+
logD(bytes, "Unable to establish connection to route to peer: " + Request.parse(bytes));
438439
} catch (ClassNotFoundException | UnsupportedVersionException e) {
439440
// Request.parse thrown exception when we try to log it, log as much as we can
440-
logD(bytes, "Unable to route to peer, and Request.parse further caught exception" + e.getMessage());
441+
logD(bytes, "Unable to establish connection to route to peer, and Request.parse further caught exception" + e.getMessage());
441442
}
442443
return false;
443444
}
@@ -646,7 +647,6 @@ protected void doTask(final Task task) throws TaskExecutionException {
646647
final Link link = task.getLink();
647648

648649
if (Request.fromServer(data)) {
649-
650650
final AgentAttache agent = findAttache(hostId);
651651

652652
if (Request.isControl(data)) {
@@ -694,7 +694,6 @@ protected void doTask(final Task task) throws TaskExecutionException {
694694
cancel(Long.toString(Request.getManagementServerId(data)), hostId, Request.getSequence(data), e.getMessage());
695695
}
696696
} else {
697-
698697
final long mgmtId = Request.getManagementServerId(data);
699698
if (mgmtId != -1 && mgmtId != _nodeId) {
700699
routeToPeer(Long.toString(mgmtId), data);
@@ -1357,7 +1356,7 @@ private String handleShutdownManagementServerHostCommand(BaseShutdownManagementS
13571356
if (cmd instanceof PrepareForMaintenanceManagementServerHostCommand) {
13581357
logger.debug("Received PrepareForMaintenanceManagementServerHostCommand - preparing for maintenance");
13591358
try {
1360-
managementServerMaintenanceManager.prepareForMaintenance(((PrepareForMaintenanceManagementServerHostCommand) cmd).getLbAlgorithm());
1359+
managementServerMaintenanceManager.prepareForMaintenance(((PrepareForMaintenanceManagementServerHostCommand) cmd).getLbAlgorithm(), ((PrepareForMaintenanceManagementServerHostCommand) cmd).isForced());
13611360
return "Successfully prepared for maintenance";
13621361
} catch(CloudRuntimeException e) {
13631362
return e.getMessage();
@@ -1404,14 +1403,14 @@ private String handleShutdownManagementServerHostCommand(BaseShutdownManagementS
14041403
}
14051404

14061405
@Override
1407-
public boolean transferDirectAgentsFromMS(String fromMsUuid, long fromMsId, long timeoutDurationInMs) {
1406+
public boolean transferDirectAgentsFromMS(String fromMsUuid, long fromMsId, long timeoutDurationInMs, boolean excludeHostsInMaintenance) {
14081407
if (timeoutDurationInMs <= 0) {
14091408
logger.debug("Not transferring direct agents from management server node {} (id: {}) to other nodes, invalid timeout duration", fromMsId, fromMsUuid);
14101409
return false;
14111410
}
14121411

14131412
long transferStartTimeInMs = System.currentTimeMillis();
1414-
if (CollectionUtils.isEmpty(getDirectAgentHosts(fromMsId))) {
1413+
if (CollectionUtils.isEmpty(getDirectAgentHosts(fromMsId, excludeHostsInMaintenance))) {
14151414
logger.info("No direct agent hosts available on management server node {} (id: {}), to transfer", fromMsId, fromMsUuid);
14161415
return true;
14171416
}
@@ -1426,7 +1425,7 @@ public boolean transferDirectAgentsFromMS(String fromMsUuid, long fromMsId, long
14261425
int agentTransferFailedCount = 0;
14271426
List<DataCenterVO> dataCenterList = dcDao.listAll();
14281427
for (DataCenterVO dc : dataCenterList) {
1429-
List<HostVO> directAgentHostsInDc = getDirectAgentHostsInDc(fromMsId, dc.getId());
1428+
List<HostVO> directAgentHostsInDc = getDirectAgentHostsInDc(fromMsId, dc.getId(), excludeHostsInMaintenance);
14301429
if (CollectionUtils.isEmpty(directAgentHostsInDc)) {
14311430
continue;
14321431
}
@@ -1460,9 +1459,10 @@ public boolean transferDirectAgentsFromMS(String fromMsUuid, long fromMsId, long
14601459
return (agentTransferFailedCount == 0);
14611460
}
14621461

1463-
private List<HostVO> getDirectAgentHosts(long msId) {
1462+
private List<HostVO> getDirectAgentHosts(long msId, boolean excludeHostsInMaintenance) {
14641463
List<HostVO> directAgentHosts = new ArrayList<>();
1465-
List<HostVO> hosts = _hostDao.listHostsByMs(msId);
1464+
List<ResourceState> statesToExclude = excludeHostsInMaintenance ? ResourceState.s_maintenanceStates : List.of();
1465+
List<HostVO> hosts = _hostDao.listHostsByMsResourceState(msId, statesToExclude);
14661466
for (HostVO host : hosts) {
14671467
AgentAttache agent = findAttache(host.getId());
14681468
if (agent instanceof DirectAgentAttache) {
@@ -1473,9 +1473,11 @@ private List<HostVO> getDirectAgentHosts(long msId) {
14731473
return directAgentHosts;
14741474
}
14751475

1476-
private List<HostVO> getDirectAgentHostsInDc(long msId, long dcId) {
1476+
private List<HostVO> getDirectAgentHostsInDc(long msId, long dcId, boolean excludeHostsInMaintenance) {
14771477
List<HostVO> directAgentHosts = new ArrayList<>();
1478-
List<HostVO> hosts = _hostDao.listHostsByMsAndDc(msId, dcId);
1478+
// To exclude maintenance states use values from ResourceState as source of truth
1479+
List<ResourceState> statesToExclude = excludeHostsInMaintenance ? ResourceState.s_maintenanceStates : List.of();
1480+
List<HostVO> hosts = _hostDao.listHostsByMsDcResourceState(msId, dcId, statesToExclude);
14791481
for (HostVO host : hosts) {
14801482
AgentAttache agent = findAttache(host.getId());
14811483
if (agent instanceof DirectAgentAttache) {
@@ -1511,6 +1513,10 @@ public void onManagementServerPreparingForMaintenance() {
15111513
public void onManagementServerCancelPreparingForMaintenance() {
15121514
logger.debug("Management server cancel preparing for maintenance");
15131515
super.onManagementServerPreparingForMaintenance();
1516+
1517+
// needed for the case when Management Server in Preparing For Maintenance but didn't go to Maintenance state
1518+
// (where this variable will be reset)
1519+
_agentLbHappened = false;
15141520
}
15151521

15161522
@Override

0 commit comments

Comments
 (0)