Search in sources :

Example 66 with GridFinishedFuture

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);
        }
    }
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) FileWriteAheadLogManager(org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager) PageStore(org.apache.ignite.internal.pagemem.store.PageStore) FullPageId(org.apache.ignite.internal.pagemem.FullPageId) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) ClusterState(org.apache.ignite.cluster.ClusterState) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteEx(org.apache.ignite.internal.IgniteEx) PageMemory(org.apache.ignite.internal.pagemem.PageMemory) CacheRebalanceMode(org.apache.ignite.cache.CacheRebalanceMode) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) ByteBuffer(java.nio.ByteBuffer) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO) PageUtils(org.apache.ignite.internal.pagemem.PageUtils) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) RolloverType(org.apache.ignite.internal.pagemem.wal.record.RolloverType) FilePageStore(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStore) WALMode(org.apache.ignite.configuration.WALMode) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) PageIdAllocator(org.apache.ignite.internal.pagemem.PageIdAllocator) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) FilePageStoreManager(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager) Test(org.junit.Test) IgniteCache(org.apache.ignite.IgniteCache) CheckpointRecord(org.apache.ignite.internal.pagemem.wal.record.CheckpointRecord) PageMemoryImpl(org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryImpl) ByteOrder(java.nio.ByteOrder) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgnitePageStoreManager(org.apache.ignite.internal.pagemem.store.IgnitePageStoreManager) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) PageIdUtils(org.apache.ignite.internal.pagemem.PageIdUtils) MvccFeatureChecker(org.apache.ignite.testframework.MvccFeatureChecker) IgniteWriteAheadLogManager(org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) AtomicLong(java.util.concurrent.atomic.AtomicLong) CheckpointRecord(org.apache.ignite.internal.pagemem.wal.record.CheckpointRecord) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) ByteBuffer(java.nio.ByteBuffer) FullPageId(org.apache.ignite.internal.pagemem.FullPageId) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 67 with GridFinishedFuture

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);
    }
}
Also used : IgniteClusterEx(org.apache.ignite.internal.cluster.IgniteClusterEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) HashSet(java.util.HashSet) Set(java.util.Set) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridRestBaselineRequest(org.apache.ignite.internal.processors.rest.request.GridRestBaselineRequest) Collection(java.util.Collection) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 68 with GridFinishedFuture

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);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) P1(org.apache.ignite.internal.util.typedef.P1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestTopologyRequest(org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) Collection(java.util.Collection) UUID(java.util.UUID) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 69 with GridFinishedFuture

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);
}
Also used : IgniteAuthenticationException(org.apache.ignite.IgniteAuthenticationException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) OperationSecurityContext(org.apache.ignite.internal.processors.security.OperationSecurityContext) SecurityContext(org.apache.ignite.internal.processors.security.SecurityContext) GridRestAuthenticationRequest(org.apache.ignite.internal.processors.rest.request.GridRestAuthenticationRequest) SecurityException(org.apache.ignite.plugin.security.SecurityException) GridRestNodeStateBeforeStartRequest(org.apache.ignite.internal.processors.rest.request.GridRestNodeStateBeforeStartRequest) OperationSecurityContext(org.apache.ignite.internal.processors.security.OperationSecurityContext) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 70 with GridFinishedFuture

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<>();
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridRestLogRequest(org.apache.ignite.internal.processors.rest.request.GridRestLogRequest) File(java.io.File) InvalidPathException(java.nio.file.InvalidPathException) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Aggregations

GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)75 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)55 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)23 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)21 ArrayList (java.util.ArrayList)20 Map (java.util.Map)18 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)17 HashMap (java.util.HashMap)16 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)15 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)15 IgniteException (org.apache.ignite.IgniteException)14 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)14 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)14 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)14 LinkedHashMap (java.util.LinkedHashMap)13 IgniteTxRollbackCheckedException (org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException)12 GridClosureException (org.apache.ignite.internal.util.lang.GridClosureException)12 HashSet (java.util.HashSet)11 List (java.util.List)11 ClusterNode (org.apache.ignite.cluster.ClusterNode)11