Search in sources :

Example 11 with CompletionCallback

use of oracle.kubernetes.operator.work.Fiber.CompletionCallback in project weblogic-kubernetes-operator by oracle.

the class FiberGate method startFiberIfLastFiberMatches.

/**
 * Starts Fiber only if the last started Fiber matches the given old Fiber.
 * @param key Key
 * @param old Expected last Fiber
 * @param strategy Step for Fiber to begin with
 * @param packet Packet
 * @param callback Completion callback
 * @return started Fiber, or null, if no Fiber started
 */
public synchronized Fiber startFiberIfLastFiberMatches(String key, Fiber old, Step strategy, Packet packet, CompletionCallback callback) {
    Fiber f = engine.createFiber();
    WaitForOldFiberStep wfofs;
    if (old != null) {
        if (old == PLACEHOLDER) {
            if (gateMap.putIfAbsent(key, f) != null) {
                return null;
            }
        } else if (!gateMap.replace(key, old, f)) {
            return null;
        }
    } else {
        old = gateMap.put(key, f);
    }
    wfofs = new WaitForOldFiberStep(old, strategy);
    f.getComponents().put(ProcessingConstants.FIBER_COMPONENT_NAME, Component.createFor(wfofs));
    f.start(wfofs, packet, new CompletionCallback() {

        @Override
        public void onCompletion(Packet packet) {
            gateMap.remove(key, f);
            callback.onCompletion(packet);
        }

        @Override
        public void onThrowable(Packet packet, Throwable throwable) {
            gateMap.remove(key, f);
            callback.onThrowable(packet, throwable);
        }
    });
    return f;
}
Also used : CompletionCallback(oracle.kubernetes.operator.work.Fiber.CompletionCallback)

Example 12 with CompletionCallback

use of oracle.kubernetes.operator.work.Fiber.CompletionCallback in project weblogic-kubernetes-operator by oracle.

the class StepTest method testFuture.

@Test
public void testFuture() throws InterruptedException, ExecutionException, TimeoutException {
    Step stepline = Step.createStepline(Arrays.asList(Step1.class, Step2.class, Step3.class));
    Packet p = new Packet();
    List<Step> called = new ArrayList<Step>();
    List<Throwable> throwables = new ArrayList<Throwable>();
    Fiber f = engine.createFiber();
    f.start(stepline, p, new CompletionCallback() {

        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public void onCompletion(Packet packet) {
            List<Step> l = (List) packet.get(MARK);
            if (l != null) {
                called.addAll(l);
            }
        }

        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public void onThrowable(Packet packet, Throwable throwable) {
            List<Step> l = (List) packet.get(MARK);
            if (l != null) {
                called.addAll(l);
            }
            throwables.add(throwable);
        }
    });
    f.get(5, TimeUnit.SECONDS);
    assertEquals(3, called.size());
    assertTrue(called.get(0) instanceof Step1);
    assertTrue(called.get(1) instanceof Step2);
    assertTrue(called.get(2) instanceof Step3);
    assertTrue(throwables.isEmpty());
}
Also used : ArrayList(java.util.ArrayList) CompletionCallback(oracle.kubernetes.operator.work.Fiber.CompletionCallback) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 13 with CompletionCallback

use of oracle.kubernetes.operator.work.Fiber.CompletionCallback in project weblogic-kubernetes-operator by oracle.

the class StepTest method testMany.

