use of org.apache.ignite.internal.processors.datastructures.GridCacheQueueHeaderKey in project ignite by apache.
the class GridCacheQueryInternalKeysSelfTest method testInternalKeysPreloading.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
@Test
public void testInternalKeysPreloading() throws Exception {
try {
IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < ENTRY_CNT; i++) cache.put(new GridCacheQueueHeaderKey("queue" + i), 1);
// Start additional node.
startGrid(GRID_CNT);
for (int i = 0; i < ENTRY_CNT; i++) {
GridCacheQueueHeaderKey internalKey = new GridCacheQueueHeaderKey("queue" + i);
Collection<ClusterNode> nodes = grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(internalKey);
for (ClusterNode n : nodes) {
Ignite g = findGridForNodeId(n.id());
assertNotNull(g);
assertTrue("Affinity node doesn't contain internal key [key=" + internalKey + ", node=" + n + ']', ((GridNearCacheAdapter) ((IgniteKernal) g).internalCache(DEFAULT_CACHE_NAME)).dht().containsKey(internalKey));
}
}
} finally {
stopGrid(GRID_CNT);
}
}
use of org.apache.ignite.internal.processors.datastructures.GridCacheQueueHeaderKey in project ignite by apache.
the class GridCacheQueueCleanupSelfTest method testCleanup.
/**
* @throws Exception If failed.
*/
@Test
public void testCleanup() throws Exception {
IgniteQueue<Integer> queue = grid(0).queue(QUEUE_NAME1, 0, config(false));
GridCacheContext cctx = GridTestUtils.getFieldValue(queue, "cctx");
final String queueCacheName = cctx.name();
ClusterNode node = grid(0).affinity(queueCacheName).mapKeyToNode(new GridCacheQueueHeaderKey(QUEUE_NAME1));
final Ignite ignite = grid(0).localNode().equals(node) ? grid(1) : grid(0);
/*
assertNotNull(queue);
// Add/poll some items.
for (int i = 0; i < 500; i++)
queue.add(i);
for (int i = 0; i < 10; i++)
queue.poll();
assertTrue(!queue.isEmpty());
// Kill node containing queue header.
final String killGridName = node.attribute(IgniteNodeAttributes.ATTR_IGNITE_INSTANCE_NAME);
stopGrid(killGridName);
assertNull(((IgniteKernal)grid).cache(DEFAULT_CACHE_NAME).dataStructures().queue(QUEUE_NAME1, 0, false, false));
final AtomicBoolean stop = new AtomicBoolean(false);
GridFuture<?> fut1;
GridFuture<?> fut2;
try {
// Start threads using cache concurrently with cleanup thread.
fut1 = startAddPollThread(grid, stop, QUEUE_NAME1);
fut2 = startAddPollThread(grid, stop, QUEUE_NAME2);
U.sleep(3000); // Give some time for cleanup thread.
}
finally {
stop.set(true);
}
fut1.get();
fut2.get();
((IgniteKernal)grid).cache(DEFAULT_CACHE_NAME).dataStructures().removeQueue(QUEUE_NAME1);
((IgniteKernal)grid).cache(DEFAULT_CACHE_NAME).dataStructures().removeQueue(QUEUE_NAME2);
assertTrue(GridTestUtils.waitForCondition(new PAX() {
@Override public boolean applyx() {
for (int i = 0; i < gridCount(); i++) {
if (getTestIgniteInstanceName(i).equals(killGridName))
continue;
Iterator<GridCacheEntryEx<Object, Object>> entries =
((GridKernal)grid(i)).context().cache().internalCache(DEFAULT_CACHE_NAME).map().allEntries0().iterator();
if (entries.hasNext()) {
log.info("Found cache entries, will wait: " + entries.next());
return false;
}
}
return true;
}
}, 5000));
startGrid(killGridName);
// Create queue again.
queue = ((IgniteKernal)grid).cache(DEFAULT_CACHE_NAME).dataStructures().queue(QUEUE_NAME1, 0, false, true);
*/
assertEquals(0, queue.size());
for (int i = 0; i < 500; i++) queue.add(i);
assertEquals(500, queue.size());
// Remove queue and create queue with the same name.
queue.close();
queue = ignite.queue(QUEUE_NAME1, 0, config(false));
assertEquals(0, queue.size());
for (int i = 0; i < 500; i++) queue.add(i);
assertEquals(500, queue.size());
// Check that items of removed queue are removed, items of new queue not.
assertTrue(GridTestUtils.waitForCondition(new PAX() {
@Override
public boolean applyx() throws IgniteCheckedException {
int cnt = 0;
for (int i = 0; i < gridCount(); i++) {
GridCacheAdapter<Object, Object> cache = grid(i).context().cache().internalCache(queueCacheName);
for (Object e : cache.localEntries(new CachePeekMode[] { CachePeekMode.ALL })) cnt++;
}
if (cnt > 501) {
// 500 items + header.
log.info("Found more cache entries than expected, will wait: " + cnt);
return false;
}
return true;
}
}, 5000));
for (int i = 0; i < 500; i++) assertEquals((Integer) i, queue.poll());
}
use of org.apache.ignite.internal.processors.datastructures.GridCacheQueueHeaderKey in project ignite by apache.
the class CacheDataStructuresManager method queue0.
/**
* @param name Queue name.
* @param cap Capacity.
* @param colloc Collocated flag.
* @param create Create flag.
* @return Queue header.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
@Nullable
public <T> GridCacheQueueProxy<T> queue0(final String name, final int cap, boolean colloc, final boolean create) throws IgniteCheckedException {
cctx.gate().enter();
try {
GridCacheQueueHeaderKey key = new GridCacheQueueHeaderKey(name);
GridCacheQueueHeader hdr;
if (create) {
hdr = new GridCacheQueueHeader(IgniteUuid.randomUuid(), cap, colloc, 0, 0, null);
GridCacheQueueHeader old = queueHdrView.withNoRetries().getAndPutIfAbsent(key, hdr);
if (old != null) {
if (old.capacity() != cap || old.collocated() != colloc)
throw new IgniteCheckedException("Failed to create queue, queue with the same name but " + "different configuration already exists [name=" + name + ']');
hdr = old;
}
} else
hdr = queueHdrView.get(key);
if (hdr == null)
return null;
if (queueQryGuard.compareAndSet(false, true)) {
queueQryId = cctx.continuousQueries().executeInternalQuery(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
if (!busyLock.enterBusy())
return;
try {
for (CacheEntryEvent<?, ?> e : evts) {
GridCacheQueueHeaderKey key = (GridCacheQueueHeaderKey) e.getKey();
GridCacheQueueHeader hdr = (GridCacheQueueHeader) e.getValue();
for (final GridCacheQueueProxy queue : queuesMap.values()) {
if (queue.name().equals(key.queueName())) {
if (e.getEventType() == REMOVED) {
GridCacheQueueHeader oldHdr = (GridCacheQueueHeader) e.getOldValue();
assert oldHdr != null;
if (oldHdr.id().equals(queue.delegate().id())) {
queue.delegate().onRemoved(false);
queuesMap.remove(queue.delegate().id());
}
} else
queue.delegate().onHeaderChanged(hdr);
}
}
}
} finally {
busyLock.leaveBusy();
}
}
}, new QueueHeaderPredicate(), cctx.isLocal() || (cctx.isReplicated() && cctx.affinityNode()), true, false, false);
}
GridCacheQueueProxy queue = queuesMap.get(hdr.id());
if (queue == null) {
queue = new GridCacheQueueProxy(cctx, cctx.atomic() ? new GridAtomicCacheQueueImpl<>(name, hdr, cctx) : new GridTransactionalCacheQueueImpl<>(name, hdr, cctx));
GridCacheQueueProxy old = queuesMap.putIfAbsent(hdr.id(), queue);
if (old != null)
queue = old;
}
return queue;
} finally {
cctx.gate().leave();
}
}
use of org.apache.ignite.internal.processors.datastructures.GridCacheQueueHeaderKey in project ignite by apache.
the class GridCacheAbstractQueueFailoverDataConsistencySelfTest method primaryQueueNode.
/**
* @param queue Queue.
* @return Primary node for queue's header.
* @throws Exception If failed.
*/
private int primaryQueueNode(IgniteQueue queue) throws Exception {
GridCacheContext cctx = GridTestUtils.getFieldValue(queue, "cctx");
GridCacheAffinityManager aff = cctx.affinity();
CachePeekMode[] modes = new CachePeekMode[] { CachePeekMode.ALL };
for (int i = 0; i < gridCount(); i++) {
for (Cache.Entry e : grid(i).context().cache().internalCache(cctx.name()).localEntries(modes)) {
Object key = e.getKey();
if (aff.primaryByKey(grid(i).localNode(), key, AffinityTopologyVersion.NONE) && key instanceof GridCacheQueueHeaderKey)
return i;
}
}
fail("Failed to find primary node for queue header.");
return -1;
}
Aggregations