use of org.ak.trafficController.TaskExecutor in project trafficController by amitkhosla.
the class AnnotationSupportImpl method runAsync.
/**
* Handles Async operation where user is looking to submit something to get processed but calling thread should not wait for it.
* If called in Parallel flow, will be added as a async task/
* @param joinPoint Join point
* @param async Async
* @return Returns null if no exception
* @throws Throwable In case of exception in execution of annotated method
*/
@Around("execution(@org.ak.trafficController.annotations.api.Submit * *(..)) && @annotation(async)")
public Object runAsync(ProceedingJoinPoint joinPoint, Submit async) throws Throwable {
RunnableToBeExecuted taskToWorkOn = () -> {
try {
joinPoint.proceed();
} catch (Throwable e) {
logger.log(java.util.logging.Level.WARNING, "exception occured while running a submit request", e);
}
};
TaskExecutor taskExecutor = taskHelper.getTaskExecutor(async, joinPoint);
Task task = null;
TaskType taskType = async.taskType();
switch(taskType) {
case NORMAL:
task = taskExecutor.of(taskToWorkOn);
break;
case SLOW:
task = taskExecutor.slowOf(taskToWorkOn);
}
setThreadingDetailsIfAny(task, async.threadDetailsDataExtractClass(), async.threadDetailsDataExtractMethodName(), async.threadDetailsProcessorClass(), async.threadDetailsProcessorMethodName(), async.threadDetailsCleanerClass(), async.threadDetailsCleanerMethodName());
Task taskInThread = ParallelJoinHelper.getTask();
if (taskInThread == null) {
task.submit();
} else {
AtomicReference<Task> taskReference = new AtomicReference<Task>(task);
((ParallelTask) taskInThread).addRunnables(convertAnnotationTaskTypeToFrameworkTaskType(taskType), taskExecutor, () -> taskExecutor.enque(taskReference.get()));
}
return null;
}
use of org.ak.trafficController.TaskExecutor in project trafficController by amitkhosla.
the class DummyProgram method main.
public static void main(String[] args) {
ConcurrentLinkedQueue<Integer> clq = new ConcurrentLinkedQueue<>();
TaskExecutor taskExecutor = TaskExecutor.getInstance();
List<Thread> threads = new ArrayList<>();
int max = 30000;
for (int i = 0; i < max; i++) {
int k = i;
Thread thread = new Thread(() -> {
try {
process(taskExecutor, k, clq);
} catch (Throwable e) {
e.printStackTrace();
}
// System.out.println("done with " + Thread.currentThread());
});
threads.add(thread);
thread.start();
}
for (Thread t : threads) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
AtomicInteger aa = new AtomicInteger(0);
System.out.println(clq.size());
for (int i = 0; i < max; i++) {
boolean contains = clq.contains(23 + 32 + i);
if (!contains) {
System.out.println("not found... for " + i);
aa.incrementAndGet();
}
}
System.out.println("Issue with " + aa.get());
}
use of org.ak.trafficController.TaskExecutor in project trafficController by amitkhosla.
the class TaskHelper method getTaskExecutor.
/**
* Get task executor for given Submit and join point.
* If name passed in Submit, will be used else will be derived from join point.
* @param async Submit
* @param joinPoint Join point
* @return Task executor
*/
public TaskExecutor getTaskExecutor(Submit async, ProceedingJoinPoint joinPoint) {
String taskExecutorName = getTaskExecutorName(async);
TaskExecutor taskExecutor = getExecutorByName(taskExecutorName);
if (StringUtils.isEmpty(taskExecutorName)) {
taskExecutorName = getNameFromJoinPointMethodSignature(joinPoint);
}
if (Objects.isNull(taskExecutor)) {
if (taskExecutorName.equals(Constants.DEFAULT)) {
taskExecutor = TaskExecutor.getInstance();
} else {
taskExecutor = getTaskExecutorBasedOnAsyncProps(async, joinPoint);
}
taskExecutors.put(taskExecutorName, taskExecutor);
}
return taskExecutor;
}
use of org.ak.trafficController.TaskExecutor in project trafficController by amitkhosla.
the class TaskHelper method getExecutorByName.
/**
* Get Task executor by name. Name can have numbers and percentages.
* @param string Name of task executor
* @return Task executor
*/
protected TaskExecutor getExecutorByName(String string) {
TaskExecutor taskExecutor = taskExecutors.get(string);
if (!Objects.isNull(taskExecutor)) {
return taskExecutor;
}
String[] commaSepratedStrings = string.split(",");
if (commaSepratedStrings.length == 2) {
// either by number or percentage expected.
Integer[] integers = new Integer[2];
for (int i = 0; i < 2; i++) {
String numberOrPercentage = commaSepratedStrings[i];
String numOrPer = numberOrPercentage.trim();
if (numOrPer.endsWith("%")) {
int percentFromString = getNumberFromString(numOrPer.substring(0, numOrPer.length() - 1));
if (percentFromString != 0) {
int value = percentFromString * cores / 100;
if (value == 0) {
value = 1;
}
integers[i] = value;
} else {
integers[i] = 0;
}
} else {
integers[i] = getNumberFromString(numOrPer);
}
}
if (integers[0] > -1 && integers[1] > -1) {
TaskExecutor taskExecutorWithDefinedNumberOfConsumers = TaskExecutor.getTaskExecutorWithDefinedNumberOfConsumers(integers[0], integers[1]);
taskExecutors.put(string, taskExecutorWithDefinedNumberOfConsumers);
return taskExecutorWithDefinedNumberOfConsumers;
}
}
return null;
}
use of org.ak.trafficController.TaskExecutor in project trafficController by amitkhosla.
the class AnnotationSupportImpl method runControlled.
/**
* Handles controlled annotated methods.
* Controlled annotated methods are run in given executor. Current task waits for the execution to complete.
* This helps in throttling the execution, i.e., at a given time only specified number of executions are allowed for given process.
* In case it is running in Parallel flow, this just add it in task chain which will be handled in parallel.
* @param joinPoint Join point
* @param controlled Controlled
* @return Output of the annotated method
* @throws Throwable In case of exception in annotated mehtod
*/
@Around("execution(@org.ak.trafficController.annotations.api.Controlled * *(..)) && @annotation(controlled)")
public Object runControlled(ProceedingJoinPoint joinPoint, Controlled controlled) throws Throwable {
TaskExecutorDetails taskExecutorDetail = taskHelper.getTaskExecutor(controlled, joinPoint);
Task task = ParallelJoinHelper.getTask();
String nameForTheTaskExecutor = getNameForTaskExecutor(controlled, taskExecutorDetail);
boolean taskExecutorPresent = TaskExecutorsInUseThreadLocal.isTaskExecutorPresent(nameForTheTaskExecutor);
if (taskExecutorPresent) {
if (task == null) {
logger.fine("already from same executor..so processing directly.");
return joinPoint.proceed();
}
}
TaskExecutor taskExecutor = taskExecutorDetail.getTaskExecutor();
if (task != null) {
taskExecutor = addToTaskChainAsCalledFromParallel(joinPoint, controlled, task, taskExecutorPresent, nameForTheTaskExecutor, taskExecutor);
return null;
}
return executeControlled(joinPoint, controlled, nameForTheTaskExecutor, taskExecutor);
}
Aggregations