Java Code Examples for org.apache.ignite.Ignite#affinity()
The following examples show how to use
org.apache.ignite.Ignite#affinity() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridCacheTxNodeFailureSelfTest.java From ignite with Apache License 2.0 | 6 votes |
/** * @param ignite Ignite instance to generate key. * @param backup Backup key flag. * @return Generated key that is not primary nor backup for {@code ignite(0)} and primary for * {@code ignite(1)}. */ private int generateKey(Ignite ignite, boolean backup) { Affinity<Object> aff = ignite.affinity(DEFAULT_CACHE_NAME); for (int key = 0;;key++) { if (backup) { if (!aff.isBackup(ignite(0).cluster().localNode(), key)) continue; } else { if (aff.isPrimaryOrBackup(ignite(0).cluster().localNode(), key)) continue; } if (aff.isPrimary(ignite(1).cluster().localNode(), key)) return key; } }
Example 2
Source File: CacheLateAffinityAssignmentTest.java From ignite with Apache License 2.0 | 5 votes |
/** * @param ignite Node. * @param affinity Affinity. * @throws Exception If failed. */ private void checkServicesDeploy(Ignite ignite, final List<List<ClusterNode>> affinity) throws Exception { Affinity<Object> aff = ignite.affinity(CACHE_NAME1); for (int i = 0; i < 10; i++) { final int part = aff.partition(i); final String srvcName = "service-" + i; final ClusterNode srvcNode = affinity.get(part).get(0); boolean wait = GridTestUtils.waitForCondition(new PA() { @Override public boolean apply() { TestService srvc = grid(srvcNode).services().service(srvcName); if (srvc == null) return false; assertEquals(srvcNode, srvc.serviceNode()); return true; } }, 5000); assertTrue(wait); } }
Example 3
Source File: IgniteCacheTxPreloadNoWriteTest.java From ignite with Apache License 2.0 | 5 votes |
/** * @param commit {@code True} if commit transaction. * @throws Exception If failed. */ private void txNoWrite(boolean commit) throws Exception { Ignite ignite0 = startGrid(0); Affinity<Integer> aff = ignite0.affinity(DEFAULT_CACHE_NAME); IgniteCache<Integer, Object> cache0 = ignite0.cache(DEFAULT_CACHE_NAME); try (IgniteDataStreamer<Integer, Object> streamer = ignite0.dataStreamer(DEFAULT_CACHE_NAME)) { for (int i = 0; i < 1000; i++) streamer.addData(i + 10000, new byte[1024]); } Ignite ignite1 = startGrid(1); Integer key = primaryKey(ignite1.cache(DEFAULT_CACHE_NAME)); // Want test scenario when ignite1 is new primary node, but ignite0 is still partition owner. assertTrue(aff.isPrimary(ignite1.cluster().localNode(), key)); try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { cache0.get(key); if (commit) tx.commit(); } GridCacheAdapter cacheAdapter = ((IgniteKernal)ignite(0)).context().cache().internalCache(DEFAULT_CACHE_NAME); // Check all transactions are finished. assertEquals(0, cacheAdapter.context().tm().idMapSize()); // Try to start one more node. startGrid(2); }
Example 4
Source File: IgniteDbPutGetAbstractTest.java From ignite with Apache License 2.0 | 5 votes |
/** * @param partCntrs Expected per-partition entries count. */ private void checkScanPartition(Ignite ignite, IgniteCache<DbKey, DbValue> cache, Map<Integer, Integer> partCntrs, boolean loc) { Affinity<Object> aff = ignite.affinity(cache.getName()); int parts = aff.partitions(); for (int p = 0; p < parts; p++) { ScanQuery<DbKey, DbValue> qry = new ScanQuery<>(); qry.setPartition(p); qry.setLocal(loc); if (loc && !ignite.cluster().localNode().equals(aff.mapPartitionToNode(p))) continue; QueryCursor<Cache.Entry<DbKey, DbValue>> cur = cache.query(qry); Set<DbKey> allKeys = new HashSet<>(); for (Cache.Entry<DbKey, DbValue> e : cur) { allKeys.add(e.getKey()); assertEquals(e.getKey().val, e.getValue().iVal); } Integer exp = partCntrs.get(p); if (exp == null) exp = 0; assertEquals(exp, (Integer)allKeys.size()); } }
Example 5
Source File: GridCacheClientNodeBinaryObjectMetadataTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @throws Exception If failed. */ @Test public void testBinaryMetadataOnClient() throws Exception { Ignite ignite0 = ignite(gridCount() - 1); assertTrue(ignite0.configuration().isClientMode()); Ignite ignite1 = ignite(0); assertFalse(ignite1.configuration().isClientMode()); Affinity<Object> aff0 = ignite0.affinity(DEFAULT_CACHE_NAME); Affinity<Object> aff1 = ignite1.affinity(DEFAULT_CACHE_NAME); for (int i = 0; i < 100; i++) { TestObject1 obj1 = new TestObject1(i, i + 1); assertEquals(aff1.mapKeyToPrimaryAndBackups(obj1), aff0.mapKeyToPrimaryAndBackups(obj1)); TestObject2 obj2 = new TestObject2(i, i + 1); assertEquals(aff1.mapKeyToPrimaryAndBackups(obj2), aff0.mapKeyToPrimaryAndBackups(obj2)); } Collection<BinaryType> meta1 = ignite1.binary().types(); Collection<BinaryType> meta2 = ignite1.binary().types(); assertEquals(meta1.size(), meta2.size()); for (BinaryType m1 : meta1) { boolean found = false; for (BinaryType m2 : meta1) { if (m1.typeName().equals(m2.typeName())) { assertEquals(m1.affinityKeyFieldName(), m2.affinityKeyFieldName()); assertEquals(m1.fieldNames(), m2.fieldNames()); found = true; break; } } assertTrue(found); } }
Example 6
Source File: IgniteSqlSplitterSelfTest.java From ignite with Apache License 2.0 | 4 votes |
/** Constructor */ AffinityKeyGenerator(Ignite node, String cacheName) { this.affinity = node.affinity(cacheName); this.node = node.cluster().localNode(); }
Example 7
Source File: IgniteCacheClientNodeChangingTopologyTest.java From ignite with Apache License 2.0 | 4 votes |
/** * Tries to find keys for two partitions: for one partition assignment should not change after node join, * for another primary node should change. * * @param ignite Ignite. * @param nodes Current nodes. * @return Found keys. */ private IgniteBiTuple<Integer, Integer> findKeys(Ignite ignite, ClusterNode...nodes) { ClusterNode newNode = new TcpDiscoveryNode(); GridTestUtils.setFieldValue(newNode, "consistentId", getTestIgniteInstanceName(4)); GridTestUtils.setFieldValue(newNode, "id", UUID.randomUUID()); List<ClusterNode> topNodes = new ArrayList<>(); Collections.addAll(topNodes, nodes); topNodes.add(newNode); DiscoveryEvent discoEvt = new DiscoveryEvent(newNode, "", EventType.EVT_NODE_JOINED, newNode); final long topVer = ignite.cluster().topologyVersion(); GridAffinityFunctionContextImpl ctx = new GridAffinityFunctionContextImpl(topNodes, null, discoEvt, new AffinityTopologyVersion(topVer + 1), 1); AffinityFunction affFunc = ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getAffinity(); List<List<ClusterNode>> newAff = affFunc.assignPartitions(ctx); List<List<ClusterNode>> curAff = ((IgniteKernal)ignite).context().cache().internalCache(DEFAULT_CACHE_NAME).context(). affinity().assignments(new AffinityTopologyVersion(topVer)); Integer key1 = null; Integer key2 = null; Affinity<Integer> aff = ignite.affinity(DEFAULT_CACHE_NAME); for (int i = 0; i < curAff.size(); i++) { if (key1 == null) { List<ClusterNode> oldNodes = curAff.get(i); List<ClusterNode> newNodes = newAff.get(i); if (oldNodes.equals(newNodes)) key1 = findKey(aff, i); } if (key2 == null) { ClusterNode oldPrimary = F.first(curAff.get(i)); ClusterNode newPrimary = F.first(newAff.get(i)); if (!oldPrimary.equals(newPrimary)) key2 = findKey(aff, i); } if (key1 != null && key2 != null) break; } if (key1 == null || key2 == null) fail("Failed to find nodes required for test."); return new IgniteBiTuple<>(key1, key2); }
Example 8
Source File: GridCommonAbstractTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @param key Key. * @param cacheName Cache name. * @return Ignite instances which has backup cache for given key. */ protected List<Ignite> backupNodes(Object key, String cacheName) { List<Ignite> allGrids = Ignition.allGrids(); assertFalse("There are no alive nodes.", F.isEmpty(allGrids)); Ignite ignite = allGrids.get(0); Affinity<Object> aff = ignite.affinity(cacheName); Collection<ClusterNode> nodes = aff.mapKeyToPrimaryAndBackups(key); assertTrue("Expected more than one node for key [key=" + key + ", nodes=" + nodes + ']', nodes.size() > 1); Iterator<ClusterNode> it = nodes.iterator(); it.next(); // Skip primary. List<Ignite> backups = new ArrayList<>(nodes.size() - 1); while (it.hasNext()) backups.add(grid(it.next())); return backups; }
Example 9
Source File: GridCommonAbstractTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @param key Key. * @param cacheName Cache name. * @return Ignite instance which has primary cache for given key. */ protected Ignite primaryNode(Object key, String cacheName) { List<Ignite> allGrids = Ignition.allGrids(); assertFalse("There are no alive nodes.", F.isEmpty(allGrids)); Ignite ignite = allGrids.get(0); Affinity<Object> aff = ignite.affinity(cacheName); ClusterNode node = aff.mapKeyToNode(key); assertNotNull("There are no cache affinity nodes", node); return grid(node); }
Example 10
Source File: IgniteCacheDistributedJoinCollocatedAndNotTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @throws Exception If failed. */ @Test public void testJoin() throws Exception { Ignite client = grid(2); IgniteCache<Object, Object> personCache = client.cache(PERSON_CACHE); IgniteCache<Object, Object> orgCache = client.cache(ORG_CACHE); IgniteCache<Object, Object> accCache = client.cache(ACCOUNT_CACHE); Affinity<Object> aff = client.affinity(PERSON_CACHE); AtomicInteger orgKey = new AtomicInteger(); AtomicInteger accKey = new AtomicInteger(); ClusterNode node0 = ignite(0).cluster().localNode(); ClusterNode node1 = ignite(1).cluster().localNode(); /** * One organization, one person, two accounts. */ int orgId1 = keyForNode(aff, orgKey, node0); orgCache.put(orgId1, new Organization("obj-" + orgId1)); personCache.put(new PersonKey(1, orgId1), new Person(1, "o1-p1")); personCache.put(new PersonKey(2, orgId1), new Person(2, "o1-p2")); accCache.put(keyForNode(aff, accKey, node0), new Account(1, "a0")); accCache.put(keyForNode(aff, accKey, node1), new Account(1, "a1")); // Join on affinity keys equals condition should not be distributed. String qry = "select o.name, p._key, p.name " + "from \"org\".Organization o, \"person\".Person p " + "where p.affKey = o._key"; assertFalse(plan(qry, orgCache, false).contains("batched")); checkQuery(qry, orgCache, false, 2); checkQuery("select o.name, p._key, p.name, a.name " + "from \"org\".Organization o, \"person\".Person p, \"acc\".Account a " + "where p.affKey = o._key and p.id = a.personId", orgCache, true, 2); }
Example 11
Source File: CacheStoreUsageMultinodeAbstractTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @param cache Cache. * @param key Key. * @param tc Transaction concurrency mode. * @throws Exception If failed. */ protected void testStoreUpdate(IgniteCache<Object, Object> cache, Object key, @Nullable TransactionConcurrency tc) throws Exception { boolean storeOnPrimary = atomicityMode() == ATOMIC || locStore || writeBehind; assertTrue(writeMap.isEmpty()); Ignite ignite = cache.unwrap(Ignite.class); Affinity<Object> obj = ignite.affinity(cache.getName()); ClusterNode node = obj.mapKeyToNode(key); assertNotNull(node); String expNode = storeOnPrimary ? (String)node.attribute(ATTR_IGNITE_INSTANCE_NAME) : ignite.name(); assertNotNull(expNode); log.info("Put [node=" + ignite.name() + ", key=" + key + ", primary=" + node.attribute(ATTR_IGNITE_INSTANCE_NAME) + ", tx=" + tc + ", nearCache=" + (cache.getConfiguration(CacheConfiguration.class).getNearConfiguration() != null) + ", storeOnPrimary=" + storeOnPrimary + ']'); Transaction tx = tc != null ? ignite.transactions().txStart(tc, REPEATABLE_READ) : null; try { cache.put(key, key); if (tx != null) tx.commit(); } finally { if (tx != null) tx.close(); } boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() { @Override public boolean apply() { return !writeMap.isEmpty(); } }, 1000); assertTrue("Store is not updated", wait); assertEquals("Write on wrong node: " + writeMap, locStore ? 2 : 1, writeMap.size()); if (!locStore) assertEquals(expNode, writeMap.keySet().iterator().next()); writeMap.clear(); }
Example 12
Source File: IgniteCacheDistributedJoinQueryConditionsTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @throws Exception If failed. */ @Test public void testJoinQuery4() throws Exception { Ignite client = grid(2); try { CacheConfiguration ccfg1 = cacheConfiguration(PERSON_CACHE).setQueryEntities(F.asList(personEntity(true, false))); IgniteCache<Object, Object> pCache = client.createCache(ccfg1); ClusterNode node0 = ignite(0).cluster().localNode(); ClusterNode node1 = ignite(1).cluster().localNode(); Affinity<Object> aff = client.affinity(PERSON_CACHE); AtomicInteger pKey = new AtomicInteger(); Integer pId0 = keyForNode(aff, pKey, node0); pCache.put(pId0, new Person(0, "p0")); for (int i = 0; i < 3; i++) { Integer pId = keyForNode(aff, pKey, node1); pCache.put(pId, new Person(0, "p")); } checkQuery("select p1._key, p1.name, p2._key, p2.name " + "from Person p1, Person p2 " + "where p2._key > p1._key", pCache, 6); checkQuery("select p1._key, p1.name, p2._key, p2.name " + "from Person p1, Person p2 " + "where p2._key > p1._key and p1._key=" + pId0, pCache, 3); checkQuery("select p1._key, p1.name, p2._key, p2.name " + "from Person p1, Person p2 " + "where p2._key > p1._key and p1.name='p0'", pCache, 3); checkQuery("select p1._key, p1.name, p2._key, p2.name " + "from Person p1, Person p2 " + "where p1.name > p2.name", pCache, 3); } finally { client.destroyCache(PERSON_CACHE); client.destroyCache(ORG_CACHE); } }
Example 13
Source File: GridCacheLazyQueryPartitionsReleaseTest.java From ignite with Apache License 2.0 | 3 votes |
/** * Lazy query release partitions on cursor close test. * * @throws Exception If failed. */ @Test public void testLazyQueryPartitionsReleaseOnClose() throws Exception { Ignite node1 = startGrid(0); IgniteCache<Integer, Person> cache = node1.cache(PERSON_CACHE); cache.clear(); Affinity<Integer> aff = node1.affinity(PERSON_CACHE); int partsFilled = fillAllPartitions(cache, aff); SqlFieldsQuery qry = new SqlFieldsQuery("select name, age from person") .setPageSize(1); FieldsQueryCursor<List<?>> qryCursor = cache.query(qry); Iterator<List<?>> it = qryCursor.iterator(); if (it.hasNext()) it.next(); else fail("No query results."); startGrid(1); // Close cursor. Partitions should be released now. qryCursor.close(); for (Ignite ig : G.allGrids()) ig.cache(PERSON_CACHE).rebalance().get(); assertEquals("Wrong result set size", partsFilled, cache.query(qry).getAll().size()); }
Example 14
Source File: GridCacheAtomicNearCacheSelfTest.java From ignite with Apache License 2.0 | 3 votes |
/** * @throws Exception If failed. */ @SuppressWarnings("ZeroLengthArrayAllocation") private void checkReaderRemove() throws Exception { Ignite ignite0 = grid(0); IgniteCache<Integer, Integer> cache0 = ignite0.cache(DEFAULT_CACHE_NAME); Affinity<Integer> aff = ignite0.affinity(DEFAULT_CACHE_NAME); UUID id0 = ignite0.cluster().localNode().id(); Integer nearKey = key(ignite0, NOT_PRIMARY_AND_BACKUP); cache0.put(nearKey, 1); // Put should create near entry on grid0. for (int i = 0; i < GRID_CNT; i++) { UUID[] expReaders = aff.isPrimary(grid(i).localNode(), nearKey) ? new UUID[] {id0} : new UUID[] {}; checkEntry(grid(i), nearKey, 1, i == 0, expReaders); } cache0.remove(nearKey); // Remove from grid0, this should remove readers on primary node. for (int i = 0; i < GRID_CNT; i++) checkEntry(grid(i), nearKey, null, i == 0); Ignite primaryNode = G.ignite((String)aff.mapKeyToNode(nearKey).attribute(ATTR_IGNITE_INSTANCE_NAME)); IgniteCache<Integer, Integer> primaryCache = primaryNode.cache(DEFAULT_CACHE_NAME); primaryCache.put(nearKey, 2); // Put from primary, check there are no readers. checkEntry(primaryNode, nearKey, 2, false); }
Example 15
Source File: IgniteCacheDistributedJoinQueryConditionsTest.java From ignite with Apache License 2.0 | 3 votes |
/** * @return Organization ids. */ private List<Integer> putData1() { total = 0; Ignite client = grid(2); Affinity<Object> aff = client.affinity(PERSON_CACHE); IgniteCache<Object, Object> personCache = client.cache(PERSON_CACHE); IgniteCache<Object, Object> orgCache = client.cache(ORG_CACHE); AtomicInteger pKey = new AtomicInteger(); AtomicInteger orgKey = new AtomicInteger(); ClusterNode node0 = ignite(0).cluster().localNode(); ClusterNode node1 = ignite(1).cluster().localNode(); List<Integer> data = new ArrayList<>(); for (int i = 0; i < 5; i++) { int orgId = keyForNode(aff, orgKey, node0); orgCache.put(orgId, new Organization("obj-" + orgId)); for (int j = 0; j < i; j++) { personCache.put(keyForNode(aff, pKey, node1), new Person(orgId, "obj-" + orgId)); total++; } data.add(orgId); } return data; }
Example 16
Source File: GridCacheLazyQueryPartitionsReleaseTest.java From ignite with Apache License 2.0 | 2 votes |
/** * Lazy query release partitions test. * * @throws Exception If failed. */ @Test public void testLazyQueryPartitionsRelease() throws Exception { Ignite node1 = startGrid(0); IgniteCache<Integer, Person> cache = node1.cache(PERSON_CACHE); cache.clear(); Affinity<Integer> aff = node1.affinity(PERSON_CACHE); int partsFilled = fillAllPartitions(cache, aff); SqlFieldsQuery qry = new SqlFieldsQuery("select name, age from person") .setPageSize(1); FieldsQueryCursor<List<?>> qryCursor = cache.query(qry); Iterator<List<?>> it = qryCursor.iterator(); int resCntr = 0; if (it.hasNext()) { it.next(); resCntr++; } else fail("No query results."); startGrid(1); for (Ignite ig : G.allGrids()) ig.cache(PERSON_CACHE).rebalance().get(); while (it.hasNext()) { it.next(); resCntr++; } assertEquals("Wrong result set size", partsFilled, resCntr); }
Example 17
Source File: GridBinaryAffinityKeySelfTest.java From ignite with Apache License 2.0 | 2 votes |
/** * @param ignite Ignite. * @throws Exception If failed. */ private void checkAffinity(Ignite ignite) throws Exception { Affinity<Object> aff = ignite.affinity(DEFAULT_CACHE_NAME); GridAffinityProcessor affProc = ((IgniteKernal)ignite).context().affinity(); IgniteCacheObjectProcessor cacheObjProc = ((IgniteKernal)ignite).context().cacheObjects(); CacheObjectContext cacheObjCtx = cacheObjProc.contextForCache( ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class)); for (int i = 0; i < 1000; i++) { assertEquals(i, aff.affinityKey(i)); assertEquals(i, aff.affinityKey(new TestObject(i))); assertEquals(i, aff.affinityKey(ignite.binary().toBinary(new TestObject(i)))); assertEquals(i, aff.affinityKey(new AffinityKey(0, i))); BinaryObjectBuilder bldr = ignite.binary().builder("TestObject2"); bldr.setField("affKey", i); assertEquals(i, aff.affinityKey(bldr.build())); CacheObject cacheObj = cacheObjProc.toCacheObject(cacheObjCtx, new TestObject(i), true); assertEquals(i, aff.affinityKey(cacheObj)); assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(new TestObject(i))); assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(cacheObj)); assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, i)); assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, new TestObject(i))); assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, cacheObj)); assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new TestObject(i))); assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, cacheObj)); assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new AffinityKey(0, i)), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i)); } }
Example 18
Source File: IgniteDbPutGetAbstractTest.java From ignite with Apache License 2.0 | 2 votes |
/** * @param total Expected total entries. * @param cntrs Expected per-node entries count. */ private void checkLocalScan(int total, Map<UUID, Integer> cntrs) { Set<DbKey> allKeys = new HashSet<>(); for (int i = 0; i < gridCount(); i++) { Ignite ignite0 = grid(i); IgniteCache<DbKey, DbValue> cache0 = ignite0.cache("non-primitive"); int cnt = 0; ScanQuery<DbKey, DbValue> qry = new ScanQuery<>(); qry.setLocal(true); QueryCursor<Cache.Entry<DbKey, DbValue>> cur = cache0.query(qry); Map<Integer, Integer> partCntrs = new HashMap<>(); Affinity<Object> aff = ignite0.affinity(cache0.getName()); for (Cache.Entry<DbKey, DbValue> e : cur) { cnt++; allKeys.add(e.getKey()); assertEquals(e.getKey().val, e.getValue().iVal); int part = aff.partition(e.getKey()); Integer partCntr = partCntrs.get(part); if (partCntr == null) partCntr = 1; else partCntr += 1; partCntrs.put(part, partCntr); } assertEquals(cntrs.get(ignite0.cluster().localNode().id()), (Integer)cnt); checkScanPartition(ignite0, cache0, partCntrs, true); } assertEquals(total, allKeys.size()); }
Example 19
Source File: GridCachePartitionedAffinityExcludeNeighborsPerformanceTest.java From ignite with Apache License 2.0 | 2 votes |
/** * @param ignite Grid. * @return Affinity. */ static Affinity<Object> affinity(Ignite ignite) { return ignite.affinity(DEFAULT_CACHE_NAME); }
Example 20
Source File: GridCachePartitionedAffinitySelfTest.java From ignite with Apache License 2.0 | 2 votes |
/** * @param ignite Grid. * @return Affinity. */ static Affinity<Object> affinity(Ignite ignite) { return ignite.affinity(DEFAULT_CACHE_NAME); }