Example usage for java.util.function Supplier get

List of usage examples for java.util.function Supplier get

Introduction

In this page you can find the example usage for java.util.function Supplier get.

Prototype

T get();

Source Link

Document

Gets a result.

Usage

From source file:org.eclipse.hono.client.impl.HonoClientImpl.java

Future<MessageSender> getOrCreateSender(final String key,
        final Supplier<Future<MessageSender>> newSenderSupplier) {

    final Future<MessageSender> result = Future.future();

    context.runOnContext(get -> {// w w  w .j a v a  2 s  . c om
        final MessageSender sender = activeSenders.get(key);
        if (sender != null && sender.isOpen()) {
            LOG.debug("reusing existing message sender [target: {}, credit: {}]", key, sender.getCredit());
            result.complete(sender);
        } else if (!creationLocks.computeIfAbsent(key, k -> Boolean.FALSE)) {
            // register a handler to be notified if the underlying connection to the server fails
            // so that we can fail the result handler passed in
            final Handler<Void> connectionFailureHandler = connectionLost -> {
                // remove lock so that next attempt to open a sender doesn't fail
                creationLocks.remove(key);
                result.tryFail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE,
                        "no connection to service"));
            };
            creationRequests.add(connectionFailureHandler);
            creationLocks.put(key, Boolean.TRUE);
            LOG.debug("creating new message sender for {}", key);

            newSenderSupplier.get().setHandler(creationAttempt -> {
                creationLocks.remove(key);
                creationRequests.remove(connectionFailureHandler);
                if (creationAttempt.succeeded()) {
                    MessageSender newSender = creationAttempt.result();
                    LOG.debug("successfully created new message sender for {}", key);
                    activeSenders.put(key, newSender);
                    result.tryComplete(newSender);
                } else {
                    LOG.debug("failed to create new message sender for {}", key, creationAttempt.cause());
                    activeSenders.remove(key);
                    result.tryFail(creationAttempt.cause());
                }
            });

        } else {
            LOG.debug("already trying to create a message sender for {}", key);
            result.fail(
                    new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "no connection to service"));
        }
    });
    return result;
}

From source file:com.ikanow.aleph2.shared.crud.elasticsearch.services.TestElasticsearchCrudService.java

@Test
public void test_CreateSingleObject() throws InterruptedException, ExecutionException {

    final ElasticsearchCrudService<TestBean> service = getTestService("testCreateSingleObject", TestBean.class);

    assertEquals(0, service.countObjects().get().intValue());

    final TestBean test = new TestBean();
    test._id = "_id_1";
    test.test_string = "test_string_1";

    // 1) Add a new object to an empty DB
    {// www.  j  av  a 2  s  . c  o  m
        final Future<Supplier<Object>> result = service.storeObject(test);

        final Supplier<Object> val = result.get();

        assertEquals("_id_1", val.get());

        assertEquals(1, service.countObjects().get().intValue());

        final CompletableFuture<Optional<TestBean>> f_retval = service.getObjectById(val.get());

        assertTrue("Need to find an object", f_retval.get().isPresent());

        final TestBean retval = f_retval.get().get();

        assertEquals(1, service.countObjects().get().intValue());

        assertEquals("{\"_id\":\"_id_1\",\"test_string\":\"test_string_1\"}",
                BeanTemplateUtils.toJson(retval).toString());
    }
    // 2) Add the _id again, should fail

    final TestBean test2 = BeanTemplateUtils.clone(test).with("test_string", "test_string_2").done();

    {
        final Future<Supplier<Object>> result2 = service.storeObject(test2);

        Exception expected_ex = null;
        try {
            result2.get();
            fail("Should have thrown exception on duplicate insert");
        } catch (Exception e) {
            expected_ex = e;
        }
        if (null != expected_ex)
            assertThat(expected_ex.getCause(), instanceOf(RuntimeException.class));

        assertEquals(1, service.countObjects().get().intValue());

        final CompletableFuture<Optional<TestBean>> f_retval2 = service.getObjectById("_id_1");

        assertTrue("Found an object", f_retval2.get().isPresent());

        final TestBean retval2 = f_retval2.get().get();

        assertEquals("{\"_id\":\"_id_1\",\"test_string\":\"test_string_1\"}",
                BeanTemplateUtils.toJson(retval2).toString());
    }
    // 3) Add the same with override set 
    {
        service.storeObject(test2, true).get();

        assertEquals(1, service.countObjects().get().intValue());

        final CompletableFuture<Optional<TestBean>> f_retval3 = service
                .getObjectBySpec(CrudUtils.allOf(TestBean.class));
        //final CompletableFuture<Optional<TestBean>> f_retval3 = service.getObjectById("_id_1");
        //^ note these 2 commands are equivalent because of the level of optimization configured for these tests

        final TestBean retval3 = f_retval3.get().get();

        assertEquals("{\"_id\":\"_id_1\",\"test_string\":\"test_string_2\"}",
                BeanTemplateUtils.toJson(retval3).toString());

        //4) add with no id

        final TestBean test4 = new TestBean();
        test4.test_string = "test_string_4";

        final Supplier<Object> result4 = service.storeObject(test4, true).get();

        assertEquals(2, service.countObjects().get().intValue());

        final String id = result4.get().toString();

        final CompletableFuture<Optional<TestBean>> f_retval4 = service.getObjectById(id);

        final TestBean retval4 = f_retval4.get().get();

        assertEquals("test_string_4", retval4.test_string);
    }
}

