Skip to content

Commit

Permalink
mr offical 421
Browse files Browse the repository at this point in the history
  • Loading branch information
krihy committed Sep 5, 2023
2 parents 2aa9201 + aff12d9 commit 83af267
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 54 deletions.
16 changes: 16 additions & 0 deletions .secignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@
http://xxx.xxx.xxx.xxx:xxxx*
test@email.com
https://tools.ietf.org*
https://obodc-front.oss-cn-beijing.aliyuncs.com/ODC*
https://discord.gg*
https://qr.dingtalk.com/action/joingroup*
https://help.github.com*
https://npmmirror.com*
https://www.contributor-covenant.org*
https://www.conventionalcommits*
https://plugins.jetbrains.com
*@oceanbase.com
https://www.jetbrains.com*
https://plugins.jetbrains.com*
http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/oceanbase/odc*
http://pmd.sourceforge.net*
https://pmd.github.io*
http://ns.adobe.com*


--------------------------------------------------------
# Should use GLOB wildcard to configure and analysis the ignored folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import javax.validation.constraints.NotEmpty;

Expand Down Expand Up @@ -131,7 +132,8 @@ public List<DBDatabase> listDatabases() {
item.setCharset(charset.get());
item.setCollation(collation.get());
});
return databases;
return databases.stream().filter(database -> !ESCAPE_USER_SET.contains(database.getName()))
.collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ public class OracleSchemaAccessor implements DBSchemaAccessor {
"COMMENT ON TABLE ${schemaName}.${tableName} IS ${comment}";
private static final String ORACLE_COLUMN_COMMENT_DDL_TEMPLATE =
"COMMENT ON COLUMN ${schemaName}.${tableName}.${columnName} IS ${comment}";
private static final Set<String> ESCAPE_USER_SET = new HashSet<>(3);
protected static final Set<String> ESCAPE_USER_SET = new HashSet<>(3);

static {
ESCAPE_USER_SET.add("PUBLIC");
ESCAPE_USER_SET.add("LBACSYS");
ESCAPE_USER_SET.add("ORAAUDITOR");
ESCAPE_USER_SET.add("__public");
}
protected OracleDataDictTableNames dataDictTableNames;
protected JdbcOperations jdbcOperations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.oceanbase.odc.service.onlineschemachange;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
Expand All @@ -28,6 +29,7 @@
import com.oceanbase.odc.core.session.ConnectionSession;
import com.oceanbase.odc.core.session.ConnectionSessionConstants;
import com.oceanbase.odc.core.shared.constant.ConnectType;
import com.oceanbase.odc.core.shared.constant.ErrorCodes;
import com.oceanbase.odc.core.shared.exception.BadArgumentException;
import com.oceanbase.odc.core.shared.exception.UnsupportedException;
import com.oceanbase.odc.core.sql.execute.SyncJdbcExecutor;
Expand Down Expand Up @@ -70,7 +72,9 @@ public void setUp() {

@After
public void tearDown() {
session.getSyncJdbcExecutor(ConnectionSessionConstants.CONSOLE_DS_KEY).execute(DROP_STMT);
if (session != null) {
session.getSyncJdbcExecutor(ConnectionSessionConstants.CONSOLE_DS_KEY).execute(DROP_STMT);
}
}

@Test
Expand Down Expand Up @@ -101,6 +105,19 @@ public void test_Validate_Alter_Failed() {
OnlineSchemaChangeSqlType.CREATE));
}

@Test(expected = BadArgumentException.class)
public void test_Validate_Invalid_Sql() {
String sql = " CREATE TABLE \"ABC10_OSC_NEW_111\" (\n \"COL\" NUMBER(38) DEFAULT NULL";
try {
validService.validate(getCreateRequest(
sql,
OnlineSchemaChangeSqlType.CREATE));
} catch (BadArgumentException ex) {
Assert.assertSame(ex.getErrorCode(), ErrorCodes.ObPreCheckDdlFailed);
throw ex;
}
}

