Example usage for java.util.concurrent TimeUnit MINUTES

List of usage examples for java.util.concurrent TimeUnit MINUTES

Introduction

In this page you can find the example usage for java.util.concurrent TimeUnit MINUTES.

Prototype

TimeUnit MINUTES

To view the source code for java.util.concurrent TimeUnit MINUTES.

Click Source Link

Document

Time unit representing sixty seconds.

Usage

From source file:functionaltests.SchedulerClientTest.java

@Test(timeout = MAX_WAIT_TIME)
public void testWaitForTerminatingJob() throws Exception {
    ISchedulerClient client = clientInstance();
    Job job = defaultJob();/*w ww.j  a  va  2  s  . co m*/
    JobId jobId = submitJob(job, client);
    // should return immediately
    client.waitForJob(jobId, TimeUnit.MINUTES.toMillis(3));
}

From source file:com.blacklocus.qs.worker.util.log.SamplingQSLogServiceTest.java

@Test
public void testSampledLifeCycle() throws InterruptedException {

    // With these parameters, by far most logger interactions should be filtered out, very few sampled in.
    final int numThreads = 64, iterations = 200, processingJitterMaxMs = 16, noSoonerThanMs = 100;

    final Set<String> sampledTaskIds = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());

    final QSLogService logService = Mockito.mock(QSLogService.class);
    // track which logging interactions were allowed through (sampled in)
    Mockito.doAnswer(new Answer() {
        @Override//www  . j av a  2  s .  c o  m
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sampledTaskIds.add(((QSTaskModel) invocation.getArguments()[0]).taskId);
            return null;
        }
    }).when(logService).startedTask(Matchers.any(QSTaskModel.class));
    Mockito.doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sampledTaskIds.add(((QSLogModel) invocation.getArguments()[0]).taskId);
            return null; //TODO jason
        }
    }).when(logService).log(Matchers.any(QSLogModel.class));
    Mockito.doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sampledTaskIds.add(((QSTaskModel) invocation.getArguments()[0]).taskId);
            return null; //TODO jason
        }
    }).when(logService).completedTask(Matchers.any(QSTaskModel.class));

    Predicate<QSTaskModel> taskPredicate = SamplingPredicates.noSoonerThan(noSoonerThanMs,
            TimeUnit.MILLISECONDS);
    final QSLogService sampledLogService = new SamplingQSLogService(logService, taskPredicate);

    long startNs = System.nanoTime();
    ExecutorService threads = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numThreads; i++) {
        threads.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                LOG.debug("Thread start {}", Thread.currentThread().getName());
                for (int i = 0; i < iterations; i++) {

                    String taskId = UUID.randomUUID().toString();

                    // simulate task processing, some have logs, some don't, processing time varies between each step
                    QSTaskModel task = new QSTaskModel();
                    task.taskId = taskId;
                    Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs));

                    sampledLogService.startedTask(task);
                    Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs));

                    // random number of associated logs [0, 2]
                    for (int j = RandomUtils.nextInt(2); j > 0; j--) {
                        QSLogModel log = new QSLogModel();
                        log.taskId = taskId;
                        sampledLogService.log(log);
                        Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs));
                    }

                    sampledLogService.completedTask(task);
                }
                LOG.debug("Thread end {}", Thread.currentThread().getName());
                return null;

            }
        });
    }
    threads.shutdown();
    threads.awaitTermination(1, TimeUnit.MINUTES);
    long endNs = System.nanoTime();

    // Theoretical maximum number of sampled in task logging
    long durationMs = TimeUnit.NANOSECONDS.toMillis(endNs - startNs);
    long expectedMax = durationMs / noSoonerThanMs + 1; // +1 for time@0: sampled in
    LOG.debug("Run duration: {}ms  no sooner than: {}ms", durationMs, noSoonerThanMs);
    LOG.debug("Expected max sampled in: {}  Actually sampled: {}", expectedMax, sampledTaskIds.size());
    Assert.assertTrue(expectedMax >= sampledTaskIds.size());
}

From source file:com.rmn.qa.servlet.AutomationTestRunServlet.java

