Search in sources :

Example 6 with ClusterId

use of org.neo4j.causalclustering.identity.ClusterId in project neo4j by neo4j.

the class HazelcastClusterTopology method getClusterId.

private static ClusterId getClusterId(HazelcastInstance hazelcastInstance) {
    IAtomicReference<UUID> uuidReference = hazelcastInstance.getAtomicReference(CLUSTER_UUID);
    UUID uuid = uuidReference.get();
    return uuid != null ? new ClusterId(uuid) : null;
}
Also used : ClusterId(org.neo4j.causalclustering.identity.ClusterId) UUID(java.util.UUID)

Example 7 with ClusterId

use of org.neo4j.causalclustering.identity.ClusterId in project neo4j by neo4j.

the class HazelcastClusterTopology method getCoreTopology.

static CoreTopology getCoreTopology(HazelcastInstance hazelcastInstance, Config config, Log log) {
    Map<MemberId, CoreServerInfo> coreMembers = emptyMap();
    boolean canBeBootstrapped = false;
    ClusterId clusterId = null;
    if (hazelcastInstance != null) {
        Set<Member> hzMembers = hazelcastInstance.getCluster().getMembers();
        canBeBootstrapped = canBeBootstrapped(hazelcastInstance, config);
        coreMembers = toCoreMemberMap(hzMembers, log, hazelcastInstance);
        clusterId = getClusterId(hazelcastInstance);
    } else {
        log.info("Cannot currently bind to distributed discovery service.");
    }
    return new CoreTopology(clusterId, canBeBootstrapped, coreMembers);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) ClusterId(org.neo4j.causalclustering.identity.ClusterId) Member(com.hazelcast.core.Member)

Example 8 with ClusterId

use of org.neo4j.causalclustering.identity.ClusterId in project neo4j by neo4j.

the class RaftMessageEncoder method encode.

@Override
protected synchronized void encode(ChannelHandlerContext ctx, RaftMessages.ClusterIdAwareMessage decoratedMessage, ByteBuf out) throws Exception {
    RaftMessages.RaftMessage message = decoratedMessage.message();
    ClusterId clusterId = decoratedMessage.clusterId();
    MemberId.Marshal memberMarshal = new MemberId.Marshal();
    NetworkFlushableByteBuf channel = new NetworkFlushableByteBuf(out);
    ClusterId.Marshal.INSTANCE.marshal(clusterId, channel);
    channel.putInt(message.type().ordinal());
    memberMarshal.marshal(message.from(), channel);
    if (message instanceof RaftMessages.Vote.Request) {
        RaftMessages.Vote.Request voteRequest = (RaftMessages.Vote.Request) message;
        memberMarshal.marshal(voteRequest.candidate(), channel);
        channel.putLong(voteRequest.term());
        channel.putLong(voteRequest.lastLogIndex());
        channel.putLong(voteRequest.lastLogTerm());
    } else if (message instanceof RaftMessages.Vote.Response) {
        RaftMessages.Vote.Response voteResponse = (RaftMessages.Vote.Response) message;
        channel.putLong(voteResponse.term());
        channel.put((byte) (voteResponse.voteGranted() ? 1 : 0));
    } else if (message instanceof RaftMessages.AppendEntries.Request) {
        RaftMessages.AppendEntries.Request appendRequest = (RaftMessages.AppendEntries.Request) message;
        channel.putLong(appendRequest.leaderTerm());
        channel.putLong(appendRequest.prevLogIndex());
        channel.putLong(appendRequest.prevLogTerm());
        channel.putLong(appendRequest.leaderCommit());
        channel.putLong(appendRequest.entries().length);
        for (RaftLogEntry raftLogEntry : appendRequest.entries()) {
            channel.putLong(raftLogEntry.term());
            marshal.marshal(raftLogEntry.content(), channel);
        }
    } else if (message instanceof RaftMessages.AppendEntries.Response) {
        RaftMessages.AppendEntries.Response appendResponse = (RaftMessages.AppendEntries.Response) message;
        channel.putLong(appendResponse.term());
        channel.put((byte) (appendResponse.success() ? 1 : 0));
        channel.putLong(appendResponse.matchIndex());
        channel.putLong(appendResponse.appendIndex());
    } else if (message instanceof RaftMessages.NewEntry.Request) {
        RaftMessages.NewEntry.Request newEntryRequest = (RaftMessages.NewEntry.Request) message;
        marshal.marshal(newEntryRequest.content(), channel);
    } else if (message instanceof RaftMessages.Heartbeat) {
        RaftMessages.Heartbeat heartbeat = (RaftMessages.Heartbeat) message;
        channel.putLong(heartbeat.leaderTerm());
        channel.putLong(heartbeat.commitIndexTerm());
        channel.putLong(heartbeat.commitIndex());
    } else if (message instanceof RaftMessages.HeartbeatResponse) {
    //Heartbeat Response does not have any data attached to it.
    } else if (message instanceof RaftMessages.LogCompactionInfo) {
        RaftMessages.LogCompactionInfo logCompactionInfo = (RaftMessages.LogCompactionInfo) message;
        channel.putLong(logCompactionInfo.leaderTerm());
        channel.putLong(logCompactionInfo.prevIndex());
    } else {
        throw new IllegalArgumentException("Unknown message type: " + message);
    }
}
Also used : NetworkFlushableByteBuf(org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf) ClusterId(org.neo4j.causalclustering.identity.ClusterId) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) MemberId(org.neo4j.causalclustering.identity.MemberId)

Example 9 with ClusterId

use of org.neo4j.causalclustering.identity.ClusterId in project neo4j by neo4j.

