/** @throws Exception If failed. */ private void checkRemove(CacheAtomicityMode atomicityMode) throws Exception { this.atomicityMode = atomicityMode; try { IgniteCache<IncorrectCacheKey, String> cache = grid(0).cache(null); cache.remove(new IncorrectCacheKey(0)); fail("Key without hashCode()/equals() was successfully used for remove operation."); } catch (IllegalArgumentException e) { info("Catched expected exception: " + e.getMessage()); assertTrue( e.getMessage().startsWith("Cache key must override hashCode() and equals() methods")); } }
/** @throws Exception If failed. */ public void testClientNodeNotInAffinity() throws Exception { checkCache(CACHE1, 2); checkCache(CACHE2, 2); checkCache(CACHE3, 2); checkCache(CACHE4, 3); checkCache(CACHE5, 2); Ignite client = ignite(NODE_CNT - 1); CacheConfiguration ccfg = new CacheConfiguration(); ccfg.setBackups(0); ccfg.setNodeFilter(new TestNodesFilter()); IgniteCache<Integer, Integer> cache = client.createCache(ccfg); try { checkCache(null, 1); } finally { cache.destroy(); } cache = client.createCache(ccfg, new NearCacheConfiguration()); try { checkCache(null, 1); } finally { cache.destroy(); } }
/** * Tests offset and limit clauses for query. * * @throws Exception If failed. */ public void testOffsetLimit() throws Exception { IgniteCache<Integer, Integer> c = ignite(0).getOrCreateCache(cacheConfig("ints", true, Integer.class, Integer.class)); try { List<Integer> res = new ArrayList<>(); Random rnd = new GridRandom(); for (int i = 0; i < 10; i++) { int val = rnd.nextInt(100); c.put(i, val); res.add(val); } Collections.sort(res); String qry = "select _val from Integer order by _val "; assertEqualsCollections(res, columnQuery(c, qry)); assertEqualsCollections(res.subList(0, 0), columnQuery(c, qry + "limit ?", 0)); assertEqualsCollections(res.subList(0, 3), columnQuery(c, qry + "limit ?", 3)); assertEqualsCollections(res.subList(0, 9), columnQuery(c, qry + "limit ? offset ?", 9, 0)); assertEqualsCollections(res.subList(3, 7), columnQuery(c, qry + "limit ? offset ?", 4, 3)); assertEqualsCollections(res.subList(7, 9), columnQuery(c, qry + "limit ? offset ?", 2, 7)); assertEqualsCollections(res.subList(8, 10), columnQuery(c, qry + "limit ? offset ?", 2, 8)); assertEqualsCollections(res.subList(9, 10), columnQuery(c, qry + "limit ? offset ?", 1, 9)); assertEqualsCollections(res.subList(10, 10), columnQuery(c, qry + "limit ? offset ?", 1, 10)); assertEqualsCollections( res.subList(9, 10), columnQuery(c, qry + "limit ? offset abs(-(4 + ?))", 1, 5)); } finally { c.destroy(); } }
/** * Execute individual put and get, getting value in portable format, without de-serializing it. * * @param cache Cache. */ private static void putGetPortable(IgniteCache<Integer, Organization> cache) { // Create new Organization portable object to store in cache. Organization org = new Organization( "Microsoft", // Name. new Address("1096 Eddy Street, San Francisco, CA", 94109), // Address. OrganizationType.PRIVATE, // Type. new Timestamp(System.currentTimeMillis())); // Last update time. // Put created data entry to cache. cache.put(1, org); // Get cache that will get values as portable objects. IgniteCache<Integer, PortableObject> portableCache = cache.withKeepPortable(); // Get recently created organization as a portable object. PortableObject po = portableCache.get(1); // Get organization's name from portable object (note that // object doesn't need to be fully deserialized). String name = po.field("name"); System.out.println(); System.out.println(">>> Retrieved organization name from portable object: " + name); }
/** @throws Exception if failed. */ public void testDynamicCache() throws Exception { IgniteEx ignite = startGrid(0); String cacheName = "dynamic"; CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(cacheName); ccfg.setCacheMode(CacheMode.REPLICATED); ccfg.setRebalanceMode(CacheRebalanceMode.SYNC); IgniteCache<Object, Object> cache = ignite.createCache(ccfg); try (IgniteDataStreamer<Object, Object> streamer = ignite.dataStreamer(cacheName)) { streamer.allowOverwrite(true); for (int i = 0; i < 100_000; i++) streamer.addData(i, i); } assertEquals(CNT, cache.localSize()); Ignite ignite2 = startGrid(1); assertEquals( CNT, ignite2.cache(cacheName).localSize(CachePeekMode.PRIMARY, CachePeekMode.BACKUP)); for (int i = 0; i < CNT; i++) assertEquals(i, ignite2.cache(cacheName).localPeek(i)); }
/** * @param cache Cache. * @return Cache. */ protected <K, V> GridCacheAdapter<K, V> internalCache(IgniteCache<K, V> cache) { if (isMultiJvmObject(cache)) throw new UnsupportedOperationException( "Oparetion can't be supported automatically for multi jvm " + "(send closure instead)."); return ((IgniteKernal) cache.unwrap(Ignite.class)).internalCache(cache.getName()); }
/** * Execute bulk {@code putAll(...)} and {@code getAll(...)} operations. * * @param cache Cache. */ private static void putGetAll(IgniteCache<Integer, Organization> cache) { // Create new Organization portable objects to store in cache. Organization org1 = new Organization( "Microsoft", // Name. new Address("1096 Eddy Street, San Francisco, CA", 94109), // Address. OrganizationType.PRIVATE, // Type. new Timestamp(System.currentTimeMillis())); // Last update time. Organization org2 = new Organization( "Red Cross", // Name. new Address("184 Fidler Drive, San Antonio, TX", 78205), // Address. OrganizationType.NON_PROFIT, // Type. new Timestamp(System.currentTimeMillis())); // Last update time. Map<Integer, Organization> map = new HashMap<>(); map.put(1, org1); map.put(2, org2); // Put created data entries to cache. cache.putAll(map); // Get recently created organizations as a strongly-typed fully de-serialized instances. Map<Integer, Organization> mapFromCache = cache.getAll(map.keySet()); System.out.println(); System.out.println(">>> Retrieved organization instances from cache:"); for (Organization org : mapFromCache.values()) System.out.println(">>> " + org); }
/** * Executes example. * * @param args Command line arguments, none required. */ public static void main(String[] args) { try (Ignite ignite = Ignition.start("examples/config/portable/example-ignite-portable.xml")) { System.out.println(); System.out.println(">>> Portable objects cache put-get example started."); CacheConfiguration<Integer, Organization> cfg = new CacheConfiguration<>(); cfg.setCacheMode(CacheMode.PARTITIONED); cfg.setName(CACHE_NAME); cfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); try (IgniteCache<Integer, Organization> cache = ignite.createCache(cfg)) { if (ignite.cluster().forDataNodes(cache.getName()).nodes().isEmpty()) { System.out.println(); System.out.println(">>> This example requires remote cache node nodes to be started."); System.out.println(">>> Please start at least 1 remote cache node."); System.out.println(">>> Refer to example's javadoc for details on configuration."); System.out.println(); return; } putGet(cache); putGetPortable(cache); putGetAll(cache); putGetAllPortable(cache); System.out.println(); } finally { // Delete cache with its content completely. ignite.destroyCache(CACHE_NAME); } } }
/** * Data streamer should correctly load entries from HashMap in case of grids with more than one * node and with GridOptimizedMarshaller that requires serializable. * * @throws Exception If failed. */ public void testAddDataFromMap() throws Exception { cnt = 0; startGrids(2); Ignite g0 = grid(0); IgniteDataStreamer<Integer, String> dataLdr = g0.dataStreamer(null); Map<Integer, String> map = U.newHashMap(KEYS_COUNT); for (int i = 0; i < KEYS_COUNT; i++) map.put(i, String.valueOf(i)); dataLdr.addData(map); dataLdr.close(); Random rnd = new Random(); IgniteCache<Integer, String> c = g0.cache(null); for (int i = 0; i < KEYS_COUNT; i++) { Integer k = rnd.nextInt(KEYS_COUNT); String v = c.get(k); assertEquals(k.toString(), v); } }
/** * Query all purchases made at a specific store for 3 specific products. This query uses * cross-cache joins between {@link DimStore}, {@link DimProduct} objects stored in {@code * 'replicated'} cache and {@link FactPurchase} objects stored in {@code 'partitioned'} cache. * * @throws IgniteException If failed. */ private static void queryProductPurchases() { IgniteCache<Integer, FactPurchase> factCache = Ignition.ignite().cache(PARTITIONED_CACHE_NAME); // All purchases for certain product made at store2. // ================================================= DimProduct p1 = rand(dataProduct.values()); DimProduct p2 = rand(dataProduct.values()); DimProduct p3 = rand(dataProduct.values()); System.out.println( "IDs of products [p1=" + p1.getId() + ", p2=" + p2.getId() + ", p3=" + p3.getId() + ']'); // Create cross cache query to get all purchases made at store2 // for specified products. QueryCursor<Cache.Entry<Integer, FactPurchase>> prodPurchases = factCache.query( new SqlQuery( FactPurchase.class, "from \"" + REPLICATED_CACHE_NAME + "\".DimStore, \"" + REPLICATED_CACHE_NAME + "\".DimProduct, " + "\"" + PARTITIONED_CACHE_NAME + "\".FactPurchase " + "where DimStore.id=FactPurchase.storeId and DimProduct.id=FactPurchase.productId " + "and DimStore.name=? and DimProduct.id in(?, ?, ?)") .setArgs("Store2", p1.getId(), p2.getId(), p3.getId())); printQueryResults( "All purchases made at store2 for 3 specific products:", prodPurchases.getAll()); }
/** @throws Exception If failed. */ public void testObjectArgument() throws Exception { IgniteCache<TestKey, Person> cache = ignite(0).cache(null); for (int i = 0; i < 100; i++) cache.put(new TestKey(i), new Person("name-" + i)); SqlQuery<TestKey, Person> qry = new SqlQuery<>(Person.class, "where _key=?"); IgniteBinary binary = ignite(0).binary(); for (int i = 0; i < 100; i++) { Object key = new TestKey(i); if (i % 2 == 0) key = binary.toBinary(key); qry.setArgs(key); List<Cache.Entry<TestKey, Person>> res = cache.query(qry).getAll(); assertEquals(1, res.size()); Person p = res.get(0).getValue(); assertEquals("name-" + i, p.name); } }
/** * @param cache Cache. * @return DHT cache. */ protected static <K, V> GridDhtCacheAdapter<K, V> dht(IgniteCache<K, V> cache) { return nearEnabled(cache) ? near(cache).dht() : ((IgniteKernal) cache.unwrap(Ignite.class)) .<K, V>internalCache(cache.getName()) .context() .dht(); }
/** @throws Exception If failed. */ public void testScanQueryReconnect() throws Exception { Ignite cln = grid(serverCount()); assertTrue(cln.cluster().localNode().isClient()); final Ignite srv = clientRouter(cln); final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE); final IgniteCache<Integer, Person> srvCache = srv.getOrCreateCache(QUERY_CACHE); for (int i = 0; i < 10_000; i++) clnCache.put(i, new Person(i, "name-" + i, "surname-" + i)); final ScanQuery<Integer, Person> scanQry = new ScanQuery<>(); scanQry.setPageSize(1); scanQry.setFilter( new IgniteBiPredicate<Integer, Person>() { @Override public boolean apply(Integer integer, Person person) { return true; } }); QueryCursor<Cache.Entry<Integer, Person>> qryCursor = clnCache.query(scanQry); reconnectClientNode( cln, srv, new Runnable() { @Override public void run() { srvCache.put(10_001, new Person(10_001, "name", "surname")); try { clnCache.query(scanQry); fail(); } catch (CacheException e) { check(e); } } }); try { qryCursor.getAll(); fail(); } catch (CacheException e) { checkAndWait(e); } qryCursor = clnCache.query(scanQry); assertEquals(10_001, qryCursor.getAll().size()); }
/** @throws Exception In case of error. */ public void testPutWithExpiration() throws Exception { IgniteCache<String, String> cache1 = ignite1.cache(null); IgniteCache<String, String> cache2 = ignite2.cache(null); IgniteCache<String, String> cache3 = ignite3.cache(null); cache1.put("key", "val"); Transaction tx = ignite1.transactions().txStart(); long ttl = 500; cache1 .withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl))) .put("key", "val"); assert cache1.get("key") != null; tx.commit(); info("Going to sleep for: " + (ttl + 1000)); // Allow for expiration. Thread.sleep(ttl + 1000); String v1 = cache1.get("key"); String v2 = cache2.get("key"); String v3 = cache3.get("key"); assert v1 == null : "V1 should be null: " + v1; assert v2 == null : "V2 should be null: " + v2; assert v3 == null : "V3 should be null: " + v3; }
/** * Makes initial cache loading. * * @param cache Cache to load. */ private static void loadCache(IgniteCache<Long, Person> cache) { long start = System.currentTimeMillis(); // Start loading cache from persistent store on all caching nodes. cache.loadCache(null, ENTRY_COUNT); long end = System.currentTimeMillis(); System.out.println( ">>> Loaded " + cache.size() + " keys with backups in " + (end - start) + "ms."); }
@Override public void execute(ServiceContext ctx) throws Exception { log.info("Service execute!"); Integer a = cache.get("a"); if (a == null) { a = 0; } while (!ctx.isCancelled()) { Thread.sleep(1000); log.info("Service executing something important..."); cache.put("a", a++); } }
/** @throws Exception If test failed. */ public void testBasicLock() throws Exception { IgniteCache<Integer, String> cache = ignite1.cache(null); Lock lock = cache.lock(1); lock.lock(); assert cache.isLocalLocked(1, false); assert cache.isLocalLocked(1, true); lock.unlock(); checkUnlocked(cache, 1); }
/** @param ignite Ignite. */ private static void populateCacheCar(Ignite ignite) { if (log.isDebugEnabled()) log.debug("DEMO: Start cars population..."); IgniteCache<Integer, Parking> cacheParking = ignite.cache(PARKING_CACHE_NAME); for (int i = 0, n = 1; i < PARK_CNT; i++, n++) cacheParking.put(i, new Parking(i, "Parking #" + n, n * 10)); IgniteCache<Integer, Car> cacheCar = ignite.cache(CAR_CACHE_NAME); for (int i = 0, n = 1; i < CAR_CNT; i++, n++) cacheCar.put(i, new Car(i, rnd.nextInt(PARK_CNT), "Car #" + n)); if (log.isDebugEnabled()) log.debug("DEMO: Finished cars population."); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void onEntryAccessed(boolean rmv, EvictableEntry<K, V> entry) { if (!entry.isCached()) return; IgniteCache<K, V> cache = entry.unwrap(IgniteCache.class); int size = cache.localSize(CachePeekMode.ONHEAP); for (int i = max; i < size; i++) { Cache.Entry<K, V> e = cache.randomEntry(); if (e != null) e.unwrap(EvictableEntry.class).evict(); } }
/** {@inheritDoc} */ @Override protected void beforeTestsStarted() throws Exception { startGridsMultiThreaded(3); IgniteCache<Integer, TestObject> cache = grid(0).cache(null); assert cache != null; TestObject o = createObjectWithData(1); cache.put(1, o); cache.put(2, new TestObject(2)); cache.put(3, new TestObject(3)); Class.forName("org.apache.ignite.IgniteJdbcDriver"); }
/** * In ATOMIC cache with CLOCK mode if key is updated from different nodes at same time only one * update wins others are ignored (can happen in test event when updates are executed from * different nodes sequentially), this delay is used to avoid lost updates. * * @param cache Cache. * @throws Exception If failed. */ protected void atomicClockModeDelay(IgniteCache cache) throws Exception { CacheConfiguration ccfg = (CacheConfiguration) cache.getConfiguration(CacheConfiguration.class); if (ccfg.getCacheMode() != LOCAL && ccfg.getAtomicityMode() == CacheAtomicityMode.ATOMIC && ccfg.getAtomicWriteOrderMode() == CacheAtomicWriteOrderMode.CLOCK) U.sleep(50); }
private <T> void executeWithTimeout( Consumer<IgniteCache<K, V>> cacheOp, Handler<AsyncResult<T>> handler, long timeout) { try { IgniteCache<K, V> cache = this.cache.withAsync(); cacheOp.accept(cache); IgniteFuture<T> future = cache.future(); if (timeout >= 0) { vertx.executeBlocking(f -> future.get(timeout), handler); } else { future.listen(fut -> vertx.executeBlocking(f -> f.complete(future.get()), handler)); } } catch (Exception e) { handler.handle(Future.failedFuture(e)); } }
/** * Execute individual put and get. * * @param cache Cache. */ private static void putGet(IgniteCache<Integer, Organization> cache) { // Create new Organization portable object to store in cache. Organization org = new Organization( "Microsoft", // Name. new Address("1096 Eddy Street, San Francisco, CA", 94109), // Address. OrganizationType.PRIVATE, // Type. new Timestamp(System.currentTimeMillis())); // Last update time. // Put created data entry to cache. cache.put(1, org); // Get recently created organization as a strongly-typed fully de-serialized instance. Organization orgFromCache = cache.get(1); System.out.println(); System.out.println(">>> Retrieved organization instance from cache: " + orgFromCache); }
/** @throws Exception If failed. */ public void testQueryReconnect() throws Exception { Ignite cln = grid(serverCount()); assertTrue(cln.cluster().localNode().isClient()); final Ignite srv = clientRouter(cln); final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE); final IgniteCache<Integer, Person> srvCache = srv.getOrCreateCache(QUERY_CACHE); clnCache.put(1, new Person(1, "name1", "surname1")); clnCache.put(2, new Person(2, "name2", "surname2")); clnCache.put(3, new Person(3, "name3", "surname3")); final SqlQuery<Integer, Person> qry = new SqlQuery<>(Person.class, "_key <> 0"); qry.setPageSize(1); QueryCursor<Cache.Entry<Integer, Person>> cur = clnCache.query(qry); reconnectClientNode( cln, srv, new Runnable() { @Override public void run() { srvCache.put(4, new Person(4, "name4", "surname4")); try { clnCache.query(qry); fail(); } catch (CacheException e) { check(e); } } }); List<Cache.Entry<Integer, Person>> res = cur.getAll(); assertNotNull(res); assertEquals(4, res.size()); }
/** @throws Exception If failed. */ public void testGroupIndexOperations() throws Exception { IgniteCache<Integer, GroupIndexTestValue> c = ignite(0) .getOrCreateCache(cacheConfig("grp", false, Integer.class, GroupIndexTestValue.class)); try { // Check group index usage. String qry = "select 1 from GroupIndexTestValue "; String plan = columnQuery(c, "explain " + qry + "where a = 1 and b > 0").get(0).toString(); info("Plan: " + plan); assertTrue(plan.contains("grpIdx")); // Sorted list List<GroupIndexTestValue> list = F.asList( new GroupIndexTestValue(0, 0), new GroupIndexTestValue(0, 5), new GroupIndexTestValue(1, 1), new GroupIndexTestValue(1, 3), new GroupIndexTestValue(2, -1), new GroupIndexTestValue(2, 2)); // Fill cache. for (int i = 0; i < list.size(); i++) c.put(i, list.get(i)); // Check results. assertEquals(1, columnQuery(c, qry + "where a = 1 and b = 1").size()); assertEquals(2, columnQuery(c, qry + "where a = 1 and b < 4").size()); assertEquals(2, columnQuery(c, qry + "where a = 1 and b <= 3").size()); assertEquals(1, columnQuery(c, qry + "where a = 1 and b < 3").size()); assertEquals(2, columnQuery(c, qry + "where a = 1 and b > 0").size()); assertEquals(1, columnQuery(c, qry + "where a = 1 and b > 1").size()); assertEquals(2, columnQuery(c, qry + "where a = 1 and b >= 1").size()); assertEquals(4, columnQuery(c, qry + "where a > 0 and b > 0").size()); assertEquals(4, columnQuery(c, qry + "where a > 0 and b >= 1").size()); assertEquals(3, columnQuery(c, qry + "where a > 0 and b > 1").size()); } finally { c.destroy(); } }
/** * Executes transaction with read/write-through to persistent store. * * @param cache Cache to execute transaction on. */ private static void executeTransaction(IgniteCache<Long, Person> cache) { try (Transaction tx = Ignition.ignite().transactions().txStart()) { Person val = cache.get(id); System.out.println("Read value: " + val); val = cache.getAndPut(id, new Person(id, "Isaac", "Newton")); System.out.println("Overwrote old value: " + val); val = cache.get(id); System.out.println("Read value: " + val); tx.commit(); } System.out.println("Read value after commit: " + cache.get(id)); }
/** * @param cache Cache. * @param key Key. * @throws IgniteCheckedException If failed. */ @SuppressWarnings({"BusyWait"}) private void checkUnlocked(IgniteCache<Integer, String> cache, Integer key) throws IgniteCheckedException { assert !cache.isLocalLocked(key, true); if (partitioned()) { for (int i = 0; i < 200; i++) if (cache.isLocalLocked(key, false)) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } else return; } assertFalse( "Key locked [key=" + key + ", entries=" + entries(key) + "]", cache.isLocalLocked(key, false)); }
/** @throws Exception if failed. */ public void testStaticCache() throws Exception { IgniteEx ignite = startGrid(0); IgniteCache<Object, Object> cache = ignite.cache(STATIC_CACHE_NAME); try (IgniteDataStreamer<Object, Object> streamer = ignite.dataStreamer(STATIC_CACHE_NAME)) { streamer.allowOverwrite(true); for (int i = 0; i < 100_000; i++) streamer.addData(i, i); } assertEquals(CNT, cache.localSize()); Ignite ignite2 = startGrid(1); assertEquals( CNT, ignite2.cache(STATIC_CACHE_NAME).localSize(CachePeekMode.PRIMARY, CachePeekMode.BACKUP)); for (int i = 0; i < CNT; i++) assertEquals(i, ignite2.cache(STATIC_CACHE_NAME).localPeek(i)); }
/** * Query all purchases made at a specific store. This query uses cross-cache joins between {@link * DimStore} objects stored in {@code 'replicated'} cache and {@link FactPurchase} objects stored * in {@code 'partitioned'} cache. * * @throws IgniteException If failed. */ private static void queryStorePurchases() { IgniteCache<Integer, FactPurchase> factCache = Ignition.ignite().cache(PARTITIONED_CACHE_NAME); // All purchases for store1. // ======================== // Create cross cache query to get all purchases made at store1. QueryCursor<Cache.Entry<Integer, FactPurchase>> storePurchases = factCache.query( new SqlQuery( FactPurchase.class, "from \"" + REPLICATED_CACHE_NAME + "\".DimStore, \"" + PARTITIONED_CACHE_NAME + "\".FactPurchase " + "where DimStore.id=FactPurchase.storeId and DimStore.name=?") .setArgs("Store1")); printQueryResults("All purchases made at store1:", storePurchases.getAll()); }
/** * @param g Grid. * @throws Exception If failed. */ private void checkNodes(Ignite g) throws Exception { IgniteCache<String, String> cache = g.cache("test"); for (char c = 'a'; c <= 'z'; c++) { String key = Character.toString(c); cache.put(key, "val-" + key); String v1 = cache.get(key); String v2 = cache.get(key); // Get second time. info("v1: " + v1); info("v2: " + v2); assertNotNull(v1); assertNotNull(v2); if (affinity(cache).mapKeyToNode(key).isLocal()) assertSame(v1, v2); else assertEquals(v1, v2); } }