use of io.scalecube.transport.Message in project scalecube by scalecube.
the class GossipRequestTest method testSerializationAndDeserialization.
@Test
public void testSerializationAndDeserialization() throws Exception {
Member from = new Member("0", Address.from("localhost:1234"));
List<Gossip> gossips = getGossips();
Message message = Message.withData(new GossipRequest(gossips, from.id())).correlationId("CORR_ID").build();
ByteBuf bb = buffer();
MessageCodec.serialize(message, bb);
assertTrue(bb.readableBytes() > 0);
ByteBuf input = copiedBuffer(bb);
Message deserializedMessage = MessageCodec.deserialize(input);
assertNotNull(deserializedMessage);
Assert.assertEquals(deserializedMessage.data().getClass(), GossipRequest.class);
Assert.assertEquals("CORR_ID", deserializedMessage.correlationId());
GossipRequest gossipRequest = deserializedMessage.data();
assertNotNull(gossipRequest);
assertNotNull(gossipRequest.gossips());
assertNotNull(gossipRequest.gossips().get(0));
Object msgData = gossipRequest.gossips().get(0).message().data();
assertNotNull(msgData);
assertTrue(msgData.toString(), msgData instanceof TestData);
assertEquals(testData.getProperties(), ((TestData) msgData).getProperties());
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class FailureDetectorImpl method doPing.
// ================================================
// ============== Action Methods ==================
// ================================================
private void doPing() {
// Increment period counter
period++;
// Select ping member
Member pingMember = selectPingMember();
if (pingMember == null) {
return;
}
// Send ping
Member localMember = membership.member();
String cid = localMember.id() + "-" + Long.toString(period);
PingData pingData = new PingData(localMember, pingMember);
Message pingMsg = Message.withData(pingData).qualifier(PING).correlationId(cid).build();
try {
LOGGER.trace("Send Ping[{}] to {}", period, pingMember);
transport.listen().observeOn(scheduler).filter(this::isPingAck).filter(message -> cid.equals(message.correlationId())).take(1).timeout(config.getPingTimeout(), TimeUnit.MILLISECONDS, scheduler).subscribe(message -> {
LOGGER.trace("Received PingAck[{}] from {}", period, pingMember);
publishPingResult(pingMember, MemberStatus.ALIVE);
}, throwable -> {
LOGGER.trace("Timeout getting PingAck[{}] from {} within {} ms", period, pingMember, config.getPingTimeout());
doPingReq(pingMember, cid);
});
transport.send(pingMember.address(), pingMsg);
} catch (Exception cause) {
LOGGER.error("Exception on sending Ping[{}] to {}: {}", period, pingMember, cause.getMessage(), cause);
}
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class FailureDetectorImpl method doPingReq.
private void doPingReq(final Member pingMember, String cid) {
final int timeout = config.getPingInterval() - config.getPingTimeout();
if (timeout <= 0) {
LOGGER.trace("No PingReq[{}] occurred, because no time left (pingInterval={}, pingTimeout={})", period, config.getPingInterval(), config.getPingTimeout());
publishPingResult(pingMember, MemberStatus.SUSPECT);
return;
}
final List<Member> pingReqMembers = selectPingReqMembers(pingMember);
if (pingReqMembers.isEmpty()) {
LOGGER.trace("No PingReq[{}] occurred, because member selection is empty", period);
publishPingResult(pingMember, MemberStatus.SUSPECT);
return;
}
Member localMember = membership.member();
transport.listen().observeOn(scheduler).filter(this::isPingAck).filter(message -> cid.equals(message.correlationId())).take(1).timeout(timeout, TimeUnit.MILLISECONDS, scheduler).subscribe(message -> {
LOGGER.trace("Received transit PingAck[{}] from {} to {}", period, message.sender(), pingMember);
publishPingResult(pingMember, MemberStatus.ALIVE);
}, throwable -> {
LOGGER.trace("Timeout getting transit PingAck[{}] from {} to {} within {} ms", period, pingReqMembers, pingMember, timeout);
publishPingResult(pingMember, MemberStatus.SUSPECT);
});
PingData pingReqData = new PingData(localMember, pingMember);
Message pingReqMsg = Message.withData(pingReqData).qualifier(PING_REQ).correlationId(cid).build();
LOGGER.trace("Send PingReq[{}] to {} for {}", period, pingReqMembers, pingMember);
for (Member pingReqMember : pingReqMembers) {
transport.send(pingReqMember.address(), pingReqMsg);
}
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class FailureDetectorImpl method onPingReq.
/**
* Listens to PING_REQ message and sends PING to requested cluster member.
*/
private void onPingReq(Message message) {
LOGGER.trace("Received PingReq: {}", message);
PingData data = message.data();
Member target = data.getTo();
Member originalIssuer = data.getFrom();
String correlationId = message.correlationId();
PingData pingReqData = new PingData(membership.member(), target, originalIssuer);
Message pingMessage = Message.withData(pingReqData).qualifier(PING).correlationId(correlationId).build();
LOGGER.trace("Send transit Ping to {}", target.address());
transport.send(target.address(), pingMessage);
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class FailureDetectorImpl method onTransitPingAck.
/**
* Listens to ACK with message containing ORIGINAL_ISSUER then converts message to plain ACK and sends it to
* ORIGINAL_ISSUER.
*/
private void onTransitPingAck(Message message) {
LOGGER.trace("Received transit PingAck: {}", message);
PingData data = message.data();
Member target = data.getOriginalIssuer();
String correlationId = message.correlationId();
PingData originalAckData = new PingData(target, data.getTo());
Message originalAckMessage = Message.withData(originalAckData).qualifier(PING_ACK).correlationId(correlationId).build();
LOGGER.trace("Resend transit PingAck to {}", target.address());
transport.send(target.address(), originalAckMessage);
}
Aggregations