Search in sources :

Example 6 with Context

use of com.codahale.metrics.Timer.Context in project torodb by torodb.

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_CudAnalyzedOplog_NotRepyingRollback.

/**
   * Test the behaviour of the method
   * {@link SimpleAnalyzedOplogBatchExecutor#visit(com.torodb.mongodb.repl.oplogreplier.batch.CudAnalyzedOplogBatch, com.torodb.mongodb.repl.oplogreplier.ApplierContext)  that visits a cud batch}
   * when
   * {@link SimpleAnalyzedOplogBatchExecutor#execute(com.torodb.mongodb.repl.oplogreplier.batch.CudAnalyzedOplogBatch, com.torodb.mongodb.repl.oplogreplier.ApplierContext) the execution}
   * fails until the given attempt.
   *
   *
   * @param myRetrier
   * @param atteptsToSucceed
   * @return true if the execution finishes or false if it throw an exception.
   * @throws Exception
   */
private boolean testVisit_CudAnalyzedOplog_NotRepyingRollback(Retrier myRetrier, int atteptsToSucceed) throws Exception {
    //GIVEN
    OplogOperation lastOp = mock(OplogOperation.class);
    CudAnalyzedOplogBatch batch = mock(CudAnalyzedOplogBatch.class);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(false).build();
    given(batch.getOriginalBatch()).willReturn(Lists.newArrayList(mock(OplogOperation.class), mock(OplogOperation.class), mock(OplogOperation.class), lastOp));
    executor = spy(new SimpleAnalyzedOplogBatchExecutor(metrics, applier, server, myRetrier, namespaceJobExecutor));
    Timer timer = mock(Timer.class);
    Context context = mock(Context.class);
    given(metrics.getCudBatchTimer()).willReturn(timer);
    given(timer.time()).willReturn(context);
    doAnswer(new Answer() {

        int attempts = 0;

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            try {
                ApplierContext context = invocation.getArgument(1);
                if (attempts == 0) {
                    assert !context.treatUpdateAsUpsert() : "on this test, first attept should be not trying updates as upserts";
                    throw new RollbackException("Forcing a rollback on the first attempt");
                }
                assert context.treatUpdateAsUpsert() : "on this test, only the first attept should be " + "not trying updates as upserts, but " + attempts + " is not trying updates as upserts";
                if (attempts < (atteptsToSucceed - 1)) {
                    throw new RollbackException("forcing a rollback on the " + attempts + "th attempt");
                }
                return null;
            } finally {
                attempts++;
            }
        }
    }).when(executor).execute(eq(batch), any());
    boolean success;
    try {
        //WHEN
        OplogOperation result = executor.visit(batch, applierContext);
        //THEN
        then(executor).should(times(atteptsToSucceed)).execute(eq(batch), any());
        assertEquals(lastOp, result);
        success = true;
    } catch (RetrierGiveUpException ignore) {
        success = false;
    }
    then(metrics.getCudBatchSize()).should().update(batch.getOriginalBatch().size());
    then(metrics).should().getCudBatchTimer();
    then(metrics.getCudBatchTimer()).should().time();
    return success;
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) RollbackException(com.torodb.core.transaction.RollbackException) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Timer(com.codahale.metrics.Timer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation)

Example 7 with Context

use of com.codahale.metrics.Timer.Context in project torodb by torodb.

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_CudAnalyzedOplog_UserEx.

@Test
public void testVisit_CudAnalyzedOplog_UserEx() throws Exception {
    //GIVEN
    OplogOperation lastOp = mock(OplogOperation.class);
    CudAnalyzedOplogBatch batch = mock(CudAnalyzedOplogBatch.class);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
    given(batch.getOriginalBatch()).willReturn(Lists.newArrayList(mock(OplogOperation.class), mock(OplogOperation.class), mock(OplogOperation.class), lastOp));
    Timer timer = mock(Timer.class);
    Context context = mock(Context.class);
    given(metrics.getCudBatchTimer()).willReturn(timer);
    given(timer.time()).willReturn(context);
    doThrow(new DatabaseNotFoundException("test")).when(executor).execute(eq(batch), any());
    //WHEN
    try {
        executor.visit(batch, applierContext);
        fail("An exception was expected");
    } catch (RetrierGiveUpException | RetrierAbortException ignore) {
    }
    //THEN
    then(metrics).should().getCudBatchTimer();
    then(timer).should().time();
    then(executor).should(times(1)).execute(batch, applierContext);
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) Timer(com.codahale.metrics.Timer) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) DatabaseNotFoundException(com.torodb.core.exceptions.user.DatabaseNotFoundException) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Test(org.junit.Test)

