use of org.neo4j.causalclustering.core.replication.ReplicatedContent 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));
}
use of org.neo4j.causalclustering.core.replication.ReplicatedContent in project neo4j by neo4j.
the class CoreReplicatedContentMarshal method unmarshal0.
@Override
public ReplicatedContent unmarshal0(ReadableChannel channel) throws IOException, EndOfStreamException {
byte type = channel.get();
final ReplicatedContent content;
switch(type) {
case TX_CONTENT_TYPE:
content = ReplicatedTransactionSerializer.unmarshal(channel);
break;
case RAFT_MEMBER_SET_TYPE:
content = MemberIdSetSerializer.unmarshal(channel);
break;
case ID_RANGE_REQUEST_TYPE:
content = ReplicatedIdAllocationRequestSerializer.unmarshal(channel);
break;
case TOKEN_REQUEST_TYPE:
content = ReplicatedTokenRequestSerializer.unmarshal(channel);
break;
case NEW_LEADER_BARRIER_TYPE:
content = new NewLeaderBarrier();
break;
case LOCK_TOKEN_REQUEST:
content = ReplicatedLockTokenSerializer.unmarshal(channel);
break;
case DISTRIBUTED_OPERATION:
content = DistributedOperation.deserialize(channel);
break;
default:
throw new IllegalArgumentException(String.format("Unknown content type 0x%x", type));
}
return content;
}
use of org.neo4j.causalclustering.core.replication.ReplicatedContent in project neo4j by neo4j.
the class ReplayRaftLog method main.
public static void main(String[] args) throws IOException {
Args arg = Args.parse(args);
String from = arg.get("from");
System.out.println("From is " + from);
String to = arg.get("to");
System.out.println("to is " + to);
File logDirectory = new File(from);
System.out.println("logDirectory = " + logDirectory);
Config config = Config.embeddedDefaults(stringMap());
try (DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction()) {
LogProvider logProvider = getInstance();
CoreLogPruningStrategy pruningStrategy = new CoreLogPruningStrategyFactory(config.get(raft_log_pruning_strategy), logProvider).newInstance();
SegmentedRaftLog log = new SegmentedRaftLog(fileSystem, logDirectory, config.get(raft_log_rotation_size), new CoreReplicatedContentMarshal(), logProvider, config.get(raft_log_reader_pool_size), Clocks.systemClock(), new OnDemandJobScheduler(), pruningStrategy);
// Not really, but we need to have a way to pass in the commit index
long totalCommittedEntries = log.appendIndex();
for (int i = 0; i <= totalCommittedEntries; i++) {
ReplicatedContent content = readLogEntry(log, i).content();
if (content instanceof ReplicatedTransaction) {
ReplicatedTransaction tx = (ReplicatedTransaction) content;
ReplicatedTransactionFactory.extractTransactionRepresentation(tx, new byte[0]).accept(element -> {
System.out.println(element);
return false;
});
}
}
}
}
use of org.neo4j.causalclustering.core.replication.ReplicatedContent in project neo4j by neo4j.
the class CommandApplicationProcessTest method shouldFallbackToLogCursorOnCacheMiss.
@Test
public void shouldFallbackToLogCursorOnCacheMiss() throws Throwable {
// if the cache does not contain all things to be applied, make sure we fall back to the log
// should only happen in recovery, otherwise this is probably a bug.
applicationProcess.start();
//given cache with missing entry
ReplicatedContent operation0 = operation(nullTx);
ReplicatedContent operation1 = operation(nullTx);
ReplicatedContent operation2 = operation(nullTx);
inFlightMap.put(0L, new RaftLogEntry(0, operation0));
inFlightMap.put(2L, new RaftLogEntry(2, operation2));
raftLog.append(new RaftLogEntry(0, operation0));
raftLog.append(new RaftLogEntry(1, operation1));
raftLog.append(new RaftLogEntry(2, operation2));
//when
applicationProcess.notifyCommitted(2);
applier.sync(false);
//then the cache stops being used after it finds 1 is missing
verify(inFlightMap, times(1)).get(0L);
verify(inFlightMap, times(1)).get(1L);
verify(inFlightMap, times(0)).get(2L);
// we only look up 1 so let's remove that from the cache
verify(inFlightMap, times(1)).remove(0L);
verify(commandDispatcher, times(1)).dispatch(eq(nullTx), eq(0L), anyCallback());
verify(commandDispatcher, times(1)).dispatch(eq(nullTx), eq(1L), anyCallback());
verify(commandDispatcher, times(1)).dispatch(eq(nullTx), eq(2L), anyCallback());
verify(raftLog, times(1)).getEntryCursor(1);
}
Aggregations