Search in sources :

Example 6 with AzureException

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

the class AzureBlobOutputStream method close.

/**
 * This api waits for all pending upload (stageBlock task) futures to finish.
 * It then synchronously commits the list of blocks to persist the actual blob on storage.
 * Note: this method does not invoke flush and flush has to be explicitly called before close.
 * @throws IllegalStateException when
 *       - when closing an already closed stream
 * @throws RuntimeException when
 *       - byteArrayOutputStream.close fails or
 *       - any of the pending uploads fails or
 *       - blob's commitBlockList fails
 * throws ClassNotFoundException or IllegalAccessException or InstantiationException
 *       - while creating an instance of BlobMetadataGenerator
 */
@Override
public synchronized void close() {
    if (isClosed) {
        LOG.info("{}: already closed", blobAsyncClient.getBlobUrl().toString());
        return;
    }
    LOG.info("{}: Close", blobAsyncClient.getBlobUrl().toString());
    try {
        if (byteArrayOutputStream.isPresent()) {
            byteArrayOutputStream.get().close();
        }
        if (blockList.size() == 0) {
            return;
        }
        CompletableFuture<Void> future = CompletableFuture.allOf(pendingUpload.toArray(new CompletableFuture[0]));
        LOG.info("Closing blob: {} PendingUpload:{} ", blobAsyncClient.getBlobUrl().toString(), pendingUpload.size());
        future.get((long) flushTimeoutMs, TimeUnit.MILLISECONDS);
        LOG.info("For blob: {} committing blockList size:{}", blobAsyncClient.getBlobUrl().toString(), blockList.size());
        metrics.updateAzureCommitMetrics();
        BlobMetadataGenerator blobMetadataGenerator = getBlobMetadataGenerator();
        commitBlob(blockList, blobMetadataGenerator.getBlobMetadata(new BlobMetadataContext(streamName, totalUploadedBlockSize, totalNumberOfRecordsInBlob)));
    } catch (Exception e) {
        String msg = String.format("Close blob %s failed with exception. Total pending sends %d", blobAsyncClient.getBlobUrl().toString(), pendingUpload.size());
        throw new AzureException(msg, e);
    } finally {
        clearAndMarkClosed();
    }
}
Also used : BlobMetadataContext(org.apache.samza.system.azureblob.utils.BlobMetadataContext) CompletableFuture(java.util.concurrent.CompletableFuture) AzureException(org.apache.samza.AzureException) IOException(java.io.IOException) AzureException(org.apache.samza.AzureException) BlobMetadataGenerator(org.apache.samza.system.azureblob.utils.BlobMetadataGenerator)

Example 7 with AzureException

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

the class AzureLock method lock.

/**
 * Tries to acquire a lock in order to create intermediate streams. On failure to acquire lock, it keeps trying until the lock times out.
 * The lock is acquired when the blob is leased successfully.
 * @param timeout Duration after which timeout occurs.
 * @return true if the lock was acquired successfully, false if lock acquire operation is unsuccessful even after subsequent tries within the timeout range.
 */
@Override
public boolean lock(Duration timeout) {
    // Start timer for timeout
    long startTime = System.currentTimeMillis();
    long lockTimeout = timeout.toMillis();
    Random random = new Random();
    while ((System.currentTimeMillis() - startTime) < lockTimeout) {
        try {
            leaseId.getAndSet(leaseBlobManager.acquireLease(LEASE_TIME_IN_SEC, leaseId.get()));
        } catch (AzureException e) {
            return false;
        }
        if (leaseId.get() != null) {
            LOG.info("Acquired lock!");
            hasLock.getAndSet(true);
            return true;
        } else {
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            LOG.info("Trying to acquire lock again...");
        }
    }
    return false;
}
Also used : Random(java.util.Random) AzureException(org.apache.samza.AzureException)

Example 8 with AzureException

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

the class AzureCheckpointManager method writeCheckpoint.

