Search in sources :

Example 1 with PERMANENT_BLOB

use of org.apache.flink.runtime.blob.BlobKey.BlobType.PERMANENT_BLOB in project flink by apache.

the class BlobServerPutTest method testConcurrentPutOperations.

/**
 * [FLINK-6020] Tests that concurrent put operations will only upload the file once to the
 * {@link BlobStore} and that the files are not corrupt at any time.
 *
 * @param jobId job ID to use (or <tt>null</tt> if job-unrelated)
 * @param blobType whether the BLOB should become permanent or transient
 */
private void testConcurrentPutOperations(@Nullable final JobID jobId, final BlobKey.BlobType blobType) throws IOException, InterruptedException, ExecutionException {
    final Configuration config = new Configuration();
    final int concurrentPutOperations = 2;
    final int dataSize = 1024;
    Collection<BlobKey> persistedBlobs = ConcurrentHashMap.newKeySet();
    TestingBlobStore blobStore = new TestingBlobStoreBuilder().setPutFunction((file, jobID, blobKey) -> {
        persistedBlobs.add(blobKey);
        return true;
    }).createTestingBlobStore();
    final CountDownLatch countDownLatch = new CountDownLatch(concurrentPutOperations);
    final byte[] data = new byte[dataSize];
    ArrayList<CompletableFuture<BlobKey>> allFutures = new ArrayList<>(concurrentPutOperations);
    ExecutorService executor = Executors.newFixedThreadPool(concurrentPutOperations);
    try (final BlobServer server = new BlobServer(config, temporaryFolder.newFolder(), blobStore)) {
        server.start();
        for (int i = 0; i < concurrentPutOperations; i++) {
            CompletableFuture<BlobKey> putFuture = CompletableFuture.supplyAsync(() -> {
                try {
                    BlockingInputStream inputStream = new BlockingInputStream(countDownLatch, data);
                    BlobKey uploadedKey = put(server, jobId, inputStream, blobType);
                    // check the uploaded file's contents (concurrently)
                    verifyContents(server, jobId, uploadedKey, data);
                    return uploadedKey;
                } catch (IOException e) {
                    throw new CompletionException(new FlinkException("Could not upload blob.", e));
                }
            }, executor);
            allFutures.add(putFuture);
        }
        FutureUtils.ConjunctFuture<Collection<BlobKey>> conjunctFuture = FutureUtils.combineAll(allFutures);
        // wait until all operations have completed and check that no exception was thrown
        Collection<BlobKey> blobKeys = conjunctFuture.get();
        Iterator<BlobKey> blobKeyIterator = blobKeys.iterator();
        assertTrue(blobKeyIterator.hasNext());
        BlobKey blobKey = blobKeyIterator.next();
        // make sure that all blob keys are the same
        while (blobKeyIterator.hasNext()) {
            verifyKeyDifferentHashEquals(blobKey, blobKeyIterator.next());
        }
        // check the uploaded file's contents
        verifyContents(server, jobId, blobKey, data);
        // check that we only uploaded the file once to the blob store
        if (blobType == PERMANENT_BLOB) {
            assertThat(persistedBlobs).hasSameElementsAs(blobKeys);
        } else {
            // can't really verify much in the other cases other than that the put operations
            // should
            // work and not corrupt files
            assertThat(persistedBlobs).isEmpty();
        }
    } finally {
        executor.shutdownNow();
    }
}
Also used : Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Path(org.apache.flink.core.fs.Path) TestLogger(org.apache.flink.util.TestLogger) Assert.fail(org.junit.Assert.fail) TRANSIENT_BLOB(org.apache.flink.runtime.blob.BlobKey.BlobType.TRANSIENT_BLOB) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) Preconditions(org.apache.flink.util.Preconditions) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assume.assumeTrue(org.junit.Assume.assumeTrue) AccessDeniedException(java.nio.file.AccessDeniedException) FlinkException(org.apache.flink.util.FlinkException) BlobClientTest.validateGetAndClose(org.apache.flink.runtime.blob.BlobClientTest.validateGetAndClose) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) BlobServerGetTest.get(org.apache.flink.runtime.blob.BlobServerGetTest.get) CheckedThread(org.apache.flink.core.testutils.CheckedThread) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) ExpectedException(org.junit.rules.ExpectedException) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) Configuration(org.apache.flink.configuration.Configuration) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BlobKeyTest.verifyKeyDifferentHashEquals(org.apache.flink.runtime.blob.BlobKeyTest.verifyKeyDifferentHashEquals) OperatingSystem(org.apache.flink.util.OperatingSystem) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) JobID(org.apache.flink.api.common.JobID) Rule(org.junit.Rule) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) PERMANENT_BLOB(org.apache.flink.runtime.blob.BlobKey.BlobType.PERMANENT_BLOB) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Configuration(org.apache.flink.configuration.Configuration) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) FlinkException(org.apache.flink.util.FlinkException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection)

