You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tz...@apache.org on 2020/02/28 01:25:09 UTC
[flink-statefun] 04/05: [FLINK-16321] Add an okhttp based
RequestReplyClient
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git
commit b9d8853110c8ee4a8bb04633777467b87a1a0a44
Author: Igal Shilman <ig...@gmail.com>
AuthorDate: Thu Feb 27 21:28:34 2020 +0100
[FLINK-16321] Add an okhttp based RequestReplyClient
This commit adds an okhttp based RequestReplyClient that
captures the transport related concerns.
---
.../flink/core/httpfn/HttpRequestReplyClient.java | 86 ++++++++++++++++++++++
1 file changed, 86 insertions(+)
diff --git a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/httpfn/HttpRequestReplyClient.java b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/httpfn/HttpRequestReplyClient.java
new file mode 100644
index 0000000..794d246
--- /dev/null
+++ b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/httpfn/HttpRequestReplyClient.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.flink.statefun.flink.core.httpfn;
+
+import static org.apache.flink.statefun.flink.core.common.PolyglotUtil.parseProtobufOrThrow;
+import static org.apache.flink.util.Preconditions.checkState;
+
+import java.io.InputStream;
+import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
+import okhttp3.Call;
+import okhttp3.HttpUrl;
+import okhttp3.MediaType;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import org.apache.flink.statefun.flink.core.polyglot.generated.FromFunction;
+import org.apache.flink.statefun.flink.core.polyglot.generated.ToFunction;
+import org.apache.flink.statefun.flink.core.reqreply.RequestReplyClient;
+import org.apache.flink.util.IOUtils;
+
+final class HttpRequestReplyClient implements RequestReplyClient {
+ private static final MediaType MEDIA_TYPE_BINARY = MediaType.parse("application/octet-stream");
+
+ private final HttpUrl url;
+ private final OkHttpClient client;
+
+ HttpRequestReplyClient(HttpUrl url, OkHttpClient client) {
+ this.url = Objects.requireNonNull(url);
+ this.client = Objects.requireNonNull(client);
+ }
+
+ @Override
+ public CompletableFuture<FromFunction> call(ToFunction toFunction) {
+ Request request =
+ new Request.Builder()
+ .url(url)
+ .post(RequestBody.create(MEDIA_TYPE_BINARY, toFunction.toByteArray()))
+ .build();
+
+ Call newCall = client.newCall(request);
+ RetryingCallback callback = new RetryingCallback(newCall.timeout());
+ newCall.enqueue(callback);
+ return callback.future().thenApply(HttpRequestReplyClient::parseResponse);
+ }
+
+ private static FromFunction parseResponse(Response response) {
+ final InputStream httpResponseBody = responseBody(response);
+ try {
+ FromFunction fromFunction = parseProtobufOrThrow(FromFunction.parser(), httpResponseBody);
+ if (fromFunction.hasInvocationResult()) {
+ return fromFunction;
+ }
+ return FromFunction.getDefaultInstance();
+ } finally {
+ IOUtils.closeQuietly(httpResponseBody);
+ }
+ }
+
+ private static InputStream responseBody(Response httpResponse) {
+ checkState(httpResponse.isSuccessful(), "Unexpected HTTP status code %s", httpResponse.code());
+ checkState(httpResponse.body() != null, "Unexpected empty HTTP response (no body)");
+ checkState(
+ Objects.equals(httpResponse.body().contentType(), MEDIA_TYPE_BINARY),
+ "Wrong HTTP content-type %s",
+ httpResponse.body().contentType());
+ return httpResponse.body().byteStream();
+ }
+}