Skip to content

POC- Orchestration of DLs #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions src/main/java/org/dataloader/orchestration/CF.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package org.dataloader.orchestration;

import java.util.Iterator;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Supplier;

public class CF<T> {

private AtomicReference<Object> result = new AtomicReference<>();

private static final Object NULL = new Object();

private static class ExceptionWrapper {
private final Throwable throwable;

private ExceptionWrapper(Throwable throwable) {
this.throwable = throwable;
}
}

private final LinkedBlockingDeque<CompleteAction<?, ?>> dependedActions = new LinkedBlockingDeque<>();

private static class CompleteAction<T, V> implements Runnable {
Executor executor;
CF<V> toComplete;
CF<T> src;
BiFunction<? super T, Throwable, ? extends V> mapperFn;

public CompleteAction(CF<V> toComplete, CF<T> src, BiFunction<? super T, Throwable, ? extends V> mapperFn, Executor executor) {
this.toComplete = toComplete;
this.src = src;
this.mapperFn = mapperFn;
this.executor = executor;
}

public void execute() {
if (executor != null) {
executor.execute(this);
} else {
toComplete.completeViaMapper(mapperFn, src.result.get());
}

}

@Override
public void run() {
toComplete.completeViaMapper(mapperFn, src.result.get());
src.dependedActions.remove(this);
}
}

private CF() {

}

public <U> CF<U> newIncomplete() {
return new CF<U>();
}

public static <T> CF<T> newComplete(T completed) {
CF<T> result = new CF<>();
result.encodeAndSetResult(completed);
return result;
}

public static <T> CF<T> newExceptionally(Throwable e) {
CF<T> result = new CF<>();
result.encodeAndSetResult(e);
return result;
}

public static <T> CF<T> supplyAsync(Supplier<T> supplier,
Executor executor) {

CF<T> result = new CF<>();
executor.execute(() -> {
try {
result.encodeAndSetResult(supplier.get());
} catch (Throwable ex) {
result.encodeAndSetResult(ex);
}
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks a lot like the actual CompletebleFuture code foe scheduling ;)

java.util.concurrent.CompletableFuture.AsyncRun

return result;
}


public <U> CF<U> map(BiFunction<? super T, Throwable, ? extends U> fn) {
CF<U> newResult = new CF<>();
dependedActions.push(new CompleteAction<>(newResult, this, fn, null));
return newResult;
}

public <U> CF<U> mapAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) {
CF<U> newResult = new CF<>();
dependedActions.push(new CompleteAction<>(newResult, this, fn, executor));
return newResult;
}


public <U> CF<U> compose(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) {
CF<U> newResult = new CF<>();
dependedActions.push(new CompleteAction<>(newResult, this, fn, executor));
return newResult;
}


public boolean complete(T value) {
boolean success = result.compareAndSet(null, value);
fireDependentActions();
return success;
}

private boolean encodeAndSetResult(Object rawValue) {
if (rawValue == null) {
return result.compareAndSet(null, NULL);
} else if (rawValue instanceof Throwable) {
return result.compareAndSet(null, new ExceptionWrapper((Throwable) rawValue));
} else {
return result.compareAndSet(null, rawValue);
}
}

private Object decodeResult(Object rawValue) {
if (rawValue instanceof ExceptionWrapper) {
return ((ExceptionWrapper) rawValue).throwable;
} else if (rawValue == NULL) {
return null;
} else {
return rawValue;
}
}

private void fireDependentActions() {
Iterator<CompleteAction<?, ?>> iterator = dependedActions.iterator();
while (iterator.hasNext()) {
iterator.next().execute();
}
}

private <T, U> void completeViaMapper(BiFunction<? super T, Throwable, ? extends U> fn, Object encodedResult) {
try {
Object decodedResult = decodeResult(encodedResult);
Object mappedResult = fn.apply(
(T) decodedResult,
decodedResult instanceof Throwable ? (Throwable) decodedResult : null
);
this.result.compareAndSet(null, mappedResult);
} catch (Throwable t) {
encodeAndSetResult(t);
}
}


}
12 changes: 12 additions & 0 deletions src/main/java/org/dataloader/orchestration/ImmediateExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.dataloader.orchestration;

import java.util.concurrent.Executor;

