You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2023/01/17 10:34:38 UTC

[GitHub] [incubator-seatunnel] TyrantLucifer commented on a diff in pull request #3958: [Feature][Connector-V2][SelectDB Cloud] Support SelectDB Cloud Sink Connector

TyrantLucifer commented on code in PR #3958:
URL: https://github.com/apache/incubator-seatunnel/pull/3958#discussion_r1072026997


##########
seatunnel-connectors-v2/connector-selectdb-cloud/src/main/java/org/apache/seatunnel/connector/selectdb/sink/writer/RecordStream.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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.seatunnel.connector.selectdb.sink.writer;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Record Stream for writing record.
+ */
+public class RecordStream extends InputStream {
+    private final RecordBuffer recordBuffer;
+
+    @Override
+    public int read() throws IOException {
+        return 0;
+    }
+
+    public RecordStream(int bufferSize, int bufferCount) {
+        this.recordBuffer = new RecordBuffer(bufferSize, bufferCount);
+    }
+
+    public void startInput() {
+        recordBuffer.startBufferData();
+    }
+
+    public void endInput() throws IOException {
+        recordBuffer.stopBufferData();
+    }
+
+    @Override
+    public int read(byte[] buff) throws IOException {
+        try {
+            return recordBuffer.read(buff);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void write(byte[] buff) throws IOException {
+        try {
+            recordBuffer.write(buff);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);

Review Comment:
   Unified exception.



##########
seatunnel-connectors-v2/connector-selectdb-cloud/src/main/java/org/apache/seatunnel/connector/selectdb/sink/writer/RecordBuffer.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.seatunnel.connector.selectdb.sink.writer;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingDeque;
+
+@Slf4j
+public class RecordBuffer {
+    BlockingQueue<ByteBuffer> writeQueue;
+    BlockingQueue<ByteBuffer> readQueue;
+    int bufferCapacity;
+    int queueSize;
+    ByteBuffer currentWriteBuffer;
+    ByteBuffer currentReadBuffer;
+
+    public RecordBuffer(int capacity, int queueSize) {
+        log.info("init RecordBuffer capacity {}, count {}", capacity, queueSize);
+        checkState(capacity > 0);
+        checkState(queueSize > 1);
+        this.writeQueue = new ArrayBlockingQueue<>(queueSize);
+        for (int index = 0; index < queueSize; index++) {
+            this.writeQueue.add(ByteBuffer.allocate(capacity));
+        }
+        readQueue = new LinkedBlockingDeque<>();
+        this.bufferCapacity = capacity;
+        this.queueSize = queueSize;
+    }
+
+    public void startBufferData() {
+        log.info("start buffer data, read queue size {}, write queue size {}", readQueue.size(), writeQueue.size());
+        checkState(readQueue.size() == 0);
+        checkState(writeQueue.size() == queueSize);
+        for (ByteBuffer byteBuffer : writeQueue) {
+            checkState(byteBuffer.position() == 0);
+            checkState(byteBuffer.remaining() == bufferCapacity);
+        }
+    }
+
+    public void stopBufferData() throws IOException {
+        try {
+            // add Empty buffer as finish flag.
+            boolean isEmpty = false;
+            if (currentWriteBuffer != null) {
+                currentWriteBuffer.flip();
+                // check if the current write buffer is empty.
+                isEmpty = currentWriteBuffer.limit() == 0;
+                readQueue.put(currentWriteBuffer);
+                currentWriteBuffer = null;
+            }
+            if (!isEmpty) {
+                ByteBuffer byteBuffer = writeQueue.take();
+                byteBuffer.flip();
+                checkState(byteBuffer.limit() == 0);
+                readQueue.put(byteBuffer);
+            }
+        } catch (Exception e) {
+            throw new IOException(e);

Review Comment:
   Unified exception.



##########
seatunnel-connectors-v2/connector-selectdb-cloud/src/main/java/org/apache/seatunnel/connector/selectdb/util/HttpUtil.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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.seatunnel.connector.selectdb.util;
+
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+
+/**
+ * util to build http client.
+ */
+public class HttpUtil {
+    private final HttpClientBuilder httpClientBuilder = HttpClients
+            .custom()
+            .disableRedirectHandling();
+
+    public CloseableHttpClient getHttpClient() {
+        return httpClientBuilder.build();
+    }

Review Comment:
   ```suggestion
   public class HttpUtil {
       private HttpUtil() {}
   
       private final static HttpClientBuilder HTTP_CLIENT_BUILDER = HttpClients
               .custom()
               .disableRedirectHandling();
   
       public static CloseableHttpClient getHttpClient() {
           return HTTP_CLIENT_BUILDER.build();
       }
   ```



##########
seatunnel-connectors-v2/connector-selectdb-cloud/src/main/java/org/apache/seatunnel/connector/selectdb/sink/writer/RecordStream.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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.seatunnel.connector.selectdb.sink.writer;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Record Stream for writing record.
+ */
+public class RecordStream extends InputStream {
+    private final RecordBuffer recordBuffer;
+
+    @Override
+    public int read() throws IOException {
+        return 0;
+    }
+
+    public RecordStream(int bufferSize, int bufferCount) {
+        this.recordBuffer = new RecordBuffer(bufferSize, bufferCount);
+    }
+
+    public void startInput() {
+        recordBuffer.startBufferData();
+    }
+
+    public void endInput() throws IOException {
+        recordBuffer.stopBufferData();
+    }
+
+    @Override
+    public int read(byte[] buff) throws IOException {
+        try {
+            return recordBuffer.read(buff);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);

Review Comment:
   Unified exception.



-- 
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: commits-unsubscribe@seatunnel.apache.org

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