Example 8 with Context

use of com.codahale.metrics.Timer.Context in project torodb by torodb.

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_SingleOp_OplogApplyingEx.

@Test
public void testVisit_SingleOp_OplogApplyingEx() throws Exception {
    //GIVEN
    OplogOperation operation = mock(OplogOperation.class);
    SingleOpAnalyzedOplogBatch batch = new SingleOpAnalyzedOplogBatch(operation);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
    Timer timer = mock(Timer.class);
    Context context = mock(Context.class);
    given(metrics.getSingleOpTimer(operation)).willReturn(timer);
    given(timer.time()).willReturn(context);
    doThrow(new OplogApplyingException(new MongoException(ErrorCode.BAD_VALUE))).when(executor).execute(operation, applierContext);
    //WHEN
    try {
        executor.visit(batch, applierContext);
        fail("An exception was expected");
    } catch (RetrierGiveUpException | RetrierAbortException ignore) {
    }
    //THEN
    then(metrics).should().getSingleOpTimer(operation);
    then(timer).should().time();
    then(executor).should(times(1)).execute(operation, applierContext);
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) OplogApplyingException(com.torodb.mongodb.repl.oplogreplier.OplogOperationApplier.OplogApplyingException) MongoException(com.eightkdata.mongowp.exceptions.MongoException) Timer(com.codahale.metrics.Timer) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Test(org.junit.Test)

Example 9 with Context

use of com.codahale.metrics.Timer.Context in project opennms by OpenNMS.

the class StressCommand method doExecute.

