|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.dolphinscheduler.plugin.task.api.parser; |
| 18 | + |
| 19 | +import org.apache.commons.lang3.StringUtils; |
| 20 | +import org.apache.commons.lang3.tuple.ImmutablePair; |
| 21 | +import org.apache.commons.lang3.tuple.Pair; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import javax.annotation.concurrent.NotThreadSafe; |
| 29 | + |
| 30 | +import lombok.extern.slf4j.Slf4j; |
| 31 | + |
| 32 | +/** |
| 33 | + * Used to parse ${setValue()} and #{setValue()} from given lines. |
| 34 | + */ |
| 35 | +@Slf4j |
| 36 | +@NotThreadSafe |
| 37 | +public class TaskOutputParameterParser { |
| 38 | + |
| 39 | + // Used to avoid '${setValue(' which loss the end of ')}' |
| 40 | + private final int maxOneParameterRows; |
| 41 | + |
| 42 | + // Used to avoid '${setValue(' which length is too long, this may case OOM |
| 43 | + private final int maxOneParameterLength; |
| 44 | + |
| 45 | + private final Map<String, String> taskOutputParams; |
| 46 | + |
| 47 | + private List<String> currentTaskOutputParam; |
| 48 | + |
| 49 | + private long currentTaskOutputParamLength; |
| 50 | + |
| 51 | + public TaskOutputParameterParser() { |
| 52 | + // the default max rows of one parameter is 1024, this should be enough |
| 53 | + this(1024, Integer.MAX_VALUE); |
| 54 | + } |
| 55 | + |
| 56 | + public TaskOutputParameterParser(int maxOneParameterRows, int maxOneParameterLength) { |
| 57 | + this.maxOneParameterRows = maxOneParameterRows; |
| 58 | + this.maxOneParameterLength = maxOneParameterLength; |
| 59 | + this.taskOutputParams = new HashMap<>(); |
| 60 | + this.currentTaskOutputParam = null; |
| 61 | + this.currentTaskOutputParamLength = 0; |
| 62 | + } |
| 63 | + |
| 64 | + public void appendParseLog(String logLine) { |
| 65 | + if (logLine == null) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (currentTaskOutputParam != null) { |
| 70 | + if (currentTaskOutputParam.size() > maxOneParameterRows |
| 71 | + || currentTaskOutputParamLength > maxOneParameterLength) { |
| 72 | + log.warn( |
| 73 | + "The output param expression '{}' is too long, the max rows is {}, max length is {}, will skip this param", |
| 74 | + String.join("\n", currentTaskOutputParam), maxOneParameterLength, maxOneParameterRows); |
| 75 | + currentTaskOutputParam = null; |
| 76 | + currentTaskOutputParamLength = 0; |
| 77 | + return; |
| 78 | + } |
| 79 | + // continue to parse the rest of line |
| 80 | + int i = logLine.indexOf(")}"); |
| 81 | + if (i == -1) { |
| 82 | + // the end of var pool not found |
| 83 | + currentTaskOutputParam.add(logLine); |
| 84 | + currentTaskOutputParamLength += logLine.length(); |
| 85 | + } else { |
| 86 | + // the end of var pool found |
| 87 | + currentTaskOutputParam.add(logLine.substring(0, i + 2)); |
| 88 | + Pair<String, String> keyValue = parseOutputParam(String.join("\n", currentTaskOutputParam)); |
| 89 | + if (keyValue.getKey() != null && keyValue.getValue() != null) { |
| 90 | + taskOutputParams.put(keyValue.getKey(), keyValue.getValue()); |
| 91 | + } |
| 92 | + currentTaskOutputParam = null; |
| 93 | + currentTaskOutputParamLength = 0; |
| 94 | + // continue to parse the rest of line |
| 95 | + if (i + 2 != logLine.length()) { |
| 96 | + appendParseLog(logLine.substring(i + 2)); |
| 97 | + } |
| 98 | + } |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + int indexOfVarPoolBegin = logLine.indexOf("${setValue("); |
| 103 | + if (indexOfVarPoolBegin == -1) { |
| 104 | + indexOfVarPoolBegin = logLine.indexOf("#{setValue("); |
| 105 | + } |
| 106 | + if (indexOfVarPoolBegin == -1) { |
| 107 | + return; |
| 108 | + } |
| 109 | + currentTaskOutputParam = new ArrayList<>(); |
| 110 | + appendParseLog(logLine.substring(indexOfVarPoolBegin)); |
| 111 | + } |
| 112 | + |
| 113 | + public Map<String, String> getTaskOutputParams() { |
| 114 | + return taskOutputParams; |
| 115 | + } |
| 116 | + |
| 117 | + // #{setValue(xx=xx)} |
| 118 | + protected Pair<String, String> parseOutputParam(String outputParam) { |
| 119 | + if (StringUtils.isEmpty(outputParam)) { |
| 120 | + log.info("The task output param is empty"); |
| 121 | + return ImmutablePair.nullPair(); |
| 122 | + } |
| 123 | + if ((!outputParam.startsWith("${setValue(") && !outputParam.startsWith("#{setValue(")) |
| 124 | + || !outputParam.endsWith(")}")) { |
| 125 | + log.info("The task output param {} should start with '${setValue(' or '#{setValue(' and end with ')}'", |
| 126 | + outputParam); |
| 127 | + return ImmutablePair.nullPair(); |
| 128 | + } |
| 129 | + String keyValueExpression = outputParam.substring(11, outputParam.length() - 2); |
| 130 | + if (!keyValueExpression.contains("=")) { |
| 131 | + log.warn("The task output param {} should composite with key=value", outputParam); |
| 132 | + return ImmutablePair.nullPair(); |
| 133 | + } |
| 134 | + |
| 135 | + String[] keyValue = keyValueExpression.split("=", 2); |
| 136 | + return ImmutablePair.of(keyValue[0], keyValue[1]); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments