@Override public boolean apply() { return aff.primaryPartitions(node).length > 0; } }, 5000));
@Override public Object call() throws Exception { return affinity.primaryPartitions(n); } }, IgniteException.class, EXPECTED_MSG);
/** {@inheritDoc} */ @Override public int[] primaryPartitions(ClusterNode n) { CacheOperationContext old = gate.enter(null); try { return delegate.primaryPartitions(n); } finally { gate.leave(old); } }
/** * @param cacheName Cache name. */ private int mapped(String cacheName) { int mapped = 0; ClusterGroup srvs = ignite.cluster().forServers(); Collection<ClusterNode> nodes = srvs.forDataNodes(cacheName).nodes(); for (ClusterNode node : nodes) mapped += ignite.affinity(cacheName).primaryPartitions(node).length; return mapped; }
/** * @return true if the given node is primary for all given partitions. */ private boolean shouldExecuteLocalQuery(Ignite node, int... parts) { if (parts == null || parts.length == 0) return false; int numOfPrimaryParts = 0; for (int nodePrimaryPart : node.affinity(DEFAULT_CACHE_NAME).primaryPartitions(node.cluster().localNode())) { for (int part : parts) { if (part == nodePrimaryPart) numOfPrimaryParts++; } } return numOfPrimaryParts == parts.length; }
@Override public boolean apply(final Ignite ignite) { Collection<ClusterNode> srvs = ignite.cluster().forServers().nodes(); if (srvs.size() < serverCount()) return false; for (ClusterNode node : srvs) { int[] primaries = ignite.affinity(CACHE_NAME).primaryPartitions(node); if (primaries == null || primaries.length == 0) return false; } return true; } };
/** {@inheritDoc} */ @Override public int[] call() throws Exception { if (primary && backup) return affinity().allPartitions(n); else if (primary) return affinity().primaryPartitions(n); else if (backup) return affinity().backupPartitions(n); else throw new IllegalStateException("primary or backup or both flags should be switched on"); } }
/** * Check allPartitions, backupPartitions and primaryPartitions methods. * * @param testAff Affinity1. * @param aff Affinity2. */ private void checkPartitions(Affinity testAff, Affinity aff) { for (ClusterNode n : nodes()) { checkEqualIntArray(testAff.allPartitions(n), aff.allPartitions(n)); checkEqualIntArray(testAff.backupPartitions(n), aff.backupPartitions(n)); checkEqualIntArray(testAff.primaryPartitions(n), aff.primaryPartitions(n)); } }
/** {@inheritDoc} */ @Override public ClientResponse process(ClientConnectionContext ctx) { IgniteCache cache = cache(ctx); GridDiscoveryManager discovery = ctx.kernalContext().discovery(); Collection<ClusterNode> nodes = discovery.discoCache().cacheNodes(cache.getName()); Affinity aff = ctx.kernalContext().affinity().affinityProxy(cache.getName()); ArrayList<ClientConnectableNodePartitions> res = new ArrayList<>(); for (ClusterNode node : nodes) { Integer port = node.attribute(ClientListenerProcessor.CLIENT_LISTENER_PORT); if (port == null) continue; Collection<String> addrs = node.addresses(); int[] parts = aff.primaryPartitions(node); res.add(new ClientConnectableNodePartitions(port, addrs, parts)); } return new ClientCacheNodePartitionsResponse(requestId(), res); } }
int[] primaryPartitions = node.affinity(CACHE_NAME).primaryPartitions(node.cluster().localNode());
int[] parts = node != null ? aff.primaryPartitions(node) : U.EMPTY_INTS;
/** * @throws Exception If failed. */ private void runQuery() throws Exception { ScanQuery<String, String> scanQuery = new ScanQuery<String, String>() .setLocal(true) .setFilter(new IgniteBiPredicate<String, String>() { @Override public boolean apply(String key, String p) { return key != null && key.isEmpty(); } }); Ignite ignite = ignite(ThreadLocalRandom.current().nextInt(GRID_CNT)); IgniteCache<String, String> example = ignite.cache(CACHE_NAME); for (int partition : ignite.affinity(CACHE_NAME).primaryPartitions(ignite.cluster().localNode())) { scanQuery.setPartition(partition); try (QueryCursor cursor = example.query(scanQuery)) { for (Object p : cursor) { String value = (String) ((Cache.Entry)p).getValue(); assertNotNull(value); } } } }
/** * JUnit. * * @throws Exception If failed. */ @Test public void testPrimaryPartitions() throws Exception { // Pick 2 nodes and create a projection over them. ClusterNode n0 = grid(0).localNode(); int[] parts = grid(0).affinity(DEFAULT_CACHE_NAME).primaryPartitions(n0); info("Primary partitions count: " + parts.length); assert parts.length > 1 : "Invalid partitions: " + Arrays.toString(parts); for (int part : parts) assert part >= 0; assert !F.isEmpty(parts); AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1); List<List<ClusterNode>> assignment = affinity().assignPartitions(ctx); for (int p : parts) { Collection<ClusterNode> owners = nodes(assignment, p); assert !F.isEmpty(owners); ClusterNode primary = F.first(owners); assert F.eqNodes(n0, primary); } }
/** * JUnit. * * @throws Exception If failed. */ @Test public void testPrimaryPartitionsOneNode() throws Exception { AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1); List<List<ClusterNode>> assignment = affinity().assignPartitions(ctx); for (ClusterNode node : grid(0).cluster().nodes()) { int[] parts = grid(0).affinity(DEFAULT_CACHE_NAME).primaryPartitions(node); assert !F.isEmpty(parts); for (int p : parts) { Collection<ClusterNode> owners = nodes(assignment, p); assert !F.isEmpty(owners); ClusterNode primary = F.first(owners); assert F.eqNodes(node, primary); } } }
/** Tests local query over partitions. */ @Test public void testLocalQuery() { Affinity<Object> affinity = grid(0).affinity("cl"); int[] parts = affinity.primaryPartitions(grid(0).localNode()); Arrays.sort(parts); IgniteCache<ClientKey, Client> cl = grid(0).cache("cl"); SqlQuery<ClientKey, Client> qry1 = new SqlQuery<>(Client.class, "1=1"); qry1.setLocal(true); qry1.setPartitions(parts[0]); List<Cache.Entry<ClientKey, Client>> clients = cl.query(qry1).getAll(); for (Cache.Entry<ClientKey, Client> client : clients) assertEquals("Incorrect partition", parts[0], affinity.partition(client.getKey())); SqlFieldsQuery qry2 = new SqlFieldsQuery("select cl._KEY, cl._VAL from \"cl\".Client cl"); qry2.setLocal(true); qry2.setPartitions(parts[0]); List<List<?>> rows = cl.query(qry2).getAll(); for (List<?> row : rows) assertEquals("Incorrect partition", parts[0], affinity.partition(row.get(0))); } }
assertEquals(0, aff.primaryPartitions(((IgniteKernal)node).localNode()).length); assertEquals(0, aff.backupPartitions(((IgniteKernal)node).localNode()).length);
assertTrue(ignite0.affinity(cacheName).primaryPartitions(node).length > 0); else assertEquals(0, ignite0.affinity(cacheName).primaryPartitions(node).length);
/** {@inheritDoc} */ @Override public int[] primaryPartitions(ClusterNode n) { CacheOperationContext old = gate.enter(null); try { return delegate.primaryPartitions(n); } finally { gate.leave(old); } }
/** * @param cacheName Cache name. */ private int mapped(String cacheName) { int mapped = 0; ClusterGroup srvs = ignite.cluster().forServers(); Collection<ClusterNode> nodes = srvs.forDataNodes(cacheName).nodes(); for (ClusterNode node : nodes) mapped += ignite.affinity(cacheName).primaryPartitions(node).length; return mapped; }
/** {@inheritDoc} */ @Override public ClientResponse process(ClientConnectionContext ctx) { authorize(ctx, SecurityPermission.CACHE_READ); IgniteCache cache = cache(ctx); GridDiscoveryManager discovery = ctx.kernalContext().discovery(); Collection<ClusterNode> nodes = discovery.cacheNodes(cache.getName(), new AffinityTopologyVersion(discovery.topologyVersion())); Affinity aff = ctx.kernalContext().affinity().affinityProxy(cache.getName()); ArrayList<ClientConnectableNodePartitions> res = new ArrayList<>(); for (ClusterNode node : nodes) { Integer port = node.attribute(ClientListenerProcessor.CLIENT_LISTENER_PORT); if (port == null) continue; Collection<String> addrs = node.addresses(); int[] parts = aff.primaryPartitions(node); res.add(new ClientConnectableNodePartitions(port, addrs, parts)); } return new ClientCacheNodePartitionsResponse(requestId(), res); } }