Search in sources :

Example 46 with GridKernalContext

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

the class GridCacheNestedTxAbstractTest method testTwoTx.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testTwoTx() throws Exception {
    final IgniteCache<String, Integer> c = grid(0).cache(DEFAULT_CACHE_NAME);
    GridKernalContext ctx = ((IgniteKernal) grid(0)).context();
    c.put(CNTR_KEY, 0);
    for (int i = 0; i < 10; i++) {
        try (Transaction tx = grid(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            c.get(CNTR_KEY);
            ctx.closure().callLocalSafe((new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    assertFalse(((GridCacheAdapter) c).context().tm().inUserTx());
                    assertNull(((GridCacheAdapter) c).context().tm().userTx());
                    return true;
                }
            }), true);
            tx.commit();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteKernal(org.apache.ignite.internal.IgniteKernal) Transaction(org.apache.ignite.transactions.Transaction) GridKernalContext(org.apache.ignite.internal.GridKernalContext) Callable(java.util.concurrent.Callable) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 47 with GridKernalContext

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

the class AbstractReadRepairTest method setDifferentValuesForSameKey.

/**
 */
private InconsistentMapping setDifferentValuesForSameKey(int key, boolean misses, boolean nulls, ReadRepairStrategy strategy) throws Exception {
    List<Ignite> nodes = new ArrayList<>();
    Map<Ignite, T2<Integer, GridCacheVersion>> mapping = new HashMap<>();
    Ignite primary = primaryNode(key, DEFAULT_CACHE_NAME);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    if (rnd.nextBoolean()) {
        // Reversed order.
        nodes.addAll(backupNodes(key, DEFAULT_CACHE_NAME));
        nodes.add(primary);
    } else {
        nodes.add(primary);
        nodes.addAll(backupNodes(key, DEFAULT_CACHE_NAME));
    }
    if (// Random order.
    rnd.nextBoolean())
        Collections.shuffle(nodes);
    IgniteInternalCache<Integer, Integer> internalCache = (grid(1)).cachex(DEFAULT_CACHE_NAME);
    GridCacheVersionManager mgr = ((GridCacheAdapter) internalCache.cache()).context().shared().versions();
    int incVal = 0;
    Integer primVal = null;
    Collection<T2<Integer, GridCacheVersion>> vals = new ArrayList<>();
    if (misses) {
        List<Ignite> keeped = nodes.subList(0, rnd.nextInt(1, nodes.size()));
        nodes.stream().filter(node -> !keeped.contains(node)).forEach(node -> {
            T2<Integer, GridCacheVersion> nullT2 = new T2<>(null, null);
            vals.add(nullT2);
            mapping.put(node, nullT2);
        });
        // Recording nulls (missed values).
        nodes = keeped;
    }
    boolean rmvd = false;
    boolean incVer = rnd.nextBoolean();
    GridCacheVersion ver = null;
    for (Ignite node : nodes) {
        IgniteInternalCache<Integer, Integer> cache = ((IgniteEx) node).cachex(DEFAULT_CACHE_NAME);
        GridCacheAdapter<Integer, Integer> adapter = (GridCacheAdapter) cache.cache();
        GridCacheEntryEx entry = adapter.entryEx(key);
        if (ver == null || incVer)
            // Incremental version.
            ver = mgr.next(entry.context().kernalContext().discovery().topologyVersion());
        boolean rmv = nulls && (!rmvd || rnd.nextBoolean());
        Integer val = rmv ? null : rnd.nextBoolean() ? /*increment or same as previously*/
        ++incVal : incVal;
        T2<Integer, GridCacheVersion> valVer = new T2<>(val, val != null ? ver : null);
        vals.add(valVer);
        mapping.put(node, valVer);
        GridKernalContext kctx = ((IgniteEx) node).context();
        // Incremental value.
        byte[] bytes = kctx.cacheObjects().marshal(entry.context().cacheObjectContext(), rmv ? -1 : val);
        try {
            kctx.cache().context().database().checkpointReadLock();
            boolean init = entry.initialValue(new CacheObjectImpl(null, bytes), ver, 0, 0, false, AffinityTopologyVersion.NONE, GridDrType.DR_NONE, false, false);
            if (rmv) {
                if (cache.configuration().getAtomicityMode() == ATOMIC)
                    entry.innerUpdate(ver, ((IgniteEx) node).localNode().id(), ((IgniteEx) node).localNode().id(), GridCacheOperation.DELETE, null, null, false, false, false, false, null, false, false, false, false, AffinityTopologyVersion.NONE, null, GridDrType.DR_NONE, 0, 0, null, false, false, null, null, null, null, false);
                else
                    entry.innerRemove(null, ((IgniteEx) node).localNode().id(), ((IgniteEx) node).localNode().id(), false, false, false, false, false, null, AffinityTopologyVersion.NONE, CU.empty0(), GridDrType.DR_NONE, null, null, null, 1L);
                rmvd = true;
                assertFalse(entry.hasValue());
            } else
                assertTrue(entry.hasValue());
            assertTrue("iterableKey " + key + " already inited", init);
            if (node.equals(primary))
                primVal = val;
        } finally {
            ((IgniteEx) node).context().cache().context().database().checkpointReadUnlock();
        }
    }
    assertEquals(vals.size(), mapping.size());
    assertEquals(vals.size(), internalCache.configuration().getCacheMode() == REPLICATED ? serverNodesCount() : backupsCount() + 1);
    if (!misses && !nulls)
        // Primary value set.
        assertTrue(primVal != null);
    Integer fixed;
    boolean consistent;
    boolean repairable;
    if (vals.stream().distinct().count() == 1) {
        // Consistent value.
        consistent = true;
        repairable = true;
        fixed = vals.iterator().next().getKey();
    } else {
        consistent = false;
        // Currently, Atomic caches can not be repaired.
        repairable = atomicityMode() != ATOMIC;
        switch(strategy) {
            case LWW:
                if (misses || rmvd || !incVer) {
                    repairable = false;
                    // Should never be returned.
                    fixed = Integer.MIN_VALUE;
                } else
                    fixed = incVal;
                break;
            case PRIMARY:
                fixed = primVal;
                break;
            case RELATIVE_MAJORITY:
                // Should never be returned.
                fixed = Integer.MIN_VALUE;
                Map<T2<Integer, GridCacheVersion>, Integer> counts = new HashMap<>();
                for (T2<Integer, GridCacheVersion> val : vals) {
                    counts.putIfAbsent(val, 0);
                    counts.compute(val, (k, v) -> v + 1);
                }
                int[] sorted = counts.values().stream().sorted(Comparator.reverseOrder()).mapToInt(v -> v).toArray();
                int max = sorted[0];
                if (sorted.length > 1 && sorted[1] == max)
                    repairable = false;
                if (repairable)
                    for (Map.Entry<T2<Integer, GridCacheVersion>, Integer> count : counts.entrySet()) if (count.getValue().equals(max)) {
                        fixed = count.getKey().getKey();
                        break;
                    }
                break;
            case REMOVE:
                fixed = null;
                break;
            case CHECK_ONLY:
                repairable = false;
                // Should never be returned.
                fixed = Integer.MIN_VALUE;
                break;
            default:
                throw new UnsupportedOperationException(strategy.toString());
        }
    }
    return new InconsistentMapping(mapping, primVal, fixed, repairable, consistent);
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) IgniteEx(org.apache.ignite.internal.IgniteEx) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) Map(java.util.Map) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) EventType(org.apache.ignite.events.EventType) Collection(java.util.Collection) Event(org.apache.ignite.events.Event) ReadRepairStrategy(org.apache.ignite.cache.ReadRepairStrategy) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CU(org.apache.ignite.internal.util.typedef.internal.CU) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) ClusterState(org.apache.ignite.cluster.ClusterState) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GridKernalContext(org.apache.ignite.internal.GridKernalContext) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteIrreparableConsistencyViolationException(org.apache.ignite.internal.processors.cache.distributed.near.consistency.IgniteIrreparableConsistencyViolationException) GridDrType(org.apache.ignite.internal.processors.dr.GridDrType) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) IgniteEvents(org.apache.ignite.IgniteEvents) G(org.apache.ignite.internal.util.typedef.G) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) Ignite(org.apache.ignite.Ignite) CacheConsistencyViolationEvent(org.apache.ignite.events.CacheConsistencyViolationEvent) CacheObjectImpl(org.apache.ignite.internal.processors.cache.CacheObjectImpl) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) TRANSACTIONAL(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL) T2(org.apache.ignite.internal.util.typedef.T2) REPLICATED(org.apache.ignite.cache.CacheMode.REPLICATED) Consumer(java.util.function.Consumer) GridCacheVersionManager(org.apache.ignite.internal.processors.cache.version.GridCacheVersionManager) TreeMap(java.util.TreeMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) ATOMIC(org.apache.ignite.cache.CacheAtomicityMode.ATOMIC) EVT_CONSISTENCY_VIOLATION(org.apache.ignite.events.EventType.EVT_CONSISTENCY_VIOLATION) Comparator(java.util.Comparator) Collections(java.util.Collections) CacheMode(org.apache.ignite.cache.CacheMode) HashMap(java.util.HashMap) GridKernalContext(org.apache.ignite.internal.GridKernalContext) ArrayList(java.util.ArrayList) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) T2(org.apache.ignite.internal.util.typedef.T2) CacheObjectImpl(org.apache.ignite.internal.processors.cache.CacheObjectImpl) GridCacheVersionManager(org.apache.ignite.internal.processors.cache.version.GridCacheVersionManager) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteEx(org.apache.ignite.internal.IgniteEx)

