Skip to content

Indirect agent connection improvements - #13345

Draft
sureshanaparti wants to merge 16 commits into
apache:mainfrom
shapeblue:indirect-agent-connection-improvements
Draft

sureshanaparti wants to merge 16 commits into
apache:mainfrom
shapeblue:indirect-agent-connection-improvements

Conversation

@sureshanaparti

@sureshanaparti sureshanaparti commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

Continued from #13028

This PR improves the Indirect agent connection handling, has the following improvements.

  • Enhances the Host connecting logic to avoid connecting storm (where Agent opens multiple sockets against Management Server).
  • Implements HostConnectProcess task where Host upon connection checks whether lock is available, traces Host connecting progress, status and timeout.
  • Introduces AgentConnectStatusCommand, where Host checks whether lock for the Host is available (i.e. "previous" connect process is finished).
  • Implementes logic to check whether Management Server has lock against Host (exposed MySQL DB lock presence via API)
  • Removes synchronization on Host disconnect process, double-disconnect logic in clustered Management Server environment, added early removal from ping map (in case of combination ping timeout delay + synchronized disconnect process the Agent Manager submits more disconnect requests)
  • Introduces parameterized connection and status check timeouts
  • Implements backoff algorithm abstraction - can be used either constant backoff timeout or exponential with jitter to wait between connection Host attempts to Management Server
  • Implements ServerAttache to be used on the Agent side of communication (similar to Attache on Management Server side)
  • Enhances/Adds logs significantly to Host Agent and Agent Manager logic to trace Host connecting and disconnecting process, including ids, names, context UUIDs and timings (how much time took overall initialization/deinitialization)
  • Adds logs to communication between Management Servers (PDU requests)
  • Adds DB indexes to improve search performance, uses IDEMPOTENT_ADD_INDEX for safer DB schema updates
  • Dedupes agent connection requests. Observed scenarios where on agent with old agent code send a storm of connection requests. In such a case, to ensure other agents are not impacted, deduping the connection requests.
  • Remove IP address hard dependency for management server communication. It allows the management.server.address config to be a list of hostnames instead of static IPs. Rebalancing, cluster propagation, alerts, and stats collection all detect the format at runtime via ManagementServerAddressUtil and use the matching lookup (listNonUpStateMsHostnames vs listNonUpStateMsIPs) when computing the "avoid" list sent to agents. Hostnames survive MS restarts and IP reassignments where static IPs do not, which is required in deployment environments where management server instances can be rescheduled or replaced.
  • Propagates instance (vm, volume, etc) uuid to all operations. The uuid is propagated from MS to agent communications and to all async jobs.

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

Checked the indirect agents connections (and re-connections) with KVM hosts, SSVM & CPVM.

How did you try to break this feature and the system with this change?

@sureshanaparti

Copy link
Copy Markdown
Contributor Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@sureshanaparti a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

@sureshanaparti sureshanaparti added this to the 4.23.0 milestone Jun 4, 2026
@sureshanaparti
sureshanaparti requested a review from nvazquez June 4, 2026 06:29
@codecov

codecov Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 27.11592% with 1817 lines in your changes missing coverage. Please review.
✅ Project coverage is 20.75%. Comparing base (4dae995) to head (92a0c48).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
agent/src/main/java/com/cloud/agent/Agent.java 15.48% 309 Missing and 2 partials ⚠️
...java/com/cloud/agent/manager/AgentManagerImpl.java 42.11% 270 Missing and 31 partials ⚠️
...t/src/main/java/com/cloud/agent/ServerAttache.java 8.19% 277 Missing and 3 partials ⚠️
.../main/java/com/cloud/agent/HostConnectProcess.java 25.23% 159 Missing and 1 partial ⚠️
...c/main/java/com/cloud/utils/nio/NioConnection.java 25.92% 87 Missing and 13 partials ⚠️
...s/src/main/java/com/cloud/utils/nio/NioClient.java 18.36% 79 Missing and 1 partial ⚠️
...b/src/main/java/com/cloud/utils/db/GlobalLock.java 15.66% 51 Missing and 19 partials ⚠️
...main/java/com/cloud/agent/SynchronousListener.java 0.00% 47 Missing ⚠️
...he/cloudstack/threadcontext/ThreadContextUtil.java 9.52% 36 Missing and 2 partials ⚠️
...cloud/agent/manager/ClusteredAgentManagerImpl.java 18.60% 35 Missing ⚠️
... and 33 more
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #13345      +/-   ##
============================================
+ Coverage     20.67%   20.75%   +0.07%     
- Complexity    21727    21884     +157     
============================================
  Files          6492     6505      +13     
  Lines        590737   592595    +1858     
  Branches      72448    72665     +217     
