Search in sources :

Example 1 with DataSerializerHook

use of com.hazelcast.internal.serialization.DataSerializerHook in project hazelcast by hazelcast.

the class SplitBrainProtectionOperationTest method assertThatInternalOperationsAreNotSplitBrainProtectionDependent.

@Test
public void assertThatInternalOperationsAreNotSplitBrainProtectionDependent() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Iterator<DataSerializerHook> hooks = ServiceLoader.iterator(DataSerializerHook.class, FACTORY_ID, classLoader);
    while (hooks.hasNext()) {
        DataSerializerHook hook = hooks.next();
        String simpleClassName = hook.getClass().getName();
        LOGGER.info("Testing " + simpleClassName + "...");
        DataSerializableFactory factory = hook.createFactory();
        int typeId = 0;
        while (true) {
            IdentifiedDataSerializable ids = createIDS(factory, typeId++);
            if (ids == null) {
                break;
            }
            Class<? extends IdentifiedDataSerializable> clazz = ids.getClass();
            String className = clazz.getName();
            String name = lowerCaseInternal(clazz.getSimpleName());
            LOGGER.info(clazz.getSimpleName());
            if (matches(NO_SPLIT_BRAIN_PROTECTION_PACKAGES, className)) {
                continue;
            }
            boolean shouldBeMutatingOperation = false;
            boolean shouldBeReadonlyOperation = false;
            if (!(ids instanceof Operation) || ids instanceof UrgentSystemOperation || matches(INTERNAL_CLASS_NAMES, name) || matches(INTERNAL_PACKAGES, className)) {
                // no, urgent, internal or no split brain protection operations
                shouldBeMutatingOperation = false;
                shouldBeReadonlyOperation = false;
            } else if (ids instanceof AbstractLockOperation || matches(MUTATING_CLASS_NAMES, name)) {
                // mutating operations
                if (isForcedReadOnly(className)) {
                    shouldBeReadonlyOperation = true;
                } else {
                    shouldBeMutatingOperation = true;
                }
            } else if (matches(READONLY_CLASS_NAMES, name)) {
                // read-only operations
                shouldBeReadonlyOperation = true;
            } else {
                fail(className + " doesn't match any criteria!");
            }
            // asserts
            if (ids instanceof MutatingOperation) {
                if (!shouldBeMutatingOperation) {
                    fail(className + " implements " + MUTATING_OP_NAME);
                }
            } else if (shouldBeMutatingOperation) {
                fail(className + " should implement " + MUTATING_OP_NAME);
            }
            if (ids instanceof ReadonlyOperation) {
                if (!shouldBeReadonlyOperation) {
                    fail(className + " implements " + READ_ONLY_OP_NAME);
                }
            } else if (shouldBeReadonlyOperation) {
                fail(className + " should implement " + READ_ONLY_OP_NAME);
            }
        }
    }
}
Also used : IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) IsPartitionLoadedOperation(com.hazelcast.map.impl.operation.IsPartitionLoadedOperation) MutatingOperation(com.hazelcast.spi.impl.operationservice.MutatingOperation) TriggerLoadIfNeededOperation(com.hazelcast.map.impl.operation.TriggerLoadIfNeededOperation) Operation(com.hazelcast.spi.impl.operationservice.Operation) ReadonlyOperation(com.hazelcast.spi.impl.operationservice.ReadonlyOperation) MapEventJournalSubscribeOperation(com.hazelcast.map.impl.journal.MapEventJournalSubscribeOperation) IsKeyLoadFinishedOperation(com.hazelcast.map.impl.operation.IsKeyLoadFinishedOperation) NotifyMapFlushOperation(com.hazelcast.map.impl.operation.NotifyMapFlushOperation) UrgentSystemOperation(com.hazelcast.spi.impl.operationservice.UrgentSystemOperation) AwaitMapFlushOperation(com.hazelcast.map.impl.operation.AwaitMapFlushOperation) AbstractLockOperation(com.hazelcast.internal.locksupport.operations.AbstractLockOperation) AbstractLockOperation(com.hazelcast.internal.locksupport.operations.AbstractLockOperation) MutatingOperation(com.hazelcast.spi.impl.operationservice.MutatingOperation) DataSerializableFactory(com.hazelcast.nio.serialization.DataSerializableFactory) ReadonlyOperation(com.hazelcast.spi.impl.operationservice.ReadonlyOperation) DataSerializerHook(com.hazelcast.internal.serialization.DataSerializerHook) UrgentSystemOperation(com.hazelcast.spi.impl.operationservice.UrgentSystemOperation) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with DataSerializerHook

use of com.hazelcast.internal.serialization.DataSerializerHook in project hazelcast by hazelcast.

the class ServiceLoaderTest method testSkipHookLoadedByDifferentClassloader.

