use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class GridCacheQueryTransformerSelfTest method testLocalInjection.
/**
* @throws Exception If failed.
*/
@Test
public void testLocalInjection() throws Exception {
IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
try {
for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
Collection<List<Boolean>> lists = grid().compute().broadcast(new IgniteCallable<List<Boolean>>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public List<Boolean> call() throws Exception {
IgniteClosure<Cache.Entry<Integer, Value>, Boolean> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Boolean>() {
@IgniteInstanceResource
Ignite ignite;
@Override
public Boolean apply(Cache.Entry<Integer, Value> e) {
return ignite != null;
}
};
return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
}
});
List<Boolean> res = new ArrayList<>(F.flatCollections(lists));
assertEquals(50, res.size());
for (int i = 0; i < 50; i++) assertEquals(Boolean.TRUE, res.get(i));
} finally {
cache.destroy();
}
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class GridCacheQueryTransformerSelfTest method testLocal.
/**
* @throws Exception If failed.
*/
@Test
public void testLocal() throws Exception {
IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
try {
for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public List<Integer> call() throws Exception {
IgniteClosure<Cache.Entry<Integer, Value>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Integer>() {
@Override
public Integer apply(Cache.Entry<Integer, Value> e) {
return e.getValue().idx;
}
};
return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
}
});
List<Integer> res = new ArrayList<>(F.flatCollections(lists));
assertEquals(50, res.size());
Collections.sort(res);
for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
} finally {
cache.destroy();
}
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class GridCacheQueryTransformerSelfTest method testLocalKeepBinaryFiltered.
/**
* @throws Exception If failed.
*/
@Test
public void testLocalKeepBinaryFiltered() throws Exception {
IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
try {
for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public List<Integer> call() throws Exception {
IgniteBiPredicate<Integer, BinaryObject> filter = new IgniteBiPredicate<Integer, BinaryObject>() {
@Override
public boolean apply(Integer k, BinaryObject v) {
return v.<Integer>field("idx") % 1000 == 0;
}
};
IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {
@Override
public Integer apply(Cache.Entry<Integer, BinaryObject> e) {
return e.getValue().field("idx");
}
};
return ignite.cache("test-cache").withKeepBinary().query(new ScanQuery<>(filter).setLocal(true), transformer).getAll();
}
});
List<Integer> res = new ArrayList<>(F.flatCollections(lists));
assertEquals(5, res.size());
Collections.sort(res);
for (int i = 0; i < 5; i++) assertEquals(i * 1000, res.get(i).intValue());
} finally {
cache.destroy();
}
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class GridCacheQueryTransformerSelfTest method testLocalKeepBinary.
/**
* @throws Exception If failed.
*/
@Test
public void testLocalKeepBinary() throws Exception {
IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
try {
for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public List<Integer> call() throws Exception {
IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {
@Override
public Integer apply(Cache.Entry<Integer, BinaryObject> e) {
return e.getValue().field("idx");
}
};
return ignite.cache("test-cache").withKeepBinary().query(new ScanQuery<Integer, BinaryObject>().setLocal(true), transformer).getAll();
}
});
List<Integer> res = new ArrayList<>(F.flatCollections(lists));
assertEquals(50, res.size());
Collections.sort(res);
for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
} finally {
cache.destroy();
}
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class BaselineAutoAdjustTest method testExchangeMerge.
/**
* Tests that merging exchanges properly triggers baseline changing.
*
* @throws Exception If failed.
*/
@Test
public void testExchangeMerge() throws Exception {
// Latch that waits for PME (intTopVer == 3.0)
CountDownLatch exchangeWorkerLatch = new CountDownLatch(1);
// Lyficycle bean is needed in order to register EVT_NODE_JOIN lister that is called
// right after GridCachePartitionExchangeManager and before GridClusterStateProcessor.
lifecycleBean = new LifecycleBean() {
/**
* Ignite instance.
*/
@IgniteInstanceResource
IgniteEx ignite;
/**
* {@inheritDoc}
*/
@Override
public void onLifecycleEvent(LifecycleEventType evt) throws IgniteException {
if (evt == LifecycleEventType.BEFORE_NODE_START) {
ignite.context().internalSubscriptionProcessor().registerDistributedMetastorageListener(new DistributedMetastorageLifecycleListener() {
@Override
public void onReadyForRead(ReadableDistributedMetaStorage metastorage) {
ignite.context().event().addDiscoveryEventListener((evt, disco) -> {
if (evt.type() == EVT_NODE_JOINED && evt.topologyVersion() == 3) {
try {
// Let's wait for exchange worker starts PME
// that related to the first node joined the cluster.
exchangeWorkerLatch.await(getTestTimeout(), MILLISECONDS);
} catch (InterruptedException e) {
throw new IgniteException("exchangeWorkerLatch has been interrupted.", e);
}
}
}, EVT_NODE_JOINED);
}
});
}
}
};
// Start the coordinator node.
IgniteEx crd = startGrid(0);
// This bean is only required on the coordinator node.
lifecycleBean = null;
// Latch indicates that EVT_NODE_JOINED (topVer == 4.0) was processed by all listeners.
CountDownLatch nodeJoinLatch = new CountDownLatch(1);
// This listener is the last one in the queue of handlers.
crd.context().event().addDiscoveryEventListener((evt, disco) -> {
if (evt.type() == EVT_NODE_JOINED && evt.topologyVersion() == 4)
nodeJoinLatch.countDown();
}, EVT_NODE_JOINED);
IgniteEx nonCrd = startGrid(1);
crd.cluster().state(ACTIVE);
crd.cluster().baselineAutoAdjustEnabled(true);
crd.cluster().baselineAutoAdjustTimeout(autoAdjustTimeout);
awaitPartitionMapExchange(false, true, null);
TestRecordingCommunicationSpi spi1 = TestRecordingCommunicationSpi.spi(nonCrd);
spi1.blockMessages((node, msg) -> msg instanceof GridDhtPartitionsSingleMessage);
// Target major topology version (4 nodes)
long targetTopVer = 4;
// Let's block exchange process in order to merge two following exchanges (3.0 && 4.0).
crd.context().cache().context().exchange().mergeExchangesTestWaitVersion(new AffinityTopologyVersion(targetTopVer, 0), null);
AtomicInteger cnt = new AtomicInteger(G.allGrids().size());
runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
startGrid(cnt.getAndIncrement());
return null;
}
}, 2, "async-grid-starter");
// Make sure that PME is in progress.
assertTrue("Failed to wait for PME [topVer=3]", spi1.waitForBlocked(1, getTestTimeout()));
assertTrue("Failed to wait for the first started exchange.", waitForCondition(() -> {
GridDhtPartitionsExchangeFuture fut = crd.context().cache().context().exchange().lastTopologyFuture();
return fut.initialVersion().topologyVersion() == 3;
}, getTestTimeout()));
// This guarantees that BaselineAutoAdjustData listens to real GridDhtPartitionsExchangeFuture
// instead of readyAffinityFuture.
exchangeWorkerLatch.countDown();
assertTrue("Failed to wait for processing node join event [topVer=3]", nodeJoinLatch.await(getTestTimeout(), MILLISECONDS));
// Unblock PME
spi1.stopBlock();
assertTrue("Failed to wait for changing baseline in " + autoAdjustTimeout * 2 + " ms.", waitForCondition(() -> crd.cluster().currentBaselineTopology().size() == targetTopVer, autoAdjustTimeout * 2));
}
Aggregations