@Override
protected Void doExecute() {
    // Apply sane lower bounds to all of the configurable options
    intervalInSeconds = Math.max(1, intervalInSeconds);
    numberOfNodes = Math.max(1, numberOfNodes);
    numberOfInterfacesPerNode = Math.max(1, numberOfInterfacesPerNode);
    numberOfGroupsPerInterface = Math.max(1, numberOfGroupsPerInterface);
    numberOfNumericAttributesPerGroup = Math.max(0, numberOfNumericAttributesPerGroup);
    numberOfStringAttributesPerGroup = Math.max(0, numberOfStringAttributesPerGroup);
    reportIntervalInSeconds = Math.max(1, reportIntervalInSeconds);
    numberOfGeneratorThreads = Math.max(1, numberOfGeneratorThreads);
    stringVariationFactor = Math.max(0, stringVariationFactor);
    if (stringVariationFactor > 0) {
        stringAttributesVaried = metrics.meter("string-attributes-varied");
    }
    // Display the effective settings and rates
    double attributesPerSecond = (1 / (double) intervalInSeconds) * numberOfGroupsPerInterface * numberOfInterfacesPerNode * numberOfNodes;
    System.out.printf("Generating collection sets every %d seconds\n", intervalInSeconds);
    System.out.printf("\t for %d nodes\n", numberOfNodes);
    System.out.printf("\t with %d interfaces\n", numberOfInterfacesPerNode);
    System.out.printf("\t with %d attribute groups\n", numberOfGroupsPerInterface);
    System.out.printf("\t with %d numeric attributes\n", numberOfNumericAttributesPerGroup);
    System.out.printf("\t with %d string attributes\n", numberOfStringAttributesPerGroup);
    System.out.printf("Across %d threads\n", numberOfGeneratorThreads);
    if (stringVariationFactor > 0) {
        System.out.printf("With string variation factor %d\n", stringVariationFactor);
    }
    System.out.printf("Which will yield an effective\n");
    System.out.printf("\t %.2f numeric attributes per second\n", numberOfNumericAttributesPerGroup * attributesPerSecond);
    System.out.printf("\t %.2f string attributes per second\n", numberOfStringAttributesPerGroup * attributesPerSecond);
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
    // Setup the executor
    ThreadFactory threadFactoy = new ThreadFactoryBuilder().setNameFormat("Metrics Stress Tool Generator #%d").build();
    ExecutorService executor = Executors.newFixedThreadPool(numberOfGeneratorThreads, threadFactoy);
    // Setup auxiliary objects needed by the persister
    ServiceParameters params = new ServiceParameters(Collections.emptyMap());
    RrdRepository repository = new RrdRepository();
    repository.setStep(Math.max(intervalInSeconds, 1));
    repository.setHeartBeat(repository.getStep() * 2);
    if (rras != null && rras.size() > 0) {
        repository.setRraList(rras);
    } else {
        repository.setRraList(Lists.newArrayList(// Use the default list of RRAs we provide in our stock configuration files
        "RRA:AVERAGE:0.5:1:2016", "RRA:AVERAGE:0.5:12:1488", "RRA:AVERAGE:0.5:288:366", "RRA:MAX:0.5:288:366", "RRA:MIN:0.5:288:366"));
    }
    repository.setRrdBaseDir(Paths.get(System.getProperty("opennms.home"), "share", "rrd", "snmp").toFile());
    // Calculate how we fast we should insert the collection sets
    int sleepTimeInMillisBetweenNodes = 0;
    int sleepTimeInSecondsBetweenIterations = 0;
    System.out.printf("Sleeping for\n");
    if (burst) {
        sleepTimeInSecondsBetweenIterations = intervalInSeconds;
        System.out.printf("\t %d seconds between batches\n", sleepTimeInSecondsBetweenIterations);
    } else {
        // We want to "stream" the collection sets
        sleepTimeInMillisBetweenNodes = Math.round((((float) intervalInSeconds * 1000) / numberOfNodes) * numberOfGeneratorThreads);
        System.out.printf("\t %d milliseconds between nodes\n", sleepTimeInMillisBetweenNodes);
    }
    // Start generating, and keep generating until we're interrupted
    try {
        reporter.start(reportIntervalInSeconds, TimeUnit.SECONDS);
        while (true) {
            final Context context = batchTimer.time();
            try {
                // Split the tasks up among the threads
                List<Future<Void>> futures = new ArrayList<>();
                for (int generatorThreadId = 0; generatorThreadId < numberOfGeneratorThreads; generatorThreadId++) {
                    futures.add(executor.submit(generateAndPersistCollectionSets(params, repository, generatorThreadId, sleepTimeInMillisBetweenNodes)));
                }
                // Wait for all the tasks to complete before starting others
                for (Future<Void> future : futures) {
                    future.get();
                }
            } catch (InterruptedException | ExecutionException e) {
                break;
            } finally {
                context.stop();
            }
            try {
                Thread.sleep(sleepTimeInSecondsBetweenIterations * 1000);
            } catch (InterruptedException e) {
                break;
            }
        }
    } finally {
        reporter.stop();
        abort.set(true);
        executor.shutdownNow();
    }
    return null;
}
Also used : Context(com.codahale.metrics.Timer.Context) ThreadFactory(java.util.concurrent.ThreadFactory) ConsoleReporter(com.codahale.metrics.ConsoleReporter) ArrayList(java.util.ArrayList) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Future(java.util.concurrent.Future) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with Context

use of com.codahale.metrics.Timer.Context in project opennms by OpenNMS.

the class EventIpcBroadcastProcessor method process.

@Override
public void process(Log eventLog, boolean synchronous) throws EventProcessorException {
    if (eventLog != null && eventLog.getEvents() != null && eventLog.getEvents().getEvent() != null) {
        try (Context context = logBroadcastTimer.time()) {
            for (Event eachEvent : eventLog.getEvents().getEvent()) {
                process(eventLog.getHeader(), eachEvent, synchronous);
                eventBroadcastMeter.mark();
            }
        }
    }
}
Also used : Context(com.codahale.metrics.Timer.Context) Event(org.opennms.netmgt.xml.event.Event)

Aggregations

Context (com.codahale.metrics.Timer.Context)37 Timer (com.codahale.metrics.Timer)11 MetricsContext (com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext)10 ApplierContext (com.torodb.mongodb.repl.oplogreplier.ApplierContext)9 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)8 Test (org.junit.Test)7 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)6 SQLException (java.sql.SQLException)5 PreparedStatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper)4 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 StatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)3 RollbackException (com.torodb.core.transaction.RollbackException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 ExecutorService (java.util.concurrent.ExecutorService)3 ConsoleReporter (com.codahale.metrics.ConsoleReporter)2 Counter (com.codahale.metrics.Counter)2