com.hazelcast.nio.Address Java Examples

The following examples show how to use com.hazelcast.nio.Address. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LogicalNodeTableTestCase.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Test(expected = SnowcastNodeIdsExceededException.class)
public void test_attach_exceed_nodes_count()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    LogicalNodeTable logicalNodeTable = new LogicalNodeTable(1, definition);

    try {
        for (int i = 0; i < definition.getBoundedMaxLogicalNodeCount(); i++) {
            Address address = new Address("localhost", 10000 + i);
            int logicalNodeId = logicalNodeTable.attachLogicalNode(address);
            Address assigned = logicalNodeTable.getAttachedLogicalNode(logicalNodeId);
            assertEquals(address, assigned);
        }
    } catch (SnowcastNodeIdsExceededException e) {
        fail("Node Ids exceeded too early");
    }

    Address address2 = new Address("localhost", 54321);
    logicalNodeTable.attachLogicalNode(address2);
}
 
Example #2
Source File: LogicalNodeTable.java    From snowcast with Apache License 2.0 6 votes vote down vote up
void detachLogicalNode(@Nonnull Address address, @Min(128) @Max(8192) int logicalNodeId) {
    while (true) {
        Object[] assignmentTable = this.assignmentTable;
        Address addressOnSlot = (Address) assignmentTable[logicalNodeId];
        if (addressOnSlot == null) {
            break;
        }

        if (!address.equals(addressOnSlot)) {
            throw exception(SnowcastIllegalStateException::new, ILLEGAL_DETACH_ATTEMPT);
        }

        long offset = offset(logicalNodeId);
        if (UNSAFE.compareAndSwapObject(assignmentTable, offset, addressOnSlot, null)) {
            break;
        }
    }
}
 
Example #3
Source File: LogicalNodeTableTestCase.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Test
public void test_attach_multiple_nodes()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    LogicalNodeTable logicalNodeTable = new LogicalNodeTable(1, definition);

    Address address1 = new Address("localhost", 12345);
    int logicalNodeId1 = logicalNodeTable.attachLogicalNode(address1);
    Address assigned1 = logicalNodeTable.getAttachedLogicalNode(logicalNodeId1);
    assertEquals(address1, assigned1);

    Address address2 = new Address("localhost", 54321);
    int logicalNodeId2 = logicalNodeTable.attachLogicalNode(address2);
    Address assigned2 = logicalNodeTable.getAttachedLogicalNode(logicalNodeId2);
    assertNotEquals(logicalNodeId1, logicalNodeId2);
    assertEquals(address2, assigned2);
}
 
Example #4
Source File: LogicalNodeTable.java    From snowcast with Apache License 2.0 6 votes vote down vote up
static void writeLogicalNodeTable(@Nonnull LogicalNodeTable logicalNodeTable, @Nonnull ObjectDataOutput out)
        throws IOException {

    out.writeObject(logicalNodeTable.definition);

    Object[] assignmentTable = logicalNodeTable.assignmentTable;
    int length = assignmentTable.length;

    int size = 0;
    for (int i = 0; i < length; i++) {
        if (assignmentTable[i] != null) {
            size++;
        }
    }

    out.writeShort(size);
    for (int i = 0; i < length; i++) {
        Address address = (Address) assignmentTable[i];
        if (address != null) {
            out.writeShort(i);
            address.writeData(out);
        }
    }
}
 
Example #5
Source File: LogicalNodeTable.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Nonnull
static LogicalNodeTable readLogicalNodeTable(@Nonnegative int partitionId, @Nonnull ObjectDataInput in)
        throws IOException {

    SequencerDefinition definition = in.readObject();
    short size = in.readShort();

    Object[] assignmentTable = new Object[definition.getBoundedMaxLogicalNodeCount()];
    for (int i = 0; i < size; i++) {
        short index = in.readShort();
        Address address = new Address();
        address.readData(in);
        assignmentTable[index] = address;
    }
    return new LogicalNodeTable(partitionId, definition, assignmentTable);
}
 
