Search in sources :

Example 1 with Resource

use of io.pravega.controller.store.task.Resource in project pravega by pravega.

the class TaskTest method testTaskSweeper.

@Test
public void testTaskSweeper() throws ExecutionException, InterruptedException {
    final String deadHost = "deadHost";
    final String deadThreadId = UUID.randomUUID().toString();
    final String scope = SCOPE;
    final String stream = "streamSweeper";
    final StreamConfiguration configuration = StreamConfiguration.builder().scope(SCOPE).streamName(stream1).scalingPolicy(policy1).build();
    final Resource resource = new Resource(scope, stream);
    final long timestamp = System.currentTimeMillis();
    final TaskData taskData = new TaskData("createStream", "1.0", new Serializable[] { scope, stream, configuration, timestamp });
    for (int i = 0; i < 5; i++) {
        final TaggedResource taggedResource = new TaggedResource(UUID.randomUUID().toString(), resource);
        taskMetadataStore.putChild(deadHost, taggedResource).join();
    }
    final TaggedResource taggedResource = new TaggedResource(deadThreadId, resource);
    taskMetadataStore.putChild(deadHost, taggedResource).join();
    taskMetadataStore.lock(resource, taskData, deadHost, deadThreadId, null, null).join();
    TaskSweeper taskSweeper = new TaskSweeper(taskMetadataStore, HOSTNAME, executor, streamMetadataTasks);
    taskSweeper.handleFailedProcess(deadHost).get();
    Optional<TaskData> data = taskMetadataStore.getTask(resource, deadHost, deadThreadId).get();
    assertFalse(data.isPresent());
    Optional<TaggedResource> child = taskMetadataStore.getRandomChild(deadHost).get();
    assertFalse(child.isPresent());
    // ensure that the stream streamSweeper is created
    StreamConfiguration config = streamStore.getConfiguration(SCOPE, stream, null, executor).get();
    assertTrue(config.getStreamName().equals(configuration.getStreamName()));
    assertTrue(config.getScope().equals(configuration.getScope()));
    assertTrue(config.getScalingPolicy().equals(configuration.getScalingPolicy()));
}
Also used : StreamConfiguration(io.pravega.client.stream.StreamConfiguration) TaggedResource(io.pravega.controller.store.task.TaggedResource) Resource(io.pravega.controller.store.task.Resource) TaggedResource(io.pravega.controller.store.task.TaggedResource) Test(org.junit.Test)

Example 2 with Resource

use of io.pravega.controller.store.task.Resource in project pravega by pravega.

the class TaskTest method parallelTaskSweeperTest.

@Test
public void parallelTaskSweeperTest() throws InterruptedException, ExecutionException {
    final String deadHost = "deadHost";
    final String deadThreadId1 = UUID.randomUUID().toString();
    final String deadThreadId2 = UUID.randomUUID().toString();
    final String scope = SCOPE;
    final String stream1 = "parallelSweeper1";
    final String stream2 = "parallelSweeper2";
    final StreamConfiguration config1 = StreamConfiguration.builder().scope(SCOPE).streamName(stream1).scalingPolicy(policy1).build();
    final StreamConfiguration config2 = StreamConfiguration.builder().scope(SCOPE).streamName(stream2).scalingPolicy(policy1).build();
    final Resource resource1 = new Resource(scope, stream1);
    final long timestamp1 = System.currentTimeMillis();
    final TaskData taskData1 = new TaskData("createStream", "1.0", new Serializable[] { scope, stream1, config1, timestamp1 });
    final Resource resource2 = new Resource(scope, stream2);
    final long timestamp2 = System.currentTimeMillis();
    final TaskData taskData2 = new TaskData("createStream", "1.0", new Serializable[] { scope, stream2, config2, timestamp2 });
    for (int i = 0; i < 5; i++) {
        final TaggedResource taggedResource = new TaggedResource(UUID.randomUUID().toString(), resource1);
        taskMetadataStore.putChild(deadHost, taggedResource).join();
    }
    final TaggedResource taggedResource1 = new TaggedResource(deadThreadId1, resource1);
    taskMetadataStore.putChild(deadHost, taggedResource1).join();
    final TaggedResource taggedResource2 = new TaggedResource(deadThreadId2, resource2);
    taskMetadataStore.putChild(deadHost, taggedResource2).join();
    taskMetadataStore.lock(resource1, taskData1, deadHost, deadThreadId1, null, null).join();
    taskMetadataStore.lock(resource2, taskData2, deadHost, deadThreadId2, null, null).join();
    final SweeperThread sweeperThread1 = new SweeperThread(HOSTNAME, executor, taskMetadataStore, streamMetadataTasks, deadHost);
    final SweeperThread sweeperThread2 = new SweeperThread(HOSTNAME, executor, taskMetadataStore, streamMetadataTasks, deadHost);
    sweeperThread1.start();
    sweeperThread2.start();
    sweeperThread1.getResult().join();
    sweeperThread2.getResult().join();
    Optional<TaskData> data = taskMetadataStore.getTask(resource1, deadHost, deadThreadId1).get();
    assertFalse(data.isPresent());
    data = taskMetadataStore.getTask(resource2, deadHost, deadThreadId2).get();
    assertFalse(data.isPresent());
    Optional<TaggedResource> child = taskMetadataStore.getRandomChild(deadHost).get();
    assertFalse(child.isPresent());
    // ensure that the stream streamSweeper is created
    StreamConfiguration config = streamStore.getConfiguration(SCOPE, stream1, null, executor).get();
    assertTrue(config.getStreamName().equals(stream1));
    config = streamStore.getConfiguration(SCOPE, stream2, null, executor).get();
    assertTrue(config.getStreamName().equals(stream2));
}
Also used : StreamConfiguration(io.pravega.client.stream.StreamConfiguration) TaggedResource(io.pravega.controller.store.task.TaggedResource) Resource(io.pravega.controller.store.task.Resource) TaggedResource(io.pravega.controller.store.task.TaggedResource) Test(org.junit.Test)