@Test
public void TestUniqueNotNullOBMySql_Successfully() {
String createSql = "CREATE TABLE `not_null_unique_key` (\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@
*/
package com.oceanbase.odc.service.onlineschemachange.ddl;

import java.io.StringReader;
import java.util.List;
import java.util.Optional;

import org.junit.Assert;
import org.junit.Test;

import com.oceanbase.tools.sqlparser.OBMySQLParser;
import com.oceanbase.tools.sqlparser.OBOracleSQLParser;
import com.oceanbase.tools.sqlparser.statement.Statement;
import com.oceanbase.tools.sqlparser.statement.createtable.CreateTable;
import com.oceanbase.tools.sqlparser.statement.createtable.OutOfLineConstraint;
import com.oceanbase.tools.sqlparser.statement.createtable.OutOfLineForeignConstraint;

public class TableNameReplacerTest {
private static final String CREATE_STMT = "create table t1 (id int);";
private static final String CREATE_QUOTE_STMT = "create table \"t1\" (id int);";
Expand All @@ -44,6 +55,68 @@ public void test_RewriteCreateStmt_Oracle() {
Assert.assertEquals("create table t1_osc_new_ (id int);", newSql);
}

@Test
public void test_RewriteCreateStmtWittConstraint_Oracle() {
String createSql = "CREATE TABLE CHILD_TABLE1 (\n"
+ "COL NUMBER NOT NULL,\n"
+ "COL1 NUMBER NOT NULL,\n"
+ "CONSTRAINT P1 PRIMARY KEY (COL),\n"
+ "CONSTRAINT U1 UNIQUE (COL1),\n"
+ "CONSTRAINT F1 FOREIGN KEY (COL) REFERENCES PARENT_TABLE1 (COL) ON DELETE CASCADE \n"
+ ")";
String newSql = new OBOracleTableNameReplacer().replaceCreateStmt(createSql, "CHILD_TABLE_NEW");
Statement statement = new OBOracleSQLParser().parse(new StringReader(newSql));
Assert.assertTrue(statement instanceof CreateTable);
CreateTable createTable = (CreateTable) statement;
List<OutOfLineConstraint> constraints = createTable.getConstraints();
Optional<OutOfLineConstraint> pk = constraints.stream().filter(OutOfLineConstraint::isPrimaryKey).findFirst();
Assert.assertTrue(pk.isPresent());
Assert.assertNotEquals("P1", pk.get().getConstraintName());

Optional<OutOfLineConstraint> uk = constraints.stream().filter(OutOfLineConstraint::isUniqueKey).findFirst();
Assert.assertTrue(uk.isPresent());
Assert.assertNotEquals("U1", uk.get().getConstraintName());

Optional<OutOfLineForeignConstraint> fk =
constraints.stream().filter(c -> (c instanceof OutOfLineForeignConstraint))
.map(c -> (OutOfLineForeignConstraint) c).findFirst();
Assert.assertTrue(fk.isPresent());
Assert.assertNotEquals("F1", pk.get().getConstraintName());
}



@Test
public void test_RewriteCreateStmtWittConstraint_MySql() {
String createSql = "CREATE TABLE `child_table1` (\n"
+ "`col` int NOT NULL,\n"
+ "`col1` int NOT NULL,\n"
+ "CONSTRAINT `p1` PRIMARY KEY (`col`),\n"
+ "CONSTRAINT `u1` UNIQUE (`col`),\n"
+ "UNIQUE (`col`),\n"
+ "CONSTRAINT `f1` FOREIGN KEY (`col`) REFERENCES `parent_table1` (`col`) ON DELETE CASCADE ON "
+ "UPDATE NO ACTION\n"
+ ")\n";
String newSql = new OBMysqlTableNameReplacer().replaceCreateStmt(createSql, "`child_table1_new`");
Statement statement = new OBMySQLParser().parse(new StringReader(newSql));
Assert.assertTrue(statement instanceof CreateTable);
CreateTable createTable = (CreateTable) statement;
List<OutOfLineConstraint> constraints = createTable.getConstraints();
Optional<OutOfLineConstraint> pk = constraints.stream().filter(OutOfLineConstraint::isPrimaryKey).findFirst();
Assert.assertTrue(pk.isPresent());
Assert.assertEquals("`p1`", pk.get().getConstraintName());

Optional<OutOfLineConstraint> uk = constraints.stream().filter(OutOfLineConstraint::isUniqueKey).findFirst();
Assert.assertTrue(uk.isPresent());
Assert.assertEquals("`u1`", uk.get().getConstraintName());

Optional<OutOfLineForeignConstraint> fk =
constraints.stream().filter(c -> (c instanceof OutOfLineForeignConstraint))
.map(c -> (OutOfLineForeignConstraint) c).findFirst();
Assert.assertTrue(fk.isPresent());
Assert.assertNotEquals("`f1`", pk.get().getConstraintName());
}

@Test
public void test_RewriteCreateStmtWithQuote_Oracle() {
String newSql = new OBOracleTableNameReplacer().replaceCreateStmt(CREATE_QUOTE_STMT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ public class OdcDBSession {
private String svrIp;

public static OdcDBSession from(DBSession dbSession) {
OdcDBSession odcDBSession = new OdcDBSession();
odcDBSession.setSessionId(Long.parseLong(dbSession.getId()));
odcDBSession.setDbUser(dbSession.getUsername());
odcDBSession.setSrcIp(dbSession.getHost());
odcDBSession.setDatabase(dbSession.getDatabaseName());
odcDBSession.setCommand(dbSession.getCommand());
odcDBSession.setExecuteTime(dbSession.getExecuteTime());
odcDBSession.setStatus(dbSession.getState());
odcDBSession.setObproxyIp(dbSession.getProxyHost());
return odcDBSession;
OdcDBSession session = new OdcDBSession();
session.setSessionId(Long.parseLong(dbSession.getId()));
session.setDbUser(dbSession.getUsername());
session.setSrcIp(dbSession.getHost());
session.setDatabase(dbSession.getDatabaseName());
session.setCommand(dbSession.getCommand());
session.setExecuteTime(dbSession.getExecuteTime());
session.setStatus(dbSession.getState());
session.setObproxyIp(dbSession.getProxyHost());
session.setSql(dbSession.getLatestQueries());
return session;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#scenario of https://aone.alipay.com/v2/project/874455/bug/39224304
origin: |-
select 'hello \\world' as col1 from dual;
insert into table_1 values ('sadasd');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,17 @@ private FlowInstanceDetailResp buildWithoutApprovalNode(CreateFlowInstanceReq fl
strategyConfig);
taskInstance.setTargetTaskId(taskEntity.getId());
taskInstance.update();
FlowInstanceConfigurer taskConfigurer = flowInstance.newFlowInstance().next(taskInstance);
TaskParameters parameters = flowInstanceReq.getParameters();
FlowInstanceConfigurer taskConfigurer;
if (taskType == TaskType.ASYNC
&& Boolean.TRUE.equals(((DatabaseChangeParameters) parameters).getGenerateRollbackPlan())) {
FlowTaskInstance rollbackPlanInstance =
flowFactory.generateFlowTaskInstance(flowInstance.getId(), false, false,
TaskType.GENERATE_ROLLBACK, ExecutionStrategyConfig.autoStrategy());
taskConfigurer = flowInstance.newFlowInstance().next(rollbackPlanInstance).next(taskInstance);
} else {
taskConfigurer = flowInstance.newFlowInstance().next(taskInstance);
}
taskConfigurer.endFlowInstance();
flowInstance.buildTopology();

Expand Down Expand Up @@ -700,6 +710,7 @@ private FlowInstanceDetailResp buildFlowInstance(List<RiskLevel> riskLevels,
flowInstance.dealloc();
}
Map<String, Object> variables = new HashMap<>();
FlowTaskUtil.setFlowInstanceId(variables, flowInstance.getId());
FlowTaskUtil.setTemplateVariables(variables, buildTemplateVariables(flowInstanceReq, connectionConfig));
initVariables(variables, taskEntity, preCheckTaskEntity, connectionConfig,
buildRiskLevelDescriber(flowInstanceReq));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ protected FlowInstanceConfigurer next(@NonNull FlowApprovalInstance nextNode,
ExclusiveGatewayBuilder gatewayBuilder = nullSafeGetNodeBuilder(gatewayName, nextNode,
() -> new ExclusiveGatewayBuilder(gatewayName));
targetExecution.next(serviceTaskBuilder).next(gatewayBuilder);
String expr = RuntimeTaskConstants.SUCCESS_CREATE_EXT_INS + "_" + nextNode.getId();
targetExecution.route(String.format("${!%s}", expr), this.targetProcessBuilder.endProcess());
targetExecution.route(String.format("${!%s}", RuntimeTaskConstants.SUCCESS_CREATE_EXT_INS),
this.targetProcessBuilder.endProcess());
targetExecution.next(userTaskBuilder, new ConditionSequenceFlowBuilder(
gatewayBuilder.getGraphId() + " -> " + serviceTaskBuilder.getGraphId(),
String.format("${%s}", expr)));
String.format("${%s}", RuntimeTaskConstants.SUCCESS_CREATE_EXT_INS)));
} else {
targetExecution.next(userTaskBuilder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package com.oceanbase.odc.service.flow.task;

import java.util.Objects;
import java.util.Optional;

import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.beans.factory.annotation.Autowired;

import com.oceanbase.odc.common.util.RetryExecutor;
import com.oceanbase.odc.core.flow.BaseFlowableDelegate;
import com.oceanbase.odc.core.shared.Verify;
import com.oceanbase.odc.core.shared.constant.FlowStatus;
import com.oceanbase.odc.metadb.flow.FlowInstanceRepository;
import com.oceanbase.odc.service.flow.FlowableAdaptor;
Expand Down Expand Up @@ -62,31 +62,39 @@ protected void run(DelegateExecution execution) throws Exception {
try {
flowApprovalInstance = getFlowApprovalInstance(execution);
} catch (Exception e) {
log.warn("Get flow approval instance failed, activityId={}, processDefinitionId={}",
log.warn(
"Get flow approval instance failed, the flow instance is coming to an end, activityId={}, processDefinitionId={}",
execution.getCurrentActivityId(), execution.getProcessDefinitionId(), e);
try {
flowInstanceRepository.updateStatusById(FlowTaskUtil.getFlowInstanceId(execution),
FlowStatus.EXECUTION_FAILED);
} finally {
execution.setVariable(RuntimeTaskConstants.SUCCESS_CREATE_EXT_INS, false);
}
return;
}
Long externalApprovalId = flowApprovalInstance.getExternalApprovalId();
String expr = RuntimeTaskConstants.SUCCESS_CREATE_EXT_INS + "_" + flowApprovalInstance.getId();
if (Objects.nonNull(externalApprovalId)) {
try {
Verify.notNull(externalApprovalId, "externalApprovalId");
IntegrationConfig config = integrationService.detailWithoutPermissionCheck(externalApprovalId);
ApprovalProperties properties = ApprovalProperties.from(config);
TemplateVariables variables = FlowTaskUtil.getTemplateVariables(execution.getVariables());
String externalFlowInstanceId = approvalClient.start(properties, variables);
flowApprovalInstance.setExternalFlowInstanceId(externalFlowInstanceId);
flowApprovalInstance.update();
execution.setVariable(RuntimeTaskConstants.SUCCESS_CREATE_EXT_INS, true);
} catch (Exception e) {
log.warn("Create external approval instance failed, the flow instance is coming to an end, "
+ "flowApprovalInstanceId={}, externalApprovalId={}",
flowApprovalInstance.getId(), externalApprovalId, e);
try {
IntegrationConfig config = integrationService.detailWithoutPermissionCheck(externalApprovalId);
ApprovalProperties properties = ApprovalProperties.from(config);
TemplateVariables variables = FlowTaskUtil.getTemplateVariables(execution.getVariables());
String externalFlowInstanceId = approvalClient.start(properties, variables);
flowApprovalInstance.setExternalFlowInstanceId(externalFlowInstanceId);
flowApprovalInstance.update();
execution.setVariable(expr, true);
} catch (Exception e) {
log.warn("Create external approval instance failed, the flow instance is coming to an end, "
+ "flowApprovalInstanceId={}, externalApprovalId={}",
flowApprovalInstance.getId(), externalApprovalId, e);
flowApprovalInstance.setStatus(FlowNodeStatus.FAILED);
flowApprovalInstance.setComment(e.getLocalizedMessage());
flowApprovalInstance.update();
flowInstanceRepository.updateStatusById(flowApprovalInstance.getFlowInstanceId(),
FlowStatus.EXECUTION_FAILED);
execution.setVariable(expr, false);
} finally {
execution.setVariable(RuntimeTaskConstants.SUCCESS_CREATE_EXT_INS, false);
}
}
}
Expand All @@ -97,8 +105,7 @@ private FlowApprovalInstance getFlowApprovalInstance(DelegateExecution execution
Optional<Optional<Long>> flowInstanceIdOpt = retryExecutor.run(
() -> flowableAdaptor.getFlowInstanceIdByProcessDefinitionId(processDefinitionId), Optional::isPresent);
if (!flowInstanceIdOpt.isPresent() || !flowInstanceIdOpt.get().isPresent()) {
log.warn("Flow instance id does not exist, activityId={}, processDefinitionId={}", activityId,
processDefinitionId);
log.warn("Flow instance id does not exist, processDefinitionId={}", processDefinitionId);
throw new IllegalStateException(
"Can not find flow instance id by process definition id " + processDefinitionId);
}
Expand All @@ -108,7 +115,7 @@ private FlowApprovalInstance getFlowApprovalInstance(DelegateExecution execution
if (!instanceOpt.isPresent()) {
log.warn("Flow approval instance does not exist, activityId={}, flowInstanceId={}", activityId,
flowInstanceId);
throw new IllegalStateException("Can not find instance by activityId " + activityId);
throw new IllegalStateException("Can not find flow approval instance by activityId " + activityId);
}
return instanceOpt.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class RuntimeTaskConstants {
public static final String PRE_CHECK_TASK_ID = "preCheckTaskId";
public static final String TASK_ID = "taskId";
public static final String FLOW_INSTANCE_ID = "flowInstanceId";
public static final String TIMEOUT_MILLI_SECONDS = "timeOutMilliSeconds";
public static final String CONNECTION_CONFIG = "connectionConfig";
public static final String SCHEMA_NAME = "schemaName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ public static ConnectionConfig getConnectionConfig(@NonNull DelegateExecution ex
() -> new VerifyException("ConnectionConfig is absent"));
}

public static void setFlowInstanceId(@NonNull Map<String, Object> variables, @NonNull Long flowInstanceId) {
variables.put(RuntimeTaskConstants.FLOW_INSTANCE_ID, flowInstanceId);
}

public static Long getFlowInstanceId(@NonNull DelegateExecution execution) {
Object value = execution.getVariables().get(RuntimeTaskConstants.FLOW_INSTANCE_ID);
return internalGet(value, Long.class).orElseThrow(() -> new VerifyException("FlowInstanceId is absent"));
}

public static void setTemplateVariables(@NonNull Map<String, Object> variables,
@NonNull TemplateVariables templateVariables) {
variables.put(RuntimeTaskConstants.INTEGRATION_TEMPLATE_VARIABLES, templateVariables);
Expand Down
Loading

0 comments on commit 83af267

Please sign in to comment.