forked from spinnaker/clouddriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider/gce): Limit GCE batch sizes. (spinnaker#3125)
* feat(provider/gce): Limits GCE batch sizes. Adds a new class to limit GCE batch sizes, adds a hook to PaginatedRequest using the new batch class, and exercises both using the regional server group caching agent as the guinea pig. * feat(provider/gce): Adds new GoogleBatchRequest to all callsites. * chore(provider/gce): Adds instrumentation to new batches. * chore(provider/gce): Update specs for new batching mechanism. * refactor(provider/gce): Switch to local threadpool for new batches.
- Loading branch information
Showing
23 changed files
with
318 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
.../main/groovy/com/netflix/spinnaker/clouddriver/googlecommon/batch/GoogleBatchRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright 2018 Google, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.googlecommon.batch; | ||
|
||
import com.google.api.client.googleapis.batch.BatchRequest; | ||
import com.google.api.client.googleapis.batch.json.JsonBatchCallback; | ||
import com.google.api.client.http.HttpRequest; | ||
import com.google.api.client.http.HttpRequestInitializer; | ||
import com.google.api.services.compute.Compute; | ||
import com.google.api.services.compute.ComputeRequest; | ||
import com.google.common.collect.Lists; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Helper class for sending batch requests to GCE. | ||
*/ | ||
@Slf4j | ||
public class GoogleBatchRequest { | ||
|
||
private static final int MAX_BATCH_SIZE = 100; // Platform specified max to not overwhelm batch backends. | ||
private static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = (int) TimeUnit.MINUTES.toMillis(2); | ||
private static final int DEFAULT_READ_TIMEOUT_MILLIS = (int) TimeUnit.MINUTES.toMillis(2); | ||
|
||
private List<QueuedRequest> queuedRequests; | ||
private String clouddriverUserAgentApplicationName; | ||
private Compute compute; | ||
|
||
public GoogleBatchRequest(Compute compute, String clouddriverUserAgentApplicationName) { | ||
this.compute = compute; | ||
this.clouddriverUserAgentApplicationName = clouddriverUserAgentApplicationName; | ||
this.queuedRequests = new ArrayList<>(); | ||
} | ||
|
||
public void execute() { | ||
if (queuedRequests.size() == 0) { | ||
log.debug("No requests queued in batch, exiting."); | ||
return; | ||
} | ||
|
||
List<BatchRequest> queuedBatches = new ArrayList<>(); | ||
List<List<QueuedRequest>> requestPartitions = Lists.partition(queuedRequests, MAX_BATCH_SIZE); | ||
requestPartitions.forEach(requestPart -> { | ||
BatchRequest newBatch = newBatch(); | ||
requestPart.forEach(qr -> { | ||
try { | ||
qr.getRequest().queue(newBatch, qr.getCallback()); | ||
} catch (Exception ioe) { | ||
log.error("Queueing request {} in batch failed.", qr, ioe); | ||
} | ||
}); | ||
queuedBatches.add(newBatch); | ||
}); | ||
|
||
ExecutorService threadPool = new ForkJoinPool(10); | ||
try { | ||
threadPool.submit(() -> queuedBatches.stream().parallel().forEach(this::executeInternalBatch)).get(); | ||
} catch (Exception e) { | ||
log.error("Executing queued batches failed.", e); | ||
} | ||
threadPool.shutdown(); | ||
} | ||
|
||
private void executeInternalBatch(BatchRequest b) { | ||
try { | ||
b.execute(); | ||
} catch (Exception e) { | ||
log.error("Executing batch {} failed.", b, e); | ||
} | ||
} | ||
|
||
private BatchRequest newBatch() { | ||
return compute.batch( | ||
new HttpRequestInitializer() { | ||
@Override | ||
public void initialize(HttpRequest request) { | ||
request.getHeaders().setUserAgent(clouddriverUserAgentApplicationName); | ||
request.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS); | ||
request.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS); | ||
} | ||
} | ||
); | ||
} | ||
|
||
public void queue(ComputeRequest request, JsonBatchCallback callback) { | ||
queuedRequests.add(new QueuedRequest(request, callback)); | ||
} | ||
|
||
public Integer size() { | ||
return queuedRequests.size(); | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
private static class QueuedRequest { | ||
private ComputeRequest request; | ||
private JsonBatchCallback callback; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.