Example 48 with GridKernalContext

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

the class MaintenanceRegistrySimpleTest method initContext.

/**
 */
private GridKernalContext initContext(boolean persistenceEnabled) throws IgniteCheckedException {
    String dfltWorkDir = U.defaultWorkDirectory();
    GridKernalContext kctx = new StandaloneGridKernalContext(log, null, null) {

        @Override
        protected IgniteConfiguration prepareIgniteConfiguration() {
            IgniteConfiguration cfg = super.prepareIgniteConfiguration();
            cfg.setDataStorageConfiguration(new DataStorageConfiguration().setDefaultDataRegionConfiguration(new DataRegionConfiguration().setPersistenceEnabled(persistenceEnabled)));
            return cfg;
        }

        @Override
        public PdsFoldersResolver pdsFolderResolver() {
            return new PdsFoldersResolver() {

                @Override
                public PdsFolderSettings resolveFolders() {
                    return new PdsFolderSettings(new File(dfltWorkDir), U.maskForFileName(""));
                }
            };
        }
    };
    return kctx;
}
Also used : DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridKernalContext(org.apache.ignite.internal.GridKernalContext) StandaloneGridKernalContext(org.apache.ignite.internal.processors.cache.persistence.wal.reader.StandaloneGridKernalContext) PdsFoldersResolver(org.apache.ignite.internal.processors.cache.persistence.filename.PdsFoldersResolver) PdsFolderSettings(org.apache.ignite.internal.processors.cache.persistence.filename.PdsFolderSettings) File(java.io.File) StandaloneGridKernalContext(org.apache.ignite.internal.processors.cache.persistence.wal.reader.StandaloneGridKernalContext)

