Search in sources :

Example 1 with Unit

use of com.instaclustr.esop.impl.AbstractTracker.Unit in project esop by instaclustr.

the class AbstractTracker method submit.

public synchronized Session<UNIT> submit(final INTERACTOR interactor, final Operation<? extends REQUEST> operation, final Collection<ManifestEntry> entries, final String snapshotTag, final int concurrentConnections) {
    final Session<UNIT> currentSession = constructSession();
    currentSession.setSnapshotTag(snapshotTag);
    currentSession.setId(operation.id);
    if (entries.isEmpty()) {
        logger.info("0 files to process.");
        return currentSession;
    }
    // we have executor service per request in order to specify maximal
    // concurrent uploads, if we had one global executor, we could not "cap it".
    final ListeningExecutorService executorService = new FixedTasksExecutorSupplier().get(concurrentConnections);
    final Map<ListenableFuture<Void>, Unit> futures = new HashMap<>();
    for (final ManifestEntry entry : entries) {
        UNIT alreadySubmitted = null;
        final Iterator<UNIT> it = Collections.unmodifiableList(new ArrayList<>(units)).iterator();
        while (it.hasNext()) {
            UNIT unit = it.next();
            if (unit.getManifestEntry().objectKey.equals(entry.objectKey)) {
                alreadySubmitted = unit;
                break;
            }
        }
        if (alreadySubmitted == null) {
            final UNIT unit = constructUnitToSubmit(interactor, entry, operation.getShouldCancel(), snapshotTag, hashSpec);
            units.add(unit);
            futures.put(executorService.submit(unit), unit);
            submittedUnits.incrementAndGet();
            currentSession.addUnit(unit);
        } else {
            logger.info(String.format("Session %s skips as already submitted: %s", currentSession.getId(), alreadySubmitted.getManifestEntry().objectKey));
            currentSession.addUnit(alreadySubmitted);
        }
    }
    sessions.add(currentSession);
    submittedSessions.incrementAndGet();
    futures.forEach((key, value) -> key.addListener(() -> {
        synchronized (sessions) {
            // increment finished units across all sessions
            sessions.stream().filter(s -> s.getUnits().contains(value)).forEach(s -> {
                operationsService.operation(s.getId()).ifPresent(op -> {
                    s.finishedUnits.incrementAndGet();
                    logger.debug(String.format("Progress of operation %s: %s", op.id, s.getProgress()));
                    op.progress = s.getProgress();
                });
            });
            units.remove(value);
        }
    }, finisherExecutorService));
    currentSession.setExecutorService(executorService);
    return currentSession;
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) HashSpec(com.instaclustr.esop.impl.hash.HashSpec) OperationRequest(com.instaclustr.operations.OperationRequest) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MINUTES(java.util.concurrent.TimeUnit.MINUTES) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FINISHED(com.instaclustr.esop.impl.AbstractTracker.Unit.State.FINISHED) HashSet(java.util.HashSet) Map(java.util.Map) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Session(com.instaclustr.esop.impl.AbstractTracker.Session) FixedTasksExecutorSupplier(com.instaclustr.threading.Executors.FixedTasksExecutorSupplier) OperationsService(com.instaclustr.operations.OperationsService) Objects(com.google.common.base.Objects) Awaitility.await(org.awaitility.Awaitility.await) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Operation(com.instaclustr.operations.Operation) Collection(java.util.Collection) CANCELLED(com.instaclustr.esop.impl.AbstractTracker.Unit.State.CANCELLED) Set(java.util.Set) UUID(java.util.UUID) FAILED(com.instaclustr.esop.impl.AbstractTracker.Unit.State.FAILED) String.format(java.lang.String.format) AtomicLong(java.util.concurrent.atomic.AtomicLong) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) IGNORED(com.instaclustr.esop.impl.AbstractTracker.Unit.State.IGNORED) Optional(java.util.Optional) NOT_STARTED(com.instaclustr.esop.impl.AbstractTracker.Unit.State.NOT_STARTED) Unit(com.instaclustr.esop.impl.AbstractTracker.Unit) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) FixedTasksExecutorSupplier(com.instaclustr.threading.Executors.FixedTasksExecutorSupplier) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Unit(com.instaclustr.esop.impl.AbstractTracker.Unit)

Aggregations

JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 Objects (com.google.common.base.Objects)1 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 Session (com.instaclustr.esop.impl.AbstractTracker.Session)1 Unit (com.instaclustr.esop.impl.AbstractTracker.Unit)1 CANCELLED (com.instaclustr.esop.impl.AbstractTracker.Unit.State.CANCELLED)1 FAILED (com.instaclustr.esop.impl.AbstractTracker.Unit.State.FAILED)1 FINISHED (com.instaclustr.esop.impl.AbstractTracker.Unit.State.FINISHED)1 IGNORED (com.instaclustr.esop.impl.AbstractTracker.Unit.State.IGNORED)1 NOT_STARTED (com.instaclustr.esop.impl.AbstractTracker.Unit.State.NOT_STARTED)1 HashSpec (com.instaclustr.esop.impl.hash.HashSpec)1 Operation (com.instaclustr.operations.Operation)1 OperationRequest (com.instaclustr.operations.OperationRequest)1 OperationsService (com.instaclustr.operations.OperationsService)1 FixedTasksExecutorSupplier (com.instaclustr.threading.Executors.FixedTasksExecutorSupplier)1 String.format (java.lang.String.format)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1