Browse Source
# Conflicts: # dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessDefinitionController.java # dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/procedure/ProcedureTask.javapull/3/MERGE
baoliang
4 years ago
75 changed files with 1628 additions and 562 deletions
@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!-- |
||||
~ 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. |
||||
--> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<parent> |
||||
<artifactId>dolphinscheduler-alert-plugin</artifactId> |
||||
<groupId>org.apache.dolphinscheduler</groupId> |
||||
<version>${revision}</version> |
||||
</parent> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<groupId>org.apache.dolphinscheduler</groupId> |
||||
<artifactId>dolphinscheduler-alert-slack</artifactId> |
||||
<packaging>dolphinscheduler-plugin</packaging> |
||||
|
||||
<dependencies> |
||||
|
||||
<dependency> |
||||
<groupId>org.apache.dolphinscheduler</groupId> |
||||
<artifactId>dolphinscheduler-spi</artifactId> |
||||
<scope>provided</scope> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.apache.httpcomponents</groupId> |
||||
<artifactId>httpclient</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>com.google.guava</groupId> |
||||
<artifactId>guava</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>ch.qos.logback</groupId> |
||||
<artifactId>logback-classic</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.slf4j</groupId> |
||||
<artifactId>slf4j-api</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>com.fasterxml.jackson.core</groupId> |
||||
<artifactId>jackson-annotations</artifactId> |
||||
<scope>provided</scope> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>junit</groupId> |
||||
<artifactId>junit</artifactId> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.mockito</groupId> |
||||
<artifactId>mockito-core</artifactId> |
||||
<type>jar</type> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
</dependencies> |
||||
|
||||
<build> |
||||
<finalName>dolphinscheduler-alert-slack-${project.version}</finalName> |
||||
</build> |
||||
|
||||
</project> |
@ -0,0 +1,43 @@
|
||||
/* |
||||
* 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.alert.slack; |
||||
|
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannel; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertData; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertInfo; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertResult; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* SlackAlertChannel |
||||
*/ |
||||
public class SlackAlertChannel implements AlertChannel { |
||||
|
||||
@Override |
||||
public AlertResult process(AlertInfo alertInfo) { |
||||
AlertData alertData = alertInfo.getAlertData(); |
||||
Map<String, String> alertParams = alertInfo.getAlertParams(); |
||||
if (alertParams == null || alertParams.size() == 0) { |
||||
return new AlertResult("false", "Slack alert params is empty"); |
||||
} |
||||
SlackSender slackSender = new SlackSender(alertParams); |
||||
String response = slackSender.sendMessage(alertData.getTitle(), alertData.getContent()); |
||||
return new AlertResult("ok".equals(response) ? "true" : "false", response); |
||||
} |
||||
} |
@ -0,0 +1,66 @@
|
||||
/* |
||||
* 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.alert.slack; |
||||
|
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannel; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory; |
||||
import org.apache.dolphinscheduler.spi.params.InputParam; |
||||
import org.apache.dolphinscheduler.spi.params.base.PluginParams; |
||||
import org.apache.dolphinscheduler.spi.params.base.Validate; |
||||
|
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Slack alert factory, see {@link AlertChannelFactory} |
||||
*/ |
||||
public class SlackAlertChannelFactory implements AlertChannelFactory { |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "Slack"; |
||||
} |
||||
|
||||
@Override |
||||
public List<PluginParams> getParams() { |
||||
List<PluginParams> paramsList = new LinkedList<>(); |
||||
|
||||
InputParam webHookParam = InputParam.newBuilder(SlackParamsConstants.SLACK_WEN_HOOK_URL_NAME, SlackParamsConstants.SLACK_WEB_HOOK_URL) |
||||
.addValidate(Validate.newBuilder() |
||||
.setRequired(true) |
||||
.build()) |
||||
.setPlaceholder("Input WebHook Url") |
||||
.build(); |
||||
|
||||
InputParam botName = InputParam.newBuilder(SlackParamsConstants.SLACK_BOT_NAME, SlackParamsConstants.SLACK_BOT) |
||||
.addValidate(Validate.newBuilder() |
||||
.setRequired(true) |
||||
.build()) |
||||
.setPlaceholder("Input the bot username") |
||||
.build(); |
||||
|
||||
paramsList.add(webHookParam); |
||||
paramsList.add(botName); |
||||
return paramsList; |
||||
} |
||||
|
||||
@Override |
||||
public AlertChannel create() { |
||||
return new SlackAlertChannel(); |
||||
} |
||||
} |
@ -0,0 +1,34 @@
|
||||
/* |
||||
* 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.alert.slack; |
||||
|
||||
import org.apache.dolphinscheduler.spi.DolphinSchedulerPlugin; |
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory; |
||||
|
||||
import com.google.common.collect.ImmutableList; |
||||
|
||||
/** |
||||
* Slack alert plugin |
||||
*/ |
||||
public class SlackAlertPlugin implements DolphinSchedulerPlugin { |
||||
|
||||
@Override |
||||
public Iterable<AlertChannelFactory> getAlertChannelFactorys() { |
||||
return ImmutableList.of(new SlackAlertChannelFactory()); |
||||
} |
||||
} |
@ -0,0 +1,34 @@
|
||||
/* |
||||
* 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.alert.slack; |
||||
|
||||
public class SlackParamsConstants { |
||||
|
||||
private SlackParamsConstants() { |
||||
|
||||
} |
||||
|
||||
public static final String SLACK_WEB_HOOK_URL = "WebHook"; |
||||
public static final String SLACK_WEN_HOOK_URL_NAME = "webHook"; |
||||
public static final String SLACK_BOT = "Username"; |
||||
public static final String SLACK_BOT_NAME = "username"; |
||||
public static final String TEXT = "text"; |
||||
public static final String ATTACHMENT = "attachments"; |
||||
|
||||
public static final Integer MAX_SHOW_NUMBER = 100; |
||||
} |
@ -0,0 +1,149 @@
|
||||
/* |
||||
* 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.alert.slack; |
||||
|
||||
import org.apache.dolphinscheduler.spi.utils.JSONUtils; |
||||
import org.apache.dolphinscheduler.spi.utils.StringUtils; |
||||
|
||||
import org.apache.http.HttpEntity; |
||||
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
import org.apache.http.client.methods.HttpPost; |
||||
import org.apache.http.entity.StringEntity; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClients; |
||||
import org.apache.http.util.EntityUtils; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Map.Entry; |
||||
import java.util.Objects; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.google.common.base.Preconditions; |
||||
|
||||
public class SlackSender { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SlackSender.class); |
||||
|
||||
private String webHookUrl; |
||||
|
||||
private String botName; |
||||
|
||||
public SlackSender(Map<String, String> slackAlertParam) { |
||||
webHookUrl = slackAlertParam.get(SlackParamsConstants.SLACK_WEN_HOOK_URL_NAME); |
||||
botName = slackAlertParam.get(SlackParamsConstants.SLACK_BOT_NAME); |
||||
Preconditions.checkArgument(!Objects.isNull(webHookUrl), "SlackWebHookURL can not be null"); |
||||
Preconditions.checkArgument(webHookUrl.startsWith("https://hooks.slack.com/services/"), "SlackWebHookURL invalidate"); |
||||
Preconditions.checkArgument(!Objects.isNull(botName), "slack bot name can not be null"); |
||||
} |
||||
|
||||
/** |
||||
* Send message to slack channel |
||||
* |
||||
* @param title title |
||||
* @param content content |
||||
* @return slack response |
||||
*/ |
||||
public String sendMessage(String title, String content) { |
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
||||
Map<String, Object> paramMap = new HashMap<>(); |
||||
paramMap.put(SlackParamsConstants.SLACK_BOT_NAME, botName); |
||||
paramMap.put(SlackParamsConstants.TEXT, title); |
||||
if (StringUtils.isNotEmpty(content)) { |
||||
Map<String, String> attachmentTable = new HashMap<>(); |
||||
attachmentTable.put(SlackParamsConstants.TEXT, generateMarkDownTable(content)); |
||||
List<Map<String, String>> attachments = new ArrayList<>(); |
||||
attachments.add(attachmentTable); |
||||
paramMap.put(SlackParamsConstants.ATTACHMENT, attachments); |
||||
} |
||||
|
||||
HttpPost httpPost = new HttpPost(webHookUrl); |
||||
httpPost.setEntity(new StringEntity(JSONUtils.toJsonString(paramMap), "UTF-8")); |
||||
CloseableHttpResponse response = httpClient.execute(httpPost); |
||||
|
||||
HttpEntity entity = response.getEntity(); |
||||
return EntityUtils.toString(entity, "UTF-8"); |
||||
} catch (Exception e) { |
||||
logger.error("Send message to slack error.", e); |
||||
return "System Exception"; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Because the slack does not support table we can transform to specific markdown table |
||||
* |
||||
* @param content sql data content |
||||
*/ |
||||
private String generateMarkDownTable(String content) { |
||||
List<LinkedHashMap> linkedHashMaps = JSONUtils.toList(content, LinkedHashMap.class); |
||||
if (linkedHashMaps.size() > SlackParamsConstants.MAX_SHOW_NUMBER) { |
||||
linkedHashMaps = linkedHashMaps.subList(0, SlackParamsConstants.MAX_SHOW_NUMBER); |
||||
} |
||||
int maxLen = 0; |
||||
List<String> headers = new LinkedList<>(); |
||||
LinkedHashMap<String, Object> tmp = linkedHashMaps.get(0); |
||||
for (Entry<String, Object> entry : tmp.entrySet()) { |
||||
maxLen = Math.max(maxLen, entry.getKey().length()); |
||||
headers.add(entry.getKey()); |
||||
} |
||||
List<List<String>> elements = new ArrayList<>(tmp.size()); |
||||
// build header
|
||||
for (LinkedHashMap<String, Object> linkedHashMap : linkedHashMaps) { |
||||
List<String> element = new ArrayList<>(linkedHashMap.size()); |
||||
for (Object value : linkedHashMap.values()) { |
||||
String valueStr = value.toString(); |
||||
maxLen = Math.max(maxLen, valueStr.length()); |
||||
element.add(valueStr); |
||||
} |
||||
elements.add(element); |
||||
} |
||||
final int elementLen = maxLen; |
||||
StringBuilder stringBuilder = new StringBuilder(200); |
||||
stringBuilder.append(headers.stream() |
||||
.map(header -> generateString(header, elementLen, " ")) |
||||
.collect(Collectors.joining("|"))); |
||||
stringBuilder.append("\n"); |
||||
for (List<String> element : elements) { |
||||
stringBuilder.append(element.stream() |
||||
.map(lement -> generateString("", elementLen, "-")) |
||||
.collect(Collectors.joining("|"))); |
||||
stringBuilder.append("\n"); |
||||
stringBuilder.append(element.stream() |
||||
.map(e -> generateString(e, elementLen, " ")) |
||||
.collect(Collectors.joining("|"))); |
||||
stringBuilder.append("\n"); |
||||
} |
||||
return String.format("```%s```", stringBuilder); |
||||
} |
||||
|
||||
private String generateString(String value, int len, String supplement) { |
||||
StringBuilder stringBuilder = new StringBuilder(len); |
||||
stringBuilder.append(value); |
||||
for (int i = 0; i < len - stringBuilder.length(); i++) { |
||||
stringBuilder.append(supplement); |
||||
} |
||||
return stringBuilder.toString(); |
||||
} |
||||
} |
@ -0,0 +1,48 @@
|
||||
/* |
||||
* 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.alert.slack; |
||||
|
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannel; |
||||
import org.apache.dolphinscheduler.spi.params.base.PluginParams; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
public class SlackAlertChannelFactoryTest { |
||||
|
||||
private SlackAlertChannelFactory slackAlertChannelFactory = new SlackAlertChannelFactory(); |
||||
|
||||
@Test |
||||
public void testTestGetName() { |
||||
Assert.assertEquals("Slack", slackAlertChannelFactory.getName()); |
||||
} |
||||
|
||||
@Test |
||||
public void testGetParams() { |
||||
List<PluginParams> params = slackAlertChannelFactory.getParams(); |
||||
Assert.assertEquals(2, params.size()); |
||||
} |
||||
|
||||
@Test |
||||
public void testCreate() { |
||||
AlertChannel alertChannel = slackAlertChannelFactory.create(); |
||||
Assert.assertTrue(alertChannel instanceof SlackAlertChannel); |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
/* |
||||
* 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.alert.slack; |
||||
|
||||
import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
public class SlackAlertPluginTest { |
||||
|
||||
private SlackAlertPlugin slackAlertPlugin = new SlackAlertPlugin(); |
||||
|
||||
@Test |
||||
public void testGetAlertChannelFactorys() { |
||||
Iterable<AlertChannelFactory> alertChannelFactorys = slackAlertPlugin.getAlertChannelFactorys(); |
||||
for (AlertChannelFactory alertChannelFactory : alertChannelFactorys) { |
||||
Assert.assertTrue(alertChannelFactory instanceof SlackAlertChannelFactory); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,39 @@
|
||||
/* |
||||
* 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.alert.slack; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
public class SlackSenderTest { |
||||
|
||||
@Test |
||||
public void testSendMessage() { |
||||
Map<String, String> alertparam = new HashMap<>(); |
||||
alertparam.put(SlackParamsConstants.SLACK_WEN_HOOK_URL_NAME, |
||||
"https://hooks.slack.com/services/123456"); |
||||
alertparam.put(SlackParamsConstants.SLACK_BOT_NAME, "Dolphinscheduler"); |
||||
|
||||
SlackSender slackSender = new SlackSender(alertparam); |
||||
String response = slackSender.sendMessage("test title", "test content"); |
||||
Assert.assertNotEquals("ok", response); |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
/* |
||||
* 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.api.aspect; |
||||
|
||||
import java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
@Target(ElementType.METHOD) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Documented |
||||
public @interface AccessLogAnnotation { |
||||
// ignore request args
|
||||
String[] ignoreRequestArgs() default {}; |
||||
|
||||
boolean ignoreRequest() default false; |
||||
|
||||
boolean ignoreResponse() default true; |
||||
} |
@ -0,0 +1,125 @@
|
||||
/* |
||||
* 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.api.aspect; |
||||
|
||||
import org.apache.dolphinscheduler.common.Constants; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.util.Arrays; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.UUID; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint; |
||||
import org.aspectj.lang.annotation.Around; |
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.aspectj.lang.annotation.Pointcut; |
||||
import org.aspectj.lang.reflect.MethodSignature; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.web.context.request.RequestContextHolder; |
||||
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
||||
@Aspect |
||||
@Component |
||||
public class AccessLogAspect { |
||||
private static final Logger logger = LoggerFactory.getLogger(AccessLogAspect.class); |
||||
|
||||
@Pointcut("@annotation(org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation)") |
||||
public void logPointCut(){ |
||||
// Do nothing because of it's a pointcut
|
||||
} |
||||
|
||||
@Around("logPointCut()") |
||||
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { |
||||
long startTime = System.currentTimeMillis(); |
||||
|
||||
// fetch AccessLogAnnotation
|
||||
MethodSignature sign = (MethodSignature) proceedingJoinPoint.getSignature(); |
||||
Method method = sign.getMethod(); |
||||
AccessLogAnnotation annotation = method.getAnnotation(AccessLogAnnotation.class); |
||||
|
||||
String tranceId = UUID.randomUUID().toString(); |
||||
|
||||
// log request
|
||||
if (!annotation.ignoreRequest()) { |
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
||||
if (attributes != null) { |
||||
HttpServletRequest request = attributes.getRequest(); |
||||
|
||||
// handle login info
|
||||
String userName = parseLoginInfo(request); |
||||
|
||||
// handle args
|
||||
String argsString = parseArgs(proceedingJoinPoint, annotation); |
||||
logger.info("REQUEST TRANCE_ID:{}, LOGIN_USER:{}, URI:{}, METHOD:{}, HANDLER:{}, ARGS:{}", |
||||
tranceId, |
||||
userName, |
||||
request.getRequestURI(), |
||||
request.getMethod(), |
||||
proceedingJoinPoint.getSignature().getDeclaringTypeName() + "." + proceedingJoinPoint.getSignature().getName(), |
||||
argsString); |
||||
|
||||
} |
||||
} |
||||
|
||||
Object ob = proceedingJoinPoint.proceed(); |
||||
|
||||
// log response
|
||||
if (!annotation.ignoreResponse()) { |
||||
logger.info("RESPONSE TRANCE_ID:{}, BODY:{}, REQUEST DURATION:{} milliseconds", tranceId, ob, (System.currentTimeMillis() - startTime)); |
||||
} |
||||
|
||||
return ob; |
||||
} |
||||
|
||||
private String parseArgs(ProceedingJoinPoint proceedingJoinPoint, AccessLogAnnotation annotation) { |
||||
Object[] args = proceedingJoinPoint.getArgs(); |
||||
String argsString = Arrays.toString(args); |
||||
if (annotation.ignoreRequestArgs().length > 0) { |
||||
String[] parameterNames = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterNames(); |
||||
if (parameterNames.length > 0) { |
||||
List<String> ignoreList = Arrays.stream(annotation.ignoreRequestArgs()).collect(Collectors.toList()); |
||||
HashMap<String, Object> argsMap = new HashMap<>(); |
||||
|
||||
for (int i = 0; i < parameterNames.length; i++) { |
||||
if (!ignoreList.contains(parameterNames[i])) { |
||||
argsMap.put(parameterNames[i], args[i]); |
||||
} |
||||
} |
||||
argsString = argsMap.toString(); |
||||
} |
||||
} |
||||
return argsString; |
||||
} |
||||
|
||||
private String parseLoginInfo(HttpServletRequest request) { |
||||
String userName = "NOT LOGIN"; |
||||
User loginUser = (User) (request.getAttribute(Constants.SESSION_USER)); |
||||
if (loginUser != null) { |
||||
userName = loginUser.getUserName(); |
||||
} |
||||
return userName; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,36 @@
|
||||
/* |
||||
* 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.remote.command.log; |
||||
|
||||
import org.apache.dolphinscheduler.remote.command.Command; |
||||
import org.apache.dolphinscheduler.remote.command.CommandType; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import junit.framework.Assert; |
||||
|
||||
public class GetLogBytesRequestCommandTest { |
||||
|
||||
@Test |
||||
public void testConvert2Command() { |
||||
GetLogBytesRequestCommand getLogBytesRequestCommand = new GetLogBytesRequestCommand(); |
||||
getLogBytesRequestCommand.setPath("/opt/test"); |
||||
Command command = getLogBytesRequestCommand.convert2Command(); |
||||
Assert.assertEquals(CommandType.GET_LOG_BYTES_REQUEST, command.getType()); |
||||
} |
||||
} |
@ -0,0 +1,37 @@
|
||||
/* |
||||
* 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.remote.command.log; |
||||
|
||||
import org.apache.dolphinscheduler.remote.command.Command; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import junit.framework.Assert; |
||||
|
||||
public class GetLogBytesResponseCommandTest { |
||||
|
||||
private byte[] data; |
||||
|
||||
@Test |
||||
public void testConvert2Command() { |
||||
GetLogBytesResponseCommand getLogBytesResponseCommand = new GetLogBytesResponseCommand(); |
||||
getLogBytesResponseCommand.setData(data); |
||||
Command command = getLogBytesResponseCommand.convert2Command(122); |
||||
Assert.assertNotNull(command); |
||||
} |
||||
} |
@ -0,0 +1,119 @@
|
||||
/* |
||||
* 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.server.worker.task.procedure; |
||||
|
||||
import org.apache.dolphinscheduler.common.Constants; |
||||
import org.apache.dolphinscheduler.server.entity.ProcedureTaskExecutionContext; |
||||
import org.apache.dolphinscheduler.server.entity.TaskExecutionContext; |
||||
import org.apache.dolphinscheduler.server.worker.task.TaskProps; |
||||
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; |
||||
import org.apache.dolphinscheduler.service.process.ProcessService; |
||||
|
||||
import java.sql.CallableStatement; |
||||
import java.sql.Connection; |
||||
import java.sql.DriverManager; |
||||
import java.sql.SQLException; |
||||
import java.util.Date; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mockito; |
||||
import org.powermock.api.mockito.PowerMockito; |
||||
import org.powermock.core.classloader.annotations.PrepareForTest; |
||||
import org.powermock.modules.junit4.PowerMockRunner; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.context.ApplicationContext; |
||||
|
||||
@RunWith(PowerMockRunner.class) |
||||
@PrepareForTest({ProcedureTask.class,DriverManager.class}) |
||||
public class ProcedureTaskTest { |
||||
private static final Logger logger = LoggerFactory.getLogger(ProcedureTaskTest.class); |
||||
|
||||
private static final String CONNECTION_PARAMS = "{\"user\":\"root\",\"password\":\"123456\",\"address\":\"jdbc:mysql://127.0.0.1:3306\"," |
||||
+ "\"database\":\"test\",\"jdbcUrl\":\"jdbc:mysql://127.0.0.1:3306/test\"}"; |
||||
|
||||
private ProcedureTask procedureTask; |
||||
|
||||
private ProcessService processService; |
||||
|
||||
private ApplicationContext applicationContext; |
||||
|
||||
private TaskExecutionContext taskExecutionContext; |
||||
|
||||
@Before |
||||
public void before() throws Exception { |
||||
taskExecutionContext = new TaskExecutionContext(); |
||||
processService = PowerMockito.mock(ProcessService.class); |
||||
applicationContext = PowerMockito.mock(ApplicationContext.class); |
||||
SpringApplicationContext springApplicationContext = new SpringApplicationContext(); |
||||
springApplicationContext.setApplicationContext(applicationContext); |
||||
PowerMockito.when(applicationContext.getBean(ProcessService.class)).thenReturn(processService); |
||||
|
||||
TaskProps props = new TaskProps(); |
||||
props.setExecutePath("/tmp"); |
||||
props.setTaskAppId(String.valueOf(System.currentTimeMillis())); |
||||
props.setTaskInstanceId(1); |
||||
props.setTenantCode("1"); |
||||
props.setEnvFile(".dolphinscheduler_env.sh"); |
||||
props.setTaskStartTime(new Date()); |
||||
props.setTaskTimeout(0); |
||||
props.setTaskParams( |
||||
"{\"localParams\":[],\"type\":\"POSTGRESQL\",\"datasource\":1,\"method\":\"add\"}"); |
||||
|
||||
taskExecutionContext = PowerMockito.mock(TaskExecutionContext.class); |
||||
PowerMockito.when(taskExecutionContext.getTaskParams()).thenReturn(props.getTaskParams()); |
||||
PowerMockito.when(taskExecutionContext.getExecutePath()).thenReturn("/tmp"); |
||||
PowerMockito.when(taskExecutionContext.getTaskAppId()).thenReturn("1"); |
||||
PowerMockito.when(taskExecutionContext.getTenantCode()).thenReturn("root"); |
||||
PowerMockito.when(taskExecutionContext.getStartTime()).thenReturn(new Date()); |
||||
PowerMockito.when(taskExecutionContext.getTaskTimeout()).thenReturn(10000); |
||||
PowerMockito.when(taskExecutionContext.getLogPath()).thenReturn("/tmp/dx"); |
||||
|
||||
ProcedureTaskExecutionContext procedureTaskExecutionContext = new ProcedureTaskExecutionContext(); |
||||
procedureTaskExecutionContext.setConnectionParams(CONNECTION_PARAMS); |
||||
PowerMockito.when(taskExecutionContext.getProcedureTaskExecutionContext()).thenReturn(procedureTaskExecutionContext); |
||||
|
||||
procedureTask = new ProcedureTask(taskExecutionContext, logger); |
||||
procedureTask.init(); |
||||
} |
||||
|
||||
@Test |
||||
public void testGetParameters() { |
||||
Assert.assertNotNull(procedureTask.getParameters()); |
||||
} |
||||
|
||||
@Test |
||||
public void testHandle() throws SQLException { |
||||
|
||||
Connection connection = PowerMockito.mock(Connection.class); |
||||
PowerMockito.mockStatic(DriverManager.class); |
||||
PowerMockito.when(DriverManager.getConnection(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(connection); |
||||
CallableStatement callableStatement = PowerMockito.mock(CallableStatement.class); |
||||
PowerMockito.when(connection.prepareCall(Mockito.any())).thenReturn(callableStatement); |
||||
try { |
||||
procedureTask.handle(); |
||||
Assert.assertEquals(Constants.EXIT_CODE_SUCCESS,procedureTask.getExitStatusCode()); |
||||
} catch (Exception e) { |
||||
Assert.fail(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue