Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,30 @@
import org.apache.commons.lang3.ArrayUtils;

public class ExceptionUtils extends org.apache.commons.lang3.exception.ExceptionUtils {
private static final int CAUSE_DEPTH = 3;
private static final int ROOT_CAUSE_DEPTH = 5;

public static String getSimpleReason(final Throwable throwable) {
return getCauseReason(throwable) + " ... " + getRootCauseReason(throwable);
}

public static String getCauseReason(final Throwable throwable) {
String[] stackTrace = getStackTrace(throwable).split("\n\t");
return getTraceWithMaxDepth(stackTrace, CAUSE_DEPTH);
}

public static String getRootCauseReason(final Throwable throwable) {
String[] rootCauseStackTrace = getRootCauseStackTrace(throwable);
return getTraceWithMaxDepth(rootCauseStackTrace, ROOT_CAUSE_DEPTH);
}

private static String getTraceWithMaxDepth(String[] stackTrace, int maxDepth) {
StringBuilder rootReason = new StringBuilder();
if (ArrayUtils.isNotEmpty(rootCauseStackTrace)) {
int depth = ROOT_CAUSE_DEPTH;
if (ArrayUtils.isNotEmpty(stackTrace)) {
int depth = maxDepth;
while (depth-- > 0) {
if (rootCauseStackTrace.length > depth) {
rootReason.append(" ").append(rootCauseStackTrace[depth]);
if (stackTrace.length > depth) {
rootReason.append(" ").append(stackTrace[depth]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,35 @@ public void getRootCauseReason() {
rootCauseReason = ExceptionUtils.getRootCauseReason(e);
}
Assert.assertEquals(
" \tat com.oceanbase.odc.common.util.ExceptionUtilsTest.rethrowException(ExceptionUtilsTest.java:50)"
" \tat com.oceanbase.odc.common.util.ExceptionUtilsTest.rethrowException(ExceptionUtilsTest.java:71)"
+ " [wrapped] java.lang.RuntimeException: rethrow: "
+ " \tat com.oceanbase.odc.common.util.ExceptionUtilsTest.rethrowException(ExceptionUtilsTest.java:48)"
+ " \tat com.oceanbase.odc.common.util.ExceptionUtilsTest.rethrowException(ExceptionUtilsTest.java:69)"
+ " \tat com.oceanbase.odc.common.util.ExceptionUtilsTest.lambda$null$1(ExceptionUtilsTest.java:31)"
+ " java.lang.RuntimeException: root cause",
rootCauseReason);
}

@Test
public void getCauseReason() {
String causeReason = null;
try {
rethrowException(() -> {
rethrowException(() -> {
rethrowException(() -> {
});
throw new RuntimeException("root cause");
});
});
} catch (Exception e) {
causeReason = ExceptionUtils.getCauseReason(e);
}
Assert.assertEquals(
" at com.oceanbase.odc.common.util.ExceptionUtilsTest.getCauseReason(ExceptionUtilsTest.java:50) "
+ "at com.oceanbase.odc.common.util.ExceptionUtilsTest.rethrowException(ExceptionUtilsTest.java:71) "
+ "java.lang.RuntimeException: rethrow: ",
causeReason);
}

private void rethrowException(Runnable runnable) {
try {
runnable.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public boolean isODPSharding() {
return this == ODP_SHARDING_OB_MYSQL || this == ODP_SHARDING_OB_ORACLE;
}

public boolean isDefaultSchemaRequired() {
return isODPSharding();
}

public static ConnectType from(DialectType dialectType) {
return dialectType == null ? null : ConnectType.valueOf(dialectType.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,12 @@ public ConnectionConfig innerCreate(@NotNull @Valid ConnectionConfig connection,
TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
ConnectionConfig created;
try {

environmentAdapter.adaptConfig(connection);
connectionSSLAdaptor.adapt(connection);

if (!connection.getType().isDefaultSchemaRequired()) {
connection.setDefaultSchema(null);
}
connectionValidator.validateForUpsert(connection);
connectionValidator.validatePrivateConnectionTempOnly(connection.getTemp());

Expand Down Expand Up @@ -580,6 +582,10 @@ public ConnectionConfig update(@NotNull Long id, @NotNull @Valid ConnectionConfi
connection.setTemp(saved.getTemp());
connection.setPasswordEncrypted(null);
connection.setSysTenantPasswordEncrypted(null);

if (!connection.getType().isDefaultSchemaRequired()) {
connection.setDefaultSchema(null);
}
connectionValidator.validateForUpsert(connection);
// validate same name while rename connection
repository.findByOrganizationIdAndName(connection.getOrganizationId(), connection.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ void validateForUpsert(ConnectionConfig connection) {
PreConditions.validInHostWhiteList(connection.getHost(), connectProperties.getHostWhiteList());
PreConditions.validArgumentState(environmentService.exists(connection.getEnvironmentId()),
ErrorCodes.BadRequest, null, "invalid environment id");
if (connection.getType().isDefaultSchemaRequired()) {
PreConditions.notBlank(connection.getDefaultSchema(), "connection.defaultSchema");
}
}

void validateForUpdate(ConnectionConfig connection, ConnectionConfig saved) {
Expand All @@ -58,11 +61,6 @@ void validateForUpdate(ConnectionConfig connection, ConnectionConfig saved) {
|| Objects.equals(connection.getType(), saved.getType()),
ErrorCodes.FieldUpdateNotSupported, new Object[] {"connection.type"},
"Cannot change field 'connection.type'");
PreConditions.validRequestState(
Comment thread
yizhouxw marked this conversation as resolved.
Objects.isNull(connection.getDialectType())
|| Objects.equals(connection.getDialectType(), saved.getDialectType()),
ErrorCodes.FieldUpdateNotSupported, new Object[] {"connection.dialectType"},
"Cannot change field 'connection.dialectType'");
}

void validatePrivateConnectionTempOnly(Boolean temp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ public class ConnectionConfig
* 默认 schema,对应 /api/v1 的 defaultDBName 字段
*/
@Size(max = 128, message = "Schema name is out of range [0, 128]")
@JsonIgnore
private String defaultSchema;
/**
* 查询超时时间(单位:秒),对应 /api/v1 的 sessionTimeoutS 字段
Expand Down Expand Up @@ -346,14 +345,15 @@ public ConnectEnvironmentType getConnectType() {

@JsonProperty(access = Access.READ_ONLY)
public DialectType getDialectType() {
return Objects.nonNull(this.type) ? this.type.getDialectType() : null;
return Objects.nonNull(this.type) ? this.type.getDialectType() : DialectType.UNKNOWN;
}

public String getDefaultSchema() {
DialectType dialectType = getDialectType();
if (dialectType == null) {
return this.defaultSchema;
} else if (StringUtils.isNotBlank(this.defaultSchema)) {
}
if (StringUtils.isNotBlank(this.defaultSchema)) {
return ConnectionSessionUtil.getUserOrSchemaString(this.defaultSchema, dialectType);
}
switch (dialectType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.oceanbase.odc.common.lang.Holder;
import com.oceanbase.odc.common.unit.BinarySize;
import com.oceanbase.odc.common.unit.BinarySizeUnit;
import com.oceanbase.odc.common.util.ExceptionUtils;
import com.oceanbase.odc.common.util.LogUtils;
import com.oceanbase.odc.common.util.StringUtils;
import com.oceanbase.odc.common.util.TraceStage;
Expand Down Expand Up @@ -551,7 +552,7 @@ private SqlExecuteResult generateResult(@NonNull ConnectionSession connectionSes
try (TraceStage s = watch.start(SqlExecuteStages.INIT_COLUMN_INFO)) {
result.initColumnInfo(connectionSession, resultTable, schemaAccessor);
} catch (Exception e) {
log.warn("Failed to init column comment", e);
log.warn("Failed to init column comment, reason={}", ExceptionUtils.getSimpleReason(e));
Comment thread
yizhouxw marked this conversation as resolved.
}
}
try (TraceStage s = watch.start(SqlExecuteStages.INIT_WARNING_MESSAGE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.oceanbase.odc.common.util.ExceptionUtils;
import com.oceanbase.odc.common.util.TraceStage;
import com.oceanbase.odc.common.util.TraceWatch;
import com.oceanbase.odc.core.session.ConnectionSession;
Expand Down Expand Up @@ -241,7 +241,8 @@ public void initColumnInfo(@NonNull ConnectionSession connectionSession, OdcTabl
columnMap.put(ColumnIdentity.of(table, column.getName()), column);
}
} catch (Exception e) {
log.warn("get column list failed, table={}, reason={}", table, e.getMessage());
log.warn("get column list failed, table={}, reason={}",
table, ExceptionUtils.getSimpleReason(e));
}
}
// attach column comment into field metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.junit.Test;

import com.oceanbase.odc.core.shared.constant.Cipher;
import com.oceanbase.odc.core.shared.constant.ConnectType;
import com.oceanbase.odc.core.shared.constant.ConnectionVisibleScope;
import com.oceanbase.odc.core.shared.constant.DialectType;
import com.oceanbase.odc.metadb.connection.ConnectionEntity;
import com.oceanbase.odc.service.connection.model.ConnectionConfig;
import com.oceanbase.odc.service.connection.model.ConnectionConfig.SSLConfig;
Expand Down Expand Up @@ -63,6 +65,7 @@ public void entityToModel() {
@Test
public void modelToEntity() {
ConnectionConfig connection = new ConnectionConfig();
connection.setType(ConnectType.OB_MYSQL);
connection.setSslConfig(sslConfig());
connection.setEnvironmentId(1L);
connection.setId(1L);
Expand All @@ -75,6 +78,9 @@ public void modelToEntity() {
connection.setTemp(false);

ConnectionEntity expected = new ConnectionEntity();
expected.setType(ConnectType.OB_MYSQL);
expected.setDialectType(DialectType.OB_MYSQL);
expected.setDefaultSchema("information_schema");
expected.setEnvironmentId(1L);
expected.setSslEnabled(false);
expected.setSslCACertObjectId("1");
Expand Down