SzyWilliam commented on code in PR #12355: URL: https://github.com/apache/iotdb/pull/12355#discussion_r1568144839
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/connector/protocol/pipeconsensus/PipeConsensusAsyncConnector.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.iotdb.db.pipe.connector.protocol.pipeconsensus; + +import org.apache.iotdb.commons.conf.CommonConfig; +import org.apache.iotdb.commons.conf.CommonDescriptor; +import org.apache.iotdb.db.pipe.connector.protocol.thrift.async.IoTDBDataRegionAsyncConnector; +import org.apache.iotdb.pipe.api.event.Event; +import org.apache.iotdb.pipe.api.event.dml.insertion.TabletInsertionEvent; +import org.apache.iotdb.pipe.api.event.dml.insertion.TsFileInsertionEvent; +import org.apache.iotdb.pipe.api.exception.PipeException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Iterator; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.TimeUnit; + +public class PipeConsensusAsyncConnector extends IoTDBDataRegionAsyncConnector { + + private static final Logger LOGGER = LoggerFactory.getLogger(PipeConsensusAsyncConnector.class); + + private static final String ENQUEUE_EXCEPTION_MSG = + "Timeout: PipeConsensusConnector offers an event into transferBuffer failed, because transferBuffer is full"; + + private static final CommonConfig COMMON_CONFIG = CommonDescriptor.getInstance().getConfig(); + + private final BlockingQueue<Event> transferBuffer = + new LinkedBlockingDeque<>(COMMON_CONFIG.getPipeConsensusEventBufferSize()); + + /** Add an event to transferBuffer, whose events will be asynchronizedly transfer to receiver. */ + private boolean addEvent2Buffer(Event event) { + try { + LOGGER.debug( Review Comment: It's better to print the debug message after `LOGGER.isDebugEnabled()`. Since the `transferBuffer.size()` and `COMMON_CONFIG.getPipeConsensusEventBufferSize()` will be computed and concatenated to the log string otherwise. See https://stackoverflow.com/questions/963492/in-log4j-does-checking-isdebugenabled-before-logging-improve-performance. ########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/connector/protocol/pipeconsensus/PipeConsensusAsyncConnector.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.iotdb.db.pipe.connector.protocol.pipeconsensus; + +import org.apache.iotdb.commons.conf.CommonConfig; +import org.apache.iotdb.commons.conf.CommonDescriptor; +import org.apache.iotdb.db.pipe.connector.protocol.thrift.async.IoTDBDataRegionAsyncConnector; +import org.apache.iotdb.pipe.api.event.Event; +import org.apache.iotdb.pipe.api.event.dml.insertion.TabletInsertionEvent; +import org.apache.iotdb.pipe.api.event.dml.insertion.TsFileInsertionEvent; +import org.apache.iotdb.pipe.api.exception.PipeException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Iterator; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.TimeUnit; + +public class PipeConsensusAsyncConnector extends IoTDBDataRegionAsyncConnector { + + private static final Logger LOGGER = LoggerFactory.getLogger(PipeConsensusAsyncConnector.class); + + private static final String ENQUEUE_EXCEPTION_MSG = + "Timeout: PipeConsensusConnector offers an event into transferBuffer failed, because transferBuffer is full"; + + private static final CommonConfig COMMON_CONFIG = CommonDescriptor.getInstance().getConfig(); + + private final BlockingQueue<Event> transferBuffer = + new LinkedBlockingDeque<>(COMMON_CONFIG.getPipeConsensusEventBufferSize()); + + /** Add an event to transferBuffer, whose events will be asynchronizedly transfer to receiver. */ + private boolean addEvent2Buffer(Event event) { + try { + LOGGER.debug( + "PipeConsensus connector: one event enqueue, queue size = {}, limit size = {}", + transferBuffer.size(), + COMMON_CONFIG.getPipeConsensusEventBufferSize()); + return transferBuffer.offer( + event, COMMON_CONFIG.getPipeConsensusEventEnqueueTimeoutInMs(), TimeUnit.SECONDS); + } catch (InterruptedException e) { + LOGGER.info("PipeConsensusConnector transferBuffer queue offer is interrupted.", e); + Thread.currentThread().interrupt(); + return false; + } + } + + /** + * if one event is successfully processed by receiver in PipeConsensus, we will remove this event + * from transferBuffer in order to transfer other event. + */ + public void removeEventFromBuffer(Event event) { + synchronized (this) { Review Comment: You can simply move `synchonrized` to the method signature. -- 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]
