Skip to content

Commit

Permalink
Add thread count limit in GCPInstanceService.
Browse files Browse the repository at this point in the history
Add maintance endpoint to touch all VMs before deployment.
  • Loading branch information
ekharkunov committed Aug 15, 2024
1 parent 0477ec2 commit 35eb188
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.cloud.compute.v1.InstancesClient;
import com.google.cloud.compute.v1.InstancesSettings;
import com.google.cloud.compute.v1.Operation;
import com.defold.extender.remote.RemoteHostConfiguration;
import com.defold.extender.remote.RemoteInstanceConfig;
import com.defold.extender.services.data.GCPInstanceState;
import com.google.api.gax.core.InstantiatingExecutorProvider;
import com.google.cloud.compute.v1.Instance.Status;

@Service
@RestController
@ConditionalOnProperty(prefix = "extender", name = "gcp.controller.enabled", havingValue = "true")
public class GCPInstanceService {

Expand All @@ -35,11 +40,20 @@ public class GCPInstanceService {
@Value("${extender.gcp.controller.retry-timeout:10000}") private long retryTimeout;
@Value("${extender.gcp.controller.time-before-suspend:1800000}") private long timeBeforeSuspend;
@Value("${extender.gcp.controller.wait-startup-time:10000}") private long waitStartupTime;
@Value("${extender.gcp.controller.gax-max-thread-count:10}") private int gaxMaxThreadCount;

private Map<String, GCPInstanceState> instanceState = new HashMap<>();

public GCPInstanceService(RemoteHostConfiguration remoteHostConfiguration) throws IOException{
instancesClient = InstancesClient.create();
// we need limit thread count because by default gax can create up to 200 thread
// see more details here https://github.com/googleapis/sdk-platform-java/blob/7c32859f82f7c6ebe95b9a3461e96d8653c79a0b/gax-java/gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java
InstantiatingExecutorProvider executor = InstantiatingExecutorProvider.newBuilder()
.setExecutorThreadCount(gaxMaxThreadCount)
.build();
InstancesSettings settings = InstancesSettings.newBuilder()
.setExecutorProvider(executor)
.build();
instancesClient = InstancesClient.create(settings);
for (Map.Entry<String, RemoteInstanceConfig> entry : remoteHostConfiguration.getPlatforms().entrySet()) {
if (!entry.getValue().getAlwaysOn()) {
instanceState.put(entry.getValue().getInstanceId(), new GCPInstanceState());
Expand Down Expand Up @@ -124,4 +138,19 @@ public void checkInstancesState() {
}
}
}


// inner endpoint used to make necessary changes before starting instance update
@ConditionalOnProperty(prefix = "extender", name = "gcp.controller.enabled", havingValue = "true")
@PostMapping("/maintance_mode")
public void postMethodName() {
LOGGER.info("Set maintance mode");
for (Map.Entry<String, GCPInstanceState> entry : instanceState.entrySet()) {
try {
touchInstance(entry.getKey());
} catch (Exception exc) {
LOGGER.error(String.format("Exception during touch instance '%s'", entry.getKey()), exc);
}
}
}
}

0 comments on commit 35eb188

Please sign in to comment.