Skip to content

Commit

Permalink
Refactor SnowflakeClockMoveBackException to AlgorithmExecuteException (
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Mar 16, 2024
1 parent 4f71ffc commit 097556f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 42S02 | 10006 | Unknown column '%s' in '%s'. |
| 42S02 | 10007 | Table or view \`%s\` does not exist. |
| 42000 | 10010 | Rule does not exist. |
| 44000 | 10011 | '%s.'%s' initialization failed, reason is: %s. |
| 44000 | 10011 | Algorithm '%s.'%s' initialization failed, reason is: %s. |
| 44000 | 10012 | Can not find '%s' algorithm on table '%s'. |
| 44000 | 10013 | Algorithm '%s.%s' execute failed, reason is: %s. |
| 42S02 | 10020 | Schema \`%s\` does not exist. |
| 42S02 | 10021 | Single table \`%s\` does not exist. |
| HY000 | 10022 | Can not load table with database name \`%s\` and data source name \`%s\`. |
Expand Down Expand Up @@ -212,7 +213,6 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 44000 | 20086 | Some routed data sources do not belong to configured data sources. routed data sources: \`%s\`, configured data sources: \`%s\`. |
| 44000 | 20087 | Please check your sharding conditions \`%s\` to avoid same record in table \`%s\` routing to multiple data nodes. |
| 44000 | 20088 | Cannot found routing table factor, data source: %s, actual table: %s. |
| HY000 | 20092 | Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds. |
| HY000 | 20099 | Sharding plugin error, reason is: %s |

### 读写分离
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
| 42S02 | 10006 | Unknown column '%s' in '%s'. |
| 42S02 | 10007 | Table or view \`%s\` does not exist. |
| 42000 | 10010 | Rule does not exist. |
| 44000 | 10011 | '%s.'%s' initialization failed, reason is: %s. |
| 44000 | 10011 | Algorithm '%s.'%s' initialization failed, reason is: %s. |
| 44000 | 10012 | Can not find '%s' algorithm on table '%s'. |
| 44000 | 10013 | Algorithm '%s.%s' execute failed, reason is: %s. |
| 42S02 | 10020 | Schema \`%s\` does not exist. |
| 42S02 | 10021 | Single table \`%s\` does not exist. |
| HY000 | 10022 | Can not load table with database name \`%s\` and data source name \`%s\`. |
Expand Down Expand Up @@ -212,7 +213,6 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
| 44000 | 20086 | Some routed data sources do not belong to configured data sources. routed data sources: \`%s\`, configured data sources: \`%s\`. |
| 44000 | 20087 | Please check your sharding conditions \`%s\` to avoid same record in table \`%s\` routing to multiple data nodes. |
| 44000 | 20088 | Cannot found routing table factor, data source: %s, actual table: %s. |
| HY000 | 20092 | Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds. |
| HY000 | 20099 | Sharding plugin error, reason is: %s |

### Readwrite-splitting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.algorithm.keygen.core.exception;
package org.apache.shardingsphere.infra.algorithm.core.exception;

import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.SQLState;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.feature.FeatureSQLException;
import org.apache.shardingsphere.infra.algorithm.core.ShardingSphereAlgorithm;
import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.MetaDataSQLException;

/**
* Key generate SQL exception.
* Algorithm execute exception.
*/
public abstract class KeyGenerateSQLException extends FeatureSQLException {
public final class AlgorithmExecuteException extends MetaDataSQLException {

private static final int FEATURE_CODE = 11;
private static final long serialVersionUID = -9099514178650043282L;

private static final long serialVersionUID = 3124409584064186239L;

protected KeyGenerateSQLException(final SQLState sqlState, final int errorCode, final String reason, final Object... messageArgs) {
super(sqlState, FEATURE_CODE, errorCode, reason, messageArgs);
public AlgorithmExecuteException(final ShardingSphereAlgorithm algorithm, final String reason, final Object... args) {
super(XOpenSQLState.GENERAL_ERROR, 13, "Algorithm '%s.%s' execute failed, reason is: %s.",
algorithm.getClass().getSuperclass().getSimpleName(), algorithm.getType(), String.format(reason, args));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class AlgorithmInitializationException extends MetaDataSQLException
private static final long serialVersionUID = -7634670846091616790L;

public AlgorithmInitializationException(final ShardingSphereAlgorithm algorithm, final String reason, final Object... args) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 11, "'%s.'%s' initialization failed, reason is: %s.",
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 11, "Algorithm '%s.'%s' initialization failed, reason is: %s.",
algorithm.getClass().getSuperclass().getSimpleName(), algorithm.getType(), String.format(reason, args));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmExecuteException;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import org.apache.shardingsphere.infra.algorithm.keygen.core.KeyGenerateAlgorithm;
import org.apache.shardingsphere.infra.algorithm.keygen.snowflake.exception.SnowflakeClockMoveBackException;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.InstanceContextAware;
Expand Down Expand Up @@ -153,7 +153,8 @@ private boolean waitTolerateTimeDifferenceIfNeed(final long currentMillis) {
return false;
}
long timeDifferenceMillis = lastMillis.get() - currentMillis;
ShardingSpherePreconditions.checkState(timeDifferenceMillis < maxTolerateTimeDifferenceMillis, () -> new SnowflakeClockMoveBackException(lastMillis.get(), currentMillis));
ShardingSpherePreconditions.checkState(timeDifferenceMillis < maxTolerateTimeDifferenceMillis,
() -> new AlgorithmExecuteException(this, "Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds.", lastMillis.get(), currentMillis));
Thread.sleep(timeDifferenceMillis);
return true;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmExecuteException;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import org.apache.shardingsphere.infra.algorithm.keygen.core.KeyGenerateAlgorithm;
import org.apache.shardingsphere.infra.algorithm.keygen.snowflake.exception.SnowflakeClockMoveBackException;
import org.apache.shardingsphere.infra.algorithm.keygen.snowflake.fixture.FixedTimeService;
import org.apache.shardingsphere.infra.algorithm.keygen.snowflake.fixture.WorkerIdGeneratorFixture;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
Expand Down Expand Up @@ -166,7 +166,7 @@ void assertGenerateKeyWithClockCallBackBeyondTolerateTime() {
((InstanceContextAware) algorithm).setInstanceContext(INSTANCE);
}
setLastMillis(algorithm, timeService.getCurrentMillis() + 2);
assertThrows(SnowflakeClockMoveBackException.class, () -> batchGenerate(algorithm));
assertThrows(AlgorithmExecuteException.class, () -> batchGenerate(algorithm));
}

private void batchGenerate(final KeyGenerateAlgorithm algorithm) {
Expand Down

0 comments on commit 097556f

Please sign in to comment.