For IntelliJ IDEA,
Android Studio or Eclipse



/** * Handles a heartbeat message. */ private byte[] handleHeartbeat(Endpoint endpoint, byte[] message) { ClusterHeartbeat heartbeat = SERIALIZER.decode(message); failureDetectors.computeIfAbsent(heartbeat.nodeId(), n -> new PhiAccrualFailureDetector()).report(); activateNode(new StatefulNode(heartbeat.nodeId(), heartbeat.nodeType(), endpoint)); return SERIALIZER.encode(nodes.values().stream() .filter(node -> node.type() == Node.Type.CLIENT) .collect(Collectors.toList())); }
@Override public <M> void broadcast(String topic, M message, Function<M, byte[]> encoder) { byte[] payload = SERIALIZER.encode(new InternalMessage(InternalMessage.Type.ALL, encoder.apply(message))); getSubscriberNodes(topic).forEach(nodeId -> { Node node = clusterService.getNode(nodeId); if (node != null && node.getState() == Node.State.ACTIVE) { messagingService.sendAsync(node.endpoint(), topic, payload); } }); }
/** * Handles a bootstrap request. */ private byte[] handleBootstrap(Endpoint endpoint, byte[] payload) { return SERIALIZER.encode(nodes); }
protected <T, U> CompletableFuture<U> sendAndReceive(NodeId nodeId, String type, T request) { Endpoint endpoint = endpoint(nodeId); if (endpoint == null) { return Futures.exceptionalFuture(new ConnectException()); } return messagingService.sendAndReceive(endpoint, type, serializer.encode(request)) .thenApply(serializer::decode); }
protected <T, U> void registerHandler(String type, Function<T, CompletableFuture<U>> handler) { messagingService.registerHandler(type, (e, p) -> { CompletableFuture<byte[]> future = new CompletableFuture<>(); handler.apply(serializer.decode(p)).whenComplete((result, error) -> { if (error == null) { future.complete(serializer.encode(result)); } else { future.completeExceptionally(error); } }); return future; }); }
<T> T copy(T value) { return serializer.decode(serializer.encode(value)); }
/** * Activates the given node. */ private void activateNode(StatefulNode node) { StatefulNode existingNode = nodes.get(node.id()); if (existingNode == null) { node.setState(State.ACTIVE); nodes.put(node.id(), node); post(new ClusterEvent(ClusterEvent.Type.NODE_ADDED, node)); post(new ClusterEvent(ClusterEvent.Type.NODE_ACTIVATED, node)); sendHeartbeat(node.endpoint(), SERIALIZER.encode(new ClusterHeartbeat(localNode.id(), localNode.type()))); } else if (existingNode.getState() == State.INACTIVE) { existingNode.setState(State.ACTIVE); post(new ClusterEvent(ClusterEvent.Type.NODE_ACTIVATED, existingNode)); } }
/** * Stores the current cluster configuration. * * @param configuration The current cluster configuration. */ public synchronized void storeConfiguration(Configuration configuration) { log.trace("Store configuration {}", configuration); byte[] bytes = serializer.encode(configuration); configurationBuffer.position(0) .writeByte(1) .writeInt(bytes.length) .write(bytes); configurationBuffer.flush(); }
@Override public <M, R> CompletableFuture<R> send(String topic, M message, Function<M, byte[]> encoder, Function<byte[], R> decoder) { NodeId nodeId = getNextNodeId(topic); if (nodeId != null) { Node node = clusterService.getNode(nodeId); if (node != null && node.getState() == Node.State.ACTIVE) { byte[] payload = SERIALIZER.encode(new InternalMessage(InternalMessage.Type.DIRECT, encoder.apply(message))); return messagingService.sendAndReceive(node.endpoint(), topic, payload).thenApply(decoder); } } return Futures.exceptionalFuture(new MessagingException.NoRemoteHandler()); }
protected CompletableFuture<Void> sendAsync(NodeId nodeId, String type, Object request) { Endpoint endpoint = endpoint(nodeId); if (endpoint != null) { return messagingService.sendAsync(endpoint(nodeId), type, serializer.encode(request)); } return CompletableFuture.completedFuture(null); }
@Override @SuppressWarnings("unchecked") public <T extends E> Indexed<T> append(T entry) { // Store the entry index. final long index = getNextIndex(); // Serialize the entry. final byte[] bytes = serializer.encode(entry); final int length = bytes.length; // Compute the checksum for the entry. final Checksum crc32 = new CRC32(); crc32.update(bytes, 0, length); final long checksum = crc32.getValue(); // Write the entry length and entry to the segment. buffer.writeInt(length) .writeUnsignedInt(checksum) .write(bytes); // Update the last entry with the correct index/term/length. Indexed<E> indexedEntry = new Indexed<>(index, entry, length); this.lastEntry = indexedEntry; return (Indexed<T>) indexedEntry; }
/** * Sends an anti-entropy advertisement to the given node. */ private void sendAdvertisement(Endpoint endpoint) { clock.increment(); ClusterMetadataAdvertisement advertisement = new ClusterMetadataAdvertisement( Maps.newHashMap(Maps.transformValues(nodes, node -> new NodeDigest(node.timestamp(), node.tombstone())))); messagingService.sendAndReceive(endpoint, ADVERTISEMENT_MESSAGE, SERIALIZER.encode(advertisement)) .whenComplete((response, error) -> { if (error == null) { Set<NodeId> nodes = SERIALIZER.decode(response); for (NodeId nodeId : nodes) { ReplicatedNode node = this.nodes.get(nodeId); if (node != null) { sendUpdate(endpoint, new NodeUpdate(node, clock.increment())); } } } else { log.warn("Anti-entropy advertisement to {} failed!", endpoint); } }); }
@Override public <M> CompletableFuture<Void> unicast(String topic, M message, Function<M, byte[]> encoder) { NodeId nodeId = getNextNodeId(topic); if (nodeId != null) { Node node = clusterService.getNode(nodeId); if (node != null && node.getState() == Node.State.ACTIVE) { byte[] payload = SERIALIZER.encode(new InternalMessage(InternalMessage.Type.DIRECT, encoder.apply(message))); return messagingService.sendAsync(node.endpoint(), topic, payload); } } return CompletableFuture.completedFuture(null); }
/** * Sends the given update to the given node. */ private void sendUpdate(Endpoint endpoint, NodeUpdate update) { messagingService.sendAsync(endpoint, UPDATE_MESSAGE, SERIALIZER.encode(update)); }
byte[] encode(Object value) { return serializer.encode(value); }