Search in sources :

Example 1 with DistributionManager

use of org.infinispan.distribution.DistributionManager in project wildfly by wildfly.

the class InfinispanSessionManager method locatePrimaryOwner.

private Node locatePrimaryOwner(String sessionId) {
    DistributionManager dist = this.cache.getAdvancedCache().getDistributionManager();
    Address address = (dist != null) ? dist.getPrimaryLocation(new Key<>(sessionId)) : null;
    return (address != null) ? this.nodeFactory.createNode(address) : this.dispatcherFactory.getGroup().getLocalNode();
}
Also used : Address(org.infinispan.remoting.transport.Address) DistributionManager(org.infinispan.distribution.DistributionManager) Key(org.wildfly.clustering.infinispan.spi.distribution.Key)

Example 2 with DistributionManager

use of org.infinispan.distribution.DistributionManager in project wildfly by wildfly.

the class CacheGroup method getMembership.

@Override
public Membership getMembership() {
    if (this.isSingleton()) {
        return new SingletonMembership(this.getLocalMember());
    }
    EmbeddedCacheManager manager = this.cache.getCacheManager();
    DistributionManager dist = this.cache.getAdvancedCache().getDistributionManager();
    return (dist != null) ? new CacheMembership(manager.getAddress(), dist.getCacheTopology(), this) : new CacheMembership(manager, this);
}
Also used : EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) DistributionManager(org.infinispan.distribution.DistributionManager)

Example 3 with DistributionManager

use of org.infinispan.distribution.DistributionManager in project wildfly by wildfly.

the class DefaultKeyAffinityServiceTestCase method test.

