Search in sources :

Example 86 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class GossiperTest method testReloadSeeds.

// Note: This test might fail if for some reason the node broadcast address is in 127.99.0.0/16
@Test
public void testReloadSeeds() throws UnknownHostException {
    Gossiper gossiper = new Gossiper(false);
    List<String> loadedList;
    // Initialize the seed list directly to a known set to start with
    gossiper.seeds.clear();
    InetAddressAndPort addr = InetAddressAndPort.getByAddress(InetAddress.getByName("127.99.1.1"));
    int nextSize = 4;
    List<InetAddressAndPort> nextSeeds = new ArrayList<>(nextSize);
    for (int i = 0; i < nextSize; i++) {
        gossiper.seeds.add(addr);
        nextSeeds.add(addr);
        addr = InetAddressAndPort.getByAddress(InetAddresses.increment(addr.getAddress()));
    }
    Assert.assertEquals(nextSize, gossiper.seeds.size());
    // Add another unique address to the list
    addr = InetAddressAndPort.getByAddress(InetAddresses.increment(addr.getAddress()));
    nextSeeds.add(addr);
    nextSize++;
    DatabaseDescriptor.setSeedProvider(new TestSeedProvider(nextSeeds));
    loadedList = gossiper.reloadSeeds();
    // Check that the new entry was added
    Assert.assertEquals(nextSize, loadedList.size());
    for (InetAddressAndPort a : nextSeeds) assertTrue(loadedList.contains(a.toString()));
    // Check that the return value of the reloadSeeds matches the content of the getSeeds call
    // and that they both match the internal contents of the Gossiper seeds list
    Assert.assertEquals(loadedList.size(), gossiper.getSeeds().size());
    for (InetAddressAndPort a : gossiper.seeds) {
        assertTrue(loadedList.contains(a.toString()));
        assertTrue(gossiper.getSeeds().contains(a.toString()));
    }
    // Add a duplicate of the last address to the seed provider list
    int uniqueSize = nextSize;
    nextSeeds.add(addr);
    nextSize++;
    DatabaseDescriptor.setSeedProvider(new TestSeedProvider(nextSeeds));
    loadedList = gossiper.reloadSeeds();
    // Check that the number of seed nodes reported hasn't increased
    Assert.assertEquals(uniqueSize, loadedList.size());
    for (InetAddressAndPort a : nextSeeds) assertTrue(loadedList.contains(a.toString()));
    // Create a new list that has no overlaps with the previous list
    addr = InetAddressAndPort.getByAddress(InetAddress.getByName("127.99.2.1"));
    int disjointSize = 3;
    List<InetAddressAndPort> disjointSeeds = new ArrayList<>(disjointSize);
    for (int i = 0; i < disjointSize; i++) {
        disjointSeeds.add(addr);
        addr = InetAddressAndPort.getByAddress(InetAddresses.increment(addr.getAddress()));
    }
    DatabaseDescriptor.setSeedProvider(new TestSeedProvider(disjointSeeds));
    loadedList = gossiper.reloadSeeds();
    // Check that the list now contains exactly the new other list.
    Assert.assertEquals(disjointSize, gossiper.getSeeds().size());
    Assert.assertEquals(disjointSize, loadedList.size());
    for (InetAddressAndPort a : disjointSeeds) {
        assertTrue(gossiper.getSeeds().contains(a.toString()));
        assertTrue(loadedList.contains(a.toString()));
    }
    // Set the seed node provider to return an empty list
    DatabaseDescriptor.setSeedProvider(new TestSeedProvider(new ArrayList<InetAddressAndPort>()));
    loadedList = gossiper.reloadSeeds();
    // Check that the in memory seed node list was not modified
    Assert.assertEquals(disjointSize, loadedList.size());
    for (InetAddressAndPort a : disjointSeeds) assertTrue(loadedList.contains(a.toString()));
    // Change the seed provider to one that throws an unchecked exception
    DatabaseDescriptor.setSeedProvider(new ErrorSeedProvider());
    loadedList = gossiper.reloadSeeds();
    // Check for the expected null response from a reload error
    assertNull(loadedList);
    // Check that the in memory seed node list was not modified and the exception was caught
    Assert.assertEquals(disjointSize, gossiper.getSeeds().size());
    for (InetAddressAndPort a : disjointSeeds) assertTrue(gossiper.getSeeds().contains(a.toString()));
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 87 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class SerializationsTest method testGossipDigestWrite.

private void testGossipDigestWrite() throws IOException {
    Map<InetAddressAndPort, EndpointState> states = new HashMap<>();
    states.put(InetAddressAndPort.getByName("127.0.0.1"), Statics.EndpointSt);
    states.put(InetAddressAndPort.getByName("127.0.0.2"), Statics.EndpointSt);
    GossipDigestAck ack = new GossipDigestAck(Statics.Digests, states);
    GossipDigestAck2 ack2 = new GossipDigestAck2(states);
    GossipDigestSyn syn = new GossipDigestSyn("Not a real cluster name", StorageService.instance.getTokenMetadata().partitioner.getClass().getCanonicalName(), Statics.Digests);
    DataOutputStreamPlus out = getOutput("gms.Gossip.bin");
    for (GossipDigest gd : Statics.Digests) GossipDigest.serializer.serialize(gd, out, getVersion());
    GossipDigestAck.serializer.serialize(ack, out, getVersion());
    GossipDigestAck2.serializer.serialize(ack2, out, getVersion());
    GossipDigestSyn.serializer.serialize(syn, out, getVersion());
    out.close();
    // test serializedSize
    for (GossipDigest gd : Statics.Digests) testSerializedSize(gd, GossipDigest.serializer);
    testSerializedSize(ack, GossipDigestAck.serializer);
    testSerializedSize(ack2, GossipDigestAck2.serializer);
    testSerializedSize(syn, GossipDigestSyn.serializer);
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) HashMap(java.util.HashMap) DataOutputStreamPlus(org.apache.cassandra.io.util.DataOutputStreamPlus)

Example 88 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class CassandraGenerators method toStringRecursive.

/**
 * Uses reflection to generate a toString.  This method is aware of common Cassandra classes and can be used for
 * generators or tests to provide more details for debugging.
 */
public static String toStringRecursive(Object o) {
    return ReflectionToStringBuilder.toString(o, new MultilineRecursiveToStringStyle() {

        private String spacer = "";

        {
            // common lang uses start/end chars that are not the common ones used, so switch to the common ones
            setArrayStart("[");
            setArrayEnd("]");
            setContentStart("{");
            setContentEnd("}");
            setUseIdentityHashCode(false);
            setUseShortClassName(true);
        }

        protected boolean accept(Class<?> clazz) {
            return // toString enums
            !clazz.isEnum() && // if no fields, just toString
            Stream.of(clazz.getDeclaredFields()).anyMatch(f -> !Modifier.isStatic(f.getModifiers()));
        }

        public void appendDetail(StringBuffer buffer, String fieldName, Object value) {
            if (value instanceof ByteBuffer) {
                value = ByteBufferUtil.bytesToHex((ByteBuffer) value);
            } else if (value instanceof AbstractType) {
                value = SchemaCQLHelper.toCqlType((AbstractType) value);
            } else if (value instanceof Token || value instanceof InetAddressAndPort || value instanceof FieldIdentifier) {
                value = value.toString();
            } else if (value instanceof TableMetadata) {
                // to make sure the correct indents are taken, convert to CQL, then replace newlines with the indents
                // then prefix with the indents.
                String cql = SchemaCQLHelper.getTableMetadataAsCQL((TableMetadata) value);
                cql = NEWLINE_PATTERN.matcher(cql).replaceAll(Matcher.quoteReplacement("\n  " + spacer));
                cql = "\n  " + spacer + cql;
                value = cql;
            }
            super.appendDetail(buffer, fieldName, value);
        }

        // MultilineRecursiveToStringStyle doesn't look at what was set and instead hard codes the values when it "resets" the level
        protected void setArrayStart(String arrayStart) {
            super.setArrayStart(arrayStart.replace("{", "["));
        }

        protected void setArrayEnd(String arrayEnd) {
            super.setArrayEnd(arrayEnd.replace("}", "]"));
        }

        protected void setContentStart(String contentStart) {
            // use this to infer the spacer since it isn't exposed.
            String[] split = contentStart.split("\n", 2);
            spacer = split.length == 2 ? split[1] : "";
            super.setContentStart(contentStart.replace("[", "{"));
        }

        protected void setContentEnd(String contentEnd) {
            super.setContentEnd(contentEnd.replace("]", "}"));
        }
    }, true);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) MultilineRecursiveToStringStyle(org.apache.commons.lang3.builder.MultilineRecursiveToStringStyle) AbstractType(org.apache.cassandra.db.marshal.AbstractType) FieldIdentifier(org.apache.cassandra.cql3.FieldIdentifier) Token(org.apache.cassandra.dht.Token) ByteBuffer(java.nio.ByteBuffer)

Example 89 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class ValidatorTest method testValidatorFailed.

@Test
public void testValidatorFailed() throws Throwable {
    Range<Token> range = new Range<>(partitioner.getMinimumToken(), partitioner.getRandomToken());
    final RepairJobDesc desc = new RepairJobDesc(UUID.randomUUID(), UUID.randomUUID(), keyspace, columnFamily, Arrays.asList(range));
    final CompletableFuture<Message> outgoingMessageSink = registerOutgoingMessageSink();
    InetAddressAndPort remote = InetAddressAndPort.getByName("127.0.0.2");
    Validator validator = new Validator(desc, remote, 0, PreviewKind.NONE);
    validator.fail();
    Message message = outgoingMessageSink.get(TEST_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(Verb.VALIDATION_RSP, message.verb());
    ValidationResponse m = (ValidationResponse) message.payload;
    assertEquals(desc, m.desc);
    assertFalse(m.success());
    assertNull(m.trees);
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Message(org.apache.cassandra.net.Message) ValidationResponse(org.apache.cassandra.repair.messages.ValidationResponse) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) CompactionsTest(org.apache.cassandra.db.compaction.CompactionsTest) Test(org.junit.Test)

Example 90 with InetAddressAndPort

use of org.apache.cassandra.locator.InetAddressAndPort in project cassandra by apache.

the class ValidatorTest method testValidatorComplete.

@Test
public void testValidatorComplete() throws Throwable {
    Range<Token> range = new Range<>(partitioner.getMinimumToken(), partitioner.getRandomToken());
    final RepairJobDesc desc = new RepairJobDesc(UUID.randomUUID(), UUID.randomUUID(), keyspace, columnFamily, Arrays.asList(range));
    final CompletableFuture<Message> outgoingMessageSink = registerOutgoingMessageSink();
    InetAddressAndPort remote = InetAddressAndPort.getByName("127.0.0.2");
    ColumnFamilyStore cfs = Keyspace.open(keyspace).getColumnFamilyStore(columnFamily);
    Validator validator = new Validator(desc, remote, 0, PreviewKind.NONE);
    MerkleTrees trees = new MerkleTrees(partitioner);
    trees.addMerkleTrees((int) Math.pow(2, 15), validator.desc.ranges);
    validator.prepare(cfs, trees);
    // and confirm that the trees were split
    assertTrue(trees.size() > 1);
    // add a row
    Token mid = partitioner.midpoint(range.left, range.right);
    validator.add(EmptyIterators.unfilteredRow(cfs.metadata(), new BufferDecoratedKey(mid, ByteBufferUtil.bytes("inconceivable!")), false));
    validator.complete();
    // confirm that the trees were validated
    Token min = trees.partitioner().getMinimumToken();
    assertNotNull(trees.hash(new Range<>(min, min)));
    Message message = outgoingMessageSink.get(TEST_TIMEOUT, TimeUnit.SECONDS);
    assertEquals(Verb.VALIDATION_RSP, message.verb());
    ValidationResponse m = (ValidationResponse) message.payload;
    assertEquals(desc, m.desc);
    assertTrue(m.success());
    assertNotNull(m.trees);
}
Also used : MerkleTrees(org.apache.cassandra.utils.MerkleTrees) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Message(org.apache.cassandra.net.Message) ValidationResponse(org.apache.cassandra.repair.messages.ValidationResponse) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Token(org.apache.cassandra.dht.Token) BufferDecoratedKey(org.apache.cassandra.db.BufferDecoratedKey) Range(org.apache.cassandra.dht.Range) CompactionsTest(org.apache.cassandra.db.compaction.CompactionsTest) Test(org.junit.Test)

Aggregations

InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)281 Test (org.junit.Test)129 Token (org.apache.cassandra.dht.Token)65 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)43 EndpointsForRange (org.apache.cassandra.locator.EndpointsForRange)39 Range (org.apache.cassandra.dht.Range)28 Replica (org.apache.cassandra.locator.Replica)25 ArrayList (java.util.ArrayList)24 ByteBuffer (java.nio.ByteBuffer)23 HashMap (java.util.HashMap)23 UUID (java.util.UUID)22 HashSet (java.util.HashSet)20 Map (java.util.Map)20 Mutation (org.apache.cassandra.db.Mutation)17 PartitionIterator (org.apache.cassandra.db.partitions.PartitionIterator)17 UnfilteredPartitionIterator (org.apache.cassandra.db.partitions.UnfilteredPartitionIterator)16 VersionedValue (org.apache.cassandra.gms.VersionedValue)16 VisibleForTesting (com.google.common.annotations.VisibleForTesting)15 IPartitioner (org.apache.cassandra.dht.IPartitioner)15 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)15