Example 2 with PERMANENT_BLOB

use of org.apache.flink.runtime.blob.BlobKey.BlobType.PERMANENT_BLOB in project flink by apache.

the class BlobServerPutTest method testFailedBlobStorePutsDeletesLocalBlob.

@Test
public void testFailedBlobStorePutsDeletesLocalBlob() throws IOException {
    final BlobKey.BlobType blobType = PERMANENT_BLOB;
    final JobID jobId = JobID.generate();
    final byte[] data = new byte[] { 1, 2, 3 };
    final File storageDir = temporaryFolder.newFolder();
    final TestingBlobStore blobStore = new TestingBlobStoreBuilder().setPutFunction((file, jobID, blobKey) -> {
        throw new IOException("Could not persist the file.");
    }).createTestingBlobStore();
    try (final BlobServer blobServer = new BlobServer(new Configuration(), storageDir, blobStore)) {
        try {
            put(blobServer, jobId, data, blobType);
            fail("Expected that the put operation fails with an IOException.");
        } catch (IOException expected) {
        // expected :-)
        }
        final File jobSpecificStorageDirectory = new File(BlobUtils.getStorageLocationPath(storageDir.getAbsolutePath(), jobId));
        assertThat(jobSpecificStorageDirectory).isEmptyDirectory();
    }
}
Also used : Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Path(org.apache.flink.core.fs.Path) TestLogger(org.apache.flink.util.TestLogger) Assert.fail(org.junit.Assert.fail) TRANSIENT_BLOB(org.apache.flink.runtime.blob.BlobKey.BlobType.TRANSIENT_BLOB) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) Preconditions(org.apache.flink.util.Preconditions) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assume.assumeTrue(org.junit.Assume.assumeTrue) AccessDeniedException(java.nio.file.AccessDeniedException) FlinkException(org.apache.flink.util.FlinkException) BlobClientTest.validateGetAndClose(org.apache.flink.runtime.blob.BlobClientTest.validateGetAndClose) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) BlobServerGetTest.get(org.apache.flink.runtime.blob.BlobServerGetTest.get) CheckedThread(org.apache.flink.core.testutils.CheckedThread) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) ExpectedException(org.junit.rules.ExpectedException) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) Configuration(org.apache.flink.configuration.Configuration) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BlobKeyTest.verifyKeyDifferentHashEquals(org.apache.flink.runtime.blob.BlobKeyTest.verifyKeyDifferentHashEquals) OperatingSystem(org.apache.flink.util.OperatingSystem) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) JobID(org.apache.flink.api.common.JobID) Rule(org.junit.Rule) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) PERMANENT_BLOB(org.apache.flink.runtime.blob.BlobKey.BlobType.PERMANENT_BLOB) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Configuration(org.apache.flink.configuration.Configuration) IOException(java.io.IOException) File(java.io.File) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 3 with PERMANENT_BLOB

use of org.apache.flink.runtime.blob.BlobKey.BlobType.PERMANENT_BLOB in project flink by apache.

the class BlobServerGetTest method testGetReDownloadsCorruptedPermanentBlobFromBlobStoreInCaseOfRestart.

