makssent opened a new issue, #40255: URL: https://github.com/apache/shardingsphere/issues/40255
### Before reporting a bug - [x] I have searched open and closed [GitHub issues](https://github.com/apache/shardingsphere/issues). - [x] I have read the [ShardingSphere documentation](https://shardingsphere.apache.org/document/current/en/overview). ### ShardingSphere version master, commit [`01446d4fad4`](https://github.com/apache/shardingsphere/commit/01446d4fad48450e7fc3eb3b3631b25d6bf411df) ### ShardingSphere project ShardingSphere-Proxy ### Expected behavior The proxy should support wire compression ([`pflag_compress`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/remote/server/server.cpp#L2009) in [`op_connect`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/remote/server/server.cpp#L5051-L5052)) as the Firebird server does. If the proxy does not support it, it should not confirm it in [`op_accept_data`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/remote/server/server.cpp#L2218-L2223), so that the client works without compression. ### Actual behavior The proxy confirms wire compression, but does not compress or decompress the packets, so the client cannot connect. The same steps were executed directly on the Firebird server and through the proxy, and the rows were read directly from the server: | Steps | Firebird server | Proxy | | --- | --- | --- | | Connect with [`wireCompression`](https://github.com/FirebirdSQL/jaybird/blob/v6.0.7/src/main/org/firebirdsql/jaybird/props/PropertyNames.java#L62)`=true` | Success | Jaybird [reports](https://github.com/FirebirdSQL/jaybird/blob/v6.0.7/src/main/org/firebirdsql/gds/ng/wire/version10/V10Database.java#L112-L118) error [`isc_net_read_err`](https://github.com/FirebirdSQL/jaybird/blob/v6.0.7/src/main/org/firebirdsql/gds/ng/FbExceptionBuilder.java#L301-L305) "Error reading data from the connection." | | Insert a row with ID = 8961 without auto-commit, read it and commit | Success | Not executed, there is no connection | | Read the row with ID = 8961 directly on the server | The row is saved | The row is lost | So an application that enables wire compression cannot work through the proxy at all, although it works with the Firebird server. ### Reason analysis - [`FirebirdAcceptPacket#write`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/database/protocol/dialect/firebird/src/main/java/org/apache/shardingsphere/database/protocol/firebird/packet/handshake/FirebirdAcceptPacket.java#L81-L83) copies the [`COMPRESS`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/database/protocol/dialect/firebird/src/main/java/org/apache/shardingsphere/database/protocol/firebird/packet/handshake/FirebirdAcceptPacket.java#L38) bit of the client's protocol type into the answer, so the proxy confirms compression in [`op_accept_data`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/remote/server/server.cpp#L2218-L2223). After this answer Jaybird [compresses](https://github.com/FirebirdSQL/jaybird/blob/v6.0.7/src/main/org/firebirdsql/gds/ng/wire/WireConnection.java#L467-L470) all packets and expects compressed answers, as the Firebird server [does](https://github.com/Firebir dSQL/firebird/blob/v5.0.4/src/remote/server/server.cpp#L2219-L2223). - The Firebird codec of the proxy has no compression. [`FirebirdPacketCodecEngine#decode`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/database/protocol/dialect/firebird/src/main/java/org/apache/shardingsphere/database/protocol/firebird/codec/FirebirdPacketCodecEngine.java#L69-L70) reads the first 4 bytes of the compressed [`op_attach`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/remote/server/server.cpp#L5077-L5079) (`0x789C6260`, the zlib header `78 9C`) as an operation code, and [`FirebirdCommandPacketType#valueOf`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/database/protocol/dialect/firebird/src/main/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/FirebirdCommandPacketType.java#L201-L205) fails with "Cannot find '2023514720' in command packet type". - The codec [answers](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/database/protocol/dialect/firebird/src/main/java/org/apache/shardingsphere/database/protocol/firebird/codec/FirebirdPacketCodecEngine.java#L91) with an uncompressed error packet from [`FirebirdErrorPacketFactory#newInstance`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/database/protocol/dialect/firebird/src/main/java/org/apache/shardingsphere/database/protocol/firebird/err/FirebirdErrorPacketFactory.java#L44-L47) and closes the connection. Jaybird cannot decompress this answer and reports [`isc_net_read_err`](https://github.com/FirebirdSQL/jaybird/blob/v6.0.7/src/main/org/firebirdsql/gds/ng/FbExceptionBuilder.java#L301-L305). ### Steps to reproduce #### Environment - Firebird server 5.0. - Client: Jaybird 6.0.7. #### Steps 1. Create the table on the Firebird server: ```sql CREATE TABLE T_PLAIN (ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(20)); ``` 2. Start ShardingSphere-Proxy with this configuration: ```yaml # global.yaml authority: users: - user: root@% password: root privilege: type: ALL_PERMITTED props: proxy-frontend-database-protocol-type: Firebird ``` ```yaml # database-firebird.yaml databaseName: firebird_db dataSources: ds_0: url: jdbc:firebirdsql://<host>/<database file> username: <user> password: <password> rules: - !SINGLE tables: - "*.*" ``` 3. Execute the steps from the table under "Actual behavior" through the proxy and directly on the Firebird server, and compare the results. "Example code" runs all of them. ### Example code All cases on Jaybird 6.0.7, one method per case. The comment above each method shows what it prints against the Firebird server and through the proxy. ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class WireCompression { // The proxy; put the URL of the Firebird server here to compare static final String URL = "jdbc:firebirdsql://<proxy host>:<proxy port>/firebird_db?encoding=UTF8&wireCrypt=DISABLED"; static final String USER = "root"; static final String PASSWORD = "root"; // The Firebird server, to read the rows directly static final String SERVER_URL = "jdbc:firebirdsql://<host>/<database file>"; static final String SERVER_USER = "<user>"; static final String SERVER_PASSWORD = "<password>"; public static void main(final String[] args) throws Exception { executeOnServer("DELETE FROM T_PLAIN WHERE ID = 8961"); compressedSession(); executeOnServer("DELETE FROM T_PLAIN WHERE ID = 8961"); } // Firebird server: "connect: success", "INSERT: 1 row", "SELECT: ID = 8961", "COMMIT: success", "ID = 8961: the row is saved"; // proxy: "connect: Error reading data from the connection.", "ID = 8961: the row is lost" static void compressedSession() throws SQLException { Properties properties = new Properties(); properties.setProperty("user", USER); properties.setProperty("password", PASSWORD); // Jaybird requests wire compression with pflag_compress in op_connect properties.setProperty("wireCompression", "true"); Connection connection; try { connection = DriverManager.getConnection(URL, properties); System.out.println("connect: success"); } catch (final SQLException ex) { System.out.println("connect: " + ex.getMessage()); printRow(8961); return; } try (connection; Statement statement = connection.createStatement()) { connection.setAutoCommit(false); System.out.println("INSERT: " + statement.executeUpdate("INSERT INTO T_PLAIN (ID, NAME) VALUES (8961, 'compressed')") + " row"); try (ResultSet resultSet = statement.executeQuery("SELECT ID FROM T_PLAIN WHERE ID = 8961")) { System.out.println("SELECT: " + (resultSet.next() ? "ID = " + resultSet.getInt(1) : "no row")); } connection.commit(); System.out.println("COMMIT: success"); } printRow(8961); } // Reads the row directly on the Firebird server static void printRow(final int id) throws SQLException { try (Connection connection = DriverManager.getConnection(SERVER_URL, SERVER_USER, SERVER_PASSWORD); ResultSet resultSet = connection.createStatement().executeQuery("SELECT NAME FROM T_PLAIN WHERE ID = " + id)) { System.out.println("ID = " + id + ": " + (resultSet.next() ? "the row is saved" : "the row is lost")); } } static void executeOnServer(final String sql) throws SQLException { try (Connection connection = DriverManager.getConnection(SERVER_URL, SERVER_USER, SERVER_PASSWORD)) { connection.createStatement().execute(sql); } } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