@Test
public void testSkipHookLoadedByDifferentClassloader() {
    Class<?> otherInterface = newInterface(SpiDataSerializerHook.class.getName());
    ClassLoader otherClassloader = otherInterface.getClassLoader();
    Class<?> otherHook = newClassImplementingInterface("com.hazelcast.internal.serialization.DifferentHook", otherInterface, otherClassloader);
    // otherHook is loaded by other classloader -> it should be skipped
    ServiceLoader.ServiceDefinition definition1 = new ServiceLoader.ServiceDefinition(otherHook.getName(), otherClassloader);
    // this hook should be loaded
    ServiceLoader.ServiceDefinition definition2 = new ServiceLoader.ServiceDefinition(SpiDataSerializerHook.class.getName(), SpiDataSerializerHook.class.getClassLoader());
    Set<ServiceLoader.ServiceDefinition> definitions = setOf(definition1, definition2);
    ServiceLoader.ClassIterator<DataSerializerHook> iterator = new ServiceLoader.ClassIterator<>(definitions, DataSerializerHook.class);
    assertTrue(iterator.hasNext());
    Class<DataSerializerHook> hook = iterator.next();
    assertEquals(SpiDataSerializerHook.class, hook);
    assertFalse(iterator.hasNext());
}
Also used : SpiDataSerializerHook(com.hazelcast.spi.impl.SpiDataSerializerHook) URLClassLoader(java.net.URLClassLoader) SpiDataSerializerHook(com.hazelcast.spi.impl.SpiDataSerializerHook) DataSerializerHook(com.hazelcast.internal.serialization.DataSerializerHook) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with DataSerializerHook

use of com.hazelcast.internal.serialization.DataSerializerHook in project hazelcast by hazelcast.

the class DataSerializableConventionsTest method test_identifiedDataSerializables_areInstancesOfSameClass_whenConstructedFromFactory.

/**
 * Locates {@link IdentifiedDataSerializable} classes via reflection, iterates over them and asserts an instance created by
 * a factory is of the same classes as an instance created via reflection.
 */
@Test
public void test_identifiedDataSerializables_areInstancesOfSameClass_whenConstructedFromFactory() throws Exception {
    Set<Class<? extends DataSerializerHook>> dsHooks = REFLECTIONS.getSubTypesOf(DataSerializerHook.class);
    Map<Integer, DataSerializableFactory> factories = new HashMap<Integer, DataSerializableFactory>();
    for (Class<? extends DataSerializerHook> hookClass : dsHooks) {
        DataSerializerHook dsHook = hookClass.newInstance();
        DataSerializableFactory factory = dsHook.createFactory();
        factories.put(dsHook.getFactoryId(), factory);
    }
    Set<Class<? extends IdentifiedDataSerializable>> identifiedDataSerializables = getIDSConcreteClasses();
    for (Class<? extends IdentifiedDataSerializable> klass : identifiedDataSerializables) {
        if (AbstractLocalOperation.class.isAssignableFrom(klass)) {
            continue;
        }
        if (isReadOnlyConfig(klass)) {
            continue;
        }
        // wrap all of this in try-catch, as it is legitimate for some classes to throw UnsupportedOperationException
        try {
            Constructor<? extends IdentifiedDataSerializable> ctor = klass.getDeclaredConstructor();
            ctor.setAccessible(true);
            IdentifiedDataSerializable instance = ctor.newInstance();
            int factoryId = instance.getFactoryId();
            int typeId = instance.getClassId();
            if (!factories.containsKey(factoryId)) {
                fail("Factory with ID " + factoryId + " declared in " + klass + " not found." + " Is such a factory ID registered?");
            }
            IdentifiedDataSerializable instanceFromFactory = factories.get(factoryId).create(typeId);
            assertNotNull("Factory with ID " + factoryId + " returned null for type with ID " + typeId, instanceFromFactory);
            assertTrue("Factory with ID " + factoryId + " instantiated an object of " + instanceFromFactory.getClass() + " while expected type was " + instance.getClass(), instanceFromFactory.getClass().equals(instance.getClass()));
        } catch (UnsupportedOperationException ignored) {
        // expected from local operation classes not meant for serialization
        }
    }
}
Also used : IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) HashMap(java.util.HashMap) DataSerializableFactory(com.hazelcast.nio.serialization.DataSerializableFactory) DataSerializerHook(com.hazelcast.internal.serialization.DataSerializerHook) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

DataSerializerHook (com.hazelcast.internal.serialization.DataSerializerHook)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 DataSerializableFactory (com.hazelcast.nio.serialization.DataSerializableFactory)2 IdentifiedDataSerializable (com.hazelcast.nio.serialization.IdentifiedDataSerializable)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 AbstractLockOperation (com.hazelcast.internal.locksupport.operations.AbstractLockOperation)1 MapEventJournalSubscribeOperation (com.hazelcast.map.impl.journal.MapEventJournalSubscribeOperation)1 AwaitMapFlushOperation (com.hazelcast.map.impl.operation.AwaitMapFlushOperation)1 IsKeyLoadFinishedOperation (com.hazelcast.map.impl.operation.IsKeyLoadFinishedOperation)1 IsPartitionLoadedOperation (com.hazelcast.map.impl.operation.IsPartitionLoadedOperation)1 NotifyMapFlushOperation (com.hazelcast.map.impl.operation.NotifyMapFlushOperation)1 TriggerLoadIfNeededOperation (com.hazelcast.map.impl.operation.TriggerLoadIfNeededOperation)1 SpiDataSerializerHook (com.hazelcast.spi.impl.SpiDataSerializerHook)1 MutatingOperation (com.hazelcast.spi.impl.operationservice.MutatingOperation)1 Operation (com.hazelcast.spi.impl.operationservice.Operation)1 ReadonlyOperation (com.hazelcast.spi.impl.operationservice.ReadonlyOperation)1 UrgentSystemOperation (com.hazelcast.spi.impl.operationservice.UrgentSystemOperation)1 URLClassLoader (java.net.URLClassLoader)1 HashMap (java.util.HashMap)1