/**
   * Checks that gets work for implicit txs.
   *
   * @param cache Cache to test.
   * @throws Exception If failed.
   */
  private void checkExplicitTx(Ignite ignite, IgniteCache<String, String> cache) throws Exception {
    IgniteCache<String, String> asyncCache = cache.withAsync();

    Transaction tx = ignite.transactions().txStart();

    try {
      assertNull(cache.get("key1"));

      tx.commit();
    } finally {
      tx.close();
    }

    tx = ignite.transactions().txStart();

    try {
      asyncCache.get("key2");

      assertNull(asyncCache.future().get());

      tx.commit();
    } finally {
      tx.close();
    }

    tx = ignite.transactions().txStart();

    try {
      assertTrue(cache.getAll(F.asSet("key3", "key4")).isEmpty());

      tx.commit();
    } finally {
      tx.close();
    }

    tx = ignite.transactions().txStart();

    try {
      asyncCache.getAll(F.asSet("key5", "key6"));

      assertTrue(((Map) asyncCache.future().get()).isEmpty());

      tx.commit();
    } finally {
      tx.close();
    }

    tx = ignite.transactions().txStart();

    try {
      cache.put("key7", "key7");

      cache.remove("key7");

      assertNull(cache.get("key7"));

      tx.commit();
    } finally {
      tx.close();
    }

    checkEmpty(cache);
  }
  /**
   * Change topology.
   *
   * @param parent Grid to execute tasks on.
   * @param add New nodes count.
   * @param rmv Remove nodes count.
   * @param type Type of nodes to manipulate.
   */
  private static void changeTopology(Ignite parent, int add, int rmv, String type) {
    Collection<ComputeTaskFuture<?>> tasks = new ArrayList<>();

    IgniteCompute comp = parent.compute().withAsync();

    // Start nodes in parallel.
    while (add-- > 0) {
      comp.execute(ClientStartNodeTask.class, type);

      tasks.add(comp.future());
    }

    for (ComputeTaskFuture<?> task : tasks) task.get();

    // Stop nodes in sequence.
    while (rmv-- > 0) parent.compute().execute(ClientStopNodeTask.class, type);

    // Wait for node stops.
    // U.sleep(1000);

    Collection<String> gridNames = new ArrayList<>();

    for (Ignite g : G.allGrids()) gridNames.add(g.name());

    parent.log().info(">>> Available grids: " + gridNames);
  }
  /**
   * Listen to events that happen only on local node.
   *
   * @throws IgniteException If failed.
   */
  private static void localListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Local event listener example.");

    Ignite ignite = Ignition.ignite();

    IgnitePredicate<TaskEvent> lsnr =
        evt -> {
          System.out.println(
              "Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']');

          return true; // Return true to continue listening.
        };

    // Register event listener for all local task execution events.
    ignite.events().localListen(lsnr, EVTS_TASK_EXECUTION);

    // Generate task events.
    ignite
        .compute()
        .withName("example-event-task")
        .run(() -> System.out.println("Executing sample job."));

    // Unsubscribe local task event listener.
    ignite.events().stopLocalListen(lsnr);
  }
    /** {@inheritDoc} */
    @Override
    public Set<Long> call() throws IgniteCheckedException {
      assert ignite != null;

      if (log.isInfoEnabled())
        log.info("Running GetAndIncrementJob on node: " + ignite.cluster().localNode().id());

      IgniteAtomicSequence seq = ignite.atomicSequence(seqName, 0, true);

      assert seq != null;

      // Result set.
      Set<Long> resSet = new HashSet<>();

      // Get sequence value and try to put it result set.
      for (int i = 0; i < retries; i++) {
        long val = seq.getAndIncrement();

        assert !resSet.contains(val) : "Element already in set : " + val;

        resSet.add(val);
      }

      return resSet;
    }
  /**
   * Executes example.
   *
   * @param args Command line arguments, none required.
   */
  public static void main(String[] args) {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {

      System.out.println();
      System.out.println(">>> Cache star schema example started.");

      CacheConfiguration<Integer, FactPurchase> factCacheCfg =
          new CacheConfiguration<>(PARTITIONED_CACHE_NAME);

      factCacheCfg.setCacheMode(CacheMode.PARTITIONED);
      factCacheCfg.setIndexedTypes(Integer.class, FactPurchase.class);

      CacheConfiguration<Integer, Object> dimCacheCfg =
          new CacheConfiguration<>(REPLICATED_CACHE_NAME);

      dimCacheCfg.setCacheMode(CacheMode.REPLICATED);
      dimCacheCfg.setIndexedTypes(
          Integer.class, DimStore.class,
          Integer.class, DimProduct.class);

      try (IgniteCache<Integer, FactPurchase> factCache = ignite.getOrCreateCache(factCacheCfg);
          IgniteCache<Integer, Object> dimCache = ignite.getOrCreateCache(dimCacheCfg)) {
        populateDimensions(dimCache);
        populateFacts(factCache);

        queryStorePurchases();
        queryProductPurchases();
      }
    }
  }
  /** @throws Exception If failed. */
  public void testInternalTaskMetrics() throws Exception {
    Ignite ignite = grid();

    // Visor task is internal and should not affect metrics.
    ignite.compute().withName("visor-test-task").execute(new TestInternalTask(), "testArg");

    // Let metrics update twice.
    final CountDownLatch latch = new CountDownLatch(2);

    ignite
        .events()
        .localListen(
            new IgnitePredicate<Event>() {
              @Override
              public boolean apply(Event evt) {
                assert evt.type() == EVT_NODE_METRICS_UPDATED;

                latch.countDown();

                return true;
              }
            },
            EVT_NODE_METRICS_UPDATED);

    // Wait for metrics update.
    latch.await();

    ClusterMetrics metrics = ignite.cluster().localNode().metrics();

    info("Node metrics: " + metrics);

    assert metrics.getAverageActiveJobs() == 0;
    assert metrics.getAverageCancelledJobs() == 0;
    assert metrics.getAverageJobExecuteTime() == 0;
    assert metrics.getAverageJobWaitTime() == 0;
    assert metrics.getAverageRejectedJobs() == 0;
    assert metrics.getAverageWaitingJobs() == 0;
    assert metrics.getCurrentActiveJobs() == 0;
    assert metrics.getCurrentCancelledJobs() == 0;
    assert metrics.getCurrentJobExecuteTime() == 0;
    assert metrics.getCurrentJobWaitTime() == 0;
    assert metrics.getCurrentWaitingJobs() == 0;
    assert metrics.getMaximumActiveJobs() == 0;
    assert metrics.getMaximumCancelledJobs() == 0;
    assert metrics.getMaximumJobExecuteTime() == 0;
    assert metrics.getMaximumJobWaitTime() == 0;
    assert metrics.getMaximumRejectedJobs() == 0;
    assert metrics.getMaximumWaitingJobs() == 0;
    assert metrics.getTotalCancelledJobs() == 0;
    assert metrics.getTotalExecutedJobs() == 0;
    assert metrics.getTotalRejectedJobs() == 0;
    assert metrics.getTotalExecutedTasks() == 0;

    assertTrue(
        "MaximumJobExecuteTime="
            + metrics.getMaximumJobExecuteTime()
            + " is less than AverageJobExecuteTime="
            + metrics.getAverageJobExecuteTime(),
        metrics.getMaximumJobExecuteTime() >= metrics.getAverageJobExecuteTime());
  }
  /**
   * Tests preset eviction policy.
   *
   * @throws Exception If failed.
   */
  private void checkPolicy0() throws Exception {
    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
      txConcurrency = concurrency;

      for (TransactionIsolation isolation : TransactionIsolation.values()) {
        txIsolation = isolation;

        Ignite g = startGrids();

        IgniteCache<String, String> cache = g.cache(null);

        try {
          info(
              ">>> Checking policy [txConcurrency="
                  + txConcurrency
                  + ", txIsolation="
                  + txIsolation
                  + ", plc="
                  + plc
                  + ", nearPlc="
                  + nearPlc
                  + ']');

          checkExplicitTx(g, cache);

          checkImplicitTx(cache);
        } finally {
          stopAllGrids();
        }
      }
    }
  }
 /**
  * Injects resources.
  *
  * @param ignite Ignite
  */
 @IgniteInstanceResource
 private void injectResources(Ignite ignite) {
   if (ignite != null) {
     // Inject resources.
     gridName = ignite.name();
     locNodeId = ignite.configuration().getNodeId();
   } else {
     // Cleanup resources.
     gridName = null;
     locNodeId = null;
   }
 }
  /** @throws Exception If failed. */
  public void testIoMetrics() throws Exception {
    Ignite ignite0 = grid();
    Ignite ignite1 = startGrid(1);

    Object msg = new TestMessage();

    int size = ignite0.configuration().getMarshaller().marshal(msg).length;

    assert size > MSG_SIZE;

    final CountDownLatch latch = new CountDownLatch(MSG_CNT);

    ignite0
        .message()
        .localListen(
            null,
            new MessagingListenActor<TestMessage>() {
              @Override
              protected void receive(UUID nodeId, TestMessage rcvMsg) throws Throwable {
                latch.countDown();
              }
            });

    ignite1
        .message()
        .localListen(
            null,
            new MessagingListenActor<TestMessage>() {
              @Override
              protected void receive(UUID nodeId, TestMessage rcvMsg) throws Throwable {
                respond(rcvMsg);
              }
            });

    for (int i = 0; i < MSG_CNT; i++) message(ignite0.cluster().forRemotes()).send(null, msg);

    latch.await();

    ClusterMetrics metrics = ignite0.cluster().localNode().metrics();

    info("Node 0 metrics: " + metrics);

    // Time sync messages are being sent.
    assert metrics.getSentMessagesCount() >= MSG_CNT;
    assert metrics.getSentBytesCount() > size * MSG_CNT;
    assert metrics.getReceivedMessagesCount() >= MSG_CNT;
    assert metrics.getReceivedBytesCount() > size * MSG_CNT;

    metrics = ignite1.cluster().localNode().metrics();

    info("Node 1 metrics: " + metrics);

    // Time sync messages are being sent.
    assert metrics.getSentMessagesCount() >= MSG_CNT;
    assert metrics.getSentBytesCount() > size * MSG_CNT;
    assert metrics.getReceivedMessagesCount() >= MSG_CNT;
    assert metrics.getReceivedBytesCount() > size * MSG_CNT;
  }
  /** @throws Exception If failed. */
  @SuppressWarnings({"AssignmentToCatchBlockParameter"})
  public void testCancel() throws Exception {
    Ignite ignite = G.ignite(getTestGridName());

    ignite
        .compute()
        .localDeployTask(GridCancelTestTask.class, GridCancelTestTask.class.getClassLoader());

    ComputeTaskFuture<?> res0 =
        executeAsync(
            ignite.compute().withTimeout(maxJobExecTime * 2),
            GridCancelTestTask.class.getName(),
            null);

    try {
      Object res = res0.get();

      info("Cancel test result: " + res);

      synchronized (mux) {
        // Every execute must be called.
        assert execCnt <= SPLIT_COUNT : "Invalid execute count: " + execCnt;

        // Job returns 1 if was cancelled.
        assert (Integer) res <= SPLIT_COUNT : "Invalid task result: " + res;

        // Should be exactly the same as Jobs number.
        assert cancelCnt <= SPLIT_COUNT : "Invalid cancel count: " + cancelCnt;

        // One per start and one per stop and some that come with heartbeats.
        assert colResolutionCnt > SPLIT_COUNT + 1
            : "Invalid collision resolution count: " + colResolutionCnt;
      }
    } catch (ComputeTaskTimeoutException e) {
      error("Task execution got timed out.", e);
    } catch (Exception e) {
      assert e.getCause() != null;

      if (e.getCause() instanceof IgniteCheckedException) e = (Exception) e.getCause();

      if (e.getCause() instanceof IOException) e = (Exception) e.getCause();

      assert e.getCause() instanceof InterruptedException
          : "Invalid exception cause: " + e.getCause();
    }
  }
