Skip to content

Commit

Permalink
CLOUD-1583: thread for controller check and api call
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Sep 29, 2024
1 parent 3eb9e0d commit 65d0856
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
24 changes: 24 additions & 0 deletions server/src/main/java/com/veertu/AgentParametersProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.veertu;

import org.jetbrains.annotations.NotNull;

import com.intellij.openapi.diagnostic.Logger;

import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.serverSide.ServerExtension;
import jetbrains.buildServer.serverSide.agentTypes.AgentTypeKey;

public class AgentParametersProvider implements ServerExtension {

private static final Logger LOG = Logger.getInstance(Loggers.CLOUD_CATEGORY_ROOT);

public AgentParametersProvider() {
LOG.info("AgentParametersProvider initialized");
}

public boolean disableUpdateFromAgent(@NotNull AgentTypeKey agentTypeKey) {
LOG.info("disableUpdateFromAgent called with agentTypeKey: " + agentTypeKey);
return false;
}

}
33 changes: 31 additions & 2 deletions server/src/main/java/com/veertu/AnkaCloudClientEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -35,6 +36,8 @@ public class AnkaCloudClientEx implements CloudClientEx {
private final int maxInstances;
private final BuildAgentManagerEx buildAgentManager;
private final ConcurrentHashMap<String, AnkaCloudImage> imagesMap;
private final AtomicBoolean isControllerRunning = new AtomicBoolean(false);
private final AtomicBoolean controllerStatusCheckThread = new AtomicBoolean(true);

public AnkaCloudClientEx(
AnkaCloudConnector connector,
Expand All @@ -53,6 +56,7 @@ public AnkaCloudClientEx(
}
LOG.info(String.format("Registering AnkaCloudClientEx %s to updater", this.toString()));
updater.registerClient(this);
controllerStatusThread();
}

@NotNull
Expand Down Expand Up @@ -85,12 +89,37 @@ public void terminateInstance(@NotNull CloudInstance cloudInstance) {
public void dispose() {
LOG.info(String.format("Disposing AnkaCloudClientEx %s", this.toString()));
updater.unRegisterClient(this);
controllerStatusCheckThread.set(false);
}

private void controllerStatusThread() {
final int sleepTime = 1000;
final int checkInterval = 5000; // 5 seconds
final long[] lastCheckTime = {0};
Thread initializationThread = new Thread(() -> {
while (controllerStatusCheckThread.get()) {
long currentTime = System.currentTimeMillis();
if (currentTime - lastCheckTime[0] >= checkInterval) {
isControllerRunning.set(connector.isRunning());
lastCheckTime[0] = currentTime;
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error(String.format("Initialization thread interrupted"));
break;
}
}
});
initializationThread.setDaemon(true);
initializationThread.start();
}

@Override
public boolean isInitialized() {
// LOG.info(String.format("Checking if AnkaCloudClientEx %s is initialized", this.toString()));
return this.connector.isRunning();
// this runs hundreds of times a minute. Do not include API calls here.
return isControllerRunning.get();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.NoRouteToHostException;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -643,7 +644,7 @@ protected JSONObject doRequest(RequestMethod method, String path, JSONObject req
if (roundRobin != null) {
roundRobin.update(host, (int) elapsedTime, false);
}
} catch (HttpHostConnectException | ConnectTimeoutException e) {
} catch (SocketException e) {
if (roundRobin != null) {
roundRobin.update(host, 0, true);
}
Expand Down Expand Up @@ -694,13 +695,14 @@ protected JSONObject doRequest(RequestMethod method, String path, JSONObject req
}
}
return null;
} catch (HttpHostConnectException | ConnectTimeoutException | ClientException | SSLException | NoRouteToHostException e) {
} catch (ConnectTimeoutException | ClientException | SSLException | SocketException e) {
// don't retry on client exception
LOG.error(String.format("[retry %d] Got exception: %s %s", retry, e.getClass().getName(), e.getMessage()));
LOG.error(String.format("Got exception (not retrying): %s %s", e.getClass().getName(), e.getMessage()));
throw new AnkaMgmtException(e);
} catch (Exception e) {
LOG.error(e.toString());
LOG.error(String.format("[retry %d] Got generic exception: %s %s", retry, e.getClass().getName(), e.getMessage()));
if (e.getMessage().contains("controller URL is empty")) {
if (e.getMessage().contains("controller URL is empty") || e.getMessage().contains("Network is unreachable")) {
throw new AnkaMgmtException(e.getMessage());
}
if (retry >= maxRetries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<bean class="com.veertu.InstanceUpdater" />
<bean class="com.veertu.AnkaEditProfileController" />
<bean id="AnkaBuildServerAdapter" class="com.veertu.AnkaBuildServerAdapter" init-method="register"/>
<bean id="AgentParametersProvider" class="com.veertu.AgentParametersProvider" />
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
onFailure: function (response) {
$j(".controllerURLError").text('');
$j(".controllerURLError").append('Error: Controller communication cannot be established.');
$j(".submitButton").prop("disabled", true);
console.log(response);
if (response.status === 401) {
showAuth();
Expand All @@ -362,6 +363,7 @@
}
getTags();
$j(".dependentOnControllerConnection").removeClass("hidden");
$j(".submitButton").prop("disabled", false);
$j(".controllerURLError").text('');
}
}
Expand Down Expand Up @@ -436,7 +438,7 @@
}
} catch (e) {
$j("#groupSelect").addClass("hidden");
$j("#nodeGroupTd").append($j('<span style="color: yellow;">Requires Enterprise License.</span>'));
$j("#nodeGroupTd").append($j('<span style="color: gray;">Requires Enterprise License.</span>'));
}
}
}
Expand Down Expand Up @@ -503,8 +505,10 @@
if (isNaN(vCpuValue) || vCpuValue.indexOf('.') !== -1 || vCpuValue < 0) {
vCpuInput.val('');
$j("#error_${paramvCpu}").text('Please enter a valid integer for vCPU count.');
$j(".submitButton").prop("disabled", true);
} else {
$j("#error_${paramvCpu}").text('');
$j(".submitButton").prop("disabled", false);
}
});
Expand All @@ -514,8 +518,10 @@
if (isNaN(ramValue) || ramValue.indexOf('.') !== -1 || ramValue < 0) {
ramInput.val('');
$j("#error_${paramRam}").text('Please enter a valid integer for RAM size.');
$j(".submitButton").prop("disabled", true);
} else {
$j("#error_${paramRam}").text('');
$j(".submitButton").prop("disabled", false);
}
});
Expand Down

0 comments on commit 65d0856

Please sign in to comment.