private long measure() throws InterruptedException, ExecutionException { System.out.print("\nCounting words... "); final Map<String, Long> counts = new ConcurrentHashMap<>(); final Job job = jet.newJob(buildDag(counts)); long start = System.nanoTime(); job.join(); final long took = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); System.out.print("done in " + took + " milliseconds."); // printResults(counts); return took; }
/** * <p>Run a Jet analysis job to examine the journal of updates to * HTTP sessions. As we don't want it to run for ever, give it a * second or two to do some work then end it. * </p> */ @ShellMethod(key = "ANALYSIS", value = "Analyse the orders") public void analyseSessions() throws Exception { IMap<String, Integer> sequenceMap = this.hazelcastInstance.getMap(Constants.IMAP_NAME_SEQUENCE); sequenceMap.clear(); Job analysisJob = this.jetInstance.newJob(SequenceAnalysis.build()); analysisJob.getFuture(); // Give the job time to do something int wait = 2; log.info("Sleep {} seconds", wait); TimeUnit.SECONDS.sleep(wait); if (analysisJob.getStatus() == JobStatus.RUNNING) { analysisJob.cancel(); } else { log.error("Job status {}", analysisJob.getStatus()); } System.out.printf("%d sequence%s found%n", sequenceMap.size(), (sequenceMap.size() == 1 ? "" : "s")); }
private static String formatJob(Job job) { return "id=" + idToString(job.getId()) + ", name=" + job.getName() + ", submissionTime=" + toLocalDateTime(job.getSubmissionTime()); }
System.out.println("Tracked job: " + trackedJob1.getName() + ", status: " + trackedJob1.getStatus()); trackedJob1.cancel(); trackedJob1.join(); assert false; } catch (CancellationException e) { System.out.println("Status: " + trackedJob1.getStatus()); System.out.println("Tracked job status: " + trackedJob2.getStatus());
private void run() throws Exception { JetConfig cfg = new JetConfig(); cfg.setInstanceConfig(new InstanceConfig().setCooperativeThreadCount( Math.max(1, getRuntime().availableProcessors() / 2))); try { createKafkaCluster(); fillTopics(); JetInstance instance = Jet.newJetInstance(cfg); Jet.newJetInstance(cfg); IMapJet<String, Integer> sinkMap = instance.getMap(SINK_NAME); Pipeline p = buildPipeline(); long start = System.nanoTime(); Job job = instance.newJob(p); while (true) { int mapSize = sinkMap.size(); System.out.format("Received %d entries in %d milliseconds.%n", mapSize, NANOSECONDS.toMillis(System.nanoTime() - start)); if (mapSize == MESSAGE_COUNT_PER_TOPIC * 2) { job.cancel(); break; } Thread.sleep(100); } } finally { Jet.shutdownAll(); shutdownKafkaCluster(); } }
@Test public void test_listJobs() { // Given Job job = newJob(); // When run("jobs"); // Then String actual = captureOut(); assertContains(actual, job.getName()); assertContains(actual, job.getIdString()); assertContains(actual, job.getStatus().toString()); }
@Command( description = "Resumes a suspended job" ) public void resume( @Parameters(index = "0", paramLabel = "<job name or id>", description = "Name of the job to resume") String name ) throws IOException { runWithJet(jet -> { Job job = getJob(jet, name); if (job.getStatus() != JobStatus.SUSPENDED) { throw new RuntimeException("Job '" + name + "' is not suspended. Current state: " + job.getStatus()); } println("Resuming job " + formatJob(job) + "..."); job.resume(); waitForJobStatus(job, JobStatus.RUNNING); println("Job was successfully resumed."); }); }
@Test public void test_saveSnapshot_jobNotActive() { // Given Job job = newJob(); assertJobStatusEventually(job, JobStatus.RUNNING); job.cancel(); assertJobStatusEventually(job, JobStatus.FAILED); // When // Then exception.expectMessage("is not active"); run("save-snapshot", job.getIdString(), "my-snapshot"); }
@Test public void test_cancelJob_byJobName() { // Given Job job = newJob(); // When run("cancel", job.getName()); // Then assertJobStatusEventually(job, JobStatus.FAILED); }
@Test public void test_resumeJob_byJobId() { // Given Job job = newJob(); assertJobStatusEventually(job, JobStatus.RUNNING); job.suspend(); // When run("resume", job.getIdString()); // Then assertJobStatusEventually(job, JobStatus.RUNNING); }
@Test public void test_saveSnapshot_byJobId() { // Given Job job = newJob(); assertJobStatusEventually(job, JobStatus.RUNNING); // When run("save-snapshot", job.getIdString(), "my-snapshot"); // Then JobStateSnapshot ss = jet.getJobStateSnapshot("my-snapshot"); assertNotNull("no snapshot was found", ss); assertEquals(job.getName(), ss.jobName()); }
@Command( description = "Suspends a running job", mixinStandardHelpOptions = true ) public void suspend( @Parameters(index = "0", paramLabel = "<job name or id>", description = "Name of the job to suspend" ) String name ) throws IOException { runWithJet(jet -> { Job job = getJob(jet, name); assertJobRunning(name, job); printf("Suspending job %s...%n", formatJob(job)); job.suspend(); waitForJobStatus(job, JobStatus.SUSPENDED); println("Job was successfully suspended."); }); }
private static void waitForJobStatus(Job job, JobStatus status) { while (job.getStatus() != status) { LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(WAIT_INTERVAL_MILLIS)); } }