Search in sources :

Example 21 with Address

use of io.scalecube.transport.Address in project scalecube by scalecube.

the class FailureDetectorTest method assertStatus.

/**
 * @param address target member to expect on
 * @param status expected listen status
 * @param events events collection of failure detector events
 * @param expected expected members of the given listenStatus
 */
private void assertStatus(Address address, MemberStatus status, Collection<FailureDetectorEvent> events, Address... expected) {
    List<Address> actual = events.stream().filter(event -> event.status() == status).map(FailureDetectorEvent::member).map(Member::address).collect(Collectors.toList());
    String msg1 = String.format("Node %s expected %s %s members %s, but was: %s", address, expected.length, status, Arrays.toString(expected), events);
    assertEquals(msg1, expected.length, actual.size());
    for (Address member : expected) {
        String msg2 = String.format("Node %s expected as %s %s, but was: %s", address, status, member, events);
        assertTrue(msg2, actual.contains(member));
    }
}
Also used : Arrays(java.util.Arrays) Address(io.scalecube.transport.Address) MembershipProtocol(io.scalecube.cluster.membership.MembershipProtocol) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) MemberStatus(io.scalecube.cluster.membership.MemberStatus) SUSPECT(io.scalecube.cluster.membership.MemberStatus.SUSPECT) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ClusterConfig(io.scalecube.cluster.ClusterConfig) List(java.util.List) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) ALIVE(io.scalecube.cluster.membership.MemberStatus.ALIVE) Member(io.scalecube.cluster.Member) Transport(io.scalecube.transport.Transport) TransportConfig(io.scalecube.transport.TransportConfig) DummyMembershipProtocol(io.scalecube.cluster.membership.DummyMembershipProtocol) BaseTest(io.scalecube.testlib.BaseTest) Assert.assertEquals(org.junit.Assert.assertEquals) Address(io.scalecube.transport.Address)

Example 22 with Address

use of io.scalecube.transport.Address in project scalecube by scalecube.

the class MembershipProtocolImpl method memberAddress.

/**
 * Returns the accessible member address, either from the transport or the overridden variables.
 * @param transport transport
 * @param config membership config parameters
 * @return Accessible member address
 */
protected static Address memberAddress(Transport transport, MembershipConfig config) {
    Address memberAddress = transport.address();
    if (config.getMemberHost() != null) {
        int memberPort = config.getMemberPort() != null ? config.getMemberPort() : memberAddress.port();
        memberAddress = Address.create(config.getMemberHost(), memberPort);
    }
    return memberAddress;
}
Also used : Address(io.scalecube.transport.Address)

Example 23 with Address

use of io.scalecube.transport.Address in project scalecube by scalecube.

the class MembershipProtocolImpl method doInitialSync.

// ================================================
// ============== Action Methods ==================
// ================================================
private CompletableFuture<Void> doInitialSync() {
    LOGGER.debug("Making initial Sync to all seed members: {}", seedMembers);
    if (seedMembers.isEmpty()) {
        schedulePeriodicSync();
        return CompletableFuture.completedFuture(null);
    }
    CompletableFuture<Void> syncResponseFuture = new CompletableFuture<>();
    // Listen initial Sync Ack
    String cid = memberRef.get().id();
    transport.listen().observeOn(scheduler).filter(msg -> SYNC_ACK.equals(msg.qualifier())).filter(msg -> cid.equals(msg.correlationId())).filter(this::checkSyncGroup).take(1).timeout(config.getSyncTimeout(), TimeUnit.MILLISECONDS, scheduler).subscribe(message -> {
        SyncData syncData = message.data();
        LOGGER.info("Joined cluster '{}': {}", syncData.getSyncGroup(), syncData.getMembership());
        onSyncAck(message, true);
        schedulePeriodicSync();
        syncResponseFuture.complete(null);
    }, throwable -> {
        LOGGER.info("Timeout getting initial SyncAck from seed members: {}", seedMembers);
        schedulePeriodicSync();
        syncResponseFuture.complete(null);
    });
    Message syncMsg = prepareSyncDataMsg(SYNC, cid);
    seedMembers.forEach(address -> transport.send(address, syncMsg));
    return syncResponseFuture;
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) FailureDetector(io.scalecube.cluster.fdetector.FailureDetector) ScheduledFuture(java.util.concurrent.ScheduledFuture) Address(io.scalecube.transport.Address) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Observable(rx.Observable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) 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) ClusterMath(io.scalecube.cluster.ClusterMath) GossipProtocol(io.scalecube.cluster.gossip.GossipProtocol) Logger(org.slf4j.Logger) Subscriber(rx.Subscriber) Collection(java.util.Collection) Set(java.util.Set) FailureDetectorEvent(io.scalecube.cluster.fdetector.FailureDetectorEvent) Scheduler(rx.Scheduler) DEAD(io.scalecube.cluster.membership.MemberStatus.DEAD) Subject(rx.subjects.Subject) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ALIVE(io.scalecube.cluster.membership.MemberStatus.ALIVE) Member(io.scalecube.cluster.Member) Preconditions(com.google.common.base.Preconditions) PublishSubject(rx.subjects.PublishSubject) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) Message(io.scalecube.transport.Message)