Example 3 with Resource

use of io.pravega.controller.store.task.Resource in project pravega by pravega.

the class TaskBase method execute.

/**
 * Wrapper method that initially obtains lock then executes the passed method, and finally releases lock.
 *
 * @param resource resource to be updated by the task.
 * @param parameters method parameters.
 * @param operation lambda operation that is the actual task.
 * @param <T> type parameter of return value of operation to be executed.
 * @return return value of task execution.
 */
public <T> CompletableFuture<T> execute(final Resource resource, final Serializable[] parameters, final FutureOperation<T> operation) {
    if (!ready) {
        return Futures.failedFuture(new IllegalStateException(getClass().getName() + " not yet ready"));
    }
    final String tag = UUID.randomUUID().toString();
    final TaskData taskData = getTaskData(parameters);
    final CompletableFuture<T> result = new CompletableFuture<>();
    final TaggedResource taggedResource = new TaggedResource(tag, resource);
    log.debug("Host={}, Tag={} starting to execute task {}-{} on resource {}", context.hostId, tag, taskData.getMethodName(), taskData.getMethodVersion(), resource);
    if (createIndexOnlyMode) {
        return createIndexes(taggedResource, taskData);
    }
    // PutChild (HostId, resource)
    // Initially store the fact that I am about the update the resource.
    // Since multiple threads within this process could concurrently attempt to modify same resource,
    // we tag the resource name with a random GUID so as not to interfere with other thread's
    // creation or deletion of resource children under HostId node.
    taskMetadataStore.putChild(context.hostId, taggedResource).thenComposeAsync(x -> executeTask(resource, taskData, tag, operation), executor).whenCompleteAsync((value, e) -> taskMetadataStore.removeChild(context.hostId, taggedResource, true).whenCompleteAsync((innerValue, innerE) -> {
        // ignore the result of removeChile operations, since it is an optimization
        if (e != null) {
            result.completeExceptionally(e);
        } else {
            result.complete(value);
        }
    }, executor), executor);
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) UUID(java.util.UUID) TaggedResource(io.pravega.controller.store.task.TaggedResource) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Slf4j(lombok.extern.slf4j.Slf4j) TaskMetadataStore(io.pravega.controller.store.task.TaskMetadataStore) Annotation(java.lang.annotation.Annotation) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Data(lombok.Data) Resource(io.pravega.controller.store.task.Resource) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Method(java.lang.reflect.Method) Futures(io.pravega.common.concurrent.Futures) CompletableFuture(java.util.concurrent.CompletableFuture) TaggedResource(io.pravega.controller.store.task.TaggedResource)

Aggregations

Resource (io.pravega.controller.store.task.Resource)3 TaggedResource (io.pravega.controller.store.task.TaggedResource)3 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Futures (io.pravega.common.concurrent.Futures)1 TaskMetadataStore (io.pravega.controller.store.task.TaskMetadataStore)1 Serializable (java.io.Serializable)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Data (lombok.Data)1 Slf4j (lombok.extern.slf4j.Slf4j)1