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;
}
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());
}
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());
}
}
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());
}
Aggregations