From f326a0e63e940176099e0a77ab78c919b63aeb53 Mon Sep 17 00:00:00 2001 From: Laurent Cohen Date: Wed, 2 May 2018 08:06:31 +0200 Subject: [PATCH] implementing issue JPPF-438 - refactored org.jppf.client.Operator: extracted interface ComparisonOperator, depracated the and made new class in org.jppf.utils that implements the same interface, refactored the code using the old Operator class --- application-template/.gitignore | 3 +- .../template/TemplateApplicationRunner.java | 466 +++++------ .../client/AbstractClientConnectionPool.java | 4 +- .../org/jppf/client/JMXConnectionPool.java | 4 +- .../src/java/org/jppf/client/JPPFClient.java | 6 +- .../org/jppf/client/JPPFConnectionPool.java | 22 +- common/src/java/org/jppf/client/Operator.java | 147 ++-- .../jppf/jmx/EnvironmentProviderHandler.java | 143 ++-- .../src/java/org/jppf/jmx/JMXEnvHelper.java | 1 + common/src/java/org/jppf/jmx/JMXHelper.java | 1 + .../org/jppf/node/policy/NodesMatching.java | 269 ++++--- .../org/jppf/node/policy/PolicyBuilder.java | 747 +++++++++--------- .../jppf/node/protocol/JPPFTaskBundle.java | 16 +- .../org/jppf/node/protocol/TaskBundle.java | 336 ++++---- .../org/jppf/utils/ComparisonOperator.java | 35 + .../java/org/jppf/utils/JPPFIdentifiers.java | 218 ++--- common/src/java/org/jppf/utils/Operator.java | 68 ++ .../jppf/utils/concurrent/JPPFThreadPool.java | 1 - .../utils/concurrent/JPPFThreadPool2.java | 1 - .../utils/concurrent/SynchronizedInteger.java | 10 +- .../utils/concurrent/SynchronizedLong.java | 8 +- .../java/sample/dist/matrix/MatrixRunner.java | 398 +++++----- .../sample/test/deadlock/DeadlockRunner.java | 448 +++++------ .../test/jppfcallable/JPPFCallableRunner.java | 408 +++++----- .../driver/restart/TestDriverRestart.java | 364 ++++----- demo/src/java/test/jmx/TestJMX.java | 320 ++++---- .../test/jmx/canceljob/CancelJobRunner.java | 232 +++--- .../localexecution/LocalExecutionRunner.java | 369 ++++----- .../provisioning/NodeProvisioningRunner.java | 316 ++++---- .../jppf/jmxremote/nio/JMXTransitionTask.java | 237 +++--- .../adaptivegrid/DriverConnectionManager.java | 320 ++++---- .../concurrentjobs/ConcurrentJobs.java | 520 ++++++------ .../datadependency/AbstractTradeUpdater.java | 658 +++++++-------- .../jppf/example/ftp/runner/FTPRunner.java | 188 ++--- .../dependencies/JobDependenciesRunner.java | 218 ++--- .../org/jppf/example/jobrecovery/Runner.java | 282 +++---- .../nodelifecycle/client/DBRunner.java | 332 ++++---- .../framework/org/jppf/test/setup/Setup.java | 471 +++++------ .../test/setup/AbstractNonStandardSetup.java | 9 +- .../test/org/jppf/test/setup/BaseSetup.java | 1 + .../test/setup/common/BaseTestHelper.java | 1 + .../org/jppf/client/TestConnectionPool.java | 3 +- .../test/org/jppf/client/TestOperator.java | 180 ++--- .../jppf/discovery/TestDriverDiscovery.java | 457 +++++------ .../AbstractJobPersistenceTest.java | 1 + ...ractClientLoadBalancerPersistenceTest.java | 1 + ...MuliServerLoadBalancerPersistenceTest.java | 1 + .../org/jppf/node/policy/TestGridPolicy.java | 457 +++++------ .../node/policy/TestPreferencePolicy.java | 407 +++++----- .../TestDriverJobManagementMBean.java | 1 + .../org/jppf/server/peer/TestMultiServer.java | 307 +++---- .../peer/TestMultiServerWithOrphan.java | 181 ++--- .../jppf/server/protocol/TestJPPFJobSLA.java | 1 + .../src/tests/test/org/jppf/ssl/TestSSL.java | 211 ++--- 54 files changed, 5467 insertions(+), 5339 deletions(-) create mode 100644 common/src/java/org/jppf/utils/ComparisonOperator.java create mode 100644 common/src/java/org/jppf/utils/Operator.java diff --git a/application-template/.gitignore b/application-template/.gitignore index 840e7d3120..04596f3b00 100644 --- a/application-template/.gitignore +++ b/application-template/.gitignore @@ -1 +1,2 @@ -/classes/ +/classes/ +/.settings/ diff --git a/application-template/src/org/jppf/application/template/TemplateApplicationRunner.java b/application-template/src/org/jppf/application/template/TemplateApplicationRunner.java index 26eb327df3..6dc0b4f7f0 100644 --- a/application-template/src/org/jppf/application/template/TemplateApplicationRunner.java +++ b/application-template/src/org/jppf/application/template/TemplateApplicationRunner.java @@ -1,231 +1,235 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jppf.application.template; - -import java.util.*; - -import org.jppf.client.*; -import org.jppf.node.protocol.Task; - -/** - * This is a template JPPF application runner. - * It is fully commented and is designed to be used as a starting point - * to write an application using JPPF. - * @author Laurent Cohen - */ -public class TemplateApplicationRunner { - - /** - * The entry point for this application runner to be run from a Java command line. - * @param args by default, we do not use the command line arguments, - * however nothing prevents us from using them if need be. - */ - public static void main(final String...args) { - - // create the JPPFClient. This constructor call causes JPPF to read the configuration file - // and connect with one or multiple JPPF drivers. - try (final JPPFClient jppfClient = new JPPFClient()) { - - // create a runner instance. - final TemplateApplicationRunner runner = new TemplateApplicationRunner(); - - // create and execute a blocking job - runner.executeBlockingJob(jppfClient); - - // create and execute a non-blocking job - //runner.executeNonBlockingJob(jppfClient); - - // create and execute 3 jobs concurrently - //runner.executeMultipleConcurrentJobs(jppfClient, 3); - - } catch(final Exception e) { - e.printStackTrace(); - } - } - - /** - * Create a JPPF job that can be submitted for execution. - * @param jobName an arbitrary, human-readable name given to the job. - * @return an instance of the {@link org.jppf.client.JPPFJob JPPFJob} class. - * @throws Exception if an error occurs while creating the job or adding tasks. - */ - public JPPFJob createJob(final String jobName) throws Exception { - // create a JPPF job - final JPPFJob job = new JPPFJob(); - // give this job a readable name that we can use to monitor and manage it. - job.setName(jobName); - - // add a task to the job. - final Task task = job.add(new TemplateJPPFTask()); - // provide a user-defined name for the task - task.setId(jobName + " - Template task"); - - // add more tasks here ... - - // there is no guarantee on the order of execution of the tasks, - // however the results are guaranteed to be returned in the same order as the tasks. - return job; - } - - /** - * Execute a job in blocking mode. The application will be blocked until the job execution is complete. - * @param jppfClient the {@link JPPFClient} instance which submits the job for execution. - * @throws Exception if an error occurs while executing the job. - */ - public void executeBlockingJob(final JPPFClient jppfClient) throws Exception { - // Create a job - final JPPFJob job = createJob("Template blocking job"); - - // set the job in blocking mode. - job.setBlocking(true); - - // Submit the job and wait until the results are returned. - // The results are returned as a list of Task instances, - // in the same order as the one in which the tasks where initially added to the job. - final List> results = jppfClient.submitJob(job); - - // process the results - processExecutionResults(job.getName(), results); - } - - /** - * Execute a job in non-blocking mode. The application has the responsibility - * for handling the notification of job completion and collecting the results. - * @param jppfClient the {@link JPPFClient} instance which submits the job for execution. - * @throws Exception if an error occurs while executing the job. - */ - public void executeNonBlockingJob(final JPPFClient jppfClient) throws Exception { - // Create a job - final JPPFJob job = createJob("Template non-blocking job"); - - // set the job in non-blocking (or asynchronous) mode. - job.setBlocking(false); - - // Submit the job. This call returns immediately without waiting for the execution of - // the job to complete. As a consequence, the object returned for a non-blocking job is - // always null. Note that we are calling the exact same method as in the blocking case. - jppfClient.submitJob(job); - - // the non-blocking job execution is asynchronous, we can do anything else in the meantime - System.out.println("Doing something while the job is executing ..."); - // ... - - // We are now ready to get the results of the job execution. - // We use JPPFJob.awaitResults() for this. This method returns immediately with - // the results if the job has completed, otherwise it waits until the job execution is complete. - final List> results = job.awaitResults(); - - // process the results - processExecutionResults(job.getName(), results); - } - - /** - * Execute multiple jobs in parallel from the same JPPFClient. - *

This is an extension of the {@code executeNonBlockingJob()} method, with one additional step: - * to ensure that a sufficient number of connections to the server are present, so that jobs can be submitted concurrently. - * The number of connections determines the number of jobs that can be submitted in parallel. - * It can be set in the JPPF configuration or dynamically with the {@link JPPFConnectionPool} API. - *

As a result, the call to {@code executeNonBlockingJob(jppfClient)} is effectively - * equivalent to {@code executeMultipleConccurentJobs(jppfClient, 1)}. - *

There are many patterns that can be applied to parallel job execution, you are encouraged to read - * the dedicated section - * of the JPPF documentation for details and code samples. - * @param jppfClient the JPPF client which submits the jobs. - * @param numberOfJobs the number of jobs to execute. - * @throws Exception if any error occurs. - */ - public void executeMultipleConcurrentJobs(final JPPFClient jppfClient, final int numberOfJobs) throws Exception { - // ensure that the client connection pool has as many connections - // as the number of jobs to execute - ensureNumberOfConnections(jppfClient, numberOfJobs); - - // this list will hold all the jobs submitted for execution, - // so we can later collect and process their results - final List jobList = new ArrayList<>(numberOfJobs); - - // create and submit all the jobs - for (int i=1; i<=numberOfJobs; i++) { - // create a job with a distinct name - final JPPFJob job = createJob("Template concurrent job " + i); - - // set the job in non-blocking (or asynchronous) mode. - job.setBlocking(false); - - // submit the job for execution, without blocking the current thread - jppfClient.submitJob(job); - - // add this job to the list - jobList.add(job); - } - - // the non-blocking jobs are submitted asynchronously, we can do anything else in the meantime - System.out.println("Doing something while the jobs are executing ..."); - // ... - - // wait until the jobs are finished and process their results. - for (final JPPFJob job: jobList) { - // wait if necessary for the job to complete and collect its results - final List> results = job.awaitResults(); - - // process the job results - processExecutionResults(job.getName(), results); - } - } - - /** - * Ensure that the JPPF client has the desired number of connections. - * @param jppfClient the JPPF client which submits the jobs. - * @param numberOfConnections the desired number of connections. - * @throws Exception if any error occurs. - */ - public void ensureNumberOfConnections(final JPPFClient jppfClient, final int numberOfConnections) throws Exception { - // wait until the client has at least one connection pool with at least one avaialable connection - final JPPFConnectionPool pool = jppfClient.awaitActiveConnectionPool(); - - // if the pool doesn't have the expected number of connections, change its size - if (pool.getConnections().size() != numberOfConnections) { - // set the pool size to the desired number of connections - pool.setSize(numberOfConnections); - } - - // wait until all desired connections are available (ACTIVE status) - pool.awaitActiveConnections(Operator.AT_LEAST, numberOfConnections); - } - - /** - * Process the execution results of each submitted task. - * @param jobName the name of the job whose results are processed. - * @param results the tasks results after execution on the grid. - */ - public synchronized void processExecutionResults(final String jobName, final List> results) { - // print a results header - System.out.printf("Results for job '%s' :\n", jobName); - // process the results - for (final Task task: results) { - final String taskName = task.getId(); - // if the task execution resulted in an exception - if (task.getThrowable() != null) { - // process the exception here ... - System.out.println(taskName + ", an exception was raised: " + task.getThrowable ().getMessage()); - } else { - // process the result here ... - System.out.println(taskName + ", execution result: " + task.getResult()); - } - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jppf.application.template; + +import java.util.ArrayList; +import java.util.List; + +import org.jppf.client.JPPFClient; +import org.jppf.client.JPPFConnectionPool; +import org.jppf.client.JPPFJob; +import org.jppf.node.protocol.Task; +import org.jppf.utils.Operator; + +/** + * This is a template JPPF application runner. + * It is fully commented and is designed to be used as a starting point + * to write an application using JPPF. + * @author Laurent Cohen + */ +public class TemplateApplicationRunner { + + /** + * The entry point for this application runner to be run from a Java command line. + * @param args by default, we do not use the command line arguments, + * however nothing prevents us from using them if need be. + */ + public static void main(final String...args) { + + // create the JPPFClient. This constructor call causes JPPF to read the configuration file + // and connect with one or multiple JPPF drivers. + try (final JPPFClient jppfClient = new JPPFClient()) { + + // create a runner instance. + final TemplateApplicationRunner runner = new TemplateApplicationRunner(); + + // create and execute a blocking job + runner.executeBlockingJob(jppfClient); + + // create and execute a non-blocking job + //runner.executeNonBlockingJob(jppfClient); + + // create and execute 3 jobs concurrently + //runner.executeMultipleConcurrentJobs(jppfClient, 3); + + } catch(final Exception e) { + e.printStackTrace(); + } + } + + /** + * Create a JPPF job that can be submitted for execution. + * @param jobName an arbitrary, human-readable name given to the job. + * @return an instance of the {@link org.jppf.client.JPPFJob JPPFJob} class. + * @throws Exception if an error occurs while creating the job or adding tasks. + */ + public JPPFJob createJob(final String jobName) throws Exception { + // create a JPPF job + final JPPFJob job = new JPPFJob(); + // give this job a readable name that we can use to monitor and manage it. + job.setName(jobName); + + // add a task to the job. + final Task task = job.add(new TemplateJPPFTask()); + // provide a user-defined name for the task + task.setId(jobName + " - Template task"); + + // add more tasks here ... + + // there is no guarantee on the order of execution of the tasks, + // however the results are guaranteed to be returned in the same order as the tasks. + return job; + } + + /** + * Execute a job in blocking mode. The application will be blocked until the job execution is complete. + * @param jppfClient the {@link JPPFClient} instance which submits the job for execution. + * @throws Exception if an error occurs while executing the job. + */ + public void executeBlockingJob(final JPPFClient jppfClient) throws Exception { + // Create a job + final JPPFJob job = createJob("Template blocking job"); + + // set the job in blocking mode. + job.setBlocking(true); + + // Submit the job and wait until the results are returned. + // The results are returned as a list of Task instances, + // in the same order as the one in which the tasks where initially added to the job. + final List> results = jppfClient.submitJob(job); + + // process the results + processExecutionResults(job.getName(), results); + } + + /** + * Execute a job in non-blocking mode. The application has the responsibility + * for handling the notification of job completion and collecting the results. + * @param jppfClient the {@link JPPFClient} instance which submits the job for execution. + * @throws Exception if an error occurs while executing the job. + */ + public void executeNonBlockingJob(final JPPFClient jppfClient) throws Exception { + // Create a job + final JPPFJob job = createJob("Template non-blocking job"); + + // set the job in non-blocking (or asynchronous) mode. + job.setBlocking(false); + + // Submit the job. This call returns immediately without waiting for the execution of + // the job to complete. As a consequence, the object returned for a non-blocking job is + // always null. Note that we are calling the exact same method as in the blocking case. + jppfClient.submitJob(job); + + // the non-blocking job execution is asynchronous, we can do anything else in the meantime + System.out.println("Doing something while the job is executing ..."); + // ... + + // We are now ready to get the results of the job execution. + // We use JPPFJob.awaitResults() for this. This method returns immediately with + // the results if the job has completed, otherwise it waits until the job execution is complete. + final List> results = job.awaitResults(); + + // process the results + processExecutionResults(job.getName(), results); + } + + /** + * Execute multiple jobs in parallel from the same JPPFClient. + *

This is an extension of the {@code executeNonBlockingJob()} method, with one additional step: + * to ensure that a sufficient number of connections to the server are present, so that jobs can be submitted concurrently. + * The number of connections determines the number of jobs that can be submitted in parallel. + * It can be set in the JPPF configuration or dynamically with the {@link JPPFConnectionPool} API. + *

As a result, the call to {@code executeNonBlockingJob(jppfClient)} is effectively + * equivalent to {@code executeMultipleConccurentJobs(jppfClient, 1)}. + *

There are many patterns that can be applied to parallel job execution, you are encouraged to read + * the dedicated section + * of the JPPF documentation for details and code samples. + * @param jppfClient the JPPF client which submits the jobs. + * @param numberOfJobs the number of jobs to execute. + * @throws Exception if any error occurs. + */ + public void executeMultipleConcurrentJobs(final JPPFClient jppfClient, final int numberOfJobs) throws Exception { + // ensure that the client connection pool has as many connections + // as the number of jobs to execute + ensureNumberOfConnections(jppfClient, numberOfJobs); + + // this list will hold all the jobs submitted for execution, + // so we can later collect and process their results + final List jobList = new ArrayList<>(numberOfJobs); + + // create and submit all the jobs + for (int i=1; i<=numberOfJobs; i++) { + // create a job with a distinct name + final JPPFJob job = createJob("Template concurrent job " + i); + + // set the job in non-blocking (or asynchronous) mode. + job.setBlocking(false); + + // submit the job for execution, without blocking the current thread + jppfClient.submitJob(job); + + // add this job to the list + jobList.add(job); + } + + // the non-blocking jobs are submitted asynchronously, we can do anything else in the meantime + System.out.println("Doing something while the jobs are executing ..."); + // ... + + // wait until the jobs are finished and process their results. + for (final JPPFJob job: jobList) { + // wait if necessary for the job to complete and collect its results + final List> results = job.awaitResults(); + + // process the job results + processExecutionResults(job.getName(), results); + } + } + + /** + * Ensure that the JPPF client has the desired number of connections. + * @param jppfClient the JPPF client which submits the jobs. + * @param numberOfConnections the desired number of connections. + * @throws Exception if any error occurs. + */ + public void ensureNumberOfConnections(final JPPFClient jppfClient, final int numberOfConnections) throws Exception { + // wait until the client has at least one connection pool with at least one avaialable connection + final JPPFConnectionPool pool = jppfClient.awaitActiveConnectionPool(); + + // if the pool doesn't have the expected number of connections, change its size + if (pool.getConnections().size() != numberOfConnections) { + // set the pool size to the desired number of connections + pool.setSize(numberOfConnections); + } + + // wait until all desired connections are available (ACTIVE status) + pool.awaitActiveConnections(Operator.AT_LEAST, numberOfConnections); + } + + /** + * Process the execution results of each submitted task. + * @param jobName the name of the job whose results are processed. + * @param results the tasks results after execution on the grid. + */ + public synchronized void processExecutionResults(final String jobName, final List> results) { + // print a results header + System.out.printf("Results for job '%s' :\n", jobName); + // process the results + for (final Task task: results) { + final String taskName = task.getId(); + // if the task execution resulted in an exception + if (task.getThrowable() != null) { + // process the exception here ... + System.out.println(taskName + ", an exception was raised: " + task.getThrowable ().getMessage()); + } else { + // process the result here ... + System.out.println(taskName + ", execution result: " + task.getResult()); + } + } + } +} diff --git a/client/src/java/org/jppf/client/AbstractClientConnectionPool.java b/client/src/java/org/jppf/client/AbstractClientConnectionPool.java index c3b9d4c826..a8bfd8c99a 100644 --- a/client/src/java/org/jppf/client/AbstractClientConnectionPool.java +++ b/client/src/java/org/jppf/client/AbstractClientConnectionPool.java @@ -264,8 +264,8 @@ boolean connectionHasStatus(final JPPFClientConnection connection, final boolean * @return a list of {@link JPPFClientConnection} instances, possibly less than the requested number if the timeout expired first. * @since 5.0 */ - public List awaitConnections(final Operator operator, final int nbConnections, final long timeout, final JPPFClientConnectionStatus...statuses) { - final Operator op = operator == null ? Operator.EQUAL : operator; + public List awaitConnections(final ComparisonOperator operator, final int nbConnections, final long timeout, final JPPFClientConnectionStatus...statuses) { + final ComparisonOperator op = operator == null ? org.jppf.utils.Operator.EQUAL : operator; if (debugEnabled) log.debug(String.format("awaiting %d connections with operator=%s and status in %s", nbConnections, op, Arrays.asList(statuses))); final MutableReference> ref = new MutableReference<>(); ConcurrentUtils.awaitCondition(new ConcurrentUtils.Condition() { diff --git a/client/src/java/org/jppf/client/JMXConnectionPool.java b/client/src/java/org/jppf/client/JMXConnectionPool.java index 702287cc51..d69c9f1714 100644 --- a/client/src/java/org/jppf/client/JMXConnectionPool.java +++ b/client/src/java/org/jppf/client/JMXConnectionPool.java @@ -174,8 +174,8 @@ private void initializeConnections() { * @return a list of {@link JMXDriverConnectionWrapper} instances, possibly less than the requested number if the timeout expired first. * @since 5.0 */ - List awaitJMXConnections(final Operator operator, final int nbConnections, final long timeout, final boolean connected) { - final Operator op = operator == null ? Operator.EQUAL : operator; + List awaitJMXConnections(final ComparisonOperator operator, final int nbConnections, final long timeout, final boolean connected) { + final ComparisonOperator op = operator == null ? org.jppf.utils.Operator.EQUAL : operator; //setSize(nbConnections); final MutableReference> ref = new MutableReference<>(); ConcurrentUtils.awaitCondition(new ConcurrentUtils.Condition() { diff --git a/client/src/java/org/jppf/client/JPPFClient.java b/client/src/java/org/jppf/client/JPPFClient.java index 2113b55a60..2b996fc723 100644 --- a/client/src/java/org/jppf/client/JPPFClient.java +++ b/client/src/java/org/jppf/client/JPPFClient.java @@ -273,8 +273,8 @@ public List awaitConnectionPools(final long timeout, final J * @return a list of {@link JPPFConnectionPool} instances, possibly empty but never null. * @since 5.0 */ - public List awaitConnectionPools(final Operator operator, final int expectedConnections, final long timeout, final JPPFClientConnectionStatus...statuses) { - return awaitConnectionPools(Operator.AT_LEAST, 1, operator, expectedConnections, timeout, statuses); + public List awaitConnectionPools(final ComparisonOperator operator, final int expectedConnections, final long timeout, final JPPFClientConnectionStatus...statuses) { + return awaitConnectionPools(org.jppf.utils.Operator.AT_LEAST, 1, operator, expectedConnections, timeout, statuses); } /** @@ -295,7 +295,7 @@ public List awaitConnectionPools(final Operator operator, fi * @return a list of {@link JPPFConnectionPool} instances, possibly empty but never null. * @since 6.0 */ - public List awaitConnectionPools(final Operator poolOperator, final int expectedPools, final Operator connectionOperator, final int expectedConnections, + public List awaitConnectionPools(final ComparisonOperator poolOperator, final int expectedPools, final ComparisonOperator connectionOperator, final int expectedConnections, final long timeout, final JPPFClientConnectionStatus...statuses) { final MutableReference> ref = new MutableReference<>(); ConcurrentUtils.awaitCondition(new ConcurrentUtils.Condition() { diff --git a/client/src/java/org/jppf/client/JPPFConnectionPool.java b/client/src/java/org/jppf/client/JPPFConnectionPool.java index 4a8cd67eed..787d7e477e 100644 --- a/client/src/java/org/jppf/client/JPPFConnectionPool.java +++ b/client/src/java/org/jppf/client/JPPFConnectionPool.java @@ -22,7 +22,7 @@ import org.jppf.discovery.ClientConnectionPoolInfo; import org.jppf.management.JMXDriverConnectionWrapper; -import org.jppf.utils.LoggingUtils; +import org.jppf.utils.*; import org.slf4j.*; /** @@ -104,7 +104,7 @@ public int setSize(final int newSize) { * @return a list of {@code nbConnections} {@link JPPFClientConnection} instances with the desired status. * @since 5.0 */ - public List awaitActiveConnections(final Operator operator, final int nbConnections) { + public List awaitActiveConnections(final ComparisonOperator operator, final int nbConnections) { return awaitConnections(operator, nbConnections, Long.MAX_VALUE, JPPFClientConnectionStatus.ACTIVE); } @@ -115,7 +115,7 @@ public List awaitActiveConnections(final Operator operator * @since 5.1 */ public JPPFClientConnection awaitActiveConnection() { - return awaitActiveConnections(Operator.AT_LEAST, 1).get(0); + return awaitActiveConnections(org.jppf.utils.Operator.AT_LEAST, 1).get(0); } /** @@ -126,7 +126,7 @@ public JPPFClientConnection awaitActiveConnection() { * @return a list of {@code nbConnections} {@link JPPFClientConnection} instances with the desired status. * @since 5.0 */ - public List awaitWorkingConnections(final Operator operator, final int nbConnections) { + public List awaitWorkingConnections(final ComparisonOperator operator, final int nbConnections) { return awaitConnections(operator, nbConnections, Long.MAX_VALUE, JPPFClientConnectionStatus.ACTIVE, JPPFClientConnectionStatus.EXECUTING); } @@ -137,7 +137,7 @@ public List awaitWorkingConnections(final Operator operato * @since 5.0 */ public JPPFClientConnection awaitWorkingConnection() { - return awaitWorkingConnections(Operator.AT_LEAST, 1).get(0); + return awaitWorkingConnections(org.jppf.utils.Operator.AT_LEAST, 1).get(0); } /** @@ -149,7 +149,7 @@ public JPPFClientConnection awaitWorkingConnection() { * @return a list of {@code nbConnections} {@link JPPFClientConnection} instances. * @since 5.0 */ - public List awaitConnections(final Operator operator, final int nbConnections, final JPPFClientConnectionStatus...statuses) { + public List awaitConnections(final ComparisonOperator operator, final int nbConnections, final JPPFClientConnectionStatus...statuses) { return awaitConnections(operator, nbConnections, Long.MAX_VALUE, statuses); } @@ -161,7 +161,7 @@ public List awaitConnections(final Operator operator, fina * @since 5.1 */ public JPPFClientConnection awaitConnection(final JPPFClientConnectionStatus...statuses) { - return awaitConnections(Operator.AT_LEAST, 1, Long.MAX_VALUE, statuses).get(0); + return awaitConnections(org.jppf.utils.Operator.AT_LEAST, 1, Long.MAX_VALUE, statuses).get(0); } /** @@ -173,7 +173,7 @@ public JPPFClientConnection awaitConnection(final JPPFClientConnectionStatus...s * @return a list of at least {@code nbConnections} {@link JMXDriverConnectionWrapper} instances. * @since 5.0 */ - public List awaitJMXConnections(final Operator operator, final int nbConnections, final boolean connectedOnly) { + public List awaitJMXConnections(final ComparisonOperator operator, final int nbConnections, final boolean connectedOnly) { return jmxPool.awaitJMXConnections(operator, nbConnections, Long.MAX_VALUE, connectedOnly); } @@ -186,7 +186,7 @@ public List awaitJMXConnections(final Operator opera * @return a list of {@link JMXDriverConnectionWrapper} instances, possibly less than the requested number if the timeout expired first. * @since 5.0 */ - public List awaitJMXConnections(final Operator operator, final int nbConnections, final long timeout, final boolean connectedOnly) { + public List awaitJMXConnections(final ComparisonOperator operator, final int nbConnections, final long timeout, final boolean connectedOnly) { return jmxPool.awaitJMXConnections(operator, nbConnections, timeout, connectedOnly); } @@ -197,7 +197,7 @@ public List awaitJMXConnections(final Operator opera * @since 5.1 */ public JMXDriverConnectionWrapper awaitJMXConnection(final boolean connectedOnly) { - return awaitJMXConnections(Operator.AT_LEAST, 1, connectedOnly).get(0); + return awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, connectedOnly).get(0); } /** @@ -206,6 +206,6 @@ public JMXDriverConnectionWrapper awaitJMXConnection(final boolean connectedOnly * @since 5.1 */ public JMXDriverConnectionWrapper awaitWorkingJMXConnection() { - return awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); + return awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); } } diff --git a/common/src/java/org/jppf/client/Operator.java b/common/src/java/org/jppf/client/Operator.java index 2053e201ab..3cea7eee48 100644 --- a/common/src/java/org/jppf/client/Operator.java +++ b/common/src/java/org/jppf/client/Operator.java @@ -1,76 +1,71 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.client; - -/** - * Enumeration of the possible boolean operators that can be used as a comparison predicates for integer/long values. - * @author Laurent Cohen - */ -public enum Operator { - /** - * The number of connections is equal to the expected number. - */ - EQUAL { - @Override - public boolean evaluate(final long actual, final long expected) { return actual == expected; } - }, - /** - * The number of connections is different from the expected number. - */ - NOT_EQUAL { - @Override - public boolean evaluate(final long actual, final long expected) { return actual != expected; } - }, - /** - * The number of connections is at least the expected number. - */ - AT_LEAST { - @Override - public boolean evaluate(final long actual, final long expected) { return actual >= expected; } - }, - /** - * The number of connections is at most the expected number. - */ - AT_MOST { - @Override - public boolean evaluate(final long actual, final long expected) { return actual <= expected; } - }, - /** - * The number of connections is strictly greater than the expected number. - */ - MORE_THAN { - @Override - public boolean evaluate(final long actual, final long expected) { return actual > expected; } - }, - /** - * The number of connections is strictly less than the expected number. - */ - LESS_THAN { - @Override - public boolean evaluate(final long actual, final long expected) { return actual < expected; } - }; - - /** - * Evaluate the condition based on the actual and expected number of connections. - * @param actual the actual number of connections. - * @param expected the expected number of connections - * @return true if the condition is matched, false otherwise. - */ - public abstract boolean evaluate(long actual, long expected); -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.client; + +import org.jppf.utils.ComparisonOperator; + +/** + * Enumeration of the possible boolean operators that can be used as a comparison predicates for integer/long values. + * @author Laurent Cohen + * @deprecated use {@link org.jppf.utils.Operator} instead. + */ +public enum Operator implements ComparisonOperator { + /** + * The number of connections is equal to the expected number. + */ + EQUAL { + @Override + public boolean evaluate(final long actual, final long expected) { return actual == expected; } + }, + /** + * The number of connections is different from the expected number. + */ + NOT_EQUAL { + @Override + public boolean evaluate(final long actual, final long expected) { return actual != expected; } + }, + /** + * The number of connections is at least the expected number. + */ + AT_LEAST { + @Override + public boolean evaluate(final long actual, final long expected) { return actual >= expected; } + }, + /** + * The number of connections is at most the expected number. + */ + AT_MOST { + @Override + public boolean evaluate(final long actual, final long expected) { return actual <= expected; } + }, + /** + * The number of connections is strictly greater than the expected number. + */ + MORE_THAN { + @Override + public boolean evaluate(final long actual, final long expected) { return actual > expected; } + }, + /** + * The number of connections is strictly less than the expected number. + */ + LESS_THAN { + @Override + public boolean evaluate(final long actual, final long expected) { return actual < expected; } + }; +} diff --git a/common/src/java/org/jppf/jmx/EnvironmentProviderHandler.java b/common/src/java/org/jppf/jmx/EnvironmentProviderHandler.java index 7a65df6e9c..4e70ed507a 100644 --- a/common/src/java/org/jppf/jmx/EnvironmentProviderHandler.java +++ b/common/src/java/org/jppf/jmx/EnvironmentProviderHandler.java @@ -1,71 +1,72 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.jmx; - -import java.util.*; - -/** - * Instances of this class load and provide access to environment proivders. - * @param the type of the providers to load. - * @author Laurent Cohen - */ -public class EnvironmentProviderHandler { - /** - * The client providers. - */ - private final List providers; - - /** - * Initialize this handler witht he specified provider class. - * @param clazz the class of the providers to laod. - */ - public EnvironmentProviderHandler(final Class clazz) { - providers = Collections.unmodifiableList(loadProviders(clazz)); - } - - /** - * Load the providers of specified type via SPI. - * @param clazz the class of the providers to load. - * @return a list of the loaded providers, possibly empty. - */ - private List loadProviders(final Class clazz) { - final List list = new ArrayList<>(); - final ServiceLoader sl = ServiceLoader.load(clazz); - final Iterator it = sl.iterator(); - boolean end = false; - while (!end) { - // hasNext() and next() may throw a ServiceCOnfigurationError - try { - if (it.hasNext()) list.add(it.next()); - else end = true; - } catch(@SuppressWarnings("unused") final Exception|ServiceConfigurationError e) { - end = true; - } - } - return list; - } - - /** - * Get the client providers. - * @return a list of privder instances. - */ - public List getProviders() { - return providers; - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.jmx; + +import java.util.*; + +/** + * Instances of this class load and provide access to environment proivders. + * @param the type of the providers to load. + * @author Laurent Cohen + * @exclude + */ +public class EnvironmentProviderHandler { + /** + * The client providers. + */ + private final List providers; + + /** + * Initialize this handler witht he specified provider class. + * @param clazz the class of the providers to laod. + */ + public EnvironmentProviderHandler(final Class clazz) { + providers = Collections.unmodifiableList(loadProviders(clazz)); + } + + /** + * Load the providers of specified type via SPI. + * @param clazz the class of the providers to load. + * @return a list of the loaded providers, possibly empty. + */ + private List loadProviders(final Class clazz) { + final List list = new ArrayList<>(); + final ServiceLoader sl = ServiceLoader.load(clazz); + final Iterator it = sl.iterator(); + boolean end = false; + while (!end) { + // hasNext() and next() may throw a ServiceCOnfigurationError + try { + if (it.hasNext()) list.add(it.next()); + else end = true; + } catch(@SuppressWarnings("unused") final Exception|ServiceConfigurationError e) { + end = true; + } + } + return list; + } + + /** + * Get the client providers. + * @return a list of privder instances. + */ + public List getProviders() { + return providers; + } +} diff --git a/common/src/java/org/jppf/jmx/JMXEnvHelper.java b/common/src/java/org/jppf/jmx/JMXEnvHelper.java index fe1edc609f..c98a2dfbd8 100644 --- a/common/src/java/org/jppf/jmx/JMXEnvHelper.java +++ b/common/src/java/org/jppf/jmx/JMXEnvHelper.java @@ -26,6 +26,7 @@ /** * * @author Laurent Cohen + * @exclude */ public class JMXEnvHelper { /** diff --git a/common/src/java/org/jppf/jmx/JMXHelper.java b/common/src/java/org/jppf/jmx/JMXHelper.java index 11850867ea..dd3b6cce0b 100644 --- a/common/src/java/org/jppf/jmx/JMXHelper.java +++ b/common/src/java/org/jppf/jmx/JMXHelper.java @@ -25,6 +25,7 @@ /** * * @author Laurent Cohen + * @exclude */ public class JMXHelper { /** diff --git a/common/src/java/org/jppf/node/policy/NodesMatching.java b/common/src/java/org/jppf/node/policy/NodesMatching.java index 9c3ebe6ff0..d23c976c57 100644 --- a/common/src/java/org/jppf/node/policy/NodesMatching.java +++ b/common/src/java/org/jppf/node/policy/NodesMatching.java @@ -1,135 +1,134 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.node.policy; - -import org.jppf.JPPFRuntimeException; -import org.jppf.client.Operator; -import org.jppf.management.*; -import org.jppf.utils.PropertiesCollection; - -/** - * An execution policy rule that checks whether a specified number of nodes match a node execution policy. - * The number of nodes is expressed as a condtion of type actualNbNodes comp expectedNbNodes, where comp - * is a {@link Operator comparison operator} among ==, !=, <, >, <=, >=. - *

Instances of this class can only apply to, and be used in, a server. - *

As an example, the condition "if there are more than 4 nodes idle and with at least 2 processors" can be expressed as follows: - *

- * ExecutionPolicy nodePolicy = new Equal("jppf.node.idle", true).and(new AtLeast("availableProcessors", 2));
- * ExecutionPolicy globalPolicy = new NodesMatching(Operator.GREATER, 4, nodePolicy);
- * 
- *

Alternatively, it can also be written as an XML document: - *

- * <NodesMatching operator="GREATER" expected="4">
- *   <AND>
- *     <Equal valueType="boolean">
- *       <Property>jppf.node.idle</Property>
- *       <Value>true</Value>
- *     </Equal>
- *     <AtLeast>
- *       <Property>availableProcessors</Property>
- *       <Value>2</Value>
- *     </AtLeast>
- *   </AND>
- * </NodesMatching>
- * 
- * @author Laurent Cohen - * @since 5.2 - */ -public class NodesMatching extends ExecutionPolicy { - /** - * Explicit serialVersionUID. - */ - private static final long serialVersionUID = 1L; - /** - * Name of the corresponding XML element. - */ - public static final String XML_TAG = NodesMatching.class.getSimpleName(); - /** - * The comparison operator to use for the number of nodes. - */ - private final Operator operator; - /** - * The expected number of nodes to use in the comparison. - */ - private final Expression expectedNodes; - /** - * The execution policy to match the nodes against. - */ - private final ExecutionPolicy nodePolicy; - - /** - * Initialize this execution policy. - * @param operator the comparison operator to use for the number of nodes. - * @param expectedNodes the expected number of nodes to use in the comparison. - * @param nodePolicy the execution policy to match the nodes against. If {@code null}, then all the nodes will match. - */ - public NodesMatching(final Operator operator, final long expectedNodes, final ExecutionPolicy nodePolicy) { - this.operator = operator == null ? Operator.EQUAL : operator; - this.expectedNodes = new NumericExpression((double) expectedNodes); - this.nodePolicy = nodePolicy; - } - - /** - * Initialize this execution policy. - * @param operator the comparison operator to use for the number of nodes. - * @param expectedNodes an expression that evaluates to the number of expected nodes. - * @param nodePolicy the execution policy to match the nodes against. If {@code null}, then all the nodes will match. - */ - public NodesMatching(final Operator operator, final String expectedNodes, final ExecutionPolicy nodePolicy) { - this.operator = operator == null ? Operator.EQUAL : operator; - this.expectedNodes = new NumericExpression(expectedNodes); - this.nodePolicy = nodePolicy; - } - - @Override - public boolean accepts(final PropertiesCollection info) { - int nbNodes = 0; - try (JMXDriverConnectionWrapper jmx = new JMXDriverConnectionWrapper()) { - jmx.connect(); - nbNodes = jmx.nbNodes((nodePolicy == null) ? NodeSelector.ALL_NODES : new ExecutionPolicySelector(nodePolicy)); - return operator.evaluate(nbNodes, expectedNodes.evaluate(info).longValue()); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new JPPFRuntimeException("error evaluating global policy", e); - } - } - - @Override - public String toString(final int n) { - final StringBuilder sb = new StringBuilder(); - sb.append(indent(n)).append('<').append(XML_TAG); - sb.append(" operator=\"").append(operator.name()).append("\" expected=\"").append(expectedNodes.getExpression().replace("\"", """)).append("\">\n"); - if (nodePolicy != null) sb.append(nodePolicy.toString(n + 1)); - sb.append(indent(n)).append("\n"); - return sb.toString(); - } - - @Override - void initializeRoot() { - super.initializeRoot(); - if (nodePolicy != null) nodePolicy.setContext(context); - } - - @Override - void initializeRoot(final ExecutionPolicy root) { - super.initializeRoot(root); - if (nodePolicy != null) nodePolicy.setContext(getContext()); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.node.policy; + +import org.jppf.JPPFRuntimeException; +import org.jppf.management.*; +import org.jppf.utils.*; + +/** + * An execution policy rule that checks whether a specified number of nodes match a node execution policy. + * The number of nodes is expressed as a condtion of type actualNbNodes comp expectedNbNodes, where comp + * is a {@link Operator comparison operator} among ==, !=, <, >, <=, >=. + *

Instances of this class can only apply to, and be used in, a server. + *

As an example, the condition "if there are more than 4 nodes idle and with at least 2 processors" can be expressed as follows: + *

+ * ExecutionPolicy nodePolicy = new Equal("jppf.node.idle", true).and(new AtLeast("availableProcessors", 2));
+ * ExecutionPolicy globalPolicy = new NodesMatching(Operator.GREATER, 4, nodePolicy);
+ * 
+ *

Alternatively, it can also be written as an XML document: + *

+ * <NodesMatching operator="GREATER" expected="4">
+ *   <AND>
+ *     <Equal valueType="boolean">
+ *       <Property>jppf.node.idle</Property>
+ *       <Value>true</Value>
+ *     </Equal>
+ *     <AtLeast>
+ *       <Property>availableProcessors</Property>
+ *       <Value>2</Value>
+ *     </AtLeast>
+ *   </AND>
+ * </NodesMatching>
+ * 
+ * @author Laurent Cohen + * @since 5.2 + */ +public class NodesMatching extends ExecutionPolicy { + /** + * Explicit serialVersionUID. + */ + private static final long serialVersionUID = 1L; + /** + * Name of the corresponding XML element. + */ + public static final String XML_TAG = NodesMatching.class.getSimpleName(); + /** + * The comparison operator to use for the number of nodes. + */ + private final Operator operator; + /** + * The expected number of nodes to use in the comparison. + */ + private final Expression expectedNodes; + /** + * The execution policy to match the nodes against. + */ + private final ExecutionPolicy nodePolicy; + + /** + * Initialize this execution policy. + * @param operator the comparison operator to use for the number of nodes. + * @param expectedNodes the expected number of nodes to use in the comparison. + * @param nodePolicy the execution policy to match the nodes against. If {@code null}, then all the nodes will match. + */ + public NodesMatching(final Operator operator, final long expectedNodes, final ExecutionPolicy nodePolicy) { + this.operator = operator == null ? Operator.EQUAL : operator; + this.expectedNodes = new NumericExpression((double) expectedNodes); + this.nodePolicy = nodePolicy; + } + + /** + * Initialize this execution policy. + * @param operator the comparison operator to use for the number of nodes. + * @param expectedNodes an expression that evaluates to the number of expected nodes. + * @param nodePolicy the execution policy to match the nodes against. If {@code null}, then all the nodes will match. + */ + public NodesMatching(final Operator operator, final String expectedNodes, final ExecutionPolicy nodePolicy) { + this.operator = operator == null ? Operator.EQUAL : operator; + this.expectedNodes = new NumericExpression(expectedNodes); + this.nodePolicy = nodePolicy; + } + + @Override + public boolean accepts(final PropertiesCollection info) { + int nbNodes = 0; + try (JMXDriverConnectionWrapper jmx = new JMXDriverConnectionWrapper()) { + jmx.connect(); + nbNodes = jmx.nbNodes((nodePolicy == null) ? NodeSelector.ALL_NODES : new ExecutionPolicySelector(nodePolicy)); + return operator.evaluate(nbNodes, expectedNodes.evaluate(info).longValue()); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new JPPFRuntimeException("error evaluating global policy", e); + } + } + + @Override + public String toString(final int n) { + final StringBuilder sb = new StringBuilder(); + sb.append(indent(n)).append('<').append(XML_TAG); + sb.append(" operator=\"").append(operator.name()).append("\" expected=\"").append(expectedNodes.getExpression().replace("\"", """)).append("\">\n"); + if (nodePolicy != null) sb.append(nodePolicy.toString(n + 1)); + sb.append(indent(n)).append("\n"); + return sb.toString(); + } + + @Override + void initializeRoot() { + super.initializeRoot(); + if (nodePolicy != null) nodePolicy.setContext(context); + } + + @Override + void initializeRoot(final ExecutionPolicy root) { + super.initializeRoot(root); + if (nodePolicy != null) nodePolicy.setContext(getContext()); + } +} diff --git a/common/src/java/org/jppf/node/policy/PolicyBuilder.java b/common/src/java/org/jppf/node/policy/PolicyBuilder.java index 15c7177e1e..36c4eb1797 100644 --- a/common/src/java/org/jppf/node/policy/PolicyBuilder.java +++ b/common/src/java/org/jppf/node/policy/PolicyBuilder.java @@ -1,373 +1,374 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.node.policy; - -import java.util.*; - -import org.jppf.client.Operator; - -/** - * Instances of this class build an execution policy graph, based on a policy - * descriptor parsed from an XML document. - * @author Laurent Cohen - */ -class PolicyBuilder { - /** - * Build an execution policy from a parsed policy descriptor. - * @param desc the descriptor parsed from an XML document. - * @return an ExecutionPolicy instance. - * @throws Exception if an error occurs while generating a policy object. - */ - public ExecutionPolicy buildPolicy(final PolicyDescriptor desc) throws Exception { - switch(desc.type) { - case "NOT": return buildNotPolicy(desc); - case "AND": return buildAndPolicy(desc); - case "OR": return buildOrPolicy(desc); - case "XOR": return buildXorPolicy(desc); - case "LessThan": return buildLessThanPolicy(desc); - case "MoreThan": return buildMoreThanPolicy(desc); - case "AtMost": return buildAtMostPolicy(desc); - case "AtLeast": return buildAtLeastPolicy(desc); - case "BetweenII": return buildBetweenIIPolicy(desc); - case "BetweenIE": return buildBetweenIEPolicy(desc); - case "BetweenEI": return buildBetweenEIPolicy(desc); - case "BetweenEE": return buildBetweenEEPolicy(desc); - case "Equal": return buildEqualPolicy(desc); - case "Contains": return buildContainsPolicy(desc); - case "OneOf": return buildOneOfPolicy(desc); - case "RegExp": return buildRegExpPolicy(desc); - case "CustomRule": return buildCustomPolicy(desc); - case "Script": return buildScriptedPolicy(desc); - case "Preference": return buildPreferencePolicy(desc); - case "IsInIPv4Subnet": - case "IsInIPv6Subnet": return buildIsinIPSubnetPolicy(desc); - case "NodesMatching": return buildNodesMatchingPolicy(desc); - } - return null; - } - - /** - * Build a NOT policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - * @throws Exception if an error occurs while generating the policy object. - */ - private ExecutionPolicy buildNotPolicy(final PolicyDescriptor desc) throws Exception { - return new ExecutionPolicy.NotRule(buildPolicy(desc.children.get(0))); - } - - /** - * Build an AND policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - * @throws Exception if an error occurs while generating the policy object. - */ - private ExecutionPolicy buildAndPolicy(final PolicyDescriptor desc) throws Exception { - final ExecutionPolicy[] rules = new ExecutionPolicy[desc.children.size()]; - int count = 0; - for (final PolicyDescriptor child: desc.children) rules[count++] = buildPolicy(child); - return new ExecutionPolicy.AndRule(rules); - } - - /** - * Build an OR policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - * @throws Exception if an error occurs while generating the policy object. - */ - private ExecutionPolicy buildOrPolicy(final PolicyDescriptor desc) throws Exception { - final ExecutionPolicy[] rules = new ExecutionPolicy[desc.children.size()]; - int count = 0; - for (final PolicyDescriptor child: desc.children) rules[count++] = buildPolicy(child); - return new ExecutionPolicy.OrRule(rules); - } - - /** - * Build an XOR policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - * @throws Exception if an error occurs while generating the policy object. - */ - private ExecutionPolicy buildXorPolicy(final PolicyDescriptor desc) throws Exception { - final ExecutionPolicy[] rules = new ExecutionPolicy[desc.children.size()]; - int count = 0; - for (final PolicyDescriptor child: desc.children) rules[count++] = buildPolicy(child); - return new ExecutionPolicy.XorRule(rules); - } - - /** - * Build a LessThan policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private static ExecutionPolicy buildLessThanPolicy(final PolicyDescriptor desc) { - final String s = desc.operands.get(1); - try { - return new LessThan(desc.operands.get(0), Double.valueOf(s)); - } catch(@SuppressWarnings("unused") final NumberFormatException e) { - return new LessThan(desc.operands.get(0), s); - } - } - - /** - * Build an AtMost policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private static ExecutionPolicy buildAtMostPolicy(final PolicyDescriptor desc) { - final String s = desc.operands.get(1); - try { - return new AtMost(desc.operands.get(0), Double.valueOf(s)); - } catch(@SuppressWarnings("unused") final NumberFormatException e) { - return new AtMost(desc.operands.get(0), s); - } - } - - /** - * Build a MoreThan policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private static ExecutionPolicy buildMoreThanPolicy(final PolicyDescriptor desc) { - final String s = desc.operands.get(1); - try { - return new MoreThan(desc.operands.get(0), Double.valueOf(s)); - } catch(@SuppressWarnings("unused") final NumberFormatException e) { - return new MoreThan(desc.operands.get(0), s); - } - } - - /** - * Build an AtLeast policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private static ExecutionPolicy buildAtLeastPolicy(final PolicyDescriptor desc) { - final String s = desc.operands.get(1); - try { - return new AtLeast(desc.operands.get(0), Double.valueOf(s)); - } catch(@SuppressWarnings("unused") final NumberFormatException e) { - return new AtLeast(desc.operands.get(0), s); - } - } - - /** - * Build a BetweenII policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private ExecutionPolicy buildBetweenIIPolicy(final PolicyDescriptor desc) { - final BetweenArgs args = new BetweenArgs(desc); - if ((args.value1 != null) && (args.value2 != null)) return new BetweenII(args.prop, args.value1, args.value2); - else if ((args.value1 == null) && (args.value2 != null)) return new BetweenII(args.prop, args.expr1, args.value2); - else if ((args.value1 != null) && (args.value2 == null)) return new BetweenII(args.prop, args.value1, args.expr2); - return new BetweenII(args.prop, args.expr1, args.expr2); - } - - /** - * Build a BetweenIE policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private ExecutionPolicy buildBetweenIEPolicy(final PolicyDescriptor desc) { - final BetweenArgs args = new BetweenArgs(desc); - if ((args.value1 != null) && (args.value2 != null)) return new BetweenIE(args.prop, args.value1, args.value2); - else if ((args.value1 == null) && (args.value2 != null)) return new BetweenIE(args.prop, args.expr1, args.value2); - else if ((args.value1 != null) && (args.value2 == null)) return new BetweenIE(args.prop, args.value1, args.expr2); - return new BetweenIE(args.prop, args.expr1, args.expr2); - } - - /** - * Build a BetweenEI policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private ExecutionPolicy buildBetweenEIPolicy(final PolicyDescriptor desc) { - final BetweenArgs args = new BetweenArgs(desc); - if ((args.value1 != null) && (args.value2 != null)) return new BetweenEI(args.prop, args.value1, args.value2); - else if ((args.value1 == null) && (args.value2 != null)) return new BetweenEI(args.prop, args.expr1, args.value2); - else if ((args.value1 != null) && (args.value2 == null)) return new BetweenEI(args.prop, args.value1, args.expr2); - return new BetweenEI(args.prop, args.expr1, args.expr2); - } - - /** - * Build a BetweenEE policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private ExecutionPolicy buildBetweenEEPolicy(final PolicyDescriptor desc) { - final BetweenArgs args = new BetweenArgs(desc); - if ((args.value1 != null) && (args.value2 != null)) return new BetweenEE(args.prop, args.value1, args.value2); - else if ((args.value1 == null) && (args.value2 != null)) return new BetweenEE(args.prop, args.expr1, args.value2); - else if ((args.value1 != null) && (args.value2 == null)) return new BetweenEE(args.prop, args.value1, args.expr2); - return new BetweenEE(args.prop, args.expr1, args.expr2); - } - - /** - * Build a Equal policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private static ExecutionPolicy buildEqualPolicy(final PolicyDescriptor desc) { - final String s = desc.operands.get(1); - if ("string".equals(desc.valueType)) { - final boolean ignoreCase = (desc.ignoreCase == null) ? false : Boolean.valueOf(desc.ignoreCase); - return new Equal(desc.operands.get(0), ignoreCase, s); - } - if ("numeric".equals(desc.valueType)) { - double value = 0.0d; - try { - value = Double.valueOf(s); - return new Equal(desc.operands.get(0), value); - } catch(@SuppressWarnings("unused") final NumberFormatException e) { - return new Equal(ValueType.NUMERIC, desc.operands.get(0), s); - } - } - return new Equal(ValueType.BOOLEAN, desc.operands.get(0), s); - } - - /** - * Build a Contains policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private static ExecutionPolicy buildContainsPolicy(final PolicyDescriptor desc) { - final boolean ignoreCase = (desc.ignoreCase == null) ? false : Boolean.valueOf(desc.ignoreCase); - return new Contains(desc.operands.get(0), ignoreCase, desc.operands.get(1)); - } - - /** - * Build a OneOf policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private static ExecutionPolicy buildOneOfPolicy(final PolicyDescriptor desc) { - if ("numeric".equals(desc.valueType)) { - final int size = desc.operands.size() - 1; - final List values = new ArrayList<>(size); - for (int i=1; iExecutionPolicy instance. - */ - private static ExecutionPolicy buildRegExpPolicy(final PolicyDescriptor desc) { - return new RegExp(desc.operands.get(0), desc.operands.get(1)); - } - - /** - * Build a custom policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - * @throws Exception if an error occurs while generating the custom policy object. - */ - private static ExecutionPolicy buildCustomPolicy(final PolicyDescriptor desc) throws Exception { - final Class clazz = Class.forName(desc.className); - final CustomPolicy policy = (CustomPolicy) clazz.newInstance(); - policy.setArgs(desc.arguments.toArray(new String[desc.arguments.size()])); - policy.initialize(); - return policy; - } - - /** - * Build a scripted policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private static ExecutionPolicy buildScriptedPolicy(final PolicyDescriptor desc) { - return new ScriptedPolicy(desc.language, desc.script); - } - - /** - * Build an AND policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - * @throws Exception if an error occurs while generating the policy object. - */ - private ExecutionPolicy buildPreferencePolicy(final PolicyDescriptor desc) throws Exception { - final ExecutionPolicy[] rules = new ExecutionPolicy[desc.children.size()]; - int count = 0; - for (final PolicyDescriptor child: desc.children) rules[count++] = buildPolicy(child); - return new Preference(rules); - } - - /** - * Build a OneOf policy from a descriptor. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - */ - private static ExecutionPolicy buildIsinIPSubnetPolicy(final PolicyDescriptor desc) { - return "IsInIPv4Subnet".equals(desc.type) ? new IsInIPv4Subnet(desc.operands) : new IsInIPv6Subnet(desc.operands); - } - - /** - * Build a global policy. - * @param desc the descriptor to use. - * @return an ExecutionPolicy instance. - * @throws Exception if an error occurs while generating the policy object. - * @since 5.2 - */ - private ExecutionPolicy buildNodesMatchingPolicy(final PolicyDescriptor desc) throws Exception { - final ExecutionPolicy child = (desc.children != null) && (desc.children.size() > 0) ? buildPolicy(desc.children.get(0)) : null; - final Operator operator = Operator.valueOf(desc.operator.toUpperCase()); - try { - final long expected = Long.valueOf(desc.expected); - return new NodesMatching(operator, expected, child); - } catch(@SuppressWarnings("unused") final NumberFormatException e) { - return new NodesMatching(operator, desc.expected, child); - } - } - - /** */ - class BetweenArgs { - /** */ - String prop, expr1, expr2; - /** */ - Double value1, value2; - - /** - * @param desc the descriptor to use. - */ - BetweenArgs(final PolicyDescriptor desc) { - prop = desc.operands.get(0); - String s = desc.operands.get(1); - try { - value1 = Double.valueOf(s); - } catch(@SuppressWarnings("unused") final NumberFormatException e) { - expr1 = s; - } - s = desc.operands.get(2); - try { - value2 = Double.valueOf(s); - } catch(@SuppressWarnings("unused") final NumberFormatException e) { - expr2 = s; - } - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.node.policy; + +import java.util.*; + +import org.jppf.utils.Operator; + + +/** + * Instances of this class build an execution policy graph, based on a policy + * descriptor parsed from an XML document. + * @author Laurent Cohen + */ +class PolicyBuilder { + /** + * Build an execution policy from a parsed policy descriptor. + * @param desc the descriptor parsed from an XML document. + * @return an ExecutionPolicy instance. + * @throws Exception if an error occurs while generating a policy object. + */ + public ExecutionPolicy buildPolicy(final PolicyDescriptor desc) throws Exception { + switch(desc.type) { + case "NOT": return buildNotPolicy(desc); + case "AND": return buildAndPolicy(desc); + case "OR": return buildOrPolicy(desc); + case "XOR": return buildXorPolicy(desc); + case "LessThan": return buildLessThanPolicy(desc); + case "MoreThan": return buildMoreThanPolicy(desc); + case "AtMost": return buildAtMostPolicy(desc); + case "AtLeast": return buildAtLeastPolicy(desc); + case "BetweenII": return buildBetweenIIPolicy(desc); + case "BetweenIE": return buildBetweenIEPolicy(desc); + case "BetweenEI": return buildBetweenEIPolicy(desc); + case "BetweenEE": return buildBetweenEEPolicy(desc); + case "Equal": return buildEqualPolicy(desc); + case "Contains": return buildContainsPolicy(desc); + case "OneOf": return buildOneOfPolicy(desc); + case "RegExp": return buildRegExpPolicy(desc); + case "CustomRule": return buildCustomPolicy(desc); + case "Script": return buildScriptedPolicy(desc); + case "Preference": return buildPreferencePolicy(desc); + case "IsInIPv4Subnet": + case "IsInIPv6Subnet": return buildIsinIPSubnetPolicy(desc); + case "NodesMatching": return buildNodesMatchingPolicy(desc); + } + return null; + } + + /** + * Build a NOT policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + * @throws Exception if an error occurs while generating the policy object. + */ + private ExecutionPolicy buildNotPolicy(final PolicyDescriptor desc) throws Exception { + return new ExecutionPolicy.NotRule(buildPolicy(desc.children.get(0))); + } + + /** + * Build an AND policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + * @throws Exception if an error occurs while generating the policy object. + */ + private ExecutionPolicy buildAndPolicy(final PolicyDescriptor desc) throws Exception { + final ExecutionPolicy[] rules = new ExecutionPolicy[desc.children.size()]; + int count = 0; + for (final PolicyDescriptor child: desc.children) rules[count++] = buildPolicy(child); + return new ExecutionPolicy.AndRule(rules); + } + + /** + * Build an OR policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + * @throws Exception if an error occurs while generating the policy object. + */ + private ExecutionPolicy buildOrPolicy(final PolicyDescriptor desc) throws Exception { + final ExecutionPolicy[] rules = new ExecutionPolicy[desc.children.size()]; + int count = 0; + for (final PolicyDescriptor child: desc.children) rules[count++] = buildPolicy(child); + return new ExecutionPolicy.OrRule(rules); + } + + /** + * Build an XOR policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + * @throws Exception if an error occurs while generating the policy object. + */ + private ExecutionPolicy buildXorPolicy(final PolicyDescriptor desc) throws Exception { + final ExecutionPolicy[] rules = new ExecutionPolicy[desc.children.size()]; + int count = 0; + for (final PolicyDescriptor child: desc.children) rules[count++] = buildPolicy(child); + return new ExecutionPolicy.XorRule(rules); + } + + /** + * Build a LessThan policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private static ExecutionPolicy buildLessThanPolicy(final PolicyDescriptor desc) { + final String s = desc.operands.get(1); + try { + return new LessThan(desc.operands.get(0), Double.valueOf(s)); + } catch(@SuppressWarnings("unused") final NumberFormatException e) { + return new LessThan(desc.operands.get(0), s); + } + } + + /** + * Build an AtMost policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private static ExecutionPolicy buildAtMostPolicy(final PolicyDescriptor desc) { + final String s = desc.operands.get(1); + try { + return new AtMost(desc.operands.get(0), Double.valueOf(s)); + } catch(@SuppressWarnings("unused") final NumberFormatException e) { + return new AtMost(desc.operands.get(0), s); + } + } + + /** + * Build a MoreThan policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private static ExecutionPolicy buildMoreThanPolicy(final PolicyDescriptor desc) { + final String s = desc.operands.get(1); + try { + return new MoreThan(desc.operands.get(0), Double.valueOf(s)); + } catch(@SuppressWarnings("unused") final NumberFormatException e) { + return new MoreThan(desc.operands.get(0), s); + } + } + + /** + * Build an AtLeast policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private static ExecutionPolicy buildAtLeastPolicy(final PolicyDescriptor desc) { + final String s = desc.operands.get(1); + try { + return new AtLeast(desc.operands.get(0), Double.valueOf(s)); + } catch(@SuppressWarnings("unused") final NumberFormatException e) { + return new AtLeast(desc.operands.get(0), s); + } + } + + /** + * Build a BetweenII policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private ExecutionPolicy buildBetweenIIPolicy(final PolicyDescriptor desc) { + final BetweenArgs args = new BetweenArgs(desc); + if ((args.value1 != null) && (args.value2 != null)) return new BetweenII(args.prop, args.value1, args.value2); + else if ((args.value1 == null) && (args.value2 != null)) return new BetweenII(args.prop, args.expr1, args.value2); + else if ((args.value1 != null) && (args.value2 == null)) return new BetweenII(args.prop, args.value1, args.expr2); + return new BetweenII(args.prop, args.expr1, args.expr2); + } + + /** + * Build a BetweenIE policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private ExecutionPolicy buildBetweenIEPolicy(final PolicyDescriptor desc) { + final BetweenArgs args = new BetweenArgs(desc); + if ((args.value1 != null) && (args.value2 != null)) return new BetweenIE(args.prop, args.value1, args.value2); + else if ((args.value1 == null) && (args.value2 != null)) return new BetweenIE(args.prop, args.expr1, args.value2); + else if ((args.value1 != null) && (args.value2 == null)) return new BetweenIE(args.prop, args.value1, args.expr2); + return new BetweenIE(args.prop, args.expr1, args.expr2); + } + + /** + * Build a BetweenEI policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private ExecutionPolicy buildBetweenEIPolicy(final PolicyDescriptor desc) { + final BetweenArgs args = new BetweenArgs(desc); + if ((args.value1 != null) && (args.value2 != null)) return new BetweenEI(args.prop, args.value1, args.value2); + else if ((args.value1 == null) && (args.value2 != null)) return new BetweenEI(args.prop, args.expr1, args.value2); + else if ((args.value1 != null) && (args.value2 == null)) return new BetweenEI(args.prop, args.value1, args.expr2); + return new BetweenEI(args.prop, args.expr1, args.expr2); + } + + /** + * Build a BetweenEE policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private ExecutionPolicy buildBetweenEEPolicy(final PolicyDescriptor desc) { + final BetweenArgs args = new BetweenArgs(desc); + if ((args.value1 != null) && (args.value2 != null)) return new BetweenEE(args.prop, args.value1, args.value2); + else if ((args.value1 == null) && (args.value2 != null)) return new BetweenEE(args.prop, args.expr1, args.value2); + else if ((args.value1 != null) && (args.value2 == null)) return new BetweenEE(args.prop, args.value1, args.expr2); + return new BetweenEE(args.prop, args.expr1, args.expr2); + } + + /** + * Build a Equal policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private static ExecutionPolicy buildEqualPolicy(final PolicyDescriptor desc) { + final String s = desc.operands.get(1); + if ("string".equals(desc.valueType)) { + final boolean ignoreCase = (desc.ignoreCase == null) ? false : Boolean.valueOf(desc.ignoreCase); + return new Equal(desc.operands.get(0), ignoreCase, s); + } + if ("numeric".equals(desc.valueType)) { + double value = 0.0d; + try { + value = Double.valueOf(s); + return new Equal(desc.operands.get(0), value); + } catch(@SuppressWarnings("unused") final NumberFormatException e) { + return new Equal(ValueType.NUMERIC, desc.operands.get(0), s); + } + } + return new Equal(ValueType.BOOLEAN, desc.operands.get(0), s); + } + + /** + * Build a Contains policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private static ExecutionPolicy buildContainsPolicy(final PolicyDescriptor desc) { + final boolean ignoreCase = (desc.ignoreCase == null) ? false : Boolean.valueOf(desc.ignoreCase); + return new Contains(desc.operands.get(0), ignoreCase, desc.operands.get(1)); + } + + /** + * Build a OneOf policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private static ExecutionPolicy buildOneOfPolicy(final PolicyDescriptor desc) { + if ("numeric".equals(desc.valueType)) { + final int size = desc.operands.size() - 1; + final List values = new ArrayList<>(size); + for (int i=1; iExecutionPolicy instance. + */ + private static ExecutionPolicy buildRegExpPolicy(final PolicyDescriptor desc) { + return new RegExp(desc.operands.get(0), desc.operands.get(1)); + } + + /** + * Build a custom policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + * @throws Exception if an error occurs while generating the custom policy object. + */ + private static ExecutionPolicy buildCustomPolicy(final PolicyDescriptor desc) throws Exception { + final Class clazz = Class.forName(desc.className); + final CustomPolicy policy = (CustomPolicy) clazz.newInstance(); + policy.setArgs(desc.arguments.toArray(new String[desc.arguments.size()])); + policy.initialize(); + return policy; + } + + /** + * Build a scripted policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private static ExecutionPolicy buildScriptedPolicy(final PolicyDescriptor desc) { + return new ScriptedPolicy(desc.language, desc.script); + } + + /** + * Build an AND policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + * @throws Exception if an error occurs while generating the policy object. + */ + private ExecutionPolicy buildPreferencePolicy(final PolicyDescriptor desc) throws Exception { + final ExecutionPolicy[] rules = new ExecutionPolicy[desc.children.size()]; + int count = 0; + for (final PolicyDescriptor child: desc.children) rules[count++] = buildPolicy(child); + return new Preference(rules); + } + + /** + * Build a OneOf policy from a descriptor. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + */ + private static ExecutionPolicy buildIsinIPSubnetPolicy(final PolicyDescriptor desc) { + return "IsInIPv4Subnet".equals(desc.type) ? new IsInIPv4Subnet(desc.operands) : new IsInIPv6Subnet(desc.operands); + } + + /** + * Build a global policy. + * @param desc the descriptor to use. + * @return an ExecutionPolicy instance. + * @throws Exception if an error occurs while generating the policy object. + * @since 5.2 + */ + private ExecutionPolicy buildNodesMatchingPolicy(final PolicyDescriptor desc) throws Exception { + final ExecutionPolicy child = (desc.children != null) && (desc.children.size() > 0) ? buildPolicy(desc.children.get(0)) : null; + final Operator operator = Operator.valueOf(desc.operator.toUpperCase()); + try { + final long expected = Long.valueOf(desc.expected); + return new NodesMatching(operator, expected, child); + } catch(@SuppressWarnings("unused") final NumberFormatException e) { + return new NodesMatching(operator, desc.expected, child); + } + } + + /** */ + class BetweenArgs { + /** */ + String prop, expr1, expr2; + /** */ + Double value1, value2; + + /** + * @param desc the descriptor to use. + */ + BetweenArgs(final PolicyDescriptor desc) { + prop = desc.operands.get(0); + String s = desc.operands.get(1); + try { + value1 = Double.valueOf(s); + } catch(@SuppressWarnings("unused") final NumberFormatException e) { + expr1 = s; + } + s = desc.operands.get(2); + try { + value2 = Double.valueOf(s); + } catch(@SuppressWarnings("unused") final NumberFormatException e) { + expr2 = s; + } + } + } +} diff --git a/common/src/java/org/jppf/node/protocol/JPPFTaskBundle.java b/common/src/java/org/jppf/node/protocol/JPPFTaskBundle.java index ec0e3ad593..60581d634c 100644 --- a/common/src/java/org/jppf/node/protocol/JPPFTaskBundle.java +++ b/common/src/java/org/jppf/node/protocol/JPPFTaskBundle.java @@ -42,11 +42,11 @@ public class JPPFTaskBundle extends MetadataImpl implements Comparable getUuidPath(); - - /** - * Set the uuid path of the applications (driver or client) in whose classpath the class definition may be found. - * @param uuidPath the uuid path as a list of string elements. - */ - void setUuidPath(TraversalList uuidPath); - - /** - * Get the time it took a node to execute this task. - * @return the time in milliseconds as a long value. - */ - long getNodeExecutionTime(); - - /** - * Set the time it took a node to execute this task. - * @param nodeExecutionTime the time in milliseconds as a long value. - */ - void setNodeExecutionTime(long nodeExecutionTime); - - /** - * Set the number of tasks in this bundle. - * @param taskCount the number of tasks as an int. - */ - void setTaskCount(int taskCount); - - /** - * Set the initial number of tasks in this bundle. - * @param initialTaskCount the number of tasks as an int. - */ - void setInitialTaskCount(int initialTaskCount); - - /** - * Make a copy of this bundle. - * @return a new JPPFTaskBundle instance. - */ - TaskBundle copy(); - - /** - * Get the time at which the bundle is taken out of the queue for sending to a node. - * @return the time as a long value. - */ - long getExecutionStartTime(); - - /** - * Set the time at which the bundle is taken out of the queue for sending to a node. - * @param executionStartTime the time as a long value. - */ - void setExecutionStartTime(long executionStartTime); - - /** - * Get the initial task count of this bundle. - * @return the task count as an int. - */ - int getInitialTaskCount(); - - /** - * Get the service level agreement between the job and the server. - * @param jobSLA an instance of {@link JobSLA}. - */ - void setSLA(JobSLA jobSLA); - - /** - * Set the user-defined display name for the job. - * @param name the display name as a string. - */ - void setName(String name); - - /** - * Set this bundle's metadata. - * @param jobMetadata a {@link JobMetadata} instance. - */ - void setMetadata(JobMetadata jobMetadata); - - /** - * Set the uuid of the initial job. - * @param jobUuid the uuid as a string. - */ - void setUuid(String jobUuid); - - /** - * Get the current number of tasks in this bundle. - * @return the current number of tasks as an int. - */ - int getCurrentTaskCount(); - - /** - * Set the current number of tasks in this bundle. - * @param currentTaskCount the current number of tasks as an int. - */ - void setCurrentTaskCount(int currentTaskCount); - - /** - * Get the job requeue flag. - * @return job requeue flag. - */ - boolean isPending(); - - /** - * Get the job requeue flag. - * @return job requeue flag. - */ - boolean isRequeue(); - - /** - * Set the job requeue flag. - * @param requeue job requeue flag. - */ - void setRequeue(boolean requeue); - - /** - * Get the number of tasks in this bundle at the time it is received by a driver. - * @return the number of tasks as an int. - */ - int getDriverQueueTaskCount(); - - /** - * Set the number of tasks in this bundle at the time it is received by a driver. - * @param driverQueueTaskCount the number of tasks as an int. - */ - void setDriverQueueTaskCount(int driverQueueTaskCount); - - /** - * Determine whether this object is used for handshake instead of execution. - * @return true if this bundle is a handshake bundle, false otherwise. - */ - boolean isHandshake(); - - /** - * Specify whether this object is used for handshake instead of execution. - * @param handshake true if this bundle is a handshake bundle, false otherwise. - */ - void setHandshake(boolean handshake); - -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.node.protocol; + +import org.jppf.utils.TraversalList; +import org.jppf.utils.collections.Metadata; + +/** + * Interface for job headers sent ver the network. + * @author Laurent Cohen + * @exclude + */ +public interface TaskBundle extends JPPFDistributedJob, Metadata { + /** + * Get the uuid path of the applications (driver or client) in whose classpath the class definition may be found. + * @return the uuid path as a list of string elements. + */ + TraversalList getUuidPath(); + + /** + * Set the uuid path of the applications (driver or client) in whose classpath the class definition may be found. + * @param uuidPath the uuid path as a list of string elements. + */ + void setUuidPath(TraversalList uuidPath); + + /** + * Get the time it took a node to execute this task. + * @return the time in milliseconds as a long value. + */ + long getNodeExecutionTime(); + + /** + * Set the time it took a node to execute this task. + * @param nodeExecutionTime the time in milliseconds as a long value. + */ + void setNodeExecutionTime(long nodeExecutionTime); + + /** + * Set the number of tasks in this bundle. + * @param taskCount the number of tasks as an int. + */ + void setTaskCount(int taskCount); + + /** + * Set the initial number of tasks in this bundle. + * @param initialTaskCount the number of tasks as an int. + */ + void setInitialTaskCount(int initialTaskCount); + + /** + * Make a copy of this bundle. + * @return a new JPPFTaskBundle instance. + */ + TaskBundle copy(); + + /** + * Get the time at which the bundle is taken out of the queue for sending to a node. + * @return the time as a long value. + */ + long getExecutionStartTime(); + + /** + * Set the time at which the bundle is taken out of the queue for sending to a node. + * @param executionStartTime the time as a long value. + */ + void setExecutionStartTime(long executionStartTime); + + /** + * Get the initial task count of this bundle. + * @return the task count as an int. + */ + int getInitialTaskCount(); + + /** + * Get the service level agreement between the job and the server. + * @param jobSLA an instance of {@link JobSLA}. + */ + void setSLA(JobSLA jobSLA); + + /** + * Set the user-defined display name for the job. + * @param name the display name as a string. + */ + void setName(String name); + + /** + * Set this bundle's metadata. + * @param jobMetadata a {@link JobMetadata} instance. + */ + void setMetadata(JobMetadata jobMetadata); + + /** + * Set the uuid of the initial job. + * @param jobUuid the uuid as a string. + */ + void setUuid(String jobUuid); + + /** + * Get the current number of tasks in this bundle. + * @return the current number of tasks as an int. + */ + int getCurrentTaskCount(); + + /** + * Set the current number of tasks in this bundle. + * @param currentTaskCount the current number of tasks as an int. + */ + void setCurrentTaskCount(int currentTaskCount); + + /** + * Get the job requeue flag. + * @return job requeue flag. + */ + boolean isPending(); + + /** + * Get the job requeue flag. + * @return job requeue flag. + */ + boolean isRequeue(); + + /** + * Set the job requeue flag. + * @param requeue job requeue flag. + */ + void setRequeue(boolean requeue); + + /** + * Get the number of tasks in this bundle at the time it is received by a driver. + * @return the number of tasks as an int. + */ + int getDriverQueueTaskCount(); + + /** + * Set the number of tasks in this bundle at the time it is received by a driver. + * @param driverQueueTaskCount the number of tasks as an int. + */ + void setDriverQueueTaskCount(int driverQueueTaskCount); + + /** + * Determine whether this object is used for handshake instead of execution. + * @return true if this bundle is a handshake bundle, false otherwise. + */ + boolean isHandshake(); + + /** + * Specify whether this object is used for handshake instead of execution. + * @param handshake true if this bundle is a handshake bundle, false otherwise. + */ + void setHandshake(boolean handshake); +} diff --git a/common/src/java/org/jppf/utils/ComparisonOperator.java b/common/src/java/org/jppf/utils/ComparisonOperator.java new file mode 100644 index 0000000000..a4162fc91a --- /dev/null +++ b/common/src/java/org/jppf/utils/ComparisonOperator.java @@ -0,0 +1,35 @@ +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.utils; + +/** + * Interface for numerical binary comparison operators. + * @author Laurent Cohen + */ +public interface ComparisonOperator { + + /** + * Evaluate the condition based on the actual and expected numbers. + * @param actual the actual number. + * @param expected the expected numbers + * @return true if the condition is matched, false otherwise. + */ + boolean evaluate(long actual, long expected); + +} \ No newline at end of file diff --git a/common/src/java/org/jppf/utils/JPPFIdentifiers.java b/common/src/java/org/jppf/utils/JPPFIdentifiers.java index 3db1af5de7..0bb2cb6134 100644 --- a/common/src/java/org/jppf/utils/JPPFIdentifiers.java +++ b/common/src/java/org/jppf/utils/JPPFIdentifiers.java @@ -1,103 +1,115 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.utils; - -import java.util.*; - -import org.jppf.nio.NioConstants; - -/** - * Channel identifiers sent over the network as part of the handshaking with a JPPF server. - * @author Laurent Cohen - * @exclude - */ -public final class JPPFIdentifiers { - /** - * Identifier for an unidentified channel. - */ - public static final int UNKNOWN = 0; - /** - * Identifier for a JMX remote channel. - */ - public static final int JMX_REMOTE_CHANNEL = 0xFFF8; - /** - * Identifier for an acceptor channel. - */ - public static final int ACCEPTOR_CHANNEL = 0xFFF9; - /** - * Identifier for the job data channel of a client. - */ - public static final int CLIENT_JOB_DATA_CHANNEL = 0xFFFA; - /** - * Identifier for the class loader channel of a client. - */ - public static final int CLIENT_CLASSLOADER_CHANNEL = 0xFFFB; - /** - * Identifier for the job data channel of a node. - */ - public static final int NODE_JOB_DATA_CHANNEL = 0xFFFC; - /** - * Identifier for the class loader channel of a node. - */ - public static final int NODE_CLASSLOADER_CHANNEL = 0xFFFD; - /** - * Mapping of ids to readable names. - */ - private static Map idMap = new HashMap<>(); - static { - idMap.put(ACCEPTOR_CHANNEL, "ACCEPTOR_CHANNEL"); - idMap.put(CLIENT_JOB_DATA_CHANNEL, "CLIENT_JOB_DATA_CHANNEL"); - idMap.put(CLIENT_CLASSLOADER_CHANNEL, "CLIENT_CLASSLOADER_CHANNEL"); - idMap.put(NODE_JOB_DATA_CHANNEL, "NODE_JOB_DATA_CHANNEL"); - idMap.put(NODE_CLASSLOADER_CHANNEL, "NODE_CLASSLOADER_CHANNEL"); - idMap.put(JMX_REMOTE_CHANNEL, "JMX_REMOTE_CHANNEL"); - } - /** - * Mapping of ids to server names. - */ - private static Map serverMap = new HashMap<>(); - static { - serverMap.put(ACCEPTOR_CHANNEL, NioConstants.ACCEPTOR); - serverMap.put(CLIENT_JOB_DATA_CHANNEL, NioConstants.CLIENT_SERVER); - serverMap.put(CLIENT_CLASSLOADER_CHANNEL, NioConstants.CLIENT_CLASS_SERVER); - serverMap.put(NODE_JOB_DATA_CHANNEL, NioConstants.NODE_SERVER); - serverMap.put(NODE_CLASSLOADER_CHANNEL, NioConstants.NODE_CLASS_SERVER); - serverMap.put(JMX_REMOTE_CHANNEL, NioConstants.JMX_REMOTE); - } - - /** - * Get an identifier as a string. - * @param id the id to lookup. - * @return a readable string for the id. - */ - public static String asString(final int id) { - final String s = idMap.get(id); - return s == null ? "UNKNOWN" : s; - } - - /** - * Get a server name for the psecified identifier. - * @param id the id to lookup. - * @return a readable string for the server name. - */ - public static String serverName(final int id) { - final String s = serverMap.get(id); - return s == null ? "UNKNOWN" : s; - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.utils; + +import java.util.*; + +import org.jppf.nio.NioConstants; + +/** + * Channel identifiers sent over the network as part of the handshaking with a JPPF server. + * @author Laurent Cohen + * @exclude + */ +public final class JPPFIdentifiers { + /** + * Identifier for an unidentified channel. + */ + public static final int UNKNOWN = 0; + /** + * Identifier for a driver-tot-driver heartbeat connection. + */ + public static final int PEER_HEARTBEAT_CHANNEL = 0xFFF5; + /** + * Identifier for a client-driver heartbeat connection. + */ + public static final int CLIENT_HEARTBEAT_CHANNEL = 0xFFF6; + /** + * Identifier for a nodet-driver heartbeat connection. + */ + public static final int NODE_HEARTBEAT_CHANNEL = 0xFFF7; + /** + * Identifier for a JMX remote channel. + */ + public static final int JMX_REMOTE_CHANNEL = 0xFFF8; + /** + * Identifier for an acceptor channel. + */ + public static final int ACCEPTOR_CHANNEL = 0xFFF9; + /** + * Identifier for the job data channel of a client. + */ + public static final int CLIENT_JOB_DATA_CHANNEL = 0xFFFA; + /** + * Identifier for the class loader channel of a client. + */ + public static final int CLIENT_CLASSLOADER_CHANNEL = 0xFFFB; + /** + * Identifier for the job data channel of a node. + */ + public static final int NODE_JOB_DATA_CHANNEL = 0xFFFC; + /** + * Identifier for the class loader channel of a node. + */ + public static final int NODE_CLASSLOADER_CHANNEL = 0xFFFD; + /** + * Mapping of ids to readable names. + */ + private static Map idMap = new HashMap<>(); + static { + idMap.put(ACCEPTOR_CHANNEL, "ACCEPTOR_CHANNEL"); + idMap.put(CLIENT_JOB_DATA_CHANNEL, "CLIENT_JOB_DATA_CHANNEL"); + idMap.put(CLIENT_CLASSLOADER_CHANNEL, "CLIENT_CLASSLOADER_CHANNEL"); + idMap.put(NODE_JOB_DATA_CHANNEL, "NODE_JOB_DATA_CHANNEL"); + idMap.put(NODE_CLASSLOADER_CHANNEL, "NODE_CLASSLOADER_CHANNEL"); + idMap.put(JMX_REMOTE_CHANNEL, "JMX_REMOTE_CHANNEL"); + } + /** + * Mapping of ids to server names. + */ + private static Map serverMap = new HashMap<>(); + static { + serverMap.put(ACCEPTOR_CHANNEL, NioConstants.ACCEPTOR); + serverMap.put(CLIENT_JOB_DATA_CHANNEL, NioConstants.CLIENT_SERVER); + serverMap.put(CLIENT_CLASSLOADER_CHANNEL, NioConstants.CLIENT_CLASS_SERVER); + serverMap.put(NODE_JOB_DATA_CHANNEL, NioConstants.NODE_SERVER); + serverMap.put(NODE_CLASSLOADER_CHANNEL, NioConstants.NODE_CLASS_SERVER); + serverMap.put(JMX_REMOTE_CHANNEL, NioConstants.JMX_REMOTE); + } + + /** + * Get an identifier as a string. + * @param id the id to lookup. + * @return a readable string for the id. + */ + public static String asString(final int id) { + final String s = idMap.get(id); + return s == null ? "UNKNOWN" : s; + } + + /** + * Get a server name for the psecified identifier. + * @param id the id to lookup. + * @return a readable string for the server name. + */ + public static String serverName(final int id) { + final String s = serverMap.get(id); + return s == null ? "UNKNOWN" : s; + } +} diff --git a/common/src/java/org/jppf/utils/Operator.java b/common/src/java/org/jppf/utils/Operator.java new file mode 100644 index 0000000000..f081dc63c2 --- /dev/null +++ b/common/src/java/org/jppf/utils/Operator.java @@ -0,0 +1,68 @@ +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.utils; + +/** + * Enumeration of the possible boolean operators that can be used as a comparison predicates for integer/long values. + * @author Laurent Cohen + */ +public enum Operator implements ComparisonOperator { + /** + * The number of connections is equal to the expected number. + */ + EQUAL { + @Override + public boolean evaluate(final long actual, final long expected) { return actual == expected; } + }, + /** + * The number of connections is different from the expected number. + */ + NOT_EQUAL { + @Override + public boolean evaluate(final long actual, final long expected) { return actual != expected; } + }, + /** + * The number of connections is at least the expected number. + */ + AT_LEAST { + @Override + public boolean evaluate(final long actual, final long expected) { return actual >= expected; } + }, + /** + * The number of connections is at most the expected number. + */ + AT_MOST { + @Override + public boolean evaluate(final long actual, final long expected) { return actual <= expected; } + }, + /** + * The number of connections is strictly greater than the expected number. + */ + MORE_THAN { + @Override + public boolean evaluate(final long actual, final long expected) { return actual > expected; } + }, + /** + * The number of connections is strictly less than the expected number. + */ + LESS_THAN { + @Override + public boolean evaluate(final long actual, final long expected) { return actual < expected; } + }; +} diff --git a/common/src/java/org/jppf/utils/concurrent/JPPFThreadPool.java b/common/src/java/org/jppf/utils/concurrent/JPPFThreadPool.java index 4bbc45d17b..5ffcce2296 100644 --- a/common/src/java/org/jppf/utils/concurrent/JPPFThreadPool.java +++ b/common/src/java/org/jppf/utils/concurrent/JPPFThreadPool.java @@ -23,7 +23,6 @@ import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; -import org.jppf.client.Operator; import org.jppf.utils.*; import org.slf4j.Logger; diff --git a/common/src/java/org/jppf/utils/concurrent/JPPFThreadPool2.java b/common/src/java/org/jppf/utils/concurrent/JPPFThreadPool2.java index 2c9278265c..d636fd184c 100644 --- a/common/src/java/org/jppf/utils/concurrent/JPPFThreadPool2.java +++ b/common/src/java/org/jppf/utils/concurrent/JPPFThreadPool2.java @@ -22,7 +22,6 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.*; -import org.jppf.client.Operator; import org.jppf.utils.*; import org.slf4j.Logger; diff --git a/common/src/java/org/jppf/utils/concurrent/SynchronizedInteger.java b/common/src/java/org/jppf/utils/concurrent/SynchronizedInteger.java index 458b1d0640..e4fe188147 100644 --- a/common/src/java/org/jppf/utils/concurrent/SynchronizedInteger.java +++ b/common/src/java/org/jppf/utils/concurrent/SynchronizedInteger.java @@ -18,7 +18,7 @@ package org.jppf.utils.concurrent; -import org.jppf.client.Operator; +import org.jppf.utils.ComparisonOperator; /** * An integer value on which a set of operations can be performed atomically. @@ -158,7 +158,7 @@ public boolean compareAndSet(final int expected, final int update) { * @param update the new value to set to if the comparison succeeds. * @return {@code true} if the update was performed, {@code false} otherwise. */ - public boolean compareAndSet(final Operator operator, final int expected, final int update) { + public boolean compareAndSet(final ComparisonOperator operator, final int expected, final int update) { synchronized(this) { if (operator.evaluate(value, expected)) { value = update; @@ -174,7 +174,7 @@ public boolean compareAndSet(final Operator operator, final int expected, final * @param expectedUpdate the expected value. * @return {@code true} if the update was performed, {@code false} otherwise. */ - public boolean compareAndSet(final Operator operator, final int expectedUpdate) { + public boolean compareAndSet(final ComparisonOperator operator, final int expectedUpdate) { synchronized(this) { if (operator.evaluate(value, expectedUpdate)) { value = expectedUpdate; @@ -190,7 +190,7 @@ public boolean compareAndSet(final Operator operator, final int expectedUpdate) * @param expected the expected value. * @return {@code true} if the update was performed, {@code false} otherwise. */ - public boolean compareAndIncrement(final Operator operator, final int expected) { + public boolean compareAndIncrement(final ComparisonOperator operator, final int expected) { synchronized(this) { if (operator.evaluate(value, expected)) { value++; @@ -206,7 +206,7 @@ public boolean compareAndIncrement(final Operator operator, final int expected) * @param expected the expected value. * @return {@code true} if the update was performed, {@code false} otherwise. */ - public boolean compareAndDecrement(final Operator operator, final int expected) { + public boolean compareAndDecrement(final ComparisonOperator operator, final int expected) { synchronized(this) { if (operator.evaluate(value, expected)) { value--; diff --git a/common/src/java/org/jppf/utils/concurrent/SynchronizedLong.java b/common/src/java/org/jppf/utils/concurrent/SynchronizedLong.java index 1bee4ac39a..36aad95dcc 100644 --- a/common/src/java/org/jppf/utils/concurrent/SynchronizedLong.java +++ b/common/src/java/org/jppf/utils/concurrent/SynchronizedLong.java @@ -18,7 +18,7 @@ package org.jppf.utils.concurrent; -import org.jppf.client.Operator; +import org.jppf.utils.ComparisonOperator; /** * A long value on which a set of operations can be performed atomically. @@ -118,7 +118,7 @@ public synchronized boolean compareAndSet(final long expected, final long update * @param update the new value to set to if the comparison succeeds. * @return {@code true} if the update was performed, {@code false} otherwise. */ - public synchronized boolean compareAndSet(final Operator operator, final long expected, final long update) { + public synchronized boolean compareAndSet(final ComparisonOperator operator, final long expected, final long update) { if (operator.evaluate(value, expected)) { value = update; return true; @@ -132,7 +132,7 @@ public synchronized boolean compareAndSet(final Operator operator, final long ex * @param expectedUpdate the expected value. * @return {@code true} if the update was performed, {@code false} otherwise. */ - public synchronized boolean compareAndSet(final Operator operator, final long expectedUpdate) { + public synchronized boolean compareAndSet(final ComparisonOperator operator, final long expectedUpdate) { if (operator.evaluate(value, expectedUpdate)) { value = expectedUpdate; return true; @@ -147,7 +147,7 @@ public synchronized boolean compareAndSet(final Operator operator, final long ex * @param action the action to run if the comparison succeeds. * @return {@code true} if the update was performed, {@code false} otherwise. */ - public synchronized boolean compareAndRun(final Operator operator, final long expected, final Runnable action) { + public synchronized boolean compareAndRun(final ComparisonOperator operator, final long expected, final Runnable action) { if (operator.evaluate(value, expected)) { if (action != null) action.run(); return true; diff --git a/demo/src/java/sample/dist/matrix/MatrixRunner.java b/demo/src/java/sample/dist/matrix/MatrixRunner.java index e4a0e4c8cf..cedae65b7c 100644 --- a/demo/src/java/sample/dist/matrix/MatrixRunner.java +++ b/demo/src/java/sample/dist/matrix/MatrixRunner.java @@ -1,199 +1,199 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package sample.dist.matrix; - -import java.io.File; -import java.util.List; - -import org.jppf.JPPFException; -import org.jppf.client.*; -import org.jppf.location.*; -import org.jppf.node.policy.ExecutionPolicy; -import org.jppf.node.protocol.*; -import org.jppf.utils.*; -import org.jppf.utils.configuration.JPPFProperties; -import org.jppf.utils.streams.StreamUtils; -import org.slf4j.*; - -/** - * Runner class for the square matrix multiplication demo. - * @author Laurent Cohen - */ -public class MatrixRunner { - /** - * Logger for this class. - */ - static Logger log = LoggerFactory.getLogger(MatrixRunner.class); - /** - * JPPF client used to submit execution requests. - */ - private JPPFClient jppfClient = null; - /** - * Keeps track of the current iteration number. - */ - private int iterationsCount = 0; - /** - * - */ - private ClassPath classpath = null; - - /** - * Entry point for this class, performs a matrix multiplication a number of times.,
- * The number of times is specified as a configuration property named "matrix.iterations".
- * The size of the matrices is specified as a configuration property named "matrix.size".
- * @param args not used. - */ - public static void main(final String...args) { - MatrixRunner runner = null; - try { - final String clientUuid = ((args != null) && (args.length > 0)) ? args[0] : null; - final TypedProperties props = JPPFConfiguration.getProperties(); - final int size = props.getInt("matrix.size", 300); - final int iterations = props.getInt("matrix.iterations", 10); - int nbChannels = props.getInt("matrix.nbChannels", 1); - if (nbChannels < 1) nbChannels = 1; - final int nbRows = props.getInt("task.nbRows", 1); - StreamUtils.printf(log, "Running Matrix demo with matrix size = "+size+ '*'+size+" for "+iterations+" iterations" + " with " + nbChannels + " channels"); - runner = new MatrixRunner(); - runner.perform(size, iterations, nbRows, clientUuid, nbChannels); - } catch(final Exception e) { - e.printStackTrace(); - } - } - - /** - * Perform the multiplication of 2 matrices with the specified size, for a specified number of times. - * @param size the size of the matrices. - * @param iterations the number of times the multiplication will be performed. - * @param nbRows number of rows of matrix a per task. - * @param clientUuid an optional uuid to set on the JPPF client. - * @param nbChannels number of driver channels to use for each job. - * @throws Exception if an error is raised during the execution. - */ - public void perform(final int size, final int iterations, final int nbRows, final String clientUuid, final int nbChannels) throws Exception { - try { - JPPFConfiguration.set(JPPFProperties.POOL_SIZE, nbChannels); - final String s = JPPFConfiguration.getProperties().getString("matrix.classpath"); - if (s != null) { - classpath = new ClassPathImpl(); - final String[] paths = s.split("\\|"); - for (final String path: paths) { - final String p = path.trim(); - final String name = new File(p).getName(); - final Location fileLoc = new FileLocation(p); - final Location jar = fileLoc.copyTo(new MemoryLocation((int) fileLoc.size())); - classpath.add(name, jar); - } - } - if (clientUuid != null) jppfClient = new JPPFClient(clientUuid); - else jppfClient = new JPPFClient(); - final JPPFConnectionPool pool = jppfClient.awaitWorkingConnectionPool(); - pool.setSize(nbChannels); - pool.awaitWorkingConnections(Operator.AT_LEAST, nbChannels); - // initialize the 2 matrices to multiply - final Matrix a = new Matrix(size).assignRandomValues(); - final Matrix b = new Matrix(size).assignRandomValues(); - if (size <= 500) performSequentialMultiplication(a, b); - long totalIterationTime = 0L; - long min = Long.MAX_VALUE; - long max = 0L; - - // perform "iteration" times - for (int iter=0; iter max) max = elapsed; - totalIterationTime += elapsed; - StreamUtils.printf(log, "Iteration #" + (iter+1) + " performed in " + StringUtils.toStringDuration(elapsed)); - } - StreamUtils.printf(log, "Average iteration time: " + StringUtils.toStringDuration(totalIterationTime / iterations) + - ", min = " + StringUtils.toStringDuration(min) + ", max = " + StringUtils.toStringDuration(max)); - } finally { - StreamUtils.printf(log, "closing the client"); - if (jppfClient != null) jppfClient.close(); - } - } - - /** - * Perform the sequential multiplication of 2 squares matrices of equal sizes. - * @param a the left-hand matrix. - * @param b the right-hand matrix. - * @param nbRows number of rows of matrix a per task. - * @param policy the execution policy to apply to the submitted job, may be null. - * @param nbChannels number of driver channels to use for each job. - * @return the elapsed time for the computation. - * @throws Exception if an error is raised during the execution. - */ - private long performParallelMultiplication(final Matrix a, final Matrix b, final int nbRows, final ExecutionPolicy policy, final int nbChannels) throws Exception { - final long start = System.nanoTime(); - final int size = a.getSize(); - // create a task for each row in matrix a - final JPPFJob job = new JPPFJob(); - job.setName("matrix sample " + (iterationsCount++)); - job.getClientSLA().setMaxChannels(nbChannels); - int remaining = size; - for (int i=0; i= nbRows) { - rows = new double[nbRows][]; - remaining -= nbRows; - } - else rows = new double[remaining][]; - for (int j=0; j> results = jppfClient.submitJob(job); - // initialize the resulting matrix - final Matrix c = new Matrix(size); - // Get the matrix values from the tasks results - int rowIdx = 0, pos = 0; - for (final Task matrixTask : results) { - if (matrixTask.getThrowable() != null) { - StreamUtils.printf(log, "got exception: " + ExceptionUtils.getStackTrace(matrixTask.getThrowable())); - throw new JPPFException(matrixTask.getThrowable()); - } - if (pos != matrixTask.getPosition()) throw new JPPFException(String.format("pos=%d is different from task.getPosition()=%d", pos, matrixTask.getPosition())); - final double[][] rows = (double[][]) matrixTask.getResult(); - for (int j = 0; j < rows.length; j++) { - for (int k = 0; k < size; k++) c.setValueAt(rowIdx + j, k, rows[j][k]); - } - rowIdx += rows.length; - pos++; - } - return (System.nanoTime() - start) / 1_000_000L; - } - - /** - * Perform the sequential multiplication of 2 squares matrices of equal sizes. - * @param a the left-hand matrix. - * @param b the right-hand matrix. - */ - private static void performSequentialMultiplication(final Matrix a, final Matrix b) { - final long start = System.nanoTime(); - a.multiply(b); - final long elapsed = System.nanoTime() - start; - StreamUtils.printf(log, "Sequential computation performed in " + StringUtils.toStringDuration(elapsed / 1_000_000L)); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package sample.dist.matrix; + +import java.io.File; +import java.util.List; + +import org.jppf.JPPFException; +import org.jppf.client.*; +import org.jppf.location.*; +import org.jppf.node.policy.ExecutionPolicy; +import org.jppf.node.protocol.*; +import org.jppf.utils.*; +import org.jppf.utils.configuration.JPPFProperties; +import org.jppf.utils.streams.StreamUtils; +import org.slf4j.*; + +/** + * Runner class for the square matrix multiplication demo. + * @author Laurent Cohen + */ +public class MatrixRunner { + /** + * Logger for this class. + */ + static Logger log = LoggerFactory.getLogger(MatrixRunner.class); + /** + * JPPF client used to submit execution requests. + */ + private JPPFClient jppfClient = null; + /** + * Keeps track of the current iteration number. + */ + private int iterationsCount = 0; + /** + * + */ + private ClassPath classpath = null; + + /** + * Entry point for this class, performs a matrix multiplication a number of times.,
+ * The number of times is specified as a configuration property named "matrix.iterations".
+ * The size of the matrices is specified as a configuration property named "matrix.size".
+ * @param args not used. + */ + public static void main(final String...args) { + MatrixRunner runner = null; + try { + final String clientUuid = ((args != null) && (args.length > 0)) ? args[0] : null; + final TypedProperties props = JPPFConfiguration.getProperties(); + final int size = props.getInt("matrix.size", 300); + final int iterations = props.getInt("matrix.iterations", 10); + int nbChannels = props.getInt("matrix.nbChannels", 1); + if (nbChannels < 1) nbChannels = 1; + final int nbRows = props.getInt("task.nbRows", 1); + StreamUtils.printf(log, "Running Matrix demo with matrix size = "+size+ '*'+size+" for "+iterations+" iterations" + " with " + nbChannels + " channels"); + runner = new MatrixRunner(); + runner.perform(size, iterations, nbRows, clientUuid, nbChannels); + } catch(final Exception e) { + e.printStackTrace(); + } + } + + /** + * Perform the multiplication of 2 matrices with the specified size, for a specified number of times. + * @param size the size of the matrices. + * @param iterations the number of times the multiplication will be performed. + * @param nbRows number of rows of matrix a per task. + * @param clientUuid an optional uuid to set on the JPPF client. + * @param nbChannels number of driver channels to use for each job. + * @throws Exception if an error is raised during the execution. + */ + public void perform(final int size, final int iterations, final int nbRows, final String clientUuid, final int nbChannels) throws Exception { + try { + JPPFConfiguration.set(JPPFProperties.POOL_SIZE, nbChannels); + final String s = JPPFConfiguration.getProperties().getString("matrix.classpath"); + if (s != null) { + classpath = new ClassPathImpl(); + final String[] paths = s.split("\\|"); + for (final String path: paths) { + final String p = path.trim(); + final String name = new File(p).getName(); + final Location fileLoc = new FileLocation(p); + final Location jar = fileLoc.copyTo(new MemoryLocation((int) fileLoc.size())); + classpath.add(name, jar); + } + } + if (clientUuid != null) jppfClient = new JPPFClient(clientUuid); + else jppfClient = new JPPFClient(); + final JPPFConnectionPool pool = jppfClient.awaitWorkingConnectionPool(); + pool.setSize(nbChannels); + pool.awaitWorkingConnections(org.jppf.utils.Operator.AT_LEAST, nbChannels); + // initialize the 2 matrices to multiply + final Matrix a = new Matrix(size).assignRandomValues(); + final Matrix b = new Matrix(size).assignRandomValues(); + if (size <= 500) performSequentialMultiplication(a, b); + long totalIterationTime = 0L; + long min = Long.MAX_VALUE; + long max = 0L; + + // perform "iteration" times + for (int iter=0; iter max) max = elapsed; + totalIterationTime += elapsed; + StreamUtils.printf(log, "Iteration #" + (iter+1) + " performed in " + StringUtils.toStringDuration(elapsed)); + } + StreamUtils.printf(log, "Average iteration time: " + StringUtils.toStringDuration(totalIterationTime / iterations) + + ", min = " + StringUtils.toStringDuration(min) + ", max = " + StringUtils.toStringDuration(max)); + } finally { + StreamUtils.printf(log, "closing the client"); + if (jppfClient != null) jppfClient.close(); + } + } + + /** + * Perform the sequential multiplication of 2 squares matrices of equal sizes. + * @param a the left-hand matrix. + * @param b the right-hand matrix. + * @param nbRows number of rows of matrix a per task. + * @param policy the execution policy to apply to the submitted job, may be null. + * @param nbChannels number of driver channels to use for each job. + * @return the elapsed time for the computation. + * @throws Exception if an error is raised during the execution. + */ + private long performParallelMultiplication(final Matrix a, final Matrix b, final int nbRows, final ExecutionPolicy policy, final int nbChannels) throws Exception { + final long start = System.nanoTime(); + final int size = a.getSize(); + // create a task for each row in matrix a + final JPPFJob job = new JPPFJob(); + job.setName("matrix sample " + (iterationsCount++)); + job.getClientSLA().setMaxChannels(nbChannels); + int remaining = size; + for (int i=0; i= nbRows) { + rows = new double[nbRows][]; + remaining -= nbRows; + } + else rows = new double[remaining][]; + for (int j=0; j> results = jppfClient.submitJob(job); + // initialize the resulting matrix + final Matrix c = new Matrix(size); + // Get the matrix values from the tasks results + int rowIdx = 0, pos = 0; + for (final Task matrixTask : results) { + if (matrixTask.getThrowable() != null) { + StreamUtils.printf(log, "got exception: " + ExceptionUtils.getStackTrace(matrixTask.getThrowable())); + throw new JPPFException(matrixTask.getThrowable()); + } + if (pos != matrixTask.getPosition()) throw new JPPFException(String.format("pos=%d is different from task.getPosition()=%d", pos, matrixTask.getPosition())); + final double[][] rows = (double[][]) matrixTask.getResult(); + for (int j = 0; j < rows.length; j++) { + for (int k = 0; k < size; k++) c.setValueAt(rowIdx + j, k, rows[j][k]); + } + rowIdx += rows.length; + pos++; + } + return (System.nanoTime() - start) / 1_000_000L; + } + + /** + * Perform the sequential multiplication of 2 squares matrices of equal sizes. + * @param a the left-hand matrix. + * @param b the right-hand matrix. + */ + private static void performSequentialMultiplication(final Matrix a, final Matrix b) { + final long start = System.nanoTime(); + a.multiply(b); + final long elapsed = System.nanoTime() - start; + StreamUtils.printf(log, "Sequential computation performed in " + StringUtils.toStringDuration(elapsed / 1_000_000L)); + } +} diff --git a/demo/src/java/sample/test/deadlock/DeadlockRunner.java b/demo/src/java/sample/test/deadlock/DeadlockRunner.java index af8dafb438..c911d42bba 100644 --- a/demo/src/java/sample/test/deadlock/DeadlockRunner.java +++ b/demo/src/java/sample/test/deadlock/DeadlockRunner.java @@ -1,224 +1,224 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sample.test.deadlock; - -import java.util.List; - -import org.jppf.client.*; -import org.jppf.management.*; -import org.jppf.management.forwarding.JPPFNodeForwardingMBean; -import org.jppf.node.policy.Equal; -import org.jppf.node.protocol.Task; -import org.jppf.utils.*; -import org.slf4j.*; - -/** - * An illustration of the patterns for submitting multiple jobs in parallel. - */ -public class DeadlockRunner { - /** - * Logger for this class. - */ - private static Logger log = LoggerFactory.getLogger(ProvisioningThread.class); - /** - * - */ - private static JMXDriverConnectionWrapper jmx = null; - - /** - * Entry point for this demo. - * @param args the first argument is a function number that determines which method to call. - */ - public static void main(final String[] args) { - try { - //StreamUtils.waitKeyPressed("Please press [Enter]"); - final DeadlockRunner runner = new DeadlockRunner(); - runner.jobStreaming(); - //runner.testNodes(); - } catch (final Exception e) { - e.printStackTrace(); - } - } - - /** - * Execute a stream of non-blocking jobs from a single thread, process the results asynchronously. - */ - public void jobStreaming() { - final RunOptions ro = new RunOptions(); - printf("Running with conccurencyLimit=%d, nbJobs=%d, tasksPerJob=%d, taskDuration=%d", ro.concurrencyLimit, ro.nbJobs, ro.tasksPerJob, ro.taskOptions.taskDuration); - ProvisioningThread pt = null; - MasterNodeMonitoringThread mnmt = null; - try (JPPFClient client = new JPPFClient(); JobStreamImpl jobProvider = new JobStreamImpl(ro)) { - getJmxConnection(client); - ro.callback = new ScriptedJobCallback(); - //ro.callback = new SystemExitCallback(); - //ro.callback = new JobPersistenceCallback(); - ensureSufficientConnections(client, ro.clientConnections); - if (ro.slaves >= 0) updateSlaveNodes(client, ro.slaves); - if (ro.simulateNodeCrashes) { - pt = new ProvisioningThread(client, ro.waitTime); - mnmt = new MasterNodeMonitoringThread(client, 5000L, pt); - } - try { - if (ro.simulateNodeCrashes) { - new Thread(pt, "ProvisioningThread").start(); - new Thread(mnmt, "MasterNodeMonitoringThread").start(); - } - final TimeMarker marker = new TimeMarker().start(); - int count = 0; - for (final JPPFJob job: jobProvider) { - if ((job != null) && !client.isClosed()) client.submitJob(job); - if (count == ro.triggerNodeDeadlockAfter) { - final JPPFJob deadlockingJob = new JPPFJob(); - deadlockingJob.setName("deadlock trigger job"); - deadlockingJob.setBlocking(false); - deadlockingJob.add(new DeadlockingTask()); - client.submitJob(deadlockingJob); - } - count++; - //requestNodeShutdown(client); - } - while (jobProvider.hasPendingJob()) Thread.sleep(10L); - printf("*** executed a total of %,d jobs and %,d tasks in %s", jobProvider.getJobCount(), jobProvider.getTaskCount(), marker.stop().getLastElapsedAsString()); - } finally { - if (ro.simulateNodeCrashes) { - pt.setStopped(true); - mnmt.setStopped(true); - } - } - } catch (final Exception e) { - e.printStackTrace(); - } - } - - /** - * - */ - public void testNodes() { - final TypedProperties config = JPPFConfiguration.getProperties(); - final int[] nbSlaves = StringUtils.parseIntValues(config.getString("deadlock.nbSlaves", "0")); - try (JPPFClient jppfClient = new JPPFClient()) { - ensureSufficientConnections(jppfClient, 1); - updateSlaveNodes(jppfClient, 0); - final TimeMarker marker = new TimeMarker().start(); - for (int n: nbSlaves) updateSlaveNodes(jppfClient, n); - printf("total time: %s", marker.stop().getLastElapsedAsString()); - updateSlaveNodes(jppfClient, 0); - } catch(final Exception e) { - e.printStackTrace(); - } - } - - /** - * Process the results of a job. - * @param job the JPPF job whose results are printed. - */ - public static void processResults(final JPPFJob job) { - printf("*** results for job '%s' ***", job.getName()); - final List> results = job.getAllResults(); - for (Task task: results) { - if (task.getThrowable() != null) printf("%s raised an exception : %s", task.getId(), ExceptionUtils.getMessage(task.getThrowable())); - //else System.out.printf("result of %s : %s\n", task.getId(), task.getResult()); - } - } - - /** - * Ensure that the JPPF client has the specified number of connections. - * @param client the jppf client. - * @param nbConnections the desired number of connections. - * @throws Exception if any error occurs. - */ - private static void ensureSufficientConnections(final JPPFClient client, final int nbConnections) throws Exception { - printf("***** ensuring %d connections ...", nbConnections); - final JPPFConnectionPool pool = client.awaitConnectionPool(); - printf("***** ensuring %d connections, found pool = %s", nbConnections, pool); - pool.setSize(nbConnections); - printf("***** ensuring %d connections, called setSize(%d)", nbConnections, nbConnections); - pool.awaitActiveConnections(Operator.AT_LEAST, nbConnections); - printf("***** ensuring %d connections, after pool.await()", nbConnections); - } - - /** - * Update the number of running slave nodes. - * @param client the JPPF client to get a JMX connection from. - * @param nbSlaves the number of slave nodes to reach. - * @throws Exception if any error occurs. - */ - private static void updateSlaveNodes(final JPPFClient client, final int nbSlaves) throws Exception { - printf("ensuring %d slaves ...", nbSlaves); - final JMXDriverConnectionWrapper jmx = getJmxConnection(client); - if (jmx.nbNodes() == nbSlaves + 1) return; - final JPPFNodeForwardingMBean forwarder = jmx.getNodeForwarder(); - final NodeSelector masterSelector = new ExecutionPolicySelector(new Equal("jppf.node.provisioning.master", true)); - // request that slave nodes be provisioned - final TimeMarker marker = new TimeMarker().start(); - forwarder.provisionSlaveNodes(masterSelector, nbSlaves, null); - while (jmx.nbNodes() != nbSlaves + 1) Thread.sleep(10L); - printf("slaves confirmation wait time: %s", marker.stop().getLastElapsedAsString()); - } - - /** - * Request that a node be shutdown. - * @param client the JPPF client to get a JMX connection from. - * @throws Exception if any error occurs. - */ - @SuppressWarnings("unused") - private static void requestNodeShutdown(final JPPFClient client) throws Exception { - printf("requesting node shutdown ..."); - final JMXDriverConnectionWrapper jmx = getJmxConnection(client); - final NodeSelector selector = new ExecutionPolicySelector(new Equal("jppf.node.provisioning.master", true)); - jmx.getNodeForwarder().shutdown(selector, false); - } - - /** - * Get a JMX connectionf rom the specified client. - * @param client the client ot get the connection from. - * @return a {@link JMXDriverConnectionWrapper} instance. - * @throws Exception if any error occurs. - */ - static synchronized JMXDriverConnectionWrapper getJmxConnection(final JPPFClient client) throws Exception { - if (jmx == null) jmx = client.awaitWorkingConnectionPool().awaitWorkingJMXConnection(); - return jmx; - } - - /** - * Submit a job with a single task wich triggers a deadlock int he node where it executes. - * @param client the client which submits the job. - * @throws Exception if any error occurs. - */ - @SuppressWarnings("unused") - private static void submitDeadlockingJob(final JPPFClient client) throws Exception { - final JPPFJob job = new JPPFJob(); - job.setName("Deadlocking job"); - job.setBlocking(false); - job.add(new DeadlockingTask()); - client.submitJob(job); - } - - /** - * Print and log the specified formatted message. - * @param format the message format. - * @param params the parameters of the message. - */ - static void printf(final String format, final Object...params) { - final String msg = String.format(format, params); - System.out.println(msg); - log.info(msg); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.test.deadlock; + +import java.util.List; + +import org.jppf.client.*; +import org.jppf.management.*; +import org.jppf.management.forwarding.JPPFNodeForwardingMBean; +import org.jppf.node.policy.Equal; +import org.jppf.node.protocol.Task; +import org.jppf.utils.*; +import org.slf4j.*; + +/** + * An illustration of the patterns for submitting multiple jobs in parallel. + */ +public class DeadlockRunner { + /** + * Logger for this class. + */ + private static Logger log = LoggerFactory.getLogger(ProvisioningThread.class); + /** + * + */ + private static JMXDriverConnectionWrapper jmx = null; + + /** + * Entry point for this demo. + * @param args the first argument is a function number that determines which method to call. + */ + public static void main(final String[] args) { + try { + //StreamUtils.waitKeyPressed("Please press [Enter]"); + final DeadlockRunner runner = new DeadlockRunner(); + runner.jobStreaming(); + //runner.testNodes(); + } catch (final Exception e) { + e.printStackTrace(); + } + } + + /** + * Execute a stream of non-blocking jobs from a single thread, process the results asynchronously. + */ + public void jobStreaming() { + final RunOptions ro = new RunOptions(); + printf("Running with conccurencyLimit=%d, nbJobs=%d, tasksPerJob=%d, taskDuration=%d", ro.concurrencyLimit, ro.nbJobs, ro.tasksPerJob, ro.taskOptions.taskDuration); + ProvisioningThread pt = null; + MasterNodeMonitoringThread mnmt = null; + try (JPPFClient client = new JPPFClient(); JobStreamImpl jobProvider = new JobStreamImpl(ro)) { + getJmxConnection(client); + ro.callback = new ScriptedJobCallback(); + //ro.callback = new SystemExitCallback(); + //ro.callback = new JobPersistenceCallback(); + ensureSufficientConnections(client, ro.clientConnections); + if (ro.slaves >= 0) updateSlaveNodes(client, ro.slaves); + if (ro.simulateNodeCrashes) { + pt = new ProvisioningThread(client, ro.waitTime); + mnmt = new MasterNodeMonitoringThread(client, 5000L, pt); + } + try { + if (ro.simulateNodeCrashes) { + new Thread(pt, "ProvisioningThread").start(); + new Thread(mnmt, "MasterNodeMonitoringThread").start(); + } + final TimeMarker marker = new TimeMarker().start(); + int count = 0; + for (final JPPFJob job: jobProvider) { + if ((job != null) && !client.isClosed()) client.submitJob(job); + if (count == ro.triggerNodeDeadlockAfter) { + final JPPFJob deadlockingJob = new JPPFJob(); + deadlockingJob.setName("deadlock trigger job"); + deadlockingJob.setBlocking(false); + deadlockingJob.add(new DeadlockingTask()); + client.submitJob(deadlockingJob); + } + count++; + //requestNodeShutdown(client); + } + while (jobProvider.hasPendingJob()) Thread.sleep(10L); + printf("*** executed a total of %,d jobs and %,d tasks in %s", jobProvider.getJobCount(), jobProvider.getTaskCount(), marker.stop().getLastElapsedAsString()); + } finally { + if (ro.simulateNodeCrashes) { + pt.setStopped(true); + mnmt.setStopped(true); + } + } + } catch (final Exception e) { + e.printStackTrace(); + } + } + + /** + * + */ + public void testNodes() { + final TypedProperties config = JPPFConfiguration.getProperties(); + final int[] nbSlaves = StringUtils.parseIntValues(config.getString("deadlock.nbSlaves", "0")); + try (JPPFClient jppfClient = new JPPFClient()) { + ensureSufficientConnections(jppfClient, 1); + updateSlaveNodes(jppfClient, 0); + final TimeMarker marker = new TimeMarker().start(); + for (int n: nbSlaves) updateSlaveNodes(jppfClient, n); + printf("total time: %s", marker.stop().getLastElapsedAsString()); + updateSlaveNodes(jppfClient, 0); + } catch(final Exception e) { + e.printStackTrace(); + } + } + + /** + * Process the results of a job. + * @param job the JPPF job whose results are printed. + */ + public static void processResults(final JPPFJob job) { + printf("*** results for job '%s' ***", job.getName()); + final List> results = job.getAllResults(); + for (Task task: results) { + if (task.getThrowable() != null) printf("%s raised an exception : %s", task.getId(), ExceptionUtils.getMessage(task.getThrowable())); + //else System.out.printf("result of %s : %s\n", task.getId(), task.getResult()); + } + } + + /** + * Ensure that the JPPF client has the specified number of connections. + * @param client the jppf client. + * @param nbConnections the desired number of connections. + * @throws Exception if any error occurs. + */ + private static void ensureSufficientConnections(final JPPFClient client, final int nbConnections) throws Exception { + printf("***** ensuring %d connections ...", nbConnections); + final JPPFConnectionPool pool = client.awaitConnectionPool(); + printf("***** ensuring %d connections, found pool = %s", nbConnections, pool); + pool.setSize(nbConnections); + printf("***** ensuring %d connections, called setSize(%d)", nbConnections, nbConnections); + pool.awaitActiveConnections(org.jppf.utils.Operator.AT_LEAST, nbConnections); + printf("***** ensuring %d connections, after pool.await()", nbConnections); + } + + /** + * Update the number of running slave nodes. + * @param client the JPPF client to get a JMX connection from. + * @param nbSlaves the number of slave nodes to reach. + * @throws Exception if any error occurs. + */ + private static void updateSlaveNodes(final JPPFClient client, final int nbSlaves) throws Exception { + printf("ensuring %d slaves ...", nbSlaves); + final JMXDriverConnectionWrapper jmx = getJmxConnection(client); + if (jmx.nbNodes() == nbSlaves + 1) return; + final JPPFNodeForwardingMBean forwarder = jmx.getNodeForwarder(); + final NodeSelector masterSelector = new ExecutionPolicySelector(new Equal("jppf.node.provisioning.master", true)); + // request that slave nodes be provisioned + final TimeMarker marker = new TimeMarker().start(); + forwarder.provisionSlaveNodes(masterSelector, nbSlaves, null); + while (jmx.nbNodes() != nbSlaves + 1) Thread.sleep(10L); + printf("slaves confirmation wait time: %s", marker.stop().getLastElapsedAsString()); + } + + /** + * Request that a node be shutdown. + * @param client the JPPF client to get a JMX connection from. + * @throws Exception if any error occurs. + */ + @SuppressWarnings("unused") + private static void requestNodeShutdown(final JPPFClient client) throws Exception { + printf("requesting node shutdown ..."); + final JMXDriverConnectionWrapper jmx = getJmxConnection(client); + final NodeSelector selector = new ExecutionPolicySelector(new Equal("jppf.node.provisioning.master", true)); + jmx.getNodeForwarder().shutdown(selector, false); + } + + /** + * Get a JMX connectionf rom the specified client. + * @param client the client ot get the connection from. + * @return a {@link JMXDriverConnectionWrapper} instance. + * @throws Exception if any error occurs. + */ + static synchronized JMXDriverConnectionWrapper getJmxConnection(final JPPFClient client) throws Exception { + if (jmx == null) jmx = client.awaitWorkingConnectionPool().awaitWorkingJMXConnection(); + return jmx; + } + + /** + * Submit a job with a single task wich triggers a deadlock int he node where it executes. + * @param client the client which submits the job. + * @throws Exception if any error occurs. + */ + @SuppressWarnings("unused") + private static void submitDeadlockingJob(final JPPFClient client) throws Exception { + final JPPFJob job = new JPPFJob(); + job.setName("Deadlocking job"); + job.setBlocking(false); + job.add(new DeadlockingTask()); + client.submitJob(job); + } + + /** + * Print and log the specified formatted message. + * @param format the message format. + * @param params the parameters of the message. + */ + static void printf(final String format, final Object...params) { + final String msg = String.format(format, params); + System.out.println(msg); + log.info(msg); + } +} diff --git a/demo/src/java/sample/test/jppfcallable/JPPFCallableRunner.java b/demo/src/java/sample/test/jppfcallable/JPPFCallableRunner.java index 3e38181ca9..37ee3f3356 100644 --- a/demo/src/java/sample/test/jppfcallable/JPPFCallableRunner.java +++ b/demo/src/java/sample/test/jppfcallable/JPPFCallableRunner.java @@ -1,204 +1,204 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package sample.test.jppfcallable; - -import java.util.*; - -import org.jppf.client.*; -import org.jppf.client.event.*; -import org.jppf.logging.jmx.JmxLogger; -import org.jppf.management.JMXDriverConnectionWrapper; -import org.jppf.utils.StringUtils; -import org.slf4j.*; - -/** - * Runner class for the "Long Task" demo. - * @author Laurent Cohen - */ -public class JPPFCallableRunner { - /** - * Logger for this class. - */ - static Logger log = LoggerFactory.getLogger(JPPFCallableRunner.class); - /** - * JPPF client used to submit execution requests. - */ - private static JPPFClient jppfClient = null; - /** - * Used to test JPPFTask.compute(JPPFCallable) in method {@link #testComputeCallable()}. - */ - static String callableResult = ""; - /** - * - */ - private static MyLoggingHandler loggingHandler = null; - - /** - * Entry point for this class, submits the tasks with a set duration to the server. - * @param args not used. - * @throws Exception if an error is raised during the execution. - */ - public static void main(final String...args) throws Exception { - final int nbRuns = 1; - //loggingHandler = new MyLoggingHandler(); - for (int i=1; i<=nbRuns; i++) { - print("*---------- run " + StringUtils.padLeft(String.valueOf(i), '0', 3) + " ----------*"); - try { - perform(); - } catch(final Exception e) { - e.printStackTrace(); - } finally { - //restartDriver(1L, i < nbRuns ? 2000L : -1L); - if (jppfClient != null) jppfClient.close(); - if (i < nbRuns) Thread.sleep(3000L); - } - } - } - - /** - * Perform the test using JPPFClient.submit(JPPFJob) to submit the tasks. - * @throws Exception if an error is raised during the execution. - */ - private static void perform() throws Exception { - final int nbTasks = 400; - final int nbJobs = 1; - final int maxChannels = 1; - final int size = 1024; - final long time = 10L; - configure(); - jppfClient = new JPPFClient(); - while (!jppfClient.hasAvailableConnection()) Thread.sleep(20L); - JmxLogger jmxLogger = null; - if (loggingHandler != null) { - jmxLogger = getJmxLogger(); - loggingHandler.register(jmxLogger); - } - print("submitting " + nbJobs + " jobs with " + nbTasks + " tasks"); - final List jobList = new ArrayList<>(); - for (int n=1; n<=nbJobs; n++) { - final String name = "job-" + StringUtils.padLeft(String.valueOf(n), '0', 4); - final JPPFJob job = new JPPFJob(name); - job.getClientSLA().setMaxChannels(maxChannels); - job.setBlocking(false); - for (int i=1; i<=nbTasks; i++) job.add(new MyTask(time, size)).setId(name + ":task-" + StringUtils.padLeft(String.valueOf(i), '0', 5)); - job.addJobListener(new JobListenerAdapter() { - @Override - public synchronized void jobReturned(final JobEvent event) { - print("received " + event.getJobTasks().size() + " results"); - } - }); - //job.addJobListener(new MyJobListener()); - jobList.add(job); - } - callableResult = "from MyCallable"; - for (JPPFJob job: jobList) jppfClient.submitJob(job); - for (JPPFJob job: jobList) { - job.awaitResults(); - print("got results for job '" + job.getName() + "'"); - } - if (loggingHandler != null) loggingHandler.unregister(jmxLogger); - } - - /** - * Perform optional configuration before creating the JPPF client. - */ - public static void configure() { - } - - /** - * Print a message tot he log and to the console. - * @param msg the message to print. - */ - private static void print(final String msg) { - log.info(msg); - System.out.println(msg); - } - - /** - * Use JMX to stop the driver. - * @param shutdownDelay . - * @param restartDelay . - * @throws Exception if any error occurs. - */ - @SuppressWarnings("unused") - private static void restartDriver(final long shutdownDelay, final long restartDelay) throws Exception { - final JMXDriverConnectionWrapper jmx = getDriverJmx(); - try { - jmx.restartShutdown(shutdownDelay, restartDelay); - } finally { - try { - jmx.close(); - } catch (final Exception ignore) { - } - } - } - - /** - * Get a driver JMX connection. - * @return a {@link org.jppf.management.JMXDriverConnectionWrapper} instance. - * @throws Exception if any error occurs. - */ - private static JMXDriverConnectionWrapper getDriverJmx() throws Exception { - return jppfClient.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - } - - /** - * Get a proxy to the JmxLooger. - * @return a {@link JmxLogger} instance. - * @throws Exception if any error occurs. - */ - private static JmxLogger getJmxLogger() throws Exception { - return getDriverJmx().getProxy(JmxLogger.DEFAULT_MBEAN_NAME, JmxLogger.class); - } - - /** - * - */ - public static class MyJobListener extends JobListenerAdapter { - /** - * - */ - private int lastCount = 0; - - @Override - public void jobReturned(final JobEvent event) { - System.out.println("job '" + event.getJob().getName() + "' returned"); - final JPPFJob job = event.getJob(); - final int size = job.getResults().size(); - if (size - lastCount > 100) { - System.out.println("received " + size + " tasks for job '" + job.getName() + "'"); - lastCount = size; - } - } - - @Override - public void jobStarted(final JobEvent event) { - System.out.println("job '" + event.getJob().getName() + "' started"); - } - - @Override - public void jobEnded(final JobEvent event) { - System.out.println("job '" + event.getJob().getName() + "' ended"); - } - - @Override - public void jobDispatched(final JobEvent event) { - System.out.println("job '" + event.getJob().getName() + "' dispatched"); - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package sample.test.jppfcallable; + +import java.util.*; + +import org.jppf.client.*; +import org.jppf.client.event.*; +import org.jppf.logging.jmx.JmxLogger; +import org.jppf.management.JMXDriverConnectionWrapper; +import org.jppf.utils.StringUtils; +import org.slf4j.*; + +/** + * Runner class for the "Long Task" demo. + * @author Laurent Cohen + */ +public class JPPFCallableRunner { + /** + * Logger for this class. + */ + static Logger log = LoggerFactory.getLogger(JPPFCallableRunner.class); + /** + * JPPF client used to submit execution requests. + */ + private static JPPFClient jppfClient = null; + /** + * Used to test JPPFTask.compute(JPPFCallable) in method {@link #testComputeCallable()}. + */ + static String callableResult = ""; + /** + * + */ + private static MyLoggingHandler loggingHandler = null; + + /** + * Entry point for this class, submits the tasks with a set duration to the server. + * @param args not used. + * @throws Exception if an error is raised during the execution. + */ + public static void main(final String...args) throws Exception { + final int nbRuns = 1; + //loggingHandler = new MyLoggingHandler(); + for (int i=1; i<=nbRuns; i++) { + print("*---------- run " + StringUtils.padLeft(String.valueOf(i), '0', 3) + " ----------*"); + try { + perform(); + } catch(final Exception e) { + e.printStackTrace(); + } finally { + //restartDriver(1L, i < nbRuns ? 2000L : -1L); + if (jppfClient != null) jppfClient.close(); + if (i < nbRuns) Thread.sleep(3000L); + } + } + } + + /** + * Perform the test using JPPFClient.submit(JPPFJob) to submit the tasks. + * @throws Exception if an error is raised during the execution. + */ + private static void perform() throws Exception { + final int nbTasks = 400; + final int nbJobs = 1; + final int maxChannels = 1; + final int size = 1024; + final long time = 10L; + configure(); + jppfClient = new JPPFClient(); + while (!jppfClient.hasAvailableConnection()) Thread.sleep(20L); + JmxLogger jmxLogger = null; + if (loggingHandler != null) { + jmxLogger = getJmxLogger(); + loggingHandler.register(jmxLogger); + } + print("submitting " + nbJobs + " jobs with " + nbTasks + " tasks"); + final List jobList = new ArrayList<>(); + for (int n=1; n<=nbJobs; n++) { + final String name = "job-" + StringUtils.padLeft(String.valueOf(n), '0', 4); + final JPPFJob job = new JPPFJob(name); + job.getClientSLA().setMaxChannels(maxChannels); + job.setBlocking(false); + for (int i=1; i<=nbTasks; i++) job.add(new MyTask(time, size)).setId(name + ":task-" + StringUtils.padLeft(String.valueOf(i), '0', 5)); + job.addJobListener(new JobListenerAdapter() { + @Override + public synchronized void jobReturned(final JobEvent event) { + print("received " + event.getJobTasks().size() + " results"); + } + }); + //job.addJobListener(new MyJobListener()); + jobList.add(job); + } + callableResult = "from MyCallable"; + for (JPPFJob job: jobList) jppfClient.submitJob(job); + for (JPPFJob job: jobList) { + job.awaitResults(); + print("got results for job '" + job.getName() + "'"); + } + if (loggingHandler != null) loggingHandler.unregister(jmxLogger); + } + + /** + * Perform optional configuration before creating the JPPF client. + */ + public static void configure() { + } + + /** + * Print a message tot he log and to the console. + * @param msg the message to print. + */ + private static void print(final String msg) { + log.info(msg); + System.out.println(msg); + } + + /** + * Use JMX to stop the driver. + * @param shutdownDelay . + * @param restartDelay . + * @throws Exception if any error occurs. + */ + @SuppressWarnings("unused") + private static void restartDriver(final long shutdownDelay, final long restartDelay) throws Exception { + final JMXDriverConnectionWrapper jmx = getDriverJmx(); + try { + jmx.restartShutdown(shutdownDelay, restartDelay); + } finally { + try { + jmx.close(); + } catch (final Exception ignore) { + } + } + } + + /** + * Get a driver JMX connection. + * @return a {@link org.jppf.management.JMXDriverConnectionWrapper} instance. + * @throws Exception if any error occurs. + */ + private static JMXDriverConnectionWrapper getDriverJmx() throws Exception { + return jppfClient.awaitActiveConnectionPool().awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); + } + + /** + * Get a proxy to the JmxLooger. + * @return a {@link JmxLogger} instance. + * @throws Exception if any error occurs. + */ + private static JmxLogger getJmxLogger() throws Exception { + return getDriverJmx().getProxy(JmxLogger.DEFAULT_MBEAN_NAME, JmxLogger.class); + } + + /** + * + */ + public static class MyJobListener extends JobListenerAdapter { + /** + * + */ + private int lastCount = 0; + + @Override + public void jobReturned(final JobEvent event) { + System.out.println("job '" + event.getJob().getName() + "' returned"); + final JPPFJob job = event.getJob(); + final int size = job.getResults().size(); + if (size - lastCount > 100) { + System.out.println("received " + size + " tasks for job '" + job.getName() + "'"); + lastCount = size; + } + } + + @Override + public void jobStarted(final JobEvent event) { + System.out.println("job '" + event.getJob().getName() + "' started"); + } + + @Override + public void jobEnded(final JobEvent event) { + System.out.println("job '" + event.getJob().getName() + "' ended"); + } + + @Override + public void jobDispatched(final JobEvent event) { + System.out.println("job '" + event.getJob().getName() + "' dispatched"); + } + } +} diff --git a/demo/src/java/test/driver/restart/TestDriverRestart.java b/demo/src/java/test/driver/restart/TestDriverRestart.java index ad2f8306a6..00385c7519 100644 --- a/demo/src/java/test/driver/restart/TestDriverRestart.java +++ b/demo/src/java/test/driver/restart/TestDriverRestart.java @@ -1,182 +1,182 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.driver.restart; - -import java.io.IOException; -import java.util.TimerTask; -import java.util.concurrent.atomic.AtomicBoolean; - -import org.jppf.client.*; -import org.jppf.management.*; -import org.jppf.management.diagnostics.*; -import org.jppf.management.forwarding.JPPFNodeForwardingMBean; -import org.jppf.utils.ExceptionUtils; - -/** - * - * @author Laurent Cohen - */ -public class TestDriverRestart { - /** - * The JPPF client. - */ - private static JPPFClient client = null; - /** */ - private static AtomicBoolean initializing = new AtomicBoolean(false); - /** */ - private static JPPFNodeForwardingMBean nodeForwarder = null; - /** */ - private static DiagnosticsMBean diagnostics = null; - - /** - * Entry point. - * @param args not used. - */ - public static void main(final String...args) { - try { - client = new JPPFClient(); - - JMXDriverConnectionWrapper jmx = null; - while ((jmx = getJmxWrapper()) == null) Thread.sleep(10L); - initializeProxies(); - while (diagnostics == null) Thread.sleep(10L); - performGC(); - - jmx.restartShutdown(1L, 1L); // restart the driver - System.out.println("first try after restart"); - performGC(); // fails with IOException : this is expected since driver was restarted - - // wait until connection is initialized and retry - while ((getJmxWrapper() == null) || (diagnostics == null)) Thread.sleep(10L); - System.out.println("second try after restart"); - performGC(); - } catch(final Exception e) { - e.printStackTrace(); - } finally { - if (client != null) client.close(); - } - } - - /** - * Get a connection to the remote driver's MBean server.. - * @return a {@link JMXDriverConnectionWrapper} instance. - */ - public static JMXDriverConnectionWrapper getJmxWrapper() { - try { - return client.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - } catch(final Exception e) { - e.printStackTrace(); - return null; - } - /* - JMXDriverConnectionWrapper jmxConnection = null; - final List connections = client.getAllConnections(); - for (final JPPFClientConnection c : connections) { - if (c.getStatus() == JPPFClientConnectionStatus.ACTIVE) { - jmxConnection = c.getJmxConnection(); - if (jmxConnection != null && jmxConnection.isConnected()) { - break; - } else { - jmxConnection = null; - } - } - } - return jmxConnection; - */ - } - - /** */ - @SuppressWarnings("unused") - private static class KillDriverTask extends TimerTask { - @Override - public void run() { - try { - final JPPFDriverAdminMBean jmx = getJmxWrapper(); - jmx.restartShutdown(10L, 10L); - } catch (final Exception e) { - } - } - } - - /** */ - private static void initializeProxies() { - if (initializing.compareAndSet(false, true)) { - nodeForwarder = null; - diagnostics = null; - new Thread(new ProxySettingTask()).start(); - } - } - - /** */ - private static class ProxySettingTask implements Runnable { - @SuppressWarnings("unused") - @Override - public void run() { - try { - boolean hasNullProxy = true; - while (hasNullProxy) { - final JMXDriverConnectionWrapper jmxWrapper = getJmxWrapper(); - if (jmxWrapper != null) { - try { - if (nodeForwarder == null) nodeForwarder = jmxWrapper.getNodeForwarder(); - } catch (final Exception ignore) { - } - try { - if (diagnostics == null) { - diagnostics = jmxWrapper.getDiagnosticsProxy(); - } - } catch (final Exception ignore) { - } - } - hasNullProxy = (nodeForwarder == null) || (diagnostics == null); - try { - if (hasNullProxy) { - Thread.sleep(500L); - } - } catch (final InterruptedException ignore) { - } - } - } finally { - initializing.set(false); - } - } - } - - /** */ - private static void performGC() { - System.out.println("Performing garbage collection."); - // Do the gc() in the drivers - try { - if (diagnostics == null) { - return; - } - - final HealthSnapshot healthSnapshot = diagnostics.healthSnapshot(); - System.out.println("health snapshot = " + healthSnapshot); - if (healthSnapshot.getHeapUsedRatio() >= 0.6 || healthSnapshot.getNonheapUsedRatio() >= 0.6) { - diagnostics.gc(); - } - } catch (final IOException ex) { - System.out.println("Unable to collect garbage in the drivers." + ExceptionUtils.getStackTrace(ex)); - initializeProxies(); - } catch (final Exception ex) { - System.out.println("Unable to collect garbage in the drivers." + ExceptionUtils.getStackTrace(ex)); - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.driver.restart; + +import java.io.IOException; +import java.util.TimerTask; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.jppf.client.*; +import org.jppf.management.*; +import org.jppf.management.diagnostics.*; +import org.jppf.management.forwarding.JPPFNodeForwardingMBean; +import org.jppf.utils.ExceptionUtils; + +/** + * + * @author Laurent Cohen + */ +public class TestDriverRestart { + /** + * The JPPF client. + */ + private static JPPFClient client = null; + /** */ + private static AtomicBoolean initializing = new AtomicBoolean(false); + /** */ + private static JPPFNodeForwardingMBean nodeForwarder = null; + /** */ + private static DiagnosticsMBean diagnostics = null; + + /** + * Entry point. + * @param args not used. + */ + public static void main(final String...args) { + try { + client = new JPPFClient(); + + JMXDriverConnectionWrapper jmx = null; + while ((jmx = getJmxWrapper()) == null) Thread.sleep(10L); + initializeProxies(); + while (diagnostics == null) Thread.sleep(10L); + performGC(); + + jmx.restartShutdown(1L, 1L); // restart the driver + System.out.println("first try after restart"); + performGC(); // fails with IOException : this is expected since driver was restarted + + // wait until connection is initialized and retry + while ((getJmxWrapper() == null) || (diagnostics == null)) Thread.sleep(10L); + System.out.println("second try after restart"); + performGC(); + } catch(final Exception e) { + e.printStackTrace(); + } finally { + if (client != null) client.close(); + } + } + + /** + * Get a connection to the remote driver's MBean server.. + * @return a {@link JMXDriverConnectionWrapper} instance. + */ + public static JMXDriverConnectionWrapper getJmxWrapper() { + try { + return client.awaitActiveConnectionPool().awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); + } catch(final Exception e) { + e.printStackTrace(); + return null; + } + /* + JMXDriverConnectionWrapper jmxConnection = null; + final List connections = client.getAllConnections(); + for (final JPPFClientConnection c : connections) { + if (c.getStatus() == JPPFClientConnectionStatus.ACTIVE) { + jmxConnection = c.getJmxConnection(); + if (jmxConnection != null && jmxConnection.isConnected()) { + break; + } else { + jmxConnection = null; + } + } + } + return jmxConnection; + */ + } + + /** */ + @SuppressWarnings("unused") + private static class KillDriverTask extends TimerTask { + @Override + public void run() { + try { + final JPPFDriverAdminMBean jmx = getJmxWrapper(); + jmx.restartShutdown(10L, 10L); + } catch (final Exception e) { + } + } + } + + /** */ + private static void initializeProxies() { + if (initializing.compareAndSet(false, true)) { + nodeForwarder = null; + diagnostics = null; + new Thread(new ProxySettingTask()).start(); + } + } + + /** */ + private static class ProxySettingTask implements Runnable { + @SuppressWarnings("unused") + @Override + public void run() { + try { + boolean hasNullProxy = true; + while (hasNullProxy) { + final JMXDriverConnectionWrapper jmxWrapper = getJmxWrapper(); + if (jmxWrapper != null) { + try { + if (nodeForwarder == null) nodeForwarder = jmxWrapper.getNodeForwarder(); + } catch (final Exception ignore) { + } + try { + if (diagnostics == null) { + diagnostics = jmxWrapper.getDiagnosticsProxy(); + } + } catch (final Exception ignore) { + } + } + hasNullProxy = (nodeForwarder == null) || (diagnostics == null); + try { + if (hasNullProxy) { + Thread.sleep(500L); + } + } catch (final InterruptedException ignore) { + } + } + } finally { + initializing.set(false); + } + } + } + + /** */ + private static void performGC() { + System.out.println("Performing garbage collection."); + // Do the gc() in the drivers + try { + if (diagnostics == null) { + return; + } + + final HealthSnapshot healthSnapshot = diagnostics.healthSnapshot(); + System.out.println("health snapshot = " + healthSnapshot); + if (healthSnapshot.getHeapUsedRatio() >= 0.6 || healthSnapshot.getNonheapUsedRatio() >= 0.6) { + diagnostics.gc(); + } + } catch (final IOException ex) { + System.out.println("Unable to collect garbage in the drivers." + ExceptionUtils.getStackTrace(ex)); + initializeProxies(); + } catch (final Exception ex) { + System.out.println("Unable to collect garbage in the drivers." + ExceptionUtils.getStackTrace(ex)); + } + } +} diff --git a/demo/src/java/test/jmx/TestJMX.java b/demo/src/java/test/jmx/TestJMX.java index f6b9f1cda9..bf871ea8d7 100644 --- a/demo/src/java/test/jmx/TestJMX.java +++ b/demo/src/java/test/jmx/TestJMX.java @@ -1,160 +1,160 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.jmx; - -import java.io.StringWriter; -import java.util.*; - -import javax.management.*; - -import org.jppf.client.*; -import org.jppf.management.*; -import org.jppf.management.diagnostics.*; -import org.jppf.management.forwarding.*; -import org.jppf.node.protocol.Task; -import org.jppf.scheduling.JPPFSchedule; -import org.slf4j.*; - -import sample.dist.tasklength.LongTask; - -/** - * - * @author Laurent Cohen - */ -public class TestJMX { - /** - * Logger for this class. - */ - static Logger log = LoggerFactory.getLogger(TestJMX.class); - /** - * - */ - private static JMXDriverConnectionWrapper driverJmx = null; - /** - * - */ - private static JPPFClient client = null; - - /** - * Entry point. - * @param args not used. - */ - public static void main(final String... args) { - try { - client = new JPPFClient(); - driverJmx = client.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - System.out.println("waiting till jmx is connected ..."); - while (!driverJmx.isConnected()) Thread.sleep(10L); - perform3(); - } catch (final Throwable e) { - e.printStackTrace(); - } finally { - if (client != null) client.close(); - } - } - - /** - * Test diagnostics. - * @throws Exception if any error occurs. - */ - @SuppressWarnings("unused") - private static void perform1() throws Exception { - final DiagnosticsMBean diag = driverJmx.getDiagnosticsProxy(); - final ThreadDump td = diag.threadDump(); - final StringWriter sw = new StringWriter(); - final HTMLThreadDumpWriter writer = new HTMLThreadDumpWriter(sw, "driver " + driverJmx.getDisplayName()); - writer.printThreadDump(td); - writer.close(); - output("driver thread dump:"); - output(sw.toString()); - output("... jmx connected"); - } - - /** - * Test notification forwarding. - * @throws Exception if any error occurs. - */ - @SuppressWarnings("unused") - private static void perform2() throws Exception { - Thread.sleep(500L); - final NodeNotificationListener listener = new NodeNotificationListener(); - final String listenerID = driverJmx.registerForwardingNotificationListener(new AllNodesSelector(), JPPFNodeTaskMonitorMBean.MBEAN_NAME, listener, null, "testing"); - final JPPFJob job = new JPPFJob(); - for (int i = 0; i < 5; i++) job.add(new LongTask(100L)).setId(String.valueOf(i + 1)); - final List> results = client.submitJob(job); - Thread.sleep(500L); - driverJmx.unregisterForwardingNotificationListener(listenerID); - Thread.sleep(500L); - } - - /** - * Test cancelling tasks from node listener. - * @throws Exception if any error occurs. - */ - private static void perform3() throws Exception { - final int nbJobs = 10; - final int nbTasks = 100; - for (int i = 0; i < nbJobs; i++) { - final JPPFJob job = new JPPFJob(); - job.setName("job" + (i + 1)); - for (int j = 0; j < nbTasks; j++) { - final Task task = new LongTask(100L); - task.setTimeoutSchedule(new JPPFSchedule(50L)); - job.add(task).setId(String.valueOf(j + 1)); - } - final List> results = client.submitJob(job); - output(job.getName() + " : received " + results.size() + " results"); - } - } - - /** - * Prints qnd logs the specified ;essqge. - * @param message ;essqge to print. - */ - private static void output(final String message) { - System.out.println(message); - log.info(message); - } - - /** - * - */ - public static class NodeNotificationListener implements NotificationListener { - /** - * The task information received as notifications from the node. - */ - public List notifs = new ArrayList<>(); - /** - * - */ - public Exception exception = null; - - @Override - public void handleNotification(final Notification notification, final Object handback) { - try { - System.out.println("received notification " + notification); - final JPPFNodeForwardingNotification notif = (JPPFNodeForwardingNotification) notification; - System.out.println("nodeUuid=" + notif.getNodeUuid() + ", mBeanName='" + notif.getMBeanName() + "', inner notification=" + notif.getNotification()); - notifs.add(notification); - } catch (final Exception e) { - if (exception == null) exception = e; - } - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.jmx; + +import java.io.StringWriter; +import java.util.*; + +import javax.management.*; + +import org.jppf.client.*; +import org.jppf.management.*; +import org.jppf.management.diagnostics.*; +import org.jppf.management.forwarding.*; +import org.jppf.node.protocol.Task; +import org.jppf.scheduling.JPPFSchedule; +import org.slf4j.*; + +import sample.dist.tasklength.LongTask; + +/** + * + * @author Laurent Cohen + */ +public class TestJMX { + /** + * Logger for this class. + */ + static Logger log = LoggerFactory.getLogger(TestJMX.class); + /** + * + */ + private static JMXDriverConnectionWrapper driverJmx = null; + /** + * + */ + private static JPPFClient client = null; + + /** + * Entry point. + * @param args not used. + */ + public static void main(final String... args) { + try { + client = new JPPFClient(); + driverJmx = client.awaitActiveConnectionPool().awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); + System.out.println("waiting till jmx is connected ..."); + while (!driverJmx.isConnected()) Thread.sleep(10L); + perform3(); + } catch (final Throwable e) { + e.printStackTrace(); + } finally { + if (client != null) client.close(); + } + } + + /** + * Test diagnostics. + * @throws Exception if any error occurs. + */ + @SuppressWarnings("unused") + private static void perform1() throws Exception { + final DiagnosticsMBean diag = driverJmx.getDiagnosticsProxy(); + final ThreadDump td = diag.threadDump(); + final StringWriter sw = new StringWriter(); + final HTMLThreadDumpWriter writer = new HTMLThreadDumpWriter(sw, "driver " + driverJmx.getDisplayName()); + writer.printThreadDump(td); + writer.close(); + output("driver thread dump:"); + output(sw.toString()); + output("... jmx connected"); + } + + /** + * Test notification forwarding. + * @throws Exception if any error occurs. + */ + @SuppressWarnings("unused") + private static void perform2() throws Exception { + Thread.sleep(500L); + final NodeNotificationListener listener = new NodeNotificationListener(); + final String listenerID = driverJmx.registerForwardingNotificationListener(new AllNodesSelector(), JPPFNodeTaskMonitorMBean.MBEAN_NAME, listener, null, "testing"); + final JPPFJob job = new JPPFJob(); + for (int i = 0; i < 5; i++) job.add(new LongTask(100L)).setId(String.valueOf(i + 1)); + final List> results = client.submitJob(job); + Thread.sleep(500L); + driverJmx.unregisterForwardingNotificationListener(listenerID); + Thread.sleep(500L); + } + + /** + * Test cancelling tasks from node listener. + * @throws Exception if any error occurs. + */ + private static void perform3() throws Exception { + final int nbJobs = 10; + final int nbTasks = 100; + for (int i = 0; i < nbJobs; i++) { + final JPPFJob job = new JPPFJob(); + job.setName("job" + (i + 1)); + for (int j = 0; j < nbTasks; j++) { + final Task task = new LongTask(100L); + task.setTimeoutSchedule(new JPPFSchedule(50L)); + job.add(task).setId(String.valueOf(j + 1)); + } + final List> results = client.submitJob(job); + output(job.getName() + " : received " + results.size() + " results"); + } + } + + /** + * Prints qnd logs the specified ;essqge. + * @param message ;essqge to print. + */ + private static void output(final String message) { + System.out.println(message); + log.info(message); + } + + /** + * + */ + public static class NodeNotificationListener implements NotificationListener { + /** + * The task information received as notifications from the node. + */ + public List notifs = new ArrayList<>(); + /** + * + */ + public Exception exception = null; + + @Override + public void handleNotification(final Notification notification, final Object handback) { + try { + System.out.println("received notification " + notification); + final JPPFNodeForwardingNotification notif = (JPPFNodeForwardingNotification) notification; + System.out.println("nodeUuid=" + notif.getNodeUuid() + ", mBeanName='" + notif.getMBeanName() + "', inner notification=" + notif.getNotification()); + notifs.add(notification); + } catch (final Exception e) { + if (exception == null) exception = e; + } + } + } +} diff --git a/demo/src/java/test/jmx/canceljob/CancelJobRunner.java b/demo/src/java/test/jmx/canceljob/CancelJobRunner.java index 4351a562a5..5ddda6abd0 100644 --- a/demo/src/java/test/jmx/canceljob/CancelJobRunner.java +++ b/demo/src/java/test/jmx/canceljob/CancelJobRunner.java @@ -1,116 +1,116 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package test.jmx.canceljob; - -import java.util.*; - -import org.jppf.client.*; -import org.jppf.job.JobSelector; -import org.jppf.management.*; -import org.jppf.management.forwarding.JPPFNodeForwardingMBean; -import org.jppf.server.job.management.DriverJobManagementMBean; -import org.slf4j.*; - -/** - * Runner class for the "Long Task" demo. - * @author Laurent Cohen - */ -public class CancelJobRunner { - /** - * Logger for this class. - */ - static Logger log = LoggerFactory.getLogger(CancelJobRunner.class); - /** - * JPPF client used to submit execution requests. - */ - private static JPPFClient client = null; - - /** - * Entry point for this class, submits the tasks with a set duration to the server. - * @param args not used. - */ - public static void main(final String... args) { - try { - configure(); - client = new JPPFClient(); - final long duration = 100_000L; - final int n = 30; - final JPPFConnectionPool pool = client.awaitWorkingConnectionPool(); - pool.setSize(n); - print("waiting for " + n + " client connections ..."); - client.awaitConnectionPools(Operator.EQUAL, n, 100_000L, JPPFClientConnectionStatus.workingStatuses()); - final JMXDriverConnectionWrapper jmx = pool.awaitWorkingJMXConnection(); - final JPPFNodeForwardingMBean forwarder = jmx.getNodeForwarder(); - final DriverJobManagementMBean jobManager = jmx.getJobManager(); - forwarder.provisionSlaveNodes(NodeSelector.ALL_NODES, n - 1); - int idleNodes; - print("waiting for " + (n-1) + " slave nodes ..."); - while ((idleNodes = jmx.nbIdleNodes()) < n) Thread.sleep(10L); - final List jobs = new ArrayList<>(); - for (int i=0; i jobs = new ArrayList<>(); + for (int i=0; iJPPFClient.submit(JPPFJob) to submit the tasks. - * @param nbTasks the number of tasks to send at each iteration. - * @param length the executionlength of each task. - * @param iter the run number. - * @throws Exception if an error is raised during the execution. - */ - @SuppressWarnings("unused") - private static void perform(final int nbTasks, final int length, final int iter) throws Exception { - try { - final long start = System.nanoTime(); - final JPPFJob job = new JPPFJob(); - job.setName("Long task iteration " + iter); - for (int i = 0; i < nbTasks; i++) { - final LongTask task = new LongTask(length, false); - task.setId("" + (iter + 1) + ':' + (i + 1)); - job.add(task); - } - // submit the tasks for execution - final List> results = jppfClient.submitJob(job); - for (Task task : results) { - final Throwable e = task.getThrowable(); - if (e != null) throw e; - } - final long elapsed = DateTimeUtils.elapsedFrom(start); - print("run " + iter + " time: " + StringUtils.toStringDuration(elapsed)); - - } catch (final Throwable t) { - throw new JPPFException(t.getMessage(), t); - } - } - - /** - * Perform the test using JPPFClient.submit(JPPFJob) to submit the tasks. - * @param nbTasks the number of tasks to send at each iteration. - * @param length the executionlength of each task. - * @param nbJobs the number of non-blocking jobs to submit. - * @throws Exception if an error is raised during the execution. - */ - @SuppressWarnings("unused") - private static void perform2(final int nbTasks, final int length, final int nbJobs) throws Exception { - try { - jppfClient.setLocalExecutionEnabled(true); - Thread.sleep(1000L); - print("creating the jobs"); - final List jobs = new ArrayList<>(nbJobs); - for (int i = 0; i < nbJobs; i++) { - final JPPFJob job = new JPPFJob(); - job.setName("job " + i); - job.setBlocking(false); - for (int j = 0; j < nbTasks; j++) job.add(new LongTask(length)).setId("task " + i + ':' + j); - jobs.add(job); - } - final long start = System.nanoTime(); - print("submitting the jobs"); - for (JPPFJob job : jobs) jppfClient.submitJob(job); - print("getting the results"); - for (JPPFJob job : jobs) { - job.awaitResults(); - print("got results for " + job.getName()); - } - final long elapsed = System.nanoTime() - start; - print("ran " + nbJobs + " in: " + StringUtils.toStringDuration(elapsed / 1000000)); - - } catch (final Exception e) { - throw new JPPFException(e.getMessage(), e); - } - } - - /** - * Print a message tot he log and to the console. - * @param msg the message to print. - */ - private static void print(final String msg) { - log.info(msg); - System.out.println(msg); - } - - /** - * Get a proxy to the driver's job management MBean. - * @return an instance of {@link DriverJobManagementMBean}. - * @throws Exception if any error occurs. - */ - @SuppressWarnings("unused") - private static DriverJobManagementMBean getJobManagement() throws Exception { - final JMXDriverConnectionWrapper wrapper = jppfClient.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - return wrapper.getJobManager(); - } - - /** - * Perform the test using JPPFClient.submit(JPPFJob) to submit the tasks. - * @throws Throwable if an error is raised during the execution. - */ - private static void perform3() throws Throwable { - final long start = System.nanoTime(); - final JPPFJob job = new JPPFJob(); - job.setName("test jar download"); - job.add(new MyTask()); - //job.setDataProvider(new ClientDataProvider()); - // submit the tasks for execution - final List> results = jppfClient.submitJob(job); - for (Task task : results) { - final Throwable e = task.getThrowable(); - if (e != null) throw e; - } - final long elapsed = System.nanoTime() - start; - print("run time: " + StringUtils.toStringDuration(elapsed / 1000000)); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package test.localexecution; + +import java.util.*; + +import org.jppf.JPPFException; +import org.jppf.client.*; +import org.jppf.management.JMXDriverConnectionWrapper; +import org.jppf.node.protocol.Task; +import org.jppf.server.job.management.DriverJobManagementMBean; +import org.jppf.utils.*; +import org.jppf.utils.Operator; +import org.jppf.utils.configuration.JPPFProperties; +import org.slf4j.*; + +import sample.dist.tasklength.LongTask; + +/** + * Runner class for testing the local execution toggle feature. + * @author Laurent Cohen + */ +public class LocalExecutionRunner { + /** + * Logger for this class. + */ + static Logger log = LoggerFactory.getLogger(LocalExecutionRunner.class); + /** + * JPPF client used to submit execution requests. + */ + private static JPPFClient jppfClient = null; + + /** + * Entry point for this class, submits the tasks with a set duration to the server. + * @param args not used. + */ + public static void main(final String... args) { + try { + final TypedProperties props = JPPFConfiguration.getProperties(); + props.set(JPPFProperties.LOCAL_EXECUTION_ENABLED, false); + print("starting client ..."); + final long start = System.nanoTime(); + jppfClient = new JPPFClient(); + final long elapsed = System.nanoTime() - start; + print("client started in " + StringUtils.toStringDuration(elapsed / 1000000)); + /* print("run 1 with local execution off"); + * perform(nbTask, length, 1); */ + //print("run with local execution on"); + //jppfClient.setLocalExecutionEnabled(true); + //perform2(100, 5, 200); + perform3(); + /* print("run 3 with local execution off"); + * jppfClient.setLocalExecutionEnabled(false); + * perform(nbTask, length, 3); */ + } catch (final Throwable t) { + t.printStackTrace(); + } finally { + if (jppfClient != null) jppfClient.close(); + } + } + + /** + * Perform the test using JPPFClient.submit(JPPFJob) to submit the tasks. + * @param nbTasks the number of tasks to send at each iteration. + * @param length the executionlength of each task. + * @param iter the run number. + * @throws Exception if an error is raised during the execution. + */ + @SuppressWarnings("unused") + private static void perform(final int nbTasks, final int length, final int iter) throws Exception { + try { + final long start = System.nanoTime(); + final JPPFJob job = new JPPFJob(); + job.setName("Long task iteration " + iter); + for (int i = 0; i < nbTasks; i++) { + final LongTask task = new LongTask(length, false); + task.setId("" + (iter + 1) + ':' + (i + 1)); + job.add(task); + } + // submit the tasks for execution + final List> results = jppfClient.submitJob(job); + for (Task task : results) { + final Throwable e = task.getThrowable(); + if (e != null) throw e; + } + final long elapsed = DateTimeUtils.elapsedFrom(start); + print("run " + iter + " time: " + StringUtils.toStringDuration(elapsed)); + + } catch (final Throwable t) { + throw new JPPFException(t.getMessage(), t); + } + } + + /** + * Perform the test using JPPFClient.submit(JPPFJob) to submit the tasks. + * @param nbTasks the number of tasks to send at each iteration. + * @param length the executionlength of each task. + * @param nbJobs the number of non-blocking jobs to submit. + * @throws Exception if an error is raised during the execution. + */ + @SuppressWarnings("unused") + private static void perform2(final int nbTasks, final int length, final int nbJobs) throws Exception { + try { + jppfClient.setLocalExecutionEnabled(true); + Thread.sleep(1000L); + print("creating the jobs"); + final List jobs = new ArrayList<>(nbJobs); + for (int i = 0; i < nbJobs; i++) { + final JPPFJob job = new JPPFJob(); + job.setName("job " + i); + job.setBlocking(false); + for (int j = 0; j < nbTasks; j++) job.add(new LongTask(length)).setId("task " + i + ':' + j); + jobs.add(job); + } + final long start = System.nanoTime(); + print("submitting the jobs"); + for (JPPFJob job : jobs) jppfClient.submitJob(job); + print("getting the results"); + for (JPPFJob job : jobs) { + job.awaitResults(); + print("got results for " + job.getName()); + } + final long elapsed = System.nanoTime() - start; + print("ran " + nbJobs + " in: " + StringUtils.toStringDuration(elapsed / 1000000)); + + } catch (final Exception e) { + throw new JPPFException(e.getMessage(), e); + } + } + + /** + * Print a message tot he log and to the console. + * @param msg the message to print. + */ + private static void print(final String msg) { + log.info(msg); + System.out.println(msg); + } + + /** + * Get a proxy to the driver's job management MBean. + * @return an instance of {@link DriverJobManagementMBean}. + * @throws Exception if any error occurs. + */ + @SuppressWarnings("unused") + private static DriverJobManagementMBean getJobManagement() throws Exception { + final JMXDriverConnectionWrapper wrapper = jppfClient.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); + return wrapper.getJobManager(); + } + + /** + * Perform the test using JPPFClient.submit(JPPFJob) to submit the tasks. + * @throws Throwable if an error is raised during the execution. + */ + private static void perform3() throws Throwable { + final long start = System.nanoTime(); + final JPPFJob job = new JPPFJob(); + job.setName("test jar download"); + job.add(new MyTask()); + //job.setDataProvider(new ClientDataProvider()); + // submit the tasks for execution + final List> results = jppfClient.submitJob(job); + for (Task task : results) { + final Throwable e = task.getThrowable(); + if (e != null) throw e; + } + final long elapsed = System.nanoTime() - start; + print("run time: " + StringUtils.toStringDuration(elapsed / 1000000)); + } +} diff --git a/demo/src/java/test/node/provisioning/NodeProvisioningRunner.java b/demo/src/java/test/node/provisioning/NodeProvisioningRunner.java index 7b7c280660..6d027cc4a2 100644 --- a/demo/src/java/test/node/provisioning/NodeProvisioningRunner.java +++ b/demo/src/java/test/node/provisioning/NodeProvisioningRunner.java @@ -1,158 +1,158 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.node.provisioning; - -import java.util.*; - -import org.jppf.client.*; -import org.jppf.management.*; -import org.jppf.management.forwarding.JPPFNodeForwardingMBean; -import org.jppf.node.policy.Equal; -import org.jppf.node.protocol.*; -import org.jppf.utils.ExceptionUtils; - -import sample.dist.tasklength.LongTask; - -/** - * Runs a simple demo which requests that 4 slaves be sarted on each master, - * runs a job on the slaves only, then requests that all slaves be terminated. - * @author Laurent Cohen - */ -public class NodeProvisioningRunner { - /** - * - */ - private static NodeSelector masterSelector = new ExecutionPolicySelector(new Equal("jppf.node.provisioning.master", true)); - - /** - * Entry point ofr this application. - * @param args not used. - */ - public static void main(final String[] args) { - try (JPPFClient client = new JPPFClient()) { - perform2(client); - } catch (final Exception e) { - e.printStackTrace(); - } - } - - /** - * - * @param client the lcient to use. - * @throws Exception if any error occurs. - */ - @SuppressWarnings("unused") - private static void perform1(final JPPFClient client) throws Exception { - final JPPFConnectionPool pool = client.awaitWorkingConnectionPool(); - final JMXDriverConnectionWrapper jmxDriver = pool.awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - final JPPFNodeForwardingMBean forwarder = jmxDriver.getNodeForwarder(); - - final int nbSlaves = 3; - System.out.printf("provisioning %d slaves%n", nbSlaves); - final Object o = forwarder.provisionSlaveNodes(masterSelector, nbSlaves, false); - Thread.sleep(3000L); - printNbSlaves(forwarder); - - System.out.println("submitting job ..."); - final JPPFJob job = new JPPFJob(); - job.setBlocking(false); - job.setName("Hello World"); - for (int i=1; i<=4; i++) job.add(new LongTask(30_000L)).setId("task " + i); - client.submitJob(job); - - Thread.sleep(2000L); - System.out.printf("provisioning 0 slaves%n"); - forwarder.provisionSlaveNodes(masterSelector, 0, false); - Thread.sleep(3000L); - printNbSlaves(forwarder); - System.out.printf("driver has %d nodes%n", jmxDriver.nbNodes()); - - Thread.sleep(2000L); - System.out.printf("provisioning %d slaves%n", nbSlaves); - forwarder.provisionSlaveNodes(masterSelector, nbSlaves, false); - Thread.sleep(3000L); - printNbSlaves(forwarder); - System.out.printf("driver has %d nodes%n", jmxDriver.nbNodes()); - - final List> results = job.awaitResults(); - System.out.println("got " + results.size() + " results for job"); - - System.out.println("shutting down all slaves ..."); - forwarder.provisionSlaveNodes(masterSelector, 0, false); - Thread.sleep(3000L); - printNbSlaves(forwarder); - } - - /** - * - * @param client the lcient to use. - * @throws Exception if any error occurs. - */ - private static void perform2(final JPPFClient client) throws Exception { - final JMXDriverConnectionWrapper jmx = client.awaitWorkingConnectionPool().awaitJMXConnection(true); - final JPPFNodeForwardingMBean forwarder = jmx.getNodeForwarder(); - final int nbSlaves = 10; - long totalElapsed = 0L; - for (int i=1; i<=10; i++) { - System.out.println("*******************"); - System.out.printf("iteration %d: provisioning %d slaves%n", i, nbSlaves); - long start = System.nanoTime(); - forwarder.provisionSlaveNodes(masterSelector, nbSlaves, true); - while (jmx.nbIdleNodes() != nbSlaves + 1) Thread.sleep(1L); - long elapsed = System.nanoTime() - start; - totalElapsed += elapsed; - System.out.printf("iteration %d: provisioning %d slaves took %,d ms%n", i, nbSlaves, elapsed/1_000_000L); - System.out.printf("iteration %d: un-provisioning %d slaves%n", i, nbSlaves); - start = System.nanoTime(); - forwarder.provisionSlaveNodes(masterSelector, 0, true); - while (jmx.nbIdleNodes() != 1) Thread.sleep(1L); - elapsed = System.nanoTime() - start; - totalElapsed += elapsed; - System.out.printf("iteration %d: un-provisioning %d slaves took %,d ms%n", i, nbSlaves, elapsed/1_000_000L); - } - System.out.println("*******************"); - System.out.printf("total time: %,d ms%n", totalElapsed/1_000_000L); - } - - /** - * - * @param forwarder . - * @throws Exception . - */ - private static void printNbSlaves(final JPPFNodeForwardingMBean forwarder) throws Exception { - final Map resultsMap = forwarder.getNbSlaves(masterSelector); - for (final Map.Entry entry: resultsMap.entrySet()) { - if (entry.getValue() instanceof Throwable) System.out.printf("node %s raised %s%n", entry.getKey(), ExceptionUtils.getStackTrace((Throwable) entry.getValue())); - else System.out.printf("master node %s has %d slaves%n", entry.getKey(), entry.getValue()); - } - } - - /** - * A simple task example. - */ - public static class ExampleTask extends AbstractTask { - @Override - public void run() { - final String message = "hello from " + getId(); - // this should be printed in the stdout.log of the slave node - System.out.println(message); - setResult(message); - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.node.provisioning; + +import java.util.*; + +import org.jppf.client.*; +import org.jppf.management.*; +import org.jppf.management.forwarding.JPPFNodeForwardingMBean; +import org.jppf.node.policy.Equal; +import org.jppf.node.protocol.*; +import org.jppf.utils.ExceptionUtils; + +import sample.dist.tasklength.LongTask; + +/** + * Runs a simple demo which requests that 4 slaves be sarted on each master, + * runs a job on the slaves only, then requests that all slaves be terminated. + * @author Laurent Cohen + */ +public class NodeProvisioningRunner { + /** + * + */ + private static NodeSelector masterSelector = new ExecutionPolicySelector(new Equal("jppf.node.provisioning.master", true)); + + /** + * Entry point ofr this application. + * @param args not used. + */ + public static void main(final String[] args) { + try (JPPFClient client = new JPPFClient()) { + perform2(client); + } catch (final Exception e) { + e.printStackTrace(); + } + } + + /** + * + * @param client the lcient to use. + * @throws Exception if any error occurs. + */ + @SuppressWarnings("unused") + private static void perform1(final JPPFClient client) throws Exception { + final JPPFConnectionPool pool = client.awaitWorkingConnectionPool(); + final JMXDriverConnectionWrapper jmxDriver = pool.awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); + final JPPFNodeForwardingMBean forwarder = jmxDriver.getNodeForwarder(); + + final int nbSlaves = 3; + System.out.printf("provisioning %d slaves%n", nbSlaves); + final Object o = forwarder.provisionSlaveNodes(masterSelector, nbSlaves, false); + Thread.sleep(3000L); + printNbSlaves(forwarder); + + System.out.println("submitting job ..."); + final JPPFJob job = new JPPFJob(); + job.setBlocking(false); + job.setName("Hello World"); + for (int i=1; i<=4; i++) job.add(new LongTask(30_000L)).setId("task " + i); + client.submitJob(job); + + Thread.sleep(2000L); + System.out.printf("provisioning 0 slaves%n"); + forwarder.provisionSlaveNodes(masterSelector, 0, false); + Thread.sleep(3000L); + printNbSlaves(forwarder); + System.out.printf("driver has %d nodes%n", jmxDriver.nbNodes()); + + Thread.sleep(2000L); + System.out.printf("provisioning %d slaves%n", nbSlaves); + forwarder.provisionSlaveNodes(masterSelector, nbSlaves, false); + Thread.sleep(3000L); + printNbSlaves(forwarder); + System.out.printf("driver has %d nodes%n", jmxDriver.nbNodes()); + + final List> results = job.awaitResults(); + System.out.println("got " + results.size() + " results for job"); + + System.out.println("shutting down all slaves ..."); + forwarder.provisionSlaveNodes(masterSelector, 0, false); + Thread.sleep(3000L); + printNbSlaves(forwarder); + } + + /** + * + * @param client the lcient to use. + * @throws Exception if any error occurs. + */ + private static void perform2(final JPPFClient client) throws Exception { + final JMXDriverConnectionWrapper jmx = client.awaitWorkingConnectionPool().awaitJMXConnection(true); + final JPPFNodeForwardingMBean forwarder = jmx.getNodeForwarder(); + final int nbSlaves = 10; + long totalElapsed = 0L; + for (int i=1; i<=10; i++) { + System.out.println("*******************"); + System.out.printf("iteration %d: provisioning %d slaves%n", i, nbSlaves); + long start = System.nanoTime(); + forwarder.provisionSlaveNodes(masterSelector, nbSlaves, true); + while (jmx.nbIdleNodes() != nbSlaves + 1) Thread.sleep(1L); + long elapsed = System.nanoTime() - start; + totalElapsed += elapsed; + System.out.printf("iteration %d: provisioning %d slaves took %,d ms%n", i, nbSlaves, elapsed/1_000_000L); + System.out.printf("iteration %d: un-provisioning %d slaves%n", i, nbSlaves); + start = System.nanoTime(); + forwarder.provisionSlaveNodes(masterSelector, 0, true); + while (jmx.nbIdleNodes() != 1) Thread.sleep(1L); + elapsed = System.nanoTime() - start; + totalElapsed += elapsed; + System.out.printf("iteration %d: un-provisioning %d slaves took %,d ms%n", i, nbSlaves, elapsed/1_000_000L); + } + System.out.println("*******************"); + System.out.printf("total time: %,d ms%n", totalElapsed/1_000_000L); + } + + /** + * + * @param forwarder . + * @throws Exception . + */ + private static void printNbSlaves(final JPPFNodeForwardingMBean forwarder) throws Exception { + final Map resultsMap = forwarder.getNbSlaves(masterSelector); + for (final Map.Entry entry: resultsMap.entrySet()) { + if (entry.getValue() instanceof Throwable) System.out.printf("node %s raised %s%n", entry.getKey(), ExceptionUtils.getStackTrace((Throwable) entry.getValue())); + else System.out.printf("master node %s has %d slaves%n", entry.getKey(), entry.getValue()); + } + } + + /** + * A simple task example. + */ + public static class ExampleTask extends AbstractTask { + @Override + public void run() { + final String message = "hello from " + getId(); + // this should be printed in the stdout.log of the slave node + System.out.println(message); + setResult(message); + } + } +} diff --git a/jmxremote-nio/src/java/org/jppf/jmxremote/nio/JMXTransitionTask.java b/jmxremote-nio/src/java/org/jppf/jmxremote/nio/JMXTransitionTask.java index 2cf8c3e501..cc7f620c18 100644 --- a/jmxremote-nio/src/java/org/jppf/jmxremote/nio/JMXTransitionTask.java +++ b/jmxremote-nio/src/java/org/jppf/jmxremote/nio/JMXTransitionTask.java @@ -1,119 +1,118 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.jmxremote.nio; - -import java.nio.channels.SelectionKey; - -import org.jppf.client.Operator; -import org.jppf.utils.*; -import org.jppf.utils.concurrent.SynchronizedInteger; -import org.slf4j.*; - -/** - * Instances of this class perform the transition of a channel from one state to another. - * They extend the Runnable interface so they can be executed concurrently by a pool of threads. - * @author Laurent Cohen - */ -public class JMXTransitionTask implements Runnable { - /** - * Logger for this class. - */ - private static Logger log = LoggerFactory.getLogger(JMXTransitionTask.class); - /** - * Determines whether DEBUG logging level is enabled. - */ - private static boolean debugEnabled = LoggingUtils.isDebugEnabled(log); - /** - * Determines whether TRACE logging level is enabled. - */ - private static boolean traceEnabled = log.isTraceEnabled(); - /** - * The channel whose state is changing. - */ - private final JMXContext context; - /** - * The associated nio srver. - */ - private final JMXNioServer server; - /** - * - */ - private final SynchronizedInteger requestCount = new SynchronizedInteger(); - /** - * - */ - private final boolean selecting; - - /** - * Initialize this task with the specified key and factory. - * @param channel the channel whose state is changing. - * @param server the NIO server. - * @param selecting whether the selector is selecting. - */ - public JMXTransitionTask(final JMXContext channel, final JMXNioServer server, final boolean selecting) { - this.context = channel; - this.server = server; - this.selecting = selecting; - } - - @Override - public void run() { - if (!requestCount.compareAndSet(0, 1)) return; - try { - try { - if (traceEnabled) log.trace("writing to channel {}", context); - boolean result = false; - synchronized(context) { - while (requestCount.get() > 0) { - try { - result = JMXMessageWriter.write(context); - } finally { - requestCount.decrementAndGet(); - } - } - } - if (result) { - if (selecting) server.updateInterestOps(context.getSelectionKey(), SelectionKey.OP_WRITE, true); - else JMXNioServer.updateInterestOpsNoWakeup(context.getSelectionKey(), SelectionKey.OP_WRITE, true); - } - } catch (final Exception|Error e) { - context.setEnabled(false); - throw e; - } - } catch(final Exception|Error e) { - try { - if (debugEnabled) log.debug("error on channel {} :\n{}", context, ExceptionUtils.getStackTrace(e)); - else log.warn("error on channel {} : {}", context, ExceptionUtils.getMessage(e)); - } catch (final Exception e2) { - if (debugEnabled) log.debug("error on channel: {}", ExceptionUtils.getStackTrace(e2)); - else log.warn("error on channel: {}", ExceptionUtils.getMessage(e2)); - } - if (e instanceof Exception) context.handleException(null, (Exception) e); - else throw (Error) e; - } - } - - /** - * - * @return . - */ - public boolean incrementCountIfNeeded() { - return requestCount.compareAndIncrement(Operator.MORE_THAN, 0); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.jmxremote.nio; + +import java.nio.channels.SelectionKey; + +import org.jppf.utils.*; +import org.jppf.utils.concurrent.SynchronizedInteger; +import org.slf4j.*; + +/** + * Instances of this class perform the transition of a channel from one state to another. + * They extend the Runnable interface so they can be executed concurrently by a pool of threads. + * @author Laurent Cohen + */ +public class JMXTransitionTask implements Runnable { + /** + * Logger for this class. + */ + private static Logger log = LoggerFactory.getLogger(JMXTransitionTask.class); + /** + * Determines whether DEBUG logging level is enabled. + */ + private static boolean debugEnabled = LoggingUtils.isDebugEnabled(log); + /** + * Determines whether TRACE logging level is enabled. + */ + private static boolean traceEnabled = log.isTraceEnabled(); + /** + * The channel whose state is changing. + */ + private final JMXContext context; + /** + * The associated nio srver. + */ + private final JMXNioServer server; + /** + * + */ + private final SynchronizedInteger requestCount = new SynchronizedInteger(); + /** + * + */ + private final boolean selecting; + + /** + * Initialize this task with the specified key and factory. + * @param channel the channel whose state is changing. + * @param server the NIO server. + * @param selecting whether the selector is selecting. + */ + public JMXTransitionTask(final JMXContext channel, final JMXNioServer server, final boolean selecting) { + this.context = channel; + this.server = server; + this.selecting = selecting; + } + + @Override + public void run() { + if (!requestCount.compareAndSet(0, 1)) return; + try { + try { + if (traceEnabled) log.trace("writing to channel {}", context); + boolean result = false; + synchronized(context) { + while (requestCount.get() > 0) { + try { + result = JMXMessageWriter.write(context); + } finally { + requestCount.decrementAndGet(); + } + } + } + if (result) { + if (selecting) server.updateInterestOps(context.getSelectionKey(), SelectionKey.OP_WRITE, true); + else JMXNioServer.updateInterestOpsNoWakeup(context.getSelectionKey(), SelectionKey.OP_WRITE, true); + } + } catch (final Exception|Error e) { + context.setEnabled(false); + throw e; + } + } catch(final Exception|Error e) { + try { + if (debugEnabled) log.debug("error on channel {} :\n{}", context, ExceptionUtils.getStackTrace(e)); + else log.warn("error on channel {} : {}", context, ExceptionUtils.getMessage(e)); + } catch (final Exception e2) { + if (debugEnabled) log.debug("error on channel: {}", ExceptionUtils.getStackTrace(e2)); + else log.warn("error on channel: {}", ExceptionUtils.getMessage(e2)); + } + if (e instanceof Exception) context.handleException(null, (Exception) e); + else throw (Error) e; + } + } + + /** + * + * @return . + */ + public boolean incrementCountIfNeeded() { + return requestCount.compareAndIncrement(Operator.MORE_THAN, 0); + } +} diff --git a/samples-pack/AdaptiveGrid/src/org/jppf/example/adaptivegrid/DriverConnectionManager.java b/samples-pack/AdaptiveGrid/src/org/jppf/example/adaptivegrid/DriverConnectionManager.java index ab8795747e..5433d99bd8 100644 --- a/samples-pack/AdaptiveGrid/src/org/jppf/example/adaptivegrid/DriverConnectionManager.java +++ b/samples-pack/AdaptiveGrid/src/org/jppf/example/adaptivegrid/DriverConnectionManager.java @@ -1,160 +1,160 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.example.adaptivegrid; - -import org.jppf.client.*; -import org.jppf.management.*; -import org.jppf.management.forwarding.JPPFNodeForwardingMBean; -import org.jppf.node.policy.*; -import org.jppf.utils.TypedProperties; - -/** - * This class encapsulates the functionality for managing a JPPF driver - * and its attached nodes via the JMX-based management APIs. - * @author Laurent Cohen - */ -public class DriverConnectionManager { - /** - * A proxy to the driver MBean which forwards management requests to the nodes. - */ - private JPPFNodeForwardingMBean forwarder; - /** - * The client connection pool holding the connections to the driver. - */ - private final JPPFConnectionPool connectionPool; - /** - * A node selector that only selects master nodes. - */ - private final NodeSelector masterSelector; - /** - * The maximum number of nodes that can be running on the host. - */ - private final int maxAllowedNodes; - /** - * The maximum allowed number of connections in the pool. - */ - private final int maxAllowedPoolSize; - /** - * The current number of nodes. We attempt to store and maintain it locally because - * obtaining it from the server is a costly operation that requires a remote method call. - */ - private int currentNodes; - - /** - * Initialize this manager with the specified JPPF client. - * @param client the client to use. - * @param maxAllowedNodes the maximum number of nodes that can be running on the host. - * @param maxAllowedPoolSize the maximum allowed number of connections in the pool. - * @throws Exception if any error occurs. - */ - public DriverConnectionManager(final JPPFClient client, final int maxAllowedNodes, final int maxAllowedPoolSize) throws Exception { - // wait until there is a connection pool with the at least one active connection to the driver - connectionPool = client.awaitActiveConnectionPool(); - // wait until at least one JMX connection wrapper is established - final JMXDriverConnectionWrapper jmx = connectionPool.awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - this.forwarder = jmx.getNodeForwarder(); - // create a node selector that only selects master nodes - final ExecutionPolicy masterPolicy = new Equal("jppf.node.provisioning.master", true); - this.masterSelector = new ExecutionPolicySelector(masterPolicy); - this.maxAllowedNodes = maxAllowedNodes; - this.maxAllowedPoolSize = maxAllowedPoolSize; - this.currentNodes = 1; - } - - /** - * Update the connection pool and number of slave nodes based - * on the specified number of jobs to submit concurrently. - * @param nbJobs the number of jobs to submit. - */ - public void updateGridSetup(final int nbJobs) { - // Adjust the connection pool size - int newPoolSize = computePoolSize(nbJobs); - if (newPoolSize > maxAllowedPoolSize) newPoolSize = maxAllowedPoolSize; - final int currentPoolSize = connectionPool.connectionCount(); - if (newPoolSize != currentPoolSize) { - AdaptiveGridDemo.print("%screasing the number of server connections to %d", (newPoolSize > currentPoolSize) ? "in" : "de", newPoolSize); - connectionPool.setSize(newPoolSize); - // wait until all requested connections are established - connectionPool.awaitWorkingConnections(Operator.EQUAL, newPoolSize); - } - - // Adjust the number of nodes - final int newNbNodes = computeNbNodes(nbJobs); - if (newNbNodes != currentNodes) { - AdaptiveGridDemo.print("%screasing the number of nodes to %d", (newNbNodes > currentNodes) ? "in" : "de", newNbNodes); - try { - // -1 because the master node is counted as a an execution node - updateSlaveNodes(newNbNodes - 1); - currentNodes = newNbNodes; - } catch(final Exception e) { - e.printStackTrace(); - // We don't know how many nodes were actually started, - // so we have to ask the server - try { - currentNodes = connectionPool.getJmxConnection().nbNodes(); - } catch(final Exception e2) { - e2.printStackTrace(); - } - } - } - } - - /** - * Update the number of running slave nodes. - * @param nbSlaves the number of slave nodes to reach. - * @param configOverrides optional overrides to the slave nodes' configuration. - * @throws Exception if any error occurs. - */ - private void updateSlaveNodes(final int nbSlaves, final TypedProperties configOverrides) throws Exception { - // request that slave nodes be provisioned - forwarder.provisionSlaveNodes(masterSelector, nbSlaves, configOverrides); - } - - /** - * Update the number of running slaves nodes, without configuration oveerides. - * @param nbSlaves the number of slave nodes to reach. - * @throws Exception if any error occurs. - */ - private void updateSlaveNodes(final int nbSlaves) throws Exception { - updateSlaveNodes(nbSlaves, null); - } - - /** - * Compute the desired connection pool size for the specified number of jobs. - * @param nbJobs the number of jobs to submit concurrently. - * @return the new size of the connection pool, in the range [1, maxAllowedPoolSize]. - */ - private int computePoolSize(final int nbJobs) { - // We apply a simple rule that makes the connection pool as large as the - // number of jobs, up to the maximum allowed pool size. - return Math.max(1, Math.min(nbJobs, maxAllowedPoolSize)); - } - - /** - * Compute the desired number of nodes for the specified number of jobs. - * @param nbJobs the number of jobs to submit concurrently. - * @return the new desired number of nodes, always in the range [1, maxAllowedNodes]. - */ - private int computeNbNodes(final int nbJobs) { - // Since nodes take a lot of system resourcess, we can't have too many. - // Here we apply a rule that there should be 1 node for every 5 jobs in - // the queue, up to the allowed maximum number of nodes. - return Math.min(1 + nbJobs / 5, maxAllowedNodes); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.example.adaptivegrid; + +import org.jppf.client.*; +import org.jppf.management.*; +import org.jppf.management.forwarding.JPPFNodeForwardingMBean; +import org.jppf.node.policy.*; +import org.jppf.utils.TypedProperties; + +/** + * This class encapsulates the functionality for managing a JPPF driver + * and its attached nodes via the JMX-based management APIs. + * @author Laurent Cohen + */ +public class DriverConnectionManager { + /** + * A proxy to the driver MBean which forwards management requests to the nodes. + */ + private JPPFNodeForwardingMBean forwarder; + /** + * The client connection pool holding the connections to the driver. + */ + private final JPPFConnectionPool connectionPool; + /** + * A node selector that only selects master nodes. + */ + private final NodeSelector masterSelector; + /** + * The maximum number of nodes that can be running on the host. + */ + private final int maxAllowedNodes; + /** + * The maximum allowed number of connections in the pool. + */ + private final int maxAllowedPoolSize; + /** + * The current number of nodes. We attempt to store and maintain it locally because + * obtaining it from the server is a costly operation that requires a remote method call. + */ + private int currentNodes; + + /** + * Initialize this manager with the specified JPPF client. + * @param client the client to use. + * @param maxAllowedNodes the maximum number of nodes that can be running on the host. + * @param maxAllowedPoolSize the maximum allowed number of connections in the pool. + * @throws Exception if any error occurs. + */ + public DriverConnectionManager(final JPPFClient client, final int maxAllowedNodes, final int maxAllowedPoolSize) throws Exception { + // wait until there is a connection pool with the at least one active connection to the driver + connectionPool = client.awaitActiveConnectionPool(); + // wait until at least one JMX connection wrapper is established + final JMXDriverConnectionWrapper jmx = connectionPool.awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); + this.forwarder = jmx.getNodeForwarder(); + // create a node selector that only selects master nodes + final ExecutionPolicy masterPolicy = new Equal("jppf.node.provisioning.master", true); + this.masterSelector = new ExecutionPolicySelector(masterPolicy); + this.maxAllowedNodes = maxAllowedNodes; + this.maxAllowedPoolSize = maxAllowedPoolSize; + this.currentNodes = 1; + } + + /** + * Update the connection pool and number of slave nodes based + * on the specified number of jobs to submit concurrently. + * @param nbJobs the number of jobs to submit. + */ + public void updateGridSetup(final int nbJobs) { + // Adjust the connection pool size + int newPoolSize = computePoolSize(nbJobs); + if (newPoolSize > maxAllowedPoolSize) newPoolSize = maxAllowedPoolSize; + final int currentPoolSize = connectionPool.connectionCount(); + if (newPoolSize != currentPoolSize) { + AdaptiveGridDemo.print("%screasing the number of server connections to %d", (newPoolSize > currentPoolSize) ? "in" : "de", newPoolSize); + connectionPool.setSize(newPoolSize); + // wait until all requested connections are established + connectionPool.awaitWorkingConnections(org.jppf.utils.Operator.EQUAL, newPoolSize); + } + + // Adjust the number of nodes + final int newNbNodes = computeNbNodes(nbJobs); + if (newNbNodes != currentNodes) { + AdaptiveGridDemo.print("%screasing the number of nodes to %d", (newNbNodes > currentNodes) ? "in" : "de", newNbNodes); + try { + // -1 because the master node is counted as a an execution node + updateSlaveNodes(newNbNodes - 1); + currentNodes = newNbNodes; + } catch(final Exception e) { + e.printStackTrace(); + // We don't know how many nodes were actually started, + // so we have to ask the server + try { + currentNodes = connectionPool.getJmxConnection().nbNodes(); + } catch(final Exception e2) { + e2.printStackTrace(); + } + } + } + } + + /** + * Update the number of running slave nodes. + * @param nbSlaves the number of slave nodes to reach. + * @param configOverrides optional overrides to the slave nodes' configuration. + * @throws Exception if any error occurs. + */ + private void updateSlaveNodes(final int nbSlaves, final TypedProperties configOverrides) throws Exception { + // request that slave nodes be provisioned + forwarder.provisionSlaveNodes(masterSelector, nbSlaves, configOverrides); + } + + /** + * Update the number of running slaves nodes, without configuration oveerides. + * @param nbSlaves the number of slave nodes to reach. + * @throws Exception if any error occurs. + */ + private void updateSlaveNodes(final int nbSlaves) throws Exception { + updateSlaveNodes(nbSlaves, null); + } + + /** + * Compute the desired connection pool size for the specified number of jobs. + * @param nbJobs the number of jobs to submit concurrently. + * @return the new size of the connection pool, in the range [1, maxAllowedPoolSize]. + */ + private int computePoolSize(final int nbJobs) { + // We apply a simple rule that makes the connection pool as large as the + // number of jobs, up to the maximum allowed pool size. + return Math.max(1, Math.min(nbJobs, maxAllowedPoolSize)); + } + + /** + * Compute the desired number of nodes for the specified number of jobs. + * @param nbJobs the number of jobs to submit concurrently. + * @return the new desired number of nodes, always in the range [1, maxAllowedNodes]. + */ + private int computeNbNodes(final int nbJobs) { + // Since nodes take a lot of system resourcess, we can't have too many. + // Here we apply a rule that there should be 1 node for every 5 jobs in + // the queue, up to the allowed maximum number of nodes. + return Math.min(1 + nbJobs / 5, maxAllowedNodes); + } +} diff --git a/samples-pack/ConcurrentJobs/src/org/jppf/example/concurrentjobs/ConcurrentJobs.java b/samples-pack/ConcurrentJobs/src/org/jppf/example/concurrentjobs/ConcurrentJobs.java index 4a1280d436..4b94aeb029 100644 --- a/samples-pack/ConcurrentJobs/src/org/jppf/example/concurrentjobs/ConcurrentJobs.java +++ b/samples-pack/ConcurrentJobs/src/org/jppf/example/concurrentjobs/ConcurrentJobs.java @@ -1,260 +1,260 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.example.concurrentjobs; - -import java.util.*; -import java.util.concurrent.*; - -import org.jppf.client.*; -import org.jppf.client.event.*; -import org.jppf.client.utils.AbstractJPPFJobStream; -import org.jppf.node.protocol.Task; -import org.jppf.utils.ExceptionUtils; - -/** - * An illustration of the patterns for submitting multiple jobs in parallel. - */ -public class ConcurrentJobs { - - /** - * Entry point for this demo. - * @param args the first argument is a function number that determines which method to call. - */ - public static void main(final String[] args) { - try { - // parse the function number from the first command line argument - final int function = Integer.parseInt(args[0]); - if ((function < 1) || (function > 4)) throw new IllegalArgumentException("function number must be between 1 and 4"); - final ConcurrentJobs runner = new ConcurrentJobs(); - // call the appropriate method based on the function number - switch(function) { - case 1: - runner.multipleThreadsBlockingJobs(); - break; - case 2: - runner.singleThreadNonBlockingJobs(); - break; - case 3: - runner.asynchronousNonBlockingJobs(); - break; - case 4: - runner.jobStreaming(); - break; - } - } catch (final Exception e) { - e.printStackTrace(); - } - } - - /** - * Execute multiple blocking jobs from multiple threads. - * @throws Exception if any error occurs. - */ - public void multipleThreadsBlockingJobs() throws Exception { - final int nbJobs = 4; - final ExecutorService executor = Executors.newFixedThreadPool(nbJobs); - try (final JPPFClient jppfClient = new JPPFClient()) { - // make sure the client has enough connections - ensureSufficientConnections(jppfClient, nbJobs); - final List> futures = new ArrayList<>(nbJobs); - // delegate the job submissions to separate threads - for (int i=1; i<=nbJobs; i++) { - final JPPFJob job = createJob("multipleThreadsBlockingJob " + i, 3, 1000L); - futures.add(executor.submit(new MyCallable(jppfClient, job))); - } - for (final Future future: futures) { - try { - final JPPFJob job = future.get(); - // process the job results - processResults(job); - } catch (final Exception e) { - e.printStackTrace(); - } - } - } finally { - executor.shutdown(); - } - } - - /** - * Submits a job and returns it after it completes. - */ - public static class MyCallable implements Callable { - /** - * The JPPF client which submits the job. - */ - private final JPPFClient jppfClient; - /** - * The job to submit. - */ - private final JPPFJob job; - - /** - * Initialize this callable. - * @param jppfClient the JPPF client which submits the job. - * @param job the job to submit. - */ - public MyCallable(final JPPFClient jppfClient, final JPPFJob job) { - this.jppfClient = jppfClient; - this.job = job; - } - - @Override - public JPPFJob call() throws Exception { - // submit the job, blocking until the job completes - jppfClient.submitJob(job); - // return the job once completed - return job; - } - } - - /** - * Submit multiple non-blocking jobs from a single thread, process the results in sequence. - * @throws Exception if any error occurs. - */ - public void singleThreadNonBlockingJobs() throws Exception { - final int nbJobs = 4; - try (final JPPFClient jppfClient = new JPPFClient()) { - // make sure the client has enough connections - ensureSufficientConnections(jppfClient, nbJobs); - final List jobs = new ArrayList<>(nbJobs); - for (int i=0; i> results = job.getAllResults(); - for (final Task task: results) { - if (task.getThrowable() != null) { // if the task execution raised an exception - System.out.printf("%s raised an exception : %s%n", task.getId(), ExceptionUtils.getMessage(task.getThrowable())); - } else { // otherwise display the task result - System.out.printf("result of %s : %s%n", task.getId(), task.getResult()); - } - } - } - - /** - * Ensure that the JPPF client has the specified number of connections. - * @param jppfClient the jppf client. - * @param nbConnections the desried number of connections. - * @throws Exception if any error occurs. - */ - private static void ensureSufficientConnections(final JPPFClient jppfClient, final int nbConnections) throws Exception { - // wait until a connection pool is available - final JPPFConnectionPool pool = jppfClient.awaitActiveConnectionPool(); - // make sure the pool has enough connections and wait until all connections are active - pool.awaitActiveConnections(Operator.AT_LEAST, nbConnections); - // alternatively with a single method call: wait until there is a connection pool with at least active connections, for as long as it takes - //jppfClient.awaitConnectionPools(Operator.AT_LEAST, nbConnections, Long.MAX_VALUE, JPPFClientConnectionStatus.ACTIVE); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.example.concurrentjobs; + +import java.util.*; +import java.util.concurrent.*; + +import org.jppf.client.*; +import org.jppf.client.event.*; +import org.jppf.client.utils.AbstractJPPFJobStream; +import org.jppf.node.protocol.Task; +import org.jppf.utils.ExceptionUtils; + +/** + * An illustration of the patterns for submitting multiple jobs in parallel. + */ +public class ConcurrentJobs { + + /** + * Entry point for this demo. + * @param args the first argument is a function number that determines which method to call. + */ + public static void main(final String[] args) { + try { + // parse the function number from the first command line argument + final int function = Integer.parseInt(args[0]); + if ((function < 1) || (function > 4)) throw new IllegalArgumentException("function number must be between 1 and 4"); + final ConcurrentJobs runner = new ConcurrentJobs(); + // call the appropriate method based on the function number + switch(function) { + case 1: + runner.multipleThreadsBlockingJobs(); + break; + case 2: + runner.singleThreadNonBlockingJobs(); + break; + case 3: + runner.asynchronousNonBlockingJobs(); + break; + case 4: + runner.jobStreaming(); + break; + } + } catch (final Exception e) { + e.printStackTrace(); + } + } + + /** + * Execute multiple blocking jobs from multiple threads. + * @throws Exception if any error occurs. + */ + public void multipleThreadsBlockingJobs() throws Exception { + final int nbJobs = 4; + final ExecutorService executor = Executors.newFixedThreadPool(nbJobs); + try (final JPPFClient jppfClient = new JPPFClient()) { + // make sure the client has enough connections + ensureSufficientConnections(jppfClient, nbJobs); + final List> futures = new ArrayList<>(nbJobs); + // delegate the job submissions to separate threads + for (int i=1; i<=nbJobs; i++) { + final JPPFJob job = createJob("multipleThreadsBlockingJob " + i, 3, 1000L); + futures.add(executor.submit(new MyCallable(jppfClient, job))); + } + for (final Future future: futures) { + try { + final JPPFJob job = future.get(); + // process the job results + processResults(job); + } catch (final Exception e) { + e.printStackTrace(); + } + } + } finally { + executor.shutdown(); + } + } + + /** + * Submits a job and returns it after it completes. + */ + public static class MyCallable implements Callable { + /** + * The JPPF client which submits the job. + */ + private final JPPFClient jppfClient; + /** + * The job to submit. + */ + private final JPPFJob job; + + /** + * Initialize this callable. + * @param jppfClient the JPPF client which submits the job. + * @param job the job to submit. + */ + public MyCallable(final JPPFClient jppfClient, final JPPFJob job) { + this.jppfClient = jppfClient; + this.job = job; + } + + @Override + public JPPFJob call() throws Exception { + // submit the job, blocking until the job completes + jppfClient.submitJob(job); + // return the job once completed + return job; + } + } + + /** + * Submit multiple non-blocking jobs from a single thread, process the results in sequence. + * @throws Exception if any error occurs. + */ + public void singleThreadNonBlockingJobs() throws Exception { + final int nbJobs = 4; + try (final JPPFClient jppfClient = new JPPFClient()) { + // make sure the client has enough connections + ensureSufficientConnections(jppfClient, nbJobs); + final List jobs = new ArrayList<>(nbJobs); + for (int i=0; i> results = job.getAllResults(); + for (final Task task: results) { + if (task.getThrowable() != null) { // if the task execution raised an exception + System.out.printf("%s raised an exception : %s%n", task.getId(), ExceptionUtils.getMessage(task.getThrowable())); + } else { // otherwise display the task result + System.out.printf("result of %s : %s%n", task.getId(), task.getResult()); + } + } + } + + /** + * Ensure that the JPPF client has the specified number of connections. + * @param jppfClient the jppf client. + * @param nbConnections the desried number of connections. + * @throws Exception if any error occurs. + */ + private static void ensureSufficientConnections(final JPPFClient jppfClient, final int nbConnections) throws Exception { + // wait until a connection pool is available + final JPPFConnectionPool pool = jppfClient.awaitActiveConnectionPool(); + // make sure the pool has enough connections and wait until all connections are active + pool.awaitActiveConnections(org.jppf.utils.Operator.AT_LEAST, nbConnections); + // alternatively with a single method call: wait until there is a connection pool with at least active connections, for as long as it takes + //jppfClient.awaitConnectionPools(Operator.AT_LEAST, nbConnections, Long.MAX_VALUE, JPPFClientConnectionStatus.ACTIVE); + } +} diff --git a/samples-pack/DataDependency/src/org/jppf/example/datadependency/AbstractTradeUpdater.java b/samples-pack/DataDependency/src/org/jppf/example/datadependency/AbstractTradeUpdater.java index e2a72737fe..0eb5de0a29 100644 --- a/samples-pack/DataDependency/src/org/jppf/example/datadependency/AbstractTradeUpdater.java +++ b/samples-pack/DataDependency/src/org/jppf/example/datadependency/AbstractTradeUpdater.java @@ -1,329 +1,329 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.example.datadependency; - -import java.util.*; -import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicLong; - -import org.jppf.client.*; -import org.jppf.example.datadependency.model.*; -import org.jppf.example.datadependency.simulation.*; -import org.jppf.management.*; -import org.jppf.node.policy.*; -import org.jppf.node.protocol.Task; -import org.jppf.utils.*; -import org.jppf.utils.concurrent.JPPFThreadFactory; -import org.slf4j.*; - -/** - * - * @author Laurent Cohen - */ -public abstract class AbstractTradeUpdater implements TickerListener, Runnable { - /** - * Logger for this class. - */ - private static Logger log = LoggerFactory.getLogger(AbstractTradeUpdater.class); - /** - * Debug enabled flag. - */ - private static boolean debugEnabled = LoggingUtils.isDebugEnabled(log); - /** - * The JPPF client, handles all communications with the server. - */ - protected static JPPFClient jppfClient = null; - /** - * The JPPF configuration. - */ - protected TypedProperties config = JPPFConfiguration.getProperties(); - /** - * Handles the distributed market data. - */ - protected MarketDataHandler marketDataHandler = new MarketDataHandler(); - /** - * The node selector. - */ - protected NodeSelector nodeSelector = null; - /** - * The generated list of market data objects. - */ - protected List marketDataList = null; - /** - * Associations between market data and trades. - */ - protected SortedMap> dataToTradeMap = new TreeMap<>(); - /** - * Executes the job submissions. - */ - protected ExecutorService jobExecutor = Executors.newFixedThreadPool(1, new JPPFThreadFactory("JobExecutor")); - /** - * Executes the results collection and processing. - */ - protected ExecutorService resultsExecutor = Executors.newFixedThreadPool(1, new JPPFThreadFactory("ResultsExecutor")); - /** - * Count of submitted jobs - used as part of their id. - */ - protected AtomicLong jobCount = new AtomicLong(0); - /** - * Collects statistics about the execution. - */ - protected StatsCollector statsCollector = new StatsCollector(); - /** - * Source of randomly generated data. - */ - protected DataFactory dataFactory = null; - /** - * Minimum task duration. - */ - protected int minTaskDuration = config.getInt("minTaskDuration", 1); - /** - * Maximum task duration. - */ - protected int maxTaskDuration = config.getInt("maxTaskDuration", 1); - - /** - * Default constructor. - */ - public AbstractTradeUpdater() { - } - - /** - * Generate and initialize data used in the simulation. - * @throws Exception if any error occurs. - */ - protected void initializeData() throws Exception { - final String s = config.getString("dataFactoryImpl", "uniform"); - if (s.equalsIgnoreCase("gaussian")) dataFactory = new GaussianDataFactory(); - else dataFactory = new UniformDataFactory(); - // generate random market data - marketDataList = dataFactory.generateDataMarketObjects(config.getInt("nbMarketData", 10)); - // generate random trades - final List tradeList = dataFactory.generateTradeObjects(config.getInt("nbTrades", 10), marketDataList, - config.getInt("minDataPerTrade", 1), config.getInt("maxDataPerTrade", 5)); - // initialize the nodes: collect their id and send them the current market data - marketDataHandler.populateMarketData(marketDataList); - final List idList = getNodeIds(); - nodeSelector = new NodeSelector(tradeList, idList); - for (Trade t: tradeList) { - for (String marketDataId: t.getDataDependencies()) { - Set tradeSet = dataToTradeMap.get(marketDataId); - if (tradeSet == null) { - tradeSet = new HashSet<>(); - dataToTradeMap.put(marketDataId, tradeSet); - } - tradeSet.add(t.getId()); - } - } - } - - /** - * Get the uuids of all nodes using the management APIs. - * @return the ids as a list of strings. - * @throws Exception if any error occurs. - */ - protected List getNodeIds() throws Exception { - final JMXDriverConnectionWrapper driver = jppfClient.awaitWorkingConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - final Collection nodesInfo = driver.nodesInformation(); - final List idList = new ArrayList<>(nodesInfo.size()); - for (JPPFManagementInfo info: nodesInfo) idList.add(info.getUuid()); - return idList; - } - - /** - * Print a message to the log and to the system standard output. - * @param s the message to print. - */ - protected void print(final String s) { - System.out.println(s); - log.info(s); - } - - /** - * Task whose role is to submit a number of jobs determined when a market datum is updated. - */ - public class SubmissionTask implements Runnable { - /** - * The updated market data. - */ - MarketData[] marketData = null; - /** - * Event notification timestamp. - */ - long timestamp = System.currentTimeMillis(); - - /** - * Initialize this task with the specified market data. - * @param marketData the updated market data. - */ - public SubmissionTask(final MarketData...marketData) { - this.marketData = marketData; - } - - /** - * Submits a job. - */ - @Override - public void run() { - try { - if (debugEnabled) log.debug("processing update for " + Arrays.toString(marketData)); - // determine which trades are impacted - final Set tradeIdSet = new HashSet<>(); - for (final MarketData md: marketData) { - final Set set = dataToTradeMap.get(md.getId()); - if (set != null) tradeIdSet.addAll(set); - } - if (tradeIdSet.isEmpty()) return; - // associate each node with a list of impacted trades - final Map> nodeMap = new HashMap<>(); - for (final String tradeId: tradeIdSet) { - final String nodeId = nodeSelector.getNodeId(tradeId); - List list = nodeMap.get(nodeId); - if (list == null) { - list = new ArrayList<>(); - nodeMap.put(nodeId, list); - } - list.add(tradeId); - } - // create a job for each node - for (final Map.Entry> entry : nodeMap.entrySet()) { - final List list = entry.getValue(); - submitOneJobPerNode(entry.getKey(), list); - //submitOneJobPerTrade(nodeId, list); - } - } catch(final Exception e) { - System.out.println(e.getMessage()); - log.error(e.getMessage(), e); - } - } - - /** - * This method submits one job per node, each job comprising as many tasks - * as there are trades to recompute on this node. - * @param nodeId the id of the node on which to execute the job. - * @param tradeIdList the list of impacted trades to recompute on the node. - * @throws Exception if any error occurs. - */ - private void submitOneJobPerNode(final String nodeId, final List tradeIdList) throws Exception { - final JPPFJob job = new JPPFJob(); - job.setName("Job (" + jobCount.incrementAndGet() + ")"); - job.setBlocking(false); - // set an execution policy that forces execution on the node with the specified id - job.getSLA().setExecutionPolicy(new Equal("jppf.uuid", false, nodeId)); - // create a task for each trade - for (String tradeId: tradeIdList) job.add(createTask(tradeId)); - jppfClient.submitJob(job); - resultsExecutor.execute(new ResultCollectionTask(job, timestamp)); - } - - /** - * This method submits one job per trade, each job comprising a single task - * that recomputes a single trade. - * @param nodeId the id of the node on which to execute the job. - * @param tradeIdList the list of impacted trades to recompute on the node. - * @throws Exception if any error occurs. - */ - void submitOneJobPerTrade(final String nodeId, final List tradeIdList) throws Exception { - final ExecutionPolicy policy = new Equal("jppf.uuid", false, nodeId); - // create a job for each trade - for (final String tradeId: tradeIdList) { - final JPPFJob job = new JPPFJob(); - job.setName("[Node id=" + nodeId + "] trade=" + tradeId + " (" + jobCount.incrementAndGet() + ")"); - job.setBlocking(false); - // set an execution policy that forces execution on the node with the specified id - job.getSLA().setExecutionPolicy(policy); - job.add(createTask(tradeId)); - jppfClient.submitJob(job); - resultsExecutor.execute(new ResultCollectionTask(job, timestamp)); - } - } - - /** - * Create a trade update task from the specified trade. - * @param tradeId the trade t recompute. - * @return a new TradeUpdateTask instance. - */ - private TradeUpdateTask createTask(final String tradeId) { - final TradeUpdateTask task = new TradeUpdateTask(tradeId); - task.setTaskDuration(dataFactory.getRandomInt(minTaskDuration, maxTaskDuration)); - return task; - } - } - - /** - * Task whose role is to submit a number of jobs determined when a market datum is updated. - */ - public class ResultCollectionTask implements Runnable { - /** - * The job from which to get the results. - */ - private JPPFJob job; - /** - * Event notification timestamp. - */ - private long timestamp; - - /** - * Initialize this task with the specified parameter. - * @param job the job from which to get the results. - * @param timestamp the event notification timestamp. - */ - public ResultCollectionTask(final JPPFJob job, final long timestamp) { - this.job = job; - this.timestamp = timestamp; - } - - /** - * Process a set of results. - */ - @Override - public void run() { - try { - final List> results = job.awaitResults(); - // do something with the results? - final StringBuilder sb = new StringBuilder("Updated trades: "); - for (int i=0; i 0) sb.append(", "); - final TradeUpdateTask task = (TradeUpdateTask) results.get(i); - sb.append(task.getTradeId()); - } - final long time = System.currentTimeMillis() - timestamp; - sb.append('[').append(time).append(" ms]"); - log.info(sb.toString()); - statsCollector.jobProcessed(results, time); - } catch(final Exception e) { - e.printStackTrace(); - } - } - } - - /** - * Start the jppf client instance. - */ - public static void openJPPFClient() { - jppfClient = new JPPFClient(); - } - - /** - * Close the jppf client. - */ - public static void closeJPPFClient() { - if (jppfClient != null) jppfClient.close(); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.example.datadependency; + +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicLong; + +import org.jppf.client.*; +import org.jppf.example.datadependency.model.*; +import org.jppf.example.datadependency.simulation.*; +import org.jppf.management.*; +import org.jppf.node.policy.*; +import org.jppf.node.protocol.Task; +import org.jppf.utils.*; +import org.jppf.utils.concurrent.JPPFThreadFactory; +import org.slf4j.*; + +/** + * + * @author Laurent Cohen + */ +public abstract class AbstractTradeUpdater implements TickerListener, Runnable { + /** + * Logger for this class. + */ + private static Logger log = LoggerFactory.getLogger(AbstractTradeUpdater.class); + /** + * Debug enabled flag. + */ + private static boolean debugEnabled = LoggingUtils.isDebugEnabled(log); + /** + * The JPPF client, handles all communications with the server. + */ + protected static JPPFClient jppfClient = null; + /** + * The JPPF configuration. + */ + protected TypedProperties config = JPPFConfiguration.getProperties(); + /** + * Handles the distributed market data. + */ + protected MarketDataHandler marketDataHandler = new MarketDataHandler(); + /** + * The node selector. + */ + protected NodeSelector nodeSelector = null; + /** + * The generated list of market data objects. + */ + protected List marketDataList = null; + /** + * Associations between market data and trades. + */ + protected SortedMap> dataToTradeMap = new TreeMap<>(); + /** + * Executes the job submissions. + */ + protected ExecutorService jobExecutor = Executors.newFixedThreadPool(1, new JPPFThreadFactory("JobExecutor")); + /** + * Executes the results collection and processing. + */ + protected ExecutorService resultsExecutor = Executors.newFixedThreadPool(1, new JPPFThreadFactory("ResultsExecutor")); + /** + * Count of submitted jobs - used as part of their id. + */ + protected AtomicLong jobCount = new AtomicLong(0); + /** + * Collects statistics about the execution. + */ + protected StatsCollector statsCollector = new StatsCollector(); + /** + * Source of randomly generated data. + */ + protected DataFactory dataFactory = null; + /** + * Minimum task duration. + */ + protected int minTaskDuration = config.getInt("minTaskDuration", 1); + /** + * Maximum task duration. + */ + protected int maxTaskDuration = config.getInt("maxTaskDuration", 1); + + /** + * Default constructor. + */ + public AbstractTradeUpdater() { + } + + /** + * Generate and initialize data used in the simulation. + * @throws Exception if any error occurs. + */ + protected void initializeData() throws Exception { + final String s = config.getString("dataFactoryImpl", "uniform"); + if (s.equalsIgnoreCase("gaussian")) dataFactory = new GaussianDataFactory(); + else dataFactory = new UniformDataFactory(); + // generate random market data + marketDataList = dataFactory.generateDataMarketObjects(config.getInt("nbMarketData", 10)); + // generate random trades + final List tradeList = dataFactory.generateTradeObjects(config.getInt("nbTrades", 10), marketDataList, + config.getInt("minDataPerTrade", 1), config.getInt("maxDataPerTrade", 5)); + // initialize the nodes: collect their id and send them the current market data + marketDataHandler.populateMarketData(marketDataList); + final List idList = getNodeIds(); + nodeSelector = new NodeSelector(tradeList, idList); + for (Trade t: tradeList) { + for (String marketDataId: t.getDataDependencies()) { + Set tradeSet = dataToTradeMap.get(marketDataId); + if (tradeSet == null) { + tradeSet = new HashSet<>(); + dataToTradeMap.put(marketDataId, tradeSet); + } + tradeSet.add(t.getId()); + } + } + } + + /** + * Get the uuids of all nodes using the management APIs. + * @return the ids as a list of strings. + * @throws Exception if any error occurs. + */ + protected List getNodeIds() throws Exception { + final JMXDriverConnectionWrapper driver = jppfClient.awaitWorkingConnectionPool().awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); + final Collection nodesInfo = driver.nodesInformation(); + final List idList = new ArrayList<>(nodesInfo.size()); + for (JPPFManagementInfo info: nodesInfo) idList.add(info.getUuid()); + return idList; + } + + /** + * Print a message to the log and to the system standard output. + * @param s the message to print. + */ + protected void print(final String s) { + System.out.println(s); + log.info(s); + } + + /** + * Task whose role is to submit a number of jobs determined when a market datum is updated. + */ + public class SubmissionTask implements Runnable { + /** + * The updated market data. + */ + MarketData[] marketData = null; + /** + * Event notification timestamp. + */ + long timestamp = System.currentTimeMillis(); + + /** + * Initialize this task with the specified market data. + * @param marketData the updated market data. + */ + public SubmissionTask(final MarketData...marketData) { + this.marketData = marketData; + } + + /** + * Submits a job. + */ + @Override + public void run() { + try { + if (debugEnabled) log.debug("processing update for " + Arrays.toString(marketData)); + // determine which trades are impacted + final Set tradeIdSet = new HashSet<>(); + for (final MarketData md: marketData) { + final Set set = dataToTradeMap.get(md.getId()); + if (set != null) tradeIdSet.addAll(set); + } + if (tradeIdSet.isEmpty()) return; + // associate each node with a list of impacted trades + final Map> nodeMap = new HashMap<>(); + for (final String tradeId: tradeIdSet) { + final String nodeId = nodeSelector.getNodeId(tradeId); + List list = nodeMap.get(nodeId); + if (list == null) { + list = new ArrayList<>(); + nodeMap.put(nodeId, list); + } + list.add(tradeId); + } + // create a job for each node + for (final Map.Entry> entry : nodeMap.entrySet()) { + final List list = entry.getValue(); + submitOneJobPerNode(entry.getKey(), list); + //submitOneJobPerTrade(nodeId, list); + } + } catch(final Exception e) { + System.out.println(e.getMessage()); + log.error(e.getMessage(), e); + } + } + + /** + * This method submits one job per node, each job comprising as many tasks + * as there are trades to recompute on this node. + * @param nodeId the id of the node on which to execute the job. + * @param tradeIdList the list of impacted trades to recompute on the node. + * @throws Exception if any error occurs. + */ + private void submitOneJobPerNode(final String nodeId, final List tradeIdList) throws Exception { + final JPPFJob job = new JPPFJob(); + job.setName("Job (" + jobCount.incrementAndGet() + ")"); + job.setBlocking(false); + // set an execution policy that forces execution on the node with the specified id + job.getSLA().setExecutionPolicy(new Equal("jppf.uuid", false, nodeId)); + // create a task for each trade + for (String tradeId: tradeIdList) job.add(createTask(tradeId)); + jppfClient.submitJob(job); + resultsExecutor.execute(new ResultCollectionTask(job, timestamp)); + } + + /** + * This method submits one job per trade, each job comprising a single task + * that recomputes a single trade. + * @param nodeId the id of the node on which to execute the job. + * @param tradeIdList the list of impacted trades to recompute on the node. + * @throws Exception if any error occurs. + */ + void submitOneJobPerTrade(final String nodeId, final List tradeIdList) throws Exception { + final ExecutionPolicy policy = new Equal("jppf.uuid", false, nodeId); + // create a job for each trade + for (final String tradeId: tradeIdList) { + final JPPFJob job = new JPPFJob(); + job.setName("[Node id=" + nodeId + "] trade=" + tradeId + " (" + jobCount.incrementAndGet() + ")"); + job.setBlocking(false); + // set an execution policy that forces execution on the node with the specified id + job.getSLA().setExecutionPolicy(policy); + job.add(createTask(tradeId)); + jppfClient.submitJob(job); + resultsExecutor.execute(new ResultCollectionTask(job, timestamp)); + } + } + + /** + * Create a trade update task from the specified trade. + * @param tradeId the trade t recompute. + * @return a new TradeUpdateTask instance. + */ + private TradeUpdateTask createTask(final String tradeId) { + final TradeUpdateTask task = new TradeUpdateTask(tradeId); + task.setTaskDuration(dataFactory.getRandomInt(minTaskDuration, maxTaskDuration)); + return task; + } + } + + /** + * Task whose role is to submit a number of jobs determined when a market datum is updated. + */ + public class ResultCollectionTask implements Runnable { + /** + * The job from which to get the results. + */ + private JPPFJob job; + /** + * Event notification timestamp. + */ + private long timestamp; + + /** + * Initialize this task with the specified parameter. + * @param job the job from which to get the results. + * @param timestamp the event notification timestamp. + */ + public ResultCollectionTask(final JPPFJob job, final long timestamp) { + this.job = job; + this.timestamp = timestamp; + } + + /** + * Process a set of results. + */ + @Override + public void run() { + try { + final List> results = job.awaitResults(); + // do something with the results? + final StringBuilder sb = new StringBuilder("Updated trades: "); + for (int i=0; i 0) sb.append(", "); + final TradeUpdateTask task = (TradeUpdateTask) results.get(i); + sb.append(task.getTradeId()); + } + final long time = System.currentTimeMillis() - timestamp; + sb.append('[').append(time).append(" ms]"); + log.info(sb.toString()); + statsCollector.jobProcessed(results, time); + } catch(final Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Start the jppf client instance. + */ + public static void openJPPFClient() { + jppfClient = new JPPFClient(); + } + + /** + * Close the jppf client. + */ + public static void closeJPPFClient() { + if (jppfClient != null) jppfClient.close(); + } +} diff --git a/samples-pack/FTPServer/src/org/jppf/example/ftp/runner/FTPRunner.java b/samples-pack/FTPServer/src/org/jppf/example/ftp/runner/FTPRunner.java index decce863ba..048c846516 100644 --- a/samples-pack/FTPServer/src/org/jppf/example/ftp/runner/FTPRunner.java +++ b/samples-pack/FTPServer/src/org/jppf/example/ftp/runner/FTPRunner.java @@ -1,94 +1,94 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jppf.example.ftp.runner; - -import java.util.List; - -import org.jppf.client.*; -import org.jppf.management.JMXDriverConnectionWrapper; -import org.jppf.node.protocol.*; -import org.jppf.utils.*; -import org.slf4j.*; - -/** - * Runner class for the matrix multiplication demo. - * @author Laurent Cohen - */ -public class FTPRunner { - /** - * Logger for this class. - */ - static Logger log = LoggerFactory.getLogger(FTPRunner.class); - /** - * JPPF client used to submit execution requests. - */ - private static JPPFClient jppfClient = null; - - /** - * Entry point for this class, performs a matrix multiplication a number of times.,
- * The number of times is specified as a configuration property named "matrix.iterations".
- * The size of the matrices is specified as a configuration property named "matrix.size".
- * @param args not used. - */ - public static void main(final String... args) { - try { - jppfClient = new JPPFClient(); - perform(); - } catch (final Exception e) { - e.printStackTrace(); - } finally { - if (jppfClient != null) jppfClient.close(); - } - } - - /** - * Perform the test. - * @throws Exception if an error is raised during the execution. - */ - private static void perform() throws Exception { - output("Running FTP demo"); - long totalTime = System.nanoTime(); - final JPPFJob job = new JPPFJob(); - job.setName("FTP server example job"); - // fetch the host from the JPPF client, so we don't have to hard-code it in the task. - final JMXDriverConnectionWrapper jmxDriver = jppfClient.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - final String host = jmxDriver.getHost(); - // store the host in a data provider - final DataProvider dataProvider = new MemoryMapDataProvider(); - dataProvider.setParameter("ftp.host", host); - job.setDataProvider(dataProvider); - // add a single task - job.add(new FTPTask("input.txt", "output.html")); - final List> results = jppfClient.submitJob(job); - for (final Task t : results) { - if (t.getThrowable() != null) System.out.println("task error: " + ExceptionUtils.getStackTrace(t.getThrowable())); - else System.out.println("task result: " + t.getResult()); - } - totalTime = System.nanoTime() - totalTime; - output("Computation time: " + StringUtils.toStringDuration(totalTime / 1000000L)); - } - - /** - * Print a message to the console and/or log file. - * @param message the message to print. - */ - private static void output(final String message) { - System.out.println(message); - log.info(message); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jppf.example.ftp.runner; + +import java.util.List; + +import org.jppf.client.*; +import org.jppf.management.JMXDriverConnectionWrapper; +import org.jppf.node.protocol.*; +import org.jppf.utils.*; +import org.slf4j.*; + +/** + * Runner class for the matrix multiplication demo. + * @author Laurent Cohen + */ +public class FTPRunner { + /** + * Logger for this class. + */ + static Logger log = LoggerFactory.getLogger(FTPRunner.class); + /** + * JPPF client used to submit execution requests. + */ + private static JPPFClient jppfClient = null; + + /** + * Entry point for this class, performs a matrix multiplication a number of times.,
+ * The number of times is specified as a configuration property named "matrix.iterations".
+ * The size of the matrices is specified as a configuration property named "matrix.size".
+ * @param args not used. + */ + public static void main(final String... args) { + try { + jppfClient = new JPPFClient(); + perform(); + } catch (final Exception e) { + e.printStackTrace(); + } finally { + if (jppfClient != null) jppfClient.close(); + } + } + + /** + * Perform the test. + * @throws Exception if an error is raised during the execution. + */ + private static void perform() throws Exception { + output("Running FTP demo"); + long totalTime = System.nanoTime(); + final JPPFJob job = new JPPFJob(); + job.setName("FTP server example job"); + // fetch the host from the JPPF client, so we don't have to hard-code it in the task. + final JMXDriverConnectionWrapper jmxDriver = jppfClient.awaitActiveConnectionPool().awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); + final String host = jmxDriver.getHost(); + // store the host in a data provider + final DataProvider dataProvider = new MemoryMapDataProvider(); + dataProvider.setParameter("ftp.host", host); + job.setDataProvider(dataProvider); + // add a single task + job.add(new FTPTask("input.txt", "output.html")); + final List> results = jppfClient.submitJob(job); + for (final Task t : results) { + if (t.getThrowable() != null) System.out.println("task error: " + ExceptionUtils.getStackTrace(t.getThrowable())); + else System.out.println("task result: " + t.getResult()); + } + totalTime = System.nanoTime() - totalTime; + output("Computation time: " + StringUtils.toStringDuration(totalTime / 1000000L)); + } + + /** + * Print a message to the console and/or log file. + * @param message the message to print. + */ + private static void output(final String message) { + System.out.println(message); + log.info(message); + } +} diff --git a/samples-pack/JobDependencies/src/org/jppf/example/job/dependencies/JobDependenciesRunner.java b/samples-pack/JobDependencies/src/org/jppf/example/job/dependencies/JobDependenciesRunner.java index 0bfc6f56ed..7c12b72068 100644 --- a/samples-pack/JobDependencies/src/org/jppf/example/job/dependencies/JobDependenciesRunner.java +++ b/samples-pack/JobDependencies/src/org/jppf/example/job/dependencies/JobDependenciesRunner.java @@ -1,109 +1,109 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.example.job.dependencies; - -import java.util.*; - -import org.jppf.client.*; -import org.jppf.client.event.*; -import org.jppf.node.protocol.Task; -import org.jppf.utils.ExceptionUtils; - -/** - * Run the dependencies managment sample. - * @author Laurent Cohen - */ -public class JobDependenciesRunner { - /** - * Job listener registered with each job to display a message when it completes. - */ - private static final JobListener JOB_LISTENER = new JobListenerAdapter() { - @Override - public synchronized void jobEnded(final JobEvent event) { - Utils.print("job_listener: '%s' has ended", event.getJob().getName()); - } - }; - - /** - * Entry point for the Job Dependencies demo. - * @param args not used. - */ - public static void main(final String[] args) { - try (JPPFClient client = new JPPFClient()) { - // read the jobs and their dependencies from the "./dependency_graph.txt" file - final List dependencies = Utils.readDependencies(); - - // ensure all jobs can be submitted concurrently by adjusting the connection pool size - final int n = Math.max(dependencies.size(), 1); - final JPPFConnectionPool pool = client.awaitWorkingConnectionPool(); - pool.setSize(n); - // wait until all connections are initialized - pool.awaitWorkingConnections(Operator.AT_LEAST, n); - - // Create the jobs according to the dependency graph - final List jobs = new ArrayList<>(); - for (final DependencySpec spec: dependencies) { - jobs.add(createJob(spec)); - } - - // submit all the jobs asynchronously - for (JPPFJob job: jobs) client.submitJob(job); - // await the jobs results and print them - for (JPPFJob job: jobs) printJobResults(job); - - } catch (final Exception e) { - e.printStackTrace(); - } - } - - /** - * Create a job with the specified dependencies. - * @param spec the dependencies specification for the job. - * @return the newly created job. - * @throws Exception if any error occurs. - */ - private static JPPFJob createJob(final DependencySpec spec) throws Exception { - final JPPFJob job = new JPPFJob(); - job.setName(spec.getId()); - // add the dependencies information to the job metadata - job.getMetadata().setParameter(DependencySpec.DEPENDENCIES_METADATA_KEY, spec); - // asynchronous job execution - job.setBlocking(false); - // the job MUST be suspended before submission - job.getSLA().setSuspended(true); - // add a single task and give it a readable id - job.add(new MyTask()).setId(spec.getId() + "-task"); - // the job listener is not required, we just want to print a message when a job completes on the client side - job.addJobListener(JOB_LISTENER); - return job; - } - - /** - * Print the results of a job, task by task. - * @param job the job whose results to print out. - */ - private static void printJobResults(final JPPFJob job) { - Utils.print("runner: ***** awaiting results for '%s' *****", job.getName()); - final List> results = job.awaitResults(); - for (final Task task : results) { - if (task.getThrowable() != null) Utils.print("runner: got exception: %s", ExceptionUtils.getStackTrace(task.getThrowable())); - else Utils.print("runner: got result: %s", task.getResult()); - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.example.job.dependencies; + +import java.util.*; + +import org.jppf.client.*; +import org.jppf.client.event.*; +import org.jppf.node.protocol.Task; +import org.jppf.utils.ExceptionUtils; + +/** + * Run the dependencies managment sample. + * @author Laurent Cohen + */ +public class JobDependenciesRunner { + /** + * Job listener registered with each job to display a message when it completes. + */ + private static final JobListener JOB_LISTENER = new JobListenerAdapter() { + @Override + public synchronized void jobEnded(final JobEvent event) { + Utils.print("job_listener: '%s' has ended", event.getJob().getName()); + } + }; + + /** + * Entry point for the Job Dependencies demo. + * @param args not used. + */ + public static void main(final String[] args) { + try (JPPFClient client = new JPPFClient()) { + // read the jobs and their dependencies from the "./dependency_graph.txt" file + final List dependencies = Utils.readDependencies(); + + // ensure all jobs can be submitted concurrently by adjusting the connection pool size + final int n = Math.max(dependencies.size(), 1); + final JPPFConnectionPool pool = client.awaitWorkingConnectionPool(); + pool.setSize(n); + // wait until all connections are initialized + pool.awaitWorkingConnections(org.jppf.utils.Operator.AT_LEAST, n); + + // Create the jobs according to the dependency graph + final List jobs = new ArrayList<>(); + for (final DependencySpec spec: dependencies) { + jobs.add(createJob(spec)); + } + + // submit all the jobs asynchronously + for (JPPFJob job: jobs) client.submitJob(job); + // await the jobs results and print them + for (JPPFJob job: jobs) printJobResults(job); + + } catch (final Exception e) { + e.printStackTrace(); + } + } + + /** + * Create a job with the specified dependencies. + * @param spec the dependencies specification for the job. + * @return the newly created job. + * @throws Exception if any error occurs. + */ + private static JPPFJob createJob(final DependencySpec spec) throws Exception { + final JPPFJob job = new JPPFJob(); + job.setName(spec.getId()); + // add the dependencies information to the job metadata + job.getMetadata().setParameter(DependencySpec.DEPENDENCIES_METADATA_KEY, spec); + // asynchronous job execution + job.setBlocking(false); + // the job MUST be suspended before submission + job.getSLA().setSuspended(true); + // add a single task and give it a readable id + job.add(new MyTask()).setId(spec.getId() + "-task"); + // the job listener is not required, we just want to print a message when a job completes on the client side + job.addJobListener(JOB_LISTENER); + return job; + } + + /** + * Print the results of a job, task by task. + * @param job the job whose results to print out. + */ + private static void printJobResults(final JPPFJob job) { + Utils.print("runner: ***** awaiting results for '%s' *****", job.getName()); + final List> results = job.awaitResults(); + for (final Task task : results) { + if (task.getThrowable() != null) Utils.print("runner: got exception: %s", ExceptionUtils.getStackTrace(task.getThrowable())); + else Utils.print("runner: got result: %s", task.getResult()); + } + } +} diff --git a/samples-pack/JobRecovery/src/org/jppf/example/jobrecovery/Runner.java b/samples-pack/JobRecovery/src/org/jppf/example/jobrecovery/Runner.java index 284ea39cde..85ee220c3b 100644 --- a/samples-pack/JobRecovery/src/org/jppf/example/jobrecovery/Runner.java +++ b/samples-pack/JobRecovery/src/org/jppf/example/jobrecovery/Runner.java @@ -1,141 +1,141 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.example.jobrecovery; - -import java.util.*; - -import org.jppf.client.*; -import org.jppf.client.persistence.*; -import org.jppf.load.balancer.LoadBalancingInformation; -import org.jppf.management.JMXDriverConnectionWrapper; -import org.jppf.node.protocol.Task; -import org.jppf.utils.*; -import org.jppf.utils.stats.JPPFStatisticsHelper; - -/** - * Demonstration of the job persistence API to implement jobs failover and recovery - * in the use case of an application crash before it completes. - * @author Laurent Cohen - */ -public class Runner { - /** - * The JPPF client. - */ - private static JPPFClient client = null; - - /** - * Entry point. - * @param args not used. - */ - public static void main(final String[] args) { - JobPersistence persistenceManager = null; - try { - client = new JPPFClient(); - // configure the driver so it behaves suitably for this demo - final int nbNodes = configureDriver(); - System.out.println("updated load-balancing settings, " + nbNodes + " nodes connected to the driver"); - // Create the persistence manager, the root path of the underlying store - // is in the folder "job_store" under the current directory - persistenceManager = new DefaultFilePersistenceManager("./job_store"); - final Collection keys = persistenceManager.allKeys(); - // if there is no job in the persistent store, - // we submit a job normally and simulate an application crash - if (keys.isEmpty()) { - final int nbTasks = 10 * nbNodes; - System.out.println("no job found in persistence store, creating a new job with " + nbTasks + " tasks"); - final JPPFJob job = new JPPFJob(); - // add 10 tasks per node, each task waiting for 1 second - for (int i = 0; i < nbTasks; i++) - job.add(new MyTask(1000L, i + 1)); - // set the persistence manager so the job will be persisted - // each time completed tasks are received from the driver - job.setPersistenceManager(persistenceManager); - // the application will exit after 6 seconds (simulated crash) - final Runnable quit = new Runnable() { - @Override - public void run() { - try { - Thread.sleep(6000L); - } catch (@SuppressWarnings("unused") final Exception e) { - } - System.exit(1); - } - }; - new Thread(quit).start(); - // meanwhile, start the job execution - executeJob(job); - } - // otherwise, if there are jobs in the persistence store, - // we load them and execute them on the grid - else { - System.out.println("found jobs in persistence store: " + keys); - for (final String key : keys) { - // load the job from the persistent store, using its key (= job uuid) - final JPPFJob job = persistenceManager.loadJob(key); - System.out.println("loaded job '" + key + "' from persistence store " + persistenceManager); - // don't forget this! the application may crash again - job.setPersistenceManager(persistenceManager); - // start the job execution, only non-completed tasks will be executed - executeJob(job); - // delete the persisted job after successful completion - persistenceManager.deleteJob(key); - } - } - } catch (final Exception e) { - e.printStackTrace(); - } finally { - if (client != null) client.close(); - if (persistenceManager != null) persistenceManager.close(); - } - } - - /** - * Execute the specified job. - * @param job the job to execute. - * @throws Exception if any error occurs. - */ - private static void executeJob(final JPPFJob job) throws Exception { - final List> results = client.submitJob(job); - for (final Task task : results) { - if (task.getThrowable() != null) System.out.println("task " + task.getId() + " exception occurred: " + ExceptionUtils.getStackTrace(task.getThrowable())); - else System.out.println("task " + task.getId() + " result: " + task.getResult()); - } - } - - /** - * This method updates the load balancer setting of the driver, - * to configure the "manual" algorithm with a size of 1. - * This means no more than one task will be sent to each node at any given time. - * @return the number of nodes connected to the driver. - * @throws Exception if any error occurs while configuring the driver. - */ - private static int configureDriver() throws Exception { - // get a connection to the driver's JMX server - final JMXDriverConnectionWrapper jmxDriver = client.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - // obtain the current load-balancing settings - final LoadBalancingInformation lbi = jmxDriver.loadBalancerInformation(); - if (lbi == null) return 1; - final TypedProperties props = lbi.getParameters(); - props.setProperty("size", "1"); - // set load-balancing algorithm to "manual" with a size of 1 - jmxDriver.changeLoadBalancerSettings("manual", props); - // return the current number of nodes - return (int) jmxDriver.statistics().getSnapshot(JPPFStatisticsHelper.NODES).getLatest(); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.example.jobrecovery; + +import java.util.*; + +import org.jppf.client.*; +import org.jppf.client.persistence.*; +import org.jppf.load.balancer.LoadBalancingInformation; +import org.jppf.management.JMXDriverConnectionWrapper; +import org.jppf.node.protocol.Task; +import org.jppf.utils.*; +import org.jppf.utils.stats.JPPFStatisticsHelper; + +/** + * Demonstration of the job persistence API to implement jobs failover and recovery + * in the use case of an application crash before it completes. + * @author Laurent Cohen + */ +public class Runner { + /** + * The JPPF client. + */ + private static JPPFClient client = null; + + /** + * Entry point. + * @param args not used. + */ + public static void main(final String[] args) { + JobPersistence persistenceManager = null; + try { + client = new JPPFClient(); + // configure the driver so it behaves suitably for this demo + final int nbNodes = configureDriver(); + System.out.println("updated load-balancing settings, " + nbNodes + " nodes connected to the driver"); + // Create the persistence manager, the root path of the underlying store + // is in the folder "job_store" under the current directory + persistenceManager = new DefaultFilePersistenceManager("./job_store"); + final Collection keys = persistenceManager.allKeys(); + // if there is no job in the persistent store, + // we submit a job normally and simulate an application crash + if (keys.isEmpty()) { + final int nbTasks = 10 * nbNodes; + System.out.println("no job found in persistence store, creating a new job with " + nbTasks + " tasks"); + final JPPFJob job = new JPPFJob(); + // add 10 tasks per node, each task waiting for 1 second + for (int i = 0; i < nbTasks; i++) + job.add(new MyTask(1000L, i + 1)); + // set the persistence manager so the job will be persisted + // each time completed tasks are received from the driver + job.setPersistenceManager(persistenceManager); + // the application will exit after 6 seconds (simulated crash) + final Runnable quit = new Runnable() { + @Override + public void run() { + try { + Thread.sleep(6000L); + } catch (@SuppressWarnings("unused") final Exception e) { + } + System.exit(1); + } + }; + new Thread(quit).start(); + // meanwhile, start the job execution + executeJob(job); + } + // otherwise, if there are jobs in the persistence store, + // we load them and execute them on the grid + else { + System.out.println("found jobs in persistence store: " + keys); + for (final String key : keys) { + // load the job from the persistent store, using its key (= job uuid) + final JPPFJob job = persistenceManager.loadJob(key); + System.out.println("loaded job '" + key + "' from persistence store " + persistenceManager); + // don't forget this! the application may crash again + job.setPersistenceManager(persistenceManager); + // start the job execution, only non-completed tasks will be executed + executeJob(job); + // delete the persisted job after successful completion + persistenceManager.deleteJob(key); + } + } + } catch (final Exception e) { + e.printStackTrace(); + } finally { + if (client != null) client.close(); + if (persistenceManager != null) persistenceManager.close(); + } + } + + /** + * Execute the specified job. + * @param job the job to execute. + * @throws Exception if any error occurs. + */ + private static void executeJob(final JPPFJob job) throws Exception { + final List> results = client.submitJob(job); + for (final Task task : results) { + if (task.getThrowable() != null) System.out.println("task " + task.getId() + " exception occurred: " + ExceptionUtils.getStackTrace(task.getThrowable())); + else System.out.println("task " + task.getId() + " result: " + task.getResult()); + } + } + + /** + * This method updates the load balancer setting of the driver, + * to configure the "manual" algorithm with a size of 1. + * This means no more than one task will be sent to each node at any given time. + * @return the number of nodes connected to the driver. + * @throws Exception if any error occurs while configuring the driver. + */ + private static int configureDriver() throws Exception { + // get a connection to the driver's JMX server + final JMXDriverConnectionWrapper jmxDriver = client.awaitActiveConnectionPool().awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); + // obtain the current load-balancing settings + final LoadBalancingInformation lbi = jmxDriver.loadBalancerInformation(); + if (lbi == null) return 1; + final TypedProperties props = lbi.getParameters(); + props.setProperty("size", "1"); + // set load-balancing algorithm to "manual" with a size of 1 + jmxDriver.changeLoadBalancerSettings("manual", props); + // return the current number of nodes + return (int) jmxDriver.statistics().getSnapshot(JPPFStatisticsHelper.NODES).getLatest(); + } +} diff --git a/samples-pack/NodeLifeCycle/src/org/jppf/example/nodelifecycle/client/DBRunner.java b/samples-pack/NodeLifeCycle/src/org/jppf/example/nodelifecycle/client/DBRunner.java index 24a3905829..3aa992bee1 100644 --- a/samples-pack/NodeLifeCycle/src/org/jppf/example/nodelifecycle/client/DBRunner.java +++ b/samples-pack/NodeLifeCycle/src/org/jppf/example/nodelifecycle/client/DBRunner.java @@ -1,166 +1,166 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jppf.example.nodelifecycle.client; - -import java.sql.*; -import java.util.Collection; - -import org.jppf.client.*; -import org.jppf.client.event.*; -import org.jppf.management.*; -import org.jppf.node.protocol.Task; -import org.jppf.utils.*; -import org.slf4j.*; - -/** - * Runner class for the square matrix multiplication demo. - * @author Laurent Cohen - */ -public class DBRunner { - /** - * Logger for this class. - */ - private static Logger log = LoggerFactory.getLogger(DBRunner.class); - /** - * JPPF client used to submit execution requests. - */ - private static JPPFClient jppfClient = null; - /** - * A JMX connection to one of the nodes. - */ - private static JMXNodeConnectionWrapper jmxNode = null; - - /*** - * Send a job with a number of tasks that insert a row in a database table. - * We use the management APIs to kill a node before the job execution is complete, - * so we can demonstrate the transaction recovery mechanism (implemented in Atomikos) - * on the node side. Once the job is complete, we display all the rows in the table. - * @param args the first argument, if any, will be used as the JPPF client's uuid. - */ - public static void main(final String... args) { - try { - final TypedProperties config = JPPFConfiguration.getProperties(); - final int nbTasks = config.getInt("job.nbtasks", 20); - final long taskSleepTime = config.getLong("task.sleep.time", 2000L); - final long timeBeforeRestartNode = config.getLong("time.before.restart.node", 6000L); - if ((args != null) && (args.length > 0)) jppfClient = new JPPFClient(args[0]); - else jppfClient = new JPPFClient(); - // Initialize the JMX connection to the node - getNode(); - // Create a job with the specified number of tasks - final JPPFJob job = new JPPFJob(); - job.setName("NodeLifeCycle demo job"); - for (int i = 1; i <= nbTasks; i++) { - final DBTask task = new DBTask(taskSleepTime); - task.setId("" + i); - job.add(task); - } - job.setBlocking(false); - // customize the result listener to display a message each time a task result is received - final JobListener jobListener = new JobListenerAdapter() { - @Override - public synchronized void jobReturned(final JobEvent event) { - for (Task task : event.getJobTasks()) { - if (task.getThrowable() != null) output("task " + task.getId() + " error: " + task.getThrowable().getMessage()); - else output("task " + task.getId() + " result: " + task.getResult()); - } - } - }; - job.addJobListener(jobListener); - jppfClient.submitJob(job); - Thread.sleep(timeBeforeRestartNode); - // restart the node to demonstrate the transaction recovery - output("restarting node"); - restartNode(); - // wait for the job completion - job.awaitResults(); - // display the list of rows in the DB table - if (config.getBoolean("display.db.content", false)) displayDBContent(); - output("demo ended"); - } catch (final Exception e) { - e.printStackTrace(); - } finally { - if (jppfClient != null) jppfClient.close(); - } - } - - /** - * Get a JMX connection to one of the nodes. - * @return the node connection as a {@link JMXNodeConnectionWrapper} instance. - * @throws Exception if the node connection could not be established. - */ - private static JMXNodeConnectionWrapper getNode() throws Exception { - if (jmxNode == null) { - final JMXDriverConnectionWrapper jmxDriver = jppfClient.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - final Collection nodesInfo = jmxDriver.nodesInformation(); - final JPPFManagementInfo info = nodesInfo.iterator().next(); - jmxNode = new JMXNodeConnectionWrapper(info.getHost(), info.getPort()); - jmxNode.connect(); - } - return jmxNode; - } - - /** - * Kill the node. - */ - private static void restartNode() { - try { - final JMXNodeConnectionWrapper jmxNode = getNode(); - jmxNode.restart(); - } catch (final Exception e) { - output("Could not restart a node:\n" + ExceptionUtils.getStackTrace(e)); - } - } - - /** - * List all the rows in the TASK_RESULT table. - * @throws Exception if any error occurs. - */ - private static void displayDBContent() throws Exception { - Class.forName("org.h2.Driver"); - final Connection c = DriverManager.getConnection("jdbc:h2:tcp://localhost:9092/./jppf_samples;SCHEMA=PUBLIC", "jppf", "jppf"); - //Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/jppf_samples", "jppf", "jppf"); - final String sql = "SELECT * FROM task_result"; - final Statement stmt = c.createStatement(); - final ResultSet rs = stmt.executeQuery(sql); - int count = 1; - output("\n***** displaying the DB table content *****"); - while (rs.next()) { - final StringBuilder sb = new StringBuilder(); - sb.append("row ").append(count).append(": "); - sb.append("id=").append(rs.getObject("id")); - sb.append(", task_id=").append(rs.getObject("task_id")); - sb.append(", message=").append(rs.getObject("message")); - output(sb.toString()); - count++; - } - output("***** end of DB table content *****"); - rs.close(); - stmt.close(); - c.close(); - } - - /** - * Print a message to the console and/or log file. - * @param message the message to print. - */ - private static void output(final String message) { - System.out.println(message); - log.info(message); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jppf.example.nodelifecycle.client; + +import java.sql.*; +import java.util.Collection; + +import org.jppf.client.*; +import org.jppf.client.event.*; +import org.jppf.management.*; +import org.jppf.node.protocol.Task; +import org.jppf.utils.*; +import org.slf4j.*; + +/** + * Runner class for the square matrix multiplication demo. + * @author Laurent Cohen + */ +public class DBRunner { + /** + * Logger for this class. + */ + private static Logger log = LoggerFactory.getLogger(DBRunner.class); + /** + * JPPF client used to submit execution requests. + */ + private static JPPFClient jppfClient = null; + /** + * A JMX connection to one of the nodes. + */ + private static JMXNodeConnectionWrapper jmxNode = null; + + /*** + * Send a job with a number of tasks that insert a row in a database table. + * We use the management APIs to kill a node before the job execution is complete, + * so we can demonstrate the transaction recovery mechanism (implemented in Atomikos) + * on the node side. Once the job is complete, we display all the rows in the table. + * @param args the first argument, if any, will be used as the JPPF client's uuid. + */ + public static void main(final String... args) { + try { + final TypedProperties config = JPPFConfiguration.getProperties(); + final int nbTasks = config.getInt("job.nbtasks", 20); + final long taskSleepTime = config.getLong("task.sleep.time", 2000L); + final long timeBeforeRestartNode = config.getLong("time.before.restart.node", 6000L); + if ((args != null) && (args.length > 0)) jppfClient = new JPPFClient(args[0]); + else jppfClient = new JPPFClient(); + // Initialize the JMX connection to the node + getNode(); + // Create a job with the specified number of tasks + final JPPFJob job = new JPPFJob(); + job.setName("NodeLifeCycle demo job"); + for (int i = 1; i <= nbTasks; i++) { + final DBTask task = new DBTask(taskSleepTime); + task.setId("" + i); + job.add(task); + } + job.setBlocking(false); + // customize the result listener to display a message each time a task result is received + final JobListener jobListener = new JobListenerAdapter() { + @Override + public synchronized void jobReturned(final JobEvent event) { + for (Task task : event.getJobTasks()) { + if (task.getThrowable() != null) output("task " + task.getId() + " error: " + task.getThrowable().getMessage()); + else output("task " + task.getId() + " result: " + task.getResult()); + } + } + }; + job.addJobListener(jobListener); + jppfClient.submitJob(job); + Thread.sleep(timeBeforeRestartNode); + // restart the node to demonstrate the transaction recovery + output("restarting node"); + restartNode(); + // wait for the job completion + job.awaitResults(); + // display the list of rows in the DB table + if (config.getBoolean("display.db.content", false)) displayDBContent(); + output("demo ended"); + } catch (final Exception e) { + e.printStackTrace(); + } finally { + if (jppfClient != null) jppfClient.close(); + } + } + + /** + * Get a JMX connection to one of the nodes. + * @return the node connection as a {@link JMXNodeConnectionWrapper} instance. + * @throws Exception if the node connection could not be established. + */ + private static JMXNodeConnectionWrapper getNode() throws Exception { + if (jmxNode == null) { + final JMXDriverConnectionWrapper jmxDriver = jppfClient.awaitActiveConnectionPool().awaitJMXConnections(org.jppf.utils.Operator.AT_LEAST, 1, true).get(0); + final Collection nodesInfo = jmxDriver.nodesInformation(); + final JPPFManagementInfo info = nodesInfo.iterator().next(); + jmxNode = new JMXNodeConnectionWrapper(info.getHost(), info.getPort()); + jmxNode.connect(); + } + return jmxNode; + } + + /** + * Kill the node. + */ + private static void restartNode() { + try { + final JMXNodeConnectionWrapper jmxNode = getNode(); + jmxNode.restart(); + } catch (final Exception e) { + output("Could not restart a node:\n" + ExceptionUtils.getStackTrace(e)); + } + } + + /** + * List all the rows in the TASK_RESULT table. + * @throws Exception if any error occurs. + */ + private static void displayDBContent() throws Exception { + Class.forName("org.h2.Driver"); + final Connection c = DriverManager.getConnection("jdbc:h2:tcp://localhost:9092/./jppf_samples;SCHEMA=PUBLIC", "jppf", "jppf"); + //Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/jppf_samples", "jppf", "jppf"); + final String sql = "SELECT * FROM task_result"; + final Statement stmt = c.createStatement(); + final ResultSet rs = stmt.executeQuery(sql); + int count = 1; + output("\n***** displaying the DB table content *****"); + while (rs.next()) { + final StringBuilder sb = new StringBuilder(); + sb.append("row ").append(count).append(": "); + sb.append("id=").append(rs.getObject("id")); + sb.append(", task_id=").append(rs.getObject("task_id")); + sb.append(", message=").append(rs.getObject("message")); + output(sb.toString()); + count++; + } + output("***** end of DB table content *****"); + rs.close(); + stmt.close(); + c.close(); + } + + /** + * Print a message to the console and/or log file. + * @param message the message to print. + */ + private static void output(final String message) { + System.out.println(message); + log.info(message); + } +} diff --git a/stress-tests/src/framework/org/jppf/test/setup/Setup.java b/stress-tests/src/framework/org/jppf/test/setup/Setup.java index f0aa5d5c9e..a068d6e46a 100644 --- a/stress-tests/src/framework/org/jppf/test/setup/Setup.java +++ b/stress-tests/src/framework/org/jppf/test/setup/Setup.java @@ -1,235 +1,236 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jppf.test.setup; - -import java.util.concurrent.atomic.AtomicInteger; - -import org.jppf.client.*; -import org.jppf.client.event.ConnectionPoolListener; -import org.jppf.management.JMXDriverConnectionWrapper; -import org.jppf.server.job.management.DriverJobManagementMBean; -import org.jppf.test.scenario.ScenarioConfiguration; -import org.jppf.utils.JPPFConfiguration; - -import test.org.jppf.test.setup.ConfigurationHelper; - -/** - * Helper object for setting up and cleaning the environment before and after testing. - * @author Laurent Cohen - */ -public class Setup { - /** - * The jppf client to use. - */ - protected JPPFClient client = null; - /** - * The nodes to launch for the test. - */ - protected RestartableNodeProcessLauncher[] nodes = null; - /** - * The drivers to launch for the test. - */ - protected RestartableDriverProcessLauncher[] drivers = null; - /** - * Shutdown hook used to destroy the driver and node processes, in case the JVM terminates abnormally. - */ - protected Thread shutdownHook = null; - /** - * The configuration of the scenario to run. - */ - protected final ScenarioConfiguration config; - /** - * Manages the JMX connections to ddrivers and nodes. - */ - private JMXHandler jmxHandler = null; - /** - * - */ - private AtomicInteger clientCount = new AtomicInteger(0); - - /** - * Initialize this tests etup with the psecified scenario configuration. - * @param config the configuration of the scenario to run. - */ - public Setup(final ScenarioConfiguration config) { - this.config = config; - } - - /** - * Get a proxy to the job management MBean. - * @return an instance of DriverJobManagementMBean. - * @throws Exception if the proxy could not be obtained. - */ - public DriverJobManagementMBean getJobManagementProxy() throws Exception { - final JMXDriverConnectionWrapper driver = getDriverManagementProxy(); - return driver.getJobManager(); - } - - /** - * Get a proxy to the driver admin MBean. - * @return an instance of DriverJobManagementMBean. - * @throws Exception if the proxy could not be obtained. - */ - public JMXDriverConnectionWrapper getDriverManagementProxy() throws Exception { - return client.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); - } - - /** - * Launches a driver and node and start the client. - * @param nbDrivers the number of drivers to launch. - * @param nbNodes the number of nodes to launch. - * @return an instance of JPPFClient. - * @throws Exception if a process could not be started. - */ - public JPPFClient setup(final int nbDrivers, final int nbNodes) throws Exception { - System.out.println("performing setup with " + nbDrivers + " drivers, " + nbNodes + " nodes and 1 client"); - createShutdownHook(); - drivers = new RestartableDriverProcessLauncher[nbDrivers]; - for (int i = 0; i < nbDrivers; i++) { - drivers[i] = new RestartableDriverProcessLauncher(i + 1, config); - new Thread(drivers[i], drivers[i].getName() + "process launcher").start(); - } - nodes = new RestartableNodeProcessLauncher[nbNodes]; - for (int i = 0; i < nbNodes; i++) { - nodes[i] = new RestartableNodeProcessLauncher(i + 1, config); - new Thread(nodes[i], nodes[i].getName() + "process launcher").start(); - } - if (config.isStartClient()) { - client = createClient("c" + clientCount.incrementAndGet(), true); - if (config.getProperties().getBoolean("jppf.scenario.jmx.checks.enabled", true)) jmxHandler.checkDriverAndNodesInitialized(nbDrivers, nbNodes); - } - return client; - } - - /** - * Create a client with the specified uuid. - * @param uuid if null, let the client generate its uuid. - * @param reset if true, the JPPF ocnfiguration is reloaded. - * @param listener an optional {@link ConnectionPoolListener}. - * @return a JPPFClient instance. - * @throws Exception if any error occurs. - */ - public JPPFClient createClient(final String uuid, final boolean reset, final ConnectionPoolListener listener) throws Exception { - if (reset) JPPFConfiguration.reset(); - client = (uuid == null) ? new JPPFClient(listener) : new JPPFClient(uuid, listener); - while (!client.hasAvailableConnection()) - Thread.sleep(10L); - jmxHandler = new JMXHandler(client); - return client; - } - - /** - * Create a client with the specified uuid. - * @param uuid if null, let the client generate its uuid. - * @param reset if true, the JPPF ocnfiguration is reloaded. - * @return a JPPFClient instance. - * @throws Exception if any error occurs. - */ - public JPPFClient createClient(final String uuid, final boolean reset) throws Exception { - return createClient(uuid, reset, null); - } - - /** - * Stops the driver and node and close the client. - * @throws Exception if a process could not be stopped. - */ - public void cleanup() throws Exception { - if (client != null) { - client.close(); - client = null; - jmxHandler = null; - Thread.sleep(500L); - } - System.gc(); - stopProcesses(); - ConfigurationHelper.cleanup(); - if (shutdownHook != null) Runtime.getRuntime().removeShutdownHook(shutdownHook); - } - - /** - * Stop driver and node processes. - */ - protected void stopProcesses() { - try { - if (nodes != null) for (RestartableNodeProcessLauncher n: nodes) - if (n != null) n.stopProcess(); - if (drivers != null) for (RestartableDriverProcessLauncher d: drivers) - if (d != null) d.stopProcess(); - } catch (final Throwable t) { - t.printStackTrace(); - } finally { - nodes = null; - drivers = null; - } - } - - /** - * Create the shutdown hook. - */ - protected void createShutdownHook() { - shutdownHook = new Thread() { - @Override - public void run() { - stopProcesses(); - ConfigurationHelper.cleanup(); - } - }; - Runtime.getRuntime().addShutdownHook(shutdownHook); - } - - /** - * Get the jppf client to use. - * @return a {@link JPPFClient} instance. - */ - public JPPFClient getClient() { - return client; - } - - /** - * Set the jppf client to use. - * @param client a {@link JPPFClient} instance. - */ - public void setClient(final JPPFClient client) { - this.client = client; - } - - /** - * Get the nodes to launch for the test. - * @return an array of NodeProcessLauncher instances. - */ - public RestartableNodeProcessLauncher[] getNodes() { - return nodes; - } - - /** - * Get the drivers to launch for the test. - * @return an array of DriverProcessLauncher instances. - */ - public RestartableDriverProcessLauncher[] getDrivers() { - return drivers; - } - - /** - * Get the object which manages the JMX connections to ddrivers and nodes. - * @return a {@link JMXHandler} instance. - */ - public JMXHandler getJmxHandler() { - return jmxHandler; - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jppf.test.setup; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.jppf.client.*; +import org.jppf.client.event.ConnectionPoolListener; +import org.jppf.management.JMXDriverConnectionWrapper; +import org.jppf.server.job.management.DriverJobManagementMBean; +import org.jppf.test.scenario.ScenarioConfiguration; +import org.jppf.utils.JPPFConfiguration; +import org.jppf.utils.Operator; + +import test.org.jppf.test.setup.ConfigurationHelper; + +/** + * Helper object for setting up and cleaning the environment before and after testing. + * @author Laurent Cohen + */ +public class Setup { + /** + * The jppf client to use. + */ + protected JPPFClient client = null; + /** + * The nodes to launch for the test. + */ + protected RestartableNodeProcessLauncher[] nodes = null; + /** + * The drivers to launch for the test. + */ + protected RestartableDriverProcessLauncher[] drivers = null; + /** + * Shutdown hook used to destroy the driver and node processes, in case the JVM terminates abnormally. + */ + protected Thread shutdownHook = null; + /** + * The configuration of the scenario to run. + */ + protected final ScenarioConfiguration config; + /** + * Manages the JMX connections to ddrivers and nodes. + */ + private JMXHandler jmxHandler = null; + /** + * + */ + private AtomicInteger clientCount = new AtomicInteger(0); + + /** + * Initialize this tests etup with the psecified scenario configuration. + * @param config the configuration of the scenario to run. + */ + public Setup(final ScenarioConfiguration config) { + this.config = config; + } + + /** + * Get a proxy to the job management MBean. + * @return an instance of DriverJobManagementMBean. + * @throws Exception if the proxy could not be obtained. + */ + public DriverJobManagementMBean getJobManagementProxy() throws Exception { + final JMXDriverConnectionWrapper driver = getDriverManagementProxy(); + return driver.getJobManager(); + } + + /** + * Get a proxy to the driver admin MBean. + * @return an instance of DriverJobManagementMBean. + * @throws Exception if the proxy could not be obtained. + */ + public JMXDriverConnectionWrapper getDriverManagementProxy() throws Exception { + return client.awaitActiveConnectionPool().awaitJMXConnections(Operator.AT_LEAST, 1, true).get(0); + } + + /** + * Launches a driver and node and start the client. + * @param nbDrivers the number of drivers to launch. + * @param nbNodes the number of nodes to launch. + * @return an instance of JPPFClient. + * @throws Exception if a process could not be started. + */ + public JPPFClient setup(final int nbDrivers, final int nbNodes) throws Exception { + System.out.println("performing setup with " + nbDrivers + " drivers, " + nbNodes + " nodes and 1 client"); + createShutdownHook(); + drivers = new RestartableDriverProcessLauncher[nbDrivers]; + for (int i = 0; i < nbDrivers; i++) { + drivers[i] = new RestartableDriverProcessLauncher(i + 1, config); + new Thread(drivers[i], drivers[i].getName() + "process launcher").start(); + } + nodes = new RestartableNodeProcessLauncher[nbNodes]; + for (int i = 0; i < nbNodes; i++) { + nodes[i] = new RestartableNodeProcessLauncher(i + 1, config); + new Thread(nodes[i], nodes[i].getName() + "process launcher").start(); + } + if (config.isStartClient()) { + client = createClient("c" + clientCount.incrementAndGet(), true); + if (config.getProperties().getBoolean("jppf.scenario.jmx.checks.enabled", true)) jmxHandler.checkDriverAndNodesInitialized(nbDrivers, nbNodes); + } + return client; + } + + /** + * Create a client with the specified uuid. + * @param uuid if null, let the client generate its uuid. + * @param reset if true, the JPPF ocnfiguration is reloaded. + * @param listener an optional {@link ConnectionPoolListener}. + * @return a JPPFClient instance. + * @throws Exception if any error occurs. + */ + public JPPFClient createClient(final String uuid, final boolean reset, final ConnectionPoolListener listener) throws Exception { + if (reset) JPPFConfiguration.reset(); + client = (uuid == null) ? new JPPFClient(listener) : new JPPFClient(uuid, listener); + while (!client.hasAvailableConnection()) + Thread.sleep(10L); + jmxHandler = new JMXHandler(client); + return client; + } + + /** + * Create a client with the specified uuid. + * @param uuid if null, let the client generate its uuid. + * @param reset if true, the JPPF ocnfiguration is reloaded. + * @return a JPPFClient instance. + * @throws Exception if any error occurs. + */ + public JPPFClient createClient(final String uuid, final boolean reset) throws Exception { + return createClient(uuid, reset, null); + } + + /** + * Stops the driver and node and close the client. + * @throws Exception if a process could not be stopped. + */ + public void cleanup() throws Exception { + if (client != null) { + client.close(); + client = null; + jmxHandler = null; + Thread.sleep(500L); + } + System.gc(); + stopProcesses(); + ConfigurationHelper.cleanup(); + if (shutdownHook != null) Runtime.getRuntime().removeShutdownHook(shutdownHook); + } + + /** + * Stop driver and node processes. + */ + protected void stopProcesses() { + try { + if (nodes != null) for (RestartableNodeProcessLauncher n: nodes) + if (n != null) n.stopProcess(); + if (drivers != null) for (RestartableDriverProcessLauncher d: drivers) + if (d != null) d.stopProcess(); + } catch (final Throwable t) { + t.printStackTrace(); + } finally { + nodes = null; + drivers = null; + } + } + + /** + * Create the shutdown hook. + */ + protected void createShutdownHook() { + shutdownHook = new Thread() { + @Override + public void run() { + stopProcesses(); + ConfigurationHelper.cleanup(); + } + }; + Runtime.getRuntime().addShutdownHook(shutdownHook); + } + + /** + * Get the jppf client to use. + * @return a {@link JPPFClient} instance. + */ + public JPPFClient getClient() { + return client; + } + + /** + * Set the jppf client to use. + * @param client a {@link JPPFClient} instance. + */ + public void setClient(final JPPFClient client) { + this.client = client; + } + + /** + * Get the nodes to launch for the test. + * @return an array of NodeProcessLauncher instances. + */ + public RestartableNodeProcessLauncher[] getNodes() { + return nodes; + } + + /** + * Get the drivers to launch for the test. + * @return an array of DriverProcessLauncher instances. + */ + public RestartableDriverProcessLauncher[] getDrivers() { + return drivers; + } + + /** + * Get the object which manages the JMX connections to ddrivers and nodes. + * @return a {@link JMXHandler} instance. + */ + public JMXHandler getJmxHandler() { + return jmxHandler; + } +} diff --git a/tests/src/framework/test/org/jppf/test/setup/AbstractNonStandardSetup.java b/tests/src/framework/test/org/jppf/test/setup/AbstractNonStandardSetup.java index a676aaab46..f78fa8f965 100644 --- a/tests/src/framework/test/org/jppf/test/setup/AbstractNonStandardSetup.java +++ b/tests/src/framework/test/org/jppf/test/setup/AbstractNonStandardSetup.java @@ -31,6 +31,7 @@ import org.jppf.node.protocol.Task; import org.jppf.ssl.SSLHelper; import org.jppf.utils.*; +import org.jppf.utils.Operator; import org.jppf.utils.collections.*; import org.jppf.utils.concurrent.*; import org.junit.AfterClass; @@ -46,9 +47,7 @@ public class AbstractNonStandardSetup extends BaseTest { protected static final NodeSelector NON_PEER_SELECTOR = new ExecutionPolicySelector(new Equal("jppf.peer.driver", false)); /** */ protected static final NodeSelector PEER_SELECTOR = new ExecutionPolicySelector(new Equal("jppf.peer.driver", true)); - /** - * - */ + /** */ protected static TestConfiguration testConfig = null; /** @@ -380,7 +379,7 @@ public boolean evaluate() { * @param timeout how long to wait for the condtion to be {@link true}. * @throws Exception if any error occurs or the tiemout expires. */ - protected static void awaitNbIdleNodes(final JMXDriverConnectionWrapper jmx, final Operator operator, final int nbNodes, final long timeout) throws Exception { + protected static void awaitNbIdleNodes(final JMXDriverConnectionWrapper jmx, final ComparisonOperator operator, final int nbNodes, final long timeout) throws Exception { awaitNbIdleNodes(jmx, NON_PEER_SELECTOR, false, operator, nbNodes, timeout); } @@ -395,7 +394,7 @@ protected static void awaitNbIdleNodes(final JMXDriverConnectionWrapper jmx, fin * @throws Exception if any error occurs or the tiemout expires. */ protected static void awaitNbIdleNodes(final JMXDriverConnectionWrapper jmx, final NodeSelector selector, final boolean includePeers, - final Operator operator, final int nbNodes, final long timeout) throws Exception { + final ComparisonOperator operator, final int nbNodes, final long timeout) throws Exception { ConcurrentUtils.awaitCondition(new ConcurrentUtils.Condition() { @Override public boolean evaluate() { diff --git a/tests/src/framework/test/org/jppf/test/setup/BaseSetup.java b/tests/src/framework/test/org/jppf/test/setup/BaseSetup.java index 927b836f4c..fc7890abe2 100644 --- a/tests/src/framework/test/org/jppf/test/setup/BaseSetup.java +++ b/tests/src/framework/test/org/jppf/test/setup/BaseSetup.java @@ -30,6 +30,7 @@ import org.jppf.server.job.management.DriverJobManagementMBean; import org.jppf.ssl.SSLHelper; import org.jppf.utils.*; +import org.jppf.utils.Operator; import org.slf4j.*; import test.org.jppf.test.setup.common.TestUtils; diff --git a/tests/src/framework/test/org/jppf/test/setup/common/BaseTestHelper.java b/tests/src/framework/test/org/jppf/test/setup/common/BaseTestHelper.java index 2b7cc2df74..856cd74ec4 100644 --- a/tests/src/framework/test/org/jppf/test/setup/common/BaseTestHelper.java +++ b/tests/src/framework/test/org/jppf/test/setup/common/BaseTestHelper.java @@ -28,6 +28,7 @@ import org.jppf.management.forwarding.JPPFNodeForwardingMBean; import org.jppf.node.protocol.Task; import org.jppf.utils.*; +import org.jppf.utils.Operator; import org.slf4j.*; import test.org.jppf.test.setup.BaseTest; diff --git a/tests/src/tests/test/org/jppf/client/TestConnectionPool.java b/tests/src/tests/test/org/jppf/client/TestConnectionPool.java index 74ed6e12a0..549889c33c 100644 --- a/tests/src/tests/test/org/jppf/client/TestConnectionPool.java +++ b/tests/src/tests/test/org/jppf/client/TestConnectionPool.java @@ -30,6 +30,7 @@ import org.jppf.discovery.*; import org.jppf.node.protocol.Task; import org.jppf.utils.*; +import org.jppf.utils.Operator; import org.junit.*; import test.org.jppf.test.setup.*; @@ -241,7 +242,7 @@ private static void testJobResults(final int nbTasks, final List> result * @param nbPools the number of pools on which to apply the condition. * @throws Exception if any error occurs. */ - private static void awaitConnections(final JPPFClient client, final Operator operator, final int nbPools) throws Exception { + private static void awaitConnections(final JPPFClient client, final ComparisonOperator operator, final int nbPools) throws Exception { BaseTest.print(false, false, "waiting for nbAvailableConnections %s %d", operator, nbPools); while (!operator.evaluate(client.awaitWorkingConnectionPools().size(), nbPools)) Thread.sleep(10L); final JobManagerClient mgr = (JobManagerClient) client.getJobManager(); diff --git a/tests/src/tests/test/org/jppf/client/TestOperator.java b/tests/src/tests/test/org/jppf/client/TestOperator.java index 4f62d257fb..3d6aa69f4a 100644 --- a/tests/src/tests/test/org/jppf/client/TestOperator.java +++ b/tests/src/tests/test/org/jppf/client/TestOperator.java @@ -1,90 +1,90 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.org.jppf.client; - -import static org.junit.Assert.*; - -import org.jppf.client.Operator; -import org.junit.Test; - -import test.org.jppf.test.setup.BaseTest; - -/** - * Tests for the {@link Operator} class. - * @author Laurent Cohen - */ -public class TestOperator extends BaseTest { - /** - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testEqual() throws Exception { - assertTrue(Operator.EQUAL.evaluate(2, 2)); - assertFalse(Operator.EQUAL.evaluate(1, 2)); - } - - /** - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testNotEqual() throws Exception { - assertFalse(Operator.NOT_EQUAL.evaluate(2, 2)); - assertTrue(Operator.NOT_EQUAL.evaluate(1, 2)); - } - - /** - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testAtLeast() throws Exception { - assertTrue(Operator.AT_LEAST.evaluate(2, 2)); - assertTrue(Operator.AT_LEAST.evaluate(3, 2)); - assertFalse(Operator.AT_LEAST.evaluate(1, 2)); - } - - /** - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testAtMost() throws Exception { - assertTrue(Operator.AT_MOST.evaluate(1, 2)); - assertTrue(Operator.AT_MOST.evaluate(2, 2)); - assertFalse(Operator.AT_MOST.evaluate(3, 2)); - } - - /** - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testMoreThan() throws Exception { - assertTrue(Operator.MORE_THAN.evaluate(3, 2)); - assertFalse(Operator.MORE_THAN.evaluate(2, 2)); - assertFalse(Operator.MORE_THAN.evaluate(1, 2)); - } - - /** - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testLessThan() throws Exception { - assertTrue(Operator.LESS_THAN.evaluate(1, 2)); - assertFalse(Operator.LESS_THAN.evaluate(2, 2)); - assertFalse(Operator.LESS_THAN.evaluate(3, 2)); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.org.jppf.client; + +import static org.junit.Assert.*; + +import org.jppf.utils.Operator; +import org.junit.Test; + +import test.org.jppf.test.setup.BaseTest; + +/** + * Tests for the {@link Operator} class. + * @author Laurent Cohen + */ +public class TestOperator extends BaseTest { + /** + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testEqual() throws Exception { + assertTrue(Operator.EQUAL.evaluate(2, 2)); + assertFalse(Operator.EQUAL.evaluate(1, 2)); + } + + /** + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testNotEqual() throws Exception { + assertFalse(Operator.NOT_EQUAL.evaluate(2, 2)); + assertTrue(Operator.NOT_EQUAL.evaluate(1, 2)); + } + + /** + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testAtLeast() throws Exception { + assertTrue(Operator.AT_LEAST.evaluate(2, 2)); + assertTrue(Operator.AT_LEAST.evaluate(3, 2)); + assertFalse(Operator.AT_LEAST.evaluate(1, 2)); + } + + /** + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testAtMost() throws Exception { + assertTrue(Operator.AT_MOST.evaluate(1, 2)); + assertTrue(Operator.AT_MOST.evaluate(2, 2)); + assertFalse(Operator.AT_MOST.evaluate(3, 2)); + } + + /** + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testMoreThan() throws Exception { + assertTrue(Operator.MORE_THAN.evaluate(3, 2)); + assertFalse(Operator.MORE_THAN.evaluate(2, 2)); + assertFalse(Operator.MORE_THAN.evaluate(1, 2)); + } + + /** + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testLessThan() throws Exception { + assertTrue(Operator.LESS_THAN.evaluate(1, 2)); + assertFalse(Operator.LESS_THAN.evaluate(2, 2)); + assertFalse(Operator.LESS_THAN.evaluate(3, 2)); + } +} diff --git a/tests/src/tests/test/org/jppf/discovery/TestDriverDiscovery.java b/tests/src/tests/test/org/jppf/discovery/TestDriverDiscovery.java index 7e6eaaec7d..f7bad42d68 100644 --- a/tests/src/tests/test/org/jppf/discovery/TestDriverDiscovery.java +++ b/tests/src/tests/test/org/jppf/discovery/TestDriverDiscovery.java @@ -1,228 +1,229 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.org.jppf.discovery; - -import static org.junit.Assert.*; - -import java.io.StringReader; -import java.util.*; - -import org.jppf.client.*; -import org.jppf.discovery.*; -import org.jppf.management.JMXDriverConnectionWrapper; -import org.jppf.node.protocol.Task; -import org.jppf.utils.*; -import org.jppf.utils.configuration.JPPFProperties; -import org.junit.*; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; - -import test.org.jppf.test.setup.*; -import test.org.jppf.test.setup.common.*; - -/** - * Test a multi-server topology with 2 servers, 1 node attached to each server and 1 client. - * @author Laurent Cohen - */ -public class TestDriverDiscovery extends AbstractNonStandardSetup { - /** - * Path to the folder containng the scripts. - */ - private final String resourcePath = getClass().getPackage().getName().replace(".", "/"); - /** - * JMX connections to all drivers. - */ - private static final JMXDriverConnectionWrapper[] JMX = new JMXDriverConnectionWrapper[2]; - /** */ - @Rule - public TestWatcher testDriverDiscoveryInstanceWatcher = new TestWatcher() { - @Override - protected void starting(final Description description) { - try { - final String msg = String.format( "***** start of method %s() *****", description.getMethodName()); - final String banner = StringUtils.padLeft("", '*', msg.length(), false); - for (final JMXDriverConnectionWrapper jmx: JMX) logInServer(jmx, banner, msg, banner); - } catch(final Exception e) { - e.printStackTrace(); - } - } - }; - - /** - * Launches 2 drivers with 1 node attached to each. - * @throws Exception if a process could not be started. - */ - @BeforeClass - public static void setup() throws Exception { - final TestConfiguration config = createConfig("discovery"); - config.driverLog4j = "classes/tests/config/discovery/log4j-driver.properties"; - BaseSetup.setup(2, 2, false, true, config); - final long start = System.currentTimeMillis(); - final long timeout = 60_000L; - for (int i=0; i pools = null; - boolean end = false; - while (!end) { - pools = client.getConnectionPools(); - if (pools.size() < 2) Thread.sleep(100L); - else end = true; - } - for (final JPPFConnectionPool pool: pools) pool.awaitActiveConnection(); - Collections.sort(pools, new Comparator() { - @Override - public int compare(final JPPFConnectionPool p1, final JPPFConnectionPool p2) { - return p1.getName().compareTo(p2.getName()); - } - }); - assertNotNull(pools); - assertEquals(2, pools.size()); - for (int i=0; i<2; i++) { - final JPPFConnectionPool pool = pools.get(i); - assertNotNull(pool); - final int port = 11101 + i; - assertEquals("ClientDiscovery_" + port, pool.getName()); - assertEquals(port, pool.getDriverPort()); - assertEquals("localhost", pool.getDriverHost()); - assertFalse(pool.isSslEnabled()); - assertEquals(2 - i, pool.getPriority()); - assertEquals(1, pool.getSize()); - assertEquals(1, pool.getJMXPoolSize()); - } - final int tasksPerNode = 5; - final int nbNodes = BaseSetup.nbNodes(); - final int nbTasks = tasksPerNode * nbNodes; - final String name = ReflectionUtils.getCurrentMethodName(); - final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 1L); - final List> results = client.submitJob(job); - assertNotNull(results); - assertEquals(nbTasks, results.size()); - for (final Task t: results) { - assertTrue("task = " + t, t instanceof LifeCycleTask); - assertNull(t.getThrowable()); - assertNotNull(t.getResult()); - assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, t.getResult()); - } - //AbstractNonStandardSetup.client = null; - } - assertTrue(discovery.shutdownFlag); - } - - /** */ - public static class ClientDiscovery extends ClientDriverDiscovery { - /** - * Whether this discovery was shutdown. - */ - boolean shutdownFlag = false; - - /** */ - public ClientDiscovery() { - BaseTest.printOut("in %s() contructor", getClass().getSimpleName()); - } - - @Override - public void discover() { - final String className = getClass().getSimpleName(); - for (int i=0; i<2; i++) { - final int port = 11101 + i; - final ClientConnectionPoolInfo info = new ClientConnectionPoolInfo("ClientDiscovery_" + port, false, "localhost", port, 2 - i, 1, 1); - BaseTest.printOut("%s 'discovering' %s", className, info); - newConnection(info); - } - } - - @Override - public void shutdown() { - synchronized(this) { - shutdownFlag = true; - } - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.org.jppf.discovery; + +import static org.junit.Assert.*; + +import java.io.StringReader; +import java.util.*; + +import org.jppf.client.*; +import org.jppf.discovery.*; +import org.jppf.management.JMXDriverConnectionWrapper; +import org.jppf.node.protocol.Task; +import org.jppf.utils.*; +import org.jppf.utils.Operator; +import org.jppf.utils.configuration.JPPFProperties; +import org.junit.*; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; + +import test.org.jppf.test.setup.*; +import test.org.jppf.test.setup.common.*; + +/** + * Test a multi-server topology with 2 servers, 1 node attached to each server and 1 client. + * @author Laurent Cohen + */ +public class TestDriverDiscovery extends AbstractNonStandardSetup { + /** + * Path to the folder containng the scripts. + */ + private final String resourcePath = getClass().getPackage().getName().replace(".", "/"); + /** + * JMX connections to all drivers. + */ + private static final JMXDriverConnectionWrapper[] JMX = new JMXDriverConnectionWrapper[2]; + /** */ + @Rule + public TestWatcher testDriverDiscoveryInstanceWatcher = new TestWatcher() { + @Override + protected void starting(final Description description) { + try { + final String msg = String.format( "***** start of method %s() *****", description.getMethodName()); + final String banner = StringUtils.padLeft("", '*', msg.length(), false); + for (final JMXDriverConnectionWrapper jmx: JMX) logInServer(jmx, banner, msg, banner); + } catch(final Exception e) { + e.printStackTrace(); + } + } + }; + + /** + * Launches 2 drivers with 1 node attached to each. + * @throws Exception if a process could not be started. + */ + @BeforeClass + public static void setup() throws Exception { + final TestConfiguration config = createConfig("discovery"); + config.driverLog4j = "classes/tests/config/discovery/log4j-driver.properties"; + BaseSetup.setup(2, 2, false, true, config); + final long start = System.currentTimeMillis(); + final long timeout = 60_000L; + for (int i=0; i pools = null; + boolean end = false; + while (!end) { + pools = client.getConnectionPools(); + if (pools.size() < 2) Thread.sleep(100L); + else end = true; + } + for (final JPPFConnectionPool pool: pools) pool.awaitActiveConnection(); + Collections.sort(pools, new Comparator() { + @Override + public int compare(final JPPFConnectionPool p1, final JPPFConnectionPool p2) { + return p1.getName().compareTo(p2.getName()); + } + }); + assertNotNull(pools); + assertEquals(2, pools.size()); + for (int i=0; i<2; i++) { + final JPPFConnectionPool pool = pools.get(i); + assertNotNull(pool); + final int port = 11101 + i; + assertEquals("ClientDiscovery_" + port, pool.getName()); + assertEquals(port, pool.getDriverPort()); + assertEquals("localhost", pool.getDriverHost()); + assertFalse(pool.isSslEnabled()); + assertEquals(2 - i, pool.getPriority()); + assertEquals(1, pool.getSize()); + assertEquals(1, pool.getJMXPoolSize()); + } + final int tasksPerNode = 5; + final int nbNodes = BaseSetup.nbNodes(); + final int nbTasks = tasksPerNode * nbNodes; + final String name = ReflectionUtils.getCurrentMethodName(); + final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 1L); + final List> results = client.submitJob(job); + assertNotNull(results); + assertEquals(nbTasks, results.size()); + for (final Task t: results) { + assertTrue("task = " + t, t instanceof LifeCycleTask); + assertNull(t.getThrowable()); + assertNotNull(t.getResult()); + assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, t.getResult()); + } + //AbstractNonStandardSetup.client = null; + } + assertTrue(discovery.shutdownFlag); + } + + /** */ + public static class ClientDiscovery extends ClientDriverDiscovery { + /** + * Whether this discovery was shutdown. + */ + boolean shutdownFlag = false; + + /** */ + public ClientDiscovery() { + BaseTest.printOut("in %s() contructor", getClass().getSimpleName()); + } + + @Override + public void discover() { + final String className = getClass().getSimpleName(); + for (int i=0; i<2; i++) { + final int port = 11101 + i; + final ClientConnectionPoolInfo info = new ClientConnectionPoolInfo("ClientDiscovery_" + port, false, "localhost", port, 2 - i, 1, 1); + BaseTest.printOut("%s 'discovering' %s", className, info); + newConnection(info); + } + } + + @Override + public void shutdown() { + synchronized(this) { + shutdownFlag = true; + } + } + } +} diff --git a/tests/src/tests/test/org/jppf/job/persistence/AbstractJobPersistenceTest.java b/tests/src/tests/test/org/jppf/job/persistence/AbstractJobPersistenceTest.java index c2f13dad7b..2afc1b6d77 100644 --- a/tests/src/tests/test/org/jppf/job/persistence/AbstractJobPersistenceTest.java +++ b/tests/src/tests/test/org/jppf/job/persistence/AbstractJobPersistenceTest.java @@ -35,6 +35,7 @@ import org.jppf.node.protocol.Task; import org.jppf.test.addons.common.AddonSimpleTask; import org.jppf.utils.*; +import org.jppf.utils.Operator; import org.jppf.utils.concurrent.ConcurrentUtils; import org.jppf.utils.concurrent.ConcurrentUtils.ConditionFalseOnException; import org.junit.*; diff --git a/tests/src/tests/test/org/jppf/load/balancer/persistence/AbstractClientLoadBalancerPersistenceTest.java b/tests/src/tests/test/org/jppf/load/balancer/persistence/AbstractClientLoadBalancerPersistenceTest.java index d407ce4162..6ca9712b1f 100644 --- a/tests/src/tests/test/org/jppf/load/balancer/persistence/AbstractClientLoadBalancerPersistenceTest.java +++ b/tests/src/tests/test/org/jppf/load/balancer/persistence/AbstractClientLoadBalancerPersistenceTest.java @@ -29,6 +29,7 @@ import org.jppf.load.balancer.persistence.LoadBalancerPersistenceManagement; import org.jppf.node.policy.Equal; import org.jppf.node.protocol.Task; +import org.jppf.utils.Operator; import org.jppf.utils.ReflectionUtils; import org.junit.*; import org.junit.rules.TestWatcher; diff --git a/tests/src/tests/test/org/jppf/load/balancer/persistence/AbstractMuliServerLoadBalancerPersistenceTest.java b/tests/src/tests/test/org/jppf/load/balancer/persistence/AbstractMuliServerLoadBalancerPersistenceTest.java index 045684574c..b54415112c 100644 --- a/tests/src/tests/test/org/jppf/load/balancer/persistence/AbstractMuliServerLoadBalancerPersistenceTest.java +++ b/tests/src/tests/test/org/jppf/load/balancer/persistence/AbstractMuliServerLoadBalancerPersistenceTest.java @@ -29,6 +29,7 @@ import org.jppf.node.policy.Equal; import org.jppf.node.protocol.Task; import org.jppf.utils.*; +import org.jppf.utils.Operator; import org.junit.*; import org.junit.rules.TestWatcher; import org.junit.runner.Description; diff --git a/tests/src/tests/test/org/jppf/node/policy/TestGridPolicy.java b/tests/src/tests/test/org/jppf/node/policy/TestGridPolicy.java index 45786ae645..44cdf821f9 100644 --- a/tests/src/tests/test/org/jppf/node/policy/TestGridPolicy.java +++ b/tests/src/tests/test/org/jppf/node/policy/TestGridPolicy.java @@ -1,228 +1,229 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.org.jppf.node.policy; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.jppf.JPPFException; -import org.jppf.client.*; -import org.jppf.management.*; -import org.jppf.node.policy.*; -import org.jppf.node.protocol.Task; -import org.jppf.scheduling.JPPFSchedule; -import org.jppf.utils.*; -import org.jppf.utils.configuration.JPPFProperties; -import org.junit.Test; - -import test.org.jppf.test.setup.Setup1D2N1C; -import test.org.jppf.test.setup.common.*; - -/** - * Unit tests for the {@link NodesMatching} class. - * @author Laurent Cohen - */ -public class TestGridPolicy extends Setup1D2N1C { - /** - * Folder where the XML policy files are located. - */ - private static final String RESOURCES_DIR = TestGridPolicy.class.getPackage().getName().replace(".", "/"); - /** - * Job expiration timeout. - */ - private static final long JOB_TIMEOUT = 3_000L; - - /** - * Test that an XML representation of a scripted policy is valid according to the ExecutionPolicy schema. - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testValidXML() throws Exception { - final String validTrueXML = FileUtils.readTextFile(RESOURCES_DIR + "/ValidTrueGridPolicy.xml"); - final String validFalseXML = FileUtils.readTextFile(RESOURCES_DIR + "/ValidFalseGridPolicy.xml"); - PolicyParser.validatePolicy(validTrueXML); - PolicyParser.validatePolicy(validFalseXML); - } - - /** - * Test that an XML representation of a scripted policy is valid according to the ExecutionPolicy.xsd schema. - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testInvalidXML() throws Exception { - try { - final String invalidXML = FileUtils.readTextFile(RESOURCES_DIR + "/InvalidGridPolicy.xml"); - PolicyParser.validatePolicy(invalidXML); - throw new IllegalStateException("the policy is invalid but passes the validation"); - } catch(final Exception e) { - assertTrue("e = " + e, e instanceof JPPFException); - } - } - - /** - * Test the results of an XML grid policy expected to let the job execute. - * @throws Exception if any error occurs. - */ - @Test(timeout=10000) - public void testTrueXmlPolicy() throws Exception { - final int nbTasks = 5; - final String xml = FileUtils.readTextFile(RESOURCES_DIR + "/ValidTrueGridPolicy.xml"); - final String name = ReflectionUtils.getCurrentClassAndMethod(); - final ExecutionPolicy p = PolicyParser.parsePolicy(xml); - printOut("%s() grid policy:%n%s%n", name, p); - final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 0L); - job.getSLA().setGridExecutionPolicy(p); - job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck - final List> results = client.submitJob(job); - assertNotNull(results); - assertEquals(results.size(), nbTasks); - for (int i=0; i task = results.get(i); - assertNotNull(task.getResult()); - assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, task.getResult()); - } - } - - /** - * Test the results of an XML grid policy, where the number of expected nodes is definedas an scripted expression with property substitutions. - * @throws Exception if any error occurs. - */ - @Test(timeout=10000) - public void tesGridPolicyWithExpression() throws Exception { - final int nbTasks = 5; - final String xml = FileUtils.readTextFile(RESOURCES_DIR + "/GridPolicyWithExpression.xml"); - final String name = ReflectionUtils.getCurrentClassAndMethod(); - final ExecutionPolicy p = PolicyParser.parsePolicy(xml); - printOut("%s() grid policy:%n%s%n", name, p); - final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 0L); - job.getSLA().setGridExecutionPolicy(p); - job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck - final List> results = client.submitJob(job); - assertNotNull(results); - assertEquals(results.size(), nbTasks); - for (int i=0; i task = results.get(i); - assertNotNull(task.getResult()); - assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, task.getResult()); - } - } - - /** - * Test the results of an XML grid policy expected to prevent the job from executing. - * @throws Exception if any error occurs. - */ - @Test(timeout=10000) - public void testFalseXmlPolicy() throws Exception { - final int nbTasks = 5; - final String xml = FileUtils.readTextFile(RESOURCES_DIR + "/ValidFalseGridPolicy.xml"); - final ExecutionPolicy p = PolicyParser.parsePolicy(xml); - final JPPFJob job = BaseTestHelper.createJob(ReflectionUtils.getCurrentClassAndMethod(), true, false, nbTasks, LifeCycleTask.class, 0L); - job.getSLA().setGridExecutionPolicy(p); - job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck - final List> results = client.submitJob(job); - assertNotNull(results); - assertEquals(results.size(), nbTasks); - for (int i=0; i task = results.get(i); - assertNull(task.getResult()); - } - } - - /** - * Test the results of a grid policy expected to let the job execute. - * @throws Exception if any error occurs. - */ - @Test(timeout=10000) - public void testTrueJavaPolicy() throws Exception { - final int nbTasks = 5; - final String name = ReflectionUtils.getCurrentClassAndMethod(); - // more than 1 node with at least 1 processing thread - final ExecutionPolicy p = new NodesMatching(Operator.MORE_THAN, 1, new AtLeast(JPPFProperties.PROCESSING_THREADS.getName(), 1)); - printOut("%s() grid policy:%n%s%n", name, p); - final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 0L); - job.getSLA().setGridExecutionPolicy(p); - job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck - final List> results = client.submitJob(job); - assertNotNull(results); - assertEquals(results.size(), nbTasks); - for (int i=0; i task = results.get(i); - assertNotNull(task.getResult()); - assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, task.getResult()); - } - } - - /** - * Test the results of a grid policy expected to prevent the job from executing. - * @throws Exception if any error occurs. - */ - @Test(timeout=10000) - public void testFalseJavaPolicy() throws Exception { - final int nbTasks = 5; - // at least 4 nodes with at least 1 processing thread - final ExecutionPolicy p = new NodesMatching(Operator.AT_LEAST, 4, new AtLeast(JPPFProperties.PROCESSING_THREADS.getName(), 1)); - final JPPFJob job = BaseTestHelper.createJob(ReflectionUtils.getCurrentClassAndMethod(), true, false, nbTasks, LifeCycleTask.class, 0L); - job.getSLA().setGridExecutionPolicy(p); - job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck - final List> results = client.submitJob(job); - assertNotNull(results); - assertEquals(results.size(), nbTasks); - for (int i=0; i task = results.get(i); - assertNull(task.getResult()); - } - } - - /** - * Test a global policy expected to prevent the job from executing in the grid's global state. - * Then, two additional nodes are started after the job is submitted, making the global policy a working one. - * @throws Exception if any error occurs. - */ - @Test(timeout=10000) - public void testWorkingFalseJavaPolicy() throws Exception { - final int nbTasks = 5; - final JMXDriverConnectionWrapper jmx = client.awaitWorkingConnectionPool().awaitWorkingJMXConnection(); - // more than 4 nodes with at least 1 processing thread - final ExecutionPolicy p = new NodesMatching(Operator.AT_LEAST, 4, new AtLeast(JPPFProperties.PROCESSING_THREADS.getName(), 1)); - final JPPFJob job = BaseTestHelper.createJob(ReflectionUtils.getCurrentClassAndMethod(), false, false, nbTasks, LifeCycleTask.class, 0L); - job.getSLA().setGridExecutionPolicy(p); - job.getSLA().setJobExpirationSchedule(new JPPFSchedule(3*JOB_TIMEOUT)); // to avoid the job being stuck - client.submitJob(job); - try { - Thread.sleep(1000L); - // a static node uuid is already assigned in the master's config file and must be overriden - final TypedProperties overrides = new TypedProperties(); - overrides.setString("jppf.node.uuid", "$script{ java.util.UUID.randomUUID().toString(); }$"); - // start 1 slave node for each master - jmx.getNodeForwarder().provisionSlaveNodes(NodeSelector.ALL_NODES, 1, overrides); - final List> results = job.awaitResults(); - assertNotNull(results); - assertEquals(results.size(), nbTasks); - for (int i=0; i task = results.get(i); - assertNotNull("result of task #" + i + " is null", task.getResult()); - assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, task.getResult()); - } - } finally { - // terminate the slave nodes - jmx.getNodeForwarder().provisionSlaveNodes(NodeSelector.ALL_NODES, 0); - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.org.jppf.node.policy; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.jppf.JPPFException; +import org.jppf.client.*; +import org.jppf.management.*; +import org.jppf.node.policy.*; +import org.jppf.node.protocol.Task; +import org.jppf.scheduling.JPPFSchedule; +import org.jppf.utils.*; +import org.jppf.utils.Operator; +import org.jppf.utils.configuration.JPPFProperties; +import org.junit.Test; + +import test.org.jppf.test.setup.Setup1D2N1C; +import test.org.jppf.test.setup.common.*; + +/** + * Unit tests for the {@link NodesMatching} class. + * @author Laurent Cohen + */ +public class TestGridPolicy extends Setup1D2N1C { + /** + * Folder where the XML policy files are located. + */ + private static final String RESOURCES_DIR = TestGridPolicy.class.getPackage().getName().replace(".", "/"); + /** + * Job expiration timeout. + */ + private static final long JOB_TIMEOUT = 3_000L; + + /** + * Test that an XML representation of a scripted policy is valid according to the ExecutionPolicy schema. + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testValidXML() throws Exception { + final String validTrueXML = FileUtils.readTextFile(RESOURCES_DIR + "/ValidTrueGridPolicy.xml"); + final String validFalseXML = FileUtils.readTextFile(RESOURCES_DIR + "/ValidFalseGridPolicy.xml"); + PolicyParser.validatePolicy(validTrueXML); + PolicyParser.validatePolicy(validFalseXML); + } + + /** + * Test that an XML representation of a scripted policy is valid according to the ExecutionPolicy.xsd schema. + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testInvalidXML() throws Exception { + try { + final String invalidXML = FileUtils.readTextFile(RESOURCES_DIR + "/InvalidGridPolicy.xml"); + PolicyParser.validatePolicy(invalidXML); + throw new IllegalStateException("the policy is invalid but passes the validation"); + } catch(final Exception e) { + assertTrue("e = " + e, e instanceof JPPFException); + } + } + + /** + * Test the results of an XML grid policy expected to let the job execute. + * @throws Exception if any error occurs. + */ + @Test(timeout=10000) + public void testTrueXmlPolicy() throws Exception { + final int nbTasks = 5; + final String xml = FileUtils.readTextFile(RESOURCES_DIR + "/ValidTrueGridPolicy.xml"); + final String name = ReflectionUtils.getCurrentClassAndMethod(); + final ExecutionPolicy p = PolicyParser.parsePolicy(xml); + printOut("%s() grid policy:%n%s%n", name, p); + final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 0L); + job.getSLA().setGridExecutionPolicy(p); + job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck + final List> results = client.submitJob(job); + assertNotNull(results); + assertEquals(results.size(), nbTasks); + for (int i=0; i task = results.get(i); + assertNotNull(task.getResult()); + assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, task.getResult()); + } + } + + /** + * Test the results of an XML grid policy, where the number of expected nodes is definedas an scripted expression with property substitutions. + * @throws Exception if any error occurs. + */ + @Test(timeout=10000) + public void tesGridPolicyWithExpression() throws Exception { + final int nbTasks = 5; + final String xml = FileUtils.readTextFile(RESOURCES_DIR + "/GridPolicyWithExpression.xml"); + final String name = ReflectionUtils.getCurrentClassAndMethod(); + final ExecutionPolicy p = PolicyParser.parsePolicy(xml); + printOut("%s() grid policy:%n%s%n", name, p); + final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 0L); + job.getSLA().setGridExecutionPolicy(p); + job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck + final List> results = client.submitJob(job); + assertNotNull(results); + assertEquals(results.size(), nbTasks); + for (int i=0; i task = results.get(i); + assertNotNull(task.getResult()); + assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, task.getResult()); + } + } + + /** + * Test the results of an XML grid policy expected to prevent the job from executing. + * @throws Exception if any error occurs. + */ + @Test(timeout=10000) + public void testFalseXmlPolicy() throws Exception { + final int nbTasks = 5; + final String xml = FileUtils.readTextFile(RESOURCES_DIR + "/ValidFalseGridPolicy.xml"); + final ExecutionPolicy p = PolicyParser.parsePolicy(xml); + final JPPFJob job = BaseTestHelper.createJob(ReflectionUtils.getCurrentClassAndMethod(), true, false, nbTasks, LifeCycleTask.class, 0L); + job.getSLA().setGridExecutionPolicy(p); + job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck + final List> results = client.submitJob(job); + assertNotNull(results); + assertEquals(results.size(), nbTasks); + for (int i=0; i task = results.get(i); + assertNull(task.getResult()); + } + } + + /** + * Test the results of a grid policy expected to let the job execute. + * @throws Exception if any error occurs. + */ + @Test(timeout=10000) + public void testTrueJavaPolicy() throws Exception { + final int nbTasks = 5; + final String name = ReflectionUtils.getCurrentClassAndMethod(); + // more than 1 node with at least 1 processing thread + final ExecutionPolicy p = new NodesMatching(Operator.MORE_THAN, 1, new AtLeast(JPPFProperties.PROCESSING_THREADS.getName(), 1)); + printOut("%s() grid policy:%n%s%n", name, p); + final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 0L); + job.getSLA().setGridExecutionPolicy(p); + job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck + final List> results = client.submitJob(job); + assertNotNull(results); + assertEquals(results.size(), nbTasks); + for (int i=0; i task = results.get(i); + assertNotNull(task.getResult()); + assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, task.getResult()); + } + } + + /** + * Test the results of a grid policy expected to prevent the job from executing. + * @throws Exception if any error occurs. + */ + @Test(timeout=10000) + public void testFalseJavaPolicy() throws Exception { + final int nbTasks = 5; + // at least 4 nodes with at least 1 processing thread + final ExecutionPolicy p = new NodesMatching(Operator.AT_LEAST, 4, new AtLeast(JPPFProperties.PROCESSING_THREADS.getName(), 1)); + final JPPFJob job = BaseTestHelper.createJob(ReflectionUtils.getCurrentClassAndMethod(), true, false, nbTasks, LifeCycleTask.class, 0L); + job.getSLA().setGridExecutionPolicy(p); + job.getSLA().setJobExpirationSchedule(new JPPFSchedule(JOB_TIMEOUT)); // to avoid the job being stuck + final List> results = client.submitJob(job); + assertNotNull(results); + assertEquals(results.size(), nbTasks); + for (int i=0; i task = results.get(i); + assertNull(task.getResult()); + } + } + + /** + * Test a global policy expected to prevent the job from executing in the grid's global state. + * Then, two additional nodes are started after the job is submitted, making the global policy a working one. + * @throws Exception if any error occurs. + */ + @Test(timeout=10000) + public void testWorkingFalseJavaPolicy() throws Exception { + final int nbTasks = 5; + final JMXDriverConnectionWrapper jmx = client.awaitWorkingConnectionPool().awaitWorkingJMXConnection(); + // more than 4 nodes with at least 1 processing thread + final ExecutionPolicy p = new NodesMatching(Operator.AT_LEAST, 4, new AtLeast(JPPFProperties.PROCESSING_THREADS.getName(), 1)); + final JPPFJob job = BaseTestHelper.createJob(ReflectionUtils.getCurrentClassAndMethod(), false, false, nbTasks, LifeCycleTask.class, 0L); + job.getSLA().setGridExecutionPolicy(p); + job.getSLA().setJobExpirationSchedule(new JPPFSchedule(3*JOB_TIMEOUT)); // to avoid the job being stuck + client.submitJob(job); + try { + Thread.sleep(1000L); + // a static node uuid is already assigned in the master's config file and must be overriden + final TypedProperties overrides = new TypedProperties(); + overrides.setString("jppf.node.uuid", "$script{ java.util.UUID.randomUUID().toString(); }$"); + // start 1 slave node for each master + jmx.getNodeForwarder().provisionSlaveNodes(NodeSelector.ALL_NODES, 1, overrides); + final List> results = job.awaitResults(); + assertNotNull(results); + assertEquals(results.size(), nbTasks); + for (int i=0; i task = results.get(i); + assertNotNull("result of task #" + i + " is null", task.getResult()); + assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, task.getResult()); + } + } finally { + // terminate the slave nodes + jmx.getNodeForwarder().provisionSlaveNodes(NodeSelector.ALL_NODES, 0); + } + } +} diff --git a/tests/src/tests/test/org/jppf/node/policy/TestPreferencePolicy.java b/tests/src/tests/test/org/jppf/node/policy/TestPreferencePolicy.java index 4d872ce4fe..b38976722a 100644 --- a/tests/src/tests/test/org/jppf/node/policy/TestPreferencePolicy.java +++ b/tests/src/tests/test/org/jppf/node/policy/TestPreferencePolicy.java @@ -1,203 +1,204 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.org.jppf.node.policy; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.jppf.JPPFException; -import org.jppf.client.*; -import org.jppf.node.policy.*; -import org.jppf.node.protocol.Task; -import org.jppf.scheduling.JPPFSchedule; -import org.jppf.utils.ReflectionUtils; -import org.junit.Test; - -import test.org.jppf.test.setup.Setup1D2N1C; -import test.org.jppf.test.setup.common.*; - -/** - * Unit tests for the Range class. - * @author Laurent Cohen - */ -public class TestPreferencePolicy extends Setup1D2N1C { - /** - * A valid XML representation of a preference policy, whose {@code accepts()} method returns {@code true}. - */ - private String validTrueXML = new StringBuilder() - .append("\n") - .append(" \n") - .append(" \n") - .append(" jppf.node.uuid\n") - .append(" n1\n") - .append(" \n") - .append(" \n") - .append(" jppf.node.uuid\n") - .append(" n2\n") - .append(" \n") - .append(" \n") - .append("\n").toString(); - /** - * A valid XML representation of a preference policy, whose {@code accepts()} method returns {@code false}. - */ - private String validFalseXML = new StringBuilder() - .append("\n") - .append(" \n") - .append(" \n") - .append(" jppf.node.uuid\n") - .append(" some value that will never be seen\n") - .append(" \n") - .append(" \n") - .append("\n").toString(); - /** - * An invalid XML representation of a preference policy, which does not contain any policy in its list. - */ - private String invalidXML = new StringBuilder() - .append("\n") - .append(" \n") - .append(" \n") - .append("\n").toString(); - - /** - * Test that an XML representation of a scripted policy is valid according to the ExecutionPolicy schema. - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testValidXML() throws Exception { - PolicyParser.validatePolicy(validTrueXML); - PolicyParser.validatePolicy(validFalseXML); - } - - /** - * Test that an XML representation of a scripted policy is valid according to the ExecutionPolicy.xsd schema. - * @throws Exception if any error occurs. - */ - @Test(timeout=5000) - public void testInvalidXML() throws Exception { - try { - PolicyParser.validatePolicy(invalidXML); - throw new IllegalStateException("the policy is invalid but passes the validation"); - } catch(final Exception e) { - assertTrue("e = " + e, e instanceof JPPFException); - } - } - - /** - * Test that the preference for node 1, then node 2 is applied properly on 2 concurrent jobs, with a policy parsed from an XML document. - * @throws Exception if any error occurs - */ - @Test(timeout=15000) - public void testTrueXMLPolicy() throws Exception { - try { - final ExecutionPolicy policy = PolicyParser.parsePolicy(validTrueXML); - assertTrue(policy instanceof Preference); - testTruePolicy(ReflectionUtils.getCurrentClassAndMethod(), policy); - } catch(final Throwable t) { - t.printStackTrace(); - if (t instanceof Exception) throw (Exception) t; - else if (t instanceof Error) throw (Error) t; - throw new RuntimeException(t); - } - } - - /** - * Test that the preference for node 1, then node 2 is applied properly on 2 concurrent jobs, with a policy built using a Java constructor. - * @throws Exception if any error occurs - */ - @Test(timeout=15000) - public void testTrueJavaPolicy() throws Exception { - testTruePolicy(ReflectionUtils.getCurrentClassAndMethod(), new Preference(new Equal("jppf.node.uuid", true, "n1"), new Equal("jppf.node.uuid", true, "n2"))); - } - - /** - * Test that the preference for node 1, then node 2 is applied properly on 2 concurrent jobs. - * @param name the name prefix for the submitted jobs. - * @param policy the execution policy to test. - * @throws Exception if any error occurs - */ - private static void testTruePolicy(final String name, final ExecutionPolicy policy) throws Exception { - JPPFConnectionPool pool = null; - while ((pool = client.getConnectionPool()) == null) Thread.sleep(10L); - try { - pool.setSize(2); - pool.awaitActiveConnections(Operator.AT_LEAST, 2); - final int nbTasks = 1; - final JPPFJob job1 = BaseTestHelper.createJob(name + " 1", false, false, nbTasks, LifeCycleTask.class, 3000L); - job1.getSLA().setExecutionPolicy(policy); - final JPPFJob job2 = BaseTestHelper.createJob(name + " 2", false, false, nbTasks, LifeCycleTask.class, 3000L); - job2.getSLA().setExecutionPolicy(policy); - // ensure job 2 is started by the server while job 1 is already executing - job2.getSLA().setJobSchedule(new JPPFSchedule(1000L)); - client.submitJob(job1); - client.submitJob(job2); - final List> results1 = job1.awaitResults(); - assertEquals(nbTasks, results1.size()); - final LifeCycleTask task1 = (LifeCycleTask) results1.get(0); - assertNotNull(task1.getResult()); - final List> results2 = job2.awaitResults(); - assertEquals(nbTasks, results2.size()); - final LifeCycleTask task2 = (LifeCycleTask) results2.get(0); - assertNotNull(task2.getResult()); - assertFalse(task1.getNodeUuid().equals(task2.getNodeUuid())); - //assertNotSame(task1.getNodeUuid(), task2.getNodeUuid()); - } finally { - pool.setSize(1); - } - } - - /** - * Test that the given scripted policy return {@code false} when its {@code accepts()} method is called. - * @throws Exception if any error occurs. - */ - @Test(timeout=10000) - public void testFalseXMLPolicy() throws Exception { - final ExecutionPolicy policy = PolicyParser.parsePolicy(validFalseXML); - assertTrue(policy instanceof Preference); - testFalsePolicy(ReflectionUtils.getCurrentClassAndMethod(), policy); - } - - /** - * Test that the given scripted policy return {@code false} when its {@code accepts()} method is called. - * @throws Exception if any error occurs. - */ - @Test(timeout=10000) - public void testFalseJavaPolicy() throws Exception { - testFalsePolicy(ReflectionUtils.getCurrentClassAndMethod(), new Preference(new Equal("jppf.node.uuid", true, "some value never seen"))); - } - - /** - * Test that the given scripted policy return {@code false} when its {@code accepts()} method is called. - * @param name the name of the submitted job. - * @param policy the execution policy to test. - * @throws Exception if any error occurs. - */ - private static void testFalsePolicy(final String name, final ExecutionPolicy policy) throws Exception { - final int nbTasks = 1; - assertTrue(policy instanceof Preference); - final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 10L); - job.getSLA().setExecutionPolicy(policy); - job.getSLA().setJobExpirationSchedule(new JPPFSchedule(2000L)); - final List> results = client.submitJob(job); - assertEquals(nbTasks, results.size()); - final LifeCycleTask task = (LifeCycleTask) results.get(0); - assertNull(task.getResult()); - assertNull(task.getNodeUuid()); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.org.jppf.node.policy; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.jppf.JPPFException; +import org.jppf.client.*; +import org.jppf.node.policy.*; +import org.jppf.node.protocol.Task; +import org.jppf.scheduling.JPPFSchedule; +import org.jppf.utils.Operator; +import org.jppf.utils.ReflectionUtils; +import org.junit.Test; + +import test.org.jppf.test.setup.Setup1D2N1C; +import test.org.jppf.test.setup.common.*; + +/** + * Unit tests for the Range class. + * @author Laurent Cohen + */ +public class TestPreferencePolicy extends Setup1D2N1C { + /** + * A valid XML representation of a preference policy, whose {@code accepts()} method returns {@code true}. + */ + private String validTrueXML = new StringBuilder() + .append("\n") + .append(" \n") + .append(" \n") + .append(" jppf.node.uuid\n") + .append(" n1\n") + .append(" \n") + .append(" \n") + .append(" jppf.node.uuid\n") + .append(" n2\n") + .append(" \n") + .append(" \n") + .append("\n").toString(); + /** + * A valid XML representation of a preference policy, whose {@code accepts()} method returns {@code false}. + */ + private String validFalseXML = new StringBuilder() + .append("\n") + .append(" \n") + .append(" \n") + .append(" jppf.node.uuid\n") + .append(" some value that will never be seen\n") + .append(" \n") + .append(" \n") + .append("\n").toString(); + /** + * An invalid XML representation of a preference policy, which does not contain any policy in its list. + */ + private String invalidXML = new StringBuilder() + .append("\n") + .append(" \n") + .append(" \n") + .append("\n").toString(); + + /** + * Test that an XML representation of a scripted policy is valid according to the ExecutionPolicy schema. + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testValidXML() throws Exception { + PolicyParser.validatePolicy(validTrueXML); + PolicyParser.validatePolicy(validFalseXML); + } + + /** + * Test that an XML representation of a scripted policy is valid according to the ExecutionPolicy.xsd schema. + * @throws Exception if any error occurs. + */ + @Test(timeout=5000) + public void testInvalidXML() throws Exception { + try { + PolicyParser.validatePolicy(invalidXML); + throw new IllegalStateException("the policy is invalid but passes the validation"); + } catch(final Exception e) { + assertTrue("e = " + e, e instanceof JPPFException); + } + } + + /** + * Test that the preference for node 1, then node 2 is applied properly on 2 concurrent jobs, with a policy parsed from an XML document. + * @throws Exception if any error occurs + */ + @Test(timeout=15000) + public void testTrueXMLPolicy() throws Exception { + try { + final ExecutionPolicy policy = PolicyParser.parsePolicy(validTrueXML); + assertTrue(policy instanceof Preference); + testTruePolicy(ReflectionUtils.getCurrentClassAndMethod(), policy); + } catch(final Throwable t) { + t.printStackTrace(); + if (t instanceof Exception) throw (Exception) t; + else if (t instanceof Error) throw (Error) t; + throw new RuntimeException(t); + } + } + + /** + * Test that the preference for node 1, then node 2 is applied properly on 2 concurrent jobs, with a policy built using a Java constructor. + * @throws Exception if any error occurs + */ + @Test(timeout=15000) + public void testTrueJavaPolicy() throws Exception { + testTruePolicy(ReflectionUtils.getCurrentClassAndMethod(), new Preference(new Equal("jppf.node.uuid", true, "n1"), new Equal("jppf.node.uuid", true, "n2"))); + } + + /** + * Test that the preference for node 1, then node 2 is applied properly on 2 concurrent jobs. + * @param name the name prefix for the submitted jobs. + * @param policy the execution policy to test. + * @throws Exception if any error occurs + */ + private static void testTruePolicy(final String name, final ExecutionPolicy policy) throws Exception { + JPPFConnectionPool pool = null; + while ((pool = client.getConnectionPool()) == null) Thread.sleep(10L); + try { + pool.setSize(2); + pool.awaitActiveConnections(Operator.AT_LEAST, 2); + final int nbTasks = 1; + final JPPFJob job1 = BaseTestHelper.createJob(name + " 1", false, false, nbTasks, LifeCycleTask.class, 3000L); + job1.getSLA().setExecutionPolicy(policy); + final JPPFJob job2 = BaseTestHelper.createJob(name + " 2", false, false, nbTasks, LifeCycleTask.class, 3000L); + job2.getSLA().setExecutionPolicy(policy); + // ensure job 2 is started by the server while job 1 is already executing + job2.getSLA().setJobSchedule(new JPPFSchedule(1000L)); + client.submitJob(job1); + client.submitJob(job2); + final List> results1 = job1.awaitResults(); + assertEquals(nbTasks, results1.size()); + final LifeCycleTask task1 = (LifeCycleTask) results1.get(0); + assertNotNull(task1.getResult()); + final List> results2 = job2.awaitResults(); + assertEquals(nbTasks, results2.size()); + final LifeCycleTask task2 = (LifeCycleTask) results2.get(0); + assertNotNull(task2.getResult()); + assertFalse(task1.getNodeUuid().equals(task2.getNodeUuid())); + //assertNotSame(task1.getNodeUuid(), task2.getNodeUuid()); + } finally { + pool.setSize(1); + } + } + + /** + * Test that the given scripted policy return {@code false} when its {@code accepts()} method is called. + * @throws Exception if any error occurs. + */ + @Test(timeout=10000) + public void testFalseXMLPolicy() throws Exception { + final ExecutionPolicy policy = PolicyParser.parsePolicy(validFalseXML); + assertTrue(policy instanceof Preference); + testFalsePolicy(ReflectionUtils.getCurrentClassAndMethod(), policy); + } + + /** + * Test that the given scripted policy return {@code false} when its {@code accepts()} method is called. + * @throws Exception if any error occurs. + */ + @Test(timeout=10000) + public void testFalseJavaPolicy() throws Exception { + testFalsePolicy(ReflectionUtils.getCurrentClassAndMethod(), new Preference(new Equal("jppf.node.uuid", true, "some value never seen"))); + } + + /** + * Test that the given scripted policy return {@code false} when its {@code accepts()} method is called. + * @param name the name of the submitted job. + * @param policy the execution policy to test. + * @throws Exception if any error occurs. + */ + private static void testFalsePolicy(final String name, final ExecutionPolicy policy) throws Exception { + final int nbTasks = 1; + assertTrue(policy instanceof Preference); + final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 10L); + job.getSLA().setExecutionPolicy(policy); + job.getSLA().setJobExpirationSchedule(new JPPFSchedule(2000L)); + final List> results = client.submitJob(job); + assertEquals(nbTasks, results.size()); + final LifeCycleTask task = (LifeCycleTask) results.get(0); + assertNull(task.getResult()); + assertNull(task.getNodeUuid()); + } +} diff --git a/tests/src/tests/test/org/jppf/server/job/management/TestDriverJobManagementMBean.java b/tests/src/tests/test/org/jppf/server/job/management/TestDriverJobManagementMBean.java index 5e8bdea66c..8897cb6549 100644 --- a/tests/src/tests/test/org/jppf/server/job/management/TestDriverJobManagementMBean.java +++ b/tests/src/tests/test/org/jppf/server/job/management/TestDriverJobManagementMBean.java @@ -34,6 +34,7 @@ import org.jppf.node.protocol.*; import org.jppf.server.job.management.DriverJobManagementMBean; import org.jppf.utils.*; +import org.jppf.utils.Operator; import org.jppf.utils.concurrent.ConcurrentUtils; import org.junit.*; import org.slf4j.*; diff --git a/tests/src/tests/test/org/jppf/server/peer/TestMultiServer.java b/tests/src/tests/test/org/jppf/server/peer/TestMultiServer.java index e0a50b8256..d7ba08adda 100644 --- a/tests/src/tests/test/org/jppf/server/peer/TestMultiServer.java +++ b/tests/src/tests/test/org/jppf/server/peer/TestMultiServer.java @@ -1,153 +1,154 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.org.jppf.server.peer; - -import static org.junit.Assert.*; - -import java.util.*; - -import org.jppf.client.*; -import org.jppf.client.monitoring.topology.*; -import org.jppf.node.policy.Equal; -import org.jppf.utils.*; -import org.jppf.utils.concurrent.ConcurrentUtils; -import org.jppf.utils.configuration.JPPFProperties; -import org.junit.*; - -import test.org.jppf.test.setup.*; -import test.org.jppf.test.setup.common.BaseTestHelper; - -/** - * Test a multi-server topology with 2 servers, 1 node attached to each server and 1 client. - * @author Laurent Cohen - */ -public class TestMultiServer extends AbstractNonStandardSetup { - /** */ - private static final int TIMEOUT = 10_000; - - /** - * Launches 2 drivers with 1 node attached to each and start the client. - * @throws Exception if a process could not be started. - */ - @BeforeClass - public static void setup() throws Exception { - final TestConfiguration config = createConfig("p2p"); - config.driverLog4j = "classes/tests/config/p2p/log4j-driver.properties"; - client = BaseSetup.setup(2, 2, true, true, config); - } - - /** - * Wait until each driver has 1 idle node. - * @throws Exception if any error occurs. - */ - @Before - public void instanceSetup() throws Exception { - awaitPeersInitialized(15_000L); - } - - @Override - @Test(timeout = 10000) - public void testCancelJob() throws Exception { - BaseTestHelper.printToServers(client, "start of %s()", ReflectionUtils.getCurrentMethodName()); - super.testCancelJob(); - } - - /** - * @throws Exception if any error occurs. - */ - @Test(timeout = 10000) - public void testSimpleJob() throws Exception { - BaseTestHelper.printToServers(client, "start of %s()", ReflectionUtils.getCurrentMethodName()); - super.testSimpleJob(new Equal(JPPFProperties.SERVER_PORT.getName(), 11101)); - } - - @Override - @Test(timeout = 15000) - public void testMultipleJobs() throws Exception { - BaseTestHelper.printToServers(client, "start of %s()", ReflectionUtils.getCurrentMethodName()); - super.testMultipleJobs(); - } - - @Override - @Test(timeout = 5000) - public void testNotSerializableExceptionFromNode() throws Exception { - BaseTestHelper.printToServers(client, "start of %s()", ReflectionUtils.getCurrentMethodName()); - super.testNotSerializableExceptionFromNode(); - } - - /** - * Test there there are 2 distinct connection pools, with 1 driver connection each. - * @throws Exception if any error occurs. - */ - @Test(timeout = 5000) - public void testServerConnections() throws Exception { - final List pools = client.getConnectionPools(); - Collections.sort(pools, new Comparator() { - @Override - public int compare(final JPPFConnectionPool o1, final JPPFConnectionPool o2) { - return o1.getName().compareTo(o2.getName()); - } - }); - assertNotNull(pools); - assertEquals(2, pools.size()); - for (int i=1; i<=2; i++) { - final JPPFConnectionPool pool = pools.get(i-1); - assertNotNull(pool); - assertEquals("driver" + i, pool.getName()); - assertEquals(11100 + i, pool.getDriverPort()); - final List connections = pool.getConnections(); - assertNotNull(connections); - assertEquals(1, connections.size()); - for (final JPPFClientConnection c: connections) { - assertNotNull(c); - assertNotNull(c.getStatus()); - assertEquals(JPPFClientConnectionStatus.ACTIVE, c.getStatus()); - assertTrue(c.getStatus().isWorkingStatus()); - } - } - } - - /** - * Test that the topology monuitoring API detects the peer conenctions. - * @throws Exception if any error occurs. - */ - @Test(timeout = TIMEOUT) - public void testTopologyMonitoring() throws Exception { - client.awaitConnectionPools(Operator.EQUAL, 2, Operator.AT_LEAST, 1, TIMEOUT - 500, JPPFClientConnectionStatus.workingStatuses()); - try (final TopologyManager mgr = new TopologyManager(client)) { - final ConcurrentUtils.Condition cond = new ConcurrentUtils.Condition() { - @Override - public boolean evaluate() { - final List drivers = mgr.getDrivers(); - if ((drivers != null) && (drivers.size() == 2)) { - for (final TopologyDriver driver: drivers) { - final List peers = driver.getPeers(); - if ((peers == null) || (peers.size() != 1)) return false; - } - return true; - } - return false; - } - }; - do { - Thread.sleep(50L); - } while (!cond.evaluate()); - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.org.jppf.server.peer; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.jppf.client.*; +import org.jppf.client.monitoring.topology.*; +import org.jppf.node.policy.Equal; +import org.jppf.utils.*; +import org.jppf.utils.Operator; +import org.jppf.utils.concurrent.ConcurrentUtils; +import org.jppf.utils.configuration.JPPFProperties; +import org.junit.*; + +import test.org.jppf.test.setup.*; +import test.org.jppf.test.setup.common.BaseTestHelper; + +/** + * Test a multi-server topology with 2 servers, 1 node attached to each server and 1 client. + * @author Laurent Cohen + */ +public class TestMultiServer extends AbstractNonStandardSetup { + /** */ + private static final int TIMEOUT = 10_000; + + /** + * Launches 2 drivers with 1 node attached to each and start the client. + * @throws Exception if a process could not be started. + */ + @BeforeClass + public static void setup() throws Exception { + final TestConfiguration config = createConfig("p2p"); + config.driverLog4j = "classes/tests/config/p2p/log4j-driver.properties"; + client = BaseSetup.setup(2, 2, true, true, config); + } + + /** + * Wait until each driver has 1 idle node. + * @throws Exception if any error occurs. + */ + @Before + public void instanceSetup() throws Exception { + awaitPeersInitialized(15_000L); + } + + @Override + @Test(timeout = 10000) + public void testCancelJob() throws Exception { + BaseTestHelper.printToServers(client, "start of %s()", ReflectionUtils.getCurrentMethodName()); + super.testCancelJob(); + } + + /** + * @throws Exception if any error occurs. + */ + @Test(timeout = 10000) + public void testSimpleJob() throws Exception { + BaseTestHelper.printToServers(client, "start of %s()", ReflectionUtils.getCurrentMethodName()); + super.testSimpleJob(new Equal(JPPFProperties.SERVER_PORT.getName(), 11101)); + } + + @Override + @Test(timeout = 15000) + public void testMultipleJobs() throws Exception { + BaseTestHelper.printToServers(client, "start of %s()", ReflectionUtils.getCurrentMethodName()); + super.testMultipleJobs(); + } + + @Override + @Test(timeout = 5000) + public void testNotSerializableExceptionFromNode() throws Exception { + BaseTestHelper.printToServers(client, "start of %s()", ReflectionUtils.getCurrentMethodName()); + super.testNotSerializableExceptionFromNode(); + } + + /** + * Test there there are 2 distinct connection pools, with 1 driver connection each. + * @throws Exception if any error occurs. + */ + @Test(timeout = 5000) + public void testServerConnections() throws Exception { + final List pools = client.getConnectionPools(); + Collections.sort(pools, new Comparator() { + @Override + public int compare(final JPPFConnectionPool o1, final JPPFConnectionPool o2) { + return o1.getName().compareTo(o2.getName()); + } + }); + assertNotNull(pools); + assertEquals(2, pools.size()); + for (int i=1; i<=2; i++) { + final JPPFConnectionPool pool = pools.get(i-1); + assertNotNull(pool); + assertEquals("driver" + i, pool.getName()); + assertEquals(11100 + i, pool.getDriverPort()); + final List connections = pool.getConnections(); + assertNotNull(connections); + assertEquals(1, connections.size()); + for (final JPPFClientConnection c: connections) { + assertNotNull(c); + assertNotNull(c.getStatus()); + assertEquals(JPPFClientConnectionStatus.ACTIVE, c.getStatus()); + assertTrue(c.getStatus().isWorkingStatus()); + } + } + } + + /** + * Test that the topology monuitoring API detects the peer conenctions. + * @throws Exception if any error occurs. + */ + @Test(timeout = TIMEOUT) + public void testTopologyMonitoring() throws Exception { + client.awaitConnectionPools(Operator.EQUAL, 2, Operator.AT_LEAST, 1, TIMEOUT - 500, JPPFClientConnectionStatus.workingStatuses()); + try (final TopologyManager mgr = new TopologyManager(client)) { + final ConcurrentUtils.Condition cond = new ConcurrentUtils.Condition() { + @Override + public boolean evaluate() { + final List drivers = mgr.getDrivers(); + if ((drivers != null) && (drivers.size() == 2)) { + for (final TopologyDriver driver: drivers) { + final List peers = driver.getPeers(); + if ((peers == null) || (peers.size() != 1)) return false; + } + return true; + } + return false; + } + }; + do { + Thread.sleep(50L); + } while (!cond.evaluate()); + } + } +} diff --git a/tests/src/tests/test/org/jppf/server/peer/TestMultiServerWithOrphan.java b/tests/src/tests/test/org/jppf/server/peer/TestMultiServerWithOrphan.java index 0427d79abc..0714e5a329 100644 --- a/tests/src/tests/test/org/jppf/server/peer/TestMultiServerWithOrphan.java +++ b/tests/src/tests/test/org/jppf/server/peer/TestMultiServerWithOrphan.java @@ -1,90 +1,91 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.org.jppf.server.peer; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.jppf.client.*; -import org.jppf.management.JMXDriverConnectionWrapper; -import org.jppf.node.policy.Equal; -import org.jppf.node.protocol.Task; -import org.jppf.utils.*; -import org.jppf.utils.concurrent.ConcurrentUtils; -import org.jppf.utils.concurrent.ConcurrentUtils.Condition; -import org.junit.*; - -import test.org.jppf.test.setup.*; -import test.org.jppf.test.setup.common.*; - -/** - * Test a multi-server topology with 2 servers, 1 node attached to the first server only (2nd server is an orphan) and 1 client. - * This is to test that no job will be disptached to the server without odes and hang there. - * @author Laurent Cohen - */ -public class TestMultiServerWithOrphan extends AbstractNonStandardSetup { - /** - * Launches 2 drivers with 1 node attached to the first server only and start the client. - * @throws Exception if a process could not be started. - */ - @BeforeClass() - public static void setup() throws Exception { - printOut("checking topology"); - client = BaseSetup.setup(2, 1, true, createConfig("p2p_orphan")); - printOut("topology checked OK"); - } - - /** - * @throws Exception if any error occurs. - */ - @Test(timeout = 10000) - public void testSimpleJob() throws Exception { - final List pools = client.awaitConnectionPools(Operator.AT_LEAST, 2, Operator.AT_LEAST, 1, 5000L, JPPFClientConnectionStatus.workingStatuses()); - for (final JPPFConnectionPool pool: pools) { - final int expectedNodes = "driver1".equals(pool.getName()) ? 1 : 0; - final JMXDriverConnectionWrapper jmx = pool.awaitWorkingJMXConnection(); - ConcurrentUtils.awaitCondition(new Condition() { - @Override - public boolean evaluate() { - try { - return jmx.nbIdleNodes() == expectedNodes; - } catch (@SuppressWarnings("unused") final Exception e) { - return false; - } - } - }, 5000L, true); - } - final int nbTasks = 20; - final String name = ReflectionUtils.getCurrentClassAndMethod(); - final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 1L); - // execute only on 1st server - job.getClientSLA().setExecutionPolicy(new Equal("jppf.server.port", 11101)); - final List> results = client.submitJob(job); - assertNotNull(results); - assertEquals(nbTasks, results.size()); - for (final Task t: results) { - assertTrue("task = " + t, t instanceof LifeCycleTask); - final Throwable throwable = t.getThrowable(); - assertNull("throwable for task '" + t.getId() + "' : " + ExceptionUtils.getStackTrace(throwable), throwable); - assertNotNull(t.getResult()); - assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, t.getResult()); - } - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.org.jppf.server.peer; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.jppf.client.*; +import org.jppf.management.JMXDriverConnectionWrapper; +import org.jppf.node.policy.Equal; +import org.jppf.node.protocol.Task; +import org.jppf.utils.*; +import org.jppf.utils.Operator; +import org.jppf.utils.concurrent.ConcurrentUtils; +import org.jppf.utils.concurrent.ConcurrentUtils.Condition; +import org.junit.*; + +import test.org.jppf.test.setup.*; +import test.org.jppf.test.setup.common.*; + +/** + * Test a multi-server topology with 2 servers, 1 node attached to the first server only (2nd server is an orphan) and 1 client. + * This is to test that no job will be disptached to the server without odes and hang there. + * @author Laurent Cohen + */ +public class TestMultiServerWithOrphan extends AbstractNonStandardSetup { + /** + * Launches 2 drivers with 1 node attached to the first server only and start the client. + * @throws Exception if a process could not be started. + */ + @BeforeClass() + public static void setup() throws Exception { + printOut("checking topology"); + client = BaseSetup.setup(2, 1, true, createConfig("p2p_orphan")); + printOut("topology checked OK"); + } + + /** + * @throws Exception if any error occurs. + */ + @Test(timeout = 10000) + public void testSimpleJob() throws Exception { + final List pools = client.awaitConnectionPools(Operator.AT_LEAST, 2, Operator.AT_LEAST, 1, 5000L, JPPFClientConnectionStatus.workingStatuses()); + for (final JPPFConnectionPool pool: pools) { + final int expectedNodes = "driver1".equals(pool.getName()) ? 1 : 0; + final JMXDriverConnectionWrapper jmx = pool.awaitWorkingJMXConnection(); + ConcurrentUtils.awaitCondition(new Condition() { + @Override + public boolean evaluate() { + try { + return jmx.nbIdleNodes() == expectedNodes; + } catch (@SuppressWarnings("unused") final Exception e) { + return false; + } + } + }, 5000L, true); + } + final int nbTasks = 20; + final String name = ReflectionUtils.getCurrentClassAndMethod(); + final JPPFJob job = BaseTestHelper.createJob(name, true, false, nbTasks, LifeCycleTask.class, 1L); + // execute only on 1st server + job.getClientSLA().setExecutionPolicy(new Equal("jppf.server.port", 11101)); + final List> results = client.submitJob(job); + assertNotNull(results); + assertEquals(nbTasks, results.size()); + for (final Task t: results) { + assertTrue("task = " + t, t instanceof LifeCycleTask); + final Throwable throwable = t.getThrowable(); + assertNull("throwable for task '" + t.getId() + "' : " + ExceptionUtils.getStackTrace(throwable), throwable); + assertNotNull(t.getResult()); + assertEquals(BaseTestHelper.EXECUTION_SUCCESSFUL_MESSAGE, t.getResult()); + } + } +} diff --git a/tests/src/tests/test/org/jppf/server/protocol/TestJPPFJobSLA.java b/tests/src/tests/test/org/jppf/server/protocol/TestJPPFJobSLA.java index f2168fd6c7..bca09a574d 100644 --- a/tests/src/tests/test/org/jppf/server/protocol/TestJPPFJobSLA.java +++ b/tests/src/tests/test/org/jppf/server/protocol/TestJPPFJobSLA.java @@ -37,6 +37,7 @@ import org.jppf.scheduling.JPPFSchedule; import org.jppf.server.job.management.DriverJobManagementMBean; import org.jppf.utils.*; +import org.jppf.utils.Operator; import org.jppf.utils.streams.StreamUtils; import org.junit.*; diff --git a/tests/src/tests/test/org/jppf/ssl/TestSSL.java b/tests/src/tests/test/org/jppf/ssl/TestSSL.java index 4c76d471a0..780f62f74c 100644 --- a/tests/src/tests/test/org/jppf/ssl/TestSSL.java +++ b/tests/src/tests/test/org/jppf/ssl/TestSSL.java @@ -1,105 +1,106 @@ -/* - * JPPF. - * Copyright (C) 2005-2017 JPPF Team. - * http://www.jppf.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package test.org.jppf.ssl; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.jppf.client.*; -import org.jppf.management.JMXDriverConnectionWrapper; -import org.junit.*; - -import test.org.jppf.test.setup.*; - -/** - * SSL Unit Tests with 1-way authentication. - * @author Laurent Cohen - */ -public class TestSSL extends AbstractNonStandardSetup { - /** - * Launches a driver and 1 node and start the client, - * all setup with 1-way SSL authentication. - * @throws Exception if a process could not be started. - */ - @BeforeClass - public static void setup() throws Exception { - print(true, false, "trace 1"); - client = BaseSetup.setup(1, 1, true, false, createConfig("ssl")); - try { - print(true, false, "trace 2"); - final List pools = client.awaitConnectionPools(5000L, JPPFClientConnectionStatus.workingStatuses()); - print(true, false, "trace 3"); - assertNotNull(pools); - assertFalse(pools.isEmpty()); - print(true, false, "trace 4"); - final List drivers = pools.get(0).awaitJMXConnections(Operator.AT_LEAST, 1, 5000L, true); - print(true, false, "trace 5"); - assertNotNull(drivers); - assertFalse(drivers.isEmpty()); - final JMXDriverConnectionWrapper driver = drivers.get(0); - final long start = System.nanoTime(); - boolean ok = false; - print(true, false, "trace 6"); - while (((System.nanoTime() - start) / 1_000_000L < 5000L) && !ok) { - if (driver.nbNodes() == 1) ok = true; - else Thread.sleep(10L); - } - print(true, false, "trace 7, ok = %b", ok); - assertTrue(ok); - } catch(final AssertionError e) { - e.printStackTrace(); - BaseSetup.cleanup(); - throw e; - } - } - - /** - * Test a simple job. - * @throws Exception if any error occurs. - */ - @Test(timeout = 10000) - public void testSimpleJob() throws Exception { - super.testSimpleJob(null); - } - - @Override - @Test(timeout = 15000) - public void testMultipleJobs() throws Exception { - super.testMultipleJobs(); - } - - @Override - @Test(timeout = 10000) - public void testCancelJob() throws Exception { - super.testCancelJob(); - } - - @Override - @Test(timeout = 5000) - public void testNotSerializableExceptionFromNode() throws Exception { - super.testNotSerializableExceptionFromNode(); - } - - @Override - @Test(timeout = 8000) - public void testForwardingMBean() throws Exception { - super.testForwardingMBean(); - } -} +/* + * JPPF. + * Copyright (C) 2005-2017 JPPF Team. + * http://www.jppf.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.org.jppf.ssl; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.jppf.client.*; +import org.jppf.management.JMXDriverConnectionWrapper; +import org.jppf.utils.Operator; +import org.junit.*; + +import test.org.jppf.test.setup.*; + +/** + * SSL Unit Tests with 1-way authentication. + * @author Laurent Cohen + */ +public class TestSSL extends AbstractNonStandardSetup { + /** + * Launches a driver and 1 node and start the client, + * all setup with 1-way SSL authentication. + * @throws Exception if a process could not be started. + */ + @BeforeClass + public static void setup() throws Exception { + print(true, false, "trace 1"); + client = BaseSetup.setup(1, 1, true, false, createConfig("ssl")); + try { + print(true, false, "trace 2"); + final List pools = client.awaitConnectionPools(5000L, JPPFClientConnectionStatus.workingStatuses()); + print(true, false, "trace 3"); + assertNotNull(pools); + assertFalse(pools.isEmpty()); + print(true, false, "trace 4"); + final List drivers = pools.get(0).awaitJMXConnections(Operator.AT_LEAST, 1, 5000L, true); + print(true, false, "trace 5"); + assertNotNull(drivers); + assertFalse(drivers.isEmpty()); + final JMXDriverConnectionWrapper driver = drivers.get(0); + final long start = System.nanoTime(); + boolean ok = false; + print(true, false, "trace 6"); + while (((System.nanoTime() - start) / 1_000_000L < 5000L) && !ok) { + if (driver.nbNodes() == 1) ok = true; + else Thread.sleep(10L); + } + print(true, false, "trace 7, ok = %b", ok); + assertTrue(ok); + } catch(final AssertionError e) { + e.printStackTrace(); + BaseSetup.cleanup(); + throw e; + } + } + + /** + * Test a simple job. + * @throws Exception if any error occurs. + */ + @Test(timeout = 10000) + public void testSimpleJob() throws Exception { + super.testSimpleJob(null); + } + + @Override + @Test(timeout = 15000) + public void testMultipleJobs() throws Exception { + super.testMultipleJobs(); + } + + @Override + @Test(timeout = 10000) + public void testCancelJob() throws Exception { + super.testCancelJob(); + } + + @Override + @Test(timeout = 5000) + public void testNotSerializableExceptionFromNode() throws Exception { + super.testNotSerializableExceptionFromNode(); + } + + @Override + @Test(timeout = 8000) + public void testForwardingMBean() throws Exception { + super.testForwardingMBean(); + } +}