Search in sources :

Example 1 with NANOSECONDS

use of java.util.concurrent.TimeUnit.NANOSECONDS in project samza by apache.

the class EmbeddedTaggedRateLimiter method acquire.

@Override
public Map<String, Integer> acquire(Map<String, Integer> tagToCreditsMap, long timeout, TimeUnit unit) {
    ensureTagsAreValid(tagToCreditsMap);
    long timeoutInNanos = NANOSECONDS.convert(timeout, unit);
    Stopwatch stopwatch = Stopwatch.createStarted();
    return tagToCreditsMap.entrySet().stream().map(e -> {
        String tag = e.getKey();
        int requiredCredits = e.getValue();
        long remainingTimeoutInNanos = Math.max(0L, timeoutInNanos - stopwatch.elapsed(NANOSECONDS));
        com.google.common.util.concurrent.RateLimiter rateLimiter = tagToRateLimiterMap.get(tag);
        int availableCredits = rateLimiter.tryAcquire(requiredCredits, remainingTimeoutInNanos, NANOSECONDS) ? requiredCredits : 0;
        return new ImmutablePair<>(tag, availableCredits);
    }).collect(Collectors.toMap(ImmutablePair::getKey, ImmutablePair::getValue));
}
Also used : TaskName(org.apache.samza.container.TaskName) Logger(org.slf4j.Logger) Stopwatch(com.google.common.base.Stopwatch) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Context(org.apache.samza.context.Context) TimeUnit(java.util.concurrent.TimeUnit) Map(java.util.Map) Preconditions(com.google.common.base.Preconditions) TaskContextImpl(org.apache.samza.context.TaskContextImpl) Collections(java.util.Collections) JobModel(org.apache.samza.job.model.JobModel) Stopwatch(com.google.common.base.Stopwatch)

Example 2 with NANOSECONDS

use of java.util.concurrent.TimeUnit.NANOSECONDS in project cassandra by apache.

the class StorageProxy method describeSchemaVersions.

/**
 * initiate a request/response session with each live node to check whether or not everybody is using the same
 * migration id. This is useful for determining if a schema change has propagated through the cluster. Disagreement
 * is assumed if any node fails to respond.
 */