============================================
+ Hits         122129   122976     +847     
- Misses       455335   456259     +924     
- Partials      13273    13360      +87     
Flag Coverage Δ
uitests 3.88% <ø> (ø)
unittests 22.05% <27.11%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR is a broad refactor/enhancement of indirect agent connection handling across both Management Server and Agent sides. It introduces a host-side connect/status-check process, adds configurable backoff and host-status heuristics, and expands logging/lock-check mechanisms to reduce reconnect storms and improve observability.

Changes:

  • Adds agent-side connection orchestration (status polling + startup submission) and a server-side AgentConnectStatus* command/answer pair to coordinate connect progress.
  • Introduces configurable backoff (factory + exponential-with-jitter implementation) and propagates configuration from Management Server to Agent during startup.
  • Refactors NIO connection lifecycle and adds supporting utilities/logging, plus DB lock availability checks and DB indexes for performance.

Reviewed changes

Copilot reviewed 47 out of 47 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
utils/src/test/java/com/cloud/utils/testcase/NioTest.java Test logging cleanup (exception logging).
utils/src/test/java/com/cloud/utils/backoff/impl/ConstantTimeBackoffTest.java Updates config key name for constant backoff.
utils/src/main/java/com/cloud/utils/nio/NioServer.java NIO server init tweaks and docstring.
utils/src/main/java/com/cloud/utils/nio/NioConnection.java Connection lifecycle refactor, selector loop/logging, reject logic.
utils/src/main/java/com/cloud/utils/nio/NioClient.java Client connect/handshake logging and expanded cleanup.
utils/src/main/java/com/cloud/utils/nio/Link.java Adds local-port tracking, richer toString, termination helpers/logging changes.
utils/src/main/java/com/cloud/utils/nio/HandlerFactory.java Changes new-connection registration API to InetSocketAddress.
utils/src/main/java/com/cloud/utils/net/NetUtils.java Null guard in hostname-to-IP resolution helper.
utils/src/main/java/com/cloud/utils/LogUtils.java Adds host logging helper with optional reverse lookup.
utils/src/main/java/com/cloud/utils/exception/CSExceptionErrorCode.java Adds error-code mapping for ConnectionException.
utils/src/main/java/com/cloud/utils/DateUtil.java Adds formatMillis duration formatter.
utils/src/main/java/com/cloud/utils/backoff/impl/ExponentialWithJitterBackoffMBean.java New MBean interface for exponential-jitter backoff.
utils/src/main/java/com/cloud/utils/backoff/impl/ExponentialWithJitterBackoff.java New exponential-with-jitter backoff implementation.
utils/src/main/java/com/cloud/utils/backoff/impl/ConstantTimeBackoff.java Migrates to namespaced config keys + exposes configuration.
utils/src/main/java/com/cloud/utils/backoff/BackoffFactory.java New backoff factory for selecting/configuring algorithms.
utils/src/main/java/com/cloud/utils/backoff/BackoffAlgorithm.java Adds getConfiguration() API for propagating settings.
server/src/main/java/org/apache/cloudstack/agent/lb/IndirectAgentLBServiceImpl.java Adds notes/timeout tweaks around agent migration command dispatch.
framework/db/src/main/java/com/cloud/utils/db/GlobalLock.java Refactors lock bookkeeping/logging and adds lock-availability query.
framework/db/src/main/java/com/cloud/utils/db/DbUtil.java Adds IS_FREE_LOCK helper and improves logging around lock DB connections.
framework/config/src/main/java/org/apache/cloudstack/framework/config/ConfigKeyUtil.java New utility to parse key=value;... configuration strings.
framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java Adds DAO method to list all MS hosts including removed.
framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDao.java Adds DAO API for “including removed” listing.
framework/cluster/src/main/java/com/cloud/cluster/ClusterServiceServletImpl.java Adds request logging and more detailed RemoteException messages.
framework/cluster/src/main/java/com/cloud/cluster/ClusterServiceServletHttpHandler.java Adds debug logging of inbound request line/body.
engine/schema/src/main/resources/META-INF/db/schema-42210to42300.sql Adds indexes via idempotent add-index procedure calls.
engine/schema/src/main/resources/META-INF/db/procedures/cloud.idempotent_add_index.sql Adds/defines IDEMPOTENT_ADD_INDEX procedure.
engine/orchestration/src/test/java/com/cloud/agent/manager/ClusteredAgentManagerImplTest.java Expands tests for disconnect broadcast behavior and lock usage.
engine/orchestration/src/test/java/com/cloud/agent/manager/AgentManagerImplTest.java Adds tests for config keys and deregister/disconnect behaviors.
engine/orchestration/src/main/java/com/cloud/agent/manager/ClusteredAgentManagerImpl.java Refactors attache creation/removal and event handling behavior.
engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java Major connect/disconnect refactor, new config keys, status checks, backoff propagation.
engine/orchestration/src/main/java/com/cloud/agent/manager/AgentAttache.java Cancels listener alarm futures on unregister/cancel paths.
core/src/main/java/org/apache/cloudstack/threadcontext/ThreadContextUtil.java New helper for propagating Log4j ThreadContext across threads.
core/src/main/java/com/cloud/resource/ServerResource.java Changes default isExitOnFailures() behavior.
core/src/main/java/com/cloud/agent/transport/Request.java Improves JSON deserialization error logging.
core/src/main/java/com/cloud/agent/api/StartupAnswer.java Adds params + agent-side status-check delay transport fields.
core/src/main/java/com/cloud/agent/api/AgentConnectStatusCommand.java New command for host connect-status checks.
core/src/main/java/com/cloud/agent/api/AgentConnectStatusAnswer.java New answer carrying lock availability and host status.
agent/src/test/java/com/cloud/agent/HostConnectProcessTest.java New test around scheduling the agent connect process.
agent/src/test/java/com/cloud/agent/AgentTest.java Updates tests for new link/logging and reconnect helpers.
agent/src/main/java/com/cloud/agent/SynchronousListener.java New blocking listener for synchronous waits on answers.
agent/src/main/java/com/cloud/agent/ServerListener.java New listener interface for agent-side ServerAttache callbacks.
agent/src/main/java/com/cloud/agent/ServerAttache.java New agent-side counterpart to MS Attache for command/answer flow.
agent/src/main/java/com/cloud/agent/properties/AgentProperties.java Adds new agent properties for async timeouts and status-check delay.
agent/src/main/java/com/cloud/agent/IAgentShell.java Adds setter for dynamically updated backoff algorithm.
agent/src/main/java/com/cloud/agent/HostConnectProcess.java New connect/status-check orchestration loop on the agent side.
agent/src/main/java/com/cloud/agent/AgentShell.java Uses backoff factory + persists configuration; relaxes version handling.
agent/src/main/java/com/cloud/agent/Agent.java Major reconnect/startup refactor; integrates HostConnectProcess and ServerAttache.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java Outdated
Comment thread engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java Outdated
Comment thread engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java Outdated
Comment thread utils/src/main/java/com/cloud/utils/nio/Link.java
Comment thread utils/src/main/java/com/cloud/utils/nio/NioConnection.java
Comment thread core/src/main/java/com/cloud/resource/ServerResource.java
@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 18146

