-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2227] improvement(jdbc-backend): Improve the judgment of exception …
…information in JDBC backend (#2862) ### What changes were proposed in this pull request? Determine exceptions more accurately based on SQL Exception error codes. ### Why are the changes needed? Fix: #2227 ### How was this patch tested? Add the unit tests. --------- Co-authored-by: xiaojiebao <[email protected]>
- Loading branch information
Showing
13 changed files
with
619 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ain/java/com/datastrato/gravitino/storage/relational/converters/H2ExceptionConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational.converters; | ||
|
||
import com.datastrato.gravitino.Entity; | ||
import com.datastrato.gravitino.exceptions.AlreadyExistsException; | ||
import com.datastrato.gravitino.exceptions.GravitinoRuntimeException; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* Exception converter to Gravitino exception for H2. The definition of error codes can be found in | ||
* the document: <a href="https://h2database.com/javadoc/org/h2/api/ErrorCode.html"></a> | ||
*/ | ||
public class H2ExceptionConverter implements SQLExceptionConverter { | ||
/** It means found a duplicated primary key or unique key entry in H2. */ | ||
private static final int DUPLICATED_ENTRY_ERROR_CODE = 23505; | ||
|
||
@SuppressWarnings("FormatStringAnnotation") | ||
@Override | ||
public GravitinoRuntimeException toGravitinoException( | ||
SQLException se, Entity.EntityType type, String name) { | ||
switch (se.getErrorCode()) { | ||
case DUPLICATED_ENTRY_ERROR_CODE: | ||
return new AlreadyExistsException(se, se.getMessage()); | ||
default: | ||
return new GravitinoRuntimeException(se, se.getMessage()); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../java/com/datastrato/gravitino/storage/relational/converters/MySQLExceptionConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational.converters; | ||
|
||
import com.datastrato.gravitino.Entity; | ||
import com.datastrato.gravitino.exceptions.AlreadyExistsException; | ||
import com.datastrato.gravitino.exceptions.GravitinoRuntimeException; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* Exception converter to Gravitino exception for MySQL. The definition of error codes can be found | ||
* in the document: <a | ||
* href="https://dev.mysql.com/doc/connector-j/en/connector-j-reference-error-sqlstates.html"></a> | ||
*/ | ||
public class MySQLExceptionConverter implements SQLExceptionConverter { | ||
/** It means found a duplicated primary key or unique key entry in MySQL. */ | ||
private static final int DUPLICATED_ENTRY_ERROR_CODE = 1062; | ||
|
||
@SuppressWarnings("FormatStringAnnotation") | ||
@Override | ||
public GravitinoRuntimeException toGravitinoException( | ||
SQLException se, Entity.EntityType type, String name) { | ||
switch (se.getErrorCode()) { | ||
case DUPLICATED_ENTRY_ERROR_CODE: | ||
return new AlreadyExistsException(se, se.getMessage()); | ||
default: | ||
return new GravitinoRuntimeException(se, se.getMessage()); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...in/java/com/datastrato/gravitino/storage/relational/converters/SQLExceptionConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational.converters; | ||
|
||
import com.datastrato.gravitino.Entity; | ||
import com.datastrato.gravitino.exceptions.GravitinoRuntimeException; | ||
import java.sql.SQLException; | ||
|
||
/** Interface for converter JDBC SQL exceptions to Gravitino exceptions. */ | ||
public interface SQLExceptionConverter { | ||
/** | ||
* Convert JDBC exception to GravitinoException. | ||
* | ||
* @param sqlException The sql exception to map | ||
* @param type The type of the entity | ||
* @param name The name of the entity | ||
* @return A best attempt at a corresponding jdbc connector exception or generic with the | ||
* SQLException as the cause | ||
*/ | ||
GravitinoRuntimeException toGravitinoException( | ||
SQLException sqlException, Entity.EntityType type, String name); | ||
} |
43 changes: 43 additions & 0 deletions
43
.../com/datastrato/gravitino/storage/relational/converters/SQLExceptionConverterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational.converters; | ||
|
||
import com.datastrato.gravitino.Config; | ||
import com.datastrato.gravitino.Configs; | ||
import com.google.common.base.Preconditions; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class SQLExceptionConverterFactory { | ||
private static final Pattern TYPE_PATTERN = Pattern.compile("jdbc:(\\w+):"); | ||
private static SQLExceptionConverter converter; | ||
|
||
private SQLExceptionConverterFactory() {} | ||
|
||
public static synchronized void initConverter(Config config) { | ||
if (converter == null) { | ||
String jdbcUrl = config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL); | ||
Matcher typeMatcher = TYPE_PATTERN.matcher(jdbcUrl); | ||
if (typeMatcher.find()) { | ||
String jdbcType = typeMatcher.group(1); | ||
if (jdbcType.equalsIgnoreCase("mysql")) { | ||
converter = new MySQLExceptionConverter(); | ||
} else if (jdbcType.equalsIgnoreCase("h2")) { | ||
converter = new H2ExceptionConverter(); | ||
} else { | ||
throw new IllegalArgumentException(String.format("Unsupported jdbc type: %s", jdbcType)); | ||
} | ||
} else { | ||
throw new IllegalArgumentException( | ||
String.format("Cannot find jdbc type in jdbc url: %s", jdbcUrl)); | ||
} | ||
} | ||
} | ||
|
||
public static SQLExceptionConverter getConverter() { | ||
Preconditions.checkState(converter != null, "Exception converter is not initialized."); | ||
return converter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.