@Test
public void test() {
    KeyPartitioner partitioner = mock(KeyPartitioner.class);
    KeyGenerator<UUID> generator = mock(KeyGenerator.class);
    AdvancedCache<UUID, Object> cache = mock(AdvancedCache.class);
    Address local = mock(Address.class);
    Address remote = mock(Address.class);
    Address standby = mock(Address.class);
    Address ignored = mock(Address.class);
    KeyAffinityService<UUID> service = new DefaultKeyAffinityService<>(cache, partitioner, generator, address -> (address != ignored));
    DistributionManager dist = mock(DistributionManager.class);
    CacheTopology topology = mock(CacheTopology.class);
    ConsistentHash hash = mock(ConsistentHash.class);
    List<Address> members = Arrays.asList(local, remote, standby, ignored);
    when(cache.getAdvancedCache()).thenReturn(cache);
    when(cache.getDistributionManager()).thenReturn(dist);
    when(topology.getActualMembers()).thenReturn(members);
    when(topology.getCurrentCH()).thenReturn(hash);
    when(topology.getMembers()).thenReturn(members);
    when(topology.getMembersPersistentUUIDs()).thenReturn(Arrays.asList(PersistentUUID.randomUUID(), PersistentUUID.randomUUID(), PersistentUUID.randomUUID(), PersistentUUID.randomUUID()));
    when(topology.getPendingCH()).thenReturn(null);
    when(topology.getPhase()).thenReturn(CacheTopology.Phase.NO_REBALANCE);
    when(topology.getReadConsistentHash()).thenReturn(hash);
    when(topology.getRebalanceId()).thenReturn(0);
    when(topology.getTopologyId()).thenReturn(0);
    when(topology.getUnionCH()).thenReturn(null);
    when(topology.getWriteConsistentHash()).thenReturn(hash);
    when(hash.getMembers()).thenReturn(members);
    when(hash.getNumSegments()).thenReturn(SEGMENTS);
    when(hash.locatePrimaryOwnerForSegment(LOCAL_SEGMENT)).thenReturn(local);
    when(hash.locatePrimaryOwnerForSegment(REMOTE_SEGMENT)).thenReturn(remote);
    when(hash.locatePrimaryOwnerForSegment(FILTERED_SEGMENT)).thenReturn(ignored);
    when(hash.locateOwnersForSegment(LOCAL_SEGMENT)).thenReturn(Collections.singletonList(local));
    when(hash.locateOwnersForSegment(REMOTE_SEGMENT)).thenReturn(Collections.singletonList(remote));
    when(hash.locateOwnersForSegment(FILTERED_SEGMENT)).thenReturn(Collections.singletonList(ignored));
    when(hash.getPrimarySegmentsForOwner(local)).thenReturn(Collections.singleton(LOCAL_SEGMENT));
    when(hash.getPrimarySegmentsForOwner(remote)).thenReturn(Collections.singleton(REMOTE_SEGMENT));
    when(hash.getPrimarySegmentsForOwner(standby)).thenReturn(Collections.emptySet());
    when(hash.getPrimarySegmentsForOwner(ignored)).thenReturn(Collections.singleton(FILTERED_SEGMENT));
    when(hash.getSegmentsForOwner(local)).thenReturn(Collections.singleton(LOCAL_SEGMENT));
    when(hash.getSegmentsForOwner(remote)).thenReturn(Collections.singleton(REMOTE_SEGMENT));
    when(hash.getSegmentsForOwner(standby)).thenReturn(Collections.emptySet());
    when(hash.getSegmentsForOwner(ignored)).thenReturn(Collections.singleton(FILTERED_SEGMENT));
    LocalizedCacheTopology localizedTopology = new LocalizedCacheTopology(CacheMode.DIST_SYNC, topology, partitioner, local, true);
    when(dist.getCacheTopology()).thenReturn(localizedTopology);
    // Mock a sufficient number of keys
    OngoingStubbing<UUID> stub = when(generator.getKey());
    for (int i = 0; i < 1000; ++i) {
        UUID key = UUID.randomUUID();
        int segment = getSegment(key);
        stub = stub.thenReturn(key);
        when(partitioner.getSegment(key)).thenReturn(segment);
    }
    assertThrows(IllegalStateException.class, () -> service.getKeyForAddress(local));
    assertThrows(IllegalStateException.class, () -> service.getKeyForAddress(remote));
    assertThrows(IllegalStateException.class, () -> service.getKeyForAddress(standby));
    // This should throw IAE, since address does not pass filter
    assertThrows(IllegalArgumentException.class, () -> service.getKeyForAddress(ignored));
    service.start();
    try {
        for (int i = 0; i < 50; ++i) {
            UUID key = service.getKeyForAddress(local);
            int segment = getSegment(key);
            assertEquals(LOCAL_SEGMENT, segment);
            key = service.getCollocatedKey(key);
            segment = getSegment(key);
            assertEquals(LOCAL_SEGMENT, segment);
            key = service.getKeyForAddress(remote);
            segment = getSegment(key);
            assertEquals(REMOTE_SEGMENT, segment);
            key = service.getCollocatedKey(key);
            segment = getSegment(key);
            assertEquals(REMOTE_SEGMENT, segment);
        }
        // This should return a random key
        assertNotNull(service.getKeyForAddress(standby));
        // This should throw IAE, since address does not pass filter
        assertThrows(IllegalArgumentException.class, () -> service.getKeyForAddress(ignored));
    } finally {
        service.stop();
    }
}
Also used : ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) Address(org.infinispan.remoting.transport.Address) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) CacheTopology(org.infinispan.topology.CacheTopology) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) UUID(java.util.UUID) PersistentUUID(org.infinispan.topology.PersistentUUID) DistributionManager(org.infinispan.distribution.DistributionManager) Test(org.junit.Test)

Example 4 with DistributionManager

use of org.infinispan.distribution.DistributionManager in project kernel by exoplatform.

the class TestDistributedExoCache method testDistributedCache.

