Search in sources :

Example 51 with GridKernalContext

use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.

the class CacheLateAffinityAssignmentTest method calculateAffinity.

/**
 * @param topVer Topology version.
 * @param filterByRcvd If {@code true} filters caches by 'receivedFrom' property.
 * @param cur Optional current affinity.
 * @throws Exception If failed.
 * @return {@code True} if some primary node changed comparing to given affinity.
 */
private boolean calculateAffinity(long topVer, boolean filterByRcvd, @Nullable Map<String, List<List<ClusterNode>>> cur) throws Exception {
    List<Ignite> all = G.allGrids();
    IgniteKernal ignite = (IgniteKernal) Collections.min(all, new Comparator<Ignite>() {

        @Override
        public int compare(Ignite n1, Ignite n2) {
            return Long.compare(n1.cluster().localNode().order(), n2.cluster().localNode().order());
        }
    });
    assert !all.isEmpty();
    Map<Integer, List<List<ClusterNode>>> assignments = idealAff.get(topVer);
    if (assignments == null)
        idealAff.put(topVer, assignments = new HashMap<>());
    GridKernalContext ctx = ignite.context();
    GridCacheSharedContext cctx = ctx.cache().context();
    AffinityTopologyVersion topVer0 = new AffinityTopologyVersion(topVer);
    cctx.discovery().topologyFuture(topVer).get();
    List<GridDhtPartitionsExchangeFuture> futs = cctx.exchange().exchangeFutures();
    DiscoveryEvent evt = null;
    long stopTime = System.currentTimeMillis() + 10_000;
    boolean primaryChanged = false;
    do {
        for (int i = futs.size() - 1; i >= 0; i--) {
            GridDhtPartitionsExchangeFuture fut = futs.get(i);
            if (fut.initialVersion().equals(topVer0)) {
                evt = fut.firstEvent();
                break;
            }
        }
        if (evt == null) {
            U.sleep(500);
            futs = cctx.exchange().exchangeFutures();
        } else
            break;
    } while (System.currentTimeMillis() < stopTime);
    assertNotNull("Failed to find exchange future:", evt);
    Collection<ClusterNode> allNodes = ctx.discovery().serverNodes(topVer0);
    for (DynamicCacheDescriptor cacheDesc : ctx.cache().cacheDescriptors().values()) {
        if (assignments.get(cacheDesc.cacheId()) != null)
            continue;
        if (filterByRcvd && cacheDesc.receivedFrom() != null && ctx.discovery().node(topVer0, cacheDesc.receivedFrom()) == null)
            continue;
        AffinityFunction func = cacheDesc.cacheConfiguration().getAffinity();
        func = cctx.cache().clone(func);
        cctx.kernalContext().resource().injectGeneric(func);
        List<ClusterNode> affNodes = new ArrayList<>();
        IgnitePredicate<ClusterNode> filter = cacheDesc.cacheConfiguration().getNodeFilter();
        for (ClusterNode n : allNodes) {
            if (!n.isClient() && (filter == null || filter.apply(n)))
                affNodes.add(n);
        }
        Collections.sort(affNodes, NodeOrderComparator.getInstance());
        AffinityFunctionContext affCtx = new GridAffinityFunctionContextImpl(affNodes, previousAssignment(topVer, cacheDesc.cacheId()), evt, topVer0, cacheDesc.cacheConfiguration().getBackups());
        List<List<ClusterNode>> assignment = func.assignPartitions(affCtx);
        if (cur != null) {
            List<List<ClusterNode>> prev = cur.get(cacheDesc.cacheConfiguration().getName());
            assertEquals(prev.size(), assignment.size());
            if (!primaryChanged) {
                for (int p = 0; p < prev.size(); p++) {
                    List<ClusterNode> nodes0 = prev.get(p);
                    List<ClusterNode> nodes1 = assignment.get(p);
                    if (!nodes0.isEmpty() && !nodes1.isEmpty()) {
                        ClusterNode p0 = nodes0.get(0);
                        ClusterNode p1 = nodes1.get(0);
                        if (allNodes.contains(p0) && !p0.equals(p1)) {
                            primaryChanged = true;
                            log.info("Primary changed [cache=" + cacheDesc.cacheConfiguration().getName() + ", part=" + p + ", prev=" + F.nodeIds(nodes0) + ", new=" + F.nodeIds(nodes1) + ']');
                            break;
                        }
                    }
                }
            }
        }
        assignments.put(cacheDesc.cacheId(), assignment);
    }
    return primaryChanged;
}
Also used : GridKernalContext(org.apache.ignite.internal.GridKernalContext) ArrayList(java.util.ArrayList) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) NodeOrderComparator(org.apache.ignite.internal.cluster.NodeOrderComparator) Comparator(java.util.Comparator) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DynamicCacheDescriptor(org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction)

Example 52 with GridKernalContext

use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.

the class IgnitePageMemReplaceDelayedWriteUnitTest method createPageMemory.