Beispiel #11
0
  /** {@inheritDoc} */
  @Override
  public void testRemoteNodes() throws Exception {
    int size = remoteNodeIds().size();

    String name = "oneMoreGrid";

    try {
      Ignite g = startGrid(name);

      UUID joinedId = g.cluster().localNode().id();

      assert projection().forRemotes().nodes().size() == size + 1;

      assert F.nodeIds(projection().forRemotes().nodes()).contains(joinedId);
    } finally {
      stopGrid(name);
    }
  }
  /** @throws Exception if error occur. */
  @SuppressWarnings("unchecked")
  private void checkGar() throws Exception {
    initGar = true;

    String garDir = "modules/extdata/p2p/deploy";
    String garFileName = "p2p.gar";

    File origGarPath = U.resolveIgnitePath(garDir + '/' + garFileName);

    File tmpPath = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

    if (!tmpPath.mkdir()) throw new IOException("Can not create temp directory");

    try {
      File newGarFile = new File(tmpPath, garFileName);

      U.copy(origGarPath, newGarFile, false);

      assert newGarFile.exists();

      try {
        garFile = "file:///" + tmpPath.getAbsolutePath();

        try {
          Ignite ignite1 = startGrid(1);
          Ignite ignite2 = startGrid(2);

          Integer res =
              ignite1
                  .compute()
                  .<UUID, Integer>execute(TASK_NAME, ignite2.cluster().localNode().id());

          assert res != null;
        } finally {
          stopGrid(1);
          stopGrid(2);
        }
      } finally {
        if (newGarFile != null && !newGarFile.delete()) error("Can not delete temp gar file");
      }
    } finally {
      if (!tmpPath.delete()) error("Can not delete temp directory");
    }
  }
  /**
   * Listen to events coming from all cluster nodes.
   *
   * @throws IgniteException If failed.
   */
  private static void remoteListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Remote event listener example.");

    // This optional local callback is called for each event notification
    // that passed remote predicate listener.
    IgniteBiPredicate<UUID, TaskEvent> locLsnr =
        (nodeId, evt) -> {
          // Remote filter only accepts tasks whose name being with "good-task" prefix.
          assert evt.taskName().startsWith("good-task");

          System.out.println(
              "Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName());

          return true; // Return true to continue listening.
        };

    // Remote filter which only accepts tasks whose name begins with "good-task" prefix.
    IgnitePredicate<TaskEvent> rmtLsnr = evt -> evt.taskName().startsWith("good-task");

    Ignite ignite = Ignition.ignite();

    // Register event listeners on all nodes to listen for task events.
    ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION);

    // Generate task events.
    for (int i = 0; i < 10; i++) {
      ignite
          .compute()
          .withName(i < 5 ? "good-task-" + i : "bad-task-" + i)
          .run(
              new IgniteRunnable() {
                // Auto-inject task session.
                @TaskSessionResource private ComputeTaskSession ses;

                @Override
                public void run() {
                  System.out.println("Executing sample job for task: " + ses.getTaskName());
                }
              });
    }
  }
  /**
   * Checks for explicit events configuration.
   *
   * @param ignite Grid instance.
   * @return {@code true} if all task events explicitly specified in configuration.
   */
  public static boolean checkExplicitTaskMonitoring(Ignite ignite) {
    int[] evts = ignite.configuration().getIncludeEventTypes();

    if (F.isEmpty(evts)) return false;

    for (int evt : VISOR_TASK_EVTS) {
      if (!F.contains(evts, evt)) return false;
    }

    return true;
  }
  /** @throws Exception If failed. */
  public void testClusterNodeMetrics() throws Exception {
    final Ignite ignite0 = grid();
    final Ignite ignite1 = startGrid(1);

    GridTestUtils.waitForCondition(
        new GridAbsPredicate() {
          @Override
          public boolean apply() {
            return ignite0.cluster().nodes().size() == 2 && ignite1.cluster().nodes().size() == 2;
          }
        },
        3000L);

    ClusterMetrics metrics0 = ignite0.cluster().localNode().metrics();

    ClusterMetrics nodesMetrics =
        ignite0
            .cluster()
            .forNode(ignite0.cluster().localNode(), ignite1.cluster().localNode())
            .metrics();

    assertEquals(metrics0.getTotalCpus(), nodesMetrics.getTotalCpus());
    assertEquals(1, metrics0.getTotalNodes());
    assertEquals(2, nodesMetrics.getTotalNodes());

    assert metrics0.getHeapMemoryUsed() > 0;
    assert metrics0.getHeapMemoryTotal() > 0;
    assert metrics0.getNonHeapMemoryMaximum() > 0;
  }
  /**
   * Example for start/stop node tasks.
   *
   * @param args Not used.
   */
  public static void main(String[] args) {
    String nodeType = "tcp+ssl";

    // Start initial node = 1
    try (Ignite g = G.start(NODE_CFG.get(nodeType))) {
      // Change topology.
      changeTopology(g, 4, 1, nodeType);
      changeTopology(g, 1, 4, nodeType);

      // Stop node by id = 0
      g.compute().execute(ClientStopNodeTask.class, g.cluster().localNode().id().toString());

      // Wait for node stops.
      // U.sleep(1000);

      assert G.allGrids().isEmpty();
    } catch (Exception e) {
      System.err.println("Uncaught exception: " + e.getMessage());

      e.printStackTrace(System.err);
    }
  }
  /**
   * Test what happens if peer class loading is disabled.
   *
   * @throws Exception if error occur.
   */
  @SuppressWarnings("unchecked")
  private void checkClassNotFound() throws Exception {
    initGar = false;

    try {
      Ignite ignite1 = startGrid(1);
      Ignite ignite2 = startGrid(2);

      Class task = extLdr.loadClass(TASK_NAME);

      try {
        ignite1.compute().execute(task, ignite2.cluster().localNode().id());

        assert false;
      } catch (IgniteException e) {
        info("Received expected exception: " + e);
      }
    } finally {
      stopGrid(1);
      stopGrid(2);
    }
  }
  /** {@inheritDoc} */
  @Override
  protected Object executeJob(int gridSize, String type) {
    log.info(">>> Starting new grid node [currGridSize=" + gridSize + ", arg=" + type + "]");

    if (type == null) throw new IllegalArgumentException("Node type to start should be specified.");

    IgniteConfiguration cfg = getConfig(type);

    // Generate unique for this VM grid name.
    String gridName = cfg.getGridName() + " (" + UUID.randomUUID() + ")";

    // Update grid name (required to be unique).
    cfg.setGridName(gridName);

    // Start new node in current VM.
    Ignite g = G.start(cfg);

    log.info(
        ">>> Grid started [nodeId=" + g.cluster().localNode().id() + ", name='" + g.name() + "']");

    return true;
  }
