Search in sources :

Example 6 with MigrationInterceptor

use of com.hazelcast.internal.partition.impl.MigrationInterceptor in project hazelcast by hazelcast.

the class PartitionMigrationListenerTest method testMigrationStats_whenMigrationProcessRestarts.

@Test
public void testMigrationStats_whenMigrationProcessRestarts() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
    Config config = new Config();
    int partitionCount = 100;
    config.setProperty(ClusterProperty.PARTITION_COUNT.getName(), String.valueOf(partitionCount));
    config.setProperty(ClusterProperty.PARTITION_MAX_PARALLEL_MIGRATIONS.getName(), String.valueOf(1));
    HazelcastInstance hz1 = factory.newHazelcastInstance(config);
    warmUpPartitions(hz1);
    InternalPartitionServiceImpl partitionService = (InternalPartitionServiceImpl) getPartitionService(hz1);
    AtomicReference<HazelcastInstance> newInstanceRef = new AtomicReference<>();
    partitionService.setMigrationInterceptor(new MigrationInterceptor() {

        @Override
        public void onMigrationComplete(MigrationParticipant participant, MigrationInfo migration, boolean success) {
            MigrationStats stats = partitionService.getMigrationManager().getStats();
            if (stats.getRemainingMigrations() < 50) {
                // start a new member to restart migrations
                partitionService.resetMigrationInterceptor();
                HazelcastInstance hz = factory.newHazelcastInstance(config);
                assertClusterSize(3, hz);
                newInstanceRef.set(hz);
            }
        }
    });
    EventCollectingMigrationListener listener = new EventCollectingMigrationListener();
    hz1.getPartitionService().addMigrationListener(listener);
    // Change to NO_MIGRATION to prevent repartitioning
    // before 2nd member started and ready.
    hz1.getCluster().changeClusterState(ClusterState.NO_MIGRATION);
    // trigger migrations
    HazelcastInstance hz2 = factory.newHazelcastInstance(config);
    // Back to ACTIVE
    changeClusterStateEventually(hz2, ClusterState.ACTIVE);
    // await until 3rd member joins
    assertClusterSizeEventually(3, hz1);
    assertTrueEventually(() -> assertNotNull(newInstanceRef.get()));
    List<MigrationEventsPack> eventsPackList = listener.ensureAndGetEventPacks(2);
    // 1st migration process, which finishes without completing all migration tasks
    MigrationEventsPack firstEventsPack = eventsPackList.get(0);
    assertMigrationProcessCompleted(firstEventsPack);
    MigrationState migrationResult = firstEventsPack.migrationProcessCompleted;
    assertThat(migrationResult.getCompletedMigrations(), lessThan(migrationResult.getPlannedMigrations()));
    assertThat(migrationResult.getRemainingMigrations(), greaterThan(0));
    assertMigrationEventsConsistentWithResult(firstEventsPack);
    // 2nd migration process finishes by consuming all migration tasks
    MigrationEventsPack secondEventsPack = eventsPackList.get(1);
    if (secondEventsPack.migrationProcessCompleted.getCompletedMigrations() == 1 && !secondEventsPack.migrationsCompleted.get(0).isSuccess()) {
        // There is a failed migration process
        // because migrations restarted before 3rd member is ready.
        // This migration process is failed immediately
        // and we expect a third migration process.
        secondEventsPack = listener.ensureAndGetEventPacks(3).get(2);
    }
    assertMigrationProcessCompleted(secondEventsPack);
    assertMigrationProcessEventsConsistent(secondEventsPack);
    assertMigrationEventsConsistentWithResult(secondEventsPack);
}
Also used : MigrationInfo(com.hazelcast.internal.partition.MigrationInfo) Config(com.hazelcast.config.Config) InternalPartitionServiceImpl(com.hazelcast.internal.partition.impl.InternalPartitionServiceImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MigrationStats(com.hazelcast.internal.partition.impl.MigrationStats) MigrationInterceptor(com.hazelcast.internal.partition.impl.MigrationInterceptor) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

MigrationInterceptor (com.hazelcast.internal.partition.impl.MigrationInterceptor)6 InternalPartitionServiceImpl (com.hazelcast.internal.partition.impl.InternalPartitionServiceImpl)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 Config (com.hazelcast.config.Config)2 MigrationInfo (com.hazelcast.internal.partition.MigrationInfo)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ClientListener (com.hazelcast.client.ClientListener)1 CancellationAwareTask (com.hazelcast.client.test.executor.tasks.CancellationAwareTask)1 MembershipListener (com.hazelcast.cluster.MembershipListener)1 ListenerConfig (com.hazelcast.config.ListenerConfig)1 DistributedObjectListener (com.hazelcast.core.DistributedObjectListener)1 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)1 IExecutorService (com.hazelcast.core.IExecutorService)1 LifecycleListener (com.hazelcast.core.LifecycleListener)1 CPGroupAvailabilityListener (com.hazelcast.cp.event.CPGroupAvailabilityListener)1 CPMembershipListener (com.hazelcast.cp.event.CPMembershipListener)1 MigrationStats (com.hazelcast.internal.partition.impl.MigrationStats)1