Search in sources :

Example 76 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class TestBlobStoreUtil method testPutDirFailsIfAnySubDirFileUploadFails.

@Test
public void testPutDirFailsIfAnySubDirFileUploadFails() throws IOException, TimeoutException, InterruptedException {
    BlobStoreManager blobStoreManager = mock(BlobStoreManager.class);
    // File, dir and recursive dir added, retained and removed in local
    String local = "[a/1, b/2]";
    String remote = "[]";
    // Set up environment
    Path localSnapshotDir = BlobStoreTestUtil.createLocalDir(local);
    String basePath = localSnapshotDir.toAbsolutePath().toString();
    DirIndex remoteSnapshotDir = BlobStoreTestUtil.createDirIndex(remote);
    SnapshotMetadata snapshotMetadata = new SnapshotMetadata(checkpointId, jobName, jobId, taskName, storeName);
    DirDiff dirDiff = DirDiffUtil.getDirDiff(localSnapshotDir.toFile(), remoteSnapshotDir, (localFile, remoteFile) -> localFile.getName().equals(remoteFile.getFileName()));
    // Set up mocks
    SamzaException exception = new SamzaException("Error uploading file");
    CompletableFuture<String> failedFuture = new CompletableFuture<>();
    failedFuture.completeExceptionally(exception);
    when(blobStoreManager.put(any(InputStream.class), any(Metadata.class))).thenAnswer((Answer<CompletableFuture<String>>) invocation -> {
        Metadata metadata = invocation.getArgumentAt(1, Metadata.class);
        String path = metadata.getPayloadPath();
        if (path.endsWith("1")) {
            return CompletableFuture.completedFuture("a1BlobId");
        } else {
            return failedFuture;
        }
    });
    // Execute
    BlobStoreUtil blobStoreUtil = new BlobStoreUtil(blobStoreManager, EXECUTOR, null, null);
    CompletionStage<DirIndex> dirIndexFuture = blobStoreUtil.putDir(dirDiff, snapshotMetadata);
    try {
        // should be already complete. if not, future composition in putDir is broken.
        dirIndexFuture.toCompletableFuture().get(0, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        // Assert that the result future fails and that the cause is propagated correctly
        assertEquals(exception, cause);
        return;
    }
    fail("DirIndex future should have been completed with an exception");
}
Also used : Path(java.nio.file.Path) SortedSet(java.util.SortedSet) FileMetadata(org.apache.samza.storage.blobstore.index.FileMetadata) FileTime(java.nio.file.attribute.FileTime) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) RetriableException(org.apache.samza.storage.blobstore.exceptions.RetriableException) FileUtil(org.apache.samza.util.FileUtil) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Path(java.nio.file.Path) FutureUtil(org.apache.samza.util.FutureUtil) ImmutableSet(com.google.common.collect.ImmutableSet) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Checkpoint(org.apache.samza.checkpoint.Checkpoint) DirDiff(org.apache.samza.storage.blobstore.diff.DirDiff) CheckpointId(org.apache.samza.checkpoint.CheckpointId) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) SnapshotIndex(org.apache.samza.storage.blobstore.index.SnapshotIndex) Optional(java.util.Optional) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) SnapshotMetadata(org.apache.samza.storage.blobstore.index.SnapshotMetadata) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) DirIndex(org.apache.samza.storage.blobstore.index.DirIndex) FileBlob(org.apache.samza.storage.blobstore.index.FileBlob) Matchers(org.mockito.Matchers) CheckpointV2(org.apache.samza.checkpoint.CheckpointV2) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) BlobStoreManager(org.apache.samza.storage.blobstore.BlobStoreManager) BlobStoreStateBackendFactory(org.apache.samza.storage.blobstore.BlobStoreStateBackendFactory) ExecutorService(java.util.concurrent.ExecutorService) OutputStream(java.io.OutputStream) FileIndex(org.apache.samza.storage.blobstore.index.FileIndex) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Metadata(org.apache.samza.storage.blobstore.Metadata) File(java.io.File) SamzaException(org.apache.samza.SamzaException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) NullOutputStream(org.apache.commons.io.output.NullOutputStream) CRC32(java.util.zip.CRC32) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) DeletedException(org.apache.samza.storage.blobstore.exceptions.DeletedException) InputStream(java.io.InputStream) FileMetadata(org.apache.samza.storage.blobstore.index.FileMetadata) SnapshotMetadata(org.apache.samza.storage.blobstore.index.SnapshotMetadata) Metadata(org.apache.samza.storage.blobstore.Metadata) DirDiff(org.apache.samza.storage.blobstore.diff.DirDiff) BlobStoreManager(org.apache.samza.storage.blobstore.BlobStoreManager) SamzaException(org.apache.samza.SamzaException) CompletableFuture(java.util.concurrent.CompletableFuture) SnapshotMetadata(org.apache.samza.storage.blobstore.index.SnapshotMetadata) DirIndex(org.apache.samza.storage.blobstore.index.DirIndex) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 77 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class TestSSPMetadataCache method testGetMetadataExceptionAfterSuccessfulFetch.