private void initCleanupThreads() {
    // Wrapper to lazily fetch the Registry object as this is not populated at instantiation time
    // Spin up a scheduled thread to poll for unused test runs and clean up them
    Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new AutomationRunCleanupTask(this), 60L,
            60L, TimeUnit.SECONDS);
    // Spin up a scheduled thread to clean up and terminate nodes that were spun up
    Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
            new AutomationNodeCleanupTask(this, ec2, requestMatcher), 60L, 15L, TimeUnit.SECONDS);
    // Spin up a scheduled thread to register unregistered dynamic nodes (will happen if hub gets shut down)
    Executors.newSingleThreadScheduledExecutor()
            .scheduleAtFixedRate(new AutomationOrphanedNodeRegistryTask(this), 1L, 5L, TimeUnit.MINUTES);
    // Spin up a scheduled thread to track nodes that are pending startup
    Executors.newSingleThreadScheduledExecutor()
            .scheduleAtFixedRate(new AutomationPendingNodeRegistryTask(this, ec2), 60L, 15L, TimeUnit.SECONDS);
    // Spin up a scheduled thread to analyzed queued requests to scale up capacity
    Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new AutomationScaleNodeTask(this, ec2),
            60L, 15L, TimeUnit.SECONDS);
    String instanceId = System.getProperty(AutomationConstants.INSTANCE_ID);
    if (instanceId != null && instanceId.length() > 0) {
        log.info("Instance ID detected.  Hub termination thread will be started.");
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
                new AutomationHubCleanupTask(this, ec2, instanceId), 5L, 1L, TimeUnit.MINUTES);
    } else {//w ww .j  a  va  2  s .  c  o  m
        log.info("Hub is not a dynamic hub -- termination logic will not be started");
    }
    String runReaperThread = System.getProperty(AutomationConstants.REAPER_THREAD_CONFIG);
    // Reaper thread defaults to on unless specified not to run
    if (!"false".equalsIgnoreCase(runReaperThread)) {
        // Spin up a scheduled thread to terminate orphaned instances
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new AutomationReaperTask(this, ec2),
                1L, 15L, TimeUnit.MINUTES);
    } else {
        log.info("Reaper thread not running due to config flag.");
    }
}

From source file:org.jberet.support.io.XmlItemReaderTest.java

private void testReadWrite0(final String resource, final String writeResource, final String start,
        final String end, final Class<?> beanType, final String expect, final String forbid) throws Exception {
    final Properties params = CsvItemReaderWriterTest.createParams(CsvProperties.BEAN_TYPE_KEY,
            beanType.getName());// w w w.  j  a v  a2s  .  c om
    params.setProperty(CsvProperties.RESOURCE_KEY, resource);

    final File file = new File(CsvItemReaderWriterTest.tmpdir, writeResource);
    params.setProperty("writeResource", file.getPath());
    if (resource.equals(movieXml)) {
        params.setProperty("rootElementName", movieRootElementName);
    } else if (resource.equals(osmXml)) {
        params.setProperty("rootElementName", osmRootElementName);
    } else {
        throw new IllegalStateException("Unknown resource: " + resource);
    }

    if (start != null) {
        params.setProperty(CsvProperties.START_KEY, start);
    }
    if (end != null) {
        params.setProperty(CsvProperties.END_KEY, end);
    }
    if (customDataTypeModules != null) {
        params.setProperty("customDataTypeModules", customDataTypeModules);
    }
    CsvItemReaderWriterTest.setRandomWriteMode(params);

    final long jobExecutionId = jobOperator.start(jobName, params);
    final JobExecutionImpl jobExecution = (JobExecutionImpl) jobOperator.getJobExecution(jobExecutionId);
    jobExecution.awaitTermination(CsvItemReaderWriterTest.waitTimeoutMinutes * 100, TimeUnit.MINUTES);
    Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());

    if (!resource.equals(osmXml)) {
        //avoid reading the very large osm xml output file
        CsvItemReaderWriterTest.validate(file, expect, forbid);
    }
}

From source file:com.stimulus.archiva.index.VolumeIndex.java

