@Override public Object call() throws Exception { Long init = req.initial(); Long delta = req.delta(); boolean decr = req.command() == ATOMIC_DECREMENT; String key = (String)req.key(); IgniteAtomicLong l = ctx.grid().atomicLong(key, init != null ? init : 0, true); return l.addAndGet(decr ? -delta : delta); } }, false);
/** * @throws Exception If failed. */ @Test public void testCanCloseAtomicLongInInterruptedThread() throws Exception { doCloseByInterruptedThread(grid(0).atomicLong(STRUCTURE_NAME, 10, true)); }
/** {@inheritDoc} */ @Override public ByteBuffer makeResponse(final GridRestResponse restRes, List<String> params) { if (restRes.getResponse() == null) { // check if an atomic long with the key exists (related to incr/decr). IgniteAtomicLong l = ctx.grid().atomicLong(params.get(0), 0, false); long val; try { val = l.get(); } catch (Exception ignored) { return GridRedisProtocolParser.nil(); } return GridRedisProtocolParser.toBulkString(val); } if (restRes.getResponse() instanceof String) return GridRedisProtocolParser.toBulkString(restRes.getResponse()); else return GridRedisProtocolParser.toTypeError("Operation against a key holding the wrong kind of value"); } }
/** * @throws Exception If failed. */ @Test public void testIncrement() throws Exception { Assert.assertEquals(5, client.incr("incrKey", 3, 2)); assertEquals(5, grid(0).atomicLong("incrKey", 0, true).get()); Assert.assertEquals(15, client.incr("incrKey", 10, 0)); assertEquals(15, grid(0).atomicLong("incrKey", 0, true).get()); }
/** * @throws Exception If failed. */ @Test public void testDecrement() throws Exception { Assert.assertEquals(5, client.decr("decrKey", 10, 15)); assertEquals(5, grid(0).atomicLong("decrKey", 0, true).get()); Assert.assertEquals(2, client.decr("decrKey", 3, 0)); assertEquals(2, grid(0).atomicLong("decrKey", 0, true).get()); }
/** * @throws Exception If failed. */ @Test public void testUncommitedTxLeave() throws Exception { final int val = 10; grid(0).atomicLong(STRUCTURE_NAME, val, true); GridTestUtils.runAsync(new Callable<Object>() { @Override public Object call() throws Exception { Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME); try { g.transactions().txStart(); g.cache(TRANSACTIONAL_CACHE_NAME).put(1, 1); assertEquals(val + 1, g.atomicLong(STRUCTURE_NAME, val, false).incrementAndGet()); } finally { stopGrid(NEW_IGNITE_INSTANCE_NAME); } return null; } }).get(); waitForDiscovery(G.allGrids().toArray(new Ignite[gridCount()])); assertEquals(val + 1, grid(0).atomicLong(STRUCTURE_NAME, val, false).get()); }
/** * @throws Exception If failed. */ @Test public void testDecrement() throws Exception { String ret = content(DEFAULT_CACHE_NAME, GridRestCommand.ATOMIC_DECREMENT, "key", "decrKey", "init", "15", "delta", "10" ); JsonNode res = validateJsonResponse(ret); assertEquals(5, res.asInt()); assertEquals(5, grid(0).atomicLong("decrKey", 0, true).get()); ret = content(DEFAULT_CACHE_NAME, GridRestCommand.ATOMIC_DECREMENT, "key", "decrKey", "delta", "3" ); res = validateJsonResponse(ret); assertEquals(2, res.asInt()); assertEquals(2, grid(0).atomicLong("decrKey", 0, true).get()); }
/** * @throws Exception If failed. */ @Test public void testIncrement() throws Exception { String ret = content(DEFAULT_CACHE_NAME, GridRestCommand.ATOMIC_INCREMENT, "key", "incrKey", "init", "2", "delta", "3" ); JsonNode res = validateJsonResponse(ret); assertEquals(5, res.asInt()); assertEquals(5, grid(0).atomicLong("incrKey", 0, true).get()); ret = content(DEFAULT_CACHE_NAME, GridRestCommand.ATOMIC_INCREMENT, "key", "incrKey", "delta", "10" ); res = validateJsonResponse(ret); assertEquals(15, res.asInt()); assertEquals(15, grid(0).atomicLong("incrKey", 0, true).get()); }
/** * Prepare test environment. * @param key Key. * @throws Exception If failed. */ private void prepareAtomic(String key) throws Exception { // Start nodes. for (int i = 0; i < INIT_GRID_NUM; i++) assert startGrid(i) != null; for (int i = 0; i < INIT_GRID_NUM; i++) assert PARTITIONED == grid(i).cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getCacheMode(); // Init cache data. grid(0).atomicLong(key, 0, true).getAndSet(INIT_GRID_NUM); assertEquals(INIT_GRID_NUM, grid(0).atomicLong(key, 0, true).get()); stopGrid(0); } }
/** * @throws Exception If failed. */ @Test public void testAtomicLongTopologyChange() throws Exception { try (IgniteAtomicLong atomic = grid(0).atomicLong(STRUCTURE_NAME, 10, true)) { Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME); assertEquals(10, g.atomicLong(STRUCTURE_NAME, 10, false).get()); assertEquals(20, g.atomicLong(STRUCTURE_NAME, 10, false).addAndGet(10)); stopGrid(NEW_IGNITE_INSTANCE_NAME); assertEquals(20, grid(0).atomicLong(STRUCTURE_NAME, 10, true).get()); } }
/** * @throws Exception If failed. */ @Test public void testDecrement() throws Exception { assertEquals(15L, client().decrement("key", 20L, 5L)); assertEquals(15L, grid().atomicLong("key", 0, true).get()); assertEquals(12L, client().decrement("key", 20L, 3L)); assertEquals(12L, grid().atomicLong("key", 0, true).get()); assertEquals(10L, client().decrement("key", null, 2L)); assertEquals(10L, grid().atomicLong("key", 0, true).get()); assertEquals(15L, client().decrement("key1", 20L, 5L)); assertEquals(15L, grid().atomicLong("key1", 0, true).get()); assertEquals(12L, client().decrement("key1", 20L, 3L)); assertEquals(12L, grid().atomicLong("key1", 0, true).get()); assertEquals(10L, client().decrement("key1", null, 2L)); assertEquals(10L, grid().atomicLong("key1", 0, true).get()); }
/** * @throws Exception If failed. */ @Test public void testIncrement() throws Exception { assertEquals(15L, client().increment("key", 10L, 5L)); assertEquals(15L, grid().atomicLong("key", 0, true).get()); assertEquals(18L, client().increment("key", 20L, 3L)); assertEquals(18L, grid().atomicLong("key", 0, true).get()); assertEquals(20L, client().increment("key", null, 2L)); assertEquals(20L, grid().atomicLong("key", 0, true).get()); assertEquals(15L, client().increment("key1", 10L, 5L)); assertEquals(15L, grid().atomicLong("key1", 0, true).get()); assertEquals(18L, client().increment("key1", 20L, 3L)); assertEquals(18L, grid().atomicLong("key1", 0, true).get()); assertEquals(20L, client().increment("key1", null, 2L)); assertEquals(20L, grid().atomicLong("key1", 0, true).get()); }
/** * @throws Exception If failed. * */ @Test public void testRendezvousAffinity() throws Exception { try { affinityFunction = new RendezvousAffinityFunction(false, 10); startGrids(3); for (int i = 0; i < 3; i++) { IgniteEx igniteEx = grid(i); IgniteAtomicLong atomic = igniteEx.atomicLong("test", 0, true); GridCacheContext cctx = GridTestUtils.getFieldValue(atomic, AtomicDataStructureProxy.class, "ctx"); AffinityFunction aff = cctx.config().getAffinity(); assertNotNull(aff); assertEquals(aff.partitions(), affinityFunction.partitions()); assertEquals(aff.getClass(), affinityFunction.getClass()); } checkAtomics(); } finally { stopAllGrids(); } }
/** * Test AtomicLong. * @param name Name of atomic. * @throws Exception If failed. */ private void checkAtomic(String name) throws Exception { for (int i = INIT_GRID_NUM; i < 20; i++) { startGrid(i); assert PARTITIONED == grid(i).cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getCacheMode(); IgniteAtomicLong atomic = grid(i).atomicLong(name, 0, true); long val = atomic.get(); assertEquals("Atomic check failed for node: " + i, (long)i, val); atomic.incrementAndGet(); stopGrid(i); } }
/** * @throws Exception If failed. */ @Test public void testDefaultAffinity() throws Exception { try { affinityFunction = null; startGrids(3); for (int i = 0; i < 3; i++) { IgniteEx igniteEx = grid(i); IgniteAtomicLong atomic = igniteEx.atomicLong("test", 0, true); GridCacheContext cctx = GridTestUtils.getFieldValue(atomic, AtomicDataStructureProxy.class, "ctx"); AffinityFunction aff = cctx.config().getAffinity(); assertNotNull(aff); } checkAtomics(); } finally { stopAllGrids(); } }
IgniteAtomicLong l = ctx.grid().atomicLong(msg.key(), 0, false);
/** * Tests IgniteAtomicLong. * * @param topWorker Topology change worker. * @throws Exception If failed. */ private void doTestAtomicLong(ConstantTopologyChangeWorker topWorker) throws Exception { try (IgniteAtomicLong s = grid(0).atomicLong(STRUCTURE_NAME, 1, true)) { IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Object>() { @Override public Object apply(Ignite ignite) { assert ignite.atomicLong(STRUCTURE_NAME, 1, true).get() > 0; return null; } }); long val = s.get(); while (!fut.isDone()) { assertEquals(val, s.get()); assertEquals(++val, s.incrementAndGet()); } fut.get(); for (Ignite g : G.allGrids()) assertEquals(val, g.atomicLong(STRUCTURE_NAME, 1, false).get()); } }
/** * @throws Exception If failed. */ @Test public void testTestAffinity() throws Exception { try { affinityFunction = new TestAffinityFunction("Some value"); startGrids(3); for (int i = 0; i < 3; i++) { IgniteEx igniteEx = grid(i); IgniteAtomicLong atomic = igniteEx.atomicLong("test", 0, true); GridCacheContext cctx = GridTestUtils.getFieldValue(atomic, AtomicDataStructureProxy.class, "ctx"); TestAffinityFunction aff = (TestAffinityFunction) cctx.config().getAffinity(); assertNotNull(aff); assertEquals(aff.partitions(), affinityFunction.partitions()); assertEquals(aff.getCustomAttribute(), ((TestAffinityFunction)affinityFunction).getCustomAttribute()); } checkAtomics(); } finally { stopAllGrids(); } }
@Override public Object call() throws Exception { Long init = req.initial(); Long delta = req.delta(); boolean decr = req.command() == ATOMIC_DECREMENT; String key = (String)req.key(); IgniteAtomicLong l = ctx.grid().atomicLong(key, init != null ? init : 0, true); return l.addAndGet(decr ? -delta : delta); } }, false);
/** {@inheritDoc} */ @Override public ByteBuffer makeResponse(final GridRestResponse restRes, List<String> params) { if (restRes.getResponse() == null) { // check if an atomic long with the key exists (related to incr/decr). IgniteAtomicLong l = ctx.grid().atomicLong(params.get(0), 0, false); long val; try { val = l.get(); } catch (Exception ignored) { return GridRedisProtocolParser.nil(); } return GridRedisProtocolParser.toBulkString(val); } if (restRes.getResponse() instanceof String) return GridRedisProtocolParser.toBulkString(restRes.getResponse()); else return GridRedisProtocolParser.toTypeError("Operation against a key holding the wrong kind of value"); } }