Search in sources :

Example 21 with InternalPartitionService

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

the class InvalidationMetadataDistortionTest method distortRandomPartitionSequence.

private void distortRandomPartitionSequence(String mapName, HazelcastInstance member) {
    NodeEngineImpl nodeEngineImpl = getNodeEngineImpl(member);
    CacheService service = nodeEngineImpl.getService(CacheService.SERVICE_NAME);
    CacheEventHandler cacheEventHandler = service.getCacheEventHandler();
    MetaDataGenerator metaDataGenerator = cacheEventHandler.getMetaDataGenerator();
    InternalPartitionService partitionService = nodeEngineImpl.getPartitionService();
    int partitionCount = partitionService.getPartitionCount();
    int randomPartition = getInt(partitionCount);
    int randomSequence = getInt(MAX_VALUE);
    metaDataGenerator.setCurrentSequence(mapName, randomPartition, randomSequence);
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) CacheEventHandler(com.hazelcast.cache.impl.CacheEventHandler) MetaDataGenerator(com.hazelcast.internal.nearcache.impl.invalidation.MetaDataGenerator) CacheService(com.hazelcast.cache.impl.CacheService)

Example 22 with InternalPartitionService

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

the class InvalidationMemberAddRemoveTest method ensure_nearCachedClient_and_member_data_sync_eventually.

