Search in sources :

Example 51 with LogContext

use of org.apache.kafka.common.utils.LogContext in project apache-kafka-on-k8s by banzaicloud.

the class SslSelectorTest method testDisconnectWithIntermediateBufferedBytes.

@Test
public void testDisconnectWithIntermediateBufferedBytes() throws Exception {
    int requestSize = 100 * 1024;
    final String node = "0";
    String request = TestUtils.randomString(requestSize);
    this.selector.close();
    this.channelBuilder = new TestSslChannelBuilder(Mode.CLIENT);
    this.channelBuilder.configure(sslClientConfigs);
    this.selector = new Selector(5000, metrics, time, "MetricGroup", channelBuilder, new LogContext());
    connect(node, new InetSocketAddress("localhost", server.port));
    selector.send(createSend(node, request));
    TestUtils.waitForCondition(new TestCondition() {

        @Override
        public boolean conditionMet() {
            try {
                selector.poll(0L);
                return selector.channel(node).hasBytesBuffered();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }, 2000L, "Failed to reach socket state with bytes buffered");
    selector.close(node);
    verifySelectorEmpty();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) LogContext(org.apache.kafka.common.utils.LogContext) TestCondition(org.apache.kafka.test.TestCondition) IOException(java.io.IOException) Test(org.junit.Test)

Example 52 with LogContext

use of org.apache.kafka.common.utils.LogContext in project apache-kafka-on-k8s by banzaicloud.

the class SslTransportLayerTest method testClose.

private void testClose(SecurityProtocol securityProtocol, ChannelBuilder clientChannelBuilder) throws Exception {
    String node = "0";
    server = createEchoServer(securityProtocol);
    clientChannelBuilder.configure(sslClientConfigs);
    this.selector = new Selector(5000, new Metrics(), new MockTime(), "MetricGroup", clientChannelBuilder, new LogContext());
    InetSocketAddress addr = new InetSocketAddress("localhost", server.port());
    selector.connect(node, addr, BUFFER_SIZE, BUFFER_SIZE);
    NetworkTestUtils.waitForChannelReady(selector, node);
    final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    server.outputChannel(Channels.newChannel(bytesOut));
    server.selector().muteAll();
    byte[] message = TestUtils.randomString(100).getBytes();
    int count = 20;
    final int totalSendSize = count * (message.length + 4);
    for (int i = 0; i < count; i++) {
        selector.send(new NetworkSend(node, ByteBuffer.wrap(message)));
        do {
            selector.poll(0L);
        } while (selector.completedSends().isEmpty());
    }
    server.selector().unmuteAll();
    selector.close(node);
    TestUtils.waitForCondition(new TestCondition() {

        @Override
        public boolean conditionMet() {
            return bytesOut.toByteArray().length == totalSendSize;
        }
    }, 5000, "All requests sent were not processed");
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) InetSocketAddress(java.net.InetSocketAddress) LogContext(org.apache.kafka.common.utils.LogContext) TestCondition(org.apache.kafka.test.TestCondition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockTime(org.apache.kafka.common.utils.MockTime)

Example 53 with LogContext

use of org.apache.kafka.common.utils.LogContext in project apache-kafka-on-k8s by banzaicloud.

the class SslTransportLayerTest method testNetworkThreadTimeRecorded.

/**
 * Tests that time spent on the network thread is accumulated on each channel
 */
@Test
public void testNetworkThreadTimeRecorded() throws Exception {
    selector.close();
    this.selector = new Selector(NetworkReceive.UNLIMITED, 5000, new Metrics(), Time.SYSTEM, "MetricGroup", new HashMap<String, String>(), false, true, channelBuilder, MemoryPool.NONE, new LogContext());
    String node = "0";
    server = createEchoServer(SecurityProtocol.SSL);
    InetSocketAddress addr = new InetSocketAddress("localhost", server.port());
    selector.connect(node, addr, BUFFER_SIZE, BUFFER_SIZE);
    String message = TestUtils.randomString(1024 * 1024);
    NetworkTestUtils.waitForChannelReady(selector, node);
    KafkaChannel channel = selector.channel(node);
    assertTrue("SSL handshake time not recorded", channel.getAndResetNetworkThreadTimeNanos() > 0);
    assertEquals("Time not reset", 0, channel.getAndResetNetworkThreadTimeNanos());
    selector.mute(node);
    selector.send(new NetworkSend(node, ByteBuffer.wrap(message.getBytes())));
    while (selector.completedSends().isEmpty()) {
        selector.poll(100L);
    }
    long sendTimeNanos = channel.getAndResetNetworkThreadTimeNanos();
    assertTrue("Send time not recorded: " + sendTimeNanos, sendTimeNanos > 0);
    assertEquals("Time not reset", 0, channel.getAndResetNetworkThreadTimeNanos());
    assertFalse("Unexpected bytes buffered", channel.hasBytesBuffered());
    assertEquals(0, selector.completedReceives().size());
    selector.unmute(node);
    while (selector.completedReceives().isEmpty()) {
        selector.poll(100L);
        assertEquals(0, selector.numStagedReceives(channel));
    }
    long receiveTimeNanos = channel.getAndResetNetworkThreadTimeNanos();
    assertTrue("Receive time not recorded: " + receiveTimeNanos, receiveTimeNanos > 0);
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) LogContext(org.apache.kafka.common.utils.LogContext) Test(org.junit.Test)

Aggregations

LogContext (org.apache.kafka.common.utils.LogContext)53 Metrics (org.apache.kafka.common.metrics.Metrics)28 Test (org.junit.Test)27 Before (org.junit.Before)17 InternalMockProcessorContext (org.apache.kafka.test.InternalMockProcessorContext)14 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)13 NoOpRecordCollector (org.apache.kafka.test.NoOpRecordCollector)10 HashMap (java.util.HashMap)8 MockTime (org.apache.kafka.common.utils.MockTime)8 InetSocketAddress (java.net.InetSocketAddress)7 Properties (java.util.Properties)7 StreamsConfig (org.apache.kafka.streams.StreamsConfig)7 TopicPartition (org.apache.kafka.common.TopicPartition)6 File (java.io.File)5 NetworkClient (org.apache.kafka.clients.NetworkClient)5 InternalStreamsBuilderTest (org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest)5 ServerSocketChannel (java.nio.channels.ServerSocketChannel)4 SocketChannel (java.nio.channels.SocketChannel)4 Metadata (org.apache.kafka.clients.Metadata)4 MockClient (org.apache.kafka.clients.MockClient)4