/**
   * Execute {@code HelloWorld} example on the grid.
   *
   * @param args Command line arguments, none required but if provided first one should point to the
   *     Spring XML configuration file. See {@code "examples/config/"} for configuration file
   *     examples.
   * @throws GridException If example execution failed.
   */
  public static void main(String[] args) throws GridException {
    if (args.length == 0) {
      G.start();
    } else {
      G.start(args[0]);
    }

    try {
      // Broadcast this message to all nodes using GridClosure.
      broadcastWordsClosure("Broadcasting This Message To All Nodes!");

      // Print individual words from this phrase on different nodes
      // using GridClosure
      spreadWordsClosure("Print Worlds Functional Style!");

      // Print this message using anonymous runnable object.
      unicastWordsRunnable("Printing This Message From Runnable!");

      // Split the message into words and pass them as arguments
      // for remote execution of Callable objects.
      countLettersCallable("Letter Count With Callable!");

      // Split the message into words and pass them as arguments
      // for remote execution of GridClosure objects.
      countLettersClosure("Letter Count With Closure!");

      // Split the message into words and pass them as arguments
      // for remote execution of GridClosure objects and
      // then aggregate results using GridReducer.
      countLettersReducer("Letter Count With Reducer!");
    } finally {
      G.stop(true);
    }
  }
  /** @throws Exception If failed. */
  @SuppressWarnings("unchecked")
  public void testCancel() throws Exception {
    Grid grid = G.grid(getTestGridName());

    grid.compute()
        .localDeployTask(GridCancelTestTask.class, U.detectClassLoader(GridCancelTestTask.class));

    GridComputeTaskFuture<?> fut = grid.compute().execute(GridCancelTestTask.class.getName(), null);

    // Wait until jobs begin execution.
    boolean await = startSignal.await(WAIT_TIME, TimeUnit.MILLISECONDS);

    assert await : "Jobs did not start.";

    info("Test task result: " + fut);

    assert fut != null;

    // Only first job should successfully complete.
    Object res = fut.get();
    assert (Integer) res == 1;

    // Wait for all jobs to finish.
    await = stopSignal.await(WAIT_TIME, TimeUnit.MILLISECONDS);
    assert await : "Jobs did not stop.";

    // One is definitely processed. But there might be some more processed or cancelled or processed
    // and cancelled.
    // Thus total number should be at least SPLIT_COUNT and at most (SPLIT_COUNT - 1) *2 +1
    assert (cancelCnt + processedCnt) >= SPLIT_COUNT
            && (cancelCnt + processedCnt) <= (SPLIT_COUNT - 1) * 2 + 1
        : "Invalid cancel count value: " + cancelCnt;
  }
  /**
   * Prints a phrase on the grid nodes running anonymous callable objects and calculating total
   * number of letters.
   *
   * @param phrase Phrase to print on of the grid nodes.
   * @throws GridException If failed.
   */
  private static void countLettersCallable(String phrase) throws GridException {
    X.println(">>> Starting countLettersCallable() example...");

    Collection<Callable<Integer>> calls = new HashSet<Callable<Integer>>();

    for (final String word : phrase.split(" "))
      calls.add(
          new GridCallable<Integer>() { // Create executable logic.
            @Override
            public Integer call() throws Exception {
              // Print out a given word, just so we can
              // see which node is doing what.
              X.println(">>> Executing word: " + word);

              // Return the length of a given word, i.e. number of letters.
              return word.length();
            }
          });

    // Explicitly execute the collection of callable objects and receive a result.
    Collection<Integer> results = G.grid().call(SPREAD, calls);

    // Add up all results using convenience 'sum()' method on GridFunc class.
    int letterCnt = F.sum(results);

    X.println(">>>");
    X.println(
        ">>> Finished execution of counting letters with callables based on GridGain 3.0 API.");
    X.println(">>> You should see the phrase '" + phrase + "' printed out on the nodes.");
    X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /**
   * Prints a phrase on the grid nodes running anonymous closure objects and calculating total
   * number of letters.
   *
   * @param phrase Phrase to print on of the grid nodes.
   * @throws GridException If failed.
   */
  private static void countLettersClosure(String phrase) throws GridException {
    X.println(">>> Starting countLettersClosure() example...");

    // Explicitly execute the collection of callable objects and receive a result.
    Collection<Integer> results =
        G.grid()
            .call(
                SPREAD,
                new GridClosure<String, Integer>() { // Create executable logic.
                  @Override
                  public Integer apply(String word) {
                    // Print out a given word, just so we can
                    // see which node is doing what.
                    X.println(">>> Executing word: " + word);

                    // Return the length of a given word, i.e. number of letters.
                    return word.length();
                  }
                },
                Arrays.asList(phrase.split(" "))); // Collection of arguments for closures.

    // Add up all results using convenience 'sum()' method.
    int letterCnt = F.sum(results);

    X.println(">>>");
    X.println(">>> Finished execution of counting letters with closure based on GridGain 3.0 API.");
    X.println(">>> You should see the phrase '" + phrase + "' printed out on the nodes.");
    X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /**
   * Prints every word a phrase on different nodes.
   *
   * @param phrase Phrase from which to print words on different nodes.
   * @throws GridException If failed.
   */
  private static void spreadWordsClosure(String phrase) throws GridException {
    X.println(">>> Starting spreadWordsClosure() example...");

    // Splits the passed in phrase into words and prints every word
    // on a individual grid node. If there are more words than nodes -
    // some nodes will print more than one word.
    G.grid()
        .run(
            SPREAD,
            F.yield(
                phrase.split(" "),
                new GridInClosure<String>() {
                  @Override
                  public void apply(String word) {
                    X.println(word);
                  }
                }));

    // NOTE:
    //
    // Alternatively, you can use existing closure 'F.println()' to
    // print any yield result in 'F.yield()' like so:
    //
    // G.grid().run(SPREAD, F.yield(phrase.split(" "), F.println()));
    //

    X.println(">>>");
    X.println(
        ">>> Finished printing individual words on different nodes based on GridGain 3.0 API.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /**
   * Prints a phrase on one of the grid nodes running anonymous runnable.
   *
   * @param phrase Phrase to print on one of the grid nodes.
   * @throws GridException If failed.
   */
  private static void unicastWordsRunnable(final String phrase) throws GridException {
    X.println(">>> Starting unicastWordsRunnable() example...");

    G.grid()
        .run(
            UNICAST,
            new GridRunnable() {
              @Override
              public void run() {
                X.println(">>> Printing phrase: " + phrase);
              }
            });

    // NOTE:
    //
    // Alternatively, you can use existing closure 'F.println()' to
    // print any text like so:
    //
    // G.grid().run(UNICAST, F.println(">>> Printing phrase: " + phrase));
    //

    X.println(">>>");
    X.println(">>> Finished execution of runnable object based on GridGain 3.0 API.");
    X.println(">>> You should see the phrase '" + phrase + "' printed out on one of the nodes.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
    /**
     * Check if flags in correct state.
     *
     * @param msg Message.
     */
    private void checkSyncFlags(GridIoMessage msg) {
      if (!commSpiEnabled) return;

      Object o = msg.message();

      if (!(o instanceof GridDistributedLockRequest)) return;

      GridKernal g = (GridKernal) G.grid(nodeId);

      GridCacheTxManager<Object, Object> tm =
          g.internalCache(REPLICATED_ASYNC_CACHE_NAME).context().tm();

      GridCacheVersion v = ((GridCacheVersionable) o).version();

      GridCacheTxEx t = tm.tx(v);

      if (t.hasWriteKey("x1")) {
        assertFalse(t.syncCommit());
      } else if (t.hasWriteKey("x2")) {
        assertTrue(t.syncCommit());
      } else if (t.hasWriteKey("x3")) {
        assertFalse(t.syncCommit());
      } else if (t.hasWriteKey("x4")) {
        assertTrue(t.syncCommit());
      }
    }
  /**
   * Broadcasts a give phrase to all nodes.
   *
   * @param phrase Phrase to broadcast.
   * @throws GridException If failed.
   */
  private static void broadcastWordsClosure(final String phrase) throws GridException {
    X.println(">>> Starting broadcastWordsClosure() example...");

    G.grid()
        .run(
            BROADCAST,
            new GridAbsClosure() {
              @Override
              public void apply() {
                X.println(">>> Printing phrase: " + phrase);
              }
            });

    // NOTE:
    //
    // Alternatively, you can use existing closure 'F.println()' to
    // print any text like so:
    //
    // G.grid().run(BROADCAST, F.println(">>> Printing phrase: " + phrase));
    //

    X.println(">>>");
    X.println(">>> Finished broadcasting a phrase to all grid nodes based on GridGain 3.0 API.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /** @throws Exception If failed. */
  public void testDisabledRest() throws Exception {
    restEnabled = false;

    final Grid g = startGrid("disabled-rest");

    try {
      Thread.sleep(2 * TOP_REFRESH_FREQ);

      // As long as we have round robin load balancer this will cause every node to be queried.
      for (int i = 0; i < NODES_CNT + 1; i++)
        assertEquals(NODES_CNT + 1, client.compute().refreshTopology(false, false).size());

      final GridClientData data = client.data(PARTITIONED_CACHE_NAME);

      // Check rest-disabled node is unavailable.
      try {
        String affKey;

        do {
          affKey = UUID.randomUUID().toString();
        } while (!data.affinity(affKey).equals(g.localNode().id()));

        data.put(affKey, "asdf");

        assertEquals("asdf", cache(0, PARTITIONED_CACHE_NAME).get(affKey));
      } catch (GridServerUnreachableException e) {
        // Thrown for direct client-node connections.
        assertTrue(
            "Unexpected exception message: " + e.getMessage(),
            e.getMessage()
                .startsWith("No available endpoints to connect (is rest enabled for this node?)"));
      } catch (GridClientException e) {
        // Thrown for routed client-router-node connections.
        String msg = e.getMessage();

        assertTrue(
            "Unexpected exception message: " + msg,
            protocol() == GridClientProtocol.TCP
                ? msg.contains("No available endpoints to connect (is rest enabled for this node?)")
                : // TCP router.
                msg.startsWith(
                    "No available nodes on the router for destination node ID")); // HTTP router.
      }

      // Check rest-enabled nodes are available.
      String affKey;

      do {
        affKey = UUID.randomUUID().toString();
      } while (data.affinity(affKey).equals(g.localNode().id()));

      data.put(affKey, "fdsa");

      assertEquals("fdsa", cache(0, PARTITIONED_CACHE_NAME).get(affKey));
    } finally {
      restEnabled = true;

      G.stop(g.name(), true);
    }
  }
  /** @throws Exception If failed. */
  public void testInvalidateFlag() throws Exception {
    GridEx g0 = grid(0);

    GridCache<String, String> cache = g0.cache(PARTITIONED_CACHE_NAME);

    String key = null;

    for (int i = 0; i < 10_000; i++) {
      if (!cache.affinity().isPrimaryOrBackup(g0.localNode(), String.valueOf(i))) {
        key = String.valueOf(i);

        break;
      }
    }

    assertNotNull(key);

    cache.put(key, key); // Create entry in near cache, it is invalidated if INVALIDATE flag is set.

    assertNotNull(cache.peek(key));

    GridClientData d = client.data(PARTITIONED_CACHE_NAME);

    d.flagsOn(GridClientCacheFlag.INVALIDATE).put(key, "zzz");

    for (Grid g : G.allGrids()) {
      cache = g.cache(PARTITIONED_CACHE_NAME);

      if (cache.affinity().isPrimaryOrBackup(g.localNode(), key))
        assertEquals("zzz", cache.peek(key));
      else assertNull(cache.peek(key));
    }
  }
  /**
   * Start grid with IGFS.
   *
   * @param gridName Grid name.
   * @param igfsName IGFS name
   * @param mode IGFS mode.
   * @param secondaryFs Secondary file system (optional).
   * @param restCfg Rest configuration string (optional).
   * @return Started grid instance.
   * @throws Exception If failed.
   */
  protected Ignite startGridWithIgfs(
      String gridName,
      String igfsName,
      IgfsMode mode,
      @Nullable IgfsSecondaryFileSystem secondaryFs,
      @Nullable IgfsIpcEndpointConfiguration restCfg)
      throws Exception {
    FileSystemConfiguration igfsCfg = new FileSystemConfiguration();

    igfsCfg.setDataCacheName("dataCache");
    igfsCfg.setMetaCacheName("metaCache");
    igfsCfg.setName(igfsName);
    igfsCfg.setBlockSize(IGFS_BLOCK_SIZE);
    igfsCfg.setDefaultMode(mode);
    igfsCfg.setIpcEndpointConfiguration(restCfg);
    igfsCfg.setSecondaryFileSystem(secondaryFs);
    igfsCfg.setPrefetchBlocks(PREFETCH_BLOCKS);
    igfsCfg.setSequentialReadsBeforePrefetch(SEQ_READS_BEFORE_PREFETCH);

    CacheConfiguration dataCacheCfg = defaultCacheConfiguration();

    dataCacheCfg.setName("dataCache");
    dataCacheCfg.setCacheMode(PARTITIONED);
    dataCacheCfg.setNearConfiguration(null);
    dataCacheCfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
    dataCacheCfg.setAffinityMapper(new IgfsGroupDataBlocksKeyMapper(2));
    dataCacheCfg.setBackups(0);
    dataCacheCfg.setAtomicityMode(TRANSACTIONAL);
    dataCacheCfg.setOffHeapMaxMemory(0);

    CacheConfiguration metaCacheCfg = defaultCacheConfiguration();

    metaCacheCfg.setName("metaCache");
    metaCacheCfg.setCacheMode(REPLICATED);
    metaCacheCfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
    metaCacheCfg.setAtomicityMode(TRANSACTIONAL);

    IgniteConfiguration cfg = new IgniteConfiguration();

    cfg.setGridName(gridName);

    TcpDiscoverySpi discoSpi = new TcpDiscoverySpi();

    discoSpi.setIpFinder(new TcpDiscoveryVmIpFinder(true));

    cfg.setDiscoverySpi(discoSpi);
    cfg.setCacheConfiguration(dataCacheCfg, metaCacheCfg);
    cfg.setFileSystemConfiguration(igfsCfg);

    cfg.setLocalHost("127.0.0.1");
    cfg.setConnectorConfiguration(null);

    return G.start(cfg);
  }
  /**
   * Calculates length of a given phrase on the grid.
   *
   * @param phrase Phrase to count the number of letters in.
   * @throws GridException If failed.
   */
  private static void countLettersReducer(String phrase) throws GridException {
    X.println(">>> Starting countLettersReducer() example...");

    Grid grid = G.grid();

    // Logger to use in your closure. Note that even though we assign it
    // to a local variable, GridGain still allows to use it from remotely
    // executed code.
    final GridLogger log = grid.log();

    // Execute Hello World task.
    int letterCnt =
        grid.reduce(
            BALANCE,
            new GridClosure<String, Integer>() { // Create executable logic.
              @Override
              public Integer apply(String word) {
                // Print out a given word, just so we can
                // see which node is doing what.
                log.info(">>> Calculating for word: " + word);

                // Return the length of a given word, i.e. number of letters.
                return word.length();
              }
            },
            Arrays.asList(phrase.split(" ")), // Collection of words.
            // Create custom reducer.
            // NOTE: Alternatively, you can use existing reducer: F.sumIntReducer()
            new GridReducer<Integer, Integer>() {
              private int sum;

              @Override
              public boolean collect(Integer res) {
                sum += res;

                return true; // True means continue collecting until last result.
              }

              @Override
              public Integer apply() {
                return sum;
              }
            });

    X.println(">>>");
    X.println(">>> Finished execution of counting letters with reducer based on GridGain 3.0 API.");
    X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'.");
    X.println(">>> You should see individual words printed out on different nodes.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
Example #13
0
  /**
   * Starts new grid node.
   *
   * @param gridName name of new node.
   * @param springCfg file with spring configuration to use for this node.
   * @return a grid instance local to new node {@link GridGain#start(GridConfiguration)}.
   * @throws Exception if node run failed.
   */
  protected Grid startNode(String gridName, File springCfg) throws Exception {
    assert springCfg != null;

    ListableBeanFactory springCtx =
        new FileSystemXmlApplicationContext("file:///" + springCfg.getAbsolutePath());

    Map cfgMap = springCtx.getBeansOfType(GridConfiguration.class);

    assert cfgMap != null;
    assert !cfgMap.isEmpty();

    GridConfiguration cfg = (GridConfiguration) cfgMap.values().iterator().next();

    cfg.setGridName(gridName + "-" + getNextNodeNum());

    return G.start(cfg);
  }
 /** {@inheritDoc} */
 @Override
 protected void afterTestsStopped() throws Exception {
   G.stopAll(true);
 }
  /**
   * See <a href="http://e-docs.bea.com/wls/docs100/javadocs/weblogic/common/T3StartupDef.html">
   * http://e-docs.bea.com/wls/docs100/javadocs/weblogic/common/T3StartupDef.html</a> for more
   * information.
   *
   * @param str Virtual name by which the class is registered as a {@code startupClass} in the
   *     {@code config.xml} file
   * @param params A hashtable that is made up of the name-value pairs supplied from the {@code
   *     startupArgs} property
   * @return Result string (log message).
   * @throws Exception Thrown if error occurred.
   */
  @SuppressWarnings({"unchecked", "CatchGenericClass"})
  @Override
  public String startup(String str, Hashtable params) throws Exception {
    GridLogger log = new GridJavaLogger(LoggingHelper.getServerLogger());

    cfgFile = (String) params.get(cfgFilePathParam);

    if (cfgFile == null) {
      throw new IllegalArgumentException("Failed to read property: " + cfgFilePathParam);
    }

    String workMgrName = (String) params.get(workMgrParam);

    URL cfgUrl = U.resolveGridGainUrl(cfgFile);

    if (cfgUrl == null)
      throw new ServerLifecycleException(
          "Failed to find Spring configuration file (path provided should be "
              + "either absolute, relative to GRIDGAIN_HOME, or relative to META-INF folder): "
              + cfgFile);

    GenericApplicationContext springCtx;

    try {
      springCtx = new GenericApplicationContext();

      XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(springCtx);

      xmlReader.loadBeanDefinitions(new UrlResource(cfgUrl));

      springCtx.refresh();
    } catch (BeansException e) {
      throw new ServerLifecycleException(
          "Failed to instantiate Spring XML application context: " + e.getMessage(), e);
    }

    Map cfgMap;

    try {
      // Note: Spring is not generics-friendly.
      cfgMap = springCtx.getBeansOfType(GridConfiguration.class);
    } catch (BeansException e) {
      throw new ServerLifecycleException(
          "Failed to instantiate bean [type="
              + GridConfiguration.class
              + ", err="
              + e.getMessage()
              + ']',
          e);
    }

    if (cfgMap == null)
      throw new ServerLifecycleException(
          "Failed to find a single grid factory configuration in: " + cfgUrl);

    if (cfgMap.isEmpty())
      throw new ServerLifecycleException("Can't find grid factory configuration in: " + cfgUrl);

    try {
      ExecutorService execSvc = null;

      MBeanServer mbeanSrv = null;

      for (GridConfiguration cfg : (Collection<GridConfiguration>) cfgMap.values()) {
        assert cfg != null;

        GridConfigurationAdapter adapter = new GridConfigurationAdapter(cfg);

        // Set logger.
        if (cfg.getGridLogger() == null) adapter.setGridLogger(log);

        if (cfg.getExecutorService() == null) {
          if (execSvc == null)
            execSvc =
                workMgrName != null
                    ? new GridThreadWorkManagerExecutor(workMgrName)
                    : new GridThreadWorkManagerExecutor(J2EEWorkManager.getDefault());

          adapter.setExecutorService(execSvc);
        }

        if (cfg.getMBeanServer() == null) {
          if (mbeanSrv == null) {
            InitialContext ctx = null;

            try {
              ctx = new InitialContext();

              mbeanSrv = (MBeanServer) ctx.lookup("java:comp/jmx/runtime");
            } catch (Exception e) {
              throw new IllegalArgumentException(
                  "MBean server was not provided and failed to obtain " + "Weblogic MBean server.",
                  e);
            } finally {
              if (ctx != null) ctx.close();
            }
          }

          adapter.setMBeanServer(mbeanSrv);
        }

        Grid grid = G.start(adapter, springCtx);

        // Test if grid is not null - started properly.
        if (grid != null) gridNames.add(grid.name());
      }

      return getClass().getSimpleName() + " started successfully.";
    } catch (GridException e) {
      // Stop started grids only.
      for (String name : gridNames) G.stop(name, true);

      throw new ServerLifecycleException("Failed to start GridGain.", e);
    }
  }