@Test
public void ensure_nearCachedClient_and_member_data_sync_eventually() throws Exception {
    final AtomicBoolean stopTest = new AtomicBoolean();
    final Config config = createConfig();
    hazelcastFactory.newHazelcastInstance(config);
    CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(serverInstance);
    final CacheManager serverCacheManager = provider.getCacheManager();
    // populated from member.
    final Cache<Integer, Integer> memberCache = serverCacheManager.createCache(DEFAULT_CACHE_NAME, createCacheConfig(BINARY));
    for (int i = 0; i < KEY_COUNT; i++) {
        memberCache.put(i, i);
    }
    ClientConfig clientConfig = createClientConfig();
    clientConfig.addNearCacheConfig(createNearCacheConfig(BINARY));
    HazelcastClientProxy client = (HazelcastClientProxy) hazelcastFactory.newHazelcastClient(clientConfig);
    CachingProvider clientCachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
    final Cache<Integer, Integer> clientCache = clientCachingProvider.getCacheManager().createCache(DEFAULT_CACHE_NAME, createCacheConfig(BINARY));
    ArrayList<Thread> threads = new ArrayList<Thread>();
    // continuously adds and removes member
    Thread shadowMember = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!stopTest.get()) {
                HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config);
                sleepSeconds(5);
                member.getLifecycleService().terminate();
            }
        }
    });
    threads.add(shadowMember);
    for (int i = 0; i < NEAR_CACHE_POPULATOR_THREAD_COUNT; i++) {
        // populates client near-cache
        Thread populateClientNearCache = new Thread(new Runnable() {

            public void run() {
                while (!stopTest.get()) {
                    for (int i = 0; i < KEY_COUNT; i++) {
                        clientCache.get(i);
                    }
                }
            }
        });
        threads.add(populateClientNearCache);
    }
    // updates data from member.
    Thread putFromMember = new Thread(new Runnable() {

        public void run() {
            while (!stopTest.get()) {
                int key = getInt(KEY_COUNT);
                int value = getInt(Integer.MAX_VALUE);
                memberCache.put(key, value);
                sleepAtLeastMillis(2);
            }
        }
    });
    threads.add(putFromMember);
    Thread clearFromMember = new Thread(new Runnable() {

        public void run() {
            while (!stopTest.get()) {
                memberCache.clear();
                sleepSeconds(3);
            }
        }
    });
    threads.add(clearFromMember);
    // start threads
    for (Thread thread : threads) {
        thread.start();
    }
    // stress system some seconds
    sleepSeconds(TEST_RUN_SECONDS);
    //stop threads
    stopTest.set(true);
    for (Thread thread : threads) {
        thread.join();
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            for (int i = 0; i < KEY_COUNT; i++) {
                Integer valueSeenFromMember = memberCache.get(i);
                Integer valueSeenFromClient = clientCache.get(i);
                String msg = createFailureMessage(i);
                assertEquals(msg, valueSeenFromMember, valueSeenFromClient);
            }
        }

        private String createFailureMessage(int i) {
            InternalPartitionService partitionService = getPartitionService(serverInstance);
            Data keyData = getSerializationService(serverInstance).toData(i);
            int partitionId = partitionService.getPartitionId(keyData);
            AbstractNearCacheRecordStore nearCacheRecordStore = getAbstractNearCacheRecordStore();
            NearCacheRecord record = nearCacheRecordStore.getRecord(keyData);
            long recordSequence = record == null ? NO_SEQUENCE : record.getInvalidationSequence();
            MetaDataGenerator metaDataGenerator = getMetaDataGenerator();
            long memberSequence = metaDataGenerator.currentSequence("/hz/" + DEFAULT_CACHE_NAME, partitionId);
            MetaDataContainer metaDataContainer = nearCacheRecordStore.getStaleReadDetector().getMetaDataContainer(keyData);
            return String.format("partition=%d, onRecordSequence=%d, latestSequence=%d, staleSequence=%d, memberSequence=%d", partitionService.getPartitionId(keyData), recordSequence, metaDataContainer.getSequence(), metaDataContainer.getStaleSequence(), memberSequence);
        }

        private MetaDataGenerator getMetaDataGenerator() {
            CacheEventHandler cacheEventHandler = ((CacheService) ((CacheProxy) memberCache).getService()).getCacheEventHandler();
            return cacheEventHandler.getMetaDataGenerator();
        }

        private AbstractNearCacheRecordStore getAbstractNearCacheRecordStore() {
            DefaultNearCache defaultNearCache = (DefaultNearCache) ((ClientCacheProxy) clientCache).getNearCache().unwrap(DefaultNearCache.class);
            return (AbstractNearCacheRecordStore) defaultNearCache.getNearCacheRecordStore();
        }
    });
}
Also used : DefaultNearCache(com.hazelcast.internal.nearcache.impl.DefaultNearCache) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) CacheConfig(com.hazelcast.config.CacheConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) ArrayList(java.util.ArrayList) CacheEventHandler(com.hazelcast.cache.impl.CacheEventHandler) MetaDataContainer(com.hazelcast.internal.nearcache.impl.invalidation.MetaDataContainer) ClientCacheProxy(com.hazelcast.client.cache.impl.ClientCacheProxy) CacheProxy(com.hazelcast.cache.impl.CacheProxy) CacheManager(javax.cache.CacheManager) AbstractNearCacheRecordStore(com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientProxy(com.hazelcast.client.impl.HazelcastClientProxy) MetaDataGenerator(com.hazelcast.internal.nearcache.impl.invalidation.MetaDataGenerator) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) Data(com.hazelcast.nio.serialization.Data) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) NearCacheRecord(com.hazelcast.internal.nearcache.NearCacheRecord) AssertTask(com.hazelcast.test.AssertTask) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 23 with InternalPartitionService

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

the class CacheClearTest method testClear.

