Search in sources :

Example 31 with AbstractRequest

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

the class MockClient method respond.

public void respond(RequestMatcher matcher, AbstractResponse response) {
    ClientRequest nextRequest = requests.peek();
    if (nextRequest == null)
        throw new IllegalStateException("No current requests queued");
    AbstractRequest request = nextRequest.requestBuilder().build();
    if (!matcher.matches(request))
        throw new IllegalStateException("Request matcher did not match next-in-line request " + request);
    respond(response);
}
Also used : AbstractRequest(org.apache.kafka.common.requests.AbstractRequest)

Example 32 with AbstractRequest

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

the class SenderTest method testCanRetryWithoutIdempotence.

@Test
public void testCanRetryWithoutIdempotence() throws Exception {
    // do a successful retry
    Future<RecordMetadata> future = accumulator.append(tp0, 0L, "key".getBytes(), "value".getBytes(), null, null, MAX_BLOCK_TIMEOUT).future;
    // connect
    sender.run(time.milliseconds());
    // send produce request
    sender.run(time.milliseconds());
    String id = client.requests().peek().destination();
    Node node = new Node(Integer.parseInt(id), "localhost", 0);
    assertEquals(1, client.inFlightRequestCount());
    assertTrue(client.hasInFlightRequests());
    assertTrue("Client ready status should be true", client.isReady(node, 0L));
    assertFalse(future.isDone());
    client.respond(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            ProduceRequest request = (ProduceRequest) body;
            assertFalse(request.isIdempotent());
            return true;
        }
    }, produceResponse(tp0, -1L, Errors.TOPIC_AUTHORIZATION_FAILED, 0));
    sender.run(time.milliseconds());
    assertTrue(future.isDone());
    try {
        future.get();
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof TopicAuthorizationException);
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) Node(org.apache.kafka.common.Node) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) OutOfOrderSequenceException(org.apache.kafka.common.errors.OutOfOrderSequenceException) UnsupportedForMessageFormatException(org.apache.kafka.common.errors.UnsupportedForMessageFormatException) RecordTooLargeException(org.apache.kafka.common.errors.RecordTooLargeException) NetworkException(org.apache.kafka.common.errors.NetworkException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 33 with AbstractRequest

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

the class SenderTest method prepareAndReceiveInitProducerId.

private void prepareAndReceiveInitProducerId(long producerId, Errors error) {
    short producerEpoch = 0;
    if (error != Errors.NONE)
        producerEpoch = RecordBatch.NO_PRODUCER_EPOCH;
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            return body instanceof InitProducerIdRequest && ((InitProducerIdRequest) body).transactionalId() == null;
        }
    }, new InitProducerIdResponse(0, error, producerId, producerEpoch));
    sender.run(time.milliseconds());
}
Also used : AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) InitProducerIdRequest(org.apache.kafka.common.requests.InitProducerIdRequest) InitProducerIdResponse(org.apache.kafka.common.requests.InitProducerIdResponse) MockClient(org.apache.kafka.clients.MockClient)

Example 34 with AbstractRequest

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

the class SenderTest method testDownConversionForMismatchedMagicValues.