@Test
public void testMany() throws InterruptedException {
    Step stepline = Step.createStepline(Arrays.asList(Step1.class, Step2.class, Step3.class));
    List<Semaphore> sems = new ArrayList<>();
    List<List<Step>> calls = new ArrayList<>();
    List<List<Throwable>> ts = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        Packet p = new Packet();
        Semaphore signal = new Semaphore(0);
        List<Step> called = new ArrayList<Step>();
        List<Throwable> throwables = new ArrayList<Throwable>();
        sems.add(signal);
        calls.add(called);
        ts.add(throwables);
        engine.createFiber().start(stepline, p, new CompletionCallback() {

            @SuppressWarnings({ "unchecked", "rawtypes" })
            @Override
            public void onCompletion(Packet packet) {
                List<Step> l = (List) packet.get(MARK);
                if (l != null) {
                    called.addAll(l);
                }
                signal.release();
            }

            @SuppressWarnings({ "unchecked", "rawtypes" })
            @Override
            public void onThrowable(Packet packet, Throwable throwable) {
                List<Step> l = (List) packet.get(MARK);
                if (l != null) {
                    called.addAll(l);
                }
                throwables.add(throwable);
                signal.release();
            }
        });
    }
    for (int i = 0; i < 1000; i++) {
        Semaphore signal = sems.get(i);
        List<Step> called = calls.get(i);
        List<Throwable> throwables = ts.get(i);
        boolean result = signal.tryAcquire(5, TimeUnit.SECONDS);
        assertTrue(result);
        assertEquals(3, called.size());
        assertTrue(called.get(0) instanceof Step1);
        assertTrue(called.get(1) instanceof Step2);
        assertTrue(called.get(2) instanceof Step3);
        assertTrue(throwables.isEmpty());
    }
}
Also used : ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) CompletionCallback(oracle.kubernetes.operator.work.Fiber.CompletionCallback) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 14 with CompletionCallback

use of oracle.kubernetes.operator.work.Fiber.CompletionCallback in project weblogic-kubernetes-operator by oracle.

the class StepTest method testRetry.

@Test
public void testRetry() throws InterruptedException {
    Step stepline = Step.createStepline(Arrays.asList(Step1.class, Step2.class, Step3.class));
    Packet p = new Packet();
    Map<Class<? extends BaseStep>, Command> commandMap = new HashMap<>();
    Command c = new Command(RETRY);
    c.setCount(2);
    commandMap.put(Step2.class, c);
    p.put(NA, commandMap);
    Semaphore signal = new Semaphore(0);
    List<Step> called = new ArrayList<Step>();
    List<Throwable> throwables = new ArrayList<Throwable>();
    engine.createFiber().start(stepline, p, new CompletionCallback() {

        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public void onCompletion(Packet packet) {
            List<Step> l = (List) packet.get(MARK);
            if (l != null) {
                called.addAll(l);
            }
            signal.release();
        }

        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public void onThrowable(Packet packet, Throwable throwable) {
            List<Step> l = (List) packet.get(MARK);
            if (l != null) {
                called.addAll(l);
            }
            throwables.add(throwable);
            signal.release();
        }
    });
    boolean result = signal.tryAcquire(5, TimeUnit.SECONDS);
    assertTrue(result);
    assertEquals(5, called.size());
    assertTrue(called.get(0) instanceof Step1);
    assertTrue(called.get(1) instanceof Step2);
    assertTrue(called.get(2) instanceof Step2);
    assertTrue(called.get(3) instanceof Step2);
    assertTrue(called.get(4) instanceof Step3);
    assertTrue(throwables.isEmpty());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) CompletionCallback(oracle.kubernetes.operator.work.Fiber.CompletionCallback) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

CompletionCallback (oracle.kubernetes.operator.work.Fiber.CompletionCallback)14 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Semaphore (java.util.concurrent.Semaphore)7 Packet (oracle.kubernetes.operator.work.Packet)6 HashMap (java.util.HashMap)5 Step (oracle.kubernetes.operator.work.Step)5 ResponseStep (oracle.kubernetes.operator.helpers.ResponseStep)3 V1beta1Ingress (io.kubernetes.client.models.V1beta1Ingress)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 MainTuning (oracle.kubernetes.operator.TuningParameters.MainTuning)2 DomainPresenceInfo (oracle.kubernetes.operator.helpers.DomainPresenceInfo)2 Fiber (oracle.kubernetes.operator.work.Fiber)2 DomainList (oracle.kubernetes.weblogic.domain.v1.DomainList)2 ApiException (io.kubernetes.client.ApiException)1 JSON (io.kubernetes.client.JSON)1