Search in sources :

Example 1 with ProcessorEntity

use of org.apache.samza.coordinator.data.ProcessorEntity in project samza by apache.

the class TableUtils method updateHeartbeat.

/**
 * Updates the liveness value of a particular processor with a randomly generated integer, which in turn updates the last modified since timestamp of the row.
 * @param jmVersion Job model version of the processor row to be updated.
 * @param pid Unique processor ID of the processor row to be updated.
 */
public void updateHeartbeat(String jmVersion, String pid) {
    try {
        TableOperation retrieveEntity = TableOperation.retrieve(jmVersion, pid, ProcessorEntity.class);
        ProcessorEntity entity = table.execute(retrieveEntity).getResultAsType();
        entity.updateLiveness();
        TableOperation update = TableOperation.replace(entity);
        table.execute(update);
    } catch (StorageException e) {
        LOG.error("Azure storage exception while updating heartbeat for job model version: " + jmVersion + "and pid: " + pid, e);
    }
}
Also used : TableOperation(com.microsoft.azure.storage.table.TableOperation) ProcessorEntity(org.apache.samza.coordinator.data.ProcessorEntity) StorageException(com.microsoft.azure.storage.StorageException)

Example 2 with ProcessorEntity

use of org.apache.samza.coordinator.data.ProcessorEntity in project samza by apache.

the class TableUtils method addProcessorEntity.

/**
 * Add a row which denotes an active processor to the processor table.
 * @param jmVersion Job model version that the processor is operating on.
 * @param pid Unique processor ID.
 * @param isLeader Denotes whether the processor is a leader or not.
 * @throws AzureException If an Azure storage service error occurred.
 */
public void addProcessorEntity(String jmVersion, String pid, boolean isLeader) {
    ProcessorEntity entity = new ProcessorEntity(jmVersion, pid);
    entity.setIsLeader(isLeader);
    entity.updateLiveness();
    TableOperation add = TableOperation.insert(entity);
    try {
        table.execute(add);
    } catch (StorageException e) {
        LOG.error("Azure storage exception while adding processor entity with job model version: " + jmVersion + "and pid: " + pid, e);
        throw new AzureException(e);
    }
}
Also used : AzureException(org.apache.samza.AzureException) TableOperation(com.microsoft.azure.storage.table.TableOperation) ProcessorEntity(org.apache.samza.coordinator.data.ProcessorEntity) StorageException(com.microsoft.azure.storage.StorageException)

Example 3 with ProcessorEntity

use of org.apache.samza.coordinator.data.ProcessorEntity in project samza by apache.

the class TableUtils method updateIsLeader.

/**
 * Updates the isLeader value when the processor starts or stops being a leader.
 * @param jmVersion Job model version of the processor row to be updated.
 * @param pid Unique processor ID of the processor row to be updated.
 * @param isLeader Denotes whether the processor is a leader or not.
 * @throws AzureException If an Azure storage service error occurred.
 */
public void updateIsLeader(String jmVersion, String pid, boolean isLeader) {
    try {
        TableOperation retrieveEntity = TableOperation.retrieve(jmVersion, pid, ProcessorEntity.class);
        ProcessorEntity entity = table.execute(retrieveEntity).getResultAsType();
        entity.setIsLeader(isLeader);
        TableOperation update = TableOperation.replace(entity);
        table.execute(update);
    } catch (StorageException e) {
        LOG.error("Azure storage exception while updating isLeader value for job model version: " + jmVersion + "and pid: " + pid, e);
        throw new AzureException(e);
    }
}
Also used : AzureException(org.apache.samza.AzureException) TableOperation(com.microsoft.azure.storage.table.TableOperation) ProcessorEntity(org.apache.samza.coordinator.data.ProcessorEntity) StorageException(com.microsoft.azure.storage.StorageException)

Example 4 with ProcessorEntity

use of org.apache.samza.coordinator.data.ProcessorEntity in project samza by apache.

the class LeaderBarrierCompleteScheduler method scheduleTask.

@Override
public ScheduledFuture scheduleTask() {
    return scheduler.scheduleWithFixedDelay(() -> {
        try {
            if (!table.getEntity(currentJMVersion.get(), processorId).getIsLeader()) {
                LOG.info("Not the leader anymore. Shutting down LeaderBarrierCompleteScheduler.");
                barrierTimeout.getAndSet(true);
                listener.onStateChange();
            } else {
                LOG.info("Leader checking for barrier state");
                // Get processor IDs listed in the table that have the new job model verion.
                Iterable<ProcessorEntity> tableList = table.getEntitiesWithPartition(nextJMVersion);
                Set<String> tableProcessors = new HashSet<>();
                for (ProcessorEntity entity : tableList) {
                    tableProcessors.add(entity.getRowKey());
                }
                LOG.info("List of live processors as seen on the blob = {}", blobProcessorSet);
                LOG.info("List of live processors as seen in the table = {}", tableProcessors);
                if ((System.currentTimeMillis() - startTime) > (BARRIER_TIMEOUT_SEC * 1000)) {
                    barrierTimeout.getAndSet(true);
                    listener.onStateChange();
                } else if (blobProcessorSet.equals(tableProcessors)) {
                    listener.onStateChange();
                }
            }
        } catch (Exception e) {
            errorHandler.accept("Exception in LeaderBarrierCompleteScheduler. Stopping the processor...");
        }
    }, BARRIER_REACHED_DELAY_SEC, BARRIER_REACHED_DELAY_SEC, TimeUnit.SECONDS);
}
Also used : ProcessorEntity(org.apache.samza.coordinator.data.ProcessorEntity) HashSet(java.util.HashSet)

Example 5 with ProcessorEntity

use of org.apache.samza.coordinator.data.ProcessorEntity in project samza by apache.

the class AzureJobCoordinator method onNewJobModelConfirmed.

/**
 * Called when the JC detects that the barrier has completed by checking the barrier state on the blob.
 * @param nextJMVersion The new job model version after rebalancing.
 */
private void onNewJobModelConfirmed(final String nextJMVersion) {
    LOG.info("pid=" + processorId + "new version " + nextJMVersion + " of the job model got confirmed");
    String prevVersion = currentJMVersion.get();
    // Start heart-beating to new entry only when barrier reached.
    // Changing the current job model version enables that since we are heartbeating to a row identified by the current job model version.
    currentJMVersion.getAndSet(nextJMVersion);
    // Delete previous value
    ProcessorEntity entity = table.getEntity(prevVersion, processorId);
    if (entity != null) {
        entity.setEtag("*");
        table.deleteProcessorEntity(entity);
    }
    entity = table.getEntity(INITIAL_STATE, processorId);
    if (entity != null) {
        entity.setEtag("*");
        table.deleteProcessorEntity(entity);
    }
    // Start the container with the new model
    if (coordinatorListener != null) {
        coordinatorListener.onNewJobModel(processorId, jobModel);
    }
}
Also used : ProcessorEntity(org.apache.samza.coordinator.data.ProcessorEntity)

Aggregations

ProcessorEntity (org.apache.samza.coordinator.data.ProcessorEntity)8 StorageException (com.microsoft.azure.storage.StorageException)4 TableOperation (com.microsoft.azure.storage.table.TableOperation)4 AzureException (org.apache.samza.AzureException)3 HashSet (java.util.HashSet)2