@sureshanaparti

Copy link
Copy Markdown
Contributor Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@sureshanaparti a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 47 out of 47 changed files in this pull request and generated 7 comments.

Comment thread engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java Outdated
Comment thread framework/db/src/main/java/com/cloud/utils/db/GlobalLock.java
Comment thread agent/src/main/java/com/cloud/agent/Agent.java Outdated
Comment thread agent/src/test/java/com/cloud/agent/HostConnectProcessTest.java
Comment thread agent/src/main/java/com/cloud/agent/HostConnectProcess.java Outdated
Comment thread utils/src/main/java/com/cloud/utils/LogUtils.java
@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 18149

@sureshanaparti

Copy link
Copy Markdown
Contributor Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@sureshanaparti a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 18154

Comment on lines +19 to +23
/**
* Command to check status of {@link StartupCommand} from the Agent.
*
* @author mprokopchuk
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These @author fields are not necessary, right?

Suggested change
/**
* Command to check status of {@link StartupCommand} from the Agent.
*
* @author mprokopchuk
*/
/**
* Command to check status of {@link StartupCommand} from the Agent.
*
*/

@sureshanaparti sureshanaparti Jun 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bernardodemarco I think, it's ok to have it. we've author mentioned in few other classes as well. (check with '@author' in the code base).