public static Map<String, List<String>> describeSchemaVersions(boolean withPort) {
    final String myVersion = Schema.instance.getVersion().toString();
    final Map<InetAddressAndPort, UUID> versions = new ConcurrentHashMap<>();
    final Set<InetAddressAndPort> liveHosts = Gossiper.instance.getLiveMembers();
    final CountDownLatch latch = newCountDownLatch(liveHosts.size());
    RequestCallback<UUID> cb = message -> {
        // record the response from the remote node.
        versions.put(message.from(), message.payload);
        latch.decrement();
    };
    // an empty message acts as a request to the SchemaVersionVerbHandler.
    Message message = out(SCHEMA_VERSION_REQ, noPayload);
    for (InetAddressAndPort endpoint : liveHosts) MessagingService.instance().sendWithCallback(message, endpoint, cb);
    try {
        // wait for as long as possible. timeout-1s if possible.
        latch.await(DatabaseDescriptor.getRpcTimeout(NANOSECONDS), NANOSECONDS);
    } catch (InterruptedException e) {
        throw new UncheckedInterruptedException(e);
    }
    // maps versions to hosts that are on that version.
    Map<String, List<String>> results = new HashMap<String, List<String>>();
    Iterable<InetAddressAndPort> allHosts = concat(Gossiper.instance.getLiveMembers(), Gossiper.instance.getUnreachableMembers());
    for (InetAddressAndPort host : allHosts) {
        UUID version = versions.get(host);
        String stringVersion = version == null ? UNREACHABLE : version.toString();
        List<String> hosts = results.get(stringVersion);
        if (hosts == null) {
            hosts = new ArrayList<String>();
            results.put(stringVersion, hosts);
        }
        hosts.add(host.getHostAddress(withPort));
    }
    // we're done: the results map is ready to return to the client.  the rest is just debug logging:
    if (results.get(UNREACHABLE) != null)
        logger.debug("Hosts not in agreement. Didn't get a response from everybody: {}", join(results.get(UNREACHABLE), ","));
    for (Map.Entry<String, List<String>> entry : results.entrySet()) {
        // check for version disagreement. log the hosts that don't agree.
        if (entry.getKey().equals(UNREACHABLE) || entry.getKey().equals(myVersion))
            continue;
        for (String host : entry.getValue()) logger.debug("{} disagrees ({})", host, entry.getKey());
    }
    if (results.size() == 1)
        logger.debug("Schemas are in agreement.");
    return results;
}
Also used : Arrays(java.util.Arrays) StorageMetrics(org.apache.cassandra.metrics.StorageMetrics) Stage(org.apache.cassandra.concurrent.Stage) EndpointsForToken(org.apache.cassandra.locator.EndpointsForToken) ClientRequestsMetricsHolder.readMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.readMetrics) ReadResponse(org.apache.cassandra.db.ReadResponse) Global.nanoTime(org.apache.cassandra.utils.Clock.Global.nanoTime) ReadRepairMetrics(org.apache.cassandra.metrics.ReadRepairMetrics) UnfilteredPartitionIterator(org.apache.cassandra.db.partitions.UnfilteredPartitionIterator) Future(java.util.concurrent.Future) OverloadedException(org.apache.cassandra.exceptions.OverloadedException) Replicas(org.apache.cassandra.locator.Replicas) Mutation(org.apache.cassandra.db.Mutation) Pair(org.apache.cassandra.utils.Pair) Map(java.util.Map) HintsService(org.apache.cassandra.hints.HintsService) SinglePartitionReadCommand(org.apache.cassandra.db.SinglePartitionReadCommand) PartitionIterators(org.apache.cassandra.db.partitions.PartitionIterators) ReplicaPlans(org.apache.cassandra.locator.ReplicaPlans) ReplicaPlan(org.apache.cassandra.locator.ReplicaPlan) Keyspace(org.apache.cassandra.db.Keyspace) ReadAbortException(org.apache.cassandra.exceptions.ReadAbortException) PartitionRangeReadCommand(org.apache.cassandra.db.PartitionRangeReadCommand) FBUtilities(org.apache.cassandra.utils.FBUtilities) RejectException(org.apache.cassandra.db.RejectException) CasWriteTimeoutException(org.apache.cassandra.exceptions.CasWriteTimeoutException) Set(java.util.Set) WriteType(org.apache.cassandra.db.WriteType) Verb(org.apache.cassandra.net.Verb) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) UUIDGen(org.apache.cassandra.utils.UUIDGen) CassandraRelevantProperties(org.apache.cassandra.config.CassandraRelevantProperties) WriteFailureException(org.apache.cassandra.exceptions.WriteFailureException) ReplicaLayout(org.apache.cassandra.locator.ReplicaLayout) NoPayload.noPayload(org.apache.cassandra.net.NoPayload.noPayload) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Iterables(com.google.common.collect.Iterables) TableId(org.apache.cassandra.schema.TableId) PrepareVerbHandler.doPrepare(org.apache.cassandra.service.paxos.PrepareVerbHandler.doPrepare) Global.currentTimeMillis(org.apache.cassandra.utils.Clock.Global.currentTimeMillis) ReadCommand(org.apache.cassandra.db.ReadCommand) Message(org.apache.cassandra.net.Message) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) TruncateRequest(org.apache.cassandra.db.TruncateRequest) Schema(org.apache.cassandra.schema.Schema) IEndpointSnitch(org.apache.cassandra.locator.IEndpointSnitch) Token(org.apache.cassandra.dht.Token) CountDownLatch.newCountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch.newCountDownLatch) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BatchlogManager(org.apache.cassandra.batchlog.BatchlogManager) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) RequestTimeoutException(org.apache.cassandra.exceptions.RequestTimeoutException) TriggerExecutor(org.apache.cassandra.triggers.TriggerExecutor) ConsistencyLevel(org.apache.cassandra.db.ConsistencyLevel) MessagingService(org.apache.cassandra.net.MessagingService) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) ReadTimeoutException(org.apache.cassandra.exceptions.ReadTimeoutException) MessageParams(org.apache.cassandra.db.MessageParams) Hint(org.apache.cassandra.hints.Hint) FilteredPartition(org.apache.cassandra.db.partitions.FilteredPartition) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) AtomicLong(java.util.concurrent.atomic.AtomicLong) ViewUtils(org.apache.cassandra.db.view.ViewUtils) CasWriteUnknownResultException(org.apache.cassandra.exceptions.CasWriteUnknownResultException) Preconditions(com.google.common.base.Preconditions) ClientRequestsMetricsHolder.casReadMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.casReadMetrics) ClientRequestsMetricsHolder.casWriteMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.casWriteMetrics) CounterMutation(org.apache.cassandra.db.CounterMutation) BatchlogCleanup(org.apache.cassandra.service.BatchlogResponseHandler.BatchlogCleanup) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) CountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch) Message.out(org.apache.cassandra.net.Message.out) ByteBuffer(java.nio.ByteBuffer) Gossiper(org.apache.cassandra.gms.Gossiper) CASClientRequestMetrics(org.apache.cassandra.metrics.CASClientRequestMetrics) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ClientRequestsMetricsHolder.readMetricsForLevel(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.readMetricsForLevel) ReadRepair(org.apache.cassandra.service.reads.repair.ReadRepair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractReadExecutor(org.apache.cassandra.service.reads.AbstractReadExecutor) Iterables.concat(com.google.common.collect.Iterables.concat) RowIterator(org.apache.cassandra.db.rows.RowIterator) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) Global.randomBallot(org.apache.cassandra.service.paxos.BallotGenerator.Global.randomBallot) UnavailableException(org.apache.cassandra.exceptions.UnavailableException) MessageFlag(org.apache.cassandra.net.MessageFlag) Collection(java.util.Collection) ClientRequestsMetricsHolder.writeMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.writeMetrics) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) WriteTimeoutException(org.apache.cassandra.exceptions.WriteTimeoutException) UUID(java.util.UUID) NoSpamLogger(org.apache.cassandra.utils.NoSpamLogger) RequestCallback(org.apache.cassandra.net.RequestCallback) Collectors(java.util.stream.Collectors) SERIAL(org.apache.cassandra.db.ConsistencyLevel.SERIAL) Objects(java.util.Objects) CacheLoader(com.google.common.cache.CacheLoader) UncheckedInterruptedException(org.apache.cassandra.utils.concurrent.UncheckedInterruptedException) List(java.util.List) ProposeVerbHandler.doPropose(org.apache.cassandra.service.paxos.ProposeVerbHandler.doPropose) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ForwardingInfo(org.apache.cassandra.net.ForwardingInfo) TableMetadata(org.apache.cassandra.schema.TableMetadata) Optional(java.util.Optional) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) ClientRequestsMetricsHolder.writeMetricsForLevel(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.writeMetricsForLevel) SchemaConstants(org.apache.cassandra.schema.SchemaConstants) RangeCommands(org.apache.cassandra.service.reads.range.RangeCommands) org.apache.cassandra.service.paxos(org.apache.cassandra.service.paxos) ReadExecutionController(org.apache.cassandra.db.ReadExecutionController) PartitionDenylist(org.apache.cassandra.schema.PartitionDenylist) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Config(org.apache.cassandra.config.Config) HashMap(java.util.HashMap) TombstoneOverwhelmingException(org.apache.cassandra.db.filter.TombstoneOverwhelmingException) RequestFailureException(org.apache.cassandra.exceptions.RequestFailureException) HashSet(java.util.HashSet) ClientRequestsMetricsHolder.viewWriteMetrics(org.apache.cassandra.metrics.ClientRequestsMetricsHolder.viewWriteMetrics) StringUtils.join(org.apache.commons.lang3.StringUtils.join) IsBootstrappingException(org.apache.cassandra.exceptions.IsBootstrappingException) Clock(org.apache.cassandra.utils.Clock) Global.nextBallotTimestampMicros(org.apache.cassandra.service.paxos.BallotGenerator.Global.nextBallotTimestampMicros) DenylistMetrics(org.apache.cassandra.metrics.DenylistMetrics) Logger(org.slf4j.Logger) ReadFailureException(org.apache.cassandra.exceptions.ReadFailureException) Tracing(org.apache.cassandra.tracing.Tracing) MonotonicClock(org.apache.cassandra.utils.MonotonicClock) Ints(com.google.common.primitives.Ints) Batch(org.apache.cassandra.batchlog.Batch) Replica(org.apache.cassandra.locator.Replica) TimeUnit(java.util.concurrent.TimeUnit) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) ReadCallback(org.apache.cassandra.service.reads.ReadCallback) MBeanWrapper(org.apache.cassandra.utils.MBeanWrapper) IMutation(org.apache.cassandra.db.IMutation) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Message(org.apache.cassandra.net.Message) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CountDownLatch.newCountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch.newCountDownLatch) CountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch) UncheckedInterruptedException(org.apache.cassandra.utils.concurrent.UncheckedInterruptedException) UncheckedInterruptedException(org.apache.cassandra.utils.concurrent.UncheckedInterruptedException) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 3 with NANOSECONDS

