-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(gce): Add support for batch requests to the compute API wrap…
…per (#3792) * refactor(gce): Add support for batch requests to the new compute API wrappers * refactor(gce): introduce a wrapper around compute.images() * refactor(gce): Allow retrieval of the ComputeRequest from GoogleComputeRequest * style(google): Add copyright headers. * style(google): revert accidentally changed copyright year
- Loading branch information
Showing
24 changed files
with
1,706 additions
and
534 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
215 changes: 215 additions & 0 deletions
215
...src/main/groovy/com/netflix/spinnaker/clouddriver/google/compute/ComputeBatchRequest.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,215 @@ | ||
/* | ||
* Copyright 2019 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.google.compute; | ||
|
||
import static com.google.common.collect.Lists.partition; | ||
|
||
import com.google.api.client.googleapis.batch.BatchRequest; | ||
import com.google.api.client.googleapis.batch.json.JsonBatchCallback; | ||
import com.google.api.client.util.Throwables; | ||
import com.google.api.services.compute.Compute; | ||
import com.google.api.services.compute.ComputeRequest; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.util.concurrent.AbstractFuture; | ||
import com.google.common.util.concurrent.FutureCallback; | ||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.common.util.concurrent.ListeningExecutorService; | ||
import com.netflix.spectator.api.Registry; | ||
import java.io.IOException; | ||
import java.io.InterruptedIOException; | ||
import java.io.UncheckedIOException; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Value; | ||
import org.apache.http.client.HttpResponseException; | ||
|
||
public class ComputeBatchRequest<RequestT extends ComputeRequest<ResponseT>, ResponseT> { | ||
|
||
// Platform-specified max to not overwhelm batch backends. | ||
@VisibleForTesting static final int MAX_BATCH_SIZE = 100; | ||
private static final Duration CONNECT_TIMEOUT = Duration.ofMinutes(2); | ||
private static final Duration READ_TIMEOUT = Duration.ofMinutes(2); | ||
|
||
private final Compute compute; | ||
private final Registry registry; | ||
private final String userAgent; | ||
private final ListeningExecutorService executor; | ||
private final List<QueuedRequest<RequestT, ResponseT>> queuedRequests; | ||
|
||
ComputeBatchRequest( | ||
Compute compute, Registry registry, String userAgent, ListeningExecutorService executor) { | ||
this.compute = compute; | ||
this.registry = registry; | ||
this.userAgent = userAgent; | ||
this.executor = executor; | ||
this.queuedRequests = new ArrayList<>(); | ||
} | ||
|
||
public void queue( | ||
GoogleComputeRequest<RequestT, ResponseT> request, JsonBatchCallback<ResponseT> callback) { | ||
queuedRequests.add(new QueuedRequest<>(request.getRequest(), callback)); | ||
} | ||
|
||
public void execute(String batchContext) throws IOException { | ||
if (queuedRequests.size() == 0) { | ||
return; | ||
} | ||
|
||
List<List<QueuedRequest<RequestT, ResponseT>>> requestPartitions = | ||
partition(queuedRequests, MAX_BATCH_SIZE); | ||
List<BatchRequest> queuedBatches = createBatchRequests(requestPartitions); | ||
|
||
String statusCode = "500"; | ||
String success = "false"; | ||
long start = registry.clock().monotonicTime(); | ||
try { | ||
executeBatches(queuedBatches); | ||
success = "true"; | ||
statusCode = "200"; | ||
} catch (HttpResponseException e) { | ||
statusCode = Integer.toString(e.getStatusCode()); | ||
throw e; | ||
} finally { | ||
long nanos = registry.clock().monotonicTime() - start; | ||
String status = statusCode.charAt(0) + "xx"; | ||
Map<String, String> tags = | ||
ImmutableMap.of( | ||
"context", batchContext, | ||
"success", success, | ||
"status", status, | ||
"statusCode", statusCode); | ||
registry | ||
.timer(registry.createId("google.batchExecute", tags)) | ||
.record(Duration.ofNanos(nanos)); | ||
registry | ||
.counter(registry.createId("google.batchSize", tags)) | ||
.increment(queuedRequests.size()); | ||
} | ||
} | ||
|
||
private void executeBatches(List<BatchRequest> queuedBatches) throws IOException { | ||
if (queuedBatches.size() == 1) { | ||
queuedBatches.get(0).execute(); | ||
return; | ||
} | ||
|
||
List<ListenableFuture<Void>> futures = new ArrayList<>(); | ||
for (BatchRequest batchRequest : queuedBatches) { | ||
ListenableFuture<Void> submit = | ||
executor.submit( | ||
() -> { | ||
batchRequest.execute(); | ||
return null; | ||
}); | ||
futures.add(submit); | ||
} | ||
|
||
try { | ||
new FailFastFuture(futures, executor).get(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new InterruptedIOException(); | ||
} catch (ExecutionException e) { | ||
Throwable cause = e.getCause(); | ||
Throwables.propagateIfPossible(cause, IOException.class); | ||
throw new RuntimeException(cause); | ||
} | ||
} | ||
|
||
private List<BatchRequest> createBatchRequests( | ||
List<List<QueuedRequest<RequestT, ResponseT>>> requestPartitions) throws IOException { | ||
|
||
List<BatchRequest> queuedBatches = new ArrayList<>(); | ||
|
||
try { | ||
requestPartitions.forEach( | ||
partition -> { | ||
BatchRequest batch = newBatch(); | ||
partition.forEach( | ||
qr -> wrapIOException(() -> qr.getRequest().queue(batch, qr.getCallback()))); | ||
queuedBatches.add(batch); | ||
}); | ||
return queuedBatches; | ||
} catch (UncheckedIOException e) { | ||
throw e.getCause(); | ||
} | ||
} | ||
|
||
private BatchRequest newBatch() { | ||
return compute.batch( | ||
request -> { | ||
request.getHeaders().setUserAgent(userAgent); | ||
request.setConnectTimeout((int) CONNECT_TIMEOUT.toMillis()); | ||
request.setReadTimeout((int) READ_TIMEOUT.toMillis()); | ||
}); | ||
} | ||
|
||
@FunctionalInterface | ||
private interface IoExceptionRunnable { | ||
void run() throws IOException; | ||
} | ||
|
||
private static void wrapIOException(IoExceptionRunnable runnable) { | ||
try { | ||
runnable.run(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Value | ||
@AllArgsConstructor | ||
private static class QueuedRequest<RequestT extends ComputeRequest<ResponseT>, ResponseT> { | ||
private RequestT request; | ||
private JsonBatchCallback<ResponseT> callback; | ||
} | ||
|
||
private static class FailFastFuture extends AbstractFuture<Void> { | ||
|
||
private final AtomicInteger remainingFutures; | ||
|
||
FailFastFuture(List<ListenableFuture<Void>> futures, ExecutorService executor) { | ||
remainingFutures = new AtomicInteger(futures.size()); | ||
for (ListenableFuture<Void> future : futures) { | ||
Futures.addCallback( | ||
future, | ||
new FutureCallback<Object>() { | ||
@Override | ||
public void onSuccess(Object result) { | ||
if (remainingFutures.decrementAndGet() == 0) { | ||
set(null); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable t) { | ||
setException(t); | ||
} | ||
}, | ||
executor); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...rc/main/groovy/com/netflix/spinnaker/clouddriver/google/compute/ComputeConfiguration.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,36 @@ | ||
/* | ||
* Copyright 2019 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.google.compute; | ||
|
||
import com.google.common.util.concurrent.ListeningExecutorService; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import java.util.concurrent.Executors; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class ComputeConfiguration { | ||
|
||
public static final String BATCH_REQUEST_EXECUTOR = "batchRequestExecutor"; | ||
|
||
@Bean | ||
@Qualifier(BATCH_REQUEST_EXECUTOR) | ||
public ListeningExecutorService batchRequestExecutor() { | ||
return MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); | ||
} | ||
} |
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.