Search in sources :

Example 1 with MigrationEndpoint

use of com.hazelcast.internal.partition.MigrationEndpoint in project hazelcast by hazelcast.

the class MigrationManager method finalizeMigration.

/**
 * Finalizes a migration that has finished with {@link MigrationStatus#SUCCESS}
 * or {@link MigrationStatus#FAILED} by invoking {@link FinalizeMigrationOperation}
 * locally if this is the source or destination. The finalization is asynchronous
 * and there might be other ongoing migration finalizations.
 * <p>
 * It will also cleanup the migration state by removing the active migration and
 * clearing the migration flag on the partition owner.
 * <p>
 * This method should not be called on a node which is not the source, destination
 * or partition owner for this migration.
 *
 * @param migrationInfo the migration to be finalized
 */
void finalizeMigration(MigrationInfo migrationInfo) {
    try {
        PartitionReplica localReplica = PartitionReplica.from(node.getLocalMember());
        int partitionId = migrationInfo.getPartitionId();
        boolean source = localReplica.equals(migrationInfo.getSource());
        boolean destination = localReplica.equals(migrationInfo.getDestination());
        assert migrationInfo.getStatus() == MigrationStatus.SUCCESS || migrationInfo.getStatus() == MigrationStatus.FAILED : "Invalid migration: " + migrationInfo;
        if (source || destination) {
            boolean success = migrationInfo.getStatus() == MigrationStatus.SUCCESS;
            MigrationParticipant participant = source ? MigrationParticipant.SOURCE : MigrationParticipant.DESTINATION;
            if (success) {
                migrationInterceptor.onMigrationCommit(participant, migrationInfo);
            } else {
                migrationInterceptor.onMigrationRollback(participant, migrationInfo);
            }
            MigrationEndpoint endpoint = source ? MigrationEndpoint.SOURCE : MigrationEndpoint.DESTINATION;
            FinalizeMigrationOperation op = new FinalizeMigrationOperation(migrationInfo, endpoint, success);
            op.setPartitionId(partitionId).setNodeEngine(nodeEngine).setValidateTarget(false).setService(partitionService);
            registerFinalizingMigration(migrationInfo);
            OperationServiceImpl operationService = nodeEngine.getOperationService();
            if (logger.isFineEnabled()) {
                logger.fine("Finalizing " + migrationInfo);
            }
            if (operationService.isRunAllowed(op)) {
                // When migration finalization is triggered by subsequent migrations
                // on partition thread, finalization may run directly on calling thread.
                // Otherwise, if previously completed migration and newly submitted migration
                // are on the same partition, then finalizing previous migration later will
                // break the order, hence safety.
                operationService.run(op);
            } else {
                operationService.execute(op);
            }
            removeActiveMigration(migrationInfo);
        } else {
            PartitionReplica partitionOwner = partitionStateManager.getPartitionImpl(partitionId).getOwnerReplicaOrNull();
            if (localReplica.equals(partitionOwner)) {
                // This is the primary owner of the partition and the migration was a backup replica migration.
                // In this case, primary owner has nothing to do anymore,
                // just remove the active migration and clear the migrating flag.
                removeActiveMigration(migrationInfo);
                partitionStateManager.clearMigratingFlag(partitionId);
            } else {
                logger.severe("Failed to finalize migration because " + localReplica + " is not a participant of the migration: " + migrationInfo);
            }
        }
    } catch (Exception e) {
        logger.warning(e);
    }
}
Also used : PartitionReplica(com.hazelcast.internal.partition.PartitionReplica) FinalizeMigrationOperation(com.hazelcast.internal.partition.operation.FinalizeMigrationOperation) MigrationEndpoint(com.hazelcast.internal.partition.MigrationEndpoint) MigrationEndpoint(com.hazelcast.internal.partition.MigrationEndpoint) MigrationParticipant(com.hazelcast.internal.partition.impl.MigrationInterceptor.MigrationParticipant) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) PartitionStateVersionMismatchException(com.hazelcast.internal.partition.PartitionStateVersionMismatchException) TargetNotMemberException(com.hazelcast.spi.exception.TargetNotMemberException) OperationTimeoutException(com.hazelcast.core.OperationTimeoutException) MemberLeftException(com.hazelcast.core.MemberLeftException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with MigrationEndpoint

use of com.hazelcast.internal.partition.MigrationEndpoint in project hazelcast by hazelcast.

the class MigrationEndpointTest method testReadFromSource.

@Test
public void testReadFromSource() throws Exception {
    when(dataInput.readByte()).thenReturn((byte) 0);
    MigrationEndpoint actual = MigrationEndpoint.readFrom(dataInput);
    assertEquals(SOURCE, actual);
}
Also used : MigrationEndpoint(com.hazelcast.internal.partition.MigrationEndpoint) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with MigrationEndpoint

use of com.hazelcast.internal.partition.MigrationEndpoint in project hazelcast by hazelcast.

the class MigrationEndpointTest method testReadFromDestination.

@Test
public void testReadFromDestination() throws Exception {
    when(dataInput.readByte()).thenReturn((byte) 1);
    MigrationEndpoint actual = MigrationEndpoint.readFrom(dataInput);
    assertEquals(DESTINATION, actual);
}
Also used : MigrationEndpoint(com.hazelcast.internal.partition.MigrationEndpoint) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

MigrationEndpoint (com.hazelcast.internal.partition.MigrationEndpoint)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 Test (org.junit.Test)2 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)1 MemberLeftException (com.hazelcast.core.MemberLeftException)1 OperationTimeoutException (com.hazelcast.core.OperationTimeoutException)1 PartitionReplica (com.hazelcast.internal.partition.PartitionReplica)1 PartitionStateVersionMismatchException (com.hazelcast.internal.partition.PartitionStateVersionMismatchException)1 MigrationParticipant (com.hazelcast.internal.partition.impl.MigrationInterceptor.MigrationParticipant)1 FinalizeMigrationOperation (com.hazelcast.internal.partition.operation.FinalizeMigrationOperation)1 TargetNotMemberException (com.hazelcast.spi.exception.TargetNotMemberException)1 OperationServiceImpl (com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl)1 ExecutionException (java.util.concurrent.ExecutionException)1