Search in sources :

Example 81 with InetAddressAndPort

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

the class CassandraEntireSSTableStreamWriterTest method setupStreamingSessionForTest.

private StreamSession setupStreamingSessionForTest() {
    StreamCoordinator streamCoordinator = new StreamCoordinator(StreamOperation.BOOTSTRAP, 1, new NettyStreamingConnectionFactory(), false, false, null, PreviewKind.NONE);
    StreamResultFuture future = StreamResultFuture.createInitiator(UUID.randomUUID(), StreamOperation.BOOTSTRAP, Collections.<StreamEventHandler>emptyList(), streamCoordinator);
    InetAddressAndPort peer = FBUtilities.getBroadcastAddressAndPort();
    streamCoordinator.addSessionInfo(new SessionInfo(peer, 0, peer, Collections.emptyList(), Collections.emptyList(), StreamSession.State.INITIALIZED));
    StreamSession session = streamCoordinator.getOrCreateOutboundSession(peer);
    session.init(future);
    return session;
}
Also used : StreamResultFuture(org.apache.cassandra.streaming.StreamResultFuture) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) NettyStreamingConnectionFactory(org.apache.cassandra.streaming.async.NettyStreamingConnectionFactory) StreamSession(org.apache.cassandra.streaming.StreamSession) StreamCoordinator(org.apache.cassandra.streaming.StreamCoordinator) SessionInfo(org.apache.cassandra.streaming.SessionInfo)

Example 82 with InetAddressAndPort

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

the class CassandraEntireSSTableStreamWriterTest method testBlockReadingAndWritingOverWire.

@Test
public void testBlockReadingAndWritingOverWire() throws Throwable {
    StreamSession session = setupStreamingSessionForTest();
    InetAddressAndPort peer = FBUtilities.getBroadcastAddressAndPort();
    // This is needed as Netty releases the ByteBuffers as soon as the channel is flushed
    ByteBuf serializedFile = Unpooled.buffer(8192);
    EmbeddedChannel channel = createMockNettyChannel(serializedFile);
    try (AsyncStreamingOutputPlus out = new AsyncStreamingOutputPlus(channel);
        ComponentContext context = ComponentContext.create(descriptor)) {
        CassandraEntireSSTableStreamWriter writer = new CassandraEntireSSTableStreamWriter(sstable, session, context);
        writer.write(out);
        session.prepareReceiving(new StreamSummary(sstable.metadata().id, 1, 5104));
        CassandraStreamHeader header = CassandraStreamHeader.builder().withSSTableFormat(sstable.descriptor.formatType).withSSTableVersion(sstable.descriptor.version).withSSTableLevel(0).withEstimatedKeys(sstable.estimatedKeys()).withSections(Collections.emptyList()).withSerializationHeader(sstable.header.toComponent()).withComponentManifest(context.manifest()).isEntireSSTable(true).withFirstKey(sstable.first).withTableId(sstable.metadata().id).build();
        CassandraEntireSSTableStreamReader reader = new CassandraEntireSSTableStreamReader(new StreamMessageHeader(sstable.metadata().id, peer, session.planId(), false, 0, 0, 0, null), header, session);
        SSTableMultiWriter sstableWriter = reader.read(new DataInputBuffer(serializedFile.nioBuffer(), false));
        Collection<SSTableReader> newSstables = sstableWriter.finished();
        assertEquals(1, newSstables.size());
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) AsyncStreamingOutputPlus(org.apache.cassandra.net.AsyncStreamingOutputPlus) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) StreamSession(org.apache.cassandra.streaming.StreamSession) StreamSummary(org.apache.cassandra.streaming.StreamSummary) StreamMessageHeader(org.apache.cassandra.streaming.messages.StreamMessageHeader) SSTableMultiWriter(org.apache.cassandra.io.sstable.SSTableMultiWriter) Test(org.junit.Test)

Example 83 with InetAddressAndPort

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

the class ExpireEndpointTest method testExpireEndpoint.

@Test
public void testExpireEndpoint() throws UnknownHostException {
    InetAddressAndPort hostAddress = InetAddressAndPort.getByName("127.0.0.2");
    UUID hostId = UUID.randomUUID();
    long expireTime = System.currentTimeMillis() - 1;
    Gossiper.instance.initializeNodeUnsafe(hostAddress, hostId, 1);
    EndpointState endpointState = Gossiper.instance.getEndpointStateForEndpoint(hostAddress);
    Gossiper.runInGossipStageBlocking(() -> Gossiper.instance.markDead(hostAddress, endpointState));
    endpointState.addApplicationState(ApplicationState.STATUS_WITH_PORT, StorageService.instance.valueFactory.removedNonlocal(hostId, expireTime));
    Gossiper.instance.addExpireTimeForEndpoint(hostAddress, expireTime);
    assertTrue("Expiring endpoint not unreachable before status check", Gossiper.instance.getUnreachableMembers().contains(hostAddress));
    Gossiper.instance.doStatusCheck();
    assertFalse("Expired endpoint still part of live members", Gossiper.instance.getLiveMembers().contains(hostAddress));
    assertFalse("Expired endpoint still part of unreachable members", Gossiper.instance.getUnreachableMembers().contains(hostAddress));
    assertNull("Expired endpoint still contain endpoint state", Gossiper.instance.getEndpointStateForEndpoint(hostAddress));
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) UUID(java.util.UUID) Test(org.junit.Test)

Example 84 with InetAddressAndPort

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

the class GossiperTest method testLargeGenerationJump.

