pengxiangyu commented on a change in pull request #5375: URL: https://github.com/apache/incubator-doris/pull/5375#discussion_r580911535
########## File path: extension/flink-doris-connector/src/main/java/org/apache/doris/flink/backend/BackendClient.java ########## @@ -0,0 +1,227 @@ +// 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.doris.flink.backend; + +import org.apache.doris.flink.cfg.ConfigurationOptions; +import org.apache.doris.flink.cfg.Settings; +import org.apache.doris.flink.exception.ConnectedFailedException; +import org.apache.doris.flink.exception.DorisException; +import org.apache.doris.flink.exception.DorisInternalException; +import org.apache.doris.flink.serialization.Routing; +import org.apache.doris.flink.util.ErrorMessages; +import org.apache.doris.thrift.TDorisExternalService; +import org.apache.doris.thrift.TScanBatchResult; +import org.apache.doris.thrift.TScanCloseParams; +import org.apache.doris.thrift.TScanCloseResult; +import org.apache.doris.thrift.TScanNextBatchParams; +import org.apache.doris.thrift.TScanOpenParams; +import org.apache.doris.thrift.TScanOpenResult; +import org.apache.doris.thrift.TStatusCode; +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.apache.thrift.transport.TTransportException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Client to request Doris BE + */ +public class BackendClient { + private static Logger logger = LoggerFactory.getLogger(BackendClient.class); + + private Routing routing; + + private TDorisExternalService.Client client; + private TTransport transport; + + private boolean isConnected = false; + private final int retries; + private final int socketTimeout; + private final int connectTimeout; + + public BackendClient(Routing routing, Settings settings) throws ConnectedFailedException { + this.routing = routing; + this.connectTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS_DEFAULT); + this.socketTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS_DEFAULT); + this.retries = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_RETRIES, + ConfigurationOptions.DORIS_REQUEST_RETRIES_DEFAULT); + logger.trace("connect timeout set to '{}'. socket timeout set to '{}'. retries set to '{}'.", + this.connectTimeout, this.socketTimeout, this.retries); + open(); + } + Review comment: You need to write some annotation ########## File path: extension/flink-doris-connector/src/main/java/org/apache/doris/flink/backend/BackendClient.java ########## @@ -0,0 +1,227 @@ +// 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.doris.flink.backend; + +import org.apache.doris.flink.cfg.ConfigurationOptions; +import org.apache.doris.flink.cfg.Settings; +import org.apache.doris.flink.exception.ConnectedFailedException; +import org.apache.doris.flink.exception.DorisException; +import org.apache.doris.flink.exception.DorisInternalException; +import org.apache.doris.flink.serialization.Routing; +import org.apache.doris.flink.util.ErrorMessages; +import org.apache.doris.thrift.TDorisExternalService; +import org.apache.doris.thrift.TScanBatchResult; +import org.apache.doris.thrift.TScanCloseParams; +import org.apache.doris.thrift.TScanCloseResult; +import org.apache.doris.thrift.TScanNextBatchParams; +import org.apache.doris.thrift.TScanOpenParams; +import org.apache.doris.thrift.TScanOpenResult; +import org.apache.doris.thrift.TStatusCode; +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.apache.thrift.transport.TTransportException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Client to request Doris BE + */ +public class BackendClient { + private static Logger logger = LoggerFactory.getLogger(BackendClient.class); + + private Routing routing; + + private TDorisExternalService.Client client; + private TTransport transport; + + private boolean isConnected = false; + private final int retries; + private final int socketTimeout; + private final int connectTimeout; + + public BackendClient(Routing routing, Settings settings) throws ConnectedFailedException { + this.routing = routing; + this.connectTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS_DEFAULT); + this.socketTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS_DEFAULT); + this.retries = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_RETRIES, + ConfigurationOptions.DORIS_REQUEST_RETRIES_DEFAULT); + logger.trace("connect timeout set to '{}'. socket timeout set to '{}'. retries set to '{}'.", + this.connectTimeout, this.socketTimeout, this.retries); + open(); + } + + private void open() throws ConnectedFailedException { + logger.debug("Open client to Doris BE '{}'.", routing); + TException ex = null; + for (int attempt = 0; !isConnected && attempt < retries; ++attempt) { + logger.debug("Attempt {} to connect {}.", attempt, routing); + TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory(); + transport = new TSocket(routing.getHost(), routing.getPort(), socketTimeout, connectTimeout); + TProtocol protocol = factory.getProtocol(transport); + client = new TDorisExternalService.Client(protocol); + try { + logger.trace("Connect status before open transport to {} is '{}'.", routing, isConnected); + if (!transport.isOpen()) { + transport.open(); + isConnected = true; + } + } catch (TTransportException e) { + logger.warn(ErrorMessages.CONNECT_FAILED_MESSAGE, routing, e); + ex = e; + } + if (isConnected) { Review comment: This is better to be in try {}(just below isConnected = true;) ########## File path: extension/flink-doris-connector/src/main/java/org/apache/doris/flink/backend/BackendClient.java ########## @@ -0,0 +1,227 @@ +// 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.doris.flink.backend; + +import org.apache.doris.flink.cfg.ConfigurationOptions; +import org.apache.doris.flink.cfg.Settings; +import org.apache.doris.flink.exception.ConnectedFailedException; +import org.apache.doris.flink.exception.DorisException; +import org.apache.doris.flink.exception.DorisInternalException; +import org.apache.doris.flink.serialization.Routing; +import org.apache.doris.flink.util.ErrorMessages; +import org.apache.doris.thrift.TDorisExternalService; +import org.apache.doris.thrift.TScanBatchResult; +import org.apache.doris.thrift.TScanCloseParams; +import org.apache.doris.thrift.TScanCloseResult; +import org.apache.doris.thrift.TScanNextBatchParams; +import org.apache.doris.thrift.TScanOpenParams; +import org.apache.doris.thrift.TScanOpenResult; +import org.apache.doris.thrift.TStatusCode; +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.apache.thrift.transport.TTransportException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Client to request Doris BE + */ +public class BackendClient { + private static Logger logger = LoggerFactory.getLogger(BackendClient.class); + + private Routing routing; + + private TDorisExternalService.Client client; + private TTransport transport; + + private boolean isConnected = false; + private final int retries; + private final int socketTimeout; + private final int connectTimeout; + + public BackendClient(Routing routing, Settings settings) throws ConnectedFailedException { + this.routing = routing; + this.connectTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS_DEFAULT); + this.socketTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS_DEFAULT); + this.retries = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_RETRIES, + ConfigurationOptions.DORIS_REQUEST_RETRIES_DEFAULT); + logger.trace("connect timeout set to '{}'. socket timeout set to '{}'. retries set to '{}'.", + this.connectTimeout, this.socketTimeout, this.retries); + open(); + } + + private void open() throws ConnectedFailedException { + logger.debug("Open client to Doris BE '{}'.", routing); + TException ex = null; + for (int attempt = 0; !isConnected && attempt < retries; ++attempt) { + logger.debug("Attempt {} to connect {}.", attempt, routing); + TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory(); + transport = new TSocket(routing.getHost(), routing.getPort(), socketTimeout, connectTimeout); + TProtocol protocol = factory.getProtocol(transport); + client = new TDorisExternalService.Client(protocol); + try { + logger.trace("Connect status before open transport to {} is '{}'.", routing, isConnected); + if (!transport.isOpen()) { + transport.open(); + isConnected = true; + } + } catch (TTransportException e) { + logger.warn(ErrorMessages.CONNECT_FAILED_MESSAGE, routing, e); + ex = e; + } + if (isConnected) { + logger.info("Success connect to {}.", routing); + break; + } + } Review comment: it is better to return just after isConnected = true ########## File path: extension/flink-doris-connector/src/main/java/org/apache/doris/flink/backend/BackendClient.java ########## @@ -0,0 +1,227 @@ +// 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.doris.flink.backend; + +import org.apache.doris.flink.cfg.ConfigurationOptions; +import org.apache.doris.flink.cfg.Settings; +import org.apache.doris.flink.exception.ConnectedFailedException; +import org.apache.doris.flink.exception.DorisException; +import org.apache.doris.flink.exception.DorisInternalException; +import org.apache.doris.flink.serialization.Routing; +import org.apache.doris.flink.util.ErrorMessages; +import org.apache.doris.thrift.TDorisExternalService; +import org.apache.doris.thrift.TScanBatchResult; +import org.apache.doris.thrift.TScanCloseParams; +import org.apache.doris.thrift.TScanCloseResult; +import org.apache.doris.thrift.TScanNextBatchParams; +import org.apache.doris.thrift.TScanOpenParams; +import org.apache.doris.thrift.TScanOpenResult; +import org.apache.doris.thrift.TStatusCode; +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.apache.thrift.transport.TTransportException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Client to request Doris BE + */ +public class BackendClient { + private static Logger logger = LoggerFactory.getLogger(BackendClient.class); + + private Routing routing; + + private TDorisExternalService.Client client; + private TTransport transport; + + private boolean isConnected = false; + private final int retries; + private final int socketTimeout; + private final int connectTimeout; + + public BackendClient(Routing routing, Settings settings) throws ConnectedFailedException { + this.routing = routing; + this.connectTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS_DEFAULT); + this.socketTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS_DEFAULT); + this.retries = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_RETRIES, + ConfigurationOptions.DORIS_REQUEST_RETRIES_DEFAULT); + logger.trace("connect timeout set to '{}'. socket timeout set to '{}'. retries set to '{}'.", + this.connectTimeout, this.socketTimeout, this.retries); + open(); + } + + private void open() throws ConnectedFailedException { + logger.debug("Open client to Doris BE '{}'.", routing); + TException ex = null; + for (int attempt = 0; !isConnected && attempt < retries; ++attempt) { + logger.debug("Attempt {} to connect {}.", attempt, routing); + TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory(); + transport = new TSocket(routing.getHost(), routing.getPort(), socketTimeout, connectTimeout); + TProtocol protocol = factory.getProtocol(transport); + client = new TDorisExternalService.Client(protocol); + try { + logger.trace("Connect status before open transport to {} is '{}'.", routing, isConnected); + if (!transport.isOpen()) { + transport.open(); + isConnected = true; + } + } catch (TTransportException e) { + logger.warn(ErrorMessages.CONNECT_FAILED_MESSAGE, routing, e); + ex = e; + } + if (isConnected) { + logger.info("Success connect to {}.", routing); + break; + } + } + if (!isConnected) { + logger.error(ErrorMessages.CONNECT_FAILED_MESSAGE, routing); + throw new ConnectedFailedException(routing.toString(), ex); + } + } + + private void close() { + logger.trace("Connect status before close with '{}' is '{}'.", routing, isConnected); + isConnected = false; + if (null != client) { Review comment: You should close it before set client null ########## File path: extension/flink-doris-connector/src/main/java/org/apache/doris/flink/backend/BackendClient.java ########## @@ -0,0 +1,227 @@ +// 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.doris.flink.backend; + +import org.apache.doris.flink.cfg.ConfigurationOptions; +import org.apache.doris.flink.cfg.Settings; +import org.apache.doris.flink.exception.ConnectedFailedException; +import org.apache.doris.flink.exception.DorisException; +import org.apache.doris.flink.exception.DorisInternalException; +import org.apache.doris.flink.serialization.Routing; +import org.apache.doris.flink.util.ErrorMessages; +import org.apache.doris.thrift.TDorisExternalService; +import org.apache.doris.thrift.TScanBatchResult; +import org.apache.doris.thrift.TScanCloseParams; +import org.apache.doris.thrift.TScanCloseResult; +import org.apache.doris.thrift.TScanNextBatchParams; +import org.apache.doris.thrift.TScanOpenParams; +import org.apache.doris.thrift.TScanOpenResult; +import org.apache.doris.thrift.TStatusCode; +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.apache.thrift.transport.TTransportException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Client to request Doris BE + */ +public class BackendClient { + private static Logger logger = LoggerFactory.getLogger(BackendClient.class); + + private Routing routing; + + private TDorisExternalService.Client client; + private TTransport transport; + + private boolean isConnected = false; + private final int retries; + private final int socketTimeout; + private final int connectTimeout; + + public BackendClient(Routing routing, Settings settings) throws ConnectedFailedException { + this.routing = routing; + this.connectTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_CONNECT_TIMEOUT_MS_DEFAULT); + this.socketTimeout = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS, + ConfigurationOptions.DORIS_REQUEST_READ_TIMEOUT_MS_DEFAULT); + this.retries = settings.getIntegerProperty(ConfigurationOptions.DORIS_REQUEST_RETRIES, + ConfigurationOptions.DORIS_REQUEST_RETRIES_DEFAULT); + logger.trace("connect timeout set to '{}'. socket timeout set to '{}'. retries set to '{}'.", + this.connectTimeout, this.socketTimeout, this.retries); + open(); + } + + private void open() throws ConnectedFailedException { + logger.debug("Open client to Doris BE '{}'.", routing); + TException ex = null; + for (int attempt = 0; !isConnected && attempt < retries; ++attempt) { + logger.debug("Attempt {} to connect {}.", attempt, routing); + TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory(); + transport = new TSocket(routing.getHost(), routing.getPort(), socketTimeout, connectTimeout); + TProtocol protocol = factory.getProtocol(transport); + client = new TDorisExternalService.Client(protocol); + try { + logger.trace("Connect status before open transport to {} is '{}'.", routing, isConnected); + if (!transport.isOpen()) { + transport.open(); + isConnected = true; + } + } catch (TTransportException e) { + logger.warn(ErrorMessages.CONNECT_FAILED_MESSAGE, routing, e); + ex = e; + } + if (isConnected) { + logger.info("Success connect to {}.", routing); + break; + } + } + if (!isConnected) { + logger.error(ErrorMessages.CONNECT_FAILED_MESSAGE, routing); + throw new ConnectedFailedException(routing.toString(), ex); + } + } + + private void close() { + logger.trace("Connect status before close with '{}' is '{}'.", routing, isConnected); + isConnected = false; + if (null != client) { + client = null; + } + if ((transport != null) && transport.isOpen()) { + transport.close(); + logger.info("Closed a connection to {}.", routing); + } + } + + /** + * Open a scanner for reading Doris data. + * @param openParams thrift struct to required by request + * @return scan open result + * @throws ConnectedFailedException throw if cannot connect to Doris BE + */ + public TScanOpenResult openScanner(TScanOpenParams openParams) throws ConnectedFailedException { + logger.debug("OpenScanner to '{}', parameter is '{}'.", routing, openParams); + if (!isConnected) { + open(); + } + TException ex = null; + for (int attempt = 0; attempt < retries; ++attempt) { + logger.debug("Attempt {} to openScanner {}.", attempt, routing); + try { + TScanOpenResult result = client.open_scanner(openParams); + if (result == null) { + logger.warn("Open scanner result from {} is null.", routing); + continue; + } + if (!TStatusCode.OK.equals(result.getStatus().getStatus_code())) { + logger.warn("The status of open scanner result from {} is '{}', error message is: {}.", + routing, result.getStatus().getStatus_code(), result.getStatus().getError_msgs()); + continue; + } + return result; + } catch (TException e) { + logger.warn("Open scanner from {} failed.", routing, e); + ex = e; + } + } + logger.error(ErrorMessages.CONNECT_FAILED_MESSAGE, routing); + throw new ConnectedFailedException(routing.toString(), ex); + } + + /** + * get next row batch from Doris BE + * @param nextBatchParams thrift struct to required by request + * @return scan batch result + * @throws ConnectedFailedException throw if cannot connect to Doris BE + */ + public TScanBatchResult getNext(TScanNextBatchParams nextBatchParams) throws DorisException { + logger.debug("GetNext to '{}', parameter is '{}'.", routing, nextBatchParams); + if (!isConnected) { + open(); + } + TException ex = null; + TScanBatchResult result = null; + for (int attempt = 0; attempt < retries; ++attempt) { + logger.debug("Attempt {} to getNext {}.", attempt, routing); + try { + result = client.get_next(nextBatchParams); + if (result == null) { + logger.warn("GetNext result from {} is null.", routing); + continue; + } + if (!TStatusCode.OK.equals(result.getStatus().getStatus_code())) { + logger.warn("The status of get next result from {} is '{}', error message is: {}.", + routing, result.getStatus().getStatus_code(), result.getStatus().getError_msgs()); + continue; + } + return result; + } catch (TException e) { + logger.warn("Get next from {} failed.", routing, e); + ex = e; + } + } + if (result != null && (TStatusCode.OK != (result.getStatus().getStatus_code()))) { + logger.error(ErrorMessages.DORIS_INTERNAL_FAIL_MESSAGE, routing, result.getStatus().getStatus_code(), + result.getStatus().getError_msgs()); + throw new DorisInternalException(routing.toString(), result.getStatus().getStatus_code(), + result.getStatus().getError_msgs()); + } + logger.error(ErrorMessages.CONNECT_FAILED_MESSAGE, routing); + throw new ConnectedFailedException(routing.toString(), ex); + } + + /** + * close an scanner. + * @param closeParams thrift struct to required by request + */ + public void closeScanner(TScanCloseParams closeParams) { + logger.debug("CloseScanner to '{}', parameter is '{}'.", routing, closeParams); + if (!isConnected) { + try { + open(); Review comment: Why open here?this will create a new client, do nothing, then close it? ########## File path: extension/flink-doris-connector/src/main/java/org/apache/doris/flink/serialization/RowBatch.java ########## @@ -0,0 +1,297 @@ +// 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.doris.flink.serialization; + +import com.google.common.base.Preconditions; +import org.apache.arrow.memory.RootAllocator; + +import org.apache.arrow.vector.BigIntVector; +import org.apache.arrow.vector.BitVector; +import org.apache.arrow.vector.DecimalVector; +import org.apache.arrow.vector.FieldVector; +import org.apache.arrow.vector.Float4Vector; +import org.apache.arrow.vector.Float8Vector; +import org.apache.arrow.vector.IntVector; +import org.apache.arrow.vector.SmallIntVector; +import org.apache.arrow.vector.TinyIntVector; +import org.apache.arrow.vector.VarBinaryVector; +import org.apache.arrow.vector.VarCharVector; +import org.apache.arrow.vector.VectorSchemaRoot; +import org.apache.arrow.vector.ipc.ArrowStreamReader; +import org.apache.arrow.vector.types.Types; +import org.apache.doris.flink.exception.DorisException; +import org.apache.doris.flink.rest.models.Schema; +import org.apache.doris.thrift.TScanBatchResult; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; + +/** + * row batch data container. + */ +public class RowBatch { + private static Logger logger = LoggerFactory.getLogger(RowBatch.class); + + public static class Row { + private List<Object> cols; + + Row(int colCount) { + this.cols = new ArrayList<>(colCount); + } + + List<Object> getCols() { + return cols; + } + + public void put(Object o) { + cols.add(o); + } + } + + // offset for iterate the rowBatch + private int offsetInRowBatch = 0; + private int rowCountInOneBatch = 0; + private int readRowCount = 0; + private List<Row> rowBatch = new ArrayList<>(); + private final ArrowStreamReader arrowStreamReader; + private final VectorSchemaRoot root; + private List<FieldVector> fieldVectors; + private RootAllocator rootAllocator; + private final Schema schema; + + public RowBatch(TScanBatchResult nextResult, Schema schema) throws DorisException { + this.schema = schema; + this.rootAllocator = new RootAllocator(Integer.MAX_VALUE); + this.arrowStreamReader = new ArrowStreamReader( + new ByteArrayInputStream(nextResult.getRows()), + rootAllocator + ); + this.offsetInRowBatch = 0; + try { + this.root = arrowStreamReader.getVectorSchemaRoot(); + while (arrowStreamReader.loadNextBatch()) { + fieldVectors = root.getFieldVectors(); + if (fieldVectors.size() != schema.size()) { + logger.error("Schema size '{}' is not equal to arrow field size '{}'.", + fieldVectors.size(), schema.size()); + throw new DorisException("Load Doris data failed, schema size of fetch data is wrong."); + } + if (fieldVectors.size() == 0 || root.getRowCount() == 0) { + logger.debug("One batch in arrow has no data."); + continue; + } + rowCountInOneBatch = root.getRowCount(); + // init the rowBatch + for (int i = 0; i < rowCountInOneBatch; ++i) { + rowBatch.add(new Row(fieldVectors.size())); + } + convertArrowToRowBatch(); + readRowCount += root.getRowCount(); + } + } catch (Exception e) { + logger.error("Read Doris Data failed because: ", e); + throw new DorisException(e.getMessage()); + } finally { + close(); + } + } + + public boolean hasNext() { + if (offsetInRowBatch < readRowCount) { + return true; + } + return false; + } + Review comment: add some annotation ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