@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

This pull request has merge conflicts. Dear author, please fix the conflicts and sync your branch with the base branch.

@sureshanaparti
sureshanaparti force-pushed the indirect-agent-connection-improvements branch from d5a6d20 to 22e832c Compare June 8, 2026 13:18
@nvazquez

Copy link
Copy Markdown
Contributor

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@nvazquez a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

} catch (UnknownHostException e) {
logger.debug("Unable to resolve configured management server address {}", hostname);
}
resolutionCache.put(hostname, new ResolvedAddress(ip, now));
Comment thread utils/src/main/java/com/cloud/utils/nio/NioConnection.java Outdated
@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 19714

@nvazquez

Copy link
Copy Markdown
Contributor

@blueorangutan test

@blueorangutan

Copy link
Copy Markdown

@nvazquez a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

Copilot AI lite review requested due to automatic review settings October 28, 2026 08:40
@sureshanaparti

Copy link
Copy Markdown
Contributor Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@sureshanaparti a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sureshanaparti
sureshanaparti force-pushed the indirect-agent-connection-improvements branch from debdd10 to 64df4e3 Compare October 28, 2026 09:14
Copilot AI lite review requested due to automatic review settings October 28, 2026 09:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +65 to +75
private ScheduledExecutorService hostStatusExecutor;

public HostConnectProcess(Agent agent) {
this.agent = agent;
initExecutors();
}

private void initExecutors() {
stop();
var threadFactory = new NamedThreadFactory("Agent-" + HostStatusTask.class.getSimpleName());
hostStatusExecutor = Executors.newScheduledThreadPool(1, threadFactory);
Comment thread utils/src/main/java/com/cloud/utils/backoff/BackoffAlgorithm.java Outdated
Comment thread agent/src/main/java/com/cloud/agent/SynchronousListener.java Outdated
@sureshanaparti
sureshanaparti force-pushed the indirect-agent-connection-improvements branch from 64df4e3 to 5a5b922 Compare October 28, 2026 09:55
Copilot AI lite review requested due to automatic review settings October 28, 2026 09:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Comment on lines +102 to +111
public void scheduleConnectProcess(Link link, boolean connectionTransfer) {
logger.debug("Scheduling connect process for {}", link);
initExecutors();

var task = new HostStatusTask(link, connectionTransfer, agent);
var future = hostStatusExecutor.scheduleWithFixedDelay(ThreadContextUtil.wrapThreadContext(task),
HOST_STATUS_CHECK_INITIAL_DELAY_SEC,
hostStatusCheckDelaySec, TimeUnit.SECONDS);
task.setFuture(future);
hostStatusFutureRef.set(future);
@sureshanaparti
sureshanaparti marked this pull request as draft October 28, 2026 10:24
@sureshanaparti
sureshanaparti force-pushed the indirect-agent-connection-improvements branch from 5a5b922 to 92a0c48 Compare October 28, 2026 10:29
@sureshanaparti

Copy link
Copy Markdown
Contributor Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@sureshanaparti a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 19798

@sureshanaparti

Copy link
Copy Markdown
Contributor Author

@blueorangutan test keepEnv

@blueorangutan

Copy link
Copy Markdown

@sureshanaparti a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@blueorangutan

Copy link
Copy Markdown

[SF] Trillian test result (tid-17310)
Environment: kvm-ol8 (x2), zone: Advanced Networking with Mgmt server ol8
Total time taken: 59401 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr13345-t17310-kvm-ol8.zip
Smoke tests completed. 155 look OK, 2 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File
test_uservm_host_control_state Failure 17.11 test_host_control_state.py
test_hostha_enable_ha_when_host_disabled Error 3.09 test_hostha_kvm.py
test_hostha_enable_ha_when_host_in_maintenance Error 303.23 test_hostha_kvm.py

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.