/** * This contructor is called in response to an incoming request from a slave. */ public Peer(Gondola gondola, CoreMember cmember, Channel slaveChannel) { this(gondola, cmember, slaveChannel.getRemoteMemberId()); this.channel = slaveChannel; this.slaveMode = true; }
/** * Returns the channel's operational state. */ public boolean isOperational() { return channel.isOperational(); }
public void stop() { generation++; channel.stop(); threads.forEach(t -> t.interrupt()); for (Thread t : threads) { try { t.join(); } catch (InterruptedException e) { logger.error(e.getMessage(), e); } } threads.clear(); }
out = channel.getOutputStream(out, true); lastReceivedTs = now; out = channel.getOutputStream(out, errorOccurred); errorOccurred = false; Message message = outQueue.take();
try { InputStream oldIn = in; in = channel.getInputStream(in, errorOccurred); errorOccurred = false; if (oldIn != in) { logger.warn("[{}-{}] recv({}): end-of-file", gondola.getHostId(), cmember.memberId, peerId); in = channel.getInputStream(in, true); excess = 0; continue;
public void start() throws GondolaException { if (threads.size() > 0) { throw new IllegalStateException("start() can only be called once"); } if (channel == null) { // Peer is not a slave so create a channel to remote member channel = gondola.getNetwork().createChannel(cmember.memberId, peerId); } channel.start(); reset(); // Start local threads threads.add(new Receiver()); threads.add(new Backfiller()); threads.add(new Sender()); threads.forEach(t -> t.start()); }
public boolean stop() { generation++; boolean status = channel.stop(); return Utils.stopThreads(threads) && status; }
out = channel.getOutputStream(out, true); lastReceivedTs = now; out = channel.getOutputStream(out, errorOccurred); errorOccurred = false; Message message = outQueue.take();
try { InputStream oldIn = in; in = channel.getInputStream(in, errorOccurred); errorOccurred = false; if (oldIn != in) { logger.warn("[{}-{}] recv({}): end-of-file", gondola.getHostId(), cmember.memberId, peerId); in = channel.getInputStream(in, true); excess = 0; continue;
channel.awaitOperational();
/** * Returns the channel's operational state. */ public boolean isOperational() { return channel.isOperational(); }
if (m.getMemberId() == channel.getRemoteMemberId()) { logger.info("[{}-{}] Slave request from {} rejected: in the same shard", gondola.getHostId(), memberId, channel.getRemoteMemberId()); return false; if (slave.peerId == channel.getRemoteMemberId()) { logger.info("[{}-{}] Slave request from {} accepted", gondola.getHostId(), memberId, channel.getRemoteMemberId()); try { slave.setChannel(channel); gondola.getHostId(), memberId, channel.getRemoteMemberId()); return false; gondola.getHostId(), memberId, channel.getRemoteMemberId());
/** * Used to send messages that don't append to the log, such as heartbeats and request votes. */ public void send(Message message) { if (channel.isOperational()) { addOutQueue(message); } }
/** * Used to send messages that don't append to the log, such as heartbeats and request votes. */ public void send(Message message) { if (channel.isOperational()) { addOutQueue(message); } }
/** * Sends the message to the remote member. This version of send * increases nextIndex and backfillToIndex. Does not send if the * connection to remote member is not operational or being * backfilled. */ public void send(Message message, int prevLogIndex) { assert message.getType() == Message.TYPE_APPEND_ENTRY_REQ; lock.lock(); try { if (backfilling) { // Increase the backfill index to the new value backfillToIndex = prevLogIndex + 2; } else if (channel.isOperational() && (message.isHeartbeat() || nextIndex == prevLogIndex + 1)) { latency.head(prevLogIndex + 1); addOutQueue(message); // Increment nextIndex for each command nextIndex += message.numCommands(); } } finally { lock.unlock(); } }
/** * Sends the message to the remote member. * This version of send increases nextIndex and backfillToIndex. * Does not send if the connection to remote member is not operational or being backfilled. */ public void send(Message message, int prevLogIndex) { assert message.getType() == Message.TYPE_APPEND_ENTRY_REQ; lock.lock(); try { if (backfilling && backfillAhead < BACKFILL_AHEAD_LIMIT) { // Increase the backfill index to the new value backfillToIndex = prevLogIndex + 2; } else if (channel.isOperational() && (message.isHeartbeat() || nextIndex == prevLogIndex + 1)) { latency.head(prevLogIndex + 1); addOutQueue(message); // Increment nextIndex for each command nextIndex += message.numCommands(); } } finally { lock.unlock(); } }