You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@streampark.apache.org by GitBox <gi...@apache.org> on 2022/10/21 04:24:40 UTC

[GitHub] [incubator-streampark] wolfboys commented on a diff in pull request #1866: [Feature] Reference variables as placeholders in program args and Flink SQL #1781

wolfboys commented on code in PR #1866:
URL: https://github.com/apache/incubator-streampark/pull/1866#discussion_r1001347675


##########
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/VariableServiceImpl.java:
##########
@@ -79,4 +105,73 @@ public Variable findByVariableCode(Long teamId, String variableCode) {
     public List<Variable> findByTeamId(Long teamId) {
         return baseMapper.selectByTeamId(teamId);
     }
+
+    /**
+     * Replace placeholders with defined variable codes.
+     * @param teamId
+     * @param paramWithPlaceholders Parameters with placeholders, e.g. "--cluster ${kafka.cluster}"
+     * @return
+     */
+    @Override
+    public String replacePlaceholder(Long teamId, String paramWithPlaceholders) {
+        if (StringUtils.isEmpty(paramWithPlaceholders)) {
+            return paramWithPlaceholders;
+        }
+        String restore = paramWithPlaceholders;
+        Matcher matcher = placeholderPattern.matcher(paramWithPlaceholders);
+        while (matcher.find()) {
+            String placeholder = matcher.group();
+            String variableCode = getCodeFromPlaceholder(placeholder);
+            Variable variable = findByVariableCode(teamId, variableCode);
+            if (variable != null) {
+                restore = restore.replace(placeholder, variable.getVariableValue());
+            }
+        }
+        return restore;
+    }
+
+    private boolean isDependByApplications(Variable variable) {
+        // Detect whether the variable is dependent on the args of the application
+        List<Application> applications = applicationService.getByTeamId(variable.getTeamId());
+        if (applications != null) {
+            Iterator<Application> appIt = applications.iterator();
+            while (appIt.hasNext()) {
+                Application application = appIt.next();
+                if (isDepend(variable.getVariableCode(), application.getArgs())) {
+                    return true;
+                }
+            }
+        }
+
+        // Detect whether variables are dependent on all versions of flink sql
+        List<FlinkSql> sqls = flinkSqlService.getByTeamId(variable.getTeamId());
+        if (sqls != null) {
+            Iterator<FlinkSql> sqlIt = sqls.iterator();

Review Comment:
   code can be simplified too



##########
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/VariableServiceImpl.java:
##########
@@ -79,4 +105,73 @@ public Variable findByVariableCode(Long teamId, String variableCode) {
     public List<Variable> findByTeamId(Long teamId) {
         return baseMapper.selectByTeamId(teamId);
     }
+
+    /**
+     * Replace placeholders with defined variable codes.
+     * @param teamId
+     * @param paramWithPlaceholders Parameters with placeholders, e.g. "--cluster ${kafka.cluster}"
+     * @return
+     */
+    @Override
+    public String replacePlaceholder(Long teamId, String paramWithPlaceholders) {
+        if (StringUtils.isEmpty(paramWithPlaceholders)) {
+            return paramWithPlaceholders;
+        }
+        String restore = paramWithPlaceholders;
+        Matcher matcher = placeholderPattern.matcher(paramWithPlaceholders);
+        while (matcher.find()) {
+            String placeholder = matcher.group();
+            String variableCode = getCodeFromPlaceholder(placeholder);
+            Variable variable = findByVariableCode(teamId, variableCode);
+            if (variable != null) {
+                restore = restore.replace(placeholder, variable.getVariableValue());
+            }
+        }
+        return restore;
+    }
+
+    private boolean isDependByApplications(Variable variable) {
+        // Detect whether the variable is dependent on the args of the application
+        List<Application> applications = applicationService.getByTeamId(variable.getTeamId());
+        if (applications != null) {
+            Iterator<Application> appIt = applications.iterator();
+            while (appIt.hasNext()) {
+                Application application = appIt.next();
+                if (isDepend(variable.getVariableCode(), application.getArgs())) {
+                    return true;
+                }
+            }

Review Comment:
   code(line 137 ~ 143) can be simplified:
   ```
   for(Application app: applications) {
         if (isDepend(variable.getVariableCode(), app.getArgs())) {
             return true;
         }
     }
     ```



##########
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/VariableServiceImpl.java:
##########
@@ -17,48 +17,74 @@
 
 package org.apache.streampark.console.core.service.impl;
 
+import org.apache.streampark.common.util.DeflaterUtils;
 import org.apache.streampark.console.base.domain.RestRequest;
 import org.apache.streampark.console.base.exception.ApiAlertException;
 import org.apache.streampark.console.base.mybatis.pager.MybatisPager;
+import org.apache.streampark.console.core.entity.Application;
+import org.apache.streampark.console.core.entity.FlinkSql;
 import org.apache.streampark.console.core.entity.Variable;
 import org.apache.streampark.console.core.mapper.VariableMapper;
 import org.apache.streampark.console.core.service.ApplicationService;
 import org.apache.streampark.console.core.service.CommonService;
+import org.apache.streampark.console.core.service.FlinkSqlService;
 import org.apache.streampark.console.core.service.VariableService;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.Iterator;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 @Slf4j
 @Service
 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
 public class VariableServiceImpl extends ServiceImpl<VariableMapper, Variable> implements VariableService {
 
+    private final Pattern placeholderPattern = Pattern.compile("\\$\\{([A-Za-z])+([A-Za-z0-9._-])+\\}");
+
+    private final String placeholderLeft = "${";
+
+    private final String placeholderRight  = "}";

Review Comment:
   how about: placeholderEnd?



##########
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/VariableServiceImpl.java:
##########
@@ -17,48 +17,74 @@
 
 package org.apache.streampark.console.core.service.impl;
 
+import org.apache.streampark.common.util.DeflaterUtils;
 import org.apache.streampark.console.base.domain.RestRequest;
 import org.apache.streampark.console.base.exception.ApiAlertException;
 import org.apache.streampark.console.base.mybatis.pager.MybatisPager;
+import org.apache.streampark.console.core.entity.Application;
+import org.apache.streampark.console.core.entity.FlinkSql;
 import org.apache.streampark.console.core.entity.Variable;
 import org.apache.streampark.console.core.mapper.VariableMapper;
 import org.apache.streampark.console.core.service.ApplicationService;
 import org.apache.streampark.console.core.service.CommonService;
+import org.apache.streampark.console.core.service.FlinkSqlService;
 import org.apache.streampark.console.core.service.VariableService;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.Iterator;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 @Slf4j
 @Service
 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
 public class VariableServiceImpl extends ServiceImpl<VariableMapper, Variable> implements VariableService {
 
+    private final Pattern placeholderPattern = Pattern.compile("\\$\\{([A-Za-z])+([A-Za-z0-9._-])+\\}");
+
+    private final String placeholderLeft = "${";

Review Comment:
   how about: placeholderStart?



##########
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/VariableServiceImpl.java:
##########
@@ -79,4 +105,73 @@ public Variable findByVariableCode(Long teamId, String variableCode) {
     public List<Variable> findByTeamId(Long teamId) {
         return baseMapper.selectByTeamId(teamId);
     }
+
+    /**
+     * Replace placeholders with defined variable codes.
+     * @param teamId
+     * @param paramWithPlaceholders Parameters with placeholders, e.g. "--cluster ${kafka.cluster}"
+     * @return
+     */
+    @Override
+    public String replacePlaceholder(Long teamId, String paramWithPlaceholders) {

Review Comment:
   bad method name: `replacePlaceholder` , how about: `parseVariable` or `parse` ?



-- 
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: issues-unsubscribe@streampark.apache.org

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