Search in sources :

Example 21 with KafkaException

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

the class WorkerGroupMember method stop.

private void stop(boolean swallowException) {
    log.trace("Stopping the Connect group member.");
    AtomicReference<Throwable> firstException = new AtomicReference<>();
    this.stopped = true;
    ClientUtils.closeQuietly(coordinator, "coordinator", firstException);
    ClientUtils.closeQuietly(metrics, "consumer metrics", firstException);
    ClientUtils.closeQuietly(client, "consumer network client", firstException);
    AppInfoParser.unregisterAppInfo(JMX_PREFIX, clientId, metrics);
    if (firstException.get() != null && !swallowException)
        throw new KafkaException("Failed to stop the Connect group member", firstException.get());
    else
        log.debug("The Connect group member has stopped.");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) KafkaException(org.apache.kafka.common.KafkaException)

Example 22 with KafkaException

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

the class SaslAuthenticatorTest method testInvalidLoginModule.

/**
 * Tests that connections cannot be created if the login module class is unavailable.
 */
@Test
public void testInvalidLoginModule() throws Exception {
    TestJaasConfig jaasConfig = configureMechanisms("PLAIN", Arrays.asList("PLAIN"));
    jaasConfig.createOrUpdateEntry(TestJaasConfig.LOGIN_CONTEXT_CLIENT, "InvalidLoginModule", TestJaasConfig.defaultClientOptions());
    SecurityProtocol securityProtocol = SecurityProtocol.SASL_SSL;
    server = createEchoServer(securityProtocol);
    try {
        createSelector(securityProtocol, saslClientConfigs);
        fail("SASL/PLAIN channel created without valid login module");
    } catch (KafkaException e) {
    // Expected exception
    }
}
Also used : SecurityProtocol(org.apache.kafka.common.security.auth.SecurityProtocol) KafkaException(org.apache.kafka.common.KafkaException) Test(org.junit.Test)

Example 23 with KafkaException

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

the class ClientCompatibilityTest method testAdminClient.

void testAdminClient() throws Throwable {
    Properties adminProps = new Properties();
    adminProps.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, testConfig.bootstrapServer);
    try (final AdminClient client = AdminClient.create(adminProps)) {
        while (true) {
            Collection<Node> nodes = client.describeCluster().nodes().get();
            if (nodes.size() == testConfig.numClusterNodes) {
                break;
            } else if (nodes.size() > testConfig.numClusterNodes) {
                throw new KafkaException("Expected to see " + testConfig.numClusterNodes + " nodes, but saw " + nodes.size());
            }
            Thread.sleep(1);
            log.info("Saw only {} cluster nodes.  Waiting to see {}.", nodes.size(), testConfig.numClusterNodes);
        }
        tryFeature("createTopics", testConfig.createTopicsSupported, new Invoker() {

            @Override
            public void invoke() throws Throwable {
                try {
                    client.createTopics(Collections.singleton(new NewTopic("newtopic", 1, (short) 1))).all().get();
                } catch (ExecutionException e) {
                    throw e.getCause();
                }
            }
        }, new ResultTester() {

            @Override
            public void test() throws Throwable {
                while (true) {
                    try {
                        client.describeTopics(Collections.singleton("newtopic")).all().get();
                        break;
                    } catch (ExecutionException e) {
                        if (e.getCause() instanceof UnknownTopicOrPartitionException)
                            continue;
                        throw e;
                    }
                }
            }
        });
        while (true) {
            Collection<TopicListing> listings = client.listTopics().listings().get();
            if (!testConfig.createTopicsSupported)
                break;
            boolean foundNewTopic = false;
            for (TopicListing listing : listings) {
                if (listing.name().equals("newtopic")) {
                    if (listing.isInternal())
                        throw new KafkaException("Did not expect newtopic to be an internal topic.");
                    foundNewTopic = true;
                }
            }
            if (foundNewTopic)
                break;
            Thread.sleep(1);
            log.info("Did not see newtopic.  Retrying listTopics...");
        }
        tryFeature("describeAclsSupported", testConfig.describeAclsSupported, new Invoker() {

            @Override
            public void invoke() throws Throwable {
                try {
                    client.describeAcls(AclBindingFilter.ANY).values().get();
                } catch (ExecutionException e) {
                    if (e.getCause() instanceof SecurityDisabledException)
                        return;
                    throw e.getCause();
                }
            }
        });
    }
}
Also used : Node(org.apache.kafka.common.Node) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) Properties(java.util.Properties) TopicListing(org.apache.kafka.clients.admin.TopicListing) KafkaException(org.apache.kafka.common.KafkaException) NewTopic(org.apache.kafka.clients.admin.NewTopic) ExecutionException(java.util.concurrent.ExecutionException) SecurityDisabledException(org.apache.kafka.common.errors.SecurityDisabledException) AdminClient(org.apache.kafka.clients.admin.AdminClient)