/**
 * WARNING: For Linux distributions the following JVM parameter must be set to true: java.net.preferIPv4Stack.
 *
 * @throws Exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testDistributedCache() throws Exception {
    PortalContainer pc = PortalContainer.getInstance();
    ExoCacheConfig config = new ExoCacheConfig();
    config.setName("MyCacheDistributed");
    config.setMaxSize(5);
    config.setLiveTime(1);
    config.setImplementation("LRU");
    config.setDistributed(true);
    Map<String, String> params = new HashMap<String, String>();
    params.put("infinispan-num-owners", "1");
    ConfigurationManager cm = (ConfigurationManager) pc.getComponentInstanceOfType(ConfigurationManager.class);
    DistributedCacheManager dcm2 = new DistributedCacheManager("jar:/conf/portal/distributed-cache-configuration.xml", params, cm);
    DistributedExoCache<Serializable, Object> cache1 = (DistributedExoCache<Serializable, Object>) ((ExoCacheFactory) pc.getComponentInstanceOfType(ExoCacheFactory.class)).createCache(config);
    DistributionManager dm = cache1.getCache().getDistributionManager();
    DistributedExoCache<Serializable, Object> cache2 = (DistributedExoCache<Serializable, Object>) new ExoCacheFactoryImpl((ExoContainerContext) pc.getComponentInstanceOfType(ExoContainerContext.class), "jar:/conf/portal/cache-configuration-template.xml", cm, dcm2).createCache(config);
    KeyAffinityService kas1 = KeyAffinityServiceFactory.newLocalKeyAffinityService(cache1.getCache(), new MyKeyGenerator(cache1.getFullName()), Executors.newSingleThreadExecutor(), 100);
    KeyAffinityService kas2 = KeyAffinityServiceFactory.newLocalKeyAffinityService(cache2.getCache(), new MyKeyGenerator(cache1.getFullName()), Executors.newSingleThreadExecutor(), 100);
    try {
        Object a, b, c;
        for (int i = 0; i < 2; i++) {
            if (i == 0) {
                a = new MyKey("a", ((DistributedExoCache.CacheKey<MyKey>) kas1.getKeyForAddress(cache1.getCache().getRpcManager().getAddress())).getKey().value);
            } else {
                a = new MyKey("a", ((DistributedExoCache.CacheKey<MyKey>) kas2.getKeyForAddress(cache2.getCache().getRpcManager().getAddress())).getKey().value);
            }
            for (int j = 0; j < 2; j++) {
                if (j == 0) {
                    b = new MyKey("b", ((DistributedExoCache.CacheKey<MyKey>) kas1.getKeyForAddress(cache1.getCache().getRpcManager().getAddress())).getKey().value);
                } else {
                    b = new MyKey("b", ((DistributedExoCache.CacheKey<MyKey>) kas2.getKeyForAddress(cache2.getCache().getRpcManager().getAddress())).getKey().value);
                }
                for (int k = 0; k < 2; k++) {
                    if (k == 0) {
                        c = new MyKey("c", ((DistributedExoCache.CacheKey<MyKey>) kas1.getKeyForAddress(cache1.getCache().getRpcManager().getAddress())).getKey().value);
                    } else {
                        c = new MyKey("c", ((DistributedExoCache.CacheKey<MyKey>) kas2.getKeyForAddress(cache2.getCache().getRpcManager().getAddress())).getKey().value);
                    }
                    checkUseCase(cache1, cache2, dm, a, b, c);
                }
            }
        }
    } finally {
        dcm2.stop();
    }
}
Also used : Serializable(java.io.Serializable) KeyAffinityService(org.infinispan.affinity.KeyAffinityService) HashMap(java.util.HashMap) DistributedCacheManager(org.exoplatform.services.ispn.DistributedCacheManager) ExoCacheConfig(org.exoplatform.services.cache.ExoCacheConfig) ExoCacheFactoryImpl(org.exoplatform.services.cache.impl.infinispan.ExoCacheFactoryImpl) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager) DistributionManager(org.infinispan.distribution.DistributionManager) PortalContainer(org.exoplatform.container.PortalContainer)

Example 5 with DistributionManager

use of org.infinispan.distribution.DistributionManager in project wildfly by wildfly.

the class InfinispanBeanManager method locatePrimaryOwner.

Node locatePrimaryOwner(I id) {
    DistributionManager dist = this.cache.getAdvancedCache().getDistributionManager();
    Address address = (dist != null) ? dist.getPrimaryLocation(id) : null;
    return (address != null) ? this.nodeFactory.createNode(address) : this.registry.getGroup().getLocalNode();
}
Also used : Address(org.infinispan.remoting.transport.Address) DistributionManager(org.infinispan.distribution.DistributionManager)

Aggregations

DistributionManager (org.infinispan.distribution.DistributionManager)6 Address (org.infinispan.remoting.transport.Address)3 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)2 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 PortalContainer (org.exoplatform.container.PortalContainer)1 ConfigurationManager (org.exoplatform.container.configuration.ConfigurationManager)1 ExoCacheConfig (org.exoplatform.services.cache.ExoCacheConfig)1 ExoCacheFactoryImpl (org.exoplatform.services.cache.impl.infinispan.ExoCacheFactoryImpl)1 DistributedCacheManager (org.exoplatform.services.ispn.DistributedCacheManager)1 KeyAffinityService (org.infinispan.affinity.KeyAffinityService)1 ConsistentHash (org.infinispan.distribution.ch.ConsistentHash)1 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)1 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)1 CacheTopology (org.infinispan.topology.CacheTopology)1 PersistentUUID (org.infinispan.topology.PersistentUUID)1 Test (org.junit.Test)1 Key (org.wildfly.clustering.infinispan.spi.distribution.Key)1