Search in sources :

Example 1 with FlakeIdGenerator

use of com.hazelcast.flakeidgen.FlakeIdGenerator in project hazelcast by hazelcast.

the class FlakeIdGenerator_AbstractBackpressureTest method backpressureTest.

@Test
public void backpressureTest() {
    final FlakeIdGenerator generator = instance.getFlakeIdGenerator("gen");
    long testStartNanos = Timer.nanos();
    long allowedHiccupMillis = 2000;
    for (int i = 1; i <= 15; i++) {
        generator.newId();
        long elapsedMs = Timer.millisElapsed(testStartNanos);
        long minimumRequiredMs = Math.max(0, (i * BATCH_SIZE >> DEFAULT_BITS_SEQUENCE) - DEFAULT_ALLOWED_FUTURE_MILLIS - CTM_IMPRECISION);
        long maximumAllowedMs = minimumRequiredMs + allowedHiccupMillis;
        String msg = "Iteration " + i + ", elapsed: " + Timer.millisElapsed(testStartNanos) + "ms, " + "minimum: " + minimumRequiredMs + ", maximum: " + maximumAllowedMs;
        LOGGER.info(msg);
        assertTrue(msg, elapsedMs >= minimumRequiredMs && elapsedMs <= maximumAllowedMs);
        // drain remainder of the IDs from the batch
        for (int j = 1; j < BATCH_SIZE; j++) {
            generator.newId();
        }
    }
/*
        This is the typical output of the test:
            Iteration 1, elapsed: 0ms, minimum: 0, maximum: 2000
            Iteration 2, elapsed: 1ms, minimum: 0, maximum: 2000
            Iteration 3, elapsed: 3ms, minimum: 0, maximum: 2000
            Iteration 4, elapsed: 4ms, minimum: 0, maximum: 2000
            Iteration 5, elapsed: 5ms, minimum: 0, maximum: 2000
            Iteration 6, elapsed: 6ms, minimum: 0, maximum: 2000
            Iteration 7, elapsed: 8ms, minimum: 0, maximum: 2000
            Iteration 8, elapsed: 9ms, minimum: 0, maximum: 2000
            Iteration 9, elapsed: 10ms, minimum: 0, maximum: 2000
            Iteration 10, elapsed: 621ms, minimum: 575, maximum: 2575
            Iteration 11, elapsed: 2183ms, minimum: 2137, maximum: 4137
            Iteration 12, elapsed: 3746ms, minimum: 3700, maximum: 5700
            Iteration 13, elapsed: 5307ms, minimum: 5262, maximum: 7262
            Iteration 14, elapsed: 6871ms, minimum: 6825, maximum: 8825
            Iteration 15, elapsed: 8433ms, minimum: 8387, maximum: 10387
         */
}
Also used : FlakeIdGenerator(com.hazelcast.flakeidgen.FlakeIdGenerator) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 2 with FlakeIdGenerator

use of com.hazelcast.flakeidgen.FlakeIdGenerator in project hazelcast by hazelcast.

the class FlakeIdGenerator_NodeIdOverflowIntegrationTest method when_memberOutOfRangeNodeId_then_theOtherMemberUsed.

@Test
public void when_memberOutOfRangeNodeId_then_theOtherMemberUsed() {
    assignOutOfRangeNodeId(instance2);
    // let's use the instance with out-of-range node ID to generate IDs, it should succeed
    FlakeIdGenerator gen = instance2.getFlakeIdGenerator("gen");
    for (int i = 0; i < 100; i++) {
        gen.newId();
    }
}
Also used : FlakeIdGenerator(com.hazelcast.flakeidgen.FlakeIdGenerator) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with FlakeIdGenerator

use of com.hazelcast.flakeidgen.FlakeIdGenerator in project hazelcast by hazelcast.

the class FlakeIdGenerator_MemberIntegrationTest method statistics.

@Test
public void statistics() {
    HazelcastInstance instance = factory.newHazelcastInstance();
    FlakeIdGenerator gen = instance.getFlakeIdGenerator("gen");
    gen.newId();
    FlakeIdGeneratorService service = getNodeEngineImpl(instance).getService(FlakeIdGeneratorService.SERVICE_NAME);
    Map<String, LocalFlakeIdGeneratorStats> stats = service.getStats();
    assertTrue(!stats.isEmpty());
    assertTrue(stats.containsKey("gen"));
    LocalFlakeIdGeneratorStats genStats = stats.get("gen");
    assertEquals(1L, genStats.getBatchCount());
    assertTrue(genStats.getIdCount() > 0);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) FlakeIdGenerator(com.hazelcast.flakeidgen.FlakeIdGenerator) LocalFlakeIdGeneratorStats(com.hazelcast.internal.monitor.LocalFlakeIdGeneratorStats) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with FlakeIdGenerator

use of com.hazelcast.flakeidgen.FlakeIdGenerator in project hazelcast by hazelcast.

the class FlakeIdGenerator_NodeIdOverflowIntegrationTest method when_memberOutOfRangeNodeId_then_theOtherMemberUsed.

@Test
public void when_memberOutOfRangeNodeId_then_theOtherMemberUsed() {
    // Design of this test: we create two members. To one of them, out-of-range memberListJoinVersion
    // is assigned. This causes the client message to fail and the client will retry with another member
    // and will never again try the failed member.
    // 
    // Probability of initially choosing a good member and not going through the process of choosing another
    // one is 50%. To avoid false success, we retry 10 times, which gives the probability of false success
    // of 0.1%.
    assignOverflowedNodeId(instance2);
    ClientConfig clientConfig = new ClientConfig();
    // disable smart routing - such clients must also work reliably
    clientConfig.getNetworkConfig().setSmartRouting(false);
    for (int i = 0; i < 10; i++) {
        LOGGER.info("Creating client " + i);
        HazelcastInstance client = factory.newHazelcastClient(clientConfig);
        FlakeIdGenerator gen = client.getFlakeIdGenerator("gen");
        for (int j = 0; j < 100; j++) {
            // call should not fail
            gen.newId();
        }
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) FlakeIdGenerator(com.hazelcast.flakeidgen.FlakeIdGenerator) ClientConfig(com.hazelcast.client.config.ClientConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with FlakeIdGenerator

use of com.hazelcast.flakeidgen.FlakeIdGenerator in project hazelcast by hazelcast.

the class FlakeIdGenerator_NodeIdOverflowIntegrationTest method when_allMembersOutOfRangeNodeId_then_error.

@Test
public void when_allMembersOutOfRangeNodeId_then_error() {
    assignOverflowedNodeId(instance1);
    assignOverflowedNodeId(instance2);
    HazelcastInstance client = factory.newHazelcastClient();
    FlakeIdGenerator gen = client.getFlakeIdGenerator("gen");
    exception.expect(HazelcastException.class);
    exception.expectMessage("All members have node ID out of range");
    gen.newId();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) FlakeIdGenerator(com.hazelcast.flakeidgen.FlakeIdGenerator) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

FlakeIdGenerator (com.hazelcast.flakeidgen.FlakeIdGenerator)10 Test (org.junit.Test)10 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 ClientConfig (com.hazelcast.client.config.ClientConfig)2 ClientFlakeIdGeneratorConfig (com.hazelcast.client.config.ClientFlakeIdGeneratorConfig)1 LocalFlakeIdGeneratorStats (com.hazelcast.internal.monitor.LocalFlakeIdGeneratorStats)1 SlowTest (com.hazelcast.test.annotation.SlowTest)1