use of javax.cache.processor.EntryProcessorResult in project ignite by apache.
the class IgniteCacheEntryProcessorNodeJoinTest method checkIncrement.
/**
* @param cacheName Cache name.
* @param invokeAll If {@code true} tests invokeAll operation.
* @param fut If not null then executes updates while future is not done.
* @param latch Latch to count down when first update is done.
* @throws Exception If failed.
* @return Number of increments.
*/
private int checkIncrement(String cacheName, boolean invokeAll, @Nullable IgniteInternalFuture<?> fut, @Nullable CountDownLatch latch) throws Exception {
int increments = 0;
for (int k = 0; k < INCREMENTS || (fut != null && !fut.isDone()); k++) {
increments++;
if (invokeAll) {
IgniteCache<String, Set<String>> cache = ignite(0).cache(cacheName);
Map<String, Processor> procs = new LinkedHashMap<>();
for (int i = 0; i < KEYS; i++) {
String key = "set-" + i;
String val = "value-" + k;
procs.put(key, new Processor(val));
}
Map<String, EntryProcessorResult<Integer>> resMap = cache.invokeAll(procs);
for (String key : procs.keySet()) {
EntryProcessorResult<Integer> res = resMap.get(key);
assertNotNull(res);
assertEquals(k + 1, (Object) res.get());
}
} else {
IgniteCache<String, Set<String>> cache = ignite(0).cache(cacheName);
for (int i = 0; i < KEYS; i++) {
String key = "set-" + i;
String val = "value-" + k;
Integer valsCnt = cache.invoke(key, new Processor(val));
Integer exp = k + 1;
if (!exp.equals(valsCnt))
log.info("Unexpected return value [valsCnt=" + valsCnt + ", exp=" + exp + ", cacheVal=" + cache.get(key) + ']');
assertEquals(exp, valsCnt);
}
}
if (latch != null && k == 0)
latch.countDown();
}
return increments;
}
use of javax.cache.processor.EntryProcessorResult in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method checkInvokeAll.
/**
* @param concurrency Transaction concurrency.
* @param isolation Transaction isolation.
* @throws Exception If failed.
*/
private void checkInvokeAll(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
// TODO IGNITE-2664: enable tests for all modes when IGNITE-2664 will be fixed.
if (dataMode != DataMode.EXTERNALIZABLE && gridCount() > 1)
return;
final Object key1 = key(1);
final Object key2 = key(2);
final Object key3 = key(3);
final Object val1 = value(1);
final Object val2 = value(2);
final Object val3 = value(3);
final Object val4 = value(4);
final IgniteCache<Object, Object> cache = jcache();
cache.put(key2, val1);
cache.put(key3, val3);
if (txShouldBeUsed()) {
Map<Object, EntryProcessorResult<Object>> res;
try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
res = cache.invokeAll(F.asSet(key1, key2, key3), INCR_PROCESSOR, dataMode);
tx.commit();
}
assertEquals(val1, cache.get(key1));
assertEquals(val2, cache.get(key2));
assertEquals(val4, cache.get(key3));
assertNull(res.get(key1));
assertEquals(val1, res.get(key2).get());
assertEquals(val3, res.get(key3).get());
assertEquals(2, res.size());
cache.remove(key1);
cache.put(key2, val1);
cache.put(key3, val3);
}
Map<Object, EntryProcessorResult<Object>> res = cache.invokeAll(F.asSet(key1, key2, key3), RMV_PROCESSOR);
for (int i = 0; i < gridCount(); i++) {
assertNull(jcache(i).localPeek(key1, ONHEAP));
assertNull(jcache(i).localPeek(key2, ONHEAP));
assertNull(jcache(i).localPeek(key3, ONHEAP));
}
assertNull(res.get(key1));
assertEquals(val1, res.get(key2).get());
assertEquals(val3, res.get(key3).get());
assertEquals(2, res.size());
cache.remove(key1);
cache.put(key2, val1);
cache.put(key3, val3);
res = cache.invokeAll(F.asSet(key1, key2, key3), INCR_PROCESSOR, dataMode);
assertEquals(val1, cache.get(key1));
assertEquals(val2, cache.get(key2));
assertEquals(val4, cache.get(key3));
assertNull(res.get(key1));
assertEquals(val1, res.get(key2).get());
assertEquals(val3, res.get(key3).get());
assertEquals(2, res.size());
cache.remove(key1);
cache.put(key2, val1);
cache.put(key3, val3);
res = cache.invokeAll(F.asMap(key1, INCR_PROCESSOR, key2, INCR_PROCESSOR, key3, INCR_PROCESSOR), dataMode);
assertEquals(val1, cache.get(key1));
assertEquals(val2, cache.get(key2));
assertEquals(val4, cache.get(key3));
assertNull(res.get(key1));
assertEquals(val1, res.get(key2).get());
assertEquals(val3, res.get(key3).get());
assertEquals(2, res.size());
}
use of javax.cache.processor.EntryProcessorResult in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method checkInvokeAllAsync.
/**
* @param concurrency Transaction concurrency.
* @param isolation Transaction isolation.
* @throws Exception If failed.
*/
private void checkInvokeAllAsync(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
// TODO IGNITE-2664: enable tests for all modes when IGNITE-2664 will be fixed.
if (dataMode != DataMode.EXTERNALIZABLE && gridCount() > 1)
return;
final Object key1 = key(1);
final Object key2 = key(2);
final Object key3 = key(3);
final Object val1 = value(1);
final Object val2 = value(2);
final Object val3 = value(3);
final Object val4 = value(4);
final IgniteCache<Object, Object> cache = jcache();
cache.put(key2, val1);
cache.put(key3, val3);
if (txShouldBeUsed()) {
Map<Object, EntryProcessorResult<Object>> res;
try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
res = cache.invokeAllAsync(F.asSet(key1, key2, key3), INCR_PROCESSOR, dataMode).get();
tx.commit();
}
assertEquals(val1, cache.get(key1));
assertEquals(val2, cache.get(key2));
assertEquals(val4, cache.get(key3));
assertNull(res.get(key1));
assertEquals(val1, res.get(key2).get());
assertEquals(val3, res.get(key3).get());
assertEquals(2, res.size());
cache.remove(key1);
cache.put(key2, val1);
cache.put(key3, val3);
}
Map<Object, EntryProcessorResult<Object>> res = cache.invokeAllAsync(F.asSet(key1, key2, key3), RMV_PROCESSOR).get();
for (int i = 0; i < gridCount(); i++) {
assertNull(jcache(i).localPeek(key1, ONHEAP));
assertNull(jcache(i).localPeek(key2, ONHEAP));
assertNull(jcache(i).localPeek(key3, ONHEAP));
}
assertNull(res.get(key1));
assertEquals(val1, res.get(key2).get());
assertEquals(val3, res.get(key3).get());
assertEquals(2, res.size());
cache.remove(key1);
cache.put(key2, val1);
cache.put(key3, val3);
res = cache.invokeAllAsync(F.asSet(key1, key2, key3), INCR_PROCESSOR, dataMode).get();
assertEquals(val1, cache.get(key1));
assertEquals(val2, cache.get(key2));
assertEquals(val4, cache.get(key3));
assertNull(res.get(key1));
assertEquals(val1, res.get(key2).get());
assertEquals(val3, res.get(key3).get());
assertEquals(2, res.size());
cache.remove(key1);
cache.put(key2, val1);
cache.put(key3, val3);
res = cache.invokeAllAsync(F.asMap(key1, INCR_PROCESSOR, key2, INCR_PROCESSOR, key3, INCR_PROCESSOR), dataMode).get();
assertEquals(val1, cache.get(key1));
assertEquals(val2, cache.get(key2));
assertEquals(val4, cache.get(key3));
assertNull(res.get(key1));
assertEquals(val1, res.get(key2).get());
assertEquals(val3, res.get(key3).get());
assertEquals(2, res.size());
}
use of javax.cache.processor.EntryProcessorResult in project ignite by apache.
the class IgniteCachePutRetryAbstractSelfTest method checkRetry.
/**
* @param test Test type.
* @param evict If {@code true} uses eviction policy
* @param store If {@code true} uses cache with store.
* @throws Exception If failed.
*/
protected final void checkRetry(Test test, boolean evict, boolean store) throws Exception {
ignite(0).createCache(cacheConfiguration(evict, store));
final AtomicBoolean finished = new AtomicBoolean();
int keysCnt = keysCount();
IgniteInternalFuture<Object> fut = runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
Random rnd = new Random();
while (!finished.get()) {
stopGrid(3);
U.sleep(300);
startGrid(3);
if (// OPC possible only when there is no migration from one backup to another.
rnd.nextBoolean())
awaitPartitionMapExchange();
}
return null;
}
});
final IgniteCache<Integer, Integer> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
int iter = 0;
try {
long stopTime = System.currentTimeMillis() + DURATION;
switch(test) {
case PUT:
{
while (System.currentTimeMillis() < stopTime) {
Integer val = ++iter;
for (int i = 0; i < keysCnt; i++) cache.put(i, val);
for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
}
break;
}
case GET_AND_PUT:
{
for (int i = 0; i < keysCnt; i++) cache.put(i, 0);
while (System.currentTimeMillis() < stopTime) {
Integer expOld = iter;
Integer val = ++iter;
for (int i = 0; i < keysCnt; i++) {
Integer old = cache.getAndPut(i, val);
assertTrue("Unexpected old value [old=" + old + ", exp=" + expOld + ']', expOld.equals(old) || val.equals(old));
}
for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
}
break;
}
case TX_PUT:
{
while (System.currentTimeMillis() < stopTime) {
final Integer val = ++iter;
Ignite ignite = ignite(0);
for (int i = 0; i < keysCnt; i++) {
final Integer key = i;
doInTransaction(ignite, new Callable<Void>() {
@Override
public Void call() throws Exception {
cache.put(key, val);
return null;
}
});
}
for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
}
break;
}
case PUT_ALL:
{
while (System.currentTimeMillis() < stopTime) {
Integer val = ++iter;
Map<Integer, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < keysCnt; i++) {
map.put(i, val);
if (map.size() == 100 || i == keysCnt - 1) {
cache.putAll(map);
map.clear();
}
}
for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
}
}
case PUT_ASYNC:
{
while (System.currentTimeMillis() < stopTime) {
Integer val = ++iter;
for (int i = 0; i < keysCnt; i++) cache.putAsync(i, val).get();
for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.getAsync(i).get());
}
break;
}
case INVOKE:
{
while (System.currentTimeMillis() < stopTime) {
Integer val = ++iter;
Integer expOld = iter - 1;
for (int i = 0; i < keysCnt; i++) {
Integer old = cache.invoke(i, new SetEntryProcessor(val));
assertNotNull(old);
assertTrue(old.equals(expOld) || old.equals(val));
}
for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
}
break;
}
case INVOKE_ALL:
{
while (System.currentTimeMillis() < stopTime) {
Integer val = ++iter;
Integer expOld = iter - 1;
Set<Integer> keys = new LinkedHashSet<>();
for (int i = 0; i < keysCnt; i++) {
keys.add(i);
if (keys.size() == 100 || i == keysCnt - 1) {
Map<Integer, EntryProcessorResult<Integer>> resMap = cache.invokeAll(keys, new SetEntryProcessor(val));
for (Integer key : keys) {
EntryProcessorResult<Integer> res = resMap.get(key);
assertNotNull(res);
Integer old = res.get();
assertTrue(old.equals(expOld) || old.equals(val));
}
assertEquals(keys.size(), resMap.size());
keys.clear();
}
}
for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
}
break;
}
default:
assert false : test;
}
} finally {
finished.set(true);
fut.get();
}
for (int i = 0; i < keysCnt; i++) assertEquals((Integer) iter, cache.get(i));
}
use of javax.cache.processor.EntryProcessorResult in project ignite by apache.
the class IgfsMetaManager method createFileOrDirectory.
/**
* Create file or directory.
*
* @param dir Directory flag.
* @param pathIds Path IDs.
* @param lockInfos Lock infos.
* @param dirProps Directory properties.
* @param fileProps File properties.
* @param blockSize Block size.
* @param affKey Affinity key.
* @param evictExclude Evict exclude flag.
* @param secondaryCtx Secondary file system create context.
* @param secondaryOutHolder Secondary output stream holder.
* @return Result.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
private IgfsPathsCreateResult createFileOrDirectory(boolean dir, IgfsPathIds pathIds, Map<IgniteUuid, IgfsEntryInfo> lockInfos, Map<String, String> dirProps, Map<String, String> fileProps, int blockSize, @Nullable IgniteUuid affKey, boolean evictExclude, @Nullable IgfsSecondaryFileSystemCreateContext secondaryCtx, @Nullable T1<OutputStream> secondaryOutHolder) throws IgniteCheckedException {
// This is our starting point.
int lastExistingIdx = pathIds.lastExistingIndex();
IgfsEntryInfo lastExistingInfo = lockInfos.get(pathIds.lastExistingId());
// If current info already contains entry with the same name as it's child, then something
// has changed concurrently. We must re-try because we cannot get info of this unexpected
// element due to possible deadlocks.
int curIdx = lastExistingIdx + 1;
String curPart = pathIds.part(curIdx);
IgniteUuid curId = pathIds.surrogateId(curIdx);
if (lastExistingInfo.hasChild(curPart))
return null;
// Create entry in the secondary file system if needed.
if (secondaryCtx != null) {
assert secondaryOutHolder != null;
secondaryOutHolder.set(secondaryCtx.create());
}
Map<IgniteUuid, EntryProcessor> procMap = new HashMap<>();
// First step: add new entry to the last existing element.
procMap.put(lastExistingInfo.id(), new IgfsMetaDirectoryListingAddProcessor(curPart, new IgfsListingEntry(curId, dir || !pathIds.isLastIndex(curIdx))));
// Events support.
IgfsPath lastCreatedPath = pathIds.lastExistingPath();
List<IgfsPath> createdPaths = new ArrayList<>(pathIds.count() - curIdx);
// Second step: create middle directories.
long curTime = System.currentTimeMillis();
while (curIdx < pathIds.count() - 1) {
lastCreatedPath = new IgfsPath(lastCreatedPath, curPart);
int nextIdx = curIdx + 1;
String nextPart = pathIds.part(nextIdx);
IgniteUuid nextId = pathIds.surrogateId(nextIdx);
long accessTime;
long modificationTime;
Map<String, String> props;
if (secondaryCtx != null) {
accessTime = 0L;
modificationTime = 0L;
props = null;
} else {
accessTime = curTime;
modificationTime = curTime;
props = dirProps;
}
procMap.put(curId, new IgfsMetaDirectoryCreateProcessor(accessTime, modificationTime, props, nextPart, new IgfsListingEntry(nextId, dir || !pathIds.isLastIndex(nextIdx))));
// Save event.
createdPaths.add(lastCreatedPath);
// Advance things further.
curIdx++;
curPart = nextPart;
curId = nextId;
}
// Third step: create leaf.
if (dir) {
long accessTime;
long modificationTime;
Map<String, String> props;
if (secondaryCtx != null) {
accessTime = 0L;
modificationTime = 0L;
props = null;
} else {
accessTime = curTime;
modificationTime = curTime;
props = dirProps;
}
procMap.put(curId, new IgfsMetaDirectoryCreateProcessor(accessTime, modificationTime, props));
} else {
long newAccessTime;
long newModificationTime;
Map<String, String> newProps;
long newLen;
int newBlockSize;
if (secondaryCtx != null) {
newAccessTime = 0L;
newModificationTime = 0L;
newProps = null;
} else {
newAccessTime = curTime;
newModificationTime = curTime;
newProps = fileProps;
}
newLen = 0L;
newBlockSize = blockSize;
procMap.put(curId, new IgfsMetaFileCreateProcessor(newAccessTime, newModificationTime, newProps, newBlockSize, affKey, createFileLockId(false), evictExclude, newLen));
}
createdPaths.add(pathIds.path());
// Execute cache operations.
Map<Object, EntryProcessorResult> invokeRes = ((IgniteInternalCache) id2InfoPrj).invokeAll(procMap);
IgfsEntryInfo info = (IgfsEntryInfo) invokeRes.get(curId).get();
return new IgfsPathsCreateResult(createdPaths, info);
}
Aggregations