-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.wso2.carbon.apimgt.gateway.handlers.transaction; | ||
|
||
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration; | ||
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; | ||
|
||
public class TransactionCountConfig { | ||
|
||
private static APIManagerConfiguration apiManagerConfiguration; | ||
public TransactionCountConfig() { | ||
apiManagerConfiguration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService() | ||
.getAPIManagerConfiguration(); | ||
} | ||
Check warning on line 12 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountConfig.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountConfig.java#L9-L12
|
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.wso2.carbon.apimgt.gateway.handlers.transaction; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class TransactionCounter { | ||
|
||
private static TransactionCounter instance = null; | ||
private static AtomicInteger transactionCount = new AtomicInteger(0); | ||
private static ReentrantLock lock = new ReentrantLock(); | ||
Check warning on line 10 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java#L8-L10
|
||
private static int MAX_TRANSACTION_COUNT; | ||
private TransactionCounter(int maxTransactionCount) { | ||
this.MAX_TRANSACTION_COUNT = maxTransactionCount; | ||
} | ||
Check warning on line 14 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java#L12-L14
|
||
|
||
public static TransactionCounter getInstance(int maxTransactionCount) { | ||
if(instance == null) { | ||
instance = new TransactionCounter(maxTransactionCount); | ||
Check warning on line 18 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java#L18
|
||
} | ||
return instance; | ||
Check warning on line 20 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java#L20
|
||
} | ||
public static void increment() { | ||
lock.lock(); | ||
Check warning on line 23 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java#L23
|
||
try { | ||
transactionCount.incrementAndGet(); | ||
Check warning on line 25 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java#L25
|
||
} finally { | ||
lock.unlock(); | ||
Check warning on line 27 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java#L27
|
||
} | ||
} | ||
Check warning on line 29 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounter.java#L29
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.wso2.carbon.apimgt.gateway.handlers.transaction; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.wso2.carbon.apimgt.gateway.handlers.transaction.queue.TransactionRecordQueue; | ||
import org.wso2.carbon.apimgt.gateway.handlers.transaction.store.TransactionRecordStore; | ||
|
||
import java.util.ArrayList; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class TransactionRecordConsumer { | ||
|
||
// Todo: Make these parameters configurable via deployment.toml | ||
private int MAX_RETRY_COUNT = 3; | ||
private int TRANSACTION_COUNT_COMMIT_INTERVAL = 10; | ||
private int MAX_TRANSACTION_RECORDS_PER_COMMIT = 10; | ||
Check warning on line 17 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L15-L17
|
||
|
||
private static final Log LOG = LogFactory.getLog(TransactionRecordConsumer.class); | ||
private static TransactionRecordConsumer instance = null; | ||
Check warning on line 20 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L19-L20
|
||
private TransactionRecordStore transactionRecordStore; | ||
private TransactionRecordQueue transactionRecordQueue; | ||
private ExecutorService executorService; | ||
private final int threadPoolSize; | ||
|
||
private TransactionRecordConsumer(TransactionRecordStore transactionRecordStore, | ||
TransactionRecordQueue transactionRecordQueue, int threadPoolSize) { | ||
this.transactionRecordStore = transactionRecordStore; | ||
this.transactionRecordQueue = transactionRecordQueue; | ||
this.executorService = Executors.newFixedThreadPool(threadPoolSize); | ||
this.threadPoolSize = threadPoolSize; | ||
} | ||
Check warning on line 32 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L27-L32
|
||
|
||
public static TransactionRecordConsumer getInstance(TransactionRecordQueue transactionRecordQueue, | ||
TransactionRecordStore transactionRecordStore, | ||
int threadPoolSize) { | ||
if(instance == null) { | ||
instance = new TransactionRecordConsumer(transactionRecordStore, transactionRecordQueue, threadPoolSize); | ||
Check warning on line 38 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L38
|
||
} | ||
return instance; | ||
Check warning on line 40 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L40
|
||
} | ||
|
||
public void start() { | ||
LOG.info("Transaction record consumer started"); | ||
Check warning on line 44 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L44
|
||
// execute the startCommitting method in all the threads | ||
for (int i = 0; i < threadPoolSize; i++) { | ||
executorService.execute(this::startCommitting); | ||
Check warning on line 47 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L47
|
||
} | ||
} | ||
Check warning on line 49 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L49
|
||
|
||
private void startCommitting() { | ||
try { | ||
while (true) { | ||
commitWithRetries(); | ||
Check warning on line 54 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L54
|
||
} | ||
} catch (InterruptedException ex) { | ||
LOG.debug("Transaction record consumer interrupted"); | ||
Check warning on line 57 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L56-L57
|
||
} | ||
} | ||
Check warning on line 59 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L59
|
||
|
||
private void commitWithRetries() throws InterruptedException { | ||
|
||
// Arraylist of transaction count records will be committed to the store | ||
ArrayList<TransactionRecord> transactionRecordList = new ArrayList<>(); | ||
TransactionRecord transactionRecord = null; | ||
transactionRecord = transactionRecordQueue.take(); | ||
Check warning on line 66 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L64-L66
|
||
|
||
transactionRecordList.add(transactionRecord); | ||
transactionRecordQueue.drain(transactionRecordList, MAX_TRANSACTION_RECORDS_PER_COMMIT); | ||
Check warning on line 69 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L68-L69
|
||
|
||
// Committing the transaction count records to the store with retries | ||
// If failed to commit after MAX_RETRY_COUNT, the transaction count records will be added to the queue again | ||
boolean commited = false; | ||
int retryCount = 0; | ||
Check warning on line 74 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L73-L74
|
||
while (!commited && retryCount < MAX_RETRY_COUNT) { | ||
commited = this.transactionRecordStore.commit(transactionRecordList); | ||
retryCount++; | ||
Check warning on line 77 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L76-L77
|
||
} | ||
if (!commited) { | ||
transactionRecordQueue.addAll(transactionRecordList); | ||
Check warning on line 80 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L80
|
||
} | ||
} | ||
Check warning on line 82 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L82
|
||
|
||
public void shutdown() { | ||
this.executorService.shutdownNow(); | ||
} | ||
Check warning on line 86 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java Codecov / codecov/patchcomponents/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionRecordConsumer.java#L85-L86
|
||
|
||
} |