use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class IgnitePdsRecoveryAfterFileCorruptionTest method generateWal.
/**
* @param mem Mem.
* @param storeMgr Store manager.
* @param wal Wal.
* @param cacheId Cache id.
* @param pages Pages.
*/
private void generateWal(final PageMemoryImpl mem, final IgnitePageStoreManager storeMgr, final IgniteWriteAheadLogManager wal, final int cacheId, FullPageId[] pages) throws Exception {
// Mark the start position.
CheckpointRecord cpRec = new CheckpointRecord(null);
WALPointer start = wal.log(cpRec);
wal.flush(start, false);
for (FullPageId fullId : pages) {
long page = mem.acquirePage(fullId.groupId(), fullId.pageId());
try {
long pageAddr = mem.writeLock(fullId.groupId(), fullId.pageId(), page);
PageIO.setPageId(pageAddr, fullId.pageId());
try {
for (int j = PageIO.COMMON_HEADER_END; j < mem.realPageSize(fullId.groupId()); j += 4) PageUtils.putInt(pageAddr, j, j + (int) fullId.pageId());
} finally {
mem.writeUnlock(fullId.groupId(), fullId.pageId(), page, null, true);
}
} finally {
mem.releasePage(fullId.groupId(), fullId.pageId(), page);
}
}
Collection<FullPageId> pageIds = mem.beginCheckpoint(new GridFinishedFuture());
info("Acquired pages for checkpoint: " + pageIds.size());
try {
long begin = System.currentTimeMillis();
long cp = 0;
AtomicLong write = new AtomicLong();
PageStoreWriter pageStoreWriter = (fullPageId, buf, tag) -> {
int groupId = fullPageId.groupId();
long pageId = fullPageId.pageId();
for (int j = PageIO.COMMON_HEADER_END; j < mem.realPageSize(groupId); j += 4) assertEquals(j + (int) pageId, buf.getInt(j));
buf.rewind();
long writeStart = System.nanoTime();
storeMgr.write(cacheId, pageId, buf, tag, true);
long writeEnd = System.nanoTime();
write.getAndAdd(writeEnd - writeStart);
};
ByteBuffer tmpBuf = ByteBuffer.allocate(mem.pageSize());
tmpBuf.order(ByteOrder.nativeOrder());
for (FullPageId fullId : pages) {
if (pageIds.contains(fullId)) {
long cpStart = System.nanoTime();
mem.checkpointWritePage(fullId, tmpBuf, pageStoreWriter, null);
long cpEnd = System.nanoTime();
cp += cpEnd - cpStart;
}
}
long syncStart = System.currentTimeMillis();
storeMgr.sync(cacheId, 0);
long end = System.currentTimeMillis();
info("Written pages in " + (end - begin) + "ms, copy took " + (cp / 1_000_000) + "ms, " + "write took " + (write.get() / 1_000_000) + "ms, sync took " + (end - syncStart) + "ms");
} finally {
info("Finishing checkpoint...");
mem.finishCheckpoint();
info("Finished checkpoint");
}
wal.flush(wal.log(new CheckpointRecord(null)), false);
for (FullPageId fullId : pages) {
long page = mem.acquirePage(fullId.groupId(), fullId.pageId());
try {
assertFalse("Page has a temp heap copy after the last checkpoint: [cacheId=" + fullId.groupId() + ", pageId=" + fullId.pageId() + "]", mem.hasTempCopy(page));
assertFalse("Page is dirty after the last checkpoint: [cacheId=" + fullId.groupId() + ", pageId=" + fullId.pageId() + "]", mem.isDirty(fullId.groupId(), fullId.pageId(), page));
} finally {
mem.releasePage(fullId.groupId(), fullId.pageId(), page);
}
}
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridBaselineCommandHandler method handleAsync.
/**
* {@inheritDoc}
*/
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
assert req != null;
assert SUPPORTED_COMMANDS.contains(req.command());
assert req instanceof GridRestBaselineRequest : "Invalid type of baseline request.";
if (log.isDebugEnabled())
log.debug("Handling baseline REST request: " + req);
GridRestBaselineRequest req0 = (GridRestBaselineRequest) req;
try {
IgniteClusterEx cluster = ctx.grid().cluster();
List<Object> consistentIds = req0.consistentIds();
switch(req0.command()) {
case BASELINE_CURRENT_STATE:
{
break;
}
case BASELINE_SET:
{
Long topVer = req0.topologyVersion();
if (topVer == null && consistentIds == null)
throw new IgniteCheckedException("Failed to handle request (either topVer or consistentIds should be specified).");
if (topVer != null)
cluster.setBaselineTopology(topVer);
else
cluster.setBaselineTopology(filterServerNodesByConsId(consistentIds));
break;
}
case BASELINE_ADD:
{
if (consistentIds == null)
throw new IgniteCheckedException(missingParameter("consistentIds"));
Set<BaselineNode> baselineTop = new HashSet<>(currentBaseLine());
baselineTop.addAll(filterServerNodesByConsId(consistentIds));
cluster.setBaselineTopology(baselineTop);
break;
}
case BASELINE_REMOVE:
{
if (consistentIds == null)
throw new IgniteCheckedException(missingParameter("consistentIds"));
Collection<BaselineNode> baseline = currentBaseLine();
Set<BaselineNode> baselineTop = new HashSet<>(baseline);
baselineTop.removeAll(filterNodesByConsId(baseline, consistentIds));
cluster.setBaselineTopology(baselineTop);
break;
}
default:
assert false : "Invalid command for baseline handler: " + req;
}
return new GridFinishedFuture<>(new GridRestResponse(currentState()));
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
} finally {
if (log.isDebugEnabled())
log.debug("Handled baseline REST request: " + req);
}
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridTopologyCommandHandler method handleAsync.
/**
* {@inheritDoc}
*/
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
assert req instanceof GridRestTopologyRequest : "Invalid command for topology handler: " + req;
assert SUPPORTED_COMMANDS.contains(req.command());
if (log.isDebugEnabled())
log.debug("Handling topology REST request: " + req);
GridRestTopologyRequest req0 = (GridRestTopologyRequest) req;
GridRestResponse res = new GridRestResponse();
boolean mtr = req0.includeMetrics();
boolean attr = req0.includeAttributes();
boolean caches = req0.includeCaches();
switch(req.command()) {
case TOPOLOGY:
{
Collection<ClusterNode> allNodes = F.concat(false, ctx.discovery().allNodes(), ctx.discovery().daemonNodes());
Collection<GridClientNodeBean> top = new ArrayList<>(allNodes.size());
for (ClusterNode node : allNodes) top.add(createNodeBean(node, mtr, attr, caches));
res.setResponse(top);
break;
}
case NODE:
{
UUID id = req0.nodeId();
final String ip = req0.nodeIp();
if (id == null && ip == null)
return new GridFinishedFuture<>(new IgniteCheckedException("Failed to handle request (either id or ip should be specified)."));
ClusterNode node;
if (id != null) {
// Always refresh topology so client see most up-to-date view.
ctx.discovery().alive(id);
node = ctx.grid().cluster().node(id);
if (ip != null && node != null && !containsIp(node.addresses(), ip))
node = null;
} else
node = F.find(ctx.discovery().allNodes(), null, new P1<ClusterNode>() {
@Override
public boolean apply(ClusterNode n) {
return containsIp(n.addresses(), ip);
}
});
if (node != null)
res.setResponse(createNodeBean(node, mtr, attr, caches));
else
res.setResponse(null);
break;
}
default:
assert false : "Invalid command for topology handler: " + req;
}
if (log.isDebugEnabled())
log.debug("Handled topology REST request [res=" + res + ", req=" + req + ']');
return new GridFinishedFuture<>(res);
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridRestProcessor method handleRequest.
/**
* @param req Request.
* @return Future.
*/
private IgniteInternalFuture<GridRestResponse> handleRequest(final GridRestRequest req) {
if (req instanceof GridRestNodeStateBeforeStartRequest) {
if (startLatch.getCount() == 0)
return new GridFinishedFuture<>(new IgniteCheckedException("Node has already started."));
} else if (!(req instanceof GridRestAuthenticationRequest) && startLatch.getCount() > 0) {
try {
startLatch.await();
} catch (InterruptedException e) {
return new GridFinishedFuture<>(new IgniteCheckedException("Failed to handle request " + "(protocol handler was interrupted when awaiting grid start).", e));
}
}
if (log.isDebugEnabled())
log.debug("Received request from client: " + req);
if (securityEnabled) {
Session ses;
try {
ses = session(req);
} catch (IgniteAuthenticationException e) {
return new GridFinishedFuture<>(new GridRestResponse(STATUS_AUTH_FAILED, e.getMessage()));
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(new GridRestResponse(STATUS_FAILED, e.getMessage()));
}
assert ses != null;
req.clientId(ses.clientId);
req.sessionToken(U.uuidToBytes(ses.sesId));
if (log.isDebugEnabled())
log.debug("Next clientId and sessionToken were extracted according to request: " + "[clientId=" + req.clientId() + ", sesTok=" + Arrays.toString(req.sessionToken()) + "]");
SecurityContext secCtx0 = ses.secCtx;
try {
if (secCtx0 == null || ses.isTokenExpired(sesTokTtl))
ses.secCtx = secCtx0 = authenticate(req, ses);
try (OperationSecurityContext s = ctx.security().withContext(secCtx0)) {
authorize(req);
return handleRequest0(req);
}
} catch (SecurityException e) {
assert secCtx0 != null;
return new GridFinishedFuture<>(new GridRestResponse(STATUS_SECURITY_CHECK_FAILED, e.getMessage()));
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(new GridRestResponse(STATUS_AUTH_FAILED, e.getMessage()));
}
} else
return handleRequest0(req);
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridLogCommandHandler method handleAsync.
/**
* {@inheritDoc}
*/
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
assert req != null;
if (req.command() == LOG) {
if (log.isDebugEnabled())
log.debug("Handling log REST request: " + req);
GridRestLogRequest req0 = (GridRestLogRequest) req;
if (req0.from() < -1 || req0.to() < -1)
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "One of the request parameters is invalid [from=" + req0.from() + ", to=" + req0.to() + ']'));
int from;
if (req0.from() != -1) {
if (req0.to() == -1)
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'to' is not set."));
from = req0.from();
} else
from = DEFAULT_FROM;
int to;
if (req0.to() != -1) {
if (req0.from() == -1)
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'from' is not set."));
to = req0.to();
} else
to = DEFAULT_TO;
if (from >= to)
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'from' must be less than 'to'."));
File logFile;
try {
if (req0.path() != null) {
if (log.fileName() != null) {
if (!req0.path().equals(log.fileName())) {
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'path' must contain a path to valid log file."));
} else
logFile = new File(req0.path());
} else if (req0.path().startsWith(ctx.config().getIgniteHome()))
logFile = new File(req0.path());
else {
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'path' must contain a path to valid log file."));
}
} else if (log.fileName() == null)
logFile = new File(ctx.config().getIgniteHome() + "/work/log/ignite.log");
else
logFile = new File(log.fileName());
} catch (InvalidPathException e) {
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Incorrect path to a log file [msg=" + e.getMessage() + ']'));
}
try {
String content = readLog(from, to, logFile);
return new GridFinishedFuture<>(new GridRestResponse(content));
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, e.getMessage()));
}
}
return new GridFinishedFuture<>();
}
Aggregations