@Test
public void testGetReDownloadsCorruptedPermanentBlobFromBlobStoreInCaseOfRestart() throws IOException {
    final JobID jobId = JobID.generate();
    final byte[] data = new byte[] { 1, 2, 3 };
    final byte[] corruptedData = new byte[] { 3, 2, 1 };
    final File storageDir = temporaryFolder.newFolder();
    final OneShotLatch getCalled = new OneShotLatch();
    final BlobStore blobStore = new TestingBlobStoreBuilder().setGetFunction((jobID, blobKey, file) -> {
        getCalled.trigger();
        FileUtils.writeByteArrayToFile(file, data);
        return true;
    }).createTestingBlobStore();
    try (final BlobServer blobServer = new BlobServer(new Configuration(), Reference.borrowed(storageDir), blobStore)) {
        final BlobKey blobKey = put(blobServer, jobId, data, PERMANENT_BLOB);
        blobServer.close();
        final File blob = blobServer.getStorageLocation(jobId, blobKey);
        // corrupt the file
        FileUtils.writeByteArrayToFile(blob, corruptedData);
        try (final BlobServer restartedBlobServer = new BlobServer(new Configuration(), Reference.borrowed(storageDir), blobStore)) {
            // we should re-download the file from the BlobStore
            final File file = get(restartedBlobServer, jobId, blobKey);
            validateGetAndClose(new FileInputStream(file), data);
            assertThat(getCalled.isTriggered()).isTrue();
        }
    }
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) FlinkException(org.apache.flink.util.FlinkException) NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BlobClientTest.validateGetAndClose(org.apache.flink.runtime.blob.BlobClientTest.validateGetAndClose) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) FlinkAssertions(org.apache.flink.core.testutils.FlinkAssertions) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TestLogger(org.apache.flink.util.TestLogger) Assert.fail(org.junit.Assert.fail) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) TRANSIENT_BLOB(org.apache.flink.runtime.blob.BlobKey.BlobType.TRANSIENT_BLOB) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) BlobServerPutTest.put(org.apache.flink.runtime.blob.BlobServerPutTest.put) Configuration(org.apache.flink.configuration.Configuration) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Reference(org.apache.flink.util.Reference) FileInputStream(java.io.FileInputStream) BlobKeyTest.verifyKeyDifferentHashEquals(org.apache.flink.runtime.blob.BlobKeyTest.verifyKeyDifferentHashEquals) OperatingSystem(org.apache.flink.util.OperatingSystem) File(java.io.File) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) JobID(org.apache.flink.api.common.JobID) Rule(org.junit.Rule) JOB_DIR_PREFIX(org.apache.flink.runtime.blob.BlobUtils.JOB_DIR_PREFIX) Assume.assumeTrue(org.junit.Assume.assumeTrue) AccessDeniedException(java.nio.file.AccessDeniedException) HighAvailabilityOptions(org.apache.flink.configuration.HighAvailabilityOptions) TemporaryFolder(org.junit.rules.TemporaryFolder) PERMANENT_BLOB(org.apache.flink.runtime.blob.BlobKey.BlobType.PERMANENT_BLOB) Assert.assertEquals(org.junit.Assert.assertEquals) Configuration(org.apache.flink.configuration.Configuration) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) File(java.io.File) JobID(org.apache.flink.api.common.JobID) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 4 with PERMANENT_BLOB

use of org.apache.flink.runtime.blob.BlobKey.BlobType.PERMANENT_BLOB in project flink by apache.

the class BlobServerGetTest method testConcurrentGetOperations.

/**
 * [FLINK-6020] Tests that concurrent get operations don't concurrently access the BlobStore to
 * download a blob.
 *
 * @param jobId job ID to use (or <tt>null</tt> if job-unrelated)
 * @param blobType whether the BLOB should become permanent or transient
 */