/**
 * Given that the admin throws an exception when trying to get the metadata after a successful fetch, getMetadata
 * should propagate the exception.
 */
@Test(expected = SamzaException.class)
public void testGetMetadataExceptionAfterSuccessfulFetch() {
    SystemStreamPartition ssp = buildSSP(0);
    SSPMetadataCache cache = buildSSPMetadataCache(ImmutableSet.of(ssp));
    // do a successful fetch first
    when(clock.currentTimeMillis()).thenReturn(10L);
    when(systemAdmin.getSSPMetadata(ImmutableSet.of(ssp))).thenReturn(ImmutableMap.of(ssp, sspMetadata(1)));
    cache.getMetadata(ssp);
    // throw an exception on the next fetch
    when(clock.currentTimeMillis()).thenReturn(11 + CACHE_TTL.toMillis());
    when(systemAdmin.getSSPMetadata(ImmutableSet.of(ssp))).thenThrow(new SamzaException());
    cache.getMetadata(ssp);
}
Also used : SamzaException(org.apache.samza.SamzaException) Test(org.junit.Test)

Example 78 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class TestFutureUtil method testUnwrapExceptionReturnsNullIfNoNonWrapperCause.

@Test
public void testUnwrapExceptionReturnsNullIfNoNonWrapperCause() {
    Throwable t = new SamzaException(new SamzaException());
    Throwable unwrappedThrowable = FutureUtil.unwrapExceptions(SamzaException.class, t);
    assertNull(unwrappedThrowable);
}
Also used : SamzaException(org.apache.samza.SamzaException) Test(org.junit.Test)

Example 79 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class TestFutureUtil method testUnwrapExceptionUnwrapsMultipleExceptions.

@Test
public void testUnwrapExceptionUnwrapsMultipleExceptions() {
    IllegalArgumentException cause = new IllegalArgumentException();
    Throwable t = new SamzaException(new SamzaException(cause));
    Throwable unwrappedThrowable = FutureUtil.unwrapExceptions(SamzaException.class, t);
    assertEquals(cause, unwrappedThrowable);
}
Also used : SamzaException(org.apache.samza.SamzaException) Test(org.junit.Test)

Example 80 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class TestTaskFactoryUtil method testFinalizeTaskFactory.

@Test
public void testFinalizeTaskFactory() throws NoSuchFieldException, IllegalAccessException {
    TaskFactory mockFactory = mock(TaskFactory.class);
    try {
        TaskFactoryUtil.finalizeTaskFactory(mockFactory, null);
        fail("Should have failed with validation");
    } catch (SamzaException se) {
    // expected
    }
    StreamTaskFactory mockStreamFactory = mock(StreamTaskFactory.class);
    ExecutorService mockThreadPool = mock(ExecutorService.class);
    TaskFactory retFactory = TaskFactoryUtil.finalizeTaskFactory(mockStreamFactory, mockThreadPool);
    assertTrue(retFactory instanceof AsyncStreamTaskFactory);
    assertTrue(((AsyncStreamTaskFactory) retFactory).createInstance() instanceof AsyncStreamTaskAdapter);
    AsyncStreamTaskAdapter taskAdapter = (AsyncStreamTaskAdapter) ((AsyncStreamTaskFactory) retFactory).createInstance();
    Field executorSrvFld = AsyncStreamTaskAdapter.class.getDeclaredField("executor");
    executorSrvFld.setAccessible(true);
    ExecutorService executor = (ExecutorService) executorSrvFld.get(taskAdapter);
    assertEquals(executor, mockThreadPool);
    AsyncStreamTaskFactory mockAsyncStreamFactory = mock(AsyncStreamTaskFactory.class);
    retFactory = TaskFactoryUtil.finalizeTaskFactory(mockAsyncStreamFactory, null);
    assertEquals(retFactory, mockAsyncStreamFactory);
}
Also used : Field(java.lang.reflect.Field) ExecutorService(java.util.concurrent.ExecutorService) SamzaException(org.apache.samza.SamzaException) Test(org.junit.Test)

Aggregations

SamzaException (org.apache.samza.SamzaException)256 IOException (java.io.IOException)61 HashMap (java.util.HashMap)57 Test (org.junit.Test)40 Map (java.util.Map)38 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)34 ArrayList (java.util.ArrayList)30 List (java.util.List)27 File (java.io.File)26 JobConfig (org.apache.samza.config.JobConfig)26 Config (org.apache.samza.config.Config)25 VisibleForTesting (com.google.common.annotations.VisibleForTesting)24 SystemStream (org.apache.samza.system.SystemStream)24 CompletableFuture (java.util.concurrent.CompletableFuture)23 Logger (org.slf4j.Logger)21 LoggerFactory (org.slf4j.LoggerFactory)21 Set (java.util.Set)20 Collections (java.util.Collections)19 MapConfig (org.apache.samza.config.MapConfig)18 TaskName (org.apache.samza.container.TaskName)18