You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2022/07/25 14:02:09 UTC

[GitHub] [dolphinscheduler] ruanwenjun commented on a diff in pull request #11137: Add base loop task execotor and http template parser

ruanwenjun commented on code in PR #11137:
URL: https://github.com/apache/dolphinscheduler/pull/11137#discussion_r928921811


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/RetryUtils.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.dolphinscheduler.plugin.task.api.utils;
+
+import java.util.function.Supplier;
+
+import lombok.Data;
+import lombok.NonNull;
+import lombok.experimental.UtilityClass;
+
+@UtilityClass
+public class RetryUtils {
+
+    private static final RetryPolicy DEFAULT_RETRY_POLICY = new RetryPolicy(3, 1000L);
+
+    /**
+     * Retry to execute the given function with the default retry policy.
+     */
+    public static <T> T retryFunction(@NonNull Supplier<T> supplier) {
+        return retryFunction(supplier, DEFAULT_RETRY_POLICY);
+    }
+
+    /**
+     * Retry to execute the given function with the given retry policy.
+     */
+    public static <T> T retryFunction(@NonNull Supplier<T> supplier, @NonNull RetryPolicy retryPolicy) {
+        int retryCount = 0;
+        long retryInterval = 0L;
+        while (true) {
+            try {
+                return supplier.get();
+            } catch (Exception ex) {
+                if (retryCount == retryPolicy.getMaxRetryTimes()) {
+                    throw ex;
+                }
+                retryCount++;
+                try {
+                    Thread.sleep(retryInterval);

Review Comment:
   Done



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/OkHttpUtils.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.dolphinscheduler.plugin.task.api.utils;
+
+import org.apache.dolphinscheduler.spi.utils.JSONUtils;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import lombok.NonNull;
+import okhttp3.HttpUrl;
+import okhttp3.MediaType;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+
+public class OkHttpUtils {
+
+    private static final OkHttpClient CLIENT = new OkHttpClient();
+
+    public static @NonNull String get(@NonNull String url, @Nullable Map<String, Object> requestParams)
+        throws IOException {
+        HttpUrl httpUrl = HttpUrl.parse(url);
+        if (httpUrl == null) {
+            throw new IllegalArgumentException(String.format("url: %s is invalid", url));
+        }
+        HttpUrl.Builder urlBuilder = httpUrl.newBuilder();
+        if (requestParams != null) {
+            for (Map.Entry<String, Object> entry : requestParams.entrySet()) {
+                urlBuilder.addQueryParameter(entry.getKey(), entry.getValue().toString());
+            }
+        }
+        Request request = new Request.Builder().url(urlBuilder.build()).build();
+        try (Response response = CLIENT.newCall(request).execute()) {
+            if (response.code() != 200 || response.body() == null) {

Review Comment:
   Done



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/OkHttpUtils.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.dolphinscheduler.plugin.task.api.utils;
+
+import org.apache.dolphinscheduler.spi.utils.JSONUtils;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import lombok.NonNull;
+import okhttp3.HttpUrl;
+import okhttp3.MediaType;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+
+public class OkHttpUtils {
+
+    private static final OkHttpClient CLIENT = new OkHttpClient();
+
+    public static @NonNull String get(@NonNull String url, @Nullable Map<String, Object> requestParams)
+        throws IOException {
+        HttpUrl httpUrl = HttpUrl.parse(url);
+        if (httpUrl == null) {
+            throw new IllegalArgumentException(String.format("url: %s is invalid", url));
+        }
+        HttpUrl.Builder urlBuilder = httpUrl.newBuilder();
+        if (requestParams != null) {
+            for (Map.Entry<String, Object> entry : requestParams.entrySet()) {
+                urlBuilder.addQueryParameter(entry.getKey(), entry.getValue().toString());
+            }
+        }
+        Request request = new Request.Builder().url(urlBuilder.build()).build();
+        try (Response response = CLIENT.newCall(request).execute()) {
+            if (response.code() != 200 || response.body() == null) {
+                throw new RuntimeException(String.format("Request execute failed, httpCode: %s, httpBody: %s",
+                                                         response.code(),
+                                                         response.body()));
+            }
+            return response.body().string();
+        }
+    }
+
+    public static @NonNull String post(@NonNull String url,
+                                       @Nullable Map<String, Object> requestParamsMap,
+                                       @Nullable Map<String, Object> requestBodyMap) throws IOException {
+        HttpUrl httpUrl = HttpUrl.parse(url);
+        if (httpUrl == null) {
+            throw new IllegalArgumentException(String.format("url: %s is invalid", url));
+        }
+        HttpUrl.Builder urlBuilder = httpUrl.newBuilder();
+        if (requestParamsMap != null) {
+            for (Map.Entry<String, Object> entry : requestParamsMap.entrySet()) {
+                urlBuilder.addQueryParameter(entry.getKey(), entry.getValue().toString());
+            }
+        }
+        Request.Builder requestBuilder = new Request.Builder().url(urlBuilder.build());
+        if (requestBodyMap != null) {
+            requestBuilder = requestBuilder.post(RequestBody.create(MediaType.parse("application/json"),
+                                                                    JSONUtils.toJsonString(requestBodyMap)));
+        }
+        try (Response response = CLIENT.newCall(requestBuilder.build()).execute()) {
+            if (response.code() != 200 || response.body() == null) {

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@dolphinscheduler.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org