Search in sources :

Example 1 with HashFunctionPartitioner

use of org.infinispan.distribution.ch.impl.HashFunctionPartitioner in project infinispan by infinispan.

the class StateReceiverTest method createLocalizedCacheTopology.

private LocalizedCacheTopology createLocalizedCacheTopology(int numberOfNodes) {
    ConsistentHash hash = createConsistentHash(numberOfNodes);
    CacheTopology topology = new CacheTopology(-1, -1, hash, null, CacheTopology.Phase.NO_REBALANCE, hash.getMembers(), null);
    return new LocalizedCacheTopology(CacheMode.DIST_SYNC, topology, new HashFunctionPartitioner(), hash.getMembers().get(0), true);
}
Also used : ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) CacheTopology(org.infinispan.topology.CacheTopology) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) HashFunctionPartitioner(org.infinispan.distribution.ch.impl.HashFunctionPartitioner)

Example 2 with HashFunctionPartitioner

use of org.infinispan.distribution.ch.impl.HashFunctionPartitioner in project infinispan by infinispan.

the class StateProviderTest method test1.

public void test1() {
    // create list of 6 members
    List<Address> members1 = Arrays.asList(A, B, C, D, E, F);
    List<Address> members2 = new ArrayList<>(members1);
    members2.remove(A);
    members2.remove(F);
    members2.add(G);
    // create CHes
    KeyPartitioner keyPartitioner = new HashFunctionPartitioner(StateProviderTest.NUM_SEGMENTS);
    DefaultConsistentHashFactory chf = new DefaultConsistentHashFactory();
    DefaultConsistentHash ch1 = chf.create(2, StateProviderTest.NUM_SEGMENTS, members1, null);
    DefaultConsistentHash ch2 = chf.updateMembers(ch1, members2, null);
    // create dependencies
    when(rpcManager.getAddress()).thenReturn(A);
    when(rpcManager.invokeCommand(any(Address.class), any(), any(), any())).thenReturn(new CompletableFuture<>());
    // create state provider
    StateProviderImpl stateProvider = new StateProviderImpl();
    TestingUtil.inject(stateProvider, configuration, rpcManager, commandsFactory, cacheNotifier, persistenceManager, dataContainer, transactionTable, stateTransferLock, distributionManager, ef, keyPartitioner, TransactionOriginatorChecker.LOCAL);
    stateProvider.start();
    final List<InternalCacheEntry> cacheEntries = new ArrayList<>();
    Object key1 = new TestKey("key1", 0, keyPartitioner);
    Object key2 = new TestKey("key2", 0, keyPartitioner);
    cacheEntries.add(new ImmortalCacheEntry(key1, "value1"));
    cacheEntries.add(new ImmortalCacheEntry(key2, "value2"));
    when(dataContainer.iterator()).thenAnswer(invocation -> cacheEntries.iterator());
    when(transactionTable.getLocalTransactions()).thenReturn(Collections.emptyList());
    when(transactionTable.getRemoteTransactions()).thenReturn(Collections.emptyList());
    CacheTopology simpleTopology = new CacheTopology(1, 1, ch1, ch1, ch1, CacheTopology.Phase.READ_OLD_WRITE_ALL, ch1.getMembers(), persistentUUIDManager.mapAddresses(ch1.getMembers()));
    this.cacheTopology = new LocalizedCacheTopology(CacheMode.DIST_SYNC, simpleTopology, keyPartitioner, A, true);
    stateProvider.onTopologyUpdate(this.cacheTopology, false);
    log.debug("ch1: " + ch1);
    IntSet segmentsToRequest = IntSets.from(ch1.getSegmentsForOwner(members1.get(0)));
    CompletionStage<List<TransactionInfo>> transactionsStage = stateProvider.getTransactionsForSegments(members1.get(0), 1, segmentsToRequest);
    List<TransactionInfo> transactions = CompletionStages.join(transactionsStage);
    assertEquals(0, transactions.size());
    CompletionStage<List<TransactionInfo>> transactionsStage2 = stateProvider.getTransactionsForSegments(members1.get(0), 1, SmallIntSet.of(2, StateProviderTest.NUM_SEGMENTS));
    Exceptions.expectExecutionException(IllegalArgumentException.class, transactionsStage2.toCompletableFuture());
    verifyNoMoreInteractions(stateTransferLock);
    when(dataContainer.iterator(any())).thenReturn(cacheEntries.iterator());
    when(persistenceManager.publishEntries(any(IntSet.class), any(), anyBoolean(), anyBoolean(), any())).thenReturn(Flowable.empty());
    stateProvider.startOutboundTransfer(F, 1, IntSets.immutableSet(0), true);
    assertTrue(stateProvider.isStateTransferInProgress());
    log.debug("ch2: " + ch2);
    simpleTopology = new CacheTopology(2, 1, ch2, ch2, ch2, CacheTopology.Phase.READ_OLD_WRITE_ALL, ch2.getMembers(), persistentUUIDManager.mapAddresses(ch2.getMembers()));
    this.cacheTopology = new LocalizedCacheTopology(CacheMode.DIST_SYNC, simpleTopology, keyPartitioner, A, true);
    stateProvider.onTopologyUpdate(this.cacheTopology, true);
    assertFalse(stateProvider.isStateTransferInProgress());
    stateProvider.startOutboundTransfer(D, 1, IntSets.immutableSet(0), true);
    assertTrue(stateProvider.isStateTransferInProgress());
    stateProvider.stop();
    assertFalse(stateProvider.isStateTransferInProgress());
}
Also used : TestAddress(org.infinispan.distribution.TestAddress) Address(org.infinispan.remoting.transport.Address) SmallIntSet(org.infinispan.commons.util.SmallIntSet) IntSet(org.infinispan.commons.util.IntSet) ArrayList(java.util.ArrayList) DefaultConsistentHashFactory(org.infinispan.distribution.ch.impl.DefaultConsistentHashFactory) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) CacheTopology(org.infinispan.topology.CacheTopology) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) ImmortalCacheEntry(org.infinispan.container.entries.ImmortalCacheEntry) List(java.util.List) ArrayList(java.util.ArrayList) HashFunctionPartitioner(org.infinispan.distribution.ch.impl.HashFunctionPartitioner) DefaultConsistentHash(org.infinispan.distribution.ch.impl.DefaultConsistentHash)