private void testConcurrentGetOperations(@Nullable final JobID jobId, final BlobKey.BlobType blobType) throws IOException, InterruptedException, ExecutionException {
    final byte[] data = { 1, 2, 3, 4, 99, 42 };
    final BlobStore blobStore = new TestingBlobStoreBuilder().setGetFunction((jobID, blobKey, file) -> {
        FileUtils.writeByteArrayToFile(file, data);
        return true;
    }).createTestingBlobStore();
    final int numberConcurrentGetOperations = 3;
    final List<CompletableFuture<File>> getOperations = new ArrayList<>(numberConcurrentGetOperations);
    final ExecutorService executor = Executors.newFixedThreadPool(numberConcurrentGetOperations);
    try (final BlobServer server = new BlobServer(new Configuration(), temporaryFolder.newFolder(), blobStore)) {
        server.start();
        // upload data first
        final BlobKey blobKey = put(server, jobId, data, blobType);
        // store!)
        if (blobType == PERMANENT_BLOB) {
            // remove local copy so that a transfer from HA store takes place
            assertTrue(server.getStorageLocation(jobId, blobKey).delete());
        }
        for (int i = 0; i < numberConcurrentGetOperations; i++) {
            CompletableFuture<File> getOperation = CompletableFuture.supplyAsync(() -> {
                try {
                    File file = get(server, jobId, blobKey);
                    // check that we have read the right data
                    validateGetAndClose(new FileInputStream(file), data);
                    return file;
                } catch (IOException e) {
                    throw new CompletionException(new FlinkException("Could not read blob for key " + blobKey + '.', e));
                }
            }, executor);
            getOperations.add(getOperation);
        }
        CompletableFuture<Collection<File>> filesFuture = FutureUtils.combineAll(getOperations);
        filesFuture.get();
    } finally {
        executor.shutdownNow();
    }
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) FlinkException(org.apache.flink.util.FlinkException) NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BlobClientTest.validateGetAndClose(org.apache.flink.runtime.blob.BlobClientTest.validateGetAndClose) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) FlinkAssertions(org.apache.flink.core.testutils.FlinkAssertions) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TestLogger(org.apache.flink.util.TestLogger) Assert.fail(org.junit.Assert.fail) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) TRANSIENT_BLOB(org.apache.flink.runtime.blob.BlobKey.BlobType.TRANSIENT_BLOB) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) BlobServerPutTest.put(org.apache.flink.runtime.blob.BlobServerPutTest.put) Configuration(org.apache.flink.configuration.Configuration) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Reference(org.apache.flink.util.Reference) FileInputStream(java.io.FileInputStream) BlobKeyTest.verifyKeyDifferentHashEquals(org.apache.flink.runtime.blob.BlobKeyTest.verifyKeyDifferentHashEquals) OperatingSystem(org.apache.flink.util.OperatingSystem) File(java.io.File) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) JobID(org.apache.flink.api.common.JobID) Rule(org.junit.Rule) JOB_DIR_PREFIX(org.apache.flink.runtime.blob.BlobUtils.JOB_DIR_PREFIX) Assume.assumeTrue(org.junit.Assume.assumeTrue) AccessDeniedException(java.nio.file.AccessDeniedException) HighAvailabilityOptions(org.apache.flink.configuration.HighAvailabilityOptions) TemporaryFolder(org.junit.rules.TemporaryFolder) PERMANENT_BLOB(org.apache.flink.runtime.blob.BlobKey.BlobType.PERMANENT_BLOB) Assert.assertEquals(org.junit.Assert.assertEquals) Configuration(org.apache.flink.configuration.Configuration) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FlinkException(org.apache.flink.util.FlinkException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection) File(java.io.File)

Aggregations

File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 IOException (java.io.IOException)4 AccessDeniedException (java.nio.file.AccessDeniedException)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collection (java.util.Collection)4 List (java.util.List)4 Random (java.util.Random)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 CompletionException (java.util.concurrent.CompletionException)4 ExecutionException (java.util.concurrent.ExecutionException)4 ExecutorService (java.util.concurrent.ExecutorService)4 Executors (java.util.concurrent.Executors)4 Nullable (javax.annotation.Nullable)4 FileUtils (org.apache.commons.io.FileUtils)4 JobID (org.apache.flink.api.common.JobID)4 Configuration (org.apache.flink.configuration.Configuration)4 BlobClientTest.validateGetAndClose (org.apache.flink.runtime.blob.BlobClientTest.validateGetAndClose)4 PERMANENT_BLOB (org.apache.flink.runtime.blob.BlobKey.BlobType.PERMANENT_BLOB)4