EMsnap commented on code in PR #9150: URL: https://github.com/apache/inlong/pull/9150#discussion_r1375637065
########## inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sinks/filecollect/SenderManager.java: ########## @@ -0,0 +1,454 @@ +/* + * 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.inlong.agent.plugin.sinks.filecollect; + +import org.apache.inlong.agent.common.AgentThreadFactory; +import org.apache.inlong.agent.conf.AgentConfiguration; +import org.apache.inlong.agent.conf.InstanceProfile; +import org.apache.inlong.agent.conf.OffsetProfile; +import org.apache.inlong.agent.constant.CommonConstants; +import org.apache.inlong.agent.core.task.OffsetManager; +import org.apache.inlong.agent.core.task.file.MemoryManager; +import org.apache.inlong.agent.message.filecollect.PackageAckInfo; +import org.apache.inlong.agent.message.filecollect.SenderMessage; +import org.apache.inlong.agent.metrics.AgentMetricItem; +import org.apache.inlong.agent.metrics.AgentMetricItemSet; +import org.apache.inlong.agent.plugin.message.SequentialID; +import org.apache.inlong.agent.utils.AgentUtils; +import org.apache.inlong.agent.utils.ThreadUtils; +import org.apache.inlong.common.constant.ProtocolType; +import org.apache.inlong.common.metric.MetricRegister; +import org.apache.inlong.sdk.dataproxy.DefaultMessageSender; +import org.apache.inlong.sdk.dataproxy.ProxyClientConfig; +import org.apache.inlong.sdk.dataproxy.SendMessageCallback; +import org.apache.inlong.sdk.dataproxy.SendResult; +import org.apache.inlong.sdk.dataproxy.network.ProxysdkException; + +import io.netty.util.concurrent.DefaultThreadFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import static org.apache.inlong.agent.constant.CommonConstants.DEFAULT_PROXY_BATCH_FLUSH_INTERVAL; +import static org.apache.inlong.agent.constant.CommonConstants.PROXY_BATCH_FLUSH_INTERVAL; +import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_GLOBAL_WRITER_PERMIT; +import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_AUTH_SECRET_ID; +import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_AUTH_SECRET_KEY; +import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_VIP_HTTP_HOST; +import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_VIP_HTTP_PORT; +import static org.apache.inlong.agent.constant.TaskConstants.DEFAULT_JOB_PROXY_SEND; +import static org.apache.inlong.agent.constant.TaskConstants.INODE_INFO; +import static org.apache.inlong.agent.constant.TaskConstants.JOB_PROXY_SEND; +import static org.apache.inlong.agent.metrics.AgentMetricItem.KEY_INLONG_GROUP_ID; +import static org.apache.inlong.agent.metrics.AgentMetricItem.KEY_INLONG_STREAM_ID; +import static org.apache.inlong.agent.metrics.AgentMetricItem.KEY_PLUGIN_ID; + +/** + * proxy client + */ +public class SenderManager { + + private static final Logger LOGGER = LoggerFactory.getLogger(SenderManager.class); + private static final SequentialID SEQUENTIAL_ID = SequentialID.getInstance(); + public final int SAVE_OFFSET_INTERVAL_MS = 1000; + private final AtomicInteger SENDER_INDEX = new AtomicInteger(0); + // cache for group and sender list, share the map cross agent lifecycle. + private DefaultMessageSender sender; + private LinkedBlockingQueue<AgentSenderCallback> resendQueue; + private static final ThreadPoolExecutor EXECUTOR_SERVICE = new ThreadPoolExecutor( + 0, Integer.MAX_VALUE, + 1L, TimeUnit.SECONDS, + new SynchronousQueue<>(), + new AgentThreadFactory("sender-manager")); + // sharing worker threads between sender client + // in case of thread abusing. + private ThreadFactory SHARED_FACTORY; + private static final AtomicLong METRIC_INDEX = new AtomicLong(0); + private final String managerHost; + private final int managerPort; + private final String netTag; + private final String localhost; + private final boolean isLocalVisit; + private final int totalAsyncBufSize; + private final int aliveConnectionNum; + private final boolean isCompress; + private final int msgType; + private final boolean isFile; + private final long maxSenderTimeout; + private final int maxSenderRetry; + private final long retrySleepTime; + private final String inlongGroupId; + private final int maxSenderPerGroup; + private final String sourcePath; + private final boolean proxySend; + private volatile boolean shutdown = false; + // metric + private AgentMetricItemSet metricItemSet; + private Map<String, String> dimensions; + private OffsetManager offsetManager; + private int ioThreadNum; + private boolean enableBusyWait; + private String authSecretId; + private String authSecretKey; + protected int batchFlushInterval; + private List<PackageAckInfo> packageAckInfoList = new ArrayList<>(); + private final ReentrantReadWriteLock packageAckInfoLock = new ReentrantReadWriteLock(true); + protected InstanceProfile profile; + private Random testRandom = new Random(); + private volatile boolean offsetRunning = false; + private volatile boolean resendRunning = false; + private volatile boolean started = false; + + public SenderManager(InstanceProfile profile, String inlongGroupId, String sourcePath) { + AgentConfiguration conf = AgentConfiguration.getAgentConf(); + this.profile = profile; + managerHost = conf.get(AGENT_MANAGER_VIP_HTTP_HOST); + managerPort = conf.getInt(AGENT_MANAGER_VIP_HTTP_PORT); + proxySend = profile.getBoolean(JOB_PROXY_SEND, DEFAULT_JOB_PROXY_SEND); + localhost = profile.get(CommonConstants.PROXY_LOCAL_HOST, CommonConstants.DEFAULT_PROXY_LOCALHOST); + netTag = profile.get(CommonConstants.PROXY_NET_TAG, CommonConstants.DEFAULT_PROXY_NET_TAG); + isLocalVisit = profile.getBoolean( + CommonConstants.PROXY_IS_LOCAL_VISIT, CommonConstants.DEFAULT_PROXY_IS_LOCAL_VISIT); + totalAsyncBufSize = profile + .getInt( + CommonConstants.PROXY_TOTAL_ASYNC_PROXY_SIZE, + CommonConstants.DEFAULT_PROXY_TOTAL_ASYNC_PROXY_SIZE); + aliveConnectionNum = profile + .getInt( + CommonConstants.PROXY_ALIVE_CONNECTION_NUM, CommonConstants.DEFAULT_PROXY_ALIVE_CONNECTION_NUM); + isCompress = profile.getBoolean( + CommonConstants.PROXY_IS_COMPRESS, CommonConstants.DEFAULT_PROXY_IS_COMPRESS); + maxSenderPerGroup = profile.getInt( + CommonConstants.PROXY_MAX_SENDER_PER_GROUP, CommonConstants.DEFAULT_PROXY_MAX_SENDER_PER_GROUP); + msgType = profile.getInt(CommonConstants.PROXY_MSG_TYPE, CommonConstants.DEFAULT_PROXY_MSG_TYPE); + maxSenderTimeout = profile.getInt( + CommonConstants.PROXY_SENDER_MAX_TIMEOUT, CommonConstants.DEFAULT_PROXY_SENDER_MAX_TIMEOUT); + maxSenderRetry = profile.getInt( + CommonConstants.PROXY_SENDER_MAX_RETRY, CommonConstants.DEFAULT_PROXY_SENDER_MAX_RETRY); + retrySleepTime = profile.getLong( + CommonConstants.PROXY_RETRY_SLEEP, CommonConstants.DEFAULT_PROXY_RETRY_SLEEP); + isFile = profile.getBoolean(CommonConstants.PROXY_IS_FILE, CommonConstants.DEFAULT_IS_FILE); + offsetManager = OffsetManager.init(); + ioThreadNum = profile.getInt(CommonConstants.PROXY_CLIENT_IO_THREAD_NUM, + CommonConstants.DEFAULT_PROXY_CLIENT_IO_THREAD_NUM); + enableBusyWait = profile.getBoolean(CommonConstants.PROXY_CLIENT_ENABLE_BUSY_WAIT, + CommonConstants.DEFAULT_PROXY_CLIENT_ENABLE_BUSY_WAIT); + batchFlushInterval = profile.getInt(PROXY_BATCH_FLUSH_INTERVAL, DEFAULT_PROXY_BATCH_FLUSH_INTERVAL); + authSecretId = conf.get(AGENT_MANAGER_AUTH_SECRET_ID); + authSecretKey = conf.get(AGENT_MANAGER_AUTH_SECRET_KEY); + + this.sourcePath = sourcePath; + this.inlongGroupId = inlongGroupId; + + this.dimensions = new HashMap<>(); + dimensions.put(KEY_PLUGIN_ID, this.getClass().getSimpleName()); + String metricName = String.join("-", this.getClass().getSimpleName(), + String.valueOf(METRIC_INDEX.incrementAndGet())); + this.metricItemSet = new AgentMetricItemSet(metricName); + MetricRegister.register(metricItemSet); + resendQueue = new LinkedBlockingQueue<>(); + + } + + public void Start() throws Exception { + createMessageSender(inlongGroupId); + EXECUTOR_SERVICE.execute(flushOffset()); + EXECUTOR_SERVICE.execute(flushResendQueue()); + started = true; + } + + public void Stop() { + LOGGER.info("stop send manager"); + shutdown = true; + if (!started) { + return; + } + while (offsetRunning || resendRunning) { + AgentUtils.silenceSleepInMs(1); + } + closeMessageSender(); + clearOffset(); + LOGGER.info("stop send manager end"); + } + + private void closeMessageSender() { + if (sender != null) { + sender.close(); + } + } + + private AgentMetricItem getMetricItem(Map<String, String> otherDimensions) { + Map<String, String> dimensions = new HashMap<>(); + dimensions.put(KEY_PLUGIN_ID, this.getClass().getSimpleName()); + dimensions.putAll(otherDimensions); + return this.metricItemSet.findMetricItem(dimensions); + } + + private AgentMetricItem getMetricItem(String groupId, String streamId) { + Map<String, String> dims = new HashMap<>(); + dims.put(KEY_INLONG_GROUP_ID, groupId); + dims.put(KEY_INLONG_STREAM_ID, streamId); + return getMetricItem(dims); + } + + /** + * sender + * + * @param tagName group id + * @return DefaultMessageSender Review Comment: no return here and what's the meaning of tagname group id ? -- 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]