Example 49 with GridKernalContext

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

the class LightweightCheckpointTest method testLightCheckpointAbleToStoreOnlyGivenDataRegion.

/**
 * 1. Start the one node with disabled WAL and with two caches.
 * 2. Disable default checkpoint.
 * 3. Create light checkpoint for one cache and configure checkpoint listener for it.
 * 4. Fill the both caches.
 * 5. Trigger the light checkpoint and wait for the finish.
 * 6. Stop the node and start it again.
 * 7. Expected: Cache which was checkpointed would have the all data meanwhile second cache would be empty.
 *
 * @throws Exception if fail.
 */
@Test
public void testLightCheckpointAbleToStoreOnlyGivenDataRegion() throws Exception {
    // given: One started node with default cache and cache which won't be checkpointed.
    IgniteEx ignite0 = startGrid(0);
    ignite0.cluster().active(true);
    IgniteCache<Integer, Object> checkpointedCache = ignite0.cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, Object> notCheckpointedCache = ignite0.cache(NOT_CHECKPOINTED_CACHE);
    GridKernalContext context = ignite0.context();
    GridCacheDatabaseSharedManager db = (GridCacheDatabaseSharedManager) (context.cache().context().database());
    waitForCondition(() -> !db.getCheckpointer().currentProgress().inProgress(), 10_000);
    // and: disable the default checkpoint.
    db.enableCheckpoints(false);
    DataRegion regionForCheckpoint = db.dataRegion(DFLT_DATA_REG_DEFAULT_NAME);
    // and: Create light checkpoint with only one region.
    LightweightCheckpointManager lightweightCheckpointManager = new LightweightCheckpointManager(context::log, context.igniteInstanceName(), "light-test-checkpoint", context.workersRegistry(), context.config().getDataStorageConfiguration(), () -> Arrays.asList(regionForCheckpoint), grpId -> getPageMemoryForCacheGroup(grpId, db, context), PageMemoryImpl.ThrottlingPolicy.CHECKPOINT_BUFFER_ONLY, context.cache().context().snapshot(), db.persistentStoreMetricsImpl(), context.longJvmPauseDetector(), context.failure(), context.cache());
    // and: Add checkpoint listener for DEFAULT_CACHE in order of storing the meta pages.
    lightweightCheckpointManager.addCheckpointListener((CheckpointListener) context.cache().cacheGroup(groupIdForCache(ignite0, DEFAULT_CACHE_NAME)).offheap(), regionForCheckpoint);
    lightweightCheckpointManager.start();
    // when: Fill the caches
    for (int j = 0; j < 1024; j++) {
        checkpointedCache.put(j, j);
        notCheckpointedCache.put(j, j);
    }
    // and: Trigger and wait for the checkpoint.
    lightweightCheckpointManager.forceCheckpoint("test", null).futureFor(CheckpointState.FINISHED).get();
    // and: Stop and start node.
    stopAllGrids();
    ignite0 = startGrid(0);
    ignite0.cluster().active(true);
    checkpointedCache = ignite0.cache(DEFAULT_CACHE_NAME);
    notCheckpointedCache = ignite0.cache(NOT_CHECKPOINTED_CACHE);
    // then: Checkpointed cache should have all data meanwhile uncheckpointed cache should be empty.
    for (int j = 1; j < 1024; j++) {
        assertEquals(j, checkpointedCache.get(j));
        assertNull(notCheckpointedCache.get(j));
    }
    GridCacheDatabaseSharedManager db2 = (GridCacheDatabaseSharedManager) (ignite0.context().cache().context().database());
    waitForCondition(() -> !db2.getCheckpointer().currentProgress().inProgress(), 10_000);
    String nodeFolderName = ignite0.context().pdsFolderResolver().resolveFolders().folderName();
    File cpMarkersDir = Paths.get(U.defaultWorkDirectory(), "db", nodeFolderName, "cp").toFile();
    // then: Expected only two pairs checkpoint markers - both from the start of node.
    assertEquals(4, cpMarkersDir.listFiles().length);
}
Also used : LightweightCheckpointManager(org.apache.ignite.internal.processors.cache.persistence.checkpoint.LightweightCheckpointManager) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) GridKernalContext(org.apache.ignite.internal.GridKernalContext) IgniteEx(org.apache.ignite.internal.IgniteEx) File(java.io.File) DataRegion(org.apache.ignite.internal.processors.cache.persistence.DataRegion) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 50 with GridKernalContext

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

