org.apache.ignite.Ignite.atomicSequence()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(317)

本文整理了Java中org.apache.ignite.Ignite.atomicSequence()方法的一些代码示例,展示了Ignite.atomicSequence()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Ignite.atomicSequence()方法的具体详情如下:
包路径:org.apache.ignite.Ignite
类名称:Ignite
方法名:atomicSequence

Ignite.atomicSequence介绍

[英]Will get an atomic sequence from cache and create one if it has not been created yet and create flag is true. It will use configuration from IgniteConfiguration#getAtomicConfiguration().
[中]将从缓存中获取一个原子序列,如果尚未创建,则创建一个,并且create标志为true。它将使用IgniteConfiguration#getAtomicConfiguration()中的配置。

代码示例

代码示例来源:origin: apache/ignite

@Override public Void call() throws Exception {
    IgniteAtomicSequence seq = ig.atomicSequence("seq", 0, true);
    return null;
  }
}, IgniteException.class, "Can not perform the operation because the cluster is inactive.");

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
@Nullable @Override public IgniteAtomicSequence atomicSequence(String name, long initVal, boolean create) {
  checkIgnite();
  return g.atomicSequence(name, initVal, create);
}

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
@Override public IgniteAtomicSequence atomicSequence(String name, AtomicConfiguration cfg, long initVal,
  boolean create) throws IgniteException {
  checkIgnite();
  return g.atomicSequence(name, cfg, initVal, create);
}

代码示例来源:origin: apache/ignite

@Override public Object apply(Ignite ignite) {
    assertTrue(ignite.atomicSequence(STRUCTURE_NAME, 1, false).get() > 0);
    return null;
  }
});

代码示例来源:origin: apache/ignite

/**
 * @return ID.
 */
private long getId() {
  IgniteAtomicSequence seq = ignite.atomicSequence("ID", 0, true);
  return seq.incrementAndGet();
}

代码示例来源:origin: apache/ignite

/**
 * @return New ID.
 */
private long getId() {
  IgniteAtomicSequence seq = ignite.atomicSequence("ID", 0, true);
  return seq.incrementAndGet();
}

代码示例来源:origin: apache/ignite

@Override public void applyx(Ignite ignite) {
    IgniteAtomicSequence as = ignite.atomicSequence(TEST_SEQ_NAME, 0, true);
    for (int i = 0; i < operationsPerTx; i++) {
      as.addAndGet(RAND.nextInt(MAX_INT) + 1);
      long cnt = writes.incrementAndGet();
      if (cnt % WRITE_LOG_MOD == 0)
        info("Performed " + cnt + " writes.");
    }
  }
};

代码示例来源:origin: apache/ignite

@Override public void applyx(Ignite ignite) {
    IgniteAtomicSequence as = ignite.atomicSequence(TEST_SEQ_NAME, 0, true);
    for (int i = 0; i < operationsPerTx; i++) {
      as.get();
      long cnt = reads.incrementAndGet();
      if (cnt % READ_LOG_MOD == 0)
        info("Performed " + cnt + " reads.");
    }
  }
};

代码示例来源:origin: apache/ignite

@Override public Object call() {
    try {
      IgniteAtomicSequence seq = g.atomicSequence(STRUCTURE_NAME, 1, true);
      assert seq != null;
      for (int i = 0; i < 1000; i++)
        seq.getAndIncrement();
      return null;
    }
    catch (IgniteException e) {
      // Fail if we are on stable nodes or exception is not node stop.
      if (limitProjection || !X.hasCause(e, NodeStoppingException.class))
        throw e;
      return null;
    }
  }
});

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
  @Override public Boolean call() throws Exception {
    Ignite ignite = grid();
    IgniteCache cache = ignite.cache(DEFAULT_CACHE_NAME);
    assert cache != null;
    IgniteAtomicSequence seq = ignite.atomicSequence("SEQUENCE", 0, true);
    long start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < DURATION && !Thread.currentThread().isInterrupted()) {
      Transaction tx = ignite.transactions().txStart();
      long seqVal = seq.incrementAndGet();
      int curIdx = idx.incrementAndGet();
      if (curIdx % 1000 == 0)
        info("Sequence value [seq=" + seqVal + ", idx=" + curIdx + ']');
      tx.commit();
    }
    return true;
  }
}

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
  @Override public void run() {
    IgniteAtomicSequence seq = ignite.atomicSequence(SEQ_NAME, 0, false);
    latch.countDown();
    U.awaitQuiet(latch);
    for (int i = 0; i < ITERATIONS; i++)
      try (Transaction ignored = ignite.transactions().txStart()) {
        seq.incrementAndGet();
      }
  }
}

