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 13:17:41 UTC

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

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


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/loop/BaseLoopTaskExecutor.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.loop;
+
+import org.apache.dolphinscheduler.plugin.task.api.AbstractTaskExecutor;
+import org.apache.dolphinscheduler.plugin.task.api.TaskConstants;
+import org.apache.dolphinscheduler.plugin.task.api.TaskException;
+import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
+import org.apache.dolphinscheduler.plugin.task.api.utils.RetryUtils;
+
+import java.time.Duration;
+
+import javax.annotation.Nullable;
+
+import lombok.NonNull;
+
+/**
+ * This class is the base class for all loop task type.
+ * <p>
+ * The loop task type means, we will submit a task, and loop the task status until the task is finished.
+ */
+public abstract class BaseLoopTaskExecutor extends AbstractTaskExecutor {
+
+    /**
+     * The task instance info will be set when task has submitted successful.
+     */
+    protected @Nullable LoopTaskInstanceInfo loopTaskInstanceInfo;
+
+    protected BaseLoopTaskExecutor(@NonNull TaskExecutionContext taskExecutionContext) {
+        super(taskExecutionContext);
+    }
+
+    @Override
+    public void handle() throws TaskException {
+        try {
+            final long loopInterval = getTaskInstanceStatusQueryInterval().toMillis();
+            loopTaskInstanceInfo = submitLoopTask();
+            this.appIds = loopTaskInstanceInfo.getTaskInstanceId();
+            while (!cancel
+                && !RetryUtils.retryFunction(() -> queryTaskInstanceStatus(loopTaskInstanceInfo).isFinished())) {

Review Comment:
   please add some comments.



##########
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:
   ```suggestion
               if (response.code() != HttpStatus.OK || response.body() == null) {
   ```



##########
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:
   ```suggestion
               if (response.code() != HttpStatus.OK || response.body() == null) {
   ```



##########
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:
   Maybe we should tell users of the thread sleep when call this method.



##########
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/loop/template/http/method/HttpLoopTaskCancelTaskMethodDefinition.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.loop.template.http.method;
+
+import org.apache.dolphinscheduler.plugin.task.api.loop.LoopTaskCancelMethodDefinition;
+import org.apache.dolphinscheduler.plugin.task.api.loop.LoopTaskInstanceInfo;
+import org.apache.dolphinscheduler.plugin.task.api.loop.template.http.HttpLoopTaskMethodDefinition;
+import org.apache.dolphinscheduler.plugin.task.api.utils.OkHttpUtils;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.Map;
+
+public class HttpLoopTaskCancelTaskMethodDefinition extends HttpLoopTaskMethodDefinition
+    implements LoopTaskCancelMethodDefinition {
+
+    private final String taskInstanceIdHolder = "${taskInstanceId}";
+
+    public HttpLoopTaskCancelTaskMethodDefinition(String url,
+                                                  String httpMethodType,
+                                                  String dataType,
+                                                  Map<String, Object> requestParams,
+                                                  Map<String, Object> requestBody) {
+        super(url, httpMethodType, dataType, requestParams, requestBody);
+    }
+
+    @Override
+    public void cancelTaskInstance(LoopTaskInstanceInfo loopTaskInstanceInfo) {
+        if (requestParams != null) {

Review Comment:
   It seems some same code.



-- 
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