@Test
public void testClear() {
    ICache<String, String> cache = createCache();
    String cacheName = cache.getName();
    Map<String, String> entries = createAndFillEntries();
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        cache.put(entry.getKey(), entry.getValue());
    }
    // Verify that put works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        String actualValue = cache.get(key);
        assertEquals(expectedValue, actualValue);
    }
    Node node = getNode(hazelcastInstance);
    InternalPartitionService partitionService = node.getPartitionService();
    SerializationService serializationService = node.getSerializationService();
    // Verify that backup of put works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        Data keyData = serializationService.toData(key);
        int keyPartitionId = partitionService.getPartitionId(keyData);
        for (int i = 0; i < INSTANCE_COUNT; i++) {
            Node n = getNode(hazelcastInstances[i]);
            ICacheService cacheService = n.getNodeEngine().getService(ICacheService.SERVICE_NAME);
            ICacheRecordStore recordStore = cacheService.getRecordStore("/hz/" + cacheName, keyPartitionId);
            assertNotNull(recordStore);
            String actualValue = serializationService.toObject(recordStore.get(keyData, null));
            assertEquals(expectedValue, actualValue);
        }
    }
    cache.clear();
    // Verify that clear works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String actualValue = cache.get(key);
        assertNull(actualValue);
    }
    // Verify that backup of clear works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        Data keyData = serializationService.toData(key);
        int keyPartitionId = partitionService.getPartitionId(keyData);
        for (int i = 0; i < INSTANCE_COUNT; i++) {
            Node n = getNode(hazelcastInstances[i]);
            ICacheService cacheService = n.getNodeEngine().getService(ICacheService.SERVICE_NAME);
            ICacheRecordStore recordStore = cacheService.getRecordStore("/hz/" + cacheName, keyPartitionId);
            assertNotNull(recordStore);
            String actualValue = serializationService.toObject(recordStore.get(keyData, null));
            assertNull(actualValue);
        }
    }
}
Also used : InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) ICacheService(com.hazelcast.cache.impl.ICacheService) Node(com.hazelcast.instance.Node) SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.serialization.Data) ICacheRecordStore(com.hazelcast.cache.impl.ICacheRecordStore) HashMap(java.util.HashMap) Map(java.util.Map) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 24 with InternalPartitionService

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

the class ClusterJoinManager method startJoin.

private void startJoin() {
    logger.fine("Starting join...");
    clusterServiceLock.lock();
    try {
        InternalPartitionService partitionService = node.getPartitionService();
        try {
            joinInProgress = true;
            // pause migrations until join, member-update and post-join operations are completed
            partitionService.pauseMigration();
            Collection<MemberImpl> members = clusterService.getMemberImpls();
            Collection<MemberInfo> memberInfos = createMemberInfoList(members);
            for (MemberInfo memberJoining : joiningMembers.values()) {
                memberInfos.add(memberJoining);
            }
            long time = clusterClock.getClusterTime();
            // post join operations must be lock free, that means no locks at all:
            // no partition locks, no key-based locks, no service level locks!
            Operation[] postJoinOps = nodeEngine.getPostJoinOperations();
            boolean createPostJoinOperation = (postJoinOps != null && postJoinOps.length > 0);
            PostJoinOperation postJoinOp = (createPostJoinOperation ? new PostJoinOperation(postJoinOps) : null);
            clusterService.updateMembers(memberInfos, node.getThisAddress());
            int count = members.size() - 1 + joiningMembers.size();
            List<Future> calls = new ArrayList<Future>(count);
            PartitionRuntimeState partitionRuntimeState = partitionService.createPartitionState();
            for (MemberInfo member : joiningMembers.values()) {
                long startTime = clusterClock.getClusterStartTime();
                Operation finalizeJoinOperation = new FinalizeJoinOperation(member.getUuid(), memberInfos, postJoinOp, time, clusterService.getClusterId(), startTime, clusterStateManager.getState(), clusterService.getClusterVersion(), partitionRuntimeState);
                calls.add(invokeClusterOperation(finalizeJoinOperation, member.getAddress()));
            }
            for (MemberImpl member : members) {
                if (member.localMember() || joiningMembers.containsKey(member.getAddress())) {
                    continue;
                }
                Operation memberInfoUpdateOperation = new MemberInfoUpdateOperation(member.getUuid(), memberInfos, time, partitionRuntimeState, true);
                calls.add(invokeClusterOperation(memberInfoUpdateOperation, member.getAddress()));
            }
            int timeout = Math.min(calls.size() * FINALIZE_JOIN_TIMEOUT_FACTOR, FINALIZE_JOIN_MAX_TIMEOUT);
            waitWithDeadline(calls, timeout, TimeUnit.SECONDS, whileFinalizeJoinsExceptionHandler);
        } finally {
            reset();
            partitionService.resumeMigration();
        }
    } finally {
        clusterServiceLock.unlock();
    }
}
Also used : InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) MemberImpl(com.hazelcast.instance.MemberImpl) ArrayList(java.util.ArrayList) PostJoinOperation(com.hazelcast.internal.cluster.impl.operations.PostJoinOperation) FinalizeJoinOperation(com.hazelcast.internal.cluster.impl.operations.FinalizeJoinOperation) MemberInfoUpdateOperation(com.hazelcast.internal.cluster.impl.operations.MemberInfoUpdateOperation) ConfigMismatchOperation(com.hazelcast.internal.cluster.impl.operations.ConfigMismatchOperation) Operation(com.hazelcast.spi.Operation) BeforeJoinCheckFailureOperation(com.hazelcast.internal.cluster.impl.operations.BeforeJoinCheckFailureOperation) SetMasterOperation(com.hazelcast.internal.cluster.impl.operations.SetMasterOperation) GroupMismatchOperation(com.hazelcast.internal.cluster.impl.operations.GroupMismatchOperation) PostJoinOperation(com.hazelcast.internal.cluster.impl.operations.PostJoinOperation) MasterDiscoveryOperation(com.hazelcast.internal.cluster.impl.operations.MasterDiscoveryOperation) JoinRequestOperation(com.hazelcast.internal.cluster.impl.operations.JoinRequestOperation) AuthenticationFailureOperation(com.hazelcast.internal.cluster.impl.operations.AuthenticationFailureOperation) MemberInfoUpdateOperation(com.hazelcast.internal.cluster.impl.operations.MemberInfoUpdateOperation) FinalizeJoinOperation(com.hazelcast.internal.cluster.impl.operations.FinalizeJoinOperation) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) PartitionRuntimeState(com.hazelcast.internal.partition.PartitionRuntimeState) Future(java.util.concurrent.Future)