代码示例来源:origin: apache/ignite

/**
 * Tests sequence calls inside transactions.
 *
 * @throws Exception If failed.
 */
@Test
public void testTransactionIncrement() throws Exception {
  ignite(0).atomicSequence(SEQ_NAME, 0, true);
  for (int i = 0; i < THREAD_NUM; i++) {
    multithreaded(new Runnable() {
      @Override public void run() {
        ignite(0).compute().run(new IncrementClosure());
      }
    }, THREAD_NUM);
  }
}

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
  @Override public Set<Long> call() throws IgniteCheckedException {
    assert ignite != null;
    if (log.isInfoEnabled())
      log.info("Running IncrementAndGetJob 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.incrementAndGet();
      assert !resSet.contains(val) : "Element already in set : " + val;
      resSet.add(val);
    }
    return resSet;
  }
}

代码示例来源:origin: apache/ignite

/** {@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;
}

代码示例来源:origin: apache/ignite

/**
 *
 */
private void checkAtomics() {
  Ignite node0 = grid(0);
  node0.atomicLong("l1", 0, true).incrementAndGet();
  node0.atomicSequence("s1", 10, true);
  for (int i = 0; i < 3; i++) {
    assertEquals(1, ignite(i).atomicLong("l1", 0, false).get());
    assertNotNull(ignite(i).atomicSequence("s1", 0, false));
    ignite(i).atomicSequence("s1", 0, false).getAndIncrement();
  }
}

代码示例来源:origin: apache/ignite

/**
 *
 */
@Test
public void testCacheSequence() throws Exception {
  Ignite ignite = startGrid();
  ignite.cluster().active(true);
  Ignite client = startGrid("client");
  final IgniteAtomicSequence seq1 = client.atomicSequence("testSeq", 1, true);
  seq1.get();
  final IgniteAtomicSequence seq2 = client.atomicSequence("testSeq2", 10, true);
  seq2.get();
  injectTestSystemOut();
  assertEquals(EXIT_CODE_OK, execute("--cache", "list", "testSeq.*", "--seq"));
  assertTrue(testOut.toString().contains("testSeq"));
  assertTrue(testOut.toString().contains("testSeq2"));
}

代码示例来源:origin: apache/ignite

/**
 * @throws Exception If failed.
 */
@Test
public void testAtomicSequenceTopologyChange() throws Exception {
  try (IgniteAtomicSequence s = grid(0).atomicSequence(STRUCTURE_NAME, 10, true)) {
    Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);
    assertEquals(1010, g.atomicSequence(STRUCTURE_NAME, 10, false).get());
    assertEquals(1020, g.atomicSequence(STRUCTURE_NAME, 10, false).addAndGet(10));
    stopGrid(NEW_IGNITE_INSTANCE_NAME);
  }
}

代码示例来源:origin: apache/ignite

/**
 * @throws Exception If failed.
 */
@Test
public void testSequence() throws Exception {
  Ignite ignite = startGrids(4);
  ignite.active(true);
  IgniteAtomicSequence sequence = ignite.atomicSequence("testSequence", 0, true);
  int i = 0;
  while (i < 1000) {
    sequence.incrementAndGet();
    i++;
  }
  stopAllGrids();
  ignite = startGrids(4);
  ignite.active(true);
  sequence = ignite.atomicSequence("testSequence", 0, false);
  assertTrue(sequence.incrementAndGet() > i);
}

代码示例来源:origin: apache/ignite

/**
 * Tests that reserveSize value from explicit configuration takes preference.
 *
 * @throws Exception If failed.
 */
@Test
public void testSequenceReserveSizeFromExplicitConfiguration() throws Exception {
  Ignite ignite = grid(0);
  IgniteAtomicSequence seq = ignite.atomicSequence("seq",
    new AtomicConfiguration().setAtomicSequenceReserveSize(BATCH_SIZE + 1), 0, true);
  assertEquals(BATCH_SIZE + 1, seq.batchSize());
}

代码示例来源:origin: apache/ignite

/**
   *
   * @param g Grid.
   * @param cacheName Cache name.
   */
  private static void sample(Ignite g, String cacheName) {
    IgniteAtomicLong atomicLong = g.atomicLong("keygen", 0, true);

    IgniteAtomicSequence seq = g.atomicSequence("keygen", 0, true);

    seq.incrementAndGet();
    seq.incrementAndGet();

    seq.incrementAndGet();
    seq.incrementAndGet();

    atomicLong.incrementAndGet();
    atomicLong.incrementAndGet();
    atomicLong.incrementAndGet();

    X.println(cacheName+": Seq: " + seq.get() + " atomicLong " + atomicLong.get());
  }
}

相关文章