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);
}
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);
}
}
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());
}
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());
}
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);
}
Aggregations