Search in sources :

Example 1 with TaskExecutor

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;
}
Also used : RunnableToBeExecuted(org.ak.trafficController.RunnableToBeExecuted) TaskExecutor(org.ak.trafficController.TaskExecutor) ParallelTask(org.ak.trafficController.ParallelTask) ParallelExecutingTask(org.ak.trafficController.ParallelExecutingTask) ExecutableTask(org.ak.trafficController.ExecutableTask) Task(org.ak.trafficController.Task) UnlinkedTask(org.ak.trafficController.UnlinkedTask) TaskType(org.ak.trafficController.annotations.api.TaskType) AtomicReference(java.util.concurrent.atomic.AtomicReference) ParallelTask(org.ak.trafficController.ParallelTask) Around(org.aspectj.lang.annotation.Around)

Example 2 with TaskExecutor

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskExecutor(org.ak.trafficController.TaskExecutor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 3 with TaskExecutor

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;
}
Also used : TaskExecutor(org.ak.trafficController.TaskExecutor)

Example 4 with 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;
}
Also used : TaskExecutor(org.ak.trafficController.TaskExecutor) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Example 5 with TaskExecutor

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);
}
Also used : ParallelTask(org.ak.trafficController.ParallelTask) ParallelExecutingTask(org.ak.trafficController.ParallelExecutingTask) ExecutableTask(org.ak.trafficController.ExecutableTask) Task(org.ak.trafficController.Task) UnlinkedTask(org.ak.trafficController.UnlinkedTask) TaskExecutor(org.ak.trafficController.TaskExecutor) Around(org.aspectj.lang.annotation.Around)

Aggregations

TaskExecutor (org.ak.trafficController.TaskExecutor)10 Test (org.junit.Test)4 ExecutableTask (org.ak.trafficController.ExecutableTask)2 ParallelExecutingTask (org.ak.trafficController.ParallelExecutingTask)2 ParallelTask (org.ak.trafficController.ParallelTask)2 Task (org.ak.trafficController.Task)2 UnlinkedTask (org.ak.trafficController.UnlinkedTask)2 Around (org.aspectj.lang.annotation.Around)2 ArrayList (java.util.ArrayList)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RunnableToBeExecuted (org.ak.trafficController.RunnableToBeExecuted)1 TaskType (org.ak.trafficController.annotations.api.TaskType)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1