@@ -25,8 +25,7 @@ public class LogDispatcher {
2525 private final BlockingQueue <BatchLog > successQueue ;
2626 private final BlockingQueue <BatchLog > failureQueue ;
2727 private static final Logger LOG = LoggerFactory .getLogger (LogDispatcher .class );
28- private final AtomicInteger addLogLock = new AtomicInteger (0 );
29- private volatile boolean closed = true ;
28+ private volatile boolean closed ;
3029 private final Semaphore memoryLock ;
3130 private final AtomicInteger batchCount ;
3231 private final RetryManager retryManager ;
@@ -50,7 +49,6 @@ public LogDispatcher(ProducerConfig producerConfig, String producerName, Blockin
5049 this .batchCount = batchCount ;
5150 this .retryManager = retryManager ;
5251 this .client = ClientBuilder .newClient (producerConfig .getClientConfig ());
53- this .closed = false ;
5452 }
5553
5654 public TLSLogClient getClient () {
@@ -62,19 +60,19 @@ public ConcurrentHashMap<BatchLog.BatchKey, BatchLog.BatchManager> getBatches()
6260 }
6361
6462 public void start () {
63+ this .closed = false ;
6564 LOG .info (String .format ("log dispatcher %s started and client init success" , producerName ));
6665 }
6766
6867 public ExecutorService getExecutorService () {
6968 return this .executorService ;
7069 }
7170
72- public void close () throws InterruptedException , LogException {
73- executorService .shutdown ();
71+ public void close () {
7472 this .closed = true ;
7573 }
7674
77- public void closeNow () throws InterruptedException , LogException {
75+ public void closeNow () {
7876 executorService .shutdownNow ();
7977 this .closed = true ;
8078 }
@@ -99,9 +97,7 @@ public void resetAccessKeyToken(String accessKey, String secretKey, String secur
9997
10098 public void addBatch (String hashKey , String topicId , String source , String filename ,
10199 PutLogRequest .LogGroup logGroup , CallBack callBack ) throws InterruptedException , LogException {
102- addLogLock .incrementAndGet ();
103100 doAdd (hashKey , topicId , source , filename , logGroup , callBack );
104- addLogLock .decrementAndGet ();
105101 }
106102
107103 private void doAdd (String hashKey , String topicId , String source , String filename ,
@@ -121,6 +117,8 @@ private void doAdd(String hashKey, String topicId, String source, String filenam
121117 } else {
122118 boolean acquired = memoryLock .tryAcquire (batchSize , maxBlockMs , TimeUnit .MILLISECONDS );
123119 if (!acquired ) {
120+ LOG .warn (String .format ("Failed to acquire memory within the configured max blocking time %d ms, requiredSizeInBytes=%d, availableSizeInBytes=%d" ,
121+ producerConfig .getMaxBlockMs (), batchSize , memoryLock .availablePermits ()));
124122 throw new LogException ("Producer Error" , "dispatcher %s try acquire memory lock failed" , null );
125123 }
126124 }
@@ -132,9 +130,8 @@ private void doAdd(String hashKey, String topicId, String source, String filenam
132130 addToBatchManager (batchKey , logGroup , callBack , batchSize , batchManager );
133131 }
134132 } catch (Exception e ) {
135- throw new LogException ("Producer Error" , "dispatcher add batch concurrent error" , null );
136- } finally {
137133 memoryLock .release (batchSize );
134+ throw new LogException ("Producer Error" , "dispatcher add batch concurrent error" , null );
138135 }
139136 }
140137
@@ -146,7 +143,7 @@ private int calculateSize(PutLogRequest.LogGroup logGroup) {
146143 }
147144
148145 private void addToBatchManager (BatchLog .BatchKey batchKey , PutLogRequest .LogGroup logGroup , CallBack callBack ,
149- int batchSize , BatchLog .BatchManager batchManager ) {
146+ int batchSize , BatchLog .BatchManager batchManager ) throws LogException {
150147 // add to exist batch
151148 BatchLog batchLog = batchManager .getBatchLog ();
152149 if (batchLog != null ) {
@@ -164,7 +161,12 @@ private void addToBatchManager(BatchLog.BatchKey batchKey, PutLogRequest.LogGrou
164161 // no batch create new and try send
165162 batchLog = new BatchLog (batchKey , producerConfig );
166163 batchManager .setBatchLog (batchLog );
167- batchLog .tryAdd (logGroup , batchSize , callBack );
164+ boolean success = batchLog .tryAdd (logGroup , batchSize , callBack );
165+ if (!success ) {
166+ LOG .error (String .format ("tryAdd batchLog failed, batchKey = %s, batchSize = %d, batchCount = %d" ,
167+ batchKey .toString (), batchSize , logGroup .getLogsCount ()));
168+ throw new LogException ("Producer Error" , "tryAdd batchLog failed" , null );
169+ }
168170 if (batchManager .fullAndSendBatchRequest ()) {
169171 batchManager .addNow (producerConfig , executorService , client , successQueue , failureQueue , batchCount ,
170172 retryManager );
0 commit comments