use of java.util.concurrent.TimeUnit.NANOSECONDS in project mongo-java-driver by mongodb.

the class DefaultConnectionPool method getAsync.

@Override
public void getAsync(final SingleResultCallback<InternalConnection> callback) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("Asynchronously getting a connection from the pool for server %s", serverId));
    }
    connectionPoolListener.connectionCheckOutStarted(new ConnectionCheckOutStartedEvent(serverId));
    Timeout timeout = Timeout.startNow(settings.getMaxWaitTime(NANOSECONDS));
    SingleResultCallback<InternalConnection> eventSendingCallback = (result, failure) -> {
        SingleResultCallback<InternalConnection> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
        if (failure == null) {
            connectionPoolListener.connectionCheckedOut(new ConnectionCheckedOutEvent(getId(result)));
            errHandlingCallback.onResult(result, null);
        } else {
            errHandlingCallback.onResult(null, checkOutFailed(failure));
        }
    };
    try {
        stateAndGeneration.throwIfClosedOrPaused();
    } catch (RuntimeException e) {
        eventSendingCallback.onResult(null, e);
        return;
    }
    asyncWorkManager.enqueue(new Task(timeout, t -> {
        if (t != null) {
            eventSendingCallback.onResult(null, t);
        } else {
            PooledConnection connection;
            try {
                connection = getPooledConnection(timeout);
            } catch (RuntimeException e) {
                eventSendingCallback.onResult(null, e);
                return;
            }
            if (connection.opened()) {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace(format("Pooled connection %s to server %s is already open", getId(connection), serverId));
                }
                eventSendingCallback.onResult(connection, null);
            } else {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace(format("Pooled connection %s to server %s is not yet open", getId(connection), serverId));
                }
                openConcurrencyLimiter.openAsyncWithConcurrencyLimit(connection, timeout, eventSendingCallback);
            }
        }
    }));
}
Also used : SdamIssue(com.mongodb.internal.connection.SdamServerDescriptionManager.SdamIssue) ServerDescription(com.mongodb.connection.ServerDescription) ConcurrentPool.lockUnfair(com.mongodb.internal.connection.ConcurrentPool.lockUnfair) ConnectionPoolCreatedEvent(com.mongodb.event.ConnectionPoolCreatedEvent) ConnectionDescription(com.mongodb.connection.ConnectionDescription) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentPool.sizeToString(com.mongodb.internal.connection.ConcurrentPool.sizeToString) MongoServerUnavailableException(com.mongodb.MongoServerUnavailableException) ConnectionCheckedOutEvent(com.mongodb.event.ConnectionCheckedOutEvent) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Prune(com.mongodb.internal.connection.ConcurrentPool.Prune) MongoException(com.mongodb.MongoException) Predicate(java.util.function.Predicate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BlockingQueue(java.util.concurrent.BlockingQueue) Assertions.assertFalse(com.mongodb.assertions.Assertions.assertFalse) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) Decoder(org.bson.codecs.Decoder) PRIVATE(com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE) List(java.util.List) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) NonNull(com.mongodb.lang.NonNull) Assertions.fail(com.mongodb.assertions.Assertions.fail) Optional(java.util.Optional) ThreadSafe(com.mongodb.annotations.ThreadSafe) Reason(com.mongodb.event.ConnectionCheckOutFailedEvent.Reason) Queue(java.util.Queue) ConnectionClosedEvent(com.mongodb.event.ConnectionClosedEvent) NotThreadSafe(com.mongodb.annotations.NotThreadSafe) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) LongAdder(java.util.concurrent.atomic.LongAdder) ConnectionCheckedInEvent(com.mongodb.event.ConnectionCheckedInEvent) Assertions.assertNotNull(com.mongodb.assertions.Assertions.assertNotNull) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) ErrorHandlingResultCallback.errorHandlingCallback(com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Deque(java.util.Deque) ConcurrentPool.lockInterruptibly(com.mongodb.internal.connection.ConcurrentPool.lockInterruptibly) Supplier(java.util.function.Supplier) INFINITE_SIZE(com.mongodb.internal.connection.ConcurrentPool.INFINITE_SIZE) Assertions.isTrue(com.mongodb.assertions.Assertions.isTrue) MongoConnectionPoolClearedException(com.mongodb.MongoConnectionPoolClearedException) VisibleForTesting(com.mongodb.internal.VisibleForTesting) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SessionContext(com.mongodb.internal.session.SessionContext) ConnectionCreatedEvent(com.mongodb.event.ConnectionCreatedEvent) LinkedList(java.util.LinkedList) Logger(com.mongodb.diagnostics.logging.Logger) ExecutorService(java.util.concurrent.ExecutorService) ConnectionReadyEvent(com.mongodb.event.ConnectionReadyEvent) ByteBuf(org.bson.ByteBuf) MongoInterruptedException(com.mongodb.MongoInterruptedException) ConnectionPoolListener(com.mongodb.event.ConnectionPoolListener) ReentrantLock(java.util.concurrent.locks.ReentrantLock) EventListenerHelper.getConnectionPoolListener(com.mongodb.internal.event.EventListenerHelper.getConnectionPoolListener) ServerId(com.mongodb.connection.ServerId) RequestContext(com.mongodb.RequestContext) Timeout(com.mongodb.internal.Timeout) MongoTimeoutException(com.mongodb.MongoTimeoutException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ConnectionPoolClosedEvent(com.mongodb.event.ConnectionPoolClosedEvent) OptionalProvider(com.mongodb.internal.inject.OptionalProvider) ConnectionPoolReadyEvent(com.mongodb.event.ConnectionPoolReadyEvent) Condition(java.util.concurrent.locks.Condition) Lock(java.util.concurrent.locks.Lock) Assertions.assertNull(com.mongodb.assertions.Assertions.assertNull) ConnectionCheckOutStartedEvent(com.mongodb.event.ConnectionCheckOutStartedEvent) Assertions.assertTrue(com.mongodb.assertions.Assertions.assertTrue) Loggers(com.mongodb.diagnostics.logging.Loggers) DaemonThreadFactory(com.mongodb.internal.thread.DaemonThreadFactory) ObjectId(org.bson.types.ObjectId) Nullable(com.mongodb.lang.Nullable) ConnectionId(com.mongodb.connection.ConnectionId) ConnectionCheckOutFailedEvent(com.mongodb.event.ConnectionCheckOutFailedEvent) StampedLock(java.util.concurrent.locks.StampedLock) ConnectionCheckedOutEvent(com.mongodb.event.ConnectionCheckedOutEvent) Timeout(com.mongodb.internal.Timeout) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) ConnectionCheckOutStartedEvent(com.mongodb.event.ConnectionCheckOutStartedEvent)

Example 4 with NANOSECONDS

use of java.util.concurrent.TimeUnit.NANOSECONDS in project aries by apache.

the class AbstractPushStreamImpl method window.

@Override
public <R> PushStream<R> window(Supplier<Duration> time, IntSupplier maxEvents, Executor ex, BiFunction<Long, Collection<T>, R> f) {
    AtomicLong timestamp = new AtomicLong();
    AtomicLong counter = new AtomicLong();
    Object lock = new Object();
    AtomicReference<Queue<T>> queueRef = new AtomicReference<Queue<T>>(null);
    // This code is declared as a separate block to avoid any confusion
    // about which instance's methods and variables are in scope
    Consumer<AbstractPushStreamImpl<R>> begin = p -> {
        synchronized (lock) {
            timestamp.lazySet(System.nanoTime());
            long count = counter.get();
            scheduler.schedule(getWindowTask(p, f, time, maxEvents, lock, count, queueRef, timestamp, counter, ex), time.get().toNanos(), NANOSECONDS);
        }
        queueRef.set(getQueueForInternalBuffering(maxEvents.getAsInt()));
    };
    @SuppressWarnings("resource") AbstractPushStreamImpl<R> eventStream = new IntermediatePushStreamImpl<R>(psp, ex, scheduler, this) {

        @Override
        protected void beginning() {
            begin.accept(this);
        }
    };
    AtomicBoolean endPending = new AtomicBoolean(false);
    updateNext((event) -> {
        try {
            if (eventStream.closed.get() == CLOSED) {
                return ABORT;
            }
            Queue<T> queue;
            if (!event.isTerminal()) {
                long elapsed;
                long newCount;
                synchronized (lock) {
                    for (; ; ) {
                        queue = queueRef.get();
                        if (queue == null) {
                            if (endPending.get()) {
                                return ABORT;
                            } else {
                                continue;
                            }
                        } else if (queue.offer(event.getData())) {
                            return CONTINUE;
                        } else {
                            queueRef.lazySet(null);
                            break;
                        }
                    }
                    long now = System.nanoTime();
                    elapsed = now - timestamp.get();
                    timestamp.lazySet(now);
                    newCount = counter.get() + 1;
                    counter.lazySet(newCount);
                    // This is a non-blocking call, and must happen in the
                    // synchronized block to avoid re=ordering the executor
                    // enqueue with a subsequent incoming close operation
                    aggregateAndForward(f, eventStream, event, queue, ex, elapsed);
                }
                // These must happen outside the synchronized block as we
                // call out to user code
                queueRef.set(getQueueForInternalBuffering(maxEvents.getAsInt()));
                scheduler.schedule(getWindowTask(eventStream, f, time, maxEvents, lock, newCount, queueRef, timestamp, counter, ex), time.get().toNanos(), NANOSECONDS);
                return CONTINUE;
            } else {
                long elapsed;
                synchronized (lock) {
                    queue = queueRef.get();
                    queueRef.lazySet(null);
                    endPending.set(true);
                    long now = System.nanoTime();
                    elapsed = now - timestamp.get();
                    counter.lazySet(counter.get() + 1);
                }
                Collection<T> collected = queue == null ? emptyList() : queue;
                ex.execute(() -> {
                    try {
                        eventStream.handleEvent(PushEvent.data(f.apply(Long.valueOf(NANOSECONDS.toMillis(elapsed)), collected)));
                    } catch (Exception e) {
                        close(PushEvent.error(e));
                    }
                });
            }
            ex.execute(() -> eventStream.handleEvent(event.nodata()));
            return ABORT;
        } catch (Exception e) {
            close(PushEvent.error(e));
            return ABORT;
        }
    });
    return eventStream;
}
Also used : Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) AbstractQueue(java.util.AbstractQueue) Deferred(org.osgi.util.promise.Deferred) EventType(org.osgi.util.pushstream.PushEvent.EventType) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Collector(java.util.stream.Collector) PushStreamBuilder(org.osgi.util.pushstream.PushStreamBuilder) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Promise(org.osgi.util.promise.Promise) PushStreamProvider(org.osgi.util.pushstream.PushStreamProvider) List(java.util.List) Optional(java.util.Optional) Queue(java.util.Queue) ConcurrentModificationException(java.util.ConcurrentModificationException) PushEvent(org.osgi.util.pushstream.PushEvent) LongAdder(java.util.concurrent.atomic.LongAdder) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PushStream(org.osgi.util.pushstream.PushStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) PushEventSource(org.osgi.util.pushstream.PushEventSource) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) IntSupplier(java.util.function.IntSupplier) IntFunction(java.util.function.IntFunction) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Executor(java.util.concurrent.Executor) Semaphore(java.util.concurrent.Semaphore) State(org.apache.aries.pushstream.AbstractPushStreamImpl.State) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) PushEventConsumer(org.osgi.util.pushstream.PushEventConsumer) Comparator(java.util.Comparator) Collections(java.util.Collections) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConcurrentModificationException(java.util.ConcurrentModificationException) NoSuchElementException(java.util.NoSuchElementException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractQueue(java.util.AbstractQueue) BlockingQueue(java.util.concurrent.BlockingQueue) Queue(java.util.Queue)

Example 5 with NANOSECONDS

use of java.util.concurrent.TimeUnit.NANOSECONDS in project vert.x by eclipse.

the class HAManager method simulateKill.

public void simulateKill() {
    if (!stopped) {
        killed = true;
        CountDownLatch latch = new CountDownLatch(1);
        Promise<Void> promise = Promise.promise();
        clusterManager.leave(promise);
        promise.future().onFailure(t -> log.error("Failed to leave cluster", t)).onComplete(ar -> latch.countDown());
        long timerID = checkQuorumTimerID;
        if (timerID >= 0L) {
            checkQuorumTimerID = -1L;
            vertx.cancelTimer(timerID);
        }
        vertx.cancelTimer(quorumTimerID);
        boolean interrupted = false;
        try {
            long remainingNanos = MINUTES.toNanos(1);
            long end = System.nanoTime() + remainingNanos;
            while (true) {
                try {
                    latch.await(remainingNanos, NANOSECONDS);
                    break;
                } catch (InterruptedException e) {
                    interrupted = true;
                    remainingNanos = end - System.nanoTime();
                }
            }
        } finally {
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
        stopped = true;
    }
}
Also used : Logger(io.vertx.core.impl.logging.Logger) java.util(java.util) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) NodeListener(io.vertx.core.spi.cluster.NodeListener) MINUTES(java.util.concurrent.TimeUnit.MINUTES) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) JsonObject(io.vertx.core.json.JsonObject) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

NANOSECONDS (java.util.concurrent.TimeUnit.NANOSECONDS)5 TimeUnit (java.util.concurrent.TimeUnit)4 List (java.util.List)3 Optional (java.util.Optional)3 Set (java.util.Set)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Consumer (java.util.function.Consumer)3 Supplier (java.util.function.Supplier)3 Collectors (java.util.stream.Collectors)3 Preconditions (com.google.common.base.Preconditions)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Queue (java.util.Queue)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 Future (java.util.concurrent.Future)2