use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.
the class RemoteStoreTest method shouldCopyStoreFilesAndPullTransactions.
@Test
public void shouldCopyStoreFilesAndPullTransactions() throws Exception {
// given
StoreId storeId = new StoreId(1, 2, 3, 4);
StoreCopyClient storeCopyClient = mock(StoreCopyClient.class);
TxPullClient txPullClient = mock(TxPullClient.class);
when(txPullClient.pullTransactions(any(), any(), anyLong(), any())).thenReturn(new TxPullRequestResult(SUCCESS_END_OF_STREAM, 13));
TransactionLogCatchUpWriter writer = mock(TransactionLogCatchUpWriter.class);
RemoteStore remoteStore = new RemoteStore(NullLogProvider.getInstance(), mock(FileSystemAbstraction.class), null, storeCopyClient, txPullClient, factory(writer), new Monitors());
// when
MemberId localhost = new MemberId(UUID.randomUUID());
remoteStore.copy(localhost, storeId, new File("destination"));
// then
verify(storeCopyClient).copyStoreFiles(eq(localhost), eq(storeId), any(StoreFileStreams.class));
verify(txPullClient).pullTransactions(eq(localhost), eq(storeId), anyLong(), any(TxPullResponseListener.class));
}
use of org.neo4j.causalclustering.identity.MemberId 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.identity.MemberId in project neo4j by neo4j.
the class ConnectToRandomCoreServerStrategy method upstreamDatabase.
@Override
public Optional<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException {
final CoreTopology coreTopology = topologyService.coreServers();
if (coreTopology.members().size() == 0) {
throw new UpstreamDatabaseSelectionException("No core servers available");
}
int skippedServers = random.nextInt(coreTopology.members().size());
final Iterator<MemberId> iterator = coreTopology.members().keySet().iterator();
MemberId member;
do {
member = iterator.next();
} while (skippedServers-- > 0);
return Optional.ofNullable(member);
}
use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.
the class ReadReplicaStartupProcess method start.
@Override
public void start() throws IOException {
boolean syncedWithUpstream = false;
RetryStrategy.Timeout timeout = retryStrategy.newTimeout();
int attempt = 0;
while (!syncedWithUpstream) {
attempt++;
MemberId source = null;
try {
source = selectionStrategyPipeline.bestUpstreamDatabase();
syncStoreWithUpstream(source);
syncedWithUpstream = true;
} catch (UpstreamDatabaseSelectionException e) {
lastIssue = issueOf("finding upstream member", attempt);
debugLog.warn(lastIssue);
} catch (StoreCopyFailedException e) {
lastIssue = issueOf(format("copying store files from %s", source), attempt);
debugLog.warn(lastIssue);
} catch (StreamingTransactionsFailedException e) {
lastIssue = issueOf(format("streaming transactions from %s", source), attempt);
debugLog.warn(lastIssue);
} catch (StoreIdDownloadFailedException e) {
lastIssue = issueOf(format("getting store id from %s", source), attempt);
debugLog.warn(lastIssue);
}
try {
Thread.sleep(timeout.getMillis());
timeout.increment();
} catch (InterruptedException e) {
Thread.interrupted();
lastIssue = "Interrupted while trying to start read replica";
debugLog.warn(lastIssue);
break;
}
}
if (!syncedWithUpstream) {
userLog.error(lastIssue);
throw new RuntimeException(lastIssue);
}
try {
localDatabase.start();
txPulling.start();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.
the class UpstreamDatabaseStrategySelector method bestUpstreamDatabase.
public MemberId bestUpstreamDatabase() throws UpstreamDatabaseSelectionException {
MemberId result = null;
for (UpstreamDatabaseSelectionStrategy strategy : strategies) {
log.debug("Trying selection strategy [%s]", strategy.toString());
try {
if (strategy.upstreamDatabase().isPresent()) {
result = strategy.upstreamDatabase().get();
break;
}
} catch (NoSuchElementException ex) {
// Do nothing, this strategy failed
}
}
if (result == null) {
throw new UpstreamDatabaseSelectionException("Could not find an upstream database with which to connect.");
}
log.debug("Selected upstream database [%s]", result);
return result;
}
Aggregations