/**
 * @param cfg configuration
 * @param pageWriter writer for page replacement.
 * @param pageSize page size
 * @return implementation for test
 */
@NotNull
private PageMemoryImpl createPageMemory(IgniteConfiguration cfg, PageStoreWriter pageWriter, int pageSize) {
    IgniteCacheDatabaseSharedManager db = mock(GridCacheDatabaseSharedManager.class);
    when(db.checkpointLockIsHeldByThread()).thenReturn(true);
    GridCacheSharedContext sctx = Mockito.mock(GridCacheSharedContext.class);
    when(sctx.gridConfig()).thenReturn(cfg);
    when(sctx.pageStore()).thenReturn(new NoOpPageStoreManager());
    when(sctx.wal()).thenReturn(new NoOpWALManager());
    when(sctx.database()).thenReturn(db);
    when(sctx.logger(any(Class.class))).thenReturn(log);
    GridKernalContext kernalCtx = mock(GridKernalContext.class);
    when(kernalCtx.config()).thenReturn(cfg);
    when(kernalCtx.log(any(Class.class))).thenReturn(log);
    when(kernalCtx.internalSubscriptionProcessor()).thenAnswer(mock -> new GridInternalSubscriptionProcessor(kernalCtx));
    when(kernalCtx.encryption()).thenAnswer(mock -> new GridEncryptionManager(kernalCtx));
    when(kernalCtx.metric()).thenAnswer(mock -> new GridMetricManager(kernalCtx));
    when(kernalCtx.performanceStatistics()).thenAnswer(mock -> new PerformanceStatisticsProcessor(kernalCtx));
    when(sctx.kernalContext()).thenReturn(kernalCtx);
    when(sctx.gridEvents()).thenAnswer(invocationOnMock -> new GridEventStorageManager(kernalCtx));
    DataRegionConfiguration regCfg = cfg.getDataStorageConfiguration().getDefaultDataRegionConfiguration();
    DataRegionMetricsImpl memMetrics = new DataRegionMetricsImpl(regCfg, kernalCtx);
    long[] sizes = prepareSegmentSizes(regCfg.getMaxSize());
    DirectMemoryProvider provider = new UnsafeMemoryProvider(log);
    IgniteOutClosure<CheckpointProgress> clo = () -> Mockito.mock(CheckpointProgressImpl.class);
    PageMemoryImpl memory = new PageMemoryImpl(provider, sizes, sctx, sctx.pageStore(), pageSize, pageWriter, null, () -> true, memMetrics, PageMemoryImpl.ThrottlingPolicy.DISABLED, clo);
    memory.start();
    return memory;
}
Also used : GridEventStorageManager(org.apache.ignite.internal.managers.eventstorage.GridEventStorageManager) GridEncryptionManager(org.apache.ignite.internal.managers.encryption.GridEncryptionManager) CheckpointProgress(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointProgress) GridInternalSubscriptionProcessor(org.apache.ignite.internal.processors.subscription.GridInternalSubscriptionProcessor) GridKernalContext(org.apache.ignite.internal.GridKernalContext) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) DataRegionMetricsImpl(org.apache.ignite.internal.processors.cache.persistence.DataRegionMetricsImpl) PerformanceStatisticsProcessor(org.apache.ignite.internal.processors.performancestatistics.PerformanceStatisticsProcessor) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) DirectMemoryProvider(org.apache.ignite.internal.mem.DirectMemoryProvider) GridMetricManager(org.apache.ignite.internal.processors.metric.GridMetricManager) UnsafeMemoryProvider(org.apache.ignite.internal.mem.unsafe.UnsafeMemoryProvider) IgniteCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager) NotNull(org.jetbrains.annotations.NotNull)

Example 53 with GridKernalContext

use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.

the class GridP2PContinuousDeploymentClientDisconnectTest method blockClassLoadingRequest.

/**
 * Blocks peer-class loading for {@link #P2P_TEST_OBJ_RSRC_NAME} resource.
 *
 * @param node The node where peer-class loading should be blocked.
 */
private void blockClassLoadingRequest(IgniteEx node) {
    GridKernalContext ctx = node.context();
    GridDeploymentManager deploymentMgr = ctx.deploy();
    Object comm = GridTestUtils.getFieldValue(deploymentMgr, "comm");
    GridMessageListener peerLsnr = GridTestUtils.getFieldValue(comm, "peerLsnr");
    ctx.io().removeMessageListener(TOPIC_CLASSLOAD, peerLsnr);
    GridMessageListener newPeerLsnr = new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            GridDeploymentRequest req = (GridDeploymentRequest) msg;
            String rsrcName = GridTestUtils.getFieldValue(req, "rsrcName");
            if (rsrcName.equals(P2P_TEST_OBJ_RSRC_NAME))
                return;
            peerLsnr.onMessage(nodeId, msg, plc);
        }
    };
    ctx.io().addMessageListener(TOPIC_CLASSLOAD, newPeerLsnr);
}
Also used : GridKernalContext(org.apache.ignite.internal.GridKernalContext) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridDeploymentManager(org.apache.ignite.internal.managers.deployment.GridDeploymentManager) GridDeploymentRequest(org.apache.ignite.internal.managers.deployment.GridDeploymentRequest) UUID(java.util.UUID)

