You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/09/11 00:50:48 UTC

[GitHub] [shardingsphere] justbk2015 commented on a change in pull request #12318: [feature] add scaling adapt feature for openGauss

justbk2015 commented on a change in pull request #12318:
URL: https://github.com/apache/shardingsphere/pull/12318#discussion_r706533247



##########
File path: shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-opengauss/src/main/java/org/apache/shardingsphere/scaling/opengauss/wal/OpenGaussLogicalReplication.java
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.scaling.opengauss.wal;
+
+import org.apache.shardingsphere.scaling.core.config.datasource.StandardJDBCDataSourceConfiguration;
+import org.apache.shardingsphere.scaling.postgresql.wal.event.LogSequenceNumberBase;
+import org.opengauss.PGProperty;
+import org.opengauss.jdbc.PgConnection;
+import org.opengauss.replication.LogSequenceNumber;
+import org.opengauss.replication.PGReplicationStream;
+import org.opengauss.util.PSQLException;
+
+import java.sql.CallableStatement;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Properties;
+
+/**
+ * OpenGauss logical replication.
+ */
+public final class OpenGaussLogicalReplication {
+
+    public static final String SLOT_NAME = "sharding_scaling";
+
+    public static final String DECODE_PLUGIN = "test_decoding";
+
+    public static final String DUPLICATE_OBJECT_ERROR_CODE = "42710";
+
+    /**
+     * Create OpenGauss connection.
+     *
+     * @param jdbcDataSourceConfig JDBC data source configuration
+     * @return OpenGauss connection
+     * @throws SQLException sql exception
+     */
+    public Connection createPgConnection(final StandardJDBCDataSourceConfiguration jdbcDataSourceConfig) throws SQLException {
+        return createConnection(jdbcDataSourceConfig);
+    }
+    
+    private Connection createConnection(final StandardJDBCDataSourceConfiguration jdbcDataSourceConfig) throws SQLException {
+        Properties props = new Properties();
+        PGProperty.USER.set(props, jdbcDataSourceConfig.getHikariConfig().getUsername());
+        PGProperty.PASSWORD.set(props, jdbcDataSourceConfig.getHikariConfig().getPassword());
+        PGProperty.ASSUME_MIN_SERVER_VERSION.set(props, "9.4");
+        PGProperty.REPLICATION.set(props, "database");
+        PGProperty.PREFER_QUERY_MODE.set(props, "simple");
+        return DriverManager.getConnection(jdbcDataSourceConfig.getHikariConfig().getJdbcUrl(), props);
+    }
+    
+    /**
+     * Create OpenGauss replication stream.
+     *
+     * @param pgConnection OpenGauss connection
+     * @param startPosition start position
+     * @return replication stream
+     * @throws SQLException sql exception
+     */
+    public PGReplicationStream createReplicationStream(final PgConnection pgConnection, final LogSequenceNumberBase startPosition) throws SQLException {
+        return pgConnection.getReplicationAPI()
+                .replicationStream()
+                .logical()
+                .withSlotName(SLOT_NAME)
+                .withSlotOption("include-xids", true)
+                .withSlotOption("skip-empty-xacts", true)
+                .withStartPosition((LogSequenceNumber) startPosition.get())
+                .start();
+    }
+
+    /**
+     * Drop exist slots.
+     *
+     * @param conn the datasource connection
+     * @throws SQLException the sql exp
+     */
+    public static void createIfNotExists(final Connection conn) throws SQLException {
+        if (isSlotNameExist(conn)) {
+            dropSlot(conn);

Review comment:
       no data lost. Before replication slot work, we use inventory task to copy data. So we recreate replication after inventory task is right. avoid og wal_sender thread out of work in previous task.




-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org