@Override
public void writeCheckpoint(TaskName taskName, Checkpoint checkpoint) {
    Preconditions.checkArgument(checkpoint instanceof CheckpointV1, "Only CheckpointV1 could be written to Azure");
    if (!taskNames.contains(taskName)) {
        throw new SamzaException("writing checkpoint of unregistered task");
    }
    TableBatchOperation batchOperation = new TableBatchOperation();
    Iterator<Map.Entry<SystemStreamPartition, String>> iterator = checkpoint.getOffsets().entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<SystemStreamPartition, String> entry = iterator.next();
        SystemStreamPartition ssp = entry.getKey();
        String offset = entry.getValue();
        String partitionKey = taskName.toString();
        checkValidKey(partitionKey, "Taskname");
        String rowKey = serializeSystemStreamPartition(ssp);
        checkValidKey(rowKey, "SystemStreamPartition");
        // Create table entity
        TaskCheckpointEntity taskCheckpoint = new TaskCheckpointEntity(partitionKey, rowKey, offset);
        // Add to batch operation
        batchOperation.insertOrReplace(taskCheckpoint);
        // Execute when batch reaches capacity or this is the last item
        if (batchOperation.size() >= MAX_WRITE_BATCH_SIZE || !iterator.hasNext()) {
            try {
                cloudTable.execute(batchOperation);
            } catch (StorageException e) {
                LOG.error("Executing batch failed for task: {}", taskName);
                throw new AzureException(e);
            }
            batchOperation.clear();
        }
    }
}
Also used : AzureException(org.apache.samza.AzureException) CheckpointV1(org.apache.samza.checkpoint.CheckpointV1) SamzaException(org.apache.samza.SamzaException) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) StorageException(com.microsoft.azure.storage.StorageException) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition)

Example 9 with AzureException

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

the class AzureCheckpointManager method deleteEntities.

private void deleteEntities(Iterator<TaskCheckpointEntity> entitiesToDelete) {
    TableBatchOperation batchOperation = new TableBatchOperation();
    while (entitiesToDelete.hasNext()) {
        TaskCheckpointEntity entity = entitiesToDelete.next();
        // Add to batch operation
        batchOperation.delete(entity);
        // Execute when batch reaches capacity or when this is the last item
        if (batchOperation.size() >= MAX_WRITE_BATCH_SIZE || !entitiesToDelete.hasNext()) {
            try {
                cloudTable.execute(batchOperation);
            } catch (StorageException e) {
                LOG.error("Executing batch failed for deleting checkpoints");
                throw new AzureException(e);
            }
            batchOperation.clear();
        }
    }
}
Also used : AzureException(org.apache.samza.AzureException) StorageException(com.microsoft.azure.storage.StorageException)

Example 10 with AzureException

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

the class AzureCheckpointManager method start.

@Override
public void start() {
    try {
        // Create the table if it doesn't exist.
        cloudTable = azureClient.getTableClient().getTableReference(jobTableName);
        cloudTable.createIfNotExists();
    } catch (URISyntaxException e) {
        LOG.error("Connection string {} specifies an invalid URI while creating checkpoint table.", storageConnectionString);
        throw new AzureException(e);
    } catch (StorageException e) {
        LOG.error("Azure Storage failed when creating checkpoint table", e);
        throw new AzureException(e);
    }
}
Also used : AzureException(org.apache.samza.AzureException) URISyntaxException(java.net.URISyntaxException) StorageException(com.microsoft.azure.storage.StorageException)

Aggregations

AzureException (org.apache.samza.AzureException)12 StorageException (com.microsoft.azure.storage.StorageException)9 TableOperation (com.microsoft.azure.storage.table.TableOperation)4 IOException (java.io.IOException)4 SamzaException (org.apache.samza.SamzaException)3 ProcessorEntity (org.apache.samza.coordinator.data.ProcessorEntity)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 CheckpointV1 (org.apache.samza.checkpoint.CheckpointV1)1 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)1 BlobMetadataContext (org.apache.samza.system.azureblob.utils.BlobMetadataContext)1 BlobMetadataGenerator (org.apache.samza.system.azureblob.utils.BlobMetadataGenerator)1 Test (org.junit.Test)1