From source file:org.eclipse.hono.client.impl.HonoClientImpl.java

/**
 * Gets an existing or creates a new request-response client for a particular service.
 * //from www  . java2s  . co  m
 * @param key The key to look-up the client by.
 * @param clientSupplier A consumer for an attempt to create a new client.
 * @param resultHandler The handler to inform about the outcome of the operation.
 */
void getOrCreateRequestResponseClient(final String key,
        final Supplier<Future<RequestResponseClient>> clientSupplier,
        final Handler<AsyncResult<RequestResponseClient>> resultHandler) {

    context.runOnContext(get -> {
        final RequestResponseClient client = activeRequestResponseClients.get(key);
        if (client != null && client.isOpen()) {
            LOG.debug("reusing existing client [target: {}]", key);
            resultHandler.handle(Future.succeededFuture(client));
        } else if (!creationLocks.computeIfAbsent(key, k -> Boolean.FALSE)) {

            // register a handler to be notified if the underlying connection to the server fails
            // so that we can fail the result handler passed in
            final Handler<Void> connectionFailureHandler = connectionLost -> {
                // remove lock so that next attempt to open a sender doesn't fail
                creationLocks.remove(key);
                resultHandler
                        .handle(Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE,
                                "no connection to service")));
            };
            creationRequests.add(connectionFailureHandler);
            creationLocks.put(key, Boolean.TRUE);
            LOG.debug("creating new client for {}", key);

            clientSupplier.get().setHandler(creationAttempt -> {
                if (creationAttempt.succeeded()) {
                    LOG.debug("successfully created new client for {}", key);
                    activeRequestResponseClients.put(key, creationAttempt.result());
                } else {
                    LOG.debug("failed to create new client for {}", key, creationAttempt.cause());
                    activeRequestResponseClients.remove(key);
                }
                creationLocks.remove(key);
                creationRequests.remove(connectionFailureHandler);
                resultHandler.handle(creationAttempt);
            });

        } else {
            LOG.debug("already trying to create a client for {}", key);
            resultHandler.handle(Future.failedFuture(
                    new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "no connection to service")));
        }
    });
}

From source file:org.fineract.module.stellar.horizonadapter.HorizonServerUtilities.java

private <T extends Exception> void submitTransaction(final Account transactionSubmitter,
        final Transaction.Builder transactionBuilder, final KeyPair signingKeyPair,
        final Supplier<T> failureHandler) throws T {
    try {// ww  w  .j  a va  2  s .  co  m
        //final Long sequenceNumberSubmitted = account.getSequenceNumber();

        //noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (transactionSubmitter) {
            final Transaction transaction = transactionBuilder.build();
            transaction.sign(signingKeyPair);
            final SubmitTransactionResponse transactionResponse = server.submitTransaction(transaction);
            if (!transactionResponse.isSuccess()) {
                if (transactionResponse.getExtras() != null) {
                    logger.info("Stellar transaction failed, request: {}",
                            transactionResponse.getExtras().getEnvelopeXdr());
                    logger.info("Stellar transaction failed, response: {}",
                            transactionResponse.getExtras().getResultXdr());
                } else {
                    logger.info("Stellar transaction failed.  No extra information available.");
                }
                //TODO: resend transaction if you get a bad sequence.
                /*Thread.sleep(6000); //Wait for ledger to close.
                Long sequenceNumberShouldHaveBeen =
                    server.accounts().account(account.getKeypair()).getSequenceNumber();
                if (sequenceNumberSubmitted != sequenceNumberShouldHaveBeen) {
                  logger.info("Sequence number submitted: {}, Sequence number should have been: {}",
                      sequenceNumberSubmitted, sequenceNumberShouldHaveBeen);
                }*/
                throw failureHandler.get();
            }
        }
    } catch (final IOException e) {
        throw InvalidConfigurationException.unreachableStellarServerAddress(serverAddress);
    }
}

From source file:com.ikanow.aleph2.data_import_manager.analytics.actors.TestAnalyticsTriggerWorkerActor.java

