Search in sources :

Example 16 with GroupPartitionId

use of org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId in project ignite by apache.

the class IgniteSnapshotManagerSelfTest method testSnapshotCreateLocalCopyPartitionFail.

/**
 * @throws Exception If fails.
 */
@Test
public void testSnapshotCreateLocalCopyPartitionFail() throws Exception {
    String err_msg = "Test. Fail to copy partition: ";
    IgniteEx ig = startGridWithCache(dfltCacheCfg, CACHE_KEYS_RANGE);
    Map<Integer, Set<Integer>> parts = new HashMap<>();
    parts.put(CU.cacheId(DEFAULT_CACHE_NAME), new HashSet<>(Collections.singletonList(0)));
    IgniteSnapshotManager mgr0 = snp(ig);
    IgniteInternalFuture<?> fut = startLocalSnapshotTask(ig.context().cache().context(), SNAPSHOT_NAME, parts, encryption, new DelegateSnapshotSender(log, mgr0.snapshotExecutorService(), mgr0.localSnapshotSenderFactory().apply(SNAPSHOT_NAME)) {

        @Override
        public void sendPart0(File part, String cacheDirName, GroupPartitionId pair, Long length) {
            if (pair.getPartitionId() == 0)
                throw new IgniteException(err_msg + pair);
            delegate.sendPart0(part, cacheDirName, pair, length);
        }
    });
    assertThrowsAnyCause(log, fut::get, IgniteException.class, err_msg);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) IgniteEx(org.apache.ignite.internal.IgniteEx) File(java.io.File) GroupPartitionId(org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId) Test(org.junit.Test)

Example 17 with GroupPartitionId

use of org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId in project ignite by apache.

the class IgniteSnapshotManagerSelfTest method testSnapshotLocalPartitionMultiCpWithLoad.

/**
 * Test that all partitions are copied successfully even after multiple checkpoints occur during
 * the long copy of cache partition files.
 *
 * Data consistency checked through a test node started right from snapshot directory and all values
 * read successes.
 *
 * @throws Exception If fails.
 */
@Test
public void testSnapshotLocalPartitionMultiCpWithLoad() throws Exception {
    int valMultiplier = 2;
    CountDownLatch slowCopy = new CountDownLatch(1);
    // Start grid node with data before each test.
    IgniteEx ig = startGridsWithCache(1, CACHE_KEYS_RANGE, key -> new Account(key, key), new CacheConfiguration<>(DEFAULT_CACHE_NAME));
    GridCacheSharedContext<?, ?> cctx = ig.context().cache().context();
    AtomicInteger cntr = new AtomicInteger();
    CountDownLatch ldrLatch = new CountDownLatch(1);
    IgniteSnapshotManager mgr = snp(ig);
    GridCacheDatabaseSharedManager db = (GridCacheDatabaseSharedManager) cctx.database();
    IgniteInternalFuture<?> loadFut = GridTestUtils.runMultiThreadedAsync(() -> {
        try {
            U.await(ldrLatch);
            while (!Thread.currentThread().isInterrupted()) ig.cache(DEFAULT_CACHE_NAME).put(cntr.incrementAndGet(), new Account(cntr.incrementAndGet(), cntr.incrementAndGet()));
        } catch (IgniteInterruptedCheckedException e) {
            log.warning("Loader has been interrupted", e);
        }
    }, 5, "cache-loader-");
    // Register task but not schedule it on the checkpoint.
    SnapshotFutureTask snpFutTask = (SnapshotFutureTask) mgr.registerSnapshotTask(SNAPSHOT_NAME, cctx.localNodeId(), F.asMap(CU.cacheId(DEFAULT_CACHE_NAME), null), encryption, new DelegateSnapshotSender(log, mgr.snapshotExecutorService(), mgr.localSnapshotSenderFactory().apply(SNAPSHOT_NAME)) {

        @Override
        public void sendPart0(File part, String cacheDirName, GroupPartitionId pair, Long length) {
            try {
                U.await(slowCopy);
                delegate.sendPart0(part, cacheDirName, pair, length);
            } catch (IgniteInterruptedCheckedException e) {
                throw new IgniteException(e);
            }
        }
    });
    db.addCheckpointListener(new CheckpointListener() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void beforeCheckpointBegin(Context ctx) {
        // No-op.
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void onMarkCheckpointBegin(Context ctx) {
        // No-op.
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void onCheckpointBegin(Context ctx) {
            Map<Integer, Set<Integer>> processed = GridTestUtils.getFieldValue(snpFutTask, SnapshotFutureTask.class, "processed");
            if (!processed.isEmpty())
                ldrLatch.countDown();
        }
    });
    try {
        snpFutTask.start();
        // Change data before snapshot creation which must be included into it with correct value multiplier.
        for (int i = 0; i < CACHE_KEYS_RANGE; i++) ig.cache(DEFAULT_CACHE_NAME).put(i, new Account(i, valMultiplier * i));
        // Snapshot is still in the INIT state. beforeCheckpoint has been skipped
        // due to checkpoint already running and we need to schedule the next one
        // right after current will be completed.
        cctx.database().forceCheckpoint(String.format(CP_SNAPSHOT_REASON, SNAPSHOT_NAME));
        snpFutTask.started().get();
        db.forceCheckpoint("snapshot is ready to be created").futureFor(CheckpointState.MARKER_STORED_TO_DISK).get();
        // Change data after snapshot.
        for (int i = 0; i < CACHE_KEYS_RANGE; i++) ig.cache(DEFAULT_CACHE_NAME).put(i, new Account(i, 3 * i));
        // Snapshot on the next checkpoint must copy page to delta file before write it to a partition.
        forceCheckpoint(ig);
        slowCopy.countDown();
        snpFutTask.get();
    } finally {
        loadFut.cancel();
    }
    // Now can stop the node and check created snapshots.
    stopGrid(0);
    cleanPersistenceDir(ig.name());
    // Start Ignite instance from snapshot directory.
    IgniteEx ig2 = startGridsFromSnapshot(1, SNAPSHOT_NAME);
    for (int i = 0; i < CACHE_KEYS_RANGE; i++) {
        assertEquals("snapshot data consistency violation [key=" + i + ']', i * valMultiplier, ((Account) ig2.cache(DEFAULT_CACHE_NAME).get(i)).balance);
    }
}
Also used : GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) CheckpointListener(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) IgniteEx(org.apache.ignite.internal.IgniteEx) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) GroupPartitionId(org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId) Test(org.junit.Test)