the class RaftOutbound method send.

@Override
public void send(MemberId to, RaftMessage message) {
    Optional<ClusterId> clusterId = clusterIdentity.get();
    if (!clusterId.isPresent()) {
        log.warn("Attempting to send a message before bound to a cluster");
        return;
    }
    Optional<CoreServerInfo> coreServerInfo = coreTopologyService.coreServers().find(to);
    if (coreServerInfo.isPresent()) {
        outbound.send(coreServerInfo.get().getRaftServer(), new ClusterIdAwareMessage(clusterId.get(), message));
    } else {
        unknownAddressMonitor.logAttemptToSendToMemberWithNoKnownAddress(to);
    }
}
Also used : ClusterIdAwareMessage(org.neo4j.causalclustering.core.consensus.RaftMessages.ClusterIdAwareMessage) ClusterId(org.neo4j.causalclustering.identity.ClusterId) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo)

Example 10 with ClusterId

use of org.neo4j.causalclustering.identity.ClusterId in project neo4j by neo4j.

the class RaftMessageDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> list) throws Exception {
    ReadableChannel channel = new NetworkReadableClosableChannelNetty4(buffer);
    ClusterId clusterId = ClusterId.Marshal.INSTANCE.unmarshal(channel);
    int messageTypeWire = channel.getInt();
    RaftMessages.Type[] values = RaftMessages.Type.values();
    RaftMessages.Type messageType = values[messageTypeWire];
    MemberId from = retrieveMember(channel);
    RaftMessages.RaftMessage result;
    if (messageType.equals(VOTE_REQUEST)) {
        MemberId candidate = retrieveMember(channel);
        long term = channel.getLong();
        long lastLogIndex = channel.getLong();
        long lastLogTerm = channel.getLong();
        result = new RaftMessages.Vote.Request(from, term, candidate, lastLogIndex, lastLogTerm);
    } else if (messageType.equals(VOTE_RESPONSE)) {
        long term = channel.getLong();
        boolean voteGranted = channel.get() == 1;
        result = new RaftMessages.Vote.Response(from, term, voteGranted);
    } else if (messageType.equals(APPEND_ENTRIES_REQUEST)) {
        // how many
        long term = channel.getLong();
        long prevLogIndex = channel.getLong();
        long prevLogTerm = channel.getLong();
        long leaderCommit = channel.getLong();
        long count = channel.getLong();
        RaftLogEntry[] entries = new RaftLogEntry[(int) count];
        for (int i = 0; i < count; i++) {
            long entryTerm = channel.getLong();
            final ReplicatedContent content = marshal.unmarshal(channel);
            entries[i] = new RaftLogEntry(entryTerm, content);
        }
        result = new RaftMessages.AppendEntries.Request(from, term, prevLogIndex, prevLogTerm, entries, leaderCommit);
    } else if (messageType.equals(APPEND_ENTRIES_RESPONSE)) {
        long term = channel.getLong();
        boolean success = channel.get() == 1;
        long matchIndex = channel.getLong();
        long appendIndex = channel.getLong();
        result = new RaftMessages.AppendEntries.Response(from, term, success, matchIndex, appendIndex);
    } else if (messageType.equals(NEW_ENTRY_REQUEST)) {
        ReplicatedContent content = marshal.unmarshal(channel);
        result = new RaftMessages.NewEntry.Request(from, content);
    } else if (messageType.equals(HEARTBEAT)) {
        long leaderTerm = channel.getLong();
        long commitIndexTerm = channel.getLong();
        long commitIndex = channel.getLong();
        result = new RaftMessages.Heartbeat(from, leaderTerm, commitIndex, commitIndexTerm);
    } else if (messageType.equals(HEARTBEAT_RESPONSE)) {
        result = new RaftMessages.HeartbeatResponse(from);
    } else if (messageType.equals(LOG_COMPACTION_INFO)) {
        long leaderTerm = channel.getLong();
        long prevIndex = channel.getLong();
        result = new RaftMessages.LogCompactionInfo(from, leaderTerm, prevIndex);
    } else {
        throw new IllegalArgumentException("Unknown message type");
    }
    list.add(new RaftMessages.ClusterIdAwareMessage(clusterId, result));
}
Also used : ReadableChannel(org.neo4j.storageengine.api.ReadableChannel) NetworkReadableClosableChannelNetty4(org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) MemberId(org.neo4j.causalclustering.identity.MemberId) ClusterId(org.neo4j.causalclustering.identity.ClusterId) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent)

Aggregations

ClusterId (org.neo4j.causalclustering.identity.ClusterId)15 MemberId (org.neo4j.causalclustering.identity.MemberId)10 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 UUID (java.util.UUID)4 CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)4 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)3 CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)3 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)2 TopologyService (org.neo4j.causalclustering.discovery.TopologyService)2 Member (com.hazelcast.core.Member)1 ClusterIdAwareMessage (org.neo4j.causalclustering.core.consensus.RaftMessages.ClusterIdAwareMessage)1 RaftMessage (org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage)1 ConsensusOutcome (org.neo4j.causalclustering.core.consensus.outcome.ConsensusOutcome)1 ReplicatedContent (org.neo4j.causalclustering.core.replication.ReplicatedContent)1 SimpleFileStorage (org.neo4j.causalclustering.core.state.storage.SimpleFileStorage)1 ClientConnectorAddresses (org.neo4j.causalclustering.discovery.ClientConnectorAddresses)1 NetworkFlushableByteBuf (org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf)1 NetworkReadableClosableChannelNetty4 (org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4)1 AdvertisedSocketAddress (org.neo4j.helpers.AdvertisedSocketAddress)1