@Test
public void test_externalTriggers() throws IOException, InterruptedException {
    System.out.println("Starting test_externalTriggers");

    //setup      
    final String json = Resources.toString(
            Resources.getResource("com/ikanow/aleph2/data_import_manager/analytics/actors/trigger_bucket.json"),
            Charsets.UTF_8);// w  w w  .  ja  va  2 s.c o  m

    final DataBucketBean bucket = BeanTemplateUtils.from(json, DataBucketBean.class).get();

    insertStatusForBucket(bucket);

    // (have to save the bucket to make trigger checks work correctly)
    _service_context.getService(IManagementDbService.class, Optional.empty()).get().getDataBucketStore()
            .storeObject(bucket, true).join();

    final ICrudService<AnalyticTriggerStateBean> trigger_crud = _service_context.getCoreManagementDbService()
            .readOnlyVersion().getAnalyticBucketTriggerState(AnalyticTriggerStateBean.class);
    trigger_crud.deleteDatastore().join();

    _num_received.set(0L);

    Supplier<Long> getCount = () -> trigger_crud
            .countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                    .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name()))
            .join();

    final AnalyticThreadJobBean job_to_run_first = bucket.analytic_thread().jobs().get(0);
    final AnalyticThreadJobBean job_to_run_last = bucket.analytic_thread().jobs().get(1);

    // 0) create input directory

    final String root_dir = _service_context.getStorageService().getBucketRootPath();
    final String suffix = IStorageService.TO_IMPORT_DATA_SUFFIX;
    createDirectory(root_dir, bucket.full_name(), suffix, 0, true);

    // 1) Send a message to the worker to fill in that bucket
    {
        final BucketActionMessage.NewBucketActionMessage msg = new BucketActionMessage.NewBucketActionMessage(
                bucket, false);

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish   
        waitForData(getCount, 1, true);

        // Check the DB - 2 external triggers, 1 internal trigger 

        assertEquals(3L, getCount.get().longValue());

        // Check the message bus - nothing yet!

        assertEquals(0, _num_received.get());
    }

    // 2) Perform a trigger check, make sure that nothing has activated
    {
        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        Thread.sleep(2000L);

        // Check the DB

        // (ie no active records)
        assertEquals(3L, getCount.get().longValue());

        // Check the message bus - nothing yet!

        assertEquals(0, _num_received.get());
    }

    // 3a) Add files to input directory, checks triggers (external triggers are slower so shouldn't trigger anything)
    {
        createDirectory(root_dir, bucket.full_name(), suffix, 1, false);

        //DEBUG: leave this in since failed vs travis
        _logger.info("(Added files)");
        printTriggerDatabase();

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        Thread.sleep(2000L);

        // Check the DB

        // (ie no active records)
        assertEquals(3L, getCount.get().longValue());

        // Check the message bus - nothing yet!

        assertEquals(0, _num_received.get());
    }
    // 3b) reset triggers try again ... still nothing because we only have 1/2 matching triggers
    {
        resetTriggerCheckTimes(trigger_crud); // (since the external triggers

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        Thread.sleep(2000L);

        // Check the DB

        // (ie no active records)
        assertEquals(3L, getCount.get().longValue());

        // Check the message bus - nothing yet!

        assertEquals(0, _num_received.get());
    }

    // 4a) Launch other bucket, checks triggers - should immediately fire since the "stop" of the previous bucket should update the check times of _both_ external dependencies
    {
        this.test_jobTriggerScenario_actuallyRun(trigger_crud);

        //DEBUG: leave this in since failed vs travis
        _logger.info("(Added files)");
        printTriggerDatabase();

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        waitForData(getCount, 4, true);
        waitForData(() -> _num_received.get(), 1, true);

        // Check the DB

        // 2 external triggers + internal trigger + bucket active record 
        assertEquals(4L, getCount.get().longValue());

        // Check the message bus - should get a job start notification

        assertEquals(1, _num_received.get());
    }

    // 5) Check doesn't trigger when active, even with files in directories .. but also that it doesn't shut the job down if it takes >10s for a job to start
    {
        resetTriggerCheckTimes(trigger_crud); // (since the external triggers

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        Thread.sleep(2000L);

        // Check the DB

        // external trigger + bucket active record + job active record
        assertEquals(4L, getCount.get().longValue());

        // Check the message bus - nothing else received

        assertEquals(1, _num_received.get());
    }

    // 5b) I should now receive some started messages back - again should do nothing
    {
        final BucketActionMessage.BucketActionAnalyticJobMessage inner_msg = new BucketActionMessage.BucketActionAnalyticJobMessage(
                bucket, Arrays.asList(job_to_run_first), JobMessageType.starting);

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(inner_msg);

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        waitForData(getCount, 5, true);

        // Check the DB

        // (ie creates +2, a bucket active record and a job active record)
        assertEquals(5L, getCount.get().longValue());

        // Confirm the extra 2 records are as above
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .withNotPresent(AnalyticTriggerStateBean::job_name)).join().intValue());
        assertEquals(1L,
                trigger_crud
                        .countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                                .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                                .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                                .when(AnalyticTriggerStateBean::job_name, job_to_run_first.name()))
                        .join().intValue());

        // Check the message bus - nothing changed

        assertEquals(1, _num_received.get());
    }

    // 5c) Check doesn't trigger when active, even with files in directories .. 
    {
        resetTriggerCheckTimes(trigger_crud); // (since the external triggers are for N minutes time unless a trigger event has occurred)

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        Thread.sleep(2000L);
        waitForData(() -> _num_received.get(), 2, true);

        // Check the DB

        // external trigger + bucket active record + job active record
        assertEquals(5L, getCount.get().longValue());

        // Check the message bus - get a check completion message

        assertEquals(2, _num_received.get());
    }

    // 6) OK send first job is complete

    {
        final BucketActionMessage.BucketActionAnalyticJobMessage inner_msg = new BucketActionMessage.BucketActionAnalyticJobMessage(
                bucket, Arrays.asList(job_to_run_first), JobMessageType.stopping);

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(inner_msg);

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        waitForData(getCount, 4, false);

        // Check the DB

        // external triggers + bucket active record
        assertEquals(4L, getCount.get().longValue());

        // Check the message bus - get a start message

        assertEquals(2, _num_received.get());
    }

    // 7) Kicks off again next trigger
    {
        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        waitForData(getCount, 5, true);
        waitForData(() -> _num_received.get(), 3, true);

        // Check the DB

        // external trigger + bucket active record + new job active record
        assertEquals(5L, getCount.get().longValue());

        // Check the message bus - get a check completion message

        assertEquals(3, _num_received.get());
    }

    // 8) OK send final job is complete

    {
        final BucketActionMessage.BucketActionAnalyticJobMessage inner_msg = new BucketActionMessage.BucketActionAnalyticJobMessage(
                bucket, Arrays.asList(job_to_run_last), JobMessageType.stopping);

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(inner_msg);

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        waitForData(getCount, 4, false);

        // Check the DB

        // external trigger + bucket active record 
        assertEquals(4L, getCount.get().longValue());

        // Check the message bus - no change

        assertEquals(3, _num_received.get());
    }

    // 9) Kicks off again next trigger
    {
        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        waitForData(getCount, 3, true);
        waitForData(() -> _num_received.get(), 4, true);

        // Check the DB

        // external triggers
        assertEquals(3L, getCount.get().longValue());

        // Check the message bus - get a thread stop message

        assertEquals(4, _num_received.get());
    }

    // 10) Finally check the (reset) external triggers - won't fire because even though the file is still present because the bucket dep has reset
    {
        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish
        Thread.sleep(2000L);

        // Check the DB

        // external trigger + bucket active record + new job active record
        assertEquals(3L, getCount.get().longValue());

        // Check the message bus - get a check completion message

        assertEquals(4, _num_received.get());
    }
    // Check no malformed buckets
    assertEquals(0L, _num_received_errors.get());
}