Example 18 with GroupPartitionId

use of org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId in project ignite by apache.

the class IgniteSnapshotRestoreFromRemoteTest method assertPartitionsDuplicates.

/**
 */
private static void assertPartitionsDuplicates(List<Object> msgs) {
    List<GroupPartitionId> all = new ArrayList<>();
    for (Object o : msgs) {
        SnapshotFilesRequestMessage msg0 = (SnapshotFilesRequestMessage) o;
        Map<Integer, Set<Integer>> parts = msg0.parts();
        for (Map.Entry<Integer, Set<Integer>> e : parts.entrySet()) {
            for (Integer partId : e.getValue()) all.add(new GroupPartitionId(e.getKey(), partId));
        }
    }
    assertEquals(all.size(), new HashSet<>(all).size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Map(java.util.Map) GroupPartitionId(org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId) HashSet(java.util.HashSet)

Example 19 with GroupPartitionId

use of org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId in project ignite by apache.

the class IgniteSnapshotRemoteRequestTest method testRemoteRequestedInitiatorNodeLeft.

/**
 * @throws Exception If fails.
 */
@Test
public void testRemoteRequestedInitiatorNodeLeft() throws Exception {
    IgniteEx ignite = startGridsWithCache(2, CACHE_KEYS_RANGE, valueBuilder(), dfltCacheCfg);
    ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get(TIMEOUT);
    awaitPartitionMapExchange();
    IgniteSnapshotManager mgr1 = snp(grid(1));
    UUID rmtNodeId = grid(1).localNode().id();
    UUID locNodeId = grid(0).localNode().id();
    Map<Integer, Set<Integer>> parts = owningParts(ignite, CU.cacheId(DEFAULT_CACHE_NAME), rmtNodeId);
    CountDownLatch sndLatch = new CountDownLatch(1);
    mgr1.remoteSnapshotSenderFactory(new BiFunction<String, UUID, SnapshotSender>() {

        @Override
        public SnapshotSender apply(String s, UUID uuid) {
            return new DelegateSnapshotSender(log, mgr1.snapshotExecutorService(), mgr1.remoteSnapshotSenderFactory(s, uuid)) {

                @Override
                public void sendPart0(File part, String cacheDirName, GroupPartitionId pair, Long length) {
                    if (partId(part.getName()) > 0) {
                        try {
                            sndLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
                        } catch (Exception e) {
                            throw new IgniteException(e);
                        }
                    }
                    super.sendPart0(part, cacheDirName, pair, length);
                }
            };
        }
    });
    snp(ignite).requestRemoteSnapshotFiles(grid(1).localNode().id(), SNAPSHOT_NAME, parts, () -> false, (part, t) -> {
    });
    IgniteInternalFuture<?>[] futs = new IgniteInternalFuture[1];
    assertTrue(waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            IgniteInternalFuture<?> snpFut = snp(grid(1)).lastScheduledSnapshotResponseRemoteTask(locNodeId);
            if (snpFut == null)
                return false;
            else {
                futs[0] = snpFut;
                return true;
            }
        }
    }, 5_000L));
    stopGrid(0);
    sndLatch.countDown();
    GridTestUtils.assertThrowsAnyCause(log, () -> futs[0].get(TIMEOUT), ClusterTopologyCheckedException.class, null);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) TransmissionCancelledException(org.apache.ignite.internal.managers.communication.TransmissionCancelledException) IgniteException(org.apache.ignite.IgniteException) IgniteEx(org.apache.ignite.internal.IgniteEx) UUID(java.util.UUID) File(java.io.File) GroupPartitionId(org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId) Test(org.junit.Test)

Aggregations

GroupPartitionId (org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId)19 Map (java.util.Map)10 IgniteException (org.apache.ignite.IgniteException)10 File (java.io.File)9 HashMap (java.util.HashMap)9 Set (java.util.Set)7 UUID (java.util.UUID)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 IgniteEx (org.apache.ignite.internal.IgniteEx)7 HashSet (java.util.HashSet)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 Collections (java.util.Collections)5 List (java.util.List)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 GridCacheSharedContext (org.apache.ignite.internal.processors.cache.GridCacheSharedContext)4