Example 24 with KafkaException

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

the class FileRecordsTest method testTruncateNotCalledIfSizeIsBiggerThanTargetSize.

/**
 * Expect a KafkaException if targetSize is bigger than the size of
 * the FileRecords.
 */
@Test
public void testTruncateNotCalledIfSizeIsBiggerThanTargetSize() throws IOException {
    FileChannel channelMock = EasyMock.createMock(FileChannel.class);
    EasyMock.expect(channelMock.size()).andReturn(42L).atLeastOnce();
    EasyMock.expect(channelMock.position(42L)).andReturn(null);
    EasyMock.replay(channelMock);
    FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false);
    try {
        fileRecords.truncateTo(43);
        fail("Should throw KafkaException");
    } catch (KafkaException e) {
    // expected
    }
    EasyMock.verify(channelMock);
}
Also used : FileChannel(java.nio.channels.FileChannel) KafkaException(org.apache.kafka.common.KafkaException) Test(org.junit.Test)

Example 25 with KafkaException

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

the class TransactionManagerTest method testHasOngoingTransactionAbortableError.

@Test
public void testHasOngoingTransactionAbortableError() {
    long pid = 13131L;
    short epoch = 1;
    TopicPartition partition = new TopicPartition("foo", 0);
    assertFalse(transactionManager.hasOngoingTransaction());
    doInitTransactions(pid, epoch);
    assertFalse(transactionManager.hasOngoingTransaction());
    transactionManager.beginTransaction();
    assertTrue(transactionManager.hasOngoingTransaction());
    transactionManager.maybeAddPartitionToTransaction(partition);
    assertTrue(transactionManager.hasOngoingTransaction());
    prepareAddPartitionsToTxn(partition, Errors.NONE);
    sender.run(time.milliseconds());
    transactionManager.transitionToAbortableError(new KafkaException());
    assertTrue(transactionManager.hasOngoingTransaction());
    transactionManager.beginAbort();
    assertTrue(transactionManager.hasOngoingTransaction());
    prepareEndTxnResponse(Errors.NONE, TransactionResult.ABORT, pid, epoch);
    sender.run(time.milliseconds());
    assertFalse(transactionManager.hasOngoingTransaction());
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) KafkaException(org.apache.kafka.common.KafkaException) Test(org.junit.Test)

Aggregations

KafkaException (org.apache.kafka.common.KafkaException)262 Test (org.junit.Test)69 TopicPartition (org.apache.kafka.common.TopicPartition)56 Test (org.junit.jupiter.api.Test)47 HashMap (java.util.HashMap)40 IOException (java.io.IOException)39 StreamsException (org.apache.kafka.streams.errors.StreamsException)34 Map (java.util.Map)32 TimeoutException (org.apache.kafka.common.errors.TimeoutException)28 ArrayList (java.util.ArrayList)27 List (java.util.List)21 ByteBuffer (java.nio.ByteBuffer)19 ExecutionException (java.util.concurrent.ExecutionException)19 ConfigException (org.apache.kafka.common.config.ConfigException)16 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)14 HashSet (java.util.HashSet)13 Properties (java.util.Properties)13 Set (java.util.Set)11 Collectors (java.util.stream.Collectors)11 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)11