From source file:com.ikanow.aleph2.data_import_manager.analytics.actors.TestAnalyticsTriggerWorkerActor.java

public void test_jobTriggerScenario_actuallyRun(final ICrudService<AnalyticTriggerStateBean> trigger_crud)
        throws InterruptedException, IOException {

    // Setup://from  w ww  .  j a va2  s.c o m

    final String json_bucket = Resources.toString(
            Resources.getResource(
                    "com/ikanow/aleph2/data_import_manager/analytics/actors/simple_job_deps_bucket.json"),
            Charsets.UTF_8);
    final DataBucketBean bucket = BeanTemplateUtils.from(json_bucket, DataBucketBean.class).get();

    insertStatusForBucket(bucket);

    // This can run inside another job so need to be a bit      
    long prev = _num_received.get();
    _num_received.set(0L);
    Supplier<Long> getCount = () -> trigger_crud
            .countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                    .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name()))
            .join();

    // (have to save the bucket to make trigger checks work correctly)
    _service_context.getService(IManagementDbService.class, Optional.empty()).get().getDataBucketStore()
            .storeObject(bucket, true).join();

    // 1) Send a message to the worker to fill in that bucket
    {
        final BucketActionMessage.NewBucketActionMessage msg = new BucketActionMessage.NewBucketActionMessage(
                bucket, false);

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        waitForData(getCount, 3, true);
        //(status)
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.empty(), true), -1L, true);
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.empty(), false), -1L, true);

        // Check the DB

        assertEquals(3L, getCount.get().intValue());
        // Check they aren't suspended:
        assertEquals(3L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::is_bucket_suspended, false)).join().intValue());

        // Check the message bus - nothing yet!

        assertEquals(0, _num_received.get());

        // underlying status
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), true).longValue());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), false).longValue());
    }

    // 2) Perform a trigger check, make sure that nothing has activated
    {

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        //(code coverage! this will be ignored due to spacing)
        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        Thread.sleep(1000L);

        // Check the DB

        // (ie no active records)
        assertEquals(3L, getCount.get().intValue());

        // Check the message bus - nothing yet!

        assertEquals(0, _num_received.get());

        // underlying status
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), true).longValue());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), false).longValue());
    }

    // 3a) Let the worker know that job1 has started (which should also launch the bucket)
    final Date now_stage3a = new Date();
    {
        final BucketActionMessage.BucketActionAnalyticJobMessage inner_msg = new BucketActionMessage.BucketActionAnalyticJobMessage(
                bucket, bucket.analytic_thread().jobs().stream().filter(j -> j.name().equals("initial_phase"))
                        .collect(Collectors.toList()),
                JobMessageType.starting);

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(inner_msg);

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        waitForData(getCount, 5, true);
        //(status)
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.empty(), true), now_stage3a.getTime(),
                true);
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.empty(), false), -1L, true);

        // Check the DB

        // (ie creates a bucket active record and a job active record)
        assertEquals(5L, getCount.get().intValue());

        // Confirm the extra 2 records are as above
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .withNotPresent(AnalyticTriggerStateBean::job_name)).join().intValue());
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .when(AnalyticTriggerStateBean::job_name, "initial_phase")).join().intValue());

        // Check the message bus - nothing yet!

        assertEquals(0, _num_received.get());

        // underlying status: 1st job started, bucket started
        //global
        assertTrue(getLastRunTime(bucket.full_name(), Optional.empty(), true).longValue() >= now_stage3a
                .getTime());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), false).longValue());
        //job #1 "initial phase"
        assertTrue(getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), true)
                .longValue() >= now_stage3a.getTime());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), false).longValue());
    }

    // 3b) Send a job completion message
    Thread.sleep(100L); // (just make sure this is != now_stage3a)
    final Date now_stage3b = new Date();
    {
        final BucketActionMessage.BucketActionAnalyticJobMessage inner_msg = new BucketActionMessage.BucketActionAnalyticJobMessage(
                bucket, bucket.analytic_thread().jobs().stream().filter(j -> j.name().equals("initial_phase"))
                        .collect(Collectors.toList()),
                JobMessageType.stopping);

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(inner_msg);

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        waitForData(getCount, 4, false);
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), false), -1L, false);

        // Check the DB

        // (ie active job is removed, bucket remains)
        assertEquals(4L, getCount.get().intValue());

        // Confirm the extra record is as above
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .withNotPresent(AnalyticTriggerStateBean::job_name)).join().intValue());

        // Check the message bus - nothing yet!

        assertEquals(0, _num_received.get());

        // underlying status: 1st job stopped, bucket unchanged
        //global
        assertTrue(getLastRunTime(bucket.full_name(), Optional.empty(), true).longValue() >= now_stage3a
                .getTime());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), false).longValue());
        //job #1 "initial phase"
        final long last_time = getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), false)
                .longValue();
        assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage3a + " < " + now_stage3b,
                (last_time >= now_stage3a.getTime()) && (last_time < now_stage3b.getTime()));
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), true).longValue());
    }

    // 4) Perform a trigger check, make sure that only the right job ("next_phase") has started
    Thread.sleep(100L); // (just make sure this is != now_stage3a)
    final Date now_stage4 = new Date();
    {
        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        waitForData(getCount, 5, true);
        waitForData(() -> _num_received.get(), 1, true);
        //(status)
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.of("next_phase"), true),
                now_stage4.getTime(), true);
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.of("next_phase"), false), -1L, true);

        // Check the DB

        // (bucket active still present, now "next_phase" has started)
        assertEquals(5L, getCount.get().intValue());

        // Confirm the extra 2 records are as above
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .withNotPresent(AnalyticTriggerStateBean::job_name)).join().intValue());
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .when(AnalyticTriggerStateBean::job_name, "next_phase")).join().intValue());

        // Check the message bus - should have received a start message for the triggered job

        assertEquals(1, _num_received.get());

        // underlying status: 1st job stopped, 2nd job started, bucket unchanged
        //global
        assertTrue(getLastRunTime(bucket.full_name(), Optional.empty(), true).longValue() >= now_stage3a
                .getTime());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), false).longValue());
        //job #1 "initial phase"
        final long last_time = getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), false)
                .longValue();
        assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage3a + " < " + now_stage3b,
                (last_time >= now_stage3a.getTime()) && (last_time < now_stage3b.getTime()));
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), true).longValue());
        //job #2: next phase
        assertTrue(getLastRunTime(bucket.full_name(), Optional.of("next_phase"), true).longValue() >= now_stage4
                .getTime());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("next_phase"), false).longValue());
    }

    // 5) Send a job completion message
    Thread.sleep(100L); // (just make sure this is != now_stage3a)
    final Date now_stage5 = new Date();
    {
        final BucketActionMessage.BucketActionAnalyticJobMessage inner_msg = new BucketActionMessage.BucketActionAnalyticJobMessage(
                bucket, bucket.analytic_thread().jobs().stream().filter(j -> j.name().equals("next_phase"))
                        .collect(Collectors.toList()),
                JobMessageType.stopping);

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(inner_msg);

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        waitForData(getCount, 4, false);
        //(status)
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.of("next_phase"), false), -1L, false);

        // Check the DB

        // (ie active job is removed, bucket remains)
        assertEquals(4L, getCount.get().intValue());

        // Confirm the extra record is as above
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .withNotPresent(AnalyticTriggerStateBean::job_name)).join().intValue());

        // Check the message bus - no change

        assertEquals(1, _num_received.get());

        // underlying status: 1st job stopped, 2nd job stopped, bucket unchanged
        //global
        assertTrue(getLastRunTime(bucket.full_name(), Optional.empty(), true).longValue() >= now_stage3a
                .getTime());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), false).longValue());
        //job #1 "initial phase"
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage3a + " < " + now_stage3b,
                    (last_time >= now_stage3a.getTime()) && (last_time < now_stage3b.getTime()));
            assertEquals(-1L,
                    getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), true).longValue());
        }
        //job #2: next phase
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("next_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage4 + " < " + now_stage5,
                    (last_time >= now_stage4.getTime()) && (last_time < now_stage5.getTime()));
            assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("next_phase"), true).longValue());
        }
    }

    // 6) Perform a trigger check, make sure that only the right job ("final_phase") has started 
    Thread.sleep(100L); // (just make sure this is != now_stage3a)
    final Date now_stage6 = new Date();
    {
        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        waitForData(getCount, 5, true);
        waitForData(() -> _num_received.get(), 2, true);
        //(status)
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.of("final_phase"), true),
                now_stage6.getTime(), true);
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.of("final_phase"), false), -1L, true);

        // Check the DB

        // (bucket active still present, now "next_phase" has started)
        assertEquals(5L, getCount.get().intValue());

        // Confirm the extra 2 records are as above
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .withNotPresent(AnalyticTriggerStateBean::job_name)).join().intValue());
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .when(AnalyticTriggerStateBean::job_name, "final_phase")).join().intValue());

        // Check the message bus - should have received a start message for the triggered job

        assertEquals(2, _num_received.get()); //+1

        // underlying status: 1st job stopped, 2nd job stopped, 3rd job started bucket unchanged
        //global
        assertTrue(getLastRunTime(bucket.full_name(), Optional.empty(), true).longValue() >= now_stage3a
                .getTime());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), false).longValue());
        //job #1 "initial phase"
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage3a + " < " + now_stage3b,
                    (last_time >= now_stage3a.getTime()) && (last_time < now_stage3b.getTime()));
            assertEquals(-1L,
                    getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), true).longValue());
        }
        //job #2: next phase
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("next_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage4 + " < " + now_stage5,
                    (last_time >= now_stage4.getTime()) && (last_time < now_stage5.getTime()));
            assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("next_phase"), true).longValue());
        }
        //job #3: final phase
        {
            assertTrue(getLastRunTime(bucket.full_name(), Optional.of("final_phase"), true)
                    .longValue() >= now_stage6.getTime());
            assertEquals(-1L,
                    getLastRunTime(bucket.full_name(), Optional.of("final_phase"), false).longValue());
        }
    }

    // 7) Stop the final job
    Thread.sleep(100L); // (just make sure this is != now_stage3a)
    final Date now_stage7 = new Date();
    {
        final BucketActionMessage.BucketActionAnalyticJobMessage inner_msg = new BucketActionMessage.BucketActionAnalyticJobMessage(
                bucket, bucket.analytic_thread().jobs().stream().filter(j -> j.name().equals("final_phase"))
                        .collect(Collectors.toList()),
                JobMessageType.stopping);

        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(inner_msg);

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        waitForData(getCount, 4, false);
        //(status)
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.of("final_phase"), false), -1L, false);

        // Check the DB

        // (ie active job is removed, bucket remains)
        assertEquals(4L, getCount.get().intValue());

        // Confirm the extra record is as above
        assertEquals(1L,
                trigger_crud.countObjectsBySpec(CrudUtils.allOf(AnalyticTriggerStateBean.class)
                        .when(AnalyticTriggerStateBean::bucket_name, bucket.full_name())
                        .when(AnalyticTriggerStateBean::trigger_type, TriggerType.none)
                        .withNotPresent(AnalyticTriggerStateBean::job_name)).join().intValue());

        // Check the message bus - no change

        assertEquals(2, _num_received.get());

        // underlying status: 1st job stopped, 2nd job stopped, 3rd job stopped, bucket unchanged
        //global
        assertTrue(getLastRunTime(bucket.full_name(), Optional.empty(), true).longValue() >= now_stage3a
                .getTime());
        assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), false).longValue());
        //job #1 "initial phase"
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage3a + " < " + now_stage3b,
                    (last_time >= now_stage3a.getTime()) && (last_time < now_stage3b.getTime()));
            assertEquals(-1L,
                    getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), true).longValue());
        }
        //job #2: next phase
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("next_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage4 + " < " + now_stage5,
                    (last_time >= now_stage4.getTime()) && (last_time < now_stage5.getTime()));
            assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("next_phase"), true).longValue());
        }
        //job #3: final phase
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("final_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage6 + " < " + now_stage7,
                    (last_time >= now_stage6.getTime()) && (last_time < now_stage7.getTime()));
            assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("final_phase"), true).longValue());
        }
    }

    // 8) Trigger - should complete the bucket (since has had bucket activated, 60s timeout doesn't apply) 
    Thread.sleep(100L); // (just make sure this is != now_stage3a)
    final Date now_stage8 = new Date();
    {
        final AnalyticTriggerMessage msg = new AnalyticTriggerMessage(
                new AnalyticTriggerMessage.AnalyticsTriggerActionMessage());

        _trigger_worker.tell(msg, _trigger_worker);

        // Give it a couple of secs to finish         
        waitForData(getCount, 3, false);
        waitForData(() -> _num_received.get(), 3, true);
        //(status)
        waitForData(() -> getLastRunTime(bucket.full_name(), Optional.empty(), false), -1L, false);

        // Check the DB

        // (all active records removed)
        assertEquals(3L, getCount.get().intValue());

        // Check the message bus - should have received a stop message for the bucket

        assertEquals(3, _num_received.get());

        // underlying status: 1st job stopped, 2nd job stopped, 3rd job stopped, bucket stopped
        //global
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.empty(), false).longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage3a + " < " + now_stage8,
                    (last_time >= now_stage3a.getTime()) && (last_time < now_stage8.getTime()));
            assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.empty(), true).longValue());
        }
        //job #1 "initial phase"
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage3a + " < " + now_stage3b,
                    (last_time >= now_stage3a.getTime()) && (last_time < now_stage3b.getTime()));
            assertEquals(-1L,
                    getLastRunTime(bucket.full_name(), Optional.of("initial_phase"), true).longValue());
        }
        //job #2: next phase
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("next_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage4 + " < " + now_stage5,
                    (last_time >= now_stage4.getTime()) && (last_time < now_stage5.getTime()));
            assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("next_phase"), true).longValue());
        }
        //job #3: final phase
        {
            final long last_time = getLastRunTime(bucket.full_name(), Optional.of("final_phase"), false)
                    .longValue();
            assertTrue("Time errors: " + new Date(last_time) + " >= " + now_stage6 + " < " + now_stage7,
                    (last_time >= now_stage6.getTime()) && (last_time < now_stage7.getTime()));
            assertEquals(-1L, getLastRunTime(bucket.full_name(), Optional.of("final_phase"), true).longValue());
        }
    }
    _num_received.set(prev);

    // Check no malformed buckets
    assertEquals(0L, _num_received_errors.get());
}