Example #6
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Test
public void test_unfreeze_partition()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address1 = new Address("localhost", 1000);
    Address address2 = new Address("localhost", 1002);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address1);
    assertNotNull(logicalNodeId);

    partition.freeze();

    try {
        partition.attachLogicalNode(definition, address2);
        fail("Partition is not successfully frozen");
    } catch (SnowcastIllegalStateException e) {
        // expected, therefore ignore
    }

    partition.unfreeze();
    partition.attachLogicalNode(definition, address2);
}
 
Example #7
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Test
public void test_unfreeze_partition_double()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address1 = new Address("localhost", 1000);
    Address address2 = new Address("localhost", 1002);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address1);
    assertNotNull(logicalNodeId);

    partition.freeze();

    try {
        partition.attachLogicalNode(definition, address2);
        fail("Partition is not successfully frozen");
    } catch (SnowcastIllegalStateException e) {
        // expected, therefore ignore
    }

    partition.unfreeze();
    partition.unfreeze();
}
 
Example #8
Source File: LogicalNodeTableTestCase.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Test(expected = SnowcastIllegalStateException.class)
public void test_illegal_detach()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    LogicalNodeTable logicalNodeTable = new LogicalNodeTable(1, definition);

    Address address1 = new Address("localhost", 12345);
    int logicalNodeId1 = logicalNodeTable.attachLogicalNode(address1);
    Address assigned1 = logicalNodeTable.getAttachedLogicalNode(logicalNodeId1);
    assertEquals(address1, assigned1);

    Address address2 = new Address("localhost", 54321);
    logicalNodeTable.attachLogicalNode(address2);

    logicalNodeTable.detachLogicalNode(address1, 1);
}
 
Example #9
Source File: BackupAttachLogicalNodeOperation.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Override
protected void readInternal(ObjectDataInput in)
        throws IOException {

    super.readInternal(in);
    logicalNodeId = in.readInt();
    address = new Address();
    address.readData(in);

    long epochOffset = in.readLong();
    int maxLogicalNodeCount = in.readInt();
    short backupCount = in.readShort();

    SnowcastEpoch epoch = SnowcastEpoch.byTimestamp(epochOffset);
    definition = new SequencerDefinition(getSequencerName(), epoch, maxLogicalNodeCount, backupCount);
}
 
Example #10
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Test(expected = SnowcastSequencerAlreadyRegisteredException.class)
public void test_partition_merge_unsuccessful_different_definitions_same_name()
        throws Exception {

    SequencerDefinition definition1 = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    SequencerDefinition definition2 = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(2), 128, (short) 1);

    Address address1 = new Address("localhost", 1000);
    Address address2 = new Address("localhost", 1000);

    SequencerPartition partition1 = new SequencerPartition(1);
    partition1.assignLogicalNode(definition1, 0, address1);

    SequencerPartition partition2 = new SequencerPartition(1);
    partition2.assignLogicalNode(definition2, 0, address2);

    PartitionReplication replication = partition1.createPartitionReplication();
    replication.applyReplication(partition2);
}
 
Example #11
Source File: BackupDetachLogicalNodeOperation.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Override
protected void readInternal(ObjectDataInput in)
        throws IOException {

    super.readInternal(in);
    logicalNodeId = in.readInt();
    address = new Address();
    address.readData(in);

    long epochOffset = in.readLong();
    int maxLogicalNodeCount = in.readInt();
    short backupCount = in.readShort();

    SnowcastEpoch epoch = SnowcastEpoch.byTimestamp(epochOffset);
    definition = new SequencerDefinition(getSequencerName(), epoch, maxLogicalNodeCount, backupCount);
}
 
Example #12
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Test
public void test_partition_merge_unsuccessful_different_definitions_different_name()
        throws Exception {

    SequencerDefinition definition1 = new SequencerDefinition("empty1", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    SequencerDefinition definition2 = new SequencerDefinition("empty2", SnowcastEpoch.byTimestamp(1), 128, (short) 1);

    Address address1 = new Address("localhost", 1000);
    Address address2 = new Address("localhost", 1000);

    SequencerPartition partition1 = new SequencerPartition(1);
    partition1.assignLogicalNode(definition1, 0, address1);

    SequencerPartition partition2 = new SequencerPartition(1);
    partition2.assignLogicalNode(definition2, 0, address2);

    PartitionReplication replication = partition1.createPartitionReplication();
    replication.applyReplication(partition2);
}
 
Example #13
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Test(expected = SnowcastIllegalStateException.class)
public void test_partition_merge_unsuccessful_double_logical_node_id()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address1 = new Address("localhost", 1000);
    Address address2 = new Address("localhost", 1001);

    SequencerPartition partition1 = new SequencerPartition(1);
    partition1.assignLogicalNode(definition, 0, address1);

    SequencerPartition partition2 = new SequencerPartition(1);
    partition2.assignLogicalNode(definition, 0, address2);

    PartitionReplication replication = partition1.createPartitionReplication();
    replication.applyReplication(partition2);
}
 
Example #14
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 6 votes vote down vote up
@Test
public void test_partition_merge_successful()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address1 = new Address("localhost", 1000);
    Address address2 = new Address("localhost", 1001);

    SequencerPartition partition1 = new SequencerPartition(1);
    partition1.assignLogicalNode(definition, 0, address1);

    SequencerPartition partition2 = new SequencerPartition(1);
    partition2.assignLogicalNode(definition, 1, address2);

    PartitionReplication replication = partition1.createPartitionReplication();
    replication.applyReplication(partition2);
}
 
Example #15
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test(expected = SnowcastIllegalStateException.class)
public void test_freeze_partition()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address1 = new Address("localhost", 1000);
    Address address2 = new Address("localhost", 1002);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address1);
    assertNotNull(logicalNodeId);

    partition.freeze();
    partition.attachLogicalNode(definition, address2);
}
 
Example #16
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test(expected = SnowcastIllegalStateException.class)
public void test_partition_frozen_destroy_definition()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address);
    assertNotNull(logicalNodeId);

    partition.freeze();
    partition.destroySequencerDefinition(definition.getSequencerName());
}
 
Example #17
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test
public void test_double_registration()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address);
    assertNotNull(logicalNodeId);

    partition.checkOrRegisterSequencerDefinition(definition);
}
 
Example #18
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test(expected = SnowcastSequencerAlreadyRegisteredException.class)
public void test_double_registration_illegal()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address);
    assertNotNull(logicalNodeId);

    SequencerDefinition otherDefinition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 1000, (short) 1);
    partition.checkOrRegisterSequencerDefinition(otherDefinition);
}
 
Example #19
Source File: LogicalNodeTableTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test
public void test_attach()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    LogicalNodeTable logicalNodeTable = new LogicalNodeTable(1, definition);

    Address address = new Address("localhost", 12345);

    int logicalNodeId = logicalNodeTable.attachLogicalNode(address);
    Address assigned = logicalNodeTable.getAttachedLogicalNode(logicalNodeId);

    assertEquals(address, assigned);
}
 
Example #20
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test
public void test_freeze_partition_double()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address1 = new Address("localhost", 1000);
    Address address2 = new Address("localhost", 1002);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address1);
    assertNotNull(logicalNodeId);

    partition.freeze();
    partition.freeze();
}
 
Example #21
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test(expected = SnowcastIllegalStateException.class)
public void test_partition_frozen_detach()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address);
    assertNotNull(logicalNodeId);

    partition.freeze();
    partition.detachLogicalNode(definition.getSequencerName(), address, logicalNodeId);
}
 
