Search in sources :

Example 1 with TCPHeartbeater

use of org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.TCPHeartbeater in project fluency by komamitsu.

the class MultiSenderTest method testConstructorForTCPSender.

@Test
void testConstructorForTCPSender() throws IOException {
    MultiSender multiSender = null;
    try {
        TCPSender.Config senderConfig0 = new TCPSender.Config();
        senderConfig0.setPort(24225);
        TCPHeartbeater.Config hbConfig0 = new TCPHeartbeater.Config();
        hbConfig0.setPort(24225);
        TCPSender.Config senderConfig1 = new TCPSender.Config();
        senderConfig1.setHost("0.0.0.0");
        senderConfig1.setPort(24226);
        TCPHeartbeater.Config hbConfig1 = new TCPHeartbeater.Config();
        hbConfig1.setHost("0.0.0.0");
        hbConfig1.setPort(24226);
        multiSender = new MultiSender(new MultiSender.Config(), Arrays.asList(new TCPSender(senderConfig0, createFailureDetector(new TCPHeartbeater(hbConfig0))), new TCPSender(senderConfig1, createFailureDetector(new TCPHeartbeater(hbConfig1)))));
        assertThat(multiSender.toString().length(), greaterThan(0));
        assertEquals(2, multiSender.getSenders().size());
        TCPSender tcpSender = (TCPSender) multiSender.getSenders().get(0);
        assertEquals("127.0.0.1", tcpSender.getHost());
        assertEquals(24225, tcpSender.getPort());
        assertEquals("127.0.0.1", tcpSender.getFailureDetector().getHeartbeater().getHost());
        assertEquals(24225, tcpSender.getFailureDetector().getHeartbeater().getPort());
        tcpSender = (TCPSender) multiSender.getSenders().get(1);
        assertEquals("0.0.0.0", tcpSender.getHost());
        assertEquals(24226, tcpSender.getPort());
        assertEquals("0.0.0.0", tcpSender.getFailureDetector().getHeartbeater().getHost());
        assertEquals(24226, tcpSender.getFailureDetector().getHeartbeater().getPort());
    } finally {
        if (multiSender != null) {
            multiSender.close();
        }
    }
}
Also used : TCPHeartbeater(org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.TCPHeartbeater) Test(org.junit.jupiter.api.Test)

Example 2 with TCPHeartbeater

use of org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.TCPHeartbeater in project fluency by komamitsu.

the class FailureDetectorTest method testIsAvailable.

