@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.");
/** {@inheritDoc} */ @Nullable @Override public IgniteAtomicSequence atomicSequence(String name, long initVal, boolean create) { checkIgnite(); return g.atomicSequence(name, initVal, create); }
/** {@inheritDoc} */ @Override public IgniteAtomicSequence atomicSequence(String name, AtomicConfiguration cfg, long initVal, boolean create) throws IgniteException { checkIgnite(); return g.atomicSequence(name, cfg, initVal, create); }
@Override public Object apply(Ignite ignite) { assertTrue(ignite.atomicSequence(STRUCTURE_NAME, 1, false).get() > 0); return null; } });
/** * @return ID. */ private long getId() { IgniteAtomicSequence seq = ignite.atomicSequence("ID", 0, true); return seq.incrementAndGet(); }
/** * @return New ID. */ private long getId() { IgniteAtomicSequence seq = ignite.atomicSequence("ID", 0, true); return seq.incrementAndGet(); }
@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."); } } };
@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."); } } };
@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; } } });
/** {@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; } }
/** {@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(); } } }
/** * 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); } }
/** {@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; } }
/** {@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; }
/** * */ 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(); } }
/** * */ @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")); }
/** * @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); } }
/** * @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); }
/** * 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()); }
/** * * @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()); } }