Search in sources :

Example 11 with Message

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());
}
Also used : Message(io.scalecube.transport.Message) ByteBuf(io.netty.buffer.ByteBuf) Member(io.scalecube.cluster.Member) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 12 with Message

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);
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ScheduledFuture(java.util.concurrent.ScheduledFuture) MembershipProtocol(io.scalecube.cluster.membership.MembershipProtocol) LoggerFactory(org.slf4j.LoggerFactory) MemberStatus(io.scalecube.cluster.membership.MemberStatus) Observable(rx.Observable) ArrayList(java.util.ArrayList) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Schedulers(rx.schedulers.Schedulers) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Transport(io.scalecube.transport.Transport) Message(io.scalecube.transport.Message) Subscribers(rx.observers.Subscribers) Logger(org.slf4j.Logger) Subscriber(rx.Subscriber) Scheduler(rx.Scheduler) Subject(rx.subjects.Subject) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Member(io.scalecube.cluster.Member) MembershipEvent(io.scalecube.cluster.membership.MembershipEvent) PublishSubject(rx.subjects.PublishSubject) Collections(java.util.Collections) Message(io.scalecube.transport.Message) Member(io.scalecube.cluster.Member)

Example 13 with Message

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);
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ScheduledFuture(java.util.concurrent.ScheduledFuture) MembershipProtocol(io.scalecube.cluster.membership.MembershipProtocol) LoggerFactory(org.slf4j.LoggerFactory) MemberStatus(io.scalecube.cluster.membership.MemberStatus) Observable(rx.Observable) ArrayList(java.util.ArrayList) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Schedulers(rx.schedulers.Schedulers) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Transport(io.scalecube.transport.Transport) Message(io.scalecube.transport.Message) Subscribers(rx.observers.Subscribers) Logger(org.slf4j.Logger) Subscriber(rx.Subscriber) Scheduler(rx.Scheduler) Subject(rx.subjects.Subject) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Member(io.scalecube.cluster.Member) MembershipEvent(io.scalecube.cluster.membership.MembershipEvent) PublishSubject(rx.subjects.PublishSubject) Collections(java.util.Collections) Message(io.scalecube.transport.Message) Member(io.scalecube.cluster.Member)

Example 14 with Message

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);
}
Also used : Message(io.scalecube.transport.Message) Member(io.scalecube.cluster.Member)

Example 15 with Message

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);
}
Also used : Message(io.scalecube.transport.Message) Member(io.scalecube.cluster.Member)

Aggregations

Message (io.scalecube.transport.Message)51 BaseTest (io.scalecube.testlib.BaseTest)31 Test (org.junit.Test)31 CountDownLatch (java.util.concurrent.CountDownLatch)28 Observable (rx.Observable)10 Member (io.scalecube.cluster.Member)8 Microservices (io.scalecube.services.Microservices)6 TimeUnit (java.util.concurrent.TimeUnit)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 ServiceCall (io.scalecube.services.ServiceCall)5 Collections (java.util.Collections)5 Executors (java.util.concurrent.Executors)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Schedulers (rx.schedulers.Schedulers)5 List (java.util.List)4 Subscription (rx.Subscription)4 PublishSubject (rx.subjects.PublishSubject)4 Subject (rx.subjects.Subject)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3