From source file:com.github.aptd.simulation.datamodel.CXMLReader.java

@Override
@SuppressWarnings("unchecked")
public final IExperiment get(final IFactory p_factory, final String p_datamodel, final long p_simulationsteps,
        final boolean p_parallel, final String p_timemodel,
        final Supplier<RealDistribution> p_platformchangedurationdistributionsupplier,
        final int p_numberofpassengers, final double p_lightbarrierminfreetime, final double p_delayseconds) {
    try (final FileInputStream l_stream = new FileInputStream(p_datamodel)) {
        final Asimov l_model = (Asimov) m_context.createUnmarshaller().unmarshal(l_stream);

        // time definition

        final Instant l_starttime = ZonedDateTime.now(ZoneId.systemDefault())
                .with(ChronoField.CLOCK_HOUR_OF_DAY, 8).with(ChronoField.MINUTE_OF_HOUR, 45)
                .with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.NANO_OF_SECOND, 0)
                .with(ChronoField.DAY_OF_MONTH, 3).with(ChronoField.MONTH_OF_YEAR, 10)
                .with(ChronoField.YEAR, 2017).toInstant();

        final ITime l_time = "jump".equals(p_timemodel) ? new CJumpTime(l_starttime, p_simulationsteps)
                : new CStepTime(l_starttime, Duration.ofSeconds(1), p_simulationsteps);

        final CMessenger l_messenger = new CMessenger();

        final Set<IAction> l_actionsfrompackage = CCommon.actionsFromPackage().collect(Collectors.toSet());

        // asl agent definition
        final Map<String, String> l_agentdefs = agents(l_model.getAi());

        // macro (train-network) and microscopic model
        final Map<String, IPlatform<?>> l_platform = platform(l_model.getNetwork(), l_agentdefs, p_factory,
                l_time);//ww  w .  j ava 2 s  . c o m
        final Map<String, IStation<?>> l_station = station(l_model.getNetwork(), l_agentdefs, p_factory, l_time,
                l_platform);
        final Pair<Map<String, ITrain<?>>, Map<String, IDoor<?>>> l_train = train(l_model.getNetwork(),
                l_agentdefs, p_factory, l_time, p_lightbarrierminfreetime);

        final Map<String, IElement<?>> l_agents = new HashMap<>();
        l_agents.putAll(l_platform);
        l_agents.putAll(l_station);
        l_agents.putAll(l_train.getLeft());
        l_agents.putAll(l_train.getRight());

        final CExperiment l_experiment = new CExperiment(p_simulationsteps, p_parallel, IStatistic.EMPTY,
                l_agents, l_time, l_messenger);

        // @todo create passengersources and their passenger generators according to scenario definition

        final IElement.IGenerator<IPassenger<?>> l_passengergenerator = passengergenerator(p_factory,
                "+!activate <-\n    state/transition\n.", l_actionsfrompackage, l_time);

        l_experiment.addAgent("passengersource_test",
                passengersourcegenerator(p_factory, "+!activate <-\n    state/transition\n.",
                        l_actionsfrompackage, l_time).generatesingle(new UniformRealDistribution(0.0, 1.0),
                                l_time.current().toEpochMilli(), p_numberofpassengers, l_passengergenerator,
                                l_experiment, l_agents.get("toy-node-1"),
                                p_platformchangedurationdistributionsupplier.get()));

        l_messenger.experiment(l_experiment);

        // experiment (executable model)
        return l_experiment;

    } catch (final Exception l_execption) {
        throw new CRuntimeException(l_execption);
    }
}