@Test
void testIsAvailable() throws IOException, InterruptedException {
    final ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(null);
    int localPort = serverSocketChannel.socket().getLocalPort();
    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    Runnable serverTask = () -> {
        while (!executorService.isShutdown()) {
            try (SocketChannel accept = serverSocketChannel.accept()) {
                LOG.debug("Accepted: {}", accept);
            } catch (IOException e) {
                LOG.warn("Stab TCP server got an error", e);
            }
        }
        try {
            serverSocketChannel.close();
        } catch (IOException e) {
            LOG.warn("Failed to close serverSocketChannel", e);
        }
    };
    executorService.execute(serverTask);
    TCPHeartbeater.Config heartbeaterConfig = new TCPHeartbeater.Config();
    heartbeaterConfig.setPort(localPort);
    PhiAccrualFailureDetectStrategy.Config failureDetectorConfig = new PhiAccrualFailureDetectStrategy.Config();
    try (FailureDetector failureDetector = new FailureDetector(new PhiAccrualFailureDetectStrategy(failureDetectorConfig), new TCPHeartbeater(heartbeaterConfig))) {
        assertTrue(failureDetector.isAvailable());
        TimeUnit.SECONDS.sleep(4);
        assertTrue(failureDetector.isAvailable());
        executorService.shutdownNow();
        for (int i = 0; i < 20; i++) {
            if (!failureDetector.isAvailable()) {
                break;
            }
            TimeUnit.MILLISECONDS.sleep(500);
        }
        assertFalse(failureDetector.isAvailable());
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) TCPHeartbeater(org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.TCPHeartbeater) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Test(org.junit.jupiter.api.Test)

Example 3 with TCPHeartbeater

use of org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.TCPHeartbeater in project fluency by komamitsu.

the class TCPSenderTest method testSendWithHeartbeart.

@Test
void testSendWithHeartbeart() throws Exception {
    testSendBase(port -> {
        TCPHeartbeater.Config hbConfig = new TCPHeartbeater.Config();
        hbConfig.setPort(port);
        hbConfig.setIntervalMillis(400);
        TCPSender.Config senderConfig = new TCPSender.Config();
        senderConfig.setPort(port);
        return new TCPSender(senderConfig, new FailureDetector(new PhiAccrualFailureDetectStrategy(), new TCPHeartbeater(hbConfig)));
    }, greaterThan(1), greaterThan(1));
}
Also used : PhiAccrualFailureDetectStrategy(org.komamitsu.fluency.fluentd.ingester.sender.failuredetect.PhiAccrualFailureDetectStrategy) TCPHeartbeater(org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.TCPHeartbeater) FailureDetector(org.komamitsu.fluency.fluentd.ingester.sender.failuredetect.FailureDetector) Test(org.junit.jupiter.api.Test)

Example 4 with TCPHeartbeater

use of org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.TCPHeartbeater in project fluency by komamitsu.

the class FluencyBuilderForFluentd method createBaseSender.

private FluentdSender createBaseSender(String host, Integer port, boolean withHeartBeater) {
    if (withHeartBeater && port == null) {
        throw new IllegalArgumentException("`port` should be specified when using heartbeat");
    }
    if (isSslEnabled()) {
        SSLSender.Config senderConfig = new SSLSender.Config();
        FailureDetector failureDetector = null;
        if (host != null) {
            senderConfig.setHost(host);
        }
        if (port != null) {
            senderConfig.setPort(port);
        }
        if (withHeartBeater) {
            SSLHeartbeater.Config hbConfig = new SSLHeartbeater.Config();
            hbConfig.setHost(host);
            hbConfig.setPort(port);
            SSLHeartbeater heartbeater = new SSLHeartbeater(hbConfig);
            failureDetector = new FailureDetector(new PhiAccrualFailureDetectStrategy(), heartbeater);
        }
        if (connectionTimeoutMilli != null) {
            senderConfig.setConnectionTimeoutMilli(connectionTimeoutMilli);
        }
        if (readTimeoutMilli != null) {
            senderConfig.setReadTimeoutMilli(readTimeoutMilli);
        }
        return new SSLSender(senderConfig, failureDetector);
    } else {
        TCPSender.Config senderConfig = new TCPSender.Config();
        FailureDetector failureDetector = null;
        if (host != null) {
            senderConfig.setHost(host);
        }
        if (port != null) {
            senderConfig.setPort(port);
        }
        if (withHeartBeater) {
            TCPHeartbeater.Config hbConfig = new TCPHeartbeater.Config();
            hbConfig.setHost(host);
            hbConfig.setPort(port);
            TCPHeartbeater heartbeater = new TCPHeartbeater(hbConfig);
            failureDetector = new FailureDetector(new PhiAccrualFailureDetectStrategy(), heartbeater);
        }
        if (connectionTimeoutMilli != null) {
            senderConfig.setConnectionTimeoutMilli(connectionTimeoutMilli);
        }
        if (readTimeoutMilli != null) {
            senderConfig.setReadTimeoutMilli(readTimeoutMilli);
        }
        return new TCPSender(senderConfig, failureDetector);
    }
}
Also used : SSLSender(org.komamitsu.fluency.fluentd.ingester.sender.SSLSender) PhiAccrualFailureDetectStrategy(org.komamitsu.fluency.fluentd.ingester.sender.failuredetect.PhiAccrualFailureDetectStrategy) SSLHeartbeater(org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.SSLHeartbeater) FailureDetector(org.komamitsu.fluency.fluentd.ingester.sender.failuredetect.FailureDetector) TCPHeartbeater(org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.TCPHeartbeater) TCPSender(org.komamitsu.fluency.fluentd.ingester.sender.TCPSender)

Aggregations

TCPHeartbeater (org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.TCPHeartbeater)4 Test (org.junit.jupiter.api.Test)3 FailureDetector (org.komamitsu.fluency.fluentd.ingester.sender.failuredetect.FailureDetector)2 PhiAccrualFailureDetectStrategy (org.komamitsu.fluency.fluentd.ingester.sender.failuredetect.PhiAccrualFailureDetectStrategy)2 IOException (java.io.IOException)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 ExecutorService (java.util.concurrent.ExecutorService)1 SSLSender (org.komamitsu.fluency.fluentd.ingester.sender.SSLSender)1 TCPSender (org.komamitsu.fluency.fluentd.ingester.sender.TCPSender)1 SSLHeartbeater (org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.SSLHeartbeater)1