@Test
public void testDownConversionForMismatchedMagicValues() throws Exception {
    // it can happen that we construct a record set with mismatching magic values (perhaps
    // because the partition leader changed after the record set was initially constructed)
    // in this case, we down-convert record sets with newer magic values to match the oldest
    // created record set
    long offset = 0;
    // start off support produce request v3
    apiVersions.update("0", NodeApiVersions.create());
    Future<RecordMetadata> future1 = accumulator.append(tp0, 0L, "key".getBytes(), "value".getBytes(), null, null, MAX_BLOCK_TIMEOUT).future;
    // now the partition leader supports only v2
    apiVersions.update("0", NodeApiVersions.create(Collections.singleton(new ApiVersionsResponse.ApiVersion(ApiKeys.PRODUCE.id, (short) 0, (short) 2))));
    Future<RecordMetadata> future2 = accumulator.append(tp1, 0L, "key".getBytes(), "value".getBytes(), null, null, MAX_BLOCK_TIMEOUT).future;
    // start off support produce request v3
    apiVersions.update("0", NodeApiVersions.create());
    ProduceResponse.PartitionResponse resp = new ProduceResponse.PartitionResponse(Errors.NONE, offset, RecordBatch.NO_TIMESTAMP, 100);
    Map<TopicPartition, ProduceResponse.PartitionResponse> partResp = new HashMap<>();
    partResp.put(tp0, resp);
    partResp.put(tp1, resp);
    ProduceResponse produceResponse = new ProduceResponse(partResp, 0);
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            ProduceRequest request = (ProduceRequest) body;
            if (request.version() != 2)
                return false;
            Map<TopicPartition, MemoryRecords> recordsMap = request.partitionRecordsOrFail();
            if (recordsMap.size() != 2)
                return false;
            for (MemoryRecords records : recordsMap.values()) {
                if (records == null || records.sizeInBytes() == 0 || !records.hasMatchingMagic(RecordBatch.MAGIC_VALUE_V1))
                    return false;
            }
            return true;
        }
    }, produceResponse);
    // connect
    sender.run(time.milliseconds());
    // send produce request
    sender.run(time.milliseconds());
    assertTrue("Request should be completed", future1.isDone());
    assertTrue("Request should be completed", future2.isDone());
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) ProduceResponse(org.apache.kafka.common.requests.ProduceResponse) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) TopicPartition(org.apache.kafka.common.TopicPartition) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MockClient(org.apache.kafka.clients.MockClient) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) Test(org.junit.Test)

Example 35 with AbstractRequest

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

the class SenderTest method testClusterAuthorizationExceptionInProduceRequest.

@Test
public void testClusterAuthorizationExceptionInProduceRequest() throws Exception {
    final long producerId = 343434L;
    TransactionManager transactionManager = new TransactionManager();
    setupWithTransactionState(transactionManager);
    client.setNode(new Node(1, "localhost", 33343));
    prepareAndReceiveInitProducerId(producerId, Errors.NONE);
    assertTrue(transactionManager.hasProducerId());
    // cluster authorization is a fatal error for the producer
    Future<RecordMetadata> future = accumulator.append(tp0, time.milliseconds(), "key".getBytes(), "value".getBytes(), null, null, MAX_BLOCK_TIMEOUT).future;
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            return body instanceof ProduceRequest && ((ProduceRequest) body).isIdempotent();
        }
    }, produceResponse(tp0, -1, Errors.CLUSTER_AUTHORIZATION_FAILED, 0));
    sender.run(time.milliseconds());
    assertFutureFailure(future, ClusterAuthorizationException.class);
    // cluster authorization errors are fatal, so we should continue seeing it on future sends
    assertTrue(transactionManager.hasFatalError());
    assertSendFailure(ClusterAuthorizationException.class);
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) Node(org.apache.kafka.common.Node) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Aggregations

AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)48 MockClient (org.apache.kafka.clients.MockClient)38 Test (org.junit.Test)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 WakeupException (org.apache.kafka.common.errors.WakeupException)12 Node (org.apache.kafka.common.Node)11 ProduceRequest (org.apache.kafka.common.requests.ProduceRequest)9 SyncGroupRequest (org.apache.kafka.common.requests.SyncGroupRequest)9 JoinGroupRequest (org.apache.kafka.common.requests.JoinGroupRequest)8 HashMap (java.util.HashMap)7 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)7 TopicPartition (org.apache.kafka.common.TopicPartition)6 Map (java.util.Map)5 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)5 LeaveGroupResponse (org.apache.kafka.common.requests.LeaveGroupResponse)4 Collections.singletonMap (java.util.Collections.singletonMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Metrics (org.apache.kafka.common.metrics.Metrics)3 MutableRecordBatch (org.apache.kafka.common.record.MutableRecordBatch)3 FetchRequest (org.apache.kafka.common.requests.FetchRequest)3