Search in sources :

Example 76 with NodeEngine

use of com.hazelcast.spi.impl.NodeEngine in project hazelcast by hazelcast.

the class EntryProcessorOffloadableTest method testHeartBeatsComingWhenEntryProcessorOffloaded.

/**
 * <pre>
 * Given: Operation heartbeats are sent four times per {@link ClusterProperty#OPERATION_CALL_TIMEOUT_MILLIS}
 *        (see {@link InvocationMonitor#getHeartbeatBroadcastPeriodMillis()})
 * When: An offloaded EntryProcessor takes a long time to run.
 * Then: Heartbeats are still coming while the task is offloaded.
 * </pre>
 */
@Test
@Category(SlowTest.class)
public void testHeartBeatsComingWhenEntryProcessorOffloaded() {
    /* Shut down the cluster since we want to use a different
         * OPERATION_CALL_TIMEOUT_MILLIS value in this test. */
    shutdownNodeFactory();
    int heartbeatsIntervalSec = 15;
    Config config = getConfig();
    config.getProperties().setProperty(ClusterProperty.OPERATION_CALL_TIMEOUT_MILLIS.getName(), String.valueOf(heartbeatsIntervalSec * 4 * 1000));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    instances = factory.newInstances(config);
    final String key = generateKeyOwnedBy(instances[1]);
    String offloadableStartTimeRefName = "offloadableStartTimeRefName";
    String exitLatchName = "exitLatchName";
    IAtomicLong offloadableStartedTimeRef = instances[0].getCPSubsystem().getAtomicLong(offloadableStartTimeRefName);
    ICountDownLatch exitLatch = instances[0].getCPSubsystem().getCountDownLatch(exitLatchName);
    exitLatch.trySetCount(1);
    TimestampedSimpleValue givenValue = new TimestampedSimpleValue(1);
    final IMap<String, TimestampedSimpleValue> map = instances[0].getMap(MAP_NAME);
    map.put(key, givenValue);
    final Address instance1Address = instances[1].getCluster().getLocalMember().getAddress();
    final List<Long> heartBeatTimestamps = new LinkedList<>();
    Thread hbMonitorThread = new Thread(() -> {
        NodeEngine nodeEngine = getNodeEngineImpl(instances[0]);
        OperationServiceImpl osImpl = (OperationServiceImpl) nodeEngine.getOperationService();
        Map<Address, AtomicLong> heartBeats = osImpl.getInvocationMonitor().getHeartbeatPerMember();
        long lastbeat = Long.MIN_VALUE;
        while (!Thread.currentThread().isInterrupted()) {
            AtomicLong timestamp = heartBeats.get(instance1Address);
            if (timestamp != null) {
                long newlastbeat = timestamp.get();
                if (lastbeat != newlastbeat) {
                    lastbeat = newlastbeat;
                    long offloadableStartTime = offloadableStartedTimeRef.get();
                    if (offloadableStartTime != 0 && offloadableStartTime < newlastbeat) {
                        heartBeatTimestamps.add(newlastbeat);
                        exitLatch.countDown();
                    }
                }
            }
            HazelcastTestSupport.sleepMillis(100);
        }
    });
    final int secondsToRun = 55;
    try {
        hbMonitorThread.start();
        map.executeOnKey(key, new TimeConsumingOffloadableTask(secondsToRun, offloadableStartTimeRefName, exitLatchName));
    } finally {
        hbMonitorThread.interrupt();
    }
    int heartBeatCount = 0;
    TimestampedSimpleValue updatedValue = map.get(key);
    for (long heartBeatTimestamp : heartBeatTimestamps) {
        if (heartBeatTimestamp > updatedValue.processStart && heartBeatTimestamp < updatedValue.processEnd) {
            heartBeatCount++;
        }
    }
    assertTrue("Heartbeats should be received while offloadable entry processor is running. " + "Observed: " + heartBeatTimestamps + " EP start: " + updatedValue.processStart + " end: " + updatedValue.processEnd, heartBeatCount > 0);
}
Also used : Address(com.hazelcast.cluster.Address) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) LinkedList(java.util.LinkedList) NodeEngine(com.hazelcast.spi.impl.NodeEngine) IAtomicLong(com.hazelcast.cp.IAtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) IAtomicLong(com.hazelcast.cp.IAtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) IAtomicLong(com.hazelcast.cp.IAtomicLong) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl) Category(org.junit.experimental.categories.Category) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 77 with NodeEngine

