You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2021/08/08 14:22:50 UTC

[GitHub] [incubator-doris] morningman commented on a change in pull request #6192: ADD: support sql block rule

morningman commented on a change in pull request #6192:
URL: https://github.com/apache/incubator-doris/pull/6192#discussion_r684780556



##########
File path: fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java
##########
@@ -0,0 +1,222 @@
+// 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.doris.blockrule;
+
+import org.apache.doris.analysis.AlterSqlBlockRuleStmt;
+import org.apache.doris.analysis.CreateSqlBlockRuleStmt;
+import org.apache.doris.analysis.DropSqlBlockRuleStmt;
+import org.apache.doris.analysis.ShowSqlBlockRuleStmt;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.io.Text;
+import org.apache.doris.common.io.Writable;
+import org.apache.doris.metric.MetricRepo;
+import org.apache.doris.persist.gson.GsonUtils;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.gson.annotations.SerializedName;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class SqlBlockRuleMgr implements Writable {
+    private static final Logger LOG = LogManager.getLogger(SqlBlockRuleMgr.class);
+
+    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
+
+    @SerializedName(value = "nameToSqlBlockRuleMap")
+    private Map<String, SqlBlockRule> nameToSqlBlockRuleMap = Maps.newConcurrentMap();
+
+    @SerializedName(value = "sqlPatternMap")
+    private Map<String, Pattern> sqlPatternMap = Maps.newConcurrentMap();

Review comment:
       Are you sure the `Pattern` can be serialized by Json?
   And I think the `sqlPatternMap` is useless, why not just save the pattern in each `SqlBlockRule`?

##########
File path: fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java
##########
@@ -1882,6 +1885,14 @@ public long loadSmallFiles(DataInputStream in, long checksum) throws IOException
         return checksum;
     }
 