@Test
public void testLargeGenerationJump() throws UnknownHostException, InterruptedException {
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, 2);
    try {
        InetAddressAndPort remoteHostAddress = hosts.get(1);
        EndpointState initialRemoteState = Gossiper.instance.getEndpointStateForEndpoint(remoteHostAddress);
        HeartBeatState initialRemoteHeartBeat = initialRemoteState.getHeartBeatState();
        // Util.createInitialRing should have initialized remoteHost's HeartBeatState's generation to 1
        assertEquals(initialRemoteHeartBeat.getGeneration(), 1);
        HeartBeatState proposedRemoteHeartBeat = new HeartBeatState(initialRemoteHeartBeat.getGeneration() + Gossiper.MAX_GENERATION_DIFFERENCE + 1);
        EndpointState proposedRemoteState = new EndpointState(proposedRemoteHeartBeat);
        Gossiper.instance.applyStateLocally(ImmutableMap.of(remoteHostAddress, proposedRemoteState));
        // The generation should have been updated because it isn't over Gossiper.MAX_GENERATION_DIFFERENCE in the future
        HeartBeatState actualRemoteHeartBeat = Gossiper.instance.getEndpointStateForEndpoint(remoteHostAddress).getHeartBeatState();
        assertEquals(proposedRemoteHeartBeat.getGeneration(), actualRemoteHeartBeat.getGeneration());
        // Propose a generation 10 years in the future - this should be rejected.
        HeartBeatState badProposedRemoteHeartBeat = new HeartBeatState((int) (System.currentTimeMillis() / 1000) + Gossiper.MAX_GENERATION_DIFFERENCE * 10);
        EndpointState badProposedRemoteState = new EndpointState(badProposedRemoteHeartBeat);
        Gossiper.instance.applyStateLocally(ImmutableMap.of(remoteHostAddress, badProposedRemoteState));
        actualRemoteHeartBeat = Gossiper.instance.getEndpointStateForEndpoint(remoteHostAddress).getHeartBeatState();
        // The generation should not have been updated because it is over Gossiper.MAX_GENERATION_DIFFERENCE in the future
        assertEquals(proposedRemoteHeartBeat.getGeneration(), actualRemoteHeartBeat.getGeneration());
    } finally {
        // clean up the gossip states
        Gossiper.instance.endpointStateMap.clear();
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Test(org.junit.Test)

Example 85 with InetAddressAndPort

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

the class GossiperTest method testNotFireDuplicatedNotificationsWithUpdateContainsOldAndNewState.

@Test
public void testNotFireDuplicatedNotificationsWithUpdateContainsOldAndNewState() throws UnknownHostException {
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(DatabaseDescriptor.getPartitioner());
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, 2);
    SimpleStateChangeListener stateChangeListener = null;
    try {
        InetAddressAndPort remoteHostAddress = hosts.get(1);
        EndpointState initialRemoteState = Gossiper.instance.getEndpointStateForEndpoint(remoteHostAddress);
        HeartBeatState initialRemoteHeartBeat = initialRemoteState.getHeartBeatState();
        // Util.createInitialRing should have initialized remoteHost's HeartBeatState's generation to 1
        assertEquals(initialRemoteHeartBeat.getGeneration(), 1);
        // Test begins
        AtomicInteger notificationCount = new AtomicInteger(0);
        HeartBeatState proposedRemoteHeartBeat = new HeartBeatState(initialRemoteHeartBeat.getGeneration());
        EndpointState proposedRemoteState = new EndpointState(proposedRemoteHeartBeat);
        final Token token = DatabaseDescriptor.getPartitioner().getRandomToken();
        proposedRemoteState.addApplicationState(ApplicationState.STATUS, valueFactory.normal(Collections.singletonList(token)));
        stateChangeListener = new SimpleStateChangeListener();
        Gossiper.instance.register(stateChangeListener);
        // STEP 1. register verifier and apply state with just STATUS
        // simulate applying gossip state from a v3 peer
        stateChangeListener.setOnChangeVerifier(onChangeParams -> {
            notificationCount.getAndIncrement();
            assertEquals("It should fire notification for STATUS when gossiper local state not yet has STATUS_WITH_PORT", ApplicationState.STATUS, onChangeParams.state);
        });
        Gossiper.instance.applyStateLocally(ImmutableMap.of(remoteHostAddress, proposedRemoteState));
        // STEP 2. includes both STATUS and STATUS_WITH_PORT. The gossiper knows that the remote peer is now in v4
        // update verifier and apply state again
        proposedRemoteState.addApplicationState(ApplicationState.STATUS_WITH_PORT, valueFactory.normal(Collections.singletonList(token)));
        stateChangeListener.setOnChangeVerifier(onChangeParams -> {
            notificationCount.getAndIncrement();
            assertEquals("It should only fire notification for STATUS_WITH_PORT", ApplicationState.STATUS_WITH_PORT, onChangeParams.state);
        });
        Gossiper.instance.applyStateLocally(ImmutableMap.of(remoteHostAddress, proposedRemoteState));
        // STEP 3. somehow, the peer send only the STATUS in the update.
        proposedRemoteState = new EndpointState(proposedRemoteHeartBeat);
        proposedRemoteState.addApplicationState(ApplicationState.STATUS, valueFactory.normal(Collections.singletonList(token)));
        stateChangeListener.setOnChangeVerifier(onChangeParams -> {
            notificationCount.getAndIncrement();
            fail("It should not fire notification for STATUS");
        });
        assertEquals("Expect exact 2 notifications with the test setup", 2, notificationCount.get());
    } finally {
        // clean up the gossip states
        Gossiper.instance.endpointStateMap.clear();
        if (stateChangeListener != null)
            Gossiper.instance.unregister(stateChangeListener);
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Token(org.apache.cassandra.dht.Token) 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