use of com.hazelcast.spi.impl.NodeEngine in project hazelcast by hazelcast.

the class EvictionMaxSizePolicyTest method setTestSizeEstimator.

public static void setTestSizeEstimator(IMap map, final long oneEntryHeapCostInBytes) {
    final MapProxyImpl mapProxy = (MapProxyImpl) map;
    final MapService mapService = (MapService) mapProxy.getService();
    final MapServiceContext mapServiceContext = mapService.getMapServiceContext();
    final NodeEngine nodeEngine = mapServiceContext.getNodeEngine();
    final IPartitionService partitionService = nodeEngine.getPartitionService();
    for (int i = 0; i < partitionService.getPartitionCount(); i++) {
        final Address owner = partitionService.getPartitionOwner(i);
        if (nodeEngine.getThisAddress().equals(owner)) {
            final PartitionContainer container = mapServiceContext.getPartitionContainer(i);
            if (container == null) {
                continue;
            }
            final RecordStore recordStore = container.getRecordStore(map.getName());
            final DefaultRecordStore defaultRecordStore = (DefaultRecordStore) recordStore;
            defaultRecordStore.setSizeEstimator(new EntryCostEstimator() {

                long size;

                @Override
                public long getEstimate() {
                    return size;
                }

                @Override
                public void adjustEstimateBy(long size) {
                    this.size += size;
                }

                @Override
                public long calculateValueCost(Object record) {
                    if (record == null) {
                        return 0L;
                    }
                    return oneEntryHeapCostInBytes;
                }

                @Override
                public long calculateEntryCost(Object key, Object record) {
                    if (record == null) {
                        return 0L;
                    }
                    return 2 * oneEntryHeapCostInBytes;
                }

                @Override
                public void reset() {
                    size = 0;
                }
            });
        }
    }
}
Also used : Address(com.hazelcast.cluster.Address) PartitionContainer(com.hazelcast.map.impl.PartitionContainer) IPartitionService(com.hazelcast.internal.partition.IPartitionService) EntryCostEstimator(com.hazelcast.map.impl.EntryCostEstimator) MapServiceContext(com.hazelcast.map.impl.MapServiceContext) NodeEngine(com.hazelcast.spi.impl.NodeEngine) MapProxyImpl(com.hazelcast.map.impl.proxy.MapProxyImpl) DefaultRecordStore(com.hazelcast.map.impl.recordstore.DefaultRecordStore) RecordStore(com.hazelcast.map.impl.recordstore.RecordStore) MapService(com.hazelcast.map.impl.MapService) DefaultRecordStore(com.hazelcast.map.impl.recordstore.DefaultRecordStore)

Example 78 with NodeEngine

use of com.hazelcast.spi.impl.NodeEngine in project hazelcast by hazelcast.

the class MapMergePolicyQuickTest method testPutIfAbsentMapMergePolicy.

