@Override public Object call() throws Exception { return affinity.isBackup(n, key); } }, IgniteException.class, EXPECTED_MSG);
/** {@inheritDoc} */ @Override public boolean isBackup(ClusterNode n, K key) { CacheOperationContext old = gate.enter(null); try { return delegate.isBackup(n, key); } finally { gate.leave(old); } }
/** {@inheritDoc} */ @Override public Boolean call() throws Exception { if (primary && backup) return affinity().isPrimaryOrBackup(n, key); else if (primary) return affinity().isPrimary(n, key); else if (backup) return affinity().isBackup(n, key); else throw new IllegalStateException("primary or backup or both flags should be switched on"); } }
/** * Check isBackup, isPrimary and isPrimaryOrBackup methods. * * @param testAff Affinity1. * @param aff Affinity2. */ private void checkIsBackupOrPrimary(Affinity testAff, Affinity aff) { for (int i = 0; i < 10000; i++) for (ClusterNode n : nodes()) { assertEquals(testAff.isBackup(n, i), aff.isBackup(n, i)); assertEquals(testAff.isPrimary(n, i), aff.isPrimary(n, i)); assertEquals(testAff.isPrimaryOrBackup(n, i), aff.isPrimaryOrBackup(n, i)); } }
if (affinity.isBackup(grid(0).localNode(), key)) keys.add(key);
/** * @param gridIdx Grid index. * @param key Key. * @return {@code True} if grid is primary for given key. */ private boolean backup(int gridIdx, Object key) { Affinity<Object> aff = grid(0).affinity(CACHE_NAME); return aff.isBackup(grid(gridIdx).cluster().localNode(), key); }
/** * @param expVals Expected values in cache. */ private void assertCacheContent(Map<Integer, GridTuple<Integer>> expVals) { assert !expVals.isEmpty(); Collection<Integer> failedKeys = new HashSet<>(); for (int i = 0; i < GRID_CNT; i++) { Ignite ignite = grid(i); IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME); for (Map.Entry<Integer, GridTuple<Integer>> expVal : expVals.entrySet()) { Integer val = cache.get(expVal.getKey()); if (!F.eq(expVal.getValue().get(), val)) { failedKeys.add(expVal.getKey()); boolean primary = affinity(cache).isPrimary(ignite.cluster().localNode(), expVal.getKey()); boolean backup = affinity(cache).isBackup(ignite.cluster().localNode(), expVal.getKey()); log.error("Unexpected cache data [exp=" + expVal + ", actual=" + val + ", nodePrimary=" + primary + ", nodeBackup=" + backup + ", nodeIdx" + i + ", nodeId=" + ignite.cluster().localNode().id() + ']'); } } } assertTrue("Unexpected data for keys: " + failedKeys, failedKeys.isEmpty()); }
/** * @throws Exception If failed. */ @Test public void testTxFromBackup() throws Exception { ClusterNode txNode = grid(originatingNode()).localNode(); Integer key = null; for (int i = 0; i < Integer.MAX_VALUE; i++) { if (grid(originatingNode()).affinity(DEFAULT_CACHE_NAME).isBackup(txNode, i)) { key = i; break; } } assertNotNull(key); testTxOriginatingNodeFails(Collections.singleton(key), false); }
/** * @throws Exception If failed. */ @Test public void testTxFromNotColocated() throws Exception { ClusterNode txNode = grid(originatingNode()).localNode(); Integer key = null; for (int i = 0; i < Integer.MAX_VALUE; i++) { if (!grid(originatingNode()).affinity(DEFAULT_CACHE_NAME).isPrimary(txNode, i) && !grid(originatingNode()).affinity(DEFAULT_CACHE_NAME).isBackup(txNode, i)) { key = i; break; } } assertNotNull(key); testTxOriginatingNodeFails(Collections.singleton(key), false); }
/** * @param key Key to check. * @param val Expected value. */ private void checkValues(int key, int val) { for (int i = 0; i < 3; i++) { IgniteEx grid = grid(i); ClusterNode node = grid.localNode(); IgniteCache<Object, Object> cache = grid.cache(DEFAULT_CACHE_NAME); boolean primary = grid.affinity(DEFAULT_CACHE_NAME).isPrimary(node, key); boolean backup = grid.affinity(DEFAULT_CACHE_NAME).isBackup(node, key); if (primary || backup) assertEquals("Invalid cache value [nodeId=" + node.id() + ", primary=" + primary + ", backup=" + backup + ", key=" + key + ']', val, cache.localPeek(key, CachePeekMode.ONHEAP)); } }
/** * @throws Exception If failed. */ @Test public void testBackupLoad() throws Exception { grid(0).cache(DEFAULT_CACHE_NAME).put(1, 1); assert store.get(1) == 1; for (int i = 0; i < GRID_CNT; i++) { IgniteCache<Integer, Integer> cache = jcache(i); if (grid(i).affinity(DEFAULT_CACHE_NAME).isBackup(grid(i).localNode(), 1)) { assert cache.localPeek(1) == 1; jcache(i).localClear(1); assert cache.localPeek(1) == null; // Store is called in putx method, so we reset counter here. cnt.set(0); assert cache.get(1) == 1; assert cnt.get() == 0; } } }
/** * Evaluate primary and backup keys. * * @param primaryKeyStart Value from need to start calculate primary key. * @param backupKeyStart Value from need to start calculate backup key. * @return Pair of result. The first result is found primary key. The second is found backup key. */ private IgnitePair<Integer> evaluatePrimaryAndBackupKeys(final int primaryKeyStart, final int backupKeyStart) { int primaryKey = primaryKeyStart; int backupKey = backupKeyStart; while (!client().affinity(CACHE_NAME).isPrimary(((IgniteKernal)primary()).localNode(), primaryKey)) primaryKey++; while (!client().affinity(CACHE_NAME).isBackup(((IgniteKernal)primary()).localNode(), backupKey) && backupKey < 100 + backupKeyStart) backupKey++; return new IgnitePair<>(primaryKey, backupKey); }
/** * @param aff Affinity. * @param key Key. */ private void printValues(Affinity<Integer> aff, int key) { for (int g = 0; g < gridCount(); g++) { Ignite ignite = grid(g); boolean primary = aff.isPrimary(ignite.cluster().localNode(), key); boolean backup = aff.isBackup(ignite.cluster().localNode(), key); Object val = ignite.cache(DEFAULT_CACHE_NAME).localPeek(key); log.error("Node value [key=" + key + ", val=" + val + ", node=" + g + ", primary=" + primary + ", backup=" + backup + ']'); } } }
/** * @param keys Keys. * @throws Exception If failed. */ private void checkNoValue(Collection<Integer> keys) throws Exception { IgniteCache<Integer, Object> cache = jcache(0); for (int i = 0; i < gridCount(); i++) { ClusterNode node = grid(i).cluster().localNode(); for (Integer key : keys) { Object val = jcache(i).localPeek(key); if (val != null) { log.info("Unexpected value [grid=" + i + ", primary=" + affinity(cache).isPrimary(node, key) + ", backup=" + affinity(cache).isBackup(node, key) + ']'); } assertNull("Unexpected non-null value for grid " + i, val); } } storeMap.clear(); for (int i = 0; i < gridCount(); i++) { for (Integer key : keys) assertNull("Unexpected non-null value for grid " + i, jcache(i).get(key)); } }
/** * @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; } }
/** * @throws Exception If failed. */ @Test public void testGetFromBackupNode() throws Exception { Assume.assumeFalse("https://issues.apache.org/jira/browse/IGNITE-10274", MvccFeatureChecker.forcedMvcc()); MvccFeatureChecker.failIfNotSupported(MvccFeatureChecker.Feature.EVICTION); for (int i = 0; i < GRID_CNT; i++) { IgniteCache<String, Integer> c = grid(i).cache(DEFAULT_CACHE_NAME); if (grid(i).affinity(DEFAULT_CACHE_NAME).isBackup(grid(i).localNode(), KEY)) { info("Backup node: " + grid(i).localNode().id()); Integer val = c.get(KEY); assert val != null && val == 1; assert !await(); c.localEvict(Collections.singleton(KEY)); assert c.localPeek(KEY, CachePeekMode.ONHEAP) == null; val = c.get(KEY); assert val != null && val == 1; assert !await(); break; } } }
/** * @throws Exception If failed. */ @Test public void testPeekAsyncPartitionedModes() throws Exception { jcache().put("key", 1); for (int i = 0; i < gridCount(); i++) { boolean nearEnabled = nearEnabled(jcache(i)); Integer nearPeekVal = nearEnabled ? 1 : null; IgniteCache<String, Integer> c = jcache(i); if (grid(i).affinity(DEFAULT_CACHE_NAME).isBackup(grid(i).localNode(), "key")) { assert c.localPeek("key", NEAR) == null; assert c.localPeek("key", PRIMARY, BACKUP) == 1; } else if (!grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), "key")) { // Initialize near reader. assertEquals((Integer)1, jcache(i).get("key")); assertEquals(nearPeekVal, c.localPeek("key", NEAR)); assert c.localPeek("key", PRIMARY, BACKUP) == null; } } }
/** * @throws Exception If failed. */ @Test public void testPeekPartitionedModes() throws Exception { jcache().put("key", 1); for (int i = 0; i < gridCount(); i++) { IgniteCache<String, Integer> c = jcache(i); assertEquals((Integer)1, c.get("key")); boolean nearEnabled = nearEnabled(c); if (nearEnabled) executeOnLocalOrRemoteJvm(i, new IsNearTask()); Integer nearPeekVal = nearEnabled ? 1 : null; Affinity<Object> aff = ignite(i).affinity(DEFAULT_CACHE_NAME); info("Affinity nodes [nodes=" + F.nodeIds(aff.mapKeyToPrimaryAndBackups("key")) + ", locNode=" + ignite(i).cluster().localNode().id() + ']'); if (aff.isBackup(grid(i).localNode(), "key")) { assertNull(c.localPeek("key", NEAR)); assertEquals((Integer)1, c.localPeek("key", BACKUP)); } else if (!aff.isPrimaryOrBackup(grid(i).localNode(), "key")) { // Initialize near reader. assertEquals((Integer)1, c.get("key")); assertEquals("Failed to validate near value for node: " + i, nearPeekVal, c.localPeek("key", NEAR)); assertNull(c.localPeek("key", PRIMARY, BACKUP)); } } }