Search in sources :

Example 1 with UploadTask

use of org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask in project flink by splunk.

the class BatchingStateChangeUploadSchedulerTest method testRetry.

/**
 * Test integration with {@link RetryingExecutor}.
 */
@Test
public void testRetry() throws Exception {
    final int maxAttempts = 5;
    try (BatchingStateChangeUploadScheduler store = new BatchingStateChangeUploadScheduler(0, 0, MAX_BYTES_IN_FLIGHT, RetryPolicy.fixed(maxAttempts, 0, 0), new TestingStateChangeUploader() {

        final AtomicInteger currentAttempt = new AtomicInteger(0);

        @Override
        public UploadTasksResult upload(Collection<UploadTask> tasks) throws IOException {
            for (UploadTask uploadTask : tasks) {
                if (currentAttempt.getAndIncrement() < maxAttempts - 1) {
                    throw new IOException();
                } else {
                    uploadTask.complete(emptyList());
                }
            }
            return null;
        }
    }, new DirectScheduledExecutorService(), new RetryingExecutor(new DirectScheduledExecutorService(), createUnregisteredChangelogStorageMetricGroup().getAttemptsPerUpload()), createUnregisteredChangelogStorageMetricGroup())) {
        CompletableFuture<List<UploadResult>> completionFuture = new CompletableFuture<>();
        store.upload(new UploadTask(getChanges(4), completionFuture::complete, (unused, throwable) -> completionFuture.completeExceptionally(throwable)));
        completionFuture.get();
    }
}
Also used : BiConsumerWithException(org.apache.flink.util.function.BiConsumerWithException) Deadline(org.apache.flink.api.common.time.Deadline) Arrays(java.util.Arrays) Tuple2(org.apache.flink.api.java.tuple.Tuple2) ExceptionUtils.rethrow(org.apache.flink.util.ExceptionUtils.rethrow) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) ExceptionUtils.findThrowable(org.apache.flink.util.ExceptionUtils.findThrowable) DirectScheduledExecutorService(org.apache.flink.runtime.testutils.DirectScheduledExecutorService) HashSet(java.util.HashSet) ManuallyTriggeredScheduledExecutorService(org.apache.flink.core.testutils.ManuallyTriggeredScheduledExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) UploadTask(org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask) Collectors.toMap(java.util.stream.Collectors.toMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Assert.fail(org.junit.Assert.fail) UnregisteredChangelogStorageMetricGroup.createUnregisteredChangelogStorageMetricGroup(org.apache.flink.changelog.fs.UnregisteredChangelogStorageMetricGroup.createUnregisteredChangelogStorageMetricGroup) StateChange(org.apache.flink.runtime.state.changelog.StateChange) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) UUID(java.util.UUID) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) Function.identity(java.util.function.Function.identity) SequenceNumber(org.apache.flink.runtime.state.changelog.SequenceNumber) EmptyStreamStateHandle(org.apache.flink.runtime.state.testutils.EmptyStreamStateHandle) Assert.assertEquals(org.junit.Assert.assertEquals) DirectScheduledExecutorService(org.apache.flink.runtime.testutils.DirectScheduledExecutorService) IOException(java.io.IOException) UploadTask(org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Test(org.junit.Test)

Example 2 with UploadTask

use of org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask in project flink by splunk.

the class BatchingStateChangeUploadSchedulerTest method testRetryOnTimeout.

@Test
public void testRetryOnTimeout() throws Exception {
    int numAttempts = 3;
    AtomicReference<List<SequenceNumber>> failed = new AtomicReference<>(emptyList());
    AtomicReference<List<UploadResult>> succeeded = new AtomicReference<>(emptyList());
    UploadTask upload = new UploadTask(getChanges(4), succeeded::set, (sqn, error) -> failed.set(sqn));
    ManuallyTriggeredScheduledExecutorService executorService = new ManuallyTriggeredScheduledExecutorService();
    BlockingUploader uploader = new BlockingUploader();
    try (BatchingStateChangeUploadScheduler store = scheduler(numAttempts, executorService, uploader, 10)) {
        store.upload(upload);
        Deadline deadline = Deadline.fromNow(Duration.ofMinutes(5));
        while (uploader.getUploadsCount() < numAttempts - 1 && deadline.hasTimeLeft()) {
            executorService.triggerScheduledTasks();
            executorService.triggerAll();
            Thread.sleep(10);
        }
        uploader.unblock();
        while (!upload.finished.get() && deadline.hasTimeLeft()) {
            executorService.triggerScheduledTasks();
            executorService.triggerAll();
            Thread.sleep(10);
        }
    }
    assertTrue(upload.finished.get());
    assertEquals(upload.changeSets.stream().map(StateChangeSet::getSequenceNumber).collect(Collectors.toSet()), succeeded.get().stream().map(UploadResult::getSequenceNumber).collect(Collectors.toSet()));
    assertTrue(failed.get().isEmpty());
}
Also used : Deadline(org.apache.flink.api.common.time.Deadline) AtomicReference(java.util.concurrent.atomic.AtomicReference) UploadTask(org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask) ManuallyTriggeredScheduledExecutorService(org.apache.flink.core.testutils.ManuallyTriggeredScheduledExecutorService) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Test(org.junit.Test)

Example 3 with UploadTask

use of org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask in project flink by splunk.

the class TestingStateChangeUploader method upload.

@Override
public UploadTasksResult upload(Collection<UploadTask> tasks) throws IOException {
    for (UploadTask uploadTask : tasks) {
        this.uploaded.addAll(uploadTask.changeSets);
        this.tasks.add(uploadTask);
    }
    return new UploadTasksResult(emptyMap(), new ByteStreamStateHandle("", new byte[0]));
}
Also used : UploadTask(org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle)

Example 4 with UploadTask

use of org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask in project flink by splunk.

the class StateChangeFsUploader method upload.

private UploadTasksResult upload(Path path, Collection<UploadTask> tasks) throws IOException {
    boolean wrappedStreamClosed = false;
    FSDataOutputStream fsStream = fileSystem.create(path, NO_OVERWRITE);
    try {
        fsStream.write(compression ? 1 : 0);
        try (OutputStreamWithPos stream = wrap(fsStream)) {
            final Map<UploadTask, Map<StateChangeSet, Long>> tasksOffsets = new HashMap<>();
            for (UploadTask task : tasks) {
                tasksOffsets.put(task, format.write(stream, task.changeSets));
            }
            FileStateHandle handle = new FileStateHandle(path, stream.getPos());
            // otherwise JM may receive invalid handles
            return new UploadTasksResult(tasksOffsets, handle);
        } finally {
            wrappedStreamClosed = true;
        }
    } finally {
        if (!wrappedStreamClosed) {
            fsStream.close();
        }
    }
}
Also used : UploadTask(org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask) HashMap(java.util.HashMap) FileStateHandle(org.apache.flink.runtime.state.filesystem.FileStateHandle) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with UploadTask

use of org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask in project flink by splunk.

the class StateChangeFsUploader method upload.

public UploadTasksResult upload(Collection<UploadTask> tasks) throws IOException {
    final String fileName = generateFileName();
    LOG.debug("upload {} tasks to {}", tasks.size(), fileName);
    Path path = new Path(basePath, fileName);
    try {
        return uploadWithMetrics(path, tasks);
    } catch (IOException e) {
        metrics.getUploadFailuresCounter().inc();
        try (Closer closer = Closer.create()) {
            closer.register(() -> {
                throw e;
            });
            tasks.forEach(cs -> closer.register(() -> cs.fail(e)));
            closer.register(() -> fileSystem.delete(path, true));
        }
    }
    // closer above throws an exception
    return null;
}
Also used : Path(org.apache.flink.core.fs.Path) Closer(org.apache.flink.shaded.guava30.com.google.common.io.Closer) OutputStream(java.io.OutputStream) UncompressedStreamCompressionDecorator(org.apache.flink.runtime.state.UncompressedStreamCompressionDecorator) SystemClock(org.apache.flink.util.clock.SystemClock) Logger(org.slf4j.Logger) FileStateHandle(org.apache.flink.runtime.state.filesystem.FileStateHandle) Closer(org.apache.flink.shaded.guava30.com.google.common.io.Closer) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) HashMap(java.util.HashMap) UUID(java.util.UUID) Clock(org.apache.flink.util.clock.Clock) BufferedOutputStream(java.io.BufferedOutputStream) UploadTask(org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) FileSystem(org.apache.flink.core.fs.FileSystem) Path(org.apache.flink.core.fs.Path) Map(java.util.Map) SnappyStreamCompressionDecorator(org.apache.flink.runtime.state.SnappyStreamCompressionDecorator) NO_OVERWRITE(org.apache.flink.core.fs.FileSystem.WriteMode.NO_OVERWRITE) StreamCompressionDecorator(org.apache.flink.runtime.state.StreamCompressionDecorator) IOException(java.io.IOException)

Aggregations

UploadTask (org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask)7 ArrayList (java.util.ArrayList)3 Collections.emptyList (java.util.Collections.emptyList)3 Collections.singletonList (java.util.Collections.singletonList)3 List (java.util.List)3 Map (java.util.Map)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Deadline (org.apache.flink.api.common.time.Deadline)3 ManuallyTriggeredScheduledExecutorService (org.apache.flink.core.testutils.ManuallyTriggeredScheduledExecutorService)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 FSDataOutputStream (org.apache.flink.core.fs.FSDataOutputStream)2 SequenceNumber (org.apache.flink.runtime.state.changelog.SequenceNumber)2 FileStateHandle (org.apache.flink.runtime.state.filesystem.FileStateHandle)2 BufferedOutputStream (java.io.BufferedOutputStream)1 OutputStream (java.io.OutputStream)1