Example 3 with HashFunctionPartitioner

use of org.infinispan.distribution.ch.impl.HashFunctionPartitioner in project infinispan by infinispan.

the class ConsistentHashPerfTest method doTestDistribution.

private void doTestDistribution(int numKeys, int numNodes, List<Object> keys) {
    ConsistentHash ch = createNewConsistentHash(createAddresses(numNodes));
    Map<Address, Integer> distribution = new HashMap<>();
    KeyPartitioner keyPartitioner = new HashFunctionPartitioner(ch.getNumSegments());
    for (Object key : keys) {
        int segment = keyPartitioner.getSegment(key);
        Address a = ch.locateOwnersForSegment(segment).get(0);
        if (distribution.containsKey(a)) {
            int i = distribution.get(a);
            distribution.put(a, i + 1);
        } else {
            distribution.put(a, 1);
        }
    }
    System.out.printf("\nTesting distribution with %d keys, %d nodes\n", numKeys, numNodes);
    // System.out.println("" + distribution);
    // calc numbers
    ArrayList<Integer> counts = new ArrayList<Integer>(distribution.values());
    Collections.sort(counts);
    // instead we add a 0 for all the nonexistent keys in the distribution map and do the calculations
    for (int i = 0; i < numNodes - counts.size(); i++) counts.add(0, 0);
    double mean = 0;
    int sum = 0;
    for (Integer count : counts) sum += count;
    assert sum == numKeys;
    mean = sum / numNodes;
    double variance = 0;
    for (Integer count : counts) variance += (count - mean) * (count - mean);
    double stdDev = sqrt(variance);
    double avgAbsDev = 0;
    for (Integer count : counts) avgAbsDev += abs(count - mean);
    avgAbsDev /= numNodes;
    int median = counts.get(numNodes / 2);
    ArrayList<Integer> medianDevs = new ArrayList<Integer>(numNodes);
    for (Integer count : counts) medianDevs.add(abs(count - median));
    Collections.sort(medianDevs);
    int medianAbsDev = medianDevs.get(numNodes / 2);
    System.out.printf("Mean = %f, median = %d\n", mean, median);
    System.out.printf("Standard deviation = %.3f, or %.3f%%\n", stdDev, stdDev / mean * 100);
    System.out.printf("Average absolute deviation = %.3f, or %.3f%%\n", avgAbsDev, avgAbsDev / mean * 100);
    System.out.printf("Median absolute deviation = %d or %.3f%%\n", medianAbsDev, (double) medianAbsDev / mean * 100);
}
Also used : ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) JGroupsAddress(org.infinispan.remoting.transport.jgroups.JGroupsAddress) Address(org.infinispan.remoting.transport.Address) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashFunctionPartitioner(org.infinispan.distribution.ch.impl.HashFunctionPartitioner)

