forked from apache/shardingsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ReadOnly state for Proxy (apache#23944)
- Loading branch information
1 parent
0b8dafa
commit b79d2fd
Showing
12 changed files
with
139 additions
and
18 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,5 +22,5 @@ | |
*/ | ||
public enum StateType { | ||
|
||
OK, CIRCUIT_BREAK, LOCK | ||
OK, CIRCUIT_BREAK, READ_ONLY, LOCK | ||
} |
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
33 changes: 33 additions & 0 deletions
33
...e/src/main/java/org/apache/shardingsphere/proxy/frontend/exception/ReadOnlyException.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,33 @@ | ||
/* | ||
* 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.shardingsphere.proxy.frontend.exception; | ||
|
||
import org.apache.shardingsphere.infra.exception.ConnectionSQLException; | ||
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState; | ||
|
||
/** | ||
* Read only exception. | ||
*/ | ||
public final class ReadOnlyException extends ConnectionSQLException { | ||
|
||
private static final long serialVersionUID = 6339672680026286798L; | ||
|
||
public ReadOnlyException() { | ||
super(XOpenSQLState.GENERAL_WARNING, 11, "The current instance is read-only, Not allowed write traffic."); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
...src/main/java/org/apache/shardingsphere/proxy/frontend/state/impl/ReadOnlyProxyState.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,70 @@ | ||
/* | ||
* 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.shardingsphere.proxy.frontend.state.impl; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket; | ||
import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext; | ||
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext; | ||
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext; | ||
import org.apache.shardingsphere.infra.hint.HintManager; | ||
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; | ||
import org.apache.shardingsphere.proxy.frontend.exception.ReadOnlyException; | ||
import org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine; | ||
import org.apache.shardingsphere.proxy.frontend.state.ProxyState; | ||
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; | ||
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement; | ||
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.SelectStatementHandler; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* ReadOnly proxy state. | ||
*/ | ||
public final class ReadOnlyProxyState implements ProxyState { | ||
|
||
@Override | ||
public void execute(final ChannelHandlerContext context, final Object message, final DatabaseProtocolFrontendEngine databaseProtocolFrontendEngine, final ConnectionSession connectionSession) { | ||
if (isPrimaryRoute(connectionSession.getQueryContext().getSqlStatementContext())) { | ||
context.writeAndFlush(databaseProtocolFrontendEngine.getCommandExecuteEngine().getErrorPacket(new ReadOnlyException())); | ||
Optional<DatabasePacket<?>> databasePacket = databaseProtocolFrontendEngine.getCommandExecuteEngine().getOtherPacket(connectionSession); | ||
databasePacket.ifPresent(context::writeAndFlush); | ||
} | ||
} | ||
|
||
private boolean isPrimaryRoute(final SQLStatementContext<?> sqlStatementContext) { | ||
return isWriteRouteStatement(sqlStatementContext) || isHintWriteRouteOnly(sqlStatementContext); | ||
} | ||
|
||
private boolean isWriteRouteStatement(final SQLStatementContext<?> sqlStatementContext) { | ||
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); | ||
return containsLockSegment(sqlStatement) || containsLastInsertIdProjection(sqlStatementContext) || !(sqlStatement instanceof SelectStatement); | ||
} | ||
|
||
private boolean containsLockSegment(final SQLStatement sqlStatement) { | ||
return sqlStatement instanceof SelectStatement && SelectStatementHandler.getLockSegment((SelectStatement) sqlStatement).isPresent(); | ||
} | ||
|
||
private boolean containsLastInsertIdProjection(final SQLStatementContext<?> sqlStatementContext) { | ||
return sqlStatementContext instanceof SelectStatementContext && ((SelectStatementContext) sqlStatementContext).getProjectionsContext().isContainsLastInsertIdProjection(); | ||
} | ||
|
||
private boolean isHintWriteRouteOnly(final SQLStatementContext<?> sqlStatementContext) { | ||
return HintManager.isWriteRouteOnly() || (sqlStatementContext instanceof CommonSQLStatementContext && ((CommonSQLStatementContext<?>) sqlStatementContext).isHintWriteRouteOnly()); | ||
} | ||
} |