Example #22
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test(expected = SnowcastIllegalStateException.class)
public void test_partition_frozen_assign()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address);
    assertNotNull(logicalNodeId);

    partition.freeze();
    partition.assignLogicalNode(definition, logicalNodeId, address);
}
 
Example #23
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test(expected = SnowcastIllegalStateException.class)
public void test_partition_frozen_unassign()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address);
    assertNotNull(logicalNodeId);

    partition.freeze();
    partition.unassignLogicalNode(definition, logicalNodeId, address);
}
 
Example #24
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test
public void test_partition_frozen_get_assignment()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address);
    assertNotNull(logicalNodeId);

    partition.freeze();
    Address assigned = partition.getAttachedLogicalNode(definition.getSequencerName(), logicalNodeId);
    assertEquals(address, assigned);
}
 
Example #25
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test
public void test_partition_frozen_get_definition()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address);
    assertNotNull(logicalNodeId);

    partition.freeze();
    SequencerDefinition sequencerDefinition = partition.getSequencerDefinition(definition.getSequencerName());
    assertEquals(definition, sequencerDefinition);
}
 
Example #26
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test
public void test_partition_frozen_register_definition_existing()
        throws Exception {

    SequencerDefinition definition = new SequencerDefinition("empty", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition, address);
    assertNotNull(logicalNodeId);

    partition.freeze();
    partition.checkOrRegisterSequencerDefinition(definition);
}
 
Example #27
Source File: SequencePartitionTestCase.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Test(expected = SnowcastIllegalStateException.class)
public void test_partition_frozen_register_definition_new()
        throws Exception {

    SequencerDefinition definition1 = new SequencerDefinition("empty1", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    SequencerDefinition definition2 = new SequencerDefinition("empty2", SnowcastEpoch.byTimestamp(1), 128, (short) 1);
    Address address = new Address("localhost", 1000);

    SequencerPartition partition = new SequencerPartition(1);
    Integer logicalNodeId = partition.attachLogicalNode(definition1, address);
    assertNotNull(logicalNodeId);

    partition.freeze();
    partition.checkOrRegisterSequencerDefinition(definition2);
}
 
Example #28
Source File: LogicalNodeTable.java    From snowcast with Apache License 2.0 5 votes vote down vote up
void assignLogicalNode(@Min(128) @Max(8192) int logicalNodeId, @Nonnull Address address) {
    while (true) {
        Object[] assignmentTable = this.assignmentTable;
        if (assignmentTable[logicalNodeId] != null) {
            throw exception(SnowcastIllegalStateException::new, BACKUP_OUT_OF_SYNC, partitionId);
        }

        long offset = offset(logicalNodeId);
        if (UNSAFE.compareAndSwapObject(assignmentTable, offset, null, address)) {
            break;
        }
    }
}
 
Example #29
Source File: LogicalNodeTable.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Min(128)
@Max(8192)
int attachLogicalNode(@Nonnull Address address) {
    while (true) {
        Object[] assignmentTable = this.assignmentTable;
        int freeSlot = findFreeSlot(assignmentTable);
        if (freeSlot == -1) {
            throw new SnowcastNodeIdsExceededException();
        }
        long offset = offset(freeSlot);
        if (UNSAFE.compareAndSwapObject(assignmentTable, offset, null, address)) {
            return freeSlot;
        }
    }
}
 
Example #30
Source File: SequencerPartition.java    From snowcast with Apache License 2.0 5 votes vote down vote up
@Nullable
Address getAttachedLogicalNode(@Nonnull String sequencerName, @Nonnegative int logicalNodeId) {
    LogicalNodeTable logicalNodeTable = logicalNodeTables.get(sequencerName);
    if (logicalNodeTable == null) {
        throw exception(SnowcastIllegalStateException::new, UNREGISTERED_SEQUENCER_LOGICAL_NODE_TABLE, partitionId);
    }
    return logicalNodeTable.getAttachedLogicalNode(logicalNodeId);
}