use of org.apache.ignite.failure.FailureContext in project ignite by apache.
the class FsyncFileWriteHandle method writeBuffer.
/**
* @param pos Position in file to start write from. May be checked against actual position to wait previous writes
* to complete.
* @param buf Buffer to write to file.
* @throws StorageException If failed.
*/
@SuppressWarnings("TooBroadScope")
private void writeBuffer(long pos, ByteBuffer buf) throws StorageException {
boolean interrupted = false;
lock.lock();
try {
assert fileIO != null : "Writing to a closed segment.";
checkNode();
long lastLogged = U.currentTimeMillis();
long logBackoff = 2_000;
// If we were too fast, need to wait previous writes to complete.
while (written != pos) {
// No one can write further than we are now.
assert written < pos : "written = " + written + ", pos = " + pos;
// Permutation occurred between blocks write operations.
// Order of acquiring lock is not the same as order of write.
long now = U.currentTimeMillis();
if (now - lastLogged >= logBackoff) {
if (logBackoff < 60 * 60_000)
logBackoff *= 2;
U.warn(log, "Still waiting for a concurrent write to complete [written=" + written + ", pos=" + pos + ", lastFsyncPos=" + lastFsyncPos + ", stop=" + stop.get() + ", actualPos=" + safePosition() + ']');
lastLogged = now;
}
try {
writeComplete.await(2, TimeUnit.SECONDS);
} catch (InterruptedException ignore) {
interrupted = true;
}
checkNode();
}
// Do the write.
int size = buf.remaining();
assert size > 0 : size;
try {
assert written == fileIO.position();
fileIO.writeFully(buf);
written += size;
metrics.onWalBytesWritten(size);
assert written == fileIO.position();
} catch (IOException e) {
StorageException se = new StorageException("Unable to write", e);
cctx.kernalContext().failure().process(new FailureContext(CRITICAL_ERROR, se));
throw se;
}
} finally {
writeComplete.signalAll();
lock.unlock();
if (interrupted)
Thread.currentThread().interrupt();
}
}
use of org.apache.ignite.failure.FailureContext in project ignite by apache.
the class GridJobWorker method execute0.
/**
* @param skipNtf {@code True} to skip job processor {@code onUnheld()}
* notification (only from {@link #body()}).
*/
private void execute0(boolean skipNtf) {
// Make sure flag is not set for current thread.
HOLD.set(false);
SqlFieldsQuery.setThreadedQueryInitiatorId("task:" + ses.getTaskName() + ":" + getJobId());
try (OperationSecurityContext ignored = ctx.security().withContext(secCtx)) {
if (partsReservation != null) {
try {
if (!partsReservation.reserve()) {
finishJob(null, null, true, true);
return;
}
} catch (Exception e) {
IgniteException ex = new IgniteException("Failed to lock partitions " + "[jobId=" + ses.getJobId() + ", ses=" + ses + ']', e);
U.error(log, "Failed to lock partitions [jobId=" + ses.getJobId() + ", ses=" + ses + ']', e);
;
finishJob(null, ex, true);
return;
}
}
if (isCancelled())
// If job was cancelled prior to assigning runner to it?
super.cancel();
if (!skipNtf) {
if (holdLsnr.onUnheld(this)) {
if (held.decrementAndGet() == 0)
status = RUNNING;
} else {
if (log.isDebugEnabled())
log.debug("Ignoring job execution (job was not held).");
return;
}
}
boolean sndRes = true;
Object res = null;
IgniteException ex = null;
try {
ctx.job().currentTaskSession(ses);
if (reqTopVer != null)
GridQueryProcessor.setRequestAffinityTopologyVersion(reqTopVer);
// avoid computation altogether.
if (isTimedOut())
sndRes = false;
else {
res = U.wrapThreadLoader(dep.classLoader(), new Callable<Object>() {
@Nullable
@Override
public Object call() {
try {
if (internal && ctx.config().isPeerClassLoadingEnabled())
ctx.job().internal(true);
return job.execute();
} finally {
if (internal && ctx.config().isPeerClassLoadingEnabled())
ctx.job().internal(false);
}
}
});
if (log.isDebugEnabled()) {
log.debug(S.toString("Job execution has successfully finished", "job", job, false, "res", res, true));
}
}
} catch (IgniteException e) {
if (sysStopping && e.hasCause(IgniteInterruptedCheckedException.class, InterruptedException.class)) {
ex = handleThrowable(e);
assert ex != null;
} else {
if (X.hasCause(e, InterruptedException.class)) {
if (log.isDebugEnabled()) {
U.error(log, "Job was cancelled [jobId=" + ses.getJobId() + ", ses=" + ses + ']', e);
}
} else if (X.hasCause(e, GridServiceNotFoundException.class) || X.hasCause(e, ClusterTopologyCheckedException.class)) {
if (log.isDebugEnabled()) {
// Should be throttled, because GridServiceProxy continuously retry getting service.
LT.error(log, e, "Failed to execute job [jobId=" + ses.getJobId() + ", ses=" + ses + ']');
}
} else {
String msg = "Failed to execute job [jobId=" + ses.getJobId() + ", ses=" + ses + ']';
if (X.hasCause(e, OutOfMemoryError.class)) {
U.error(log, msg, e);
ctx.failure().process(new FailureContext(FailureType.CRITICAL_ERROR, e));
} else if (log.isDebugEnabled())
U.error(log, msg, e);
}
ex = e;
}
}// InterruptedException if job is being cancelled.
catch (Throwable e) {
ex = handleThrowable(e);
assert ex != null;
if (e instanceof Error)
throw (Error) e;
} finally {
// Finish here only if not held by this thread.
if (!HOLD.get())
finishJob(res, ex, sndRes);
else
// Make sure flag is not set for current thread.
// This may happen in case of nested internal task call with continuation.
HOLD.set(false);
ctx.job().currentTaskSession(null);
if (reqTopVer != null)
GridQueryProcessor.setRequestAffinityTopologyVersion(null);
}
} finally {
SqlFieldsQuery.resetThreadedQueryInitiatorId();
if (partsReservation != null)
partsReservation.release();
}
}
use of org.apache.ignite.failure.FailureContext in project ignite by apache.
the class FilePageStoreManager method storeCacheData.
/**
* {@inheritDoc}
*/
@Override
public void storeCacheData(StoredCacheData cacheData, boolean overwrite) throws IgniteCheckedException {
CacheConfiguration<?, ?> ccfg = cacheData.config();
File cacheWorkDir = cacheWorkDir(ccfg);
checkAndInitCacheWorkDir(cacheWorkDir);
assert cacheWorkDir.exists() : "Work directory does not exist: " + cacheWorkDir;
File file = cacheConfigurationFile(ccfg);
Path filePath = file.toPath();
chgLock.readLock().lock();
try {
if (overwrite || !Files.exists(filePath) || Files.size(filePath) == 0) {
File tmp = new File(file.getParent(), file.getName() + TMP_SUFFIX);
if (tmp.exists() && !tmp.delete()) {
log.warning("Failed to delete temporary cache config file" + "(make sure Ignite process has enough rights):" + file.getName());
}
// Pre-existing file will be truncated upon stream open.
try (OutputStream stream = new BufferedOutputStream(new FileOutputStream(tmp))) {
marshaller.marshal(cacheData, stream);
}
if (Files.exists(filePath) && Files.size(filePath) > 0) {
for (BiConsumer<String, File> lsnr : lsnrs) lsnr.accept(ccfg.getName(), file);
}
Files.move(tmp.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException ex) {
cctx.kernalContext().failure().process(new FailureContext(FailureType.CRITICAL_ERROR, ex));
throw new IgniteCheckedException("Failed to persist cache configuration: " + ccfg.getName(), ex);
} finally {
chgLock.readLock().unlock();
}
}
use of org.apache.ignite.failure.FailureContext in project ignite by apache.
the class FilePageStoreManager method initDir.
/**
* @param cacheWorkDir Work directory.
* @param grpId Group ID.
* @param partitions Number of partitions.
* @param pageMetrics Page metrics.
* @param encrypted {@code True} if this cache encrypted.
* @return Cache store holder.
* @throws IgniteCheckedException If failed.
*/
private CacheStoreHolder initDir(File cacheWorkDir, int grpId, int partitions, PageMetrics pageMetrics, boolean encrypted) throws IgniteCheckedException {
try {
boolean dirExisted = checkAndInitCacheWorkDir(cacheWorkDir);
if (dirExisted) {
MaintenanceRegistry mntcReg = cctx.kernalContext().maintenanceRegistry();
if (!mntcReg.isMaintenanceMode())
DefragmentationFileUtils.beforeInitPageStores(cacheWorkDir, log);
}
File idxFile = new File(cacheWorkDir, INDEX_FILE_NAME);
if (dirExisted && !idxFile.exists())
grpsWithoutIdx.add(grpId);
FileVersionCheckingFactory pageStoreFactory = getPageStoreFactory(grpId, encrypted);
PageStore idxStore = pageStoreFactory.createPageStore(PageStore.TYPE_IDX, idxFile, pageMetrics.totalPages()::add);
PageStore[] partStores = new PageStore[partitions];
for (int partId = 0; partId < partStores.length; partId++) {
final int p = partId;
PageStore partStore = pageStoreFactory.createPageStore(PageStore.TYPE_DATA, () -> getPartitionFilePath(cacheWorkDir, p), pageMetrics.totalPages()::add);
partStores[partId] = partStore;
}
return new CacheStoreHolder(idxStore, partStores);
} catch (IgniteCheckedException e) {
if (X.hasCause(e, StorageException.class, IOException.class))
cctx.kernalContext().failure().process(new FailureContext(FailureType.CRITICAL_ERROR, e));
throw e;
}
}
use of org.apache.ignite.failure.FailureContext in project ignite by apache.
the class InlineIndexImpl method remove.
/**
*/
private void remove(CacheDataRow row) throws IgniteCheckedException {
try {
int segment = segmentForRow(row);
IndexRowImpl idxRow = new IndexRowImpl(rowHnd, row);
idxRow.prepareCache();
segments[segment].removex(idxRow);
} catch (Throwable t) {
cctx.kernalContext().failure().process(new FailureContext(CRITICAL_ERROR, t));
throw t;
}
}
Aggregations