Beispiel #19
0
  /** @throws Exception If failed. */
  @SuppressWarnings({"TooBroadScope"})
  public void testAsyncListen() throws Exception {
    final String hello = "HELLO!";

    final String bye = "BYE!";

    final Ignite g = grid(0);

    final UUID locNodeId = g.cluster().localNode().id();

    g.message()
        .remoteListen(
            null,
            new MessagingListenActor<String>() {
              @Override
              protected void receive(UUID nodeId, String rcvMsg) throws Throwable {
                if (hello.equals(rcvMsg)) {
                  assertEquals(locNodeId, nodeId);
                  assertEquals(hello, rcvMsg);

                  stop(bye);
                }
              }
            });

    final AtomicInteger cnt = new AtomicInteger();

    g.message()
        .localListen(
            null,
            new P2<UUID, String>() {
              @Override
              public boolean apply(UUID nodeId, String msg) {
                if (msg.equals(bye)) cnt.incrementAndGet();

                return true;
              }
            });

    g.message().send(null, hello);

    GridTestUtils.waitForCondition(
        new GridAbsPredicate() {
          @Override
          public boolean apply() {
            return cnt.get() == g.cluster().nodes().size();
          }
        },
        5000);

    assertEquals(cnt.get(), g.cluster().nodes().size());
  }
  /**
   * Grabs local events and detects if events was lost since last poll.
   *
   * @param ignite Target grid.
   * @param evtOrderKey Unique key to take last order key from node local map.
   * @param evtThrottleCntrKey Unique key to take throttle count from node local map.
   * @param evtTypes Event types to collect.
   * @param evtMapper Closure to map grid events to Visor data transfer objects.
   * @return Collections of node events
   */
  public static Collection<VisorGridEvent> collectEvents(
      Ignite ignite,
      String evtOrderKey,
      String evtThrottleCntrKey,
      final int[] evtTypes,
      IgniteClosure<Event, VisorGridEvent> evtMapper) {
    assert ignite != null;
    assert evtTypes != null && evtTypes.length > 0;

    ConcurrentMap<String, Long> nl = ignite.cluster().nodeLocalMap();

    final long lastOrder = getOrElse(nl, evtOrderKey, -1L);
    final long throttle = getOrElse(nl, evtThrottleCntrKey, 0L);

    // When we first time arrive onto a node to get its local events,
    // we'll grab only last those events that not older than given period to make sure we are
    // not grabbing GBs of data accidentally.
    final long notOlderThan = System.currentTimeMillis() - EVENTS_COLLECT_TIME_WINDOW;

    // Flag for detecting gaps between events.
    final AtomicBoolean lastFound = new AtomicBoolean(lastOrder < 0);

    IgnitePredicate<Event> p =
        new IgnitePredicate<Event>() {
          /** */
          private static final long serialVersionUID = 0L;

          @Override
          public boolean apply(Event e) {
            // Detects that events were lost.
            if (!lastFound.get() && (lastOrder == e.localOrder())) lastFound.set(true);

            // Retains events by lastOrder, period and type.
            return e.localOrder() > lastOrder
                && e.timestamp() > notOlderThan
                && F.contains(evtTypes, e.type());
          }
        };

    Collection<Event> evts = ignite.events().localQuery(p);

    // Update latest order in node local, if not empty.
    if (!evts.isEmpty()) {
      Event maxEvt = Collections.max(evts, EVTS_ORDER_COMPARATOR);

      nl.put(evtOrderKey, maxEvt.localOrder());
    }

    // Update throttle counter.
    if (!lastFound.get())
      nl.put(evtThrottleCntrKey, throttle == 0 ? EVENTS_LOST_THROTTLE : throttle - 1);

    boolean lost = !lastFound.get() && throttle == 0;

    Collection<VisorGridEvent> res = new ArrayList<>(evts.size() + (lost ? 1 : 0));

    if (lost) res.add(new VisorGridEventsLost(ignite.cluster().localNode().id()));

    for (Event e : evts) {
      VisorGridEvent visorEvt = evtMapper.apply(e);

      if (visorEvt != null) res.add(visorEvt);
    }

    return res;
  }