protected void openIndex() throws MessageSearchException {
    Exception lastError = null;// www  . ja va  2s.  c  o  m
    try {
        boolean isLocked = indexLock.tryLock(10, TimeUnit.MINUTES);
        if (!isLocked) {
            throw new MessageSearchException("failed to open index. could not acquire lock on index.", logger);
        }
        if (writer == null) {
            logger.debug("openIndex() index will be opened. it is currently closed.");
        } else {
            logger.debug("openIndex() did not bother opening index. it is already open.");
            return;
        }
        logger.debug("opening index for write {" + volume + "}");
        indexer.prepareIndex(volume);
        logger.debug("opening search index for write {indexpath='" + volume.getIndexPath() + "'}");
        boolean writelock;
        int attempt = 0;
        int maxattempt = 10;

        do {
            writelock = false;
            try {
                FSDirectory fsDirectory = FSDirectory.getDirectory(volume.getIndexPath());
                writer = new IndexWriter(fsDirectory, analyzer, new IndexWriter.MaxFieldLength(50000));
                if (logger.isDebugEnabled() && indexLogOut != null) {
                    writer.setInfoStream(indexLogOut);
                }
            } catch (LockObtainFailedException lobfe) {
                logger.debug("write lock on index. will reopen in 50ms.");
                try {
                    Thread.sleep(50);
                } catch (Exception e) {
                }
                attempt++;
                writelock = true;
            } catch (CorruptIndexException cie) {
                throw new MessageSearchException(
                        "index appears to be corrupt. please reindex the active volume." + cie.getMessage(),
                        logger);
            } catch (IOException io) {
                throw new MessageSearchException("failed to write document to index:" + io.getMessage(),
                        logger);
            }
        } while (writelock && attempt < maxattempt);
        if (attempt >= 10000)
            throw new MessageSearchException(
                    "failed to open index writer {location='" + volume.getIndexPath() + "'}", lastError,
                    logger);
    } catch (InterruptedException ie) {
        throw new MessageSearchException("failed to open index. interrupted.", logger);
    } finally {
        indexLock.unlock();
    }
}

From source file:com.hortonworks.hbase.BufferedMutatorExample.java

@Override
public int run(String[] args) throws InterruptedException, ExecutionException, TimeoutException {

    /** a callback invoked when an asynchronous write fails. */
    final BufferedMutator.ExceptionListener listener = new BufferedMutator.ExceptionListener() {
        @Override//from w w w  .j a  v a 2  s.c  o  m
        public void onException(RetriesExhaustedWithDetailsException e, BufferedMutator mutator) {
            for (int i = 0; i < e.getNumExceptions(); i++) {
                LOG.info("Failed to send put " + e.getRow(i) + ".");
            }
        }
    };
    BufferedMutatorParams params = new BufferedMutatorParams(TABLE).listener(listener);

    //
    // step 1: create a single Connection and a BufferedMutator, shared by all worker threads.
    //
    Configuration conf = new Configuration();
    try (final Connection conn = ConnectionFactory.createConnection(conf);
            final BufferedMutator mutator = conn.getBufferedMutator(params)) {

        conf.set("hbase.zookeeper.quorum", "sandbox.hortonworks.com");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("zookeeper.znode.parent", "/hbase-unsecure");

        //        conf.set("hbase.zookeeper.quorum", "jetmaster2.jetnetname.artem.com,jetslave5.jetnetname.artem.com,jetslave1.jetnetname.artem.com");
        //        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //        conf.set("zookeeper.znode.parent", "/hbase-unsecure");

        /** worker pool that operates on BufferedTable instances */
        final ExecutorService workerPool = Executors.newFixedThreadPool(POOL_SIZE);
        List<Future<Void>> futures = new ArrayList<>(TASK_COUNT);

        for (int i = 0; i < TASK_COUNT; i++) {
            futures.add(workerPool.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    //
                    // step 2: each worker sends edits to the shared BufferedMutator instance. They all use
                    // the same backing buffer, call-back "listener", and RPC executor pool.
                    //
                    Put p = new Put(Bytes.toBytes("someRow"));
                    p.addColumn(FAMILY, Bytes.toBytes("someQualifier"), Bytes.toBytes("some value"));
                    mutator.mutate(p);
                    // do work... maybe you want to call mutator.flush() after many edits to ensure any of
                    // this worker's edits are sent before exiting the Callable
                    return null;
                }
            }));
        }

        //
        // step 3: clean up the worker pool, shut down.
        //
        for (Future<Void> f : futures) {
            f.get(5, TimeUnit.MINUTES);
        }
        workerPool.shutdown();
    } catch (IOException e) {
        // exception while creating/destroying Connection or BufferedMutator
        LOG.info("exception while creating/destroying Connection or BufferedMutator", e);
    } // BufferedMutator.close() ensures all work is flushed. Could be the custom listener is
      // invoked from here.
    return 0;
}