@Test
public void testPutIfAbsentMapMergePolicy() {
    HazelcastInstance instance = createHazelcastInstance(getConfig());
    String name = randomString();
    IMap<String, String> map = instance.getMap(name);
    MapServiceContext mapServiceContext = getMapServiceContext(instance);
    Data dataKey = mapServiceContext.toData("key");
    Data dataValue = mapServiceContext.toData("value1");
    Data dataValue2 = mapServiceContext.toData("value2");
    RecordStore recordStore = mapServiceContext.getRecordStore(getPartitionId(instance, "key"), name);
    recordStore.beforeOperation();
    NodeEngine nodeEngine = mapServiceContext.getNodeEngine();
    SplitBrainMergePolicyProvider mergePolicyProvider = nodeEngine.getSplitBrainMergePolicyProvider();
    SplitBrainMergePolicy mergePolicy = mergePolicyProvider.getMergePolicy(PutIfAbsentMergePolicy.class.getName());
    SimpleEntryView<Data, Data> initialEntry = new SimpleEntryView<>(dataKey, dataValue);
    recordStore.merge(createMergingEntry(nodeEngine.getSerializationService(), initialEntry), mergePolicy, CallerProvenance.NOT_WAN);
    SimpleEntryView<Data, Data> mergingEntry = new SimpleEntryView<>(dataKey, dataValue2);
    recordStore.merge(createMergingEntry(nodeEngine.getSerializationService(), mergingEntry), mergePolicy, CallerProvenance.NOT_WAN);
    assertEquals("value1", map.get("key"));
    recordStore.afterOperation();
}
Also used : NodeEngine(com.hazelcast.spi.impl.NodeEngine) SplitBrainMergePolicy(com.hazelcast.spi.merge.SplitBrainMergePolicy) HazelcastInstance(com.hazelcast.core.HazelcastInstance) PutIfAbsentMergePolicy(com.hazelcast.spi.merge.PutIfAbsentMergePolicy) RecordStore(com.hazelcast.map.impl.recordstore.RecordStore) SimpleEntryView(com.hazelcast.map.impl.SimpleEntryView) Data(com.hazelcast.internal.serialization.Data) SplitBrainMergePolicyProvider(com.hazelcast.spi.merge.SplitBrainMergePolicyProvider) MapServiceContext(com.hazelcast.map.impl.MapServiceContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 79 with NodeEngine

use of com.hazelcast.spi.impl.NodeEngine in project hazelcast by hazelcast.

the class EntryEventDataCacheTest method parameters.

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> parameters() {
    // setup mock MapServiceContext & NodeEngine, required by FilteringStrategy's
    MapServiceContext mapServiceContext = mock(MapServiceContext.class);
    NodeEngine mockNodeEngine = mock(NodeEngine.class);
    when(mockNodeEngine.getThisAddress()).thenReturn(ADDRESS);
    when(mapServiceContext.toData(anyObject())).thenReturn(new HeapData());
    when(mapServiceContext.getNodeEngine()).thenReturn(mockNodeEngine);
    return Arrays.asList(new Object[][] { { new DefaultEntryEventFilteringStrategy(null, mapServiceContext) }, { new QueryCacheNaturalFilteringStrategy(null, mapServiceContext) } });
}
Also used : NodeEngine(com.hazelcast.spi.impl.NodeEngine) HeapData(com.hazelcast.internal.serialization.impl.HeapData) MapServiceContext(com.hazelcast.map.impl.MapServiceContext)

Example 80 with NodeEngine

use of com.hazelcast.spi.impl.NodeEngine in project hazelcast by hazelcast.

the class QueryUtilsTest method testVersionMismatch.

@Test
public void testVersionMismatch() {
    HazelcastInstance member = factory.newHazelcastInstance();
    NodeEngine nodeEngine = Accessors.getNodeEngineImpl(member);
    String memberId = nodeEngine.getLocalMember().getUuid().toString();
    String memberVersion = nodeEngine.getLocalMember().getVersion().toString();
    try {
        QueryUtils.createPartitionMap(nodeEngine, new MemberVersion(0, 0, 0), false);
        fail("Must fail");
    } catch (QueryException e) {
        assertEquals(SqlErrorCode.GENERIC, e.getCode());
        assertEquals("Cannot execute SQL query when members have different versions (make sure that all members " + "have the same version) {localMemberId=" + memberId + ", localMemberVersion=0.0.0, remoteMemberId=" + memberId + ", remoteMemberVersion=" + memberVersion + "}", e.getMessage());
    }
}
Also used : NodeEngine(com.hazelcast.spi.impl.NodeEngine) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MemberVersion(com.hazelcast.version.MemberVersion) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

NodeEngine (com.hazelcast.spi.impl.NodeEngine)165 Data (com.hazelcast.internal.serialization.Data)48 OperationService (com.hazelcast.spi.impl.operationservice.OperationService)30 Address (com.hazelcast.cluster.Address)22 Test (org.junit.Test)21 ILogger (com.hazelcast.logging.ILogger)20 HazelcastInstance (com.hazelcast.core.HazelcastInstance)18 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)18 QuickTest (com.hazelcast.test.annotation.QuickTest)18 Config (com.hazelcast.config.Config)17 Operation (com.hazelcast.spi.impl.operationservice.Operation)16 MapServiceContext (com.hazelcast.map.impl.MapServiceContext)15 IPartitionService (com.hazelcast.internal.partition.IPartitionService)12 Nonnull (javax.annotation.Nonnull)12 ArrayList (java.util.ArrayList)11 Future (java.util.concurrent.Future)11 Member (com.hazelcast.cluster.Member)10 UUID (java.util.UUID)10 InitializingObject (com.hazelcast.spi.impl.InitializingObject)9 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)9