Example 54 with GridKernalContext

use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.

the class IgniteWalConverterSensitiveDataTest method beforeTestsStarted.

/**
 * {@inheritDoc}
 */
@Override
protected void beforeTestsStarted() throws Exception {
    super.beforeTestsStarted();
    sysOut = System.out;
    testOut = new ByteArrayOutputStream(16 * 1024);
    int nodeId = 0;
    IgniteEx crd = startGrid(nodeId);
    crd.cluster().active(true);
    try (Transaction tx = crd.transactions().txStart()) {
        IgniteCache<Object, Object> cache = crd.cache(DEFAULT_CACHE_NAME);
        sensitiveValues.add(SENSITIVE_DATA_VALUE_PREFIX + 0);
        sensitiveValues.add(SENSITIVE_DATA_VALUE_PREFIX + 1);
        sensitiveValues.add(SENSITIVE_DATA_VALUE_PREFIX + 2);
        String val0 = sensitiveValues.get(0);
        String val1 = sensitiveValues.get(1);
        String val2 = sensitiveValues.get(2);
        cache.put(val0, val0);
        cache.withKeepBinary().put(val1, val1);
        cache.put(val2, new Person(1, val2));
        tx.commit();
    }
    GridKernalContext kernalCtx = crd.context();
    IgniteWriteAheadLogManager wal = kernalCtx.cache().context().wal();
    for (WALRecord walRecord : withSensitiveData()) {
        if (isIncludeIntoLog(walRecord))
            wal.log(walRecord);
    }
    sensitiveValues.add(SENSITIVE_DATA_VALUE_PREFIX);
    wal.flush(null, true);
    IgniteConfiguration cfg = crd.configuration();
    String wd = cfg.getWorkDirectory();
    String wp = cfg.getDataStorageConfiguration().getWalPath();
    String fn = kernalCtx.pdsFolderResolver().resolveFolders().folderName();
    walDirPath = wd + File.separator + wp + File.separator + fn;
    pageSize = cfg.getDataStorageConfiguration().getPageSize();
    stopGrid(nodeId);
}
Also used : WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) Transaction(org.apache.ignite.transactions.Transaction) IgniteWriteAheadLogManager(org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridKernalContext(org.apache.ignite.internal.GridKernalContext) IgniteEx(org.apache.ignite.internal.IgniteEx) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 55 with GridKernalContext

use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.

the class GridAbstractTest method awaitTopologyChange.

/**
 * @throws IgniteInterruptedCheckedException If interrupted.
 */
private void awaitTopologyChange() throws IgniteInterruptedCheckedException {
    for (Ignite g : G.allGrids()) {
        final GridKernalContext ctx = ((IgniteKernal) g).context();
        if (ctx.isStopping() || ctx.gateway().getState() == DISCONNECTED || !g.active())
            continue;
        AffinityTopologyVersion topVer = ctx.discovery().topologyVersionEx();
        AffinityTopologyVersion exchVer = ctx.cache().context().exchange().readyAffinityVersion();
        if (!topVer.equals(exchVer)) {
            info("Topology version mismatch [node=" + g.name() + ", exchVer=" + exchVer + ", topVer=" + topVer + ']');
            GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    AffinityTopologyVersion topVer = ctx.discovery().topologyVersionEx();
                    AffinityTopologyVersion exchVer = ctx.cache().context().exchange().readyAffinityVersion();
                    return exchVer.equals(topVer);
                }
            }, DFLT_TOP_WAIT_TIMEOUT);
        }
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridKernalContext(org.apache.ignite.internal.GridKernalContext) Ignite(org.apache.ignite.Ignite)

Aggregations

GridKernalContext (org.apache.ignite.internal.GridKernalContext)61 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)14 IgniteEx (org.apache.ignite.internal.IgniteEx)13 GridCacheSharedContext (org.apache.ignite.internal.processors.cache.GridCacheSharedContext)12 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 ArrayList (java.util.ArrayList)10 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)10 List (java.util.List)9 IgniteLogger (org.apache.ignite.IgniteLogger)9 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)9 File (java.io.File)8 Map (java.util.Map)8 UUID (java.util.UUID)8 Ignite (org.apache.ignite.Ignite)8 IgniteKernal (org.apache.ignite.internal.IgniteKernal)8 Nullable (org.jetbrains.annotations.Nullable)8 Test (org.junit.Test)8 Collection (java.util.Collection)7 IgniteException (org.apache.ignite.IgniteException)7 FilePageStoreManager (org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager)7