@Override public Object call() throws Exception { return affinity.isPrimary(n, key); } }, IgniteException.class, EXPECTED_MSG);
/** * 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)); } }
/** */ public Integer next() { int key = start; while (start < Integer.MAX_VALUE) { if (affinity.isPrimary(node, key)) { start = key + 1; return key; } key++; } throw new IllegalStateException("Can't find next key"); } }
/** {@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"); } }
/** {@inheritDoc} */ @Override public boolean isPrimary(ClusterNode n, K key) { CacheOperationContext old = gate.enter(null); try { return delegate.isPrimary(n, key); } finally { gate.leave(old); } }
/** * @param aff Affinity. * @param key Key. * @return Backup node for given key. */ private Ignite backup(Affinity<Object> aff, Object key) { for (Ignite ignite : G.allGrids()) { ClusterNode node = ignite.cluster().localNode(); if (aff.isPrimaryOrBackup(node, key) && !aff.isPrimary(node, key)) return ignite; } fail("Failed to find backup for key: " + key); return null; }
/** {@inheritDoc} */ @Override public boolean apply(CacheEvent evt) { Affinity<Object> affinity = ignite.affinity(cacheName); // Process this event. Ignored on backups. return affinity.isPrimary(ignite.cluster().localNode(), evt.key()) && (filter == null || filter.apply(evt)); } }
/** * @return Key. */ protected int generateNearKey() { Affinity<Object> aff = ignite(0).affinity(DEFAULT_CACHE_NAME); int key = 0; while (true) { boolean primary = aff.isPrimary(ignite(1).cluster().localNode(), key); boolean primaryOrBackup = aff.isPrimaryOrBackup(ignite(0).cluster().localNode(), key); if (primary && !primaryOrBackup) return key; key++; } }
/** {@inheritDoc} */ @Override public boolean apply(CacheEvent evt) { Affinity<Object> affinity = ignite.affinity(cacheName); if (affinity.isPrimary(ignite.cluster().localNode(), evt.key())) { // Process this event. Ignored on backups. if (filter != null && filter.apply(evt)) return false; return true; } return false; } }
/** * @param aff Cache affinity. * @param node Node. * @return Finds some cache key for which given node is primary. */ private Object keyForNode(Affinity<Object> aff, ClusterNode node) { assertNotNull(node); Object key = null; for (int i = 0; i < 1000; i++) { if (aff.isPrimary(node, i)) { key = i; break; } } assertNotNull(key); return key; }
/** * Gets primary key for the given cache. * * @param cache Cache. * @return Primary key. * @throws Exception If failed. */ private int primaryKey0(Ignite ignite, IgniteCache cache) throws Exception { ClusterNode locNode = ignite.cluster().localNode(); for (int i = 0; i < Integer.MAX_VALUE; i++) { if (affinity(cache).isPrimary(locNode, i)) return i; } throw new Exception("Cannot determine affinity key."); }
/** * @param gridIdx Grid index. * @param key Key. * @return {@code True} if grid is primary for given key. */ private boolean primary(int gridIdx, Object key) { Affinity<Object> aff = grid(0).affinity(CACHE_NAME); return aff.isPrimary(grid(gridIdx).cluster().localNode(), key); }
/** {@inheritDoc} */ @Override public boolean evaluate(CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) { if (affinity(ignite.cache(cacheName)).isPrimary(ignite.cluster().localNode(), e.getKey())) { QueryTestValue prevVal = prevVals.put(e.getKey(), e.getValue()); if (prevVal != null) { if (!new QueryTestValue(prevVal.val1 + 1).equals(e.getValue())) fail = true; } } return true; } }
/** * @throws Exception If failed. */ @Test public void testTxFromPrimary() 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)) { key = i; break; } } assertNotNull(key); testTxOriginatingNodeFails(Collections.singleton(key), false); }
/** * Check that keys correctly distributed by nodes after data streamer. * * @param g Grid to check. */ private void checkDistribution(Ignite g) { ClusterNode n = g.cluster().localNode(); IgniteCache<Object, Object> c = g.cache(DEFAULT_CACHE_NAME); // Check that data streamer correctly split data by nodes. for (int i = 0; i < KEYS_COUNT; i ++) { if (g.affinity(DEFAULT_CACHE_NAME).isPrimary(n, i)) assertNotNull(c.localPeek(i)); else assertNull(c.localPeek(i)); } }
/** * @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)); } }
/** * 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); }
/** * @throws Exception If failed. */ @Test public void testGetFromPrimaryNode() throws Exception { 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).isPrimary(grid(i).localNode(), KEY)) { info("Primary node: " + grid(i).localNode().id()); c.get(KEY); break; } } assert !await(); }
/** * @throws Exception If failed. */ @Test public void testRemoveAsyncValAvgTime() throws Exception { IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME); Integer key = 0; for (int i = 0; i < 1000; i++) { if (affinity(cache).isPrimary(grid(0).localNode(), i)) { key = i; break; } } assertEquals(cache.localMetrics().getAverageRemoveTime(), 0.0, 0.0); cache.put(key, key); IgniteFuture<Boolean> fut = cache.removeAsync(key, key); assertTrue(fut.get()); assert cache.localMetrics().getAverageRemoveTime() >= 0; }