From source file:org.schedoscope.metascope.util.HTMLUtil.java

public String getDuration(long start, long end) {
    if (end == 0 || start == 0) {
        return NO_DATE_AVAILABLE;
    }/*from w  w w  .  ja v  a  2s.  c om*/
    long millis = end - start;
    return String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
            TimeUnit.MILLISECONDS.toMinutes(millis)
                    - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
            TimeUnit.MILLISECONDS.toSeconds(millis)
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
}

From source file:eu.tango.energymodeller.energypredictor.AveragePowerEnergyPredictor.java

/**
 * This creates a new average power energy predictor. The predictor when
 * running takes the last power reading and makes the assumption no change
 * will occur. An observation time window is used for taking the measurement,
 * which is set via a configuration file.
 * @param config The config to use in order to create the abstract energy
 * predictor./*from   w w  w  .  j  ava2 s  .  co  m*/
 */
public AveragePowerEnergyPredictor(PropertiesConfiguration config) {
    super(config);
    powerObservationTimeMin = config.getInt("energy.modeller.energy.predictor.cpu.utilisation.observe_time.min",
            powerObservationTimeMin);
    config.setProperty("energy.modeller.energy.predictor.cpu.utilisation.observe_time.min",
            powerObservationTimeMin);
    powerObservationTimeSec = config.getInt("energy.modeller.energy.predictor.cpu.utilisation.observe_time.sec",
            powerObservationTimeSec);
    config.setProperty("energy.modeller.energy.predictor.cpu.utilisation.observe_time.sec",
            powerObservationTimeSec);
    observationTime = powerObservationTimeSec + (int) TimeUnit.MINUTES.toSeconds(powerObservationTimeMin);
}

From source file:nl.esciencecenter.osmium.job.XenonManagerTest.java

@Test
public void testStop() throws Exception {
    // Use powermock mockito to mock XenonFactory.endXenon()
    PowerMockito.mockStatic(XenonFactory.class);
    Xenon xenon = mock(Xenon.class);
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    JobsPoller poller = mock(JobsPoller.class);
    Scheduler scheduler = mock(Scheduler.class);
    XenonManager manager = new XenonManager(null, xenon, scheduler, null, null, poller, executor);

    manager.stop();//from w w w . j  av a 2  s .  c  o m

    verify(executor).shutdown();
    verify(executor).awaitTermination(1, TimeUnit.MINUTES);
    verify(poller).stop();
    PowerMockito.verifyStatic();
    XenonFactory.endXenon(xenon);
}

From source file:com.kixeye.janus.loadbalancer.ZoneAwareLoadBalancer.java

/**
 * Constructor//from  ww w.java 2 s.  c om
 *
 * @param serviceName        the service cluster name
 * @param myAvailabilityZone the availability zone of the server running the load balancer
 * @param metricRegistry     the registry for collected metrics
 */
public ZoneAwareLoadBalancer(String serviceName, String myAvailabilityZone, MetricRegistry metricRegistry) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(serviceName), "'serviceName' cannot be null or empty.");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(myAvailabilityZone),
            "'myAvailabilityZone' cannot be null or empty.");

    this.serviceName = serviceName;
    this.myLocation = new Location(myAvailabilityZone);
    this.locations = CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES)
            .build(new CacheLoader<ServerStats, Location>() {
                @Override
                public Location load(ServerStats key) throws Exception {
                    return getLocationFromServer(key);
                }
            });

    this.propMaxRequestsPerSecond = DynamicPropertyFactory.getInstance()
            .getDoubleProperty("janus.serviceName." + serviceName + ".maxRequestsPerSecond", 100);
    this.propEscapeAreaThreshold = DynamicPropertyFactory.getInstance()
            .getDoubleProperty("janus.serviceName." + serviceName + ".escapeAreaThreshold", 0.9);
    this.propEscapeRegionThreshold = DynamicPropertyFactory.getInstance()
            .getDoubleProperty("janus.serviceName." + serviceName + ".escapeRegionThreshold", 0.9);
    this.propEscapeAvailabilityThreshold = DynamicPropertyFactory.getInstance()
            .getDoubleProperty("janus.serviceName." + serviceName + ".escapeAvailabilityThreshold", 0.9);

    this.metricRegistry = metricRegistry;
}