Example 4 with HashFunctionPartitioner

use of org.infinispan.distribution.ch.impl.HashFunctionPartitioner in project infinispan by infinispan.

the class ConsistentHashPerfTest method doPerfTest.

private Long doPerfTest(int numNodes, int numOwners, int iterations) {
    ConsistentHash ch = createNewConsistentHash(createAddresses(numNodes));
    int dummy = 0;
    long start = System.nanoTime();
    KeyPartitioner keyPartitioner = new HashFunctionPartitioner(ch.getNumSegments());
    for (int i = 0; i < iterations; i++) {
        Object key = i;
        int segment = keyPartitioner.getSegment(key);
        dummy += ch.locateOwnersForSegment(segment).size();
    }
    long duration = System.nanoTime() - start;
    assertEquals(dummy, iterations * min(numOwners, numNodes));
    return duration;
}
Also used : ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) HashFunctionPartitioner(org.infinispan.distribution.ch.impl.HashFunctionPartitioner)

Example 5 with HashFunctionPartitioner

use of org.infinispan.distribution.ch.impl.HashFunctionPartitioner in project infinispan by infinispan.

the class TopologyAwareOwnershipStatistics method setUp.

@BeforeMethod()
public void setUp() {
    chf = createConsistentHashFactory();
    chMembers = new ArrayList<>(ADDRESS_COUNT);
    capacityFactors = null;
    testAddresses = new TestTopologyAwareAddress[ADDRESS_COUNT];
    for (int i = 0; i < ADDRESS_COUNT; i++) {
        testAddresses[i] = new TestTopologyAwareAddress(i * 100);
        testAddresses[i].setName(Character.toString((char) ('A' + i)));
    }
    keyPartitioner = new HashFunctionPartitioner(numSegments);
}
Also used : TestTopologyAwareAddress(org.infinispan.distribution.TestTopologyAwareAddress) HashFunctionPartitioner(org.infinispan.distribution.ch.impl.HashFunctionPartitioner) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

HashFunctionPartitioner (org.infinispan.distribution.ch.impl.HashFunctionPartitioner)7 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)5 ArrayList (java.util.ArrayList)4 Address (org.infinispan.remoting.transport.Address)4 CacheTopology (org.infinispan.topology.CacheTopology)4 List (java.util.List)3 IntSet (org.infinispan.commons.util.IntSet)3 ImmortalCacheEntry (org.infinispan.container.entries.ImmortalCacheEntry)3 InternalCacheEntry (org.infinispan.container.entries.InternalCacheEntry)3 TestAddress (org.infinispan.distribution.TestAddress)3 ConsistentHash (org.infinispan.distribution.ch.ConsistentHash)3 DefaultConsistentHash (org.infinispan.distribution.ch.impl.DefaultConsistentHash)3 DefaultConsistentHashFactory (org.infinispan.distribution.ch.impl.DefaultConsistentHashFactory)3 SmallIntSet (org.infinispan.commons.util.SmallIntSet)2 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)2 Flowable (io.reactivex.rxjava3.core.Flowable)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1