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



##########
File path: 
fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java
##########
@@ -0,0 +1,233 @@
+// 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.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.io.Writable;
+import org.apache.doris.metric.MetricRepo;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.qe.ConnectContext;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import org.apache.commons.codec.digest.DigestUtils;
+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);
+
+    private Map<String, SqlBlockRule> nameToSqlBlockRuleMap = 
Maps.newConcurrentMap();
+
+    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> get(ShowSqlBlockRuleStmt stmt) throws 
AnalysisException {
+        String ruleName = stmt.getRuleName();
+        if 
(!Catalog.getCurrentCatalog().getAuth().checkGlobalPriv(ConnectContext.get(), 
PrivPredicate.ADMIN)) {
+            
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, 
"ADMIN");
+        }
+        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");
+                }
+                SqlBlockRule sqlBlockRule = 
nameToSqlBlockRuleMap.get(ruleName);
+                if (sqlBlockRule == null) {
+                    continue;
+                }
+                unprotectedDrop(sqlBlockRule);
+                
Catalog.getCurrentCatalog().getEditLog().logDropSqlBlockRule(sqlBlockRule);
+            }
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    public void replayDrop(SqlBlockRule sqlBlockRule) {
+        unprotectedDrop(sqlBlockRule);
+        LOG.info("replay drop sql block rule: {}", sqlBlockRule);
+    }
+
+    public void unprotectedDrop(SqlBlockRule sqlBlockRule) {
+        nameToSqlBlockRuleMap.remove(sqlBlockRule.getName());
+        // todo: remove UserProperty
+    }
+
+    public void matchSql(String sql, String user) throws AnalysisException {
+        // match global rule
+        List<SqlBlockRule> globalRules = 
nameToSqlBlockRuleMap.values().stream().filter(SqlBlockRule::getGlobal).collect(Collectors.toList());
+        for (SqlBlockRule rule : globalRules) {
+            Pattern sqlPattern = sqlPatternMap.get(rule.getSql());
+            matchSql(rule, sql, sqlPattern);
+        }
+        // match user rule
+        String binSqlBlockRules = 
Catalog.getCurrentCatalog().getAuth().getBindSqlBlockRules(user);
+        if (StringUtils.isNotEmpty(binSqlBlockRules)) {
+            String[] split = binSqlBlockRules.split(",");
+            for (String ruleName : split) {
+                SqlBlockRule rule = nameToSqlBlockRuleMap.get(ruleName);
+                Pattern sqlPattern = sqlPatternMap.get(rule.getSql());
+                matchSql(rule, sql, sqlPattern);
+            }
+        }
+    }
+
+    @VisibleForTesting
+    public static void matchSql(SqlBlockRule rule, String sql, Pattern 
sqlPattern) throws AnalysisException {
+        if (rule.getEnable() != null && rule.getEnable()) {
+            String sqlHash = rule.getSqlHash();
+            if (sqlHash != null && sqlHash.equals(DigestUtils.md5Hex(sql))) {
+                MetricRepo.COUNTER_HIT_SQL_BLOCK_RULE.increase(1L);
+                throw new AnalysisException("sql match hash sql block rule: " 
+ rule.getName());
+            }

Review comment:
       Yes, only one is allowed. I'll change this later




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

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



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

Reply via email to