use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal in project ignite by apache.
the class IgfsMetaManager method updateProperties.
/**
* Update file info (file properties) in cache.
*
* @param fileId File ID to update information for.
* @param props Properties to set for the file.
* @return Updated file info or {@code null} if such file ID not found.
* @throws IgniteCheckedException If operation failed.
*/
@Nullable
public IgfsEntryInfo updateProperties(IgniteUuid fileId, Map<String, String> props) throws IgniteCheckedException {
if (busyLock.enterBusy()) {
try {
validTxState(false);
try (GridNearTxLocal tx = startTx()) {
IgfsEntryInfo info = updatePropertiesNonTx(fileId, props);
tx.commit();
return info;
}
} finally {
busyLock.leaveBusy();
}
} else
throw new IllegalStateException("Failed to update properties because Grid is stopping [fileId=" + fileId + ", props=" + props + ']');
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal in project ignite by apache.
the class IgfsMetaManager method append.
/**
* Append routine.
*
* @param path Path.
* @param dirProps Directory properties.
* @param create Create flag.
* @param blockSize Block size.
* @param affKey Affinity key.
* @param evictExclude Evict exclude flag.
* @param fileProps File properties.
* @return Resulting info.
* @throws IgniteCheckedException If failed.
*/
IgfsEntryInfo append(final IgfsPath path, Map<String, String> dirProps, final boolean create, final int blockSize, @Nullable final IgniteUuid affKey, final boolean evictExclude, @Nullable Map<String, String> fileProps) throws IgniteCheckedException {
validTxState(false);
while (true) {
if (busyLock.enterBusy()) {
try {
// Prepare path IDs.
IgfsPathIds pathIds = pathIds(path);
// Fail-fast: create flag is not specified and some paths are missing.
if (!pathIds.allExists() && !create)
throw new IgfsPathNotFoundException("Failed to append because file is not found: " + path);
// Prepare lock IDs.
Set<IgniteUuid> lockIds = new TreeSet<>(PATH_ID_SORTING_COMPARATOR);
pathIds.addExistingIds(lockIds, relaxed);
pathIds.addSurrogateIds(lockIds);
// Start TX.
try (GridNearTxLocal tx = startTx()) {
Map<IgniteUuid, IgfsEntryInfo> lockInfos = lockIds(lockIds);
if (!pathIds.verifyIntegrity(lockInfos, relaxed))
// Directory structure changed concurrently. So we simply re-try.
continue;
if (pathIds.allExists()) {
// All participants are found. Simply open the stream.
IgfsEntryInfo info = lockInfos.get(pathIds.lastId());
// Check: is it a file?
if (!info.isFile())
throw new IgfsPathIsDirectoryException("Failed to open file for write." + path);
// Check if file already opened for write.
if (info.lockId() != null)
throw new IgfsException("File is already opened for write: " + path);
// At this point we can open the stream safely.
info = invokeLock(info.id(), false);
tx.commit();
IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EventType.EVT_IGFS_FILE_OPENED_WRITE);
return info;
} else {
// Create file and parent folders.
IgfsPathsCreateResult res = createFile(pathIds, lockInfos, dirProps, fileProps, blockSize, affKey, evictExclude, null, null);
if (res == null)
continue;
// Commit.
tx.commit();
// Generate events.
generateCreateEvents(res.createdPaths(), true);
return res.info();
}
}
} finally {
busyLock.leaveBusy();
}
} else
throw new IllegalStateException("Failed to append for file because Grid is stopping:" + path);
}
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal in project ignite by apache.
the class GridCacheSemaphoreImpl method availablePermits.
/**
* {@inheritDoc}
*/
@Override
public int availablePermits() {
ctx.kernalContext().gateway().readLock();
int ret;
try {
initializeSemaphore();
ret = retryTopologySafe(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) {
GridCacheSemaphoreState val = cacheView.get(key);
if (val == null)
throw new IgniteException("Failed to find semaphore with given name: " + name);
int cnt = val.getCount();
tx.rollback();
return cnt;
}
}
});
} catch (IgniteCheckedException e) {
throw U.convertException(e);
} finally {
ctx.kernalContext().gateway().readUnlock();
}
return ret;
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal in project ignite by apache.
the class IgniteTxConfigCacheSelfTest method checkStartTxSuccess.
/**
* Success if explicit tx doesn't fail.
*
* @param cache Cache instance.
* @throws Exception If failed.
*/
protected void checkStartTxSuccess(final IgniteInternalCache<Object, Object> cache) throws Exception {
try (final GridNearTxLocal tx = CU.txStartInternal(cache.context(), cache, PESSIMISTIC, READ_COMMITTED)) {
assert tx != null;
sleepForTxFailure();
cache.put("key", "val");
tx.commit();
}
assert cache.containsKey("key");
cache.clear();
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal in project ignite by apache.
the class IgniteCachePrimaryNodeFailureRecoveryAbstractTest method primaryAndOriginatingNodeFailure.
/**
* @param locBackupKey If {@code true} uses one key which is backup for originating node.
* @param rollback If {@code true} tests rollback after primary node failure.
* @param optimistic If {@code true} tests optimistic transaction.
* @throws Exception If failed.
*/
private void primaryAndOriginatingNodeFailure(final boolean locBackupKey, final boolean rollback, boolean optimistic) throws Exception {
// TODO IGNITE-6174: when exchanges can be merged test fails because of IGNITE-6174.
System.setProperty(IGNITE_EXCHANGE_COMPATIBILITY_VER_1, "true");
try {
IgniteCache<Integer, Integer> cache0 = jcache(0);
IgniteCache<Integer, Integer> cache2 = jcache(2);
Affinity<Integer> aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
Integer key0 = null;
for (int key = 0; key < 10_000; key++) {
if (aff.isPrimary(ignite(1).cluster().localNode(), key)) {
if (locBackupKey == aff.isBackup(ignite(0).cluster().localNode(), key)) {
key0 = key;
break;
}
}
}
assertNotNull(key0);
final Integer key1 = key0;
final Integer key2 = primaryKey(cache2);
int backups = cache0.getConfiguration(CacheConfiguration.class).getBackups();
final Collection<ClusterNode> key1Nodes = (locBackupKey && backups < 2) ? null : aff.mapKeyToPrimaryAndBackups(key1);
final Collection<ClusterNode> key2Nodes = aff.mapKeyToPrimaryAndBackups(key2);
TestCommunicationSpi commSpi = (TestCommunicationSpi) ignite(0).configuration().getCommunicationSpi();
IgniteTransactions txs = ignite(0).transactions();
Transaction tx = txs.txStart(optimistic ? OPTIMISTIC : PESSIMISTIC, REPEATABLE_READ);
log.info("Put key1 [key1=" + key1 + ", nodes=" + U.nodeIds(aff.mapKeyToPrimaryAndBackups(key1)) + ']');
cache0.put(key1, key1);
log.info("Put key2 [key2=" + key2 + ", nodes=" + U.nodeIds(aff.mapKeyToPrimaryAndBackups(key2)) + ']');
cache0.put(key2, key2);
log.info("Start prepare.");
GridNearTxLocal txEx = ((TransactionProxyImpl) tx).tx();
// Do not allow to finish prepare for key2.
commSpi.blockMessages(ignite(2).cluster().localNode().id());
IgniteInternalFuture<?> prepFut = txEx.prepareNearTxLocal();
waitPrepared(ignite(1));
log.info("Stop one primary node.");
stopGrid(1);
// Wait some time to catch possible issues in tx recovery.
U.sleep(1000);
if (!rollback) {
commSpi.stopBlock();
prepFut.get(10_000);
}
log.info("Stop originating node.");
stopGrid(0);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
try {
checkKey(key1, rollback ? null : key1Nodes);
checkKey(key2, rollback ? null : key2Nodes);
return true;
} catch (AssertionError e) {
log.info("Check failed: " + e);
return false;
}
}
}, 5000);
checkKey(key1, rollback ? null : key1Nodes);
checkKey(key2, rollback ? null : key2Nodes);
} finally {
System.clearProperty(IGNITE_EXCHANGE_COMPATIBILITY_VER_1);
}
}
Aggregations