IGNITE-45 - Examples
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/c9df98f8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/c9df98f8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/c9df98f8 Branch: refs/heads/ignite-45 Commit: c9df98f8a647067d139cbd26f591e505bbfeada5 Parents: 74009c9 Author: Valentin Kulichenko <vkuliche...@gridgain.com> Authored: Sun Mar 22 00:08:43 2015 -0700 Committer: Valentin Kulichenko <vkuliche...@gridgain.com> Committed: Sun Mar 22 00:08:43 2015 -0700 ---------------------------------------------------------------------- examples/config/example-cache.xml | 7 +- .../ComputeExecutorServiceExample.java | 71 ------- .../computegrid/ComputeScheduleExample.java | 81 -------- .../IgniteExecutorServiceExample.java | 71 +++++++ .../messaging/MessagingPingPongExample.java | 48 ++--- .../misc/schedule/ComputeScheduleExample.java | 81 ++++++++ .../examples/misc/schedule/package-info.java | 22 +++ .../servicegrid/SimpleMapServiceImpl.java | 27 ++- .../examples/servicegrid/package-info.java | 22 +++ .../ignite/examples/streaming/package-info.java | 22 +++ .../ComputeContinuousMapperExample.java | 158 ---------------- .../ComputeExecutorServiceExample.java | 69 ------- .../ComputeFibonacciContinuationExample.java | 189 ------------------- .../computegrid/ComputeReducerExample.java | 87 --------- .../computegrid/ComputeScheduleExample.java | 64 ------- .../computegrid/ComputeTaskMapExample.java | 105 ----------- .../computegrid/ComputeTaskSplitExample.java | 96 ---------- .../failover/ComputeFailoverExample.java | 137 -------------- .../failover/ComputeFailoverNodeStartup.java | 77 -------- .../computegrid/failover/package-info.java | 22 --- .../java8/computegrid/montecarlo/Credit.java | 110 ----------- .../montecarlo/CreditRiskExample.java | 149 --------------- .../montecarlo/CreditRiskManager.java | 143 -------------- .../computegrid/montecarlo/package-info.java | 22 --- .../examples/java8/datagrid/package-info.java | 22 +++ .../examples/java8/events/EventsExample.java | 132 +++++++++++++ .../examples/java8/events/package-info.java | 22 +++ .../messaging/MessagingPingPongExample.java | 111 +++++++++++ .../examples/java8/messaging/package-info.java | 22 +++ .../misc/schedule/ComputeScheduleExample.java | 64 +++++++ .../java8/misc/schedule/package-info.java | 22 +++ .../ignite/examples/java8/package-info.java | 22 +++ .../examples/java8/streaming/package-info.java | 22 +++ .../ignite/examples/BasicExamplesSelfTest.java | 3 +- .../service/GridServiceProcessor.java | 31 +-- 35 files changed, 718 insertions(+), 1635 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/config/example-cache.xml ---------------------------------------------------------------------- diff --git a/examples/config/example-cache.xml b/examples/config/example-cache.xml index f34f12c..916c452 100644 --- a/examples/config/example-cache.xml +++ b/examples/config/example-cache.xml @@ -18,7 +18,12 @@ --> <!-- - Ignite Spring configuration file to startup ignite cache. + Ignite Spring configuration file to startup Ignite cache. + + This file demonstrates how to configure cache using Spring. Provided cache + will be created on node startup. + + Use this configuration file when running HTTP REST examples (see 'examples/rest' folder). When starting a standalone node, you need to execute the following command: {IGNITE_HOME}/bin/ignite.{bat|sh} examples/config/example-cache.xml http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeExecutorServiceExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeExecutorServiceExample.java b/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeExecutorServiceExample.java deleted file mode 100644 index 3b9be86..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeExecutorServiceExample.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.computegrid; - -import org.apache.ignite.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -import java.util.concurrent.*; - -/** - * Simple example to demonstrate usage of distributed executor service provided by Ignite. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public final class ComputeExecutorServiceExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws Exception If example execution failed. - */ - @SuppressWarnings({"TooBroadScope"}) - public static void main(String[] args) throws Exception { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println(">>> Compute executor service example started."); - - // Get ignite-enabled executor service. - ExecutorService exec = ignite.executorService(); - - // Iterate through all words in the sentence and create callable jobs. - for (final String word : "Print words using runnable".split(" ")) { - // Execute runnable on some node. - exec.submit(new IgniteRunnable() { - @Override public void run() { - System.out.println(); - System.out.println(">>> Printing '" + word + "' on this node from ignite job."); - } - }); - } - - exec.shutdown(); - - // Wait for all jobs to complete (0 means no limit). - exec.awaitTermination(0, TimeUnit.MILLISECONDS); - - System.out.println(); - System.out.println(">>> Check all nodes for output (this node is also part of the cluster)."); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeScheduleExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeScheduleExample.java b/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeScheduleExample.java deleted file mode 100644 index 17812ec..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/computegrid/ComputeScheduleExample.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.computegrid; - -import org.apache.ignite.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; -import org.apache.ignite.scheduler.*; - -import java.util.concurrent.*; - -/** - * Demonstrates a cron-based {@link Runnable} execution scheduling. - * Test runnable object broadcasts a phrase to all cluster nodes every minute - * three times with initial scheduling delay equal to five seconds. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public class ComputeScheduleExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println("Compute schedule example started."); - - // Schedule output message every minute. - SchedulerFuture<?> fut = ignite.scheduler().scheduleLocal( - new Callable<Integer>() { - private int invocations; - - @Override public Integer call() { - invocations++; - - ignite.compute().broadcast( - new IgniteRunnable() { - @Override public void run() { - System.out.println(); - System.out.println("Howdy! :) "); - } - } - ); - - return invocations; - } - }, - "{5, 3} * * * * *" // Cron expression. - ); - - while (!fut.isDone()) - System.out.println(">>> Invocation #: " + fut.get()); - - System.out.println(); - System.out.println(">>> Schedule future is done and has been unscheduled."); - System.out.println(">>> Check all nodes for hello message output."); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java/org/apache/ignite/examples/datastructures/IgniteExecutorServiceExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datastructures/IgniteExecutorServiceExample.java b/examples/src/main/java/org/apache/ignite/examples/datastructures/IgniteExecutorServiceExample.java new file mode 100644 index 0000000..d8f32d3 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datastructures/IgniteExecutorServiceExample.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.ignite.examples.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.examples.*; +import org.apache.ignite.lang.*; + +import java.util.concurrent.*; + +/** + * Simple example to demonstrate usage of distributed executor service provided by Ignite. + * <p> + * Remote nodes should always be started with special configuration file which + * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. + * <p> + * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node + * with {@code examples/config/example-ignite.xml} configuration. + */ +public final class IgniteExecutorServiceExample { + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws Exception If example execution failed. + */ + @SuppressWarnings({"TooBroadScope"}) + public static void main(String[] args) throws Exception { + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(); + System.out.println(">>> Compute executor service example started."); + + // Get ignite-enabled executor service. + ExecutorService exec = ignite.executorService(); + + // Iterate through all words in the sentence and create callable jobs. + for (final String word : "Print words using runnable".split(" ")) { + // Execute runnable on some node. + exec.submit(new IgniteRunnable() { + @Override public void run() { + System.out.println(); + System.out.println(">>> Printing '" + word + "' on this node from ignite job."); + } + }); + } + + exec.shutdown(); + + // Wait for all jobs to complete (0 means no limit). + exec.awaitTermination(0, TimeUnit.MILLISECONDS); + + System.out.println(); + System.out.println(">>> Check all nodes for output (this node is also part of the cluster)."); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java/org/apache/ignite/examples/messaging/MessagingPingPongExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/messaging/MessagingPingPongExample.java b/examples/src/main/java/org/apache/ignite/examples/messaging/MessagingPingPongExample.java index 1c24f77..e4a0159 100644 --- a/examples/src/main/java/org/apache/ignite/examples/messaging/MessagingPingPongExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/messaging/MessagingPingPongExample.java @@ -20,9 +20,7 @@ package org.apache.ignite.examples.messaging; import org.apache.ignite.*; import org.apache.ignite.cluster.*; import org.apache.ignite.examples.*; -import org.apache.ignite.internal.util.lang.*; import org.apache.ignite.lang.*; -import org.apache.ignite.resources.*; import java.util.*; import java.util.concurrent.*; @@ -64,25 +62,16 @@ public class MessagingPingPongExample { // Set up remote player. ignite.message(nodeB).remoteListen(null, new IgniteBiPredicate<UUID, String>() { - /** This will be injected on node listener comes to. */ - @IgniteInstanceResource - private Ignite ignite; - @Override public boolean apply(UUID nodeId, String rcvMsg) { System.out.println("Received message [msg=" + rcvMsg + ", sender=" + nodeId + ']'); - try { - if ("PING".equals(rcvMsg)) { - ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PONG"); - - return true; // Continue listening. - } + if ("PING".equals(rcvMsg)) { + ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PONG"); - return false; // Unsubscribe. - } - catch (IgniteException e) { - throw new GridClosureException(e); + return true; // Continue listening. } + + return false; // Unsubscribe. } }); @@ -95,26 +84,21 @@ public class MessagingPingPongExample { @Override public boolean apply(UUID nodeId, String rcvMsg) { System.out.println("Received message [msg=" + rcvMsg + ", sender=" + nodeId + ']'); - try { - if (cnt.getCount() == 1) { - ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "STOP"); - - cnt.countDown(); - - return false; // Stop listening. - } - else if ("PONG".equals(rcvMsg)) - ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PING"); - else - throw new RuntimeException("Received unexpected message: " + rcvMsg); + if (cnt.getCount() == 1) { + ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "STOP"); cnt.countDown(); - return true; // Continue listening. - } - catch (IgniteException e) { - throw new GridClosureException(e); + return false; // Stop listening. } + else if ("PONG".equals(rcvMsg)) + ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PING"); + else + throw new IgniteException("Received unexpected message: " + rcvMsg); + + cnt.countDown(); + + return true; // Continue listening. } }); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java/org/apache/ignite/examples/misc/schedule/ComputeScheduleExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/misc/schedule/ComputeScheduleExample.java b/examples/src/main/java/org/apache/ignite/examples/misc/schedule/ComputeScheduleExample.java new file mode 100644 index 0000000..e087a21 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/misc/schedule/ComputeScheduleExample.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.ignite.examples.misc.schedule; + +import org.apache.ignite.*; +import org.apache.ignite.examples.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.scheduler.*; + +import java.util.concurrent.*; + +/** + * Demonstrates a cron-based {@link Runnable} execution scheduling. + * Test runnable object broadcasts a phrase to all cluster nodes every minute + * three times with initial scheduling delay equal to five seconds. + * <p> + * Remote nodes should always be started with special configuration file which + * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. + * <p> + * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node + * with {@code examples/config/example-ignite.xml} configuration. + */ +public class ComputeScheduleExample { + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(); + System.out.println("Compute schedule example started."); + + // Schedule output message every minute. + SchedulerFuture<?> fut = ignite.scheduler().scheduleLocal( + new Callable<Integer>() { + private int invocations; + + @Override public Integer call() { + invocations++; + + ignite.compute().broadcast( + new IgniteRunnable() { + @Override public void run() { + System.out.println(); + System.out.println("Howdy! :) "); + } + } + ); + + return invocations; + } + }, + "{5, 3} * * * * *" // Cron expression. + ); + + while (!fut.isDone()) + System.out.println(">>> Invocation #: " + fut.get()); + + System.out.println(); + System.out.println(">>> Schedule future is done and has been unscheduled."); + System.out.println(">>> Check all nodes for hello message output."); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java/org/apache/ignite/examples/misc/schedule/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/misc/schedule/package-info.java b/examples/src/main/java/org/apache/ignite/examples/misc/schedule/package-info.java new file mode 100644 index 0000000..5888bc2 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/misc/schedule/package-info.java @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 description. --> + * Demonstrates usage of cron-based scheduler. + */ +package org.apache.ignite.examples.misc.schedule; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java/org/apache/ignite/examples/servicegrid/SimpleMapServiceImpl.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/servicegrid/SimpleMapServiceImpl.java b/examples/src/main/java/org/apache/ignite/examples/servicegrid/SimpleMapServiceImpl.java index 5e6a9e7..5be2352 100644 --- a/examples/src/main/java/org/apache/ignite/examples/servicegrid/SimpleMapServiceImpl.java +++ b/examples/src/main/java/org/apache/ignite/examples/servicegrid/SimpleMapServiceImpl.java @@ -17,11 +17,12 @@ package org.apache.ignite.examples.servicegrid; +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.resources.*; import org.apache.ignite.services.*; -import java.util.*; -import java.util.concurrent.*; - /** * Simple service which loops infinitely and prints out a counter. */ @@ -29,8 +30,15 @@ public class SimpleMapServiceImpl<K, V> implements Service, SimpleMapService<K, /** Serial version UID. */ private static final long serialVersionUID = 0L; + /** Cache name. */ + private static final String CACHE_NAME = SimpleMapServiceImpl.class.getSimpleName(); + + /** Ignite instance. */ + @IgniteInstanceResource + private Ignite ignite; + /** Underlying cache map. */ - private Map<K, V> map; + private IgniteCache<K, V> map; /** {@inheritDoc} */ @Override public void put(K key, V val) { @@ -54,14 +62,21 @@ public class SimpleMapServiceImpl<K, V> implements Service, SimpleMapService<K, /** {@inheritDoc} */ @Override public void cancel(ServiceContext ctx) { + ignite.destroyCache(CACHE_NAME); + System.out.println("Service was cancelled: " + ctx.name()); } /** {@inheritDoc} */ @Override public void init(ServiceContext ctx) throws Exception { - System.out.println("Service was initialized: " + ctx.name()); + CacheConfiguration<K, V> cfg = new CacheConfiguration<>(); + + cfg.setCacheMode(CacheMode.PARTITIONED); + cfg.setName(CACHE_NAME); - map = new ConcurrentHashMap<>(); + map = ignite.getOrCreateCache(cfg); + + System.out.println("Service was initialized: " + ctx.name()); } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java/org/apache/ignite/examples/servicegrid/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/servicegrid/package-info.java b/examples/src/main/java/org/apache/ignite/examples/servicegrid/package-info.java new file mode 100644 index 0000000..5873209 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/servicegrid/package-info.java @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 description. --> + * Demonstrates usage of grid-managed services. + */ +package org.apache.ignite.examples.servicegrid; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java/org/apache/ignite/examples/streaming/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/streaming/package-info.java b/examples/src/main/java/org/apache/ignite/examples/streaming/package-info.java new file mode 100644 index 0000000..43dea13 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/streaming/package-info.java @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 description. --> + * Demonstrates usage of data streamer. + */ +package org.apache.ignite.examples.streaming; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeContinuousMapperExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeContinuousMapperExample.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeContinuousMapperExample.java deleted file mode 100644 index 9d16b35..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeContinuousMapperExample.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid; - -import org.apache.ignite.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.compute.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.resources.*; - -import java.util.*; -import java.util.concurrent.*; -import java.util.concurrent.atomic.*; - -/** - * Demonstrates usage of continuous mapper. With continuous mapper - * it is possible to continue mapping jobs asynchronously even after - * initial {@link ComputeTask#map(List, Object)} method completes. - * <p> - * String "Hello Continuous Mapper" is passed as an argument for execution - * of {@link ComputeContinuousMapperExample.ContinuousMapperTask}. As an outcome, participating - * nodes will print out a single word from the passed in string and return - * number of characters in that word. However, to demonstrate continuous - * mapping, next word will be mapped to a node only after the result from - * previous word has been received. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public class ComputeContinuousMapperExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - System.out.println(); - System.out.println(">>> Compute continuous mapper example started."); - - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - int phraseLen = ignite.compute().execute(ContinuousMapperTask.class, "Hello Continuous Mapper"); - - System.out.println(); - System.out.println(">>> Total number of characters in the phrase is '" + phraseLen + "'."); - } - } - - /** - * This task demonstrates how continuous mapper is used. The passed in phrase - * is split into multiple words and next word is sent out for processing only - * when the result for the previous word was received. - * <p> - * Note that annotation {@link ComputeTaskNoResultCache} is optional and tells Ignite - * not to accumulate results from individual jobs. In this example we increment - * total character count directly in {@link #result(ComputeJobResult, List)} method, - * and therefore don't need to accumulate them be be processed at reduction step. - */ - @ComputeTaskNoResultCache - private static class ContinuousMapperTask extends ComputeTaskAdapter<String, Integer> { - /** This field will be injected with task continuous mapper. */ - @TaskContinuousMapperResource - private ComputeTaskContinuousMapper mapper; - - /** Word queue. */ - private final Queue<String> words = new ConcurrentLinkedQueue<>(); - - /** Total character count. */ - private final AtomicInteger totalChrCnt = new AtomicInteger(0); - - /** {@inheritDoc} */ - @Override public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> nodes, String phrase) { - if (phrase == null || phrase.isEmpty()) - throw new IgniteException("Phrase is empty."); - - // Populate word queue. - Collections.addAll(words, phrase.split(" ")); - - // Sends first word. - sendWord(); - - // Since we have sent at least one job, we are allowed to return - // 'null' from map method. - return null; - } - - /** {@inheritDoc} */ - @Override public ComputeJobResultPolicy result(ComputeJobResult res, List<ComputeJobResult> rcvd) { - // If there is an error, fail-over to another node. - if (res.getException() != null) - return super.result(res, rcvd); - - // Add result to total character count. - totalChrCnt.addAndGet(res.<Integer>getData()); - - sendWord(); - - // If next word was sent, keep waiting, otherwise work queue is empty and we reduce. - return ComputeJobResultPolicy.WAIT; - } - - /** {@inheritDoc} */ - @Override public Integer reduce(List<ComputeJobResult> results) { - return totalChrCnt.get(); - } - - /** - * Sends next queued word to the next node implicitly selected by load balancer. - */ - private void sendWord() { - // Remove first word from the queue. - String word = words.poll(); - - if (word != null) { - // Map next word. - mapper.send(new ComputeJobAdapter(word) { - @Override public Object execute() { - String word = argument(0); - - System.out.println(); - System.out.println(">>> Printing '" + word + "' from ignite job at time: " + new Date()); - - int cnt = word.length(); - - // Sleep for some time so it will be visually noticeable that - // jobs are executed sequentially. - try { - Thread.sleep(1000); - } - catch (InterruptedException ignored) { - // No-op. - } - - return cnt; - } - }); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeExecutorServiceExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeExecutorServiceExample.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeExecutorServiceExample.java deleted file mode 100644 index 00f0c05..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeExecutorServiceExample.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid; - -import org.apache.ignite.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -import java.util.concurrent.*; - -/** - * Simple example to demonstrate usage of distributed executor service provided by Ignite. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public final class ComputeExecutorServiceExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws Exception If example execution failed. - */ - @SuppressWarnings({"TooBroadScope"}) - public static void main(String[] args) throws Exception { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println(">>> Compute executor service example started."); - - // Get ignite-enabled executor service. - ExecutorService exec = ignite.executorService(); - - // Iterate through all words in the sentence and create callable jobs. - for (String word : "Print words using runnable".split(" ")) { - // Execute runnable on some node. - exec.submit((IgniteRunnable)() -> { - System.out.println(); - System.out.println(">>> Printing '" + word + "' on this node from ignite job."); - }); - } - - exec.shutdown(); - - // Wait for all jobs to complete (0 means no limit). - exec.awaitTermination(0, TimeUnit.MILLISECONDS); - - System.out.println(); - System.out.println(">>> Check all nodes for output (this node is also part of the cluster)."); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeFibonacciContinuationExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeFibonacciContinuationExample.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeFibonacciContinuationExample.java deleted file mode 100644 index 2e68cc5..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeFibonacciContinuationExample.java +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid; - -import org.apache.ignite.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.compute.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; -import org.apache.ignite.resources.*; -import org.jetbrains.annotations.*; - -import java.math.*; -import java.util.*; -import java.util.concurrent.*; - -/** - * This example demonstrates how to use continuation feature of Ignite by - * performing the distributed recursive calculation of {@code 'Fibonacci'} - * numbers on the cluster. Continuations - * functionality is exposed via {@link ComputeJobContext#holdcc()} and - * {@link ComputeJobContext#callcc()} method calls in {@link ComputeFibonacciContinuationExample.FibonacciClosure} class. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public final class ComputeFibonacciContinuationExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println("Compute Fibonacci continuation example started."); - - long N = 100; - - UUID exampleNodeId = ignite.cluster().localNode().id(); - - // Filter to exclude this node from execution. - IgnitePredicate<ClusterNode> nodeFilter = n -> - ignite.cluster().forRemotes().nodes().isEmpty() || !n.id().equals(exampleNodeId); - - long start = System.currentTimeMillis(); - - BigInteger fib = ignite.compute(ignite.cluster().forPredicate(nodeFilter)). - apply(new FibonacciClosure(nodeFilter), N); - - long duration = System.currentTimeMillis() - start; - - System.out.println(); - System.out.println(">>> Finished executing Fibonacci for '" + N + "' in " + duration + " ms."); - System.out.println(">>> Fibonacci sequence for input number '" + N + "' is '" + fib + "'."); - System.out.println(">>> If you re-run this example w/o stopping remote nodes - the performance will"); - System.out.println(">>> increase since intermediate results are pre-cache on remote nodes."); - System.out.println(">>> You should see prints out every recursive Fibonacci execution on cluster nodes."); - System.out.println(">>> Check remote nodes for output."); - } - } - - /** - * Closure to execute. - */ - private static class FibonacciClosure implements IgniteClosure<Long, BigInteger> { - /** Future for spawned task. */ - private IgniteFuture<BigInteger> fut1; - - /** Future for spawned task. */ - private IgniteFuture<BigInteger> fut2; - - /** Auto-inject job context. */ - @JobContextResource - private ComputeJobContext jobCtx; - - /** Auto-inject ignite instance. */ - @IgniteInstanceResource - private Ignite ignite; - - /** Predicate. */ - private final IgnitePredicate<ClusterNode> nodeFilter; - - /** - * @param nodeFilter Predicate to filter nodes. - */ - FibonacciClosure(IgnitePredicate<ClusterNode> nodeFilter) { - this.nodeFilter = nodeFilter; - } - - /** {@inheritDoc} */ - @Nullable @Override public BigInteger apply(Long n) { - if (fut1 == null || fut2 == null) { - System.out.println(); - System.out.println(">>> Starting fibonacci execution for number: " + n); - - // Make sure n is not negative. - n = Math.abs(n); - - if (n <= 2) - return n == 0 ? BigInteger.ZERO : BigInteger.ONE; - - // Node-local storage. - ConcurrentMap<Long, IgniteFuture<BigInteger>> locMap = ignite.cluster().nodeLocalMap(); - - // Check if value is cached in node-local-map first. - fut1 = locMap.get(n - 1); - fut2 = locMap.get(n - 2); - - ClusterGroup p = ignite.cluster().forPredicate(nodeFilter); - - IgniteCompute compute = ignite.compute(p).withAsync(); - - // If future is not cached in node-local-map, cache it. - if (fut1 == null) { - compute.apply(new FibonacciClosure(nodeFilter), n - 1); - - ComputeTaskFuture<BigInteger> futVal = compute.future(); - - fut1 = locMap.putIfAbsent(n - 1, futVal); - - if (fut1 == null) - fut1 = futVal; - } - - // If future is not cached in node-local-map, cache it. - if (fut2 == null) { - compute.apply(new FibonacciClosure(nodeFilter), n - 2); - - ComputeTaskFuture<BigInteger> futVal = compute.<BigInteger>future(); - - fut2 = locMap.putIfAbsent(n - 2, futVal); - - if (fut2 == null) - fut2 = futVal; - } - - // If futures are not done, then wait asynchronously for the result - if (!fut1.isDone() || !fut2.isDone()) { - IgniteInClosure<IgniteFuture<BigInteger>> lsnr = f -> { - // If both futures are done, resume the continuation. - if (fut1.isDone() && fut2.isDone()) - // CONTINUATION: - // ============= - // Resume suspended job execution. - jobCtx.callcc(); - }; - - // CONTINUATION: - // ============= - // Hold (suspend) job execution. - // It will be resumed in listener above via 'callcc()' call - // once both futures are done. - jobCtx.holdcc(); - - // Attach the same listener to both futures. - fut1.listen(lsnr); - fut2.listen(lsnr); - - return null; - } - } - - assert fut1.isDone() && fut2.isDone(); - - // Return cached results. - return fut1.get().add(fut2.get()); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeReducerExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeReducerExample.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeReducerExample.java deleted file mode 100644 index 3248de5..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeReducerExample.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid; - -import org.apache.ignite.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -import java.util.*; -import java.util.concurrent.atomic.*; - -/** - * Demonstrates a simple use of Ignite with reduce closure. - * <p> - * Phrase is split into words and distributed across nodes where length of each word is - * calculated. Then total phrase length is calculated using reducer. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start Ignite node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public class ComputeReducerExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println("Compute reducer example started."); - - Integer sum = ignite.compute().apply( - word -> { - System.out.println(); - System.out.println(">>> Printing '" + word + "' on this node from ignite job."); - - // Return number of letters in the word. - return word.length(); - }, - - // Job parameters. Ignite will create as many jobs as there are parameters. - Arrays.asList("Count characters using reducer".split(" ")), - - // Reducer to process results as they come. - new IgniteReducer<Integer, Integer>() { - private AtomicInteger sum = new AtomicInteger(); - - // Callback for every job result. - @Override public boolean collect(Integer len) { - sum.addAndGet(len); - - // Return true to continue waiting until all results are received. - return true; - } - - // Reduce all results into one. - @Override public Integer reduce() { - return sum.get(); - } - } - ); - - System.out.println(); - System.out.println(">>> Total number of characters in the phrase is '" + sum + "'."); - System.out.println(">>> Check all nodes for output (this node is also part of the cluster)."); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeScheduleExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeScheduleExample.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeScheduleExample.java deleted file mode 100644 index ebc4bd55..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeScheduleExample.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid; - -import org.apache.ignite.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.scheduler.*; - -/** - * Demonstrates a cron-based {@link Runnable} execution scheduling. - * Test runnable object broadcasts a phrase to all cluster nodes every minute - * three times with initial scheduling delay equal to five seconds. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public class ComputeScheduleExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println("Compute schedule example started."); - - // Schedule output message every minute. - SchedulerFuture<?> fut = ignite.scheduler().scheduleLocal(() -> - ignite.compute().broadcast(() -> { - System.out.println(); - System.out.println("Howdy! :) "); - }), - "{5, 3} * * * * *" // Cron expression. - ); - - while (!fut.isDone()) - System.out.println(">>> Invocation #: " + fut.get()); - - System.out.println(); - System.out.println(">>> Schedule future is done and has been unscheduled."); - System.out.println(">>> Check all nodes for hello message output."); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeTaskMapExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeTaskMapExample.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeTaskMapExample.java deleted file mode 100644 index d190b2b..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeTaskMapExample.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid; - -import org.apache.ignite.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.compute.*; -import org.apache.ignite.examples.*; - -import java.util.*; - -/** - * Demonstrates a simple use of Ignite with - * {@link ComputeTaskAdapter}. - * <p> - * Phrase passed as task argument is split into words on map stage and distributed among cluster nodes. - * Each node computes word length and returns result to master node where total phrase length is - * calculated on reduce stage. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public class ComputeTaskMapExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println("Compute task map example started."); - - // Execute task on the cluster and wait for its completion. - int cnt = ignite.compute().execute(CharacterCountTask.class, "Hello Ignite Enabled World!"); - - System.out.println(); - System.out.println(">>> Total number of characters in the phrase is '" + cnt + "'."); - System.out.println(">>> Check all nodes for output (this node is also part of the cluster)."); - } - } - - /** - * Task to count non-white-space characters in a phrase. - */ - private static class CharacterCountTask extends ComputeTaskAdapter<String, Integer> { - /** - * Splits the received string to words, creates a child job for each word, and sends - * these jobs to other nodes for processing. Each such job simply prints out the received word. - * - * @param nodes Nodes available for this task execution. - * @param arg String to split into words for processing. - * @return Map of jobs to nodes. - */ - @Override public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> nodes, String arg) { - Map<ComputeJob, ClusterNode> map = new HashMap<>(); - - Iterator<ClusterNode> it = nodes.iterator(); - - for (final String word : arg.split(" ")) { - // If we used all nodes, restart the iterator. - if (!it.hasNext()) - it = nodes.iterator(); - - ClusterNode node = it.next(); - - map.put(new ComputeJobAdapter() { - @Override public Object execute() { - System.out.println(); - System.out.println(">>> Printing '" + word + "' on this node from ignite job."); - - // Return number of letters in the word. - return word.length(); - } - }, node); - } - - return map; - } - - /** {@inheritDoc} */ - @Override public Integer reduce(List<ComputeJobResult> results) { - return results.stream().mapToInt(ComputeJobResult::getData).sum(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeTaskSplitExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeTaskSplitExample.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeTaskSplitExample.java deleted file mode 100644 index de172ae..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/ComputeTaskSplitExample.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid; - -import org.apache.ignite.*; -import org.apache.ignite.compute.*; -import org.apache.ignite.examples.*; - -import java.util.*; - -/** - * Demonstrates a simple use of Ignite with {@link ComputeTaskSplitAdapter}. - * <p> - * Phrase passed as task argument is split into jobs each taking one word. Then jobs are distributed among - * cluster nodes. Each node computes word length and returns result to master node where total phrase length - * is calculated on reduce stage. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public class ComputeTaskSplitExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println("Compute task split example started."); - - // Execute task on the cluster and wait for its completion. - int cnt = ignite.compute().execute(CharacterCountTask.class, "Hello Ignite Enabled World!"); - - System.out.println(); - System.out.println(">>> Total number of characters in the phrase is '" + cnt + "'."); - System.out.println(">>> Check all nodes for output (this node is also part of the cluster)."); - } - } - - /** - * Task to count non-white-space characters in a phrase. - */ - private static class CharacterCountTask extends ComputeTaskSplitAdapter<String, Integer> { - /** - * Splits the received string to words, creates a child job for each word, and sends - * these jobs to other nodes for processing. Each such job simply prints out the received word. - * - * @param clusterSize Number of available cluster nodes. Note that returned number of - * jobs can be less, equal or greater than this cluster size. - * @param arg Task execution argument. Can be {@code null}. - * @return The list of child jobs. - */ - @Override protected Collection<? extends ComputeJob> split(int clusterSize, String arg) { - Collection<ComputeJob> jobs = new LinkedList<>(); - - for (final String word : arg.split(" ")) { - jobs.add(new ComputeJobAdapter() { - @Override public Object execute() { - System.out.println(); - System.out.println(">>> Printing '" + word + "' on this node from ignite job."); - - // Return number of letters in the word. - return word.length(); - } - }); - } - - return jobs; - } - - /** {@inheritDoc} */ - @Override public Integer reduce(List<ComputeJobResult> results) { - return results.stream().mapToInt(ComputeJobResult::getData).sum(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/ComputeFailoverExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/ComputeFailoverExample.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/ComputeFailoverExample.java deleted file mode 100644 index ce2f721..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/ComputeFailoverExample.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid.failover; - -import org.apache.ignite.*; -import org.apache.ignite.compute.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; -import org.apache.ignite.resources.*; - -import java.util.*; - -/** - * Demonstrates the usage of checkpoints in Ignite. - * <p> - * The example tries to compute phrase length. In order to mitigate possible node failures, intermediate - * result is saved as as checkpoint after each job step. - * <p> - * Remote nodes must be started using {@link ComputeFailoverNodeStartup}. - */ -public class ComputeFailoverExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException{ - try (Ignite ignite = Ignition.start(ComputeFailoverNodeStartup.configuration())) { - if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2)) - return; - - System.out.println(); - System.out.println("Compute failover example started."); - - // Number of letters. - int charCnt = ignite.compute().apply(new CheckPointJob(), "Stage1 Stage2"); - - System.out.println(); - System.out.println(">>> Finished executing fail-over example with checkpoints."); - System.out.println(">>> Total number of characters in the phrase is '" + charCnt + "'."); - System.out.println(">>> You should see exception stack trace from failed job on some node."); - System.out.println(">>> Failed job will be failed over to another node."); - } - } - - @ComputeTaskSessionFullSupport - private static final class CheckPointJob implements IgniteClosure<String, Integer> { - /** Injected distributed task session. */ - @TaskSessionResource - private ComputeTaskSession jobSes; - - /** Injected ignite logger. */ - @LoggerResource - private IgniteLogger log; - - /** */ - private IgniteBiTuple<Integer, Integer> state; - - /** */ - private String phrase; - - /** - * The job will check the checkpoint with key '{@code fail}' and if - * it's {@code true} it will throw exception to simulate a failure. - * Otherwise, it will execute enabled method. - */ - @Override public Integer apply(String phrase) { - System.out.println(); - System.out.println(">>> Executing fail-over example job."); - - this.phrase = phrase; - - List<String> words = Arrays.asList(phrase.split(" ")); - - final String cpKey = checkpointKey(); - - IgniteBiTuple<Integer, Integer> state = jobSes.loadCheckpoint(cpKey); - - int idx = 0; - int sum = 0; - - if (state != null) { - this.state = state; - - // Last processed word index and total length. - idx = state.get1(); - sum = state.get2(); - } - - for (int i = idx; i < words.size(); i++) { - sum += words.get(i).length(); - - this.state = new IgniteBiTuple<>(i + 1, sum); - - // Save checkpoint with scope of task execution. - // It will be automatically removed when task completes. - jobSes.saveCheckpoint(cpKey, this.state); - - // For example purposes, we fail on purpose after first stage. - // This exception will cause job to be failed over to another node. - if (i == 0) { - System.out.println(); - System.out.println(">>> Job will be failed over to another node."); - - throw new ComputeJobFailoverException("Expected example job exception."); - } - } - - return sum; - } - - /** - * Make reasonably unique checkpoint key. - * - * @return Checkpoint key. - */ - private String checkpointKey() { - return CheckPointJob.class.getName() + '-' + phrase; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/ComputeFailoverNodeStartup.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/ComputeFailoverNodeStartup.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/ComputeFailoverNodeStartup.java deleted file mode 100644 index a67c9fc..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/ComputeFailoverNodeStartup.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid.failover; - -import org.apache.ignite.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.spi.checkpoint.sharedfs.*; -import org.apache.ignite.spi.discovery.tcp.*; -import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; - -import java.util.*; - -/** - * Starts up an empty node with checkpoint-enabled configuration. - * <p> - * The difference is that running this class from IDE adds all example classes to classpath - * but running from command line doesn't. - */ -public class ComputeFailoverNodeStartup { - /** - * Start up an empty node with specified configuration. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - Ignition.start(configuration()); - } - - /** - * Create Ignite configuration with configured checkpoints. - * - * @return Ignite configuration. - * @throws IgniteException If configuration creation failed. - */ - public static IgniteConfiguration configuration() throws IgniteException { - IgniteConfiguration cfg = new IgniteConfiguration(); - - cfg.setLocalHost("127.0.0.1"); - cfg.setPeerClassLoadingEnabled(true); - - // Configure checkpoint SPI. - SharedFsCheckpointSpi checkpointSpi = new SharedFsCheckpointSpi(); - - checkpointSpi.setDirectoryPaths(Collections.singletonList("work/checkpoint/sharedfs")); - - cfg.setCheckpointSpi(checkpointSpi); - - // Configure discovery SPI. - TcpDiscoverySpi discoSpi = new TcpDiscoverySpi(); - - TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder(); - - ipFinder.setAddresses(Arrays.asList("127.0.0.1:47500..47509")); - - discoSpi.setIpFinder(ipFinder); - - cfg.setDiscoverySpi(discoSpi); - - return cfg; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/package-info.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/package-info.java deleted file mode 100644 index 876965f..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/failover/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 description. --> - * Compute failover example. - */ -package org.apache.ignite.examples.java8.computegrid.failover; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/montecarlo/Credit.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/montecarlo/Credit.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/montecarlo/Credit.java deleted file mode 100644 index 50d17e0..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/montecarlo/Credit.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid.montecarlo; - -import java.io.*; - -/** - * This class provides a simple model for a credit contract (or a loan). It is basically - * defines as remaining crediting amount to date, credit remaining term, APR and annual - * probability on default. Although this model is simplified for the purpose - * of this example, it is close enough to emulate the real-life credit - * risk assessment application. - */ -public class Credit implements Serializable { - /** Remaining crediting amount. */ - private final double remAmnt; - - /** Remaining crediting remTerm. */ - private final int remTerm; - - /** Annual percentage rate (APR). */ - private final double apr; - - /** Expected annual probability of default (EaDF). */ - private final double edf; - - /** - * Creates new credit instance with given information. - * - * @param remAmnt Remained crediting amount. - * @param remTerm Remained crediting remTerm. - * @param apr Annual percentage rate (APR). - * @param edf Expected annual probability of default (EaDF). - */ - public Credit(double remAmnt, int remTerm, double apr, double edf) { - this.remAmnt = remAmnt; - this.remTerm = remTerm; - this.apr = apr; - this.edf = edf; - } - - /** - * Gets remained crediting amount. - * - * @return Remained amount of credit. - */ - double getRemainingAmount() { - return remAmnt; - } - - /** - * Gets remained crediting remTerm. - * - * @return Remained crediting remTerm in days. - */ - int getRemainingTerm() { - return remTerm; - } - - /** - * Gets annual percentage rate. - * - * @return Annual percentage rate in relative percents (percentage / 100). - */ - double getAnnualRate() { - return apr; - } - - /** - * Gets either credit probability of default for the given period of time - * if remaining term is less than crediting time or probability of default - * for whole remained crediting time. - * - * @param term Default term. - * @return Credit probability of default in relative percents - * (percentage / 100). - */ - double getDefaultProbability(int term) { - return 1 - Math.exp(Math.log(1 - edf) * Math.min(remTerm, term) / 365.0); - } - - /** {@inheritDoc} */ - @Override public String toString() { - StringBuilder buf = new StringBuilder(); - - buf.append(getClass().getName()); - buf.append(" [remAmnt=").append(remAmnt); - buf.append(", remTerm=").append(remTerm); - buf.append(", apr=").append(apr); - buf.append(", edf=").append(edf); - buf.append(']'); - - return buf.toString(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c9df98f8/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/montecarlo/CreditRiskExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/montecarlo/CreditRiskExample.java b/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/montecarlo/CreditRiskExample.java deleted file mode 100644 index 84e051f..0000000 --- a/examples/src/main/java8/org/apache/ignite/examples/java8/computegrid/montecarlo/CreditRiskExample.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.ignite.examples.java8.computegrid.montecarlo; - -import org.apache.ignite.*; -import org.apache.ignite.examples.*; -import org.apache.ignite.lang.*; - -import java.util.*; - -/** - * Monte-Carlo example. Demonstrates distributed credit risk calculation. - * <p> - * Remote nodes should always be started with special configuration file which - * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. - * <p> - * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node - * with {@code examples/config/example-ignite.xml} configuration. - */ -public final class CreditRiskExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws IgniteException If example execution failed. - */ - public static void main(String[] args) throws IgniteException { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println("Credit risk example started."); - - // Create portfolio. - Credit[] portfolio = new Credit[5000]; - - Random rnd = new Random(); - - // Generate some test portfolio items. - for (int i = 0; i < portfolio.length; i++) { - portfolio[i] = new Credit( - 50000 * rnd.nextDouble(), // Credit amount. - rnd.nextInt(1000), // Credit term in days. - rnd.nextDouble() / 10, // APR. - rnd.nextDouble() / 20 + 0.02 // EDF. - ); - } - - // Forecast horizon in days. - int horizon = 365; - - // Number of Monte-Carlo iterations. - int iter = 10000; - - // Percentile. - double percentile = 0.95; - - // Mark the stopwatch. - long start = System.currentTimeMillis(); - - // Calculate credit risk and print it out. - // As you can see the ignite enabling is completely hidden from the caller - // and it is fully transparent to him. In fact, the caller is never directly - // aware if method was executed just locally or on the 100s of cluster nodes. - // Credit risk crdRisk is the minimal amount that creditor has to have - // available to cover possible defaults. - - double crdRisk = ignite.compute().call( - jobs(ignite.cluster().nodes().size(), portfolio, horizon, iter, percentile), - new IgniteReducer<Double, Double>() { - /** Collected values sum. */ - private double sum; - - /** Collected values count. */ - private int cnt; - - /** {@inheritDoc} */ - @Override public synchronized boolean collect(Double e) { - sum += e; - cnt++; - - return true; - } - - /** {@inheritDoc} */ - @Override public synchronized Double reduce() { - return sum / cnt; - } - }); - - System.out.println(); - System.out.println("Credit risk [crdRisk=" + crdRisk + ", duration=" + - (System.currentTimeMillis() - start) + "ms]"); - } - // We specifically don't do any error handling here to - // simplify the example. Real application may want to - // add error handling and application specific recovery. - } - - /** - * Creates closures for calculating credit risks. - * - * @param clusterSize Size of the cluster. - * @param portfolio Portfolio. - * @param horizon Forecast horizon in days. - * @param iter Number of Monte-Carlo iterations. - * @param percentile Percentile. - * @return Collection of closures. - */ - private static Collection<IgniteCallable<Double>> jobs(int clusterSize, final Credit[] portfolio, - final int horizon, int iter, final double percentile) { - // Number of iterations should be done by each node. - int iterPerNode = Math.round(iter / (float)clusterSize); - - // Number of iterations for the last/the only node. - int lastNodeIter = iter - (clusterSize - 1) * iterPerNode; - - Collection<IgniteCallable<Double>> clos = new ArrayList<>(clusterSize); - - // Note that for the purpose of this example we perform a simple homogeneous - // (non weighted) split assuming that all computing resources in this split - // will be identical. In real life scenarios when heterogeneous environment - // is used a split that is weighted by, for example, CPU benchmarks of each - // node in the split will be more efficient. It is fairly easy addition and - // Ignite comes with convenient Spring-compatible benchmark that can be - // used for weighted splits. - for (int i = 0; i < clusterSize; i++) { - final int nodeIter = i == clusterSize - 1 ? lastNodeIter : iterPerNode; - - clos.add(() -> new CreditRiskManager().calculateCreditRiskMonteCarlo( - portfolio, horizon, nodeIter, percentile)); - } - - return clos; - } -}