Example 24 with Address

use of io.scalecube.transport.Address in project scalecube by scalecube.

the class ServiceReferenceTest method test_ServiceReference.

@Test
public void test_ServiceReference() {
    Address addr = Address.create("localhost", 4000);
    ServiceReference reference = new ServiceReference("a", "b", null, addr);
    assertTrue(reference.address().equals(addr));
    assertTrue(reference.memberId().equals("a"));
    assertTrue(reference.serviceName().equals("b"));
    ServiceReference aref = new ServiceReference("a", "b", null, addr);
    ServiceReference bref = new ServiceReference("a", "b", null, addr);
    assertEquals(aref, aref);
    assertEquals(aref, bref);
    assertNotEquals(aref, null);
    assertEquals(reference.toString(), "ServiceReference [memberId=a, qualifier=b, address=localhost:4000]");
}
Also used : Address(io.scalecube.transport.Address) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 25 with Address

use of io.scalecube.transport.Address in project scalecube by scalecube.

the class ClientStreamProcessorTest method setUp.

@Before
public void setUp() {
    bootstrap = new Bootstrap().group(new NioEventLoopGroup(0)).channel(NioSocketChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT_MILLIS).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_REUSEADDR, true);
    clientStream = ClientStream.newClientStream(bootstrap);
    clientStreamProcessorFactory = new ClientStreamProcessorFactory(clientStream);
    listeningServerStream = ListeningServerStream.newListeningServerStream().withListenAddress("localhost");
    address = listeningServerStream.bindAwait();
    // setup echo service
    listeningServerStream.listenReadSuccess().map(Event::getMessageOrThrow).filter(message -> "q/echo".equalsIgnoreCase(message.qualifier())).subscribe(message -> {
        // send original message back to client then send onCompleted
        listeningServerStream.send(from(message).build());
        listeningServerStream.send(from(message).qualifier(Qualifier.Q_ON_COMPLETED).build());
    });
    // setup echo service replying with void
    listeningServerStream.listenReadSuccess().map(Event::getMessageOrThrow).filter(message -> "q/echoVoid".equalsIgnoreCase(message.qualifier())).subscribe(message -> {
        // just send onCompleted
        listeningServerStream.send(from(message).qualifier(Qualifier.Q_ON_COMPLETED).build());
    });
    // setup error service
    listeningServerStream.listenReadSuccess().map(Event::getMessageOrThrow).filter(message -> "q/echoError".equalsIgnoreCase(message.qualifier())).subscribe(message -> {
        // respond with error
        listeningServerStream.send(from(message).qualifier(Qualifier.Q_GENERAL_FAILURE).build());
    });
    // setup service with several responses with onCompleted message following everyting sent
    listeningServerStream.listenReadSuccess().map(Event::getMessageOrThrow).filter(message -> "q/echoStream".equalsIgnoreCase(message.qualifier())).subscribe(message -> {
        // respond with several response messages then send onCompleted
        IntStream.rangeClosed(1, 42).forEach(i -> listeningServerStream.send(from(message).qualifier("q/" + i).build()));
        listeningServerStream.send(from(message).qualifier(Qualifier.Q_ON_COMPLETED).build());
    });
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) IntStream(java.util.stream.IntStream) StreamMessage.from(io.scalecube.streams.StreamMessage.from) ChannelOption(io.netty.channel.ChannelOption) Address(io.scalecube.transport.Address) Test(org.junit.Test) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) AssertableSubscriber(rx.observers.AssertableSubscriber) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) After(org.junit.After) Duration(java.time.Duration) ConnectException(java.net.ConnectException) Before(org.junit.Before) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Before(org.junit.Before)

Aggregations

Address (io.scalecube.transport.Address)43 Test (org.junit.Test)33 BaseTest (io.scalecube.testlib.BaseTest)22 Transport (io.scalecube.transport.Transport)20 ArrayList (java.util.ArrayList)13 List (java.util.List)12 InetAddress (java.net.InetAddress)10 CompletableFuture (java.util.concurrent.CompletableFuture)5 TimeUnit (java.util.concurrent.TimeUnit)5 Member (io.scalecube.cluster.Member)4 Collection (java.util.Collection)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 ALIVE (io.scalecube.cluster.membership.MemberStatus.ALIVE)3 Collectors (java.util.stream.Collectors)3 Lists (com.google.common.collect.Lists)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ClusterConfig (io.scalecube.cluster.ClusterConfig)2 DummyMembershipProtocol (io.scalecube.cluster.membership.DummyMembershipProtocol)2 MemberStatus (io.scalecube.cluster.membership.MemberStatus)2 SUSPECT (io.scalecube.cluster.membership.MemberStatus.SUSPECT)2