the class IgniteWalIteratorSwitchSegmentTest method initiate.

/**
 * Initiate WAL manager.
 *
 * @param serVer WAL serializer version.
 * @param workDir Work directory path.
 * @return Tuple of WAL manager and WAL record serializer.
 * @throws IgniteCheckedException If some think failed.
 */
private T2<IgniteWriteAheadLogManager, RecordSerializer> initiate(int serVer, String workDir) throws IgniteCheckedException {
    GridKernalContext kctx = new StandaloneGridKernalContext(log, null, null) {

        @Override
        protected IgniteConfiguration prepareIgniteConfiguration() {
            IgniteConfiguration cfg = super.prepareIgniteConfiguration();
            cfg.setDataStorageConfiguration(new DataStorageConfiguration().setWalSegmentSize(SEGMENT_SIZE).setWalRecordIteratorBufferSize(SEGMENT_SIZE / 2).setWalMode(WALMode.FSYNC).setWalPath(workDir + WORK_SUB_DIR).setWalArchivePath(workDir + ARCHIVE_SUB_DIR).setFileIOFactory(new RandomAccessFileIOFactory()));
            cfg.setEventStorageSpi(new NoopEventStorageSpi());
            return cfg;
        }

        @Override
        public GridInternalSubscriptionProcessor internalSubscriptionProcessor() {
            return new GridInternalSubscriptionProcessor(this);
        }

        @Override
        public GridEventStorageManager event() {
            return new GridEventStorageManager(this);
        }
    };
    IgniteWriteAheadLogManager walMgr = new FileWriteAheadLogManager(kctx);
    GridTestUtils.setFieldValue(walMgr, "serializerVer", serVer);
    GridCacheSharedContext<?, ?> ctx = new GridCacheSharedContext<>(kctx, null, null, null, null, walMgr, new WalStateManager(kctx), new GridCacheDatabaseSharedManager(kctx), null, null, null, null, null, new GridCacheIoManager(), null, null, null, null, null, null, null);
    walMgr.start(ctx);
    walMgr.onActivate(kctx);
    walMgr.resumeLogging(null);
    RecordSerializer recordSerializer = new RecordSerializerFactoryImpl(ctx).createSerializer(walMgr.serializerVersion());
    return new T2<>(walMgr, recordSerializer);
}
Also used : GridEventStorageManager(org.apache.ignite.internal.managers.eventstorage.GridEventStorageManager) IgniteWriteAheadLogManager(org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager) GridInternalSubscriptionProcessor(org.apache.ignite.internal.processors.subscription.GridInternalSubscriptionProcessor) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) GridKernalContext(org.apache.ignite.internal.GridKernalContext) StandaloneGridKernalContext(org.apache.ignite.internal.processors.cache.persistence.wal.reader.StandaloneGridKernalContext) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) NoopEventStorageSpi(org.apache.ignite.spi.eventstorage.NoopEventStorageSpi) StandaloneGridKernalContext(org.apache.ignite.internal.processors.cache.persistence.wal.reader.StandaloneGridKernalContext) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) RecordSerializerFactoryImpl(org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializerFactoryImpl) WalStateManager(org.apache.ignite.internal.processors.cache.WalStateManager) FileWriteAheadLogManager(org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager) GridCacheIoManager(org.apache.ignite.internal.processors.cache.GridCacheIoManager) RecordSerializer(org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializer) T2(org.apache.ignite.internal.util.typedef.T2) RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)

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