class ImmediateExecutor implements Executor {
static final ImmediateExecutor INSTANCE = new ImmediateExecutor();

@Override
public void execute(Runnable command) {
command.run();
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/dataloader/orchestration/ObservingExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.dataloader.orchestration;

import java.util.concurrent.Executor;
import java.util.function.Consumer;

class ObservingExecutor<T> implements Executor {

private final Executor delegate;
private final T state;
private final Consumer<T> callback;

public ObservingExecutor(Executor delegate, T state, Consumer<T> callback) {
this.delegate = delegate;
this.state = state;
this.callback = callback;
}

@Override
public void execute(Runnable command) {
delegate.execute(command);
callback.accept(state);
}
}
116 changes: 116 additions & 0 deletions src/main/java/org/dataloader/orchestration/Orchestrator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.dataloader.orchestration;

import org.dataloader.DataLoader;
import org.dataloader.impl.Assertions;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;

public class Orchestrator<K, V> {

private final Executor executor;
private final Tracker tracker;
private final DataLoader<K, V> startingDL;
private final List<Step<?, ?>> steps = new ArrayList<>();

/**
* This will create a new {@link Orchestrator} that can allow multiple calls to multiple data-loader's
* to be orchestrated so they all run optimally.
*
* @param dataLoader the data loader to start with
* @param <K> the key type
* @param <V> the value type
* @return a new {@link Orchestrator}
*/
public static <K, V> Orchestrator<K, V> orchestrate(DataLoader<K, V> dataLoader) {
return new Orchestrator<>(new Tracker(), dataLoader, ImmediateExecutor.INSTANCE);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs injection of of the "tracker implementation" I think - maybe???


// TODO - make this a builder
public static <K, V> Orchestrator<K, V> orchestrate(DataLoader<K, V> dataLoader, Executor executor) {
return new Orchestrator<>(new Tracker(), dataLoader, executor);
}

private Orchestrator(Tracker tracker, DataLoader<K, V> dataLoader, Executor executor) {
this.tracker = tracker;
this.startingDL = dataLoader;
this.executor = executor;
}

public Tracker getTracker() {
return tracker;
}

public Executor getExecutor() {
return executor;
}


public Step<K, V> load(K key) {
return load(key, null);
}

public Step<K, V> load(K key, Object keyContext) {
return Step.loadImpl(this, castAs(startingDL), key, keyContext);
}

static <T> T castAs(Object o) {
//noinspection unchecked
return (T) o;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generics are hard!



<KT, VT> void record(Step<KT, VT> step) {
steps.add(step);
tracker.incrementStepCount();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we know how many steps we can have

}

/**
* This is the callback point saying to start the DataLoader loading process.
* <p>
* The type of object returned here depends on the value type of the last Step. We cant be truly generic
* here and must be case.
*
* @param <VT> the value type
* @return the final composed value
*/
<VT> CompletableFuture<VT> execute() {
Assertions.assertState(!steps.isEmpty(), () -> "How can the steps to run be empty??");
int index = 0;
Step<?, ?> firstStep = steps.get(index);

CompletableFuture<Object> currentCF = castAs(firstStep.codeToRun().apply(null)); // first load uses variable capture
whenComplete(index, firstStep, currentCF);

for (index++; index < steps.size(); index++) {
Step<?, ?> nextStep = steps.get(index);
Function<Object, CompletableFuture<?>> codeToRun = castAs(nextStep.codeToRun());
CompletableFuture<Object> nextCF = currentCF.thenCompose(value -> castAs(codeToRun.apply(value)));
currentCF = nextCF;

// side effect when this step is complete
whenComplete(index, nextStep, nextCF);
}
return castAs(currentCF);

}

private void whenComplete(int index, Step<?, ?> step, CompletableFuture<Object> cf) {
cf.whenComplete((v, throwable) -> {
getTracker().loadCallComplete(step.dataLoader());
// replace with instrumentation code
if (throwable != null) {
// TODO - should we be cancelling future steps here - no need for dispatch tracking if they will never run
System.out.println("A throwable has been thrown on step " + index + ": " + throwable.getMessage());
throwable.printStackTrace(System.out);
} else {
System.out.println("step " + index + " returned : " + v);
}
});
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we can have tracker side effects to know things.

We should probably also handle exceptions by cancelling all "expectations on future load" since they will not happen

Maybe we just mark the Tracker as "exceptional"



}
Loading
Loading