[REEF-1898] REEF Runtime Mock This adds a new REEF mock runtime, which can be used to create regression tests for an application dirver.
JIRA: [REEF-1898](https://issues.apache.org/jira/browse/REEF-1898) Pull Request: This closes #1390 Project: http://git-wip-us.apache.org/repos/asf/reef/repo Commit: http://git-wip-us.apache.org/repos/asf/reef/commit/72ecec74 Tree: http://git-wip-us.apache.org/repos/asf/reef/tree/72ecec74 Diff: http://git-wip-us.apache.org/repos/asf/reef/diff/72ecec74 Branch: refs/heads/master Commit: 72ecec743598d0dd74d37d465c72a3eb7c050955 Parents: 51b76fc Author: tcondie <[email protected]> Authored: Tue Oct 10 12:47:30 2017 -0700 Committer: Markus Weimer <[email protected]> Committed: Tue Oct 24 15:55:33 2017 -0700 ---------------------------------------------------------------------- .../reef/driver/evaluator/EvaluatorRequest.java | 7 + .../driver/evaluator/EvaluatorRequestor.java | 3 +- .../common/driver/EvaluatorRequestorImpl.java | 1 + lang/java/reef-runtime-mock/pom.xml | 67 +++ .../org/apache/reef/mock/AutoCompletable.java | 45 ++ .../mock/DefaultTaskReturnValueProvider.java | 41 ++ .../org/apache/reef/mock/MockConfiguration.java | 173 +++++++ .../java/org/apache/reef/mock/MockFailure.java | 67 +++ .../java/org/apache/reef/mock/MockRuntime.java | 76 ++++ .../reef/mock/MockTaskReturnValueProvider.java | 44 ++ .../org/apache/reef/mock/ProcessRequest.java | 54 +++ .../java/org/apache/reef/mock/package-info.java | 40 ++ .../reef/mock/request/AllocateEvaluator.java | 72 +++ .../apache/reef/mock/request/CloseContext.java | 76 ++++ .../reef/mock/request/CloseEvaluator.java | 78 ++++ .../org/apache/reef/mock/request/CloseTask.java | 89 ++++ .../apache/reef/mock/request/CompleteTask.java | 82 ++++ .../apache/reef/mock/request/CreateContext.java | 76 ++++ .../reef/mock/request/CreateContextAndTask.java | 98 ++++ .../apache/reef/mock/request/CreateTask.java | 89 ++++ .../mock/request/ProcessRequestInternal.java | 44 ++ .../request/SendMessageDriverToContext.java | 81 ++++ .../mock/request/SendMessageDriverToTask.java | 81 ++++ .../apache/reef/mock/request/SuspendTask.java | 90 ++++ .../apache/reef/mock/request/package-info.java | 23 + .../reef/mock/runtime/MockActiveContext.java | 139 ++++++ .../mock/runtime/MockAllocatedEvalautor.java | 149 ++++++ .../org/apache/reef/mock/runtime/MockClock.java | 120 +++++ .../reef/mock/runtime/MockClosedContext.java | 71 +++ .../reef/mock/runtime/MockCompletedTask.java | 57 +++ .../mock/runtime/MockEvaluatorDescriptor.java | 64 +++ .../mock/runtime/MockEvaluatorRequestor.java | 85 ++++ .../reef/mock/runtime/MockFailedContext.java | 93 ++++ .../reef/mock/runtime/MockFailedEvaluator.java | 79 ++++ .../reef/mock/runtime/MockNodeDescriptor.java | 68 +++ .../reef/mock/runtime/MockRunningTask.java | 97 ++++ .../reef/mock/runtime/MockRuntimeDriver.java | 454 +++++++++++++++++++ .../reef/mock/runtime/MockSuspendedTask.java | 54 +++ .../org/apache/reef/mock/runtime/MockUtils.java | 48 ++ .../apache/reef/mock/runtime/package-info.java | 23 + .../org/apache/reef/mock/BasicMockTests.java | 205 +++++++++ .../org/apache/reef/mock/MockApplication.java | 275 +++++++++++ .../java/org/apache/reef/mock/package-info.java | 23 + pom.xml | 1 + 44 files changed, 3700 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequest.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequest.java b/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequest.java index dfed8f6..4c48a24 100644 --- a/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequest.java +++ b/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequest.java @@ -307,5 +307,12 @@ public final class EvaluatorRequest { return new EvaluatorRequest(this.n, this.megaBytes, this.cores, this.nodeNames, this.rackNames, this.runtimeName, this.relaxLocality); } + + /** + * Short-circuit submission method for subclass implementations. + */ + public void submit() { + throw new UnsupportedOperationException(); + } } } http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequestor.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequestor.java b/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequestor.java index 534b0ff..684553e 100644 --- a/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequestor.java +++ b/lang/java/reef-common/src/main/java/org/apache/reef/driver/evaluator/EvaluatorRequestor.java @@ -21,7 +21,6 @@ package org.apache.reef.driver.evaluator; import org.apache.reef.annotations.Provided; import org.apache.reef.annotations.audience.DriverSide; import org.apache.reef.annotations.audience.Public; -import org.apache.reef.runtime.common.driver.EvaluatorRequestorImpl; /** * Interface through which Evaluators can be requested. @@ -41,5 +40,5 @@ public interface EvaluatorRequestor { * Get a new Builder for the evaluator with fluid interface. * @return Builder for the evaluator */ - EvaluatorRequestorImpl.Builder newRequest(); + EvaluatorRequest.Builder newRequest(); } http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java b/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java index 992659f..08b385d 100644 --- a/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java +++ b/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java @@ -129,6 +129,7 @@ public final class EvaluatorRequestorImpl implements EvaluatorRequestor { * {@link EvaluatorRequest}s are built using this builder. */ public final class Builder extends EvaluatorRequest.Builder<Builder> { + @Override public synchronized void submit() { EvaluatorRequestorImpl.this.submit(this.build()); } http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/pom.xml ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/pom.xml b/lang/java/reef-runtime-mock/pom.xml new file mode 100644 index 0000000..d3b63a8 --- /dev/null +++ b/lang/java/reef-runtime-mock/pom.xml @@ -0,0 +1,67 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <groupId>org.apache.reef</groupId> + <artifactId>reef-project</artifactId> + <version>0.17.0-SNAPSHOT</version> + <relativePath>../../..</relativePath> + </parent> + + <properties> + <rootPath>${basedir}/../../..</rootPath> + </properties> + + <modelVersion>4.0.0</modelVersion> + <artifactId>reef-runtime-mock</artifactId> + <name>REEF Runtime Mock</name> + <description>REEF Mockup Library for testing application control flow logic.</description> + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-checkstyle-plugin</artifactId> + <configuration> + <configLocation>lang/java/reef-common/src/main/resources/checkstyle-strict.xml</configLocation> + </configuration> + </plugin> + </plugins> + </pluginManagement> + </build> + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>tang</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>reef-common</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> +</project> + http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/AutoCompletable.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/AutoCompletable.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/AutoCompletable.java new file mode 100644 index 0000000..173b410 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/AutoCompletable.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock; + +import org.apache.reef.annotations.Unstable; + +/** + * Indicates that a process request should auto complete. + */ +@Unstable +public interface AutoCompletable { + + /** + * @return true if should auto complete + */ + boolean doAutoComplete(); + + /** + * Set auto complete. + * @param value to set + */ + void setAutoComplete(final boolean value); + + /** + * @return auto complete process request + */ + ProcessRequest getCompletionProcessRequest(); +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/DefaultTaskReturnValueProvider.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/DefaultTaskReturnValueProvider.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/DefaultTaskReturnValueProvider.java new file mode 100644 index 0000000..2ae81d4 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/DefaultTaskReturnValueProvider.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.reef.mock; + +import org.apache.reef.mock.runtime.MockRunningTask; + +import javax.inject.Inject; + +/** + * A default task return value provider. + */ +final class DefaultTaskReturnValueProvider implements MockTaskReturnValueProvider { + + @Inject + DefaultTaskReturnValueProvider() { + + } + + @Override + public byte[] getReturnValue(final MockRunningTask task) { + return new byte[0]; + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockConfiguration.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockConfiguration.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockConfiguration.java new file mode 100644 index 0000000..beec5c4 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockConfiguration.java @@ -0,0 +1,173 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.reef.mock; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.driver.client.JobMessageObserver; +import org.apache.reef.driver.context.ActiveContext; +import org.apache.reef.driver.context.ClosedContext; +import org.apache.reef.driver.context.ContextMessage; +import org.apache.reef.driver.context.FailedContext; +import org.apache.reef.driver.evaluator.AllocatedEvaluator; +import org.apache.reef.driver.evaluator.CompletedEvaluator; +import org.apache.reef.driver.evaluator.EvaluatorRequestor; +import org.apache.reef.driver.evaluator.FailedEvaluator; +import org.apache.reef.driver.parameters.*; +import org.apache.reef.driver.task.*; +import org.apache.reef.mock.runtime.MockClock; +import org.apache.reef.mock.runtime.MockEvaluatorRequestor; +import org.apache.reef.mock.runtime.MockRuntimeDriver; +import org.apache.reef.tang.formats.ConfigurationModule; +import org.apache.reef.tang.formats.ConfigurationModuleBuilder; +import org.apache.reef.tang.formats.OptionalImpl; +import org.apache.reef.tang.formats.RequiredImpl; +import org.apache.reef.wake.EventHandler; +import org.apache.reef.wake.time.Clock; +import org.apache.reef.wake.time.event.StartTime; +import org.apache.reef.wake.time.event.StopTime; + +/** + * Configure a mock runtime. + */ +@Unstable +public class MockConfiguration extends ConfigurationModuleBuilder { + + /** + * The event handler invoked right after the driver boots up. + */ + public static final RequiredImpl<EventHandler<StartTime>> ON_DRIVER_STARTED = new RequiredImpl<>(); + + /** + * The event handler invoked right before the driver shuts down. Defaults to ignore. + */ + public static final OptionalImpl<EventHandler<StopTime>> ON_DRIVER_STOP = new OptionalImpl<>(); + + // ***** EVALUATOR HANDLER BINDINGS: + + /** + * Event handler for allocated evaluators. Defaults to returning the evaluator if not bound. + */ + public static final OptionalImpl<EventHandler<AllocatedEvaluator>> ON_EVALUATOR_ALLOCATED = new OptionalImpl<>(); + + /** + * Event handler for completed evaluators. Defaults to logging if not bound. + */ + public static final OptionalImpl<EventHandler<CompletedEvaluator>> ON_EVALUATOR_COMPLETED = new OptionalImpl<>(); + + /** + * Event handler for failed evaluators. Defaults to job failure if not bound. + */ + public static final OptionalImpl<EventHandler<FailedEvaluator>> ON_EVALUATOR_FAILED = new OptionalImpl<>(); + + // ***** TASK HANDLER BINDINGS: + + /** + * Event handler for task messages. Defaults to logging if not bound. + */ + public static final OptionalImpl<EventHandler<TaskMessage>> ON_TASK_MESSAGE = new OptionalImpl<>(); + + /** + * Event handler for completed tasks. Defaults to closing the context the task ran on if not bound. + */ + public static final OptionalImpl<EventHandler<CompletedTask>> ON_TASK_COMPLETED = new OptionalImpl<>(); + + /** + * Event handler for failed tasks. Defaults to job failure if not bound. + */ + public static final OptionalImpl<EventHandler<FailedTask>> ON_TASK_FAILED = new OptionalImpl<>(); + + /** + * Event handler for running tasks. Defaults to logging if not bound. + */ + public static final OptionalImpl<EventHandler<RunningTask>> ON_TASK_RUNNING = new OptionalImpl<>(); + + /** + * Event handler for suspended tasks. Defaults to job failure if not bound. Rationale: many jobs don't support + * task suspension. Hence, this parameter should be optional. The only sane default is to crash the job, then. + */ + public static final OptionalImpl<EventHandler<SuspendedTask>> ON_TASK_SUSPENDED = new OptionalImpl<>(); + + // ***** CONTEXT HANDLER BINDINGS: + + /** + * Event handler for active context. Defaults to closing the context if not bound. + */ + public static final OptionalImpl<EventHandler<ActiveContext>> ON_CONTEXT_ACTIVE = new OptionalImpl<>(); + + /** + * Event handler for closed context. Defaults to logging if not bound. + */ + public static final OptionalImpl<EventHandler<ClosedContext>> ON_CONTEXT_CLOSED = new OptionalImpl<>(); + + /** + * Event handler for closed context. Defaults to job failure if not bound. + */ + public static final OptionalImpl<EventHandler<FailedContext>> ON_CONTEXT_FAILED = new OptionalImpl<>(); + + /** + * Event handler for context messages. Defaults to logging if not bound. + */ + public static final OptionalImpl<EventHandler<ContextMessage>> ON_CONTEXT_MESSAGE = new OptionalImpl<>(); + + + /** + * Receiver of messages sent by the Driver to the client. + */ + public static final OptionalImpl<JobMessageObserver> ON_JOB_MESSAGE = new OptionalImpl<>(); + + /** + * An implementation of a task return value provider. + */ + public static final OptionalImpl<MockTaskReturnValueProvider> TASK_RETURN_VALUE_PROVIDER = new OptionalImpl<>(); + + public static final ConfigurationModule CONF = new MockConfiguration() + .bindImplementation(EvaluatorRequestor.class, MockEvaluatorRequestor.class) // requesting evaluators + .bindImplementation(MockRuntime.class, MockRuntimeDriver.class) + .bindImplementation(MockFailure.class, MockRuntimeDriver.class) + .bindImplementation(Clock.class, MockClock.class) + .bindImplementation(MockTaskReturnValueProvider.class, TASK_RETURN_VALUE_PROVIDER) + + // client handlers + .bindImplementation(JobMessageObserver.class, ON_JOB_MESSAGE) // sending message to job client + + // Driver start/stop handlers + .bindSetEntry(DriverStartHandler.class, ON_DRIVER_STARTED) + .bindSetEntry(Clock.StopHandler.class, ON_DRIVER_STOP) + + // Evaluator handlers + .bindSetEntry(EvaluatorAllocatedHandlers.class, ON_EVALUATOR_ALLOCATED) + .bindSetEntry(EvaluatorCompletedHandlers.class, ON_EVALUATOR_COMPLETED) + .bindSetEntry(EvaluatorFailedHandlers.class, ON_EVALUATOR_FAILED) + + // Task handlers + .bindSetEntry(TaskRunningHandlers.class, ON_TASK_RUNNING) + .bindSetEntry(TaskFailedHandlers.class, ON_TASK_FAILED) + .bindSetEntry(TaskMessageHandlers.class, ON_TASK_MESSAGE) + .bindSetEntry(TaskCompletedHandlers.class, ON_TASK_COMPLETED) + .bindSetEntry(TaskSuspendedHandlers.class, ON_TASK_SUSPENDED) + + // Context handlers + .bindSetEntry(ContextActiveHandlers.class, ON_CONTEXT_ACTIVE) + .bindSetEntry(ContextClosedHandlers.class, ON_CONTEXT_CLOSED) + .bindSetEntry(ContextMessageHandlers.class, ON_CONTEXT_MESSAGE) + .bindSetEntry(ContextFailedHandlers.class, ON_CONTEXT_FAILED) + + .build(); + +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockFailure.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockFailure.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockFailure.java new file mode 100644 index 0000000..f8822a2 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockFailure.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.reef.mock; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.driver.context.ActiveContext; +import org.apache.reef.driver.evaluator.AllocatedEvaluator; +import org.apache.reef.driver.task.RunningTask; + +import java.util.Collection; + +/** + * Used to fail running REEF entities i.e., Evaluators, Contexts, Tasks. + */ +@Unstable +public interface MockFailure { + + /** + * @return current Collection of allocated evaluators. + */ + Collection<AllocatedEvaluator> getCurrentAllocatedEvaluators(); + + /** + * Fail an allocated evaluator. + * @param evaluator to be failed + */ + void fail(final AllocatedEvaluator evaluator); + + /** + * @return current Collection of active contexts + */ + Collection<ActiveContext> getCurrentActiveContexts(); + + /** + * Fail an ActiveContext. + * @param context to be failed + */ + void fail(final ActiveContext context); + + /** + * @return current Collection of running tasks + */ + Collection<RunningTask> getCurrentRunningTasks(); + + /** + * Fail a running task. + * @param task to be failed + */ + void fail(final RunningTask task); +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockRuntime.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockRuntime.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockRuntime.java new file mode 100644 index 0000000..0e09f5d --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockRuntime.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock; + +import org.apache.reef.annotations.Unstable; + +/** + * Mock API used to drive the evaluation of ProcessRequest + * events, which are triggered by the Application driver. + * Clients used this to determine whether a particular ProcessRequest + * event should succeed or fail. + */ +@Unstable +public interface MockRuntime extends MockFailure { + + /** + * Initiate the start time event to the application driver. + */ + void start(); + + /** + * Initiate the stop time event to the application driver. + */ + void stop(); + + /** + * @return true if there is an outstanding ProcessRequest + */ + boolean hasProcessRequest(); + + /** + * The client (caller) is responsible for determining what + * to do with a ProcessRequest event. There are three options: + * 1. Pass to the succeed method, which signals success to the driver. + * 2. Pass to the fail method, signaling failure to the driver. + * 3. Drop it on the floor (e.g., network failure). + * + * @return the next ProcessRequest object to be processed. + */ + ProcessRequest getNextProcessRequest(); + + /** + * The driver will be informed that the operation corresponding + * to the ProcessRequest succeeded, and will be given any relevant + * data structures e.g., AllocatedEvaluator, RunningTask, etc. + * + * @param request to be processed successfully + */ + void succeed(final ProcessRequest request); + + /** + * The driver will be informed that the operation corresponding + * to the PRocessRequest failed, and will be given any relevant + * data structures e.g., FailedEvaluator, FailedTask, etc. + * + * @param request to be failed. + */ + void fail(final ProcessRequest request); +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockTaskReturnValueProvider.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockTaskReturnValueProvider.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockTaskReturnValueProvider.java new file mode 100644 index 0000000..a0e794b --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/MockTaskReturnValueProvider.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.reef.mock; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.driver.task.CompletedTask; +import org.apache.reef.mock.runtime.MockRunningTask; +import org.apache.reef.tang.annotations.DefaultImplementation; + +/** + * Clients bind an implementation of this interface, which + * will be used to create a mock return value for a mock + * task execution. This return value will be returned by + * the {@link CompletedTask#get()}} method. + */ +@Unstable +@DefaultImplementation(DefaultTaskReturnValueProvider.class) +public interface MockTaskReturnValueProvider { + + /** + * Provide a valid return value for the {@link CompletedTask#get()} method. + * @param task that is to be provided with a return value + * @return {@link org.apache.reef.task.Task#call(byte[])} return value + */ + byte[] getReturnValue(final MockRunningTask task); +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/ProcessRequest.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/ProcessRequest.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/ProcessRequest.java new file mode 100644 index 0000000..09e9691 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/ProcessRequest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock; + +import org.apache.reef.annotations.Unstable; + +/** + * A ProcessRequest refers to an outstanding event that is + * waiting to be processed by the REEF mock runtime. Clients + * are responsible for deciding how a ProcessRequest should be + * handled, by either: + * 1. successfully processing the request + * 2. unsucessfully processing the request + * 3. dropping the processing request (i.e., loosing it) + * These decisions are conveyed through the {MockRuntime} API. + */ +@Unstable +public interface ProcessRequest extends AutoCompletable { + /** + * process request type. + */ + enum Type { + ALLOCATE_EVALUATOR, + CLOSE_EVALUATOR, + CREATE_CONTEXT, + CLOSE_CONTEXT, + CREATE_TASK, + SUSPEND_TASK, + CLOSE_TASK, + COMPLETE_TASK, + CREATE_CONTEXT_AND_TASK, + SEND_MESSAGE_DRIVER_TO_TASK, + SEND_MESSAGE_DRIVER_TO_CONTEXT + } + + Type getType(); +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/package-info.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/package-info.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/package-info.java new file mode 100644 index 0000000..fdda864 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/package-info.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +/** + * Mock runtime API. + * + * Mock runtime is meant to mimic the semantics of the REEF runtime and + * allow: + * 1. Applications to driver the forward progress of processing REEF events. + * See {@link org.apache.reef.mock.MockRuntime} API + * 2. Control the advancement of the Clock and Alarm callbacks. + * See {@link org.apache.reef.mock.runtime.MockClock} + * 3. Inject failures into the REEF applications. + * See {@link org.apache.reef.mock.MockFailure} + * + * Use {@link org.apache.reef.mock.MockConfiguration} to bind your REEF + * driver application event handlers. + * + * Use {@link org.apache.reef.mock.MockRuntime#start()} to trigger the + * driver start event and {@link org.apache.reef.mock.MockRuntime#stop()}} + * or {@link org.apache.reef.mock.runtime.MockClock#close()} to trigger the driver + * stop event. + */ +package org.apache.reef.mock; http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/AllocateEvaluator.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/AllocateEvaluator.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/AllocateEvaluator.java new file mode 100644 index 0000000..9d6e400 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/AllocateEvaluator.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.evaluator.FailedEvaluator; +import org.apache.reef.mock.ProcessRequest; +import org.apache.reef.mock.runtime.MockAllocatedEvalautor; +import org.apache.reef.mock.runtime.MockFailedEvaluator; + +/** + * Allocate Evaluator process request. + */ +@Unstable +@Private +public final class AllocateEvaluator implements + ProcessRequestInternal<MockAllocatedEvalautor, FailedEvaluator> { + + private final MockAllocatedEvalautor evaluator; + + public AllocateEvaluator(final MockAllocatedEvalautor evaluator) { + this.evaluator = evaluator; + } + + @Override + public Type getType() { + return Type.ALLOCATE_EVALUATOR; + } + + @Override + public MockAllocatedEvalautor getSuccessEvent() { + return this.evaluator; + } + + @Override + public FailedEvaluator getFailureEvent() { + return new MockFailedEvaluator(evaluator.getId()); + } + + @Override + public boolean doAutoComplete() { + return false; + } + + @Override + public void setAutoComplete(final boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseContext.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseContext.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseContext.java new file mode 100644 index 0000000..00bdf3c --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseContext.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.context.ClosedContext; +import org.apache.reef.driver.context.FailedContext; +import org.apache.reef.mock.AutoCompletable; +import org.apache.reef.mock.ProcessRequest; +import org.apache.reef.mock.runtime.MockActiveContext; +import org.apache.reef.mock.runtime.MockClosedContext; +import org.apache.reef.mock.runtime.MockFailedContext; + +/** + * close context process request. + */ +@Unstable +@Private +public final class CloseContext implements + ProcessRequestInternal<ClosedContext, FailedContext>, + AutoCompletable { + + private final MockActiveContext context; + + public CloseContext(final MockActiveContext context) { + this.context = context; + } + + @Override + public Type getType() { + return Type.CLOSE_CONTEXT; + } + + @Override + public MockClosedContext getSuccessEvent() { + return new MockClosedContext(this.context); + } + + @Override + public FailedContext getFailureEvent() { + return new MockFailedContext(this.context); + } + + @Override + public boolean doAutoComplete() { + return !this.context.getParentContext().isPresent(); + } + + @Override + public void setAutoComplete(final boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + return new CloseEvaluator(this.context.getEvaluator()); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseEvaluator.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseEvaluator.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseEvaluator.java new file mode 100644 index 0000000..6ef2b9f --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseEvaluator.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.evaluator.CompletedEvaluator; +import org.apache.reef.driver.evaluator.FailedEvaluator; +import org.apache.reef.mock.ProcessRequest; +import org.apache.reef.mock.runtime.MockAllocatedEvalautor; +import org.apache.reef.mock.runtime.MockFailedEvaluator; + +/** + * close evaluator request. + */ +@Unstable +@Private +public final class CloseEvaluator implements ProcessRequestInternal<CompletedEvaluator, FailedEvaluator> { + + private final MockAllocatedEvalautor evaluator; + + public CloseEvaluator(final MockAllocatedEvalautor evaluator) { + this.evaluator = evaluator; + } + + @Override + public Type getType() { + return Type.CLOSE_EVALUATOR; + } + + @Override + public CompletedEvaluator getSuccessEvent() { + return new CompletedEvaluator() { + @Override + public String getId() { + return evaluator.getId(); + } + }; + } + + @Override + public FailedEvaluator getFailureEvent() { + // TODO[initialize remaining failed contstructer fields] + return new MockFailedEvaluator(evaluator.getId()); + } + + @Override + public boolean doAutoComplete() { + return false; + } + + @Override + public void setAutoComplete(final boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseTask.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseTask.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseTask.java new file mode 100644 index 0000000..504161e --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CloseTask.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.task.CompletedTask; +import org.apache.reef.driver.task.FailedTask; +import org.apache.reef.mock.MockTaskReturnValueProvider; +import org.apache.reef.mock.ProcessRequest; +import org.apache.reef.mock.runtime.MockCompletedTask; +import org.apache.reef.mock.runtime.MockRunningTask; +import org.apache.reef.util.Optional; + +/** + * close task process request. + */ +@Unstable +@Private +public final class CloseTask implements ProcessRequestInternal<CompletedTask, FailedTask> { + + private final MockRunningTask task; + + private final MockTaskReturnValueProvider taskReturnValueProvider; + + public CloseTask( + final MockRunningTask task, + final MockTaskReturnValueProvider taskReturnValueProvider) { + this.task = task; + this.taskReturnValueProvider = taskReturnValueProvider; + } + + public MockRunningTask getTask() { + return task; + } + + @Override + public Type getType() { + return Type.CLOSE_TASK; + } + + @Override + public MockCompletedTask getSuccessEvent() { + return new MockCompletedTask(this.task, this.taskReturnValueProvider.getReturnValue(task)); + } + + @Override + public FailedTask getFailureEvent() { + return new FailedTask( + task.getId(), + "mock", + Optional.<String>empty(), + Optional.<Throwable>empty(), + Optional.<byte[]>empty(), + Optional.of(this.task.getActiveContext())); + } + + @Override + public boolean doAutoComplete() { + return false; + } + + @Override + public void setAutoComplete(final boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CompleteTask.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CompleteTask.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CompleteTask.java new file mode 100644 index 0000000..25e6df8 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CompleteTask.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.task.CompletedTask; +import org.apache.reef.driver.task.FailedTask; +import org.apache.reef.mock.MockTaskReturnValueProvider; +import org.apache.reef.mock.ProcessRequest; +import org.apache.reef.mock.runtime.MockCompletedTask; +import org.apache.reef.mock.runtime.MockRunningTask; + +/** + * close task process request. + */ +@Unstable +@Private +public final class CompleteTask implements ProcessRequestInternal<CompletedTask, FailedTask> { + + private final MockRunningTask task; + + private final MockTaskReturnValueProvider returnValueProvider; + + public CompleteTask( + final MockRunningTask task, + final MockTaskReturnValueProvider returnValueProvider) { + this.task = task; + this.returnValueProvider = returnValueProvider; + } + + public MockRunningTask getTask() { + return task; + } + + @Override + public Type getType() { + return Type.COMPLETE_TASK; + } + + @Override + public CompletedTask getSuccessEvent() { + return new MockCompletedTask(this.task, this.returnValueProvider.getReturnValue(task)); + } + + @Override + public FailedTask getFailureEvent() { + return null; + } + + @Override + public boolean doAutoComplete() { + return false; + } + + @Override + public void setAutoComplete(final boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateContext.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateContext.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateContext.java new file mode 100644 index 0000000..e9d533b --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateContext.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.context.FailedContext; +import org.apache.reef.mock.AutoCompletable; +import org.apache.reef.mock.ProcessRequest; +import org.apache.reef.mock.runtime.MockActiveContext; +import org.apache.reef.mock.runtime.MockFailedContext; + +/** + * create context process request. + */ +@Unstable +@Private +public final class CreateContext implements + ProcessRequestInternal<MockActiveContext, FailedContext>, + AutoCompletable { + + private final MockActiveContext context; + + private boolean autoComplete = false; + + public CreateContext(final MockActiveContext context) { + this.context = context; + } + + @Override + public Type getType() { + return Type.CREATE_CONTEXT; + } + + @Override + public MockActiveContext getSuccessEvent() { + return this.context; + } + + @Override + public FailedContext getFailureEvent() { + return new MockFailedContext(this.context); + } + + @Override + public boolean doAutoComplete() { + return this.autoComplete; + } + + @Override + public void setAutoComplete(final boolean value) { + this.autoComplete = value; + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + return new CloseContext(this.context); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateContextAndTask.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateContextAndTask.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateContextAndTask.java new file mode 100644 index 0000000..2169bfd --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateContextAndTask.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.context.ActiveContext; +import org.apache.reef.driver.task.FailedTask; +import org.apache.reef.io.Tuple; +import org.apache.reef.mock.AutoCompletable; +import org.apache.reef.mock.MockTaskReturnValueProvider; +import org.apache.reef.mock.ProcessRequest; +import org.apache.reef.mock.runtime.MockActiveContext; +import org.apache.reef.mock.runtime.MockFailedContext; +import org.apache.reef.mock.runtime.MockRunningTask; +import org.apache.reef.util.Optional; + +/** + * create context and task process request. + */ +@Unstable +@Private +public final class CreateContextAndTask implements + ProcessRequestInternal<Tuple<MockActiveContext, MockRunningTask>, Tuple<MockFailedContext, FailedTask>>, + AutoCompletable { + + private final MockActiveContext context; + + private final MockRunningTask task; + + private final MockTaskReturnValueProvider taskReturnValueProvider; + + private boolean autoComplete = true; + + public CreateContextAndTask( + final MockActiveContext context, + final MockRunningTask task, + final MockTaskReturnValueProvider taskReturnValueProvider) { + this.context = context; + this.task = task; + this.taskReturnValueProvider = taskReturnValueProvider; + } + + @Override + public Type getType() { + return Type.CREATE_CONTEXT_AND_TASK; + } + + @Override + public Tuple<MockActiveContext, MockRunningTask> getSuccessEvent() { + return new Tuple<>(this.context, this.task); + } + + @Override + public Tuple<MockFailedContext, FailedTask> getFailureEvent() { + return new Tuple<>( + new MockFailedContext(this.context), + new FailedTask( + this.task.getId(), + "mock", + Optional.<String>empty(), + Optional.<Throwable>empty(), + Optional.<byte[]>empty(), + Optional.of((ActiveContext)this.context))); + } + + @Override + public boolean doAutoComplete() { + return this.autoComplete; + } + + @Override + public void setAutoComplete(final boolean value) { + this.autoComplete = value; + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + return new CompleteTask(this.task, this.taskReturnValueProvider); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateTask.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateTask.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateTask.java new file mode 100644 index 0000000..a5eed49 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/CreateTask.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.task.FailedTask; +import org.apache.reef.driver.task.RunningTask; +import org.apache.reef.mock.AutoCompletable; +import org.apache.reef.mock.MockTaskReturnValueProvider; +import org.apache.reef.mock.ProcessRequest; +import org.apache.reef.mock.runtime.MockRunningTask; +import org.apache.reef.util.Optional; + +/** + * create task process request. + */ +@Unstable +@Private +public final class CreateTask implements + ProcessRequestInternal<RunningTask, FailedTask>, + AutoCompletable { + + private final MockRunningTask task; + + private final MockTaskReturnValueProvider returnValueProvider; + + private boolean autoComplete = true; + + public CreateTask( + final MockRunningTask task, + final MockTaskReturnValueProvider returnValueProvider) { + this.task = task; + this.returnValueProvider = returnValueProvider; + } + + @Override + public Type getType() { + return Type.CREATE_TASK; + } + + @Override + public boolean doAutoComplete() { + return this.autoComplete; + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + return new CompleteTask(this.task, this.returnValueProvider); + } + + @Override + public void setAutoComplete(final boolean value) { + this.autoComplete = value; + } + + @Override + public MockRunningTask getSuccessEvent() { + return this.task; + } + + @Override + public FailedTask getFailureEvent() { + return new FailedTask( + this.task.getId(), + "mock", + Optional.<String>empty(), + Optional.<Throwable>empty(), + Optional.<byte[]>empty(), + Optional.of(this.task.getActiveContext())); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/ProcessRequestInternal.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/ProcessRequestInternal.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/ProcessRequestInternal.java new file mode 100644 index 0000000..b9dcd2b --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/ProcessRequestInternal.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.mock.ProcessRequest; + +/** + * internal process request API. + * @param <S> successful event + * @param <F> failure event + */ +@Unstable +@Private +public interface ProcessRequestInternal<S, F> extends ProcessRequest { + + /** + * @return the outcome of a successful processing of this request + */ + S getSuccessEvent(); + + /** + * @return the outcome of an unsuccessful processing of this request + */ + F getFailureEvent(); +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SendMessageDriverToContext.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SendMessageDriverToContext.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SendMessageDriverToContext.java new file mode 100644 index 0000000..74faa60 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SendMessageDriverToContext.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.context.ActiveContext; +import org.apache.reef.mock.ProcessRequest; + +/** + * send message from driver to context process request. + */ +@Unstable +@Private +public final class SendMessageDriverToContext implements + ProcessRequestInternal<Object, Object> { + + private final ActiveContext context; + + private final byte[] message; + + public SendMessageDriverToContext(final ActiveContext context, final byte[] message) { + this.context = context; + this.message = message; + } + + @Override + public Type getType() { + return Type.SEND_MESSAGE_DRIVER_TO_CONTEXT; + } + + public ActiveContext getContext() { + return this.context; + } + + public byte[] getMessage() { + return this.message; + } + + @Override + public Object getSuccessEvent() { + throw new UnsupportedOperationException(); + } + + @Override + public Object getFailureEvent() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean doAutoComplete() { + return false; + } + + @Override + public void setAutoComplete(final boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SendMessageDriverToTask.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SendMessageDriverToTask.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SendMessageDriverToTask.java new file mode 100644 index 0000000..d6dfaaa --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SendMessageDriverToTask.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.task.RunningTask; +import org.apache.reef.mock.ProcessRequest; + +/** + * send message from driver to task process request. + */ +@Unstable +@Private +public final class SendMessageDriverToTask implements + ProcessRequestInternal<Object, Object> { + + private RunningTask task; + + private final byte[] message; + + public SendMessageDriverToTask(final RunningTask task, final byte[] message) { + this.task = task; + this.message = message; + } + + @Override + public Type getType() { + return Type.SEND_MESSAGE_DRIVER_TO_TASK; + } + + public RunningTask getTask() { + return task; + } + + public byte[] getMessage() { + return message; + } + + @Override + public Object getSuccessEvent() { + throw new UnsupportedOperationException(); + } + + @Override + public Object getFailureEvent() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean doAutoComplete() { + return false; + } + + @Override + public void setAutoComplete(final boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SuspendTask.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SuspendTask.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SuspendTask.java new file mode 100644 index 0000000..74a28f6 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/SuspendTask.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.request; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.task.FailedTask; +import org.apache.reef.driver.task.SuspendedTask; +import org.apache.reef.mock.ProcessRequest; +import org.apache.reef.mock.runtime.MockRunningTask; +import org.apache.reef.mock.runtime.MockSuspendedTask; +import org.apache.reef.util.Optional; + +/** + * suspend task process request. + */ +@Unstable +@Private +public final class SuspendTask implements ProcessRequestInternal<SuspendedTask, FailedTask> { + + private final MockRunningTask task; + + private final Optional<byte[]> message; + + public SuspendTask(final MockRunningTask task, final Optional<byte[]> message) { + this.task = task; + this.message = message; + } + + public MockRunningTask getTask() { + return task; + } + + @Override + public Type getType() { + return Type.SUSPEND_TASK; + } + + public Optional<byte[]> getMessage() { + return message; + } + + @Override + public MockSuspendedTask getSuccessEvent() { + return new MockSuspendedTask(this.task); + } + + @Override + public FailedTask getFailureEvent() { + return new FailedTask( + this.task.getId(), + "mock", + Optional.<String>empty(), + Optional.<Throwable>empty(), + Optional.<byte[]>empty(), + Optional.of(this.task.getActiveContext())); + } + + @Override + public boolean doAutoComplete() { + return false; + } + + @Override + public void setAutoComplete(final boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public ProcessRequest getCompletionProcessRequest() { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/package-info.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/package-info.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/package-info.java new file mode 100644 index 0000000..56bc6f0 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/request/package-info.java @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +/** + * process request implementations. + */ +package org.apache.reef.mock.request; http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockActiveContext.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockActiveContext.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockActiveContext.java new file mode 100644 index 0000000..ebde788 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockActiveContext.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.runtime; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.context.ActiveContext; +import org.apache.reef.driver.evaluator.EvaluatorDescriptor; +import org.apache.reef.driver.task.TaskConfigurationOptions; +import org.apache.reef.evaluator.context.parameters.ContextIdentifier; +import org.apache.reef.mock.request.CloseContext; +import org.apache.reef.mock.request.CreateContext; +import org.apache.reef.mock.request.CreateTask; +import org.apache.reef.mock.request.SendMessageDriverToContext; +import org.apache.reef.tang.Configuration; +import org.apache.reef.util.Optional; + +/** + * mock active context. + */ +@Unstable +@Private +public final class MockActiveContext implements ActiveContext { + + private final MockRuntimeDriver mockRuntimeDriver; + + private final MockAllocatedEvalautor evaluator; + + private final Optional<MockActiveContext> parentContext; + + private final String contextID; + + MockActiveContext( + final MockRuntimeDriver mockRuntimeDriver, + final MockAllocatedEvalautor evalautor, + final Optional<MockActiveContext> parentContext, + final String contextID) { + this.mockRuntimeDriver = mockRuntimeDriver; + this.evaluator = evalautor; + this.parentContext = parentContext; + this.contextID = contextID; + } + + @Override + public int hashCode() { + final String id = this.getEvaluatorId() + ":" + contextID; + return id.hashCode(); + } + + public boolean equals(final Object that) { + if (that instanceof MockActiveContext) { + return this.getEvaluatorId().equals(((MockActiveContext)that).getEvaluatorId()) && + this.contextID.equals(((MockActiveContext)that).contextID); + } + return false; + } + + public MockAllocatedEvalautor getEvaluator() { + return this.evaluator; + } + + public Optional<MockActiveContext> getParentContext() { + return this.parentContext; + } + + @Override + public void close() { + this.mockRuntimeDriver.add(new CloseContext(this)); + } + + @Override + public void submitTask(final Configuration taskConf) { + final String taskID = MockUtils.getValue(taskConf, TaskConfigurationOptions.Identifier.class); + final MockRunningTask task = new MockRunningTask(this.mockRuntimeDriver, taskID, this); + this.mockRuntimeDriver.add(new CreateTask(task, this.mockRuntimeDriver.getTaskReturnValueProvider())); + } + + @Override + public void submitContext(final Configuration contextConfiguration) { + final String childContextID = MockUtils.getValue(contextConfiguration, ContextIdentifier.class); + final MockActiveContext context = new MockActiveContext( + this.mockRuntimeDriver, + this.evaluator, + Optional.of(this), + childContextID); + this.mockRuntimeDriver.add(new CreateContext(context)); + } + + @Override + public void submitContextAndService( + final Configuration contextConfiguration, + final Configuration serviceConfiguration) { + submitContext(contextConfiguration); + } + + @Override + public void sendMessage(final byte[] message) { + this.mockRuntimeDriver.add(new SendMessageDriverToContext(this, message)); + } + + @Override + public String getEvaluatorId() { + return this.evaluator.getId(); + } + + @Override + public Optional<String> getParentId() { + return this.parentContext.isPresent() ? + Optional.of(this.parentContext.get().getId()) : + Optional.<String>empty(); + } + + @Override + public EvaluatorDescriptor getEvaluatorDescriptor() { + return this.evaluator.getEvaluatorDescriptor(); + } + + @Override + public String getId() { + return this.contextID; + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockAllocatedEvalautor.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockAllocatedEvalautor.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockAllocatedEvalautor.java new file mode 100644 index 0000000..b08d557 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockAllocatedEvalautor.java @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.runtime; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.evaluator.AllocatedEvaluator; +import org.apache.reef.driver.evaluator.EvaluatorDescriptor; +import org.apache.reef.driver.evaluator.EvaluatorProcess; +import org.apache.reef.driver.task.TaskConfigurationOptions; +import org.apache.reef.evaluator.context.parameters.ContextIdentifier; +import org.apache.reef.mock.request.CloseEvaluator; +import org.apache.reef.mock.request.CreateContextAndTask; +import org.apache.reef.tang.Configuration; +import org.apache.reef.util.Optional; + +import java.io.File; + +/** + * mock allocated evaluator. + */ +@Unstable +@Private +public final class MockAllocatedEvalautor implements AllocatedEvaluator { + public static final String ROOT_CONTEXT_IDENTIFIER_PREFIX = "ROOT.CONTEXT."; + + private final MockRuntimeDriver mockRuntimeDriver; + + private final String identifier; + + private final EvaluatorDescriptor evaluatorDescriptor; + + private final MockActiveContext rootContext; + + private boolean closed = false; + + MockAllocatedEvalautor( + final MockRuntimeDriver mockRuntimeDriver, + final String identifier, + final EvaluatorDescriptor evaluatorDescriptor) { + this.mockRuntimeDriver = mockRuntimeDriver; + this.identifier = identifier; + this.evaluatorDescriptor = evaluatorDescriptor; + this.rootContext = new MockActiveContext( + mockRuntimeDriver, + this, + Optional.<MockActiveContext>empty(), + ROOT_CONTEXT_IDENTIFIER_PREFIX + identifier); + } + + public MockActiveContext getRootContext() { + return this.rootContext; + } + + @Override + public void addFile(final File file) { + // ignore + } + + @Override + public void addLibrary(final File file) { + // ignore + } + + @Override + public EvaluatorDescriptor getEvaluatorDescriptor() { + return this.evaluatorDescriptor; + } + + @Override + public void setProcess(final EvaluatorProcess process) { + throw new UnsupportedOperationException(); + } + + @Override + public void close() { + if (!this.closed) { + this.mockRuntimeDriver.add(new CloseEvaluator(this)); + } else { + throw new IllegalStateException("evaluator already closed"); + } + } + + @Override + public void submitTask(final Configuration taskConfiguration) { + this.rootContext.submitTask(taskConfiguration); + } + + @Override + public void submitContext(final Configuration contextConfiguration) { + this.rootContext.submitContext(contextConfiguration); + } + + @Override + public void submitContextAndService( + final Configuration contextConfiguration, + final Configuration serviceConfiguration) { + this.rootContext.submitContextAndService(contextConfiguration, serviceConfiguration); + } + + @Override + public void submitContextAndTask( + final Configuration contextConfiguration, + final Configuration taskConfiguration) { + final String contextID = MockUtils.getValue(contextConfiguration, ContextIdentifier.class); + final String taskID = MockUtils.getValue(taskConfiguration, TaskConfigurationOptions.Identifier.class); + final MockActiveContext mockContext = new MockActiveContext( + this.mockRuntimeDriver, + this, + Optional.of(this.rootContext), + contextID); + final MockRunningTask mockTask = new MockRunningTask(this.mockRuntimeDriver, taskID, mockContext); + this.mockRuntimeDriver.add( + new CreateContextAndTask( + mockContext, + mockTask, + this.mockRuntimeDriver.getTaskReturnValueProvider())); + } + + @Override + public void submitContextAndServiceAndTask( + final Configuration contextConfiguration, + final Configuration serviceConfiguration, + final Configuration taskConfiguration) { + submitContextAndTask(contextConfiguration, taskConfiguration); + } + + @Override + public String getId() { + return this.identifier; + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockClock.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockClock.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockClock.java new file mode 100644 index 0000000..cd32b4f --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockClock.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.reef.mock.runtime; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.mock.MockRuntime; +import org.apache.reef.tang.InjectionFuture; +import org.apache.reef.wake.EventHandler; +import org.apache.reef.wake.time.Clock; +import org.apache.reef.wake.time.Time; +import org.apache.reef.wake.time.event.Alarm; +import org.apache.reef.wake.time.runtime.event.ClientAlarm; + +import javax.inject.Inject; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * The MockClock can be used to drive alarms set by the client application. + */ +@Unstable +@Private +public final class MockClock implements Clock { + + private final InjectionFuture<MockRuntime> runtime; + + private final List<Alarm> alarmList = new ArrayList<>(); + + private long currentTime = 0; + + private boolean closed = false; + + @Inject + MockClock(final InjectionFuture<MockRuntime> runtime) { + this.runtime = runtime; + } + + /** + * Advances the clock by the offset amount. + * @param offset amount to advance clock + */ + public void advanceClock(final int offset) { + this.currentTime += offset; + final Iterator<Alarm> iter = this.alarmList.iterator(); + while (iter.hasNext()) { + final Alarm alarm = iter.next(); + if (alarm.getTimestamp() <= this.currentTime) { + alarm.run(); + iter.remove(); + } + } + } + + /** + * @return the current mock clock time + */ + public long getCurrentTime() { + return this.currentTime; + } + + @Override + public Time scheduleAlarm(final int offset, final EventHandler<Alarm> handler) { + final Alarm alarm = new ClientAlarm(this.currentTime + offset, handler); + alarmList.add(alarm); + return alarm; + } + + @Override + public void close() { + if (!closed) { + this.runtime.get().stop(); + this.closed = true; + } + } + + @Override + public void stop() { + close(); + } + + @Override + public void stop(final Throwable exception) { + close(); + } + + @Override + public boolean isIdle() { + return this.alarmList.size() == 0; + } + + @Override + public boolean isClosed() { + return this.closed; + } + + @Override + public void run() { + this.runtime.get().start(); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/72ecec74/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockClosedContext.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockClosedContext.java b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockClosedContext.java new file mode 100644 index 0000000..31d5845 --- /dev/null +++ b/lang/java/reef-runtime-mock/src/main/java/org/apache/reef/mock/runtime/MockClosedContext.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.mock.runtime; + +import org.apache.reef.annotations.Unstable; +import org.apache.reef.annotations.audience.Private; +import org.apache.reef.driver.context.ActiveContext; +import org.apache.reef.driver.context.ClosedContext; +import org.apache.reef.driver.evaluator.EvaluatorDescriptor; +import org.apache.reef.util.Optional; + +/** + * mock closed context. + */ +@Unstable +@Private +public final class MockClosedContext implements ClosedContext { + + private final MockActiveContext mockActiveContext; + + public MockClosedContext(final MockActiveContext activeContext) { + this.mockActiveContext = activeContext; + } + + public MockActiveContext getMockActiveContext() { + return this.mockActiveContext; + } + + @Override + public ActiveContext getParentContext() { + return this.mockActiveContext.getParentContext().isPresent() ? + this.mockActiveContext.getParentContext().get() : null; + } + + @Override + public String getId() { + return this.mockActiveContext.getId(); + } + + @Override + public String getEvaluatorId() { + return this.mockActiveContext.getEvaluatorId(); + } + + @Override + public Optional<String> getParentId() { + return this.mockActiveContext.getParentId(); + } + + @Override + public EvaluatorDescriptor getEvaluatorDescriptor() { + return this.mockActiveContext.getEvaluatorDescriptor(); + } +}