From source file:gedi.util.ArrayUtils.java

@SuppressWarnings("unchecked")
public static <T> T[] create(Supplier<T> fac, Class<T> cls, int length) {
    T[] re = (T[]) Array.newInstance(cls, length);
    for (int i = 0; i < re.length; i++)
        re[i] = fac.get();
    return re;/*from   w  w  w  . j a v  a  2 s.  co  m*/
}

From source file:nl.rivm.cib.episim.model.disease.infection.MSEIRSTest.java

private <T> Observable<Entry<T, Stream<BigDecimal>>> averages(
        final Supplier<Observable<Entry<Double, long[]>>> sir, final Function<Double, T> bins, final int n) {
    return Observable.create(sub -> {
        final NavigableMap<T, long[]> sums = java.util.Collections
                .synchronizedNavigableMap(new TreeMap<T, long[]>());
        final long t0 = System.currentTimeMillis();
        final AtomicInteger iteration = new AtomicInteger();
        final AtomicLong sysTime = new AtomicLong(t0);
        Observable.range(0, n).flatMap(i -> Observable.just(i).subscribeOn(Schedulers.computation()).map(ii -> {
            final int iii = iteration.incrementAndGet();
            final long t = System.currentTimeMillis();
            sysTime.updateAndGet(t1 -> {
                if (t - t1 > 10000) {
                    LOG.trace("Progress {}% at ~{}/s, iteration {} of {}",
                            DecimalUtil.floor(DecimalUtil.divide(iii * 100, n)),
                            DecimalUtil.round(DecimalUtil.divide(iii * 1000, t - t0)), iii, n);
                    return t;
                }/*from www . j  av a 2 s .  co  m*/
                return t1;
            });
            return sir.get()
                    // group by bin size
                    .groupBy(yt -> bins.apply(yt.getKey()))
                    // take highest floating point t in this bin
                    .flatMap(gr -> gr.reduce((yt1, yt2) -> yt1.getKey().compareTo(yt2.getKey()) > 0 ? yt1 : yt2)
                            .toObservable().map(yt -> Collections.entry(gr.getKey(), yt.getValue())))
                    // add to current sums
                    .collect(() -> sums,
                            (sum, yt) -> sum.compute(yt.getKey(),
                                    (k, v) -> v == null ? yt.getValue()
                                            : IntStream.range(0, v.length)
                                                    .mapToLong(iv -> v[iv] + yt.getValue()[iv]).toArray()))
                    .blockingGet();
        })).blockingSubscribe();

        sums.forEach((k, v) -> sub
                .onNext(Collections.entry(k, Arrays.stream(v).mapToObj(y -> DecimalUtil.divide(y, n)))));
        final long dt = System.currentTimeMillis() - t0;
        LOG.trace("Completed {} iterations in {}s = {}/s", n,
                DecimalUtil.toScale(DecimalUtil.divide(dt, 1000), 1),
                DecimalUtil.round(DecimalUtil.divide(n * 1000, dt)));
    });
}