Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions agent/src/main/java/com/cloud/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,15 @@ protected void reconnect(final Link link, String preferredMSHost, boolean forTra
logger.info("Connected to the host: {}", shell.getConnectedHost());
}

protected void waitForCommandsAndReconnect(final Link link) {
int inProgress = commandsInProgress.get();
if (inProgress > 0) {
logger.warn("Agent disconnected with {} command(s) still in progress. Reconnecting — local operations (e.g. blockcommit) will continue in their threads.",
inProgress);
}
reconnect(link);
}

protected void closeAndTerminateLink(final Link link) {
if (link == null) {
return;
Expand Down Expand Up @@ -700,7 +709,7 @@ public void processStartupAnswer(final Answer answer, final Response response, f
reconnect(link);
return;
}
System.exit(1);
waitForCommandsAndReconnect(link);
}
if (!answerValid) {
logger.warn("Threw away a startup answer because we're reconnecting.");
Expand Down Expand Up @@ -772,7 +781,7 @@ protected void processRequest(final Request request, final Link link) {
reconnect(link);
return;
}
System.exit(1);
waitForCommandsAndReconnect(link);
return;
} else if (cmd instanceof MaintainCommand) {
logger.debug("Received maintainCommand, do not cancel current tasks");
Expand Down
30 changes: 30 additions & 0 deletions agent/src/test/java/com/cloud/agent/AgentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -254,4 +256,32 @@ public void testStopAndCleanupConnectionValidConnectionWaitForStopWaitsForStartu
verify(mockConnection).cleanUp();
verify(mockBackoff, times(3)).waitBeforeRetry();
}

@Test
public void testWaitForCommandsAndReconnectNoCommandsInProgress() throws ConfigurationException {
Agent spyAgent = spy(new Agent(shell, 1, serverResource));
ReflectionTestUtils.setField(spyAgent, "logger", logger);
doNothing().when(spyAgent).reconnect(any(Link.class));

Link mockLink = mock(Link.class);
spyAgent.commandsInProgress.set(0);
spyAgent.waitForCommandsAndReconnect(mockLink);

verify(spyAgent).reconnect(mockLink);
}

@Test
public void testWaitForCommandsAndReconnectWithCommandsInProgress() throws ConfigurationException {
Agent spyAgent = spy(new Agent(shell, 1, serverResource));
ReflectionTestUtils.setField(spyAgent, "logger", logger);
doNothing().when(spyAgent).reconnect(any(Link.class));

Link mockLink = mock(Link.class);
spyAgent.commandsInProgress.set(3);
spyAgent.waitForCommandsAndReconnect(mockLink);

verify(spyAgent).reconnect(mockLink);
verify(logger).warn(eq("Agent disconnected with {} command(s) still in progress. Reconnecting — local operations (e.g. blockcommit) will continue in their threads."),
eq(3));
}
}
Loading