+    public long loadSqlBlockRule(DataInputStream in, long checksum) throws IOException {
+        if (Catalog.getCurrentCatalogJournalVersion() >= FeMetaVersion.VERSION_103) {
+            sqlBlockRuleMgr.readFields(in);

Review comment:
       ```suggestion
               sqlBlockRuleMgr = SqlBlockRuleMgr.read(in); 
   ```

##########
File path: fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java
##########
@@ -0,0 +1,222 @@
+// 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.doris.blockrule;
+
+import org.apache.doris.analysis.AlterSqlBlockRuleStmt;
+import org.apache.doris.analysis.CreateSqlBlockRuleStmt;
+import org.apache.doris.analysis.DropSqlBlockRuleStmt;
+import org.apache.doris.analysis.ShowSqlBlockRuleStmt;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.io.Text;
+import org.apache.doris.common.io.Writable;
+import org.apache.doris.metric.MetricRepo;
+import org.apache.doris.persist.gson.GsonUtils;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.gson.annotations.SerializedName;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class SqlBlockRuleMgr implements Writable {
+    private static final Logger LOG = LogManager.getLogger(SqlBlockRuleMgr.class);
+
+    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
+
+    @SerializedName(value = "nameToSqlBlockRuleMap")
+    private Map<String, SqlBlockRule> nameToSqlBlockRuleMap = Maps.newConcurrentMap();
+
+    @SerializedName(value = "sqlPatternMap")
+    private Map<String, Pattern> sqlPatternMap = Maps.newConcurrentMap();
+
+    private void writeLock() {
+        lock.writeLock().lock();
+    }
+
+    private void writeUnlock() {
+        lock.writeLock().unlock();
+    }
+
+    public boolean existRule(String name) {
+        return nameToSqlBlockRuleMap.containsKey(name);
+    }
+
+    public List<SqlBlockRule> getSqlBlockRule(ShowSqlBlockRuleStmt stmt) throws AnalysisException {
+        String ruleName = stmt.getRuleName();
+        if (StringUtils.isNotEmpty(ruleName)) {
+            if (nameToSqlBlockRuleMap.containsKey(ruleName)) {
+                SqlBlockRule sqlBlockRule = nameToSqlBlockRuleMap.get(ruleName);
+                return Lists.newArrayList(sqlBlockRule);
+            }
+            return Lists.newArrayList();
+        }
+        return Lists.newArrayList(nameToSqlBlockRuleMap.values());
+    }
+
+    public void createSqlBlockRule(CreateSqlBlockRuleStmt stmt) throws UserException {
+        writeLock();
+        try {
+            SqlBlockRule sqlBlockRule = SqlBlockRule.fromCreateStmt(stmt);
+            String ruleName = sqlBlockRule.getName();
+            if (existRule(ruleName)) {
+                throw new DdlException("the sql block rule " + ruleName + " already create");
+            }
+            unprotectedAdd(sqlBlockRule);
+            Catalog.getCurrentCatalog().getEditLog().logCreateSqlBlockRule(sqlBlockRule);
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    public void replayCreate(SqlBlockRule sqlBlockRule) {
+        unprotectedAdd(sqlBlockRule);
+        LOG.info("replay create sql block rule: {}", sqlBlockRule);
+    }
+
+    public void alterSqlBlockRule(AlterSqlBlockRuleStmt stmt) throws DdlException {
+        writeLock();
+        try {
+            SqlBlockRule sqlBlockRule = SqlBlockRule.fromAlterStmt(stmt);
+            String ruleName = sqlBlockRule.getName();
+            if (!existRule(ruleName)) {
+                throw new DdlException("the sql block rule " + ruleName + " not exist");
+            }
+            SqlBlockRule originRule = nameToSqlBlockRuleMap.get(ruleName);
+            if (StringUtils.isEmpty(sqlBlockRule.getSql())) {
+                sqlBlockRule.setSql(originRule.getSql());
+            }
+            if (StringUtils.isEmpty(sqlBlockRule.getSqlHash())) {
+                sqlBlockRule.setSqlHash(originRule.getSqlHash());
+            }
+            if (sqlBlockRule.getGlobal() == null) {
+                sqlBlockRule.setGlobal(originRule.getGlobal());
+            }
+            if (sqlBlockRule.getEnable() == null) {
+                sqlBlockRule.setEnable(originRule.getEnable());
+            }
+            unprotectedUpdate(sqlBlockRule);
+            Catalog.getCurrentCatalog().getEditLog().logAlterSqlBlockRule(sqlBlockRule);
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    public void replayAlter(SqlBlockRule sqlBlockRule) {
+        unprotectedUpdate(sqlBlockRule);
+        LOG.info("replay alter sql block rule: {}", sqlBlockRule);
+    }
+
+    public void unprotectedUpdate(SqlBlockRule sqlBlockRule) {
+        nameToSqlBlockRuleMap.put(sqlBlockRule.getName(), sqlBlockRule);
+    }
+
+    public void unprotectedAdd(SqlBlockRule sqlBlockRule) {
+        nameToSqlBlockRuleMap.put(sqlBlockRule.getName(), sqlBlockRule);
+        String sql = sqlBlockRule.getSql();
+        if (StringUtils.isNotEmpty(sql)) {
+            sqlPatternMap.put(sql, Pattern.compile(sql));
+        }
+    }
+
+    public void dropSqlBlockRule(DropSqlBlockRuleStmt stmt) throws DdlException {
+        writeLock();
+        try {
+            List<String> ruleNames = stmt.getRuleNames();
+            for (String ruleName : ruleNames) {
+                if (!existRule(ruleName)) {
+                    throw new DdlException("the sql block rule " + ruleName + " not exist");
+                }
+            }
+            unprotectedDrop(ruleNames);
+            Catalog.getCurrentCatalog().getEditLog().logDropSqlBlockRule(ruleNames);
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    public void replayDrop(List<String> ruleNames) {
+        unprotectedDrop(ruleNames);
+        LOG.info("replay drop sql block ruleNames: {}", ruleNames);
+    }
+
+    public void unprotectedDrop(List<String> ruleNames) {
+        ruleNames.forEach(name -> nameToSqlBlockRuleMap.remove(name));
+    }
+
+    public void matchSql(String originSql, String sqlHash, String user) throws AnalysisException {
+        // match global rule
+        List<SqlBlockRule> globalRules = nameToSqlBlockRuleMap.values().stream().filter(SqlBlockRule::getGlobal).collect(Collectors.toList());
+        for (SqlBlockRule rule : globalRules) {
+            matchSql(rule, originSql, sqlHash);
+        }
+        // match user rule
+        String bindSqlBlockRules = Catalog.getCurrentCatalog().getAuth().getSqlBlockRules(user);
+        if (StringUtils.isNotEmpty(bindSqlBlockRules)) {
+            String[] split = bindSqlBlockRules.split(",");

Review comment:
       This `split()` should not be done every time we call `matchSql`.
   It should be prepared already.

##########
File path: fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/UserProperty.java
##########
@@ -89,12 +90,25 @@
      */
     private WhiteList whiteList = new WhiteList();
 
+    // the binding of sql_block_rule name, multiple are separated by ','
+    private String sqlBlockRules = null;

Review comment:
       This can be put into `commonProperties`

##########
File path: fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/UserProperty.java
##########
@@ -89,12 +90,25 @@
      */
     private WhiteList whiteList = new WhiteList();
 
+    // the binding of sql_block_rule name, multiple are separated by ','
+    private String sqlBlockRules = null;
+
+    @Deprecated

Review comment:
       Why adding these deprecated fields back?




-- 
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@doris.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org