Example 25 with InternalPartitionService

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

the class HttpGetCommandProcessor method handleHealthcheck.

private void handleHealthcheck(HttpGetCommand command) {
    Node node = textCommandService.getNode();
    NodeState nodeState = node.getState();
    ClusterServiceImpl clusterService = node.getClusterService();
    ClusterState clusterState = clusterService.getClusterState();
    int clusterSize = clusterService.getMembers().size();
    InternalPartitionService partitionService = node.getPartitionService();
    boolean memberStateSafe = partitionService.isMemberStateSafe();
    boolean clusterSafe = memberStateSafe && !partitionService.hasOnGoingMigration();
    long migrationQueueSize = partitionService.getMigrationQueueSize();
    StringBuilder res = new StringBuilder();
    res.append("Hazelcast::NodeState=").append(nodeState).append("\n");
    res.append("Hazelcast::ClusterState=").append(clusterState).append("\n");
    res.append("Hazelcast::ClusterSafe=").append(Boolean.toString(clusterSafe).toUpperCase()).append("\n");
    res.append("Hazelcast::MigrationQueueSize=").append(migrationQueueSize).append("\n");
    res.append("Hazelcast::ClusterSize=").append(clusterSize).append("\n");
    command.setResponse(MIME_TEXT_PLAIN, stringToBytes(res.toString()));
}
Also used : ClusterState(com.hazelcast.cluster.ClusterState) NodeState(com.hazelcast.instance.NodeState) InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) Node(com.hazelcast.instance.Node) ClusterServiceImpl(com.hazelcast.internal.cluster.impl.ClusterServiceImpl)

Aggregations

InternalPartitionService (com.hazelcast.internal.partition.InternalPartitionService)45 Address (com.hazelcast.nio.Address)11 HazelcastInstance (com.hazelcast.core.HazelcastInstance)10 Test (org.junit.Test)10 Node (com.hazelcast.instance.Node)9 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 ArrayList (java.util.ArrayList)8 Config (com.hazelcast.config.Config)6 Data (com.hazelcast.nio.serialization.Data)6 IPartition (com.hazelcast.spi.partition.IPartition)6 HashMap (java.util.HashMap)6 InternalPartition (com.hazelcast.internal.partition.InternalPartition)5 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)5 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)5 List (java.util.List)5 Map (java.util.Map)5 ICacheService (com.hazelcast.cache.impl.ICacheService)4 ICacheRecordStore (com.hazelcast.cache.impl.ICacheRecordStore)3 CacheConfig (com.hazelcast.config.CacheConfig)3