use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.
the class KafkaConsumerTest method prepareOffsetCommitResponse.
private AtomicBoolean prepareOffsetCommitResponse(MockClient client, Node coordinator, final Map<TopicPartition, Long> partitionOffsets) {
final AtomicBoolean commitReceived = new AtomicBoolean(true);
Map<TopicPartition, Errors> response = new HashMap<>();
for (TopicPartition partition : partitionOffsets.keySet()) response.put(partition, Errors.NONE);
client.prepareResponseFrom(new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
for (Map.Entry<TopicPartition, Long> partitionOffset : partitionOffsets.entrySet()) {
OffsetCommitRequest.PartitionData partitionData = commitRequest.offsetData().get(partitionOffset.getKey());
// verify that the expected offset has been committed
if (partitionData.offset != partitionOffset.getValue()) {
commitReceived.set(false);
return false;
}
}
return true;
}
}, offsetCommitResponse(response), coordinator);
return commitReceived;
}
use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.
the class TransactionManagerTest method produceRequestMatcher.
private MockClient.RequestMatcher produceRequestMatcher(final long pid, final short epoch) {
return new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
ProduceRequest produceRequest = (ProduceRequest) body;
MemoryRecords records = produceRequest.partitionRecordsOrFail().get(tp0);
assertNotNull(records);
Iterator<MutableRecordBatch> batchIterator = records.batches().iterator();
assertTrue(batchIterator.hasNext());
MutableRecordBatch batch = batchIterator.next();
assertFalse(batchIterator.hasNext());
assertTrue(batch.isTransactional());
assertEquals(pid, batch.producerId());
assertEquals(epoch, batch.producerEpoch());
assertEquals(transactionalId, produceRequest.transactionalId());
return true;
}
};
}
use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.
the class TransactionManagerTest method addPartitionsRequestMatcher.
private MockClient.RequestMatcher addPartitionsRequestMatcher(final TopicPartition topicPartition, final short epoch, final long pid) {
return new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
AddPartitionsToTxnRequest addPartitionsToTxnRequest = (AddPartitionsToTxnRequest) body;
assertEquals(pid, addPartitionsToTxnRequest.producerId());
assertEquals(epoch, addPartitionsToTxnRequest.producerEpoch());
assertEquals(singletonList(topicPartition), addPartitionsToTxnRequest.partitions());
assertEquals(transactionalId, addPartitionsToTxnRequest.transactionalId());
return true;
}
};
}
use of org.apache.kafka.common.requests.AbstractRequest in project kafka by apache.
the class MockClient method send.
@Override
public void send(ClientRequest request, long now) {
if (!connectionState(request.destination()).isReady(now))
throw new IllegalStateException("Cannot send " + request + " since the destination is not ready");
// Check if the request is directed to a node with a pending authentication error.
for (Iterator<Map.Entry<Node, Long>> authErrorIter = pendingAuthenticationErrors.entrySet().iterator(); authErrorIter.hasNext(); ) {
Map.Entry<Node, Long> entry = authErrorIter.next();
Node node = entry.getKey();
long backoffMs = entry.getValue();
if (node.idString().equals(request.destination())) {
authErrorIter.remove();
// Set up a disconnected ClientResponse and create an authentication error
// for the affected node.
authenticationFailed(node, backoffMs);
AbstractRequest.Builder<?> builder = request.requestBuilder();
short version = nodeApiVersions.latestUsableVersion(request.apiKey(), builder.oldestAllowedVersion(), builder.latestAllowedVersion());
ClientResponse resp = new ClientResponse(request.makeHeader(version), request.callback(), request.destination(), request.createdTimeMs(), time.milliseconds(), true, null, new AuthenticationException("Authentication failed"), null);
responses.add(resp);
return;
}
}
Iterator<FutureResponse> iterator = futureResponses.iterator();
while (iterator.hasNext()) {
FutureResponse futureResp = iterator.next();
if (futureResp.node != null && !request.destination().equals(futureResp.node.idString()))
continue;
AbstractRequest.Builder<?> builder = request.requestBuilder();
try {
short version = nodeApiVersions.latestUsableVersion(request.apiKey(), builder.oldestAllowedVersion(), builder.latestAllowedVersion());
UnsupportedVersionException unsupportedVersionException = null;
if (futureResp.isUnsupportedRequest) {
unsupportedVersionException = new UnsupportedVersionException("Api " + request.apiKey() + " with version " + version);
} else {
AbstractRequest abstractRequest = request.requestBuilder().build(version);
if (!futureResp.requestMatcher.matches(abstractRequest))
throw new IllegalStateException("Request matcher did not match next-in-line request " + abstractRequest + " with prepared response " + futureResp.responseBody);
}
ClientResponse resp = new ClientResponse(request.makeHeader(version), request.callback(), request.destination(), request.createdTimeMs(), time.milliseconds(), futureResp.disconnected, unsupportedVersionException, null, futureResp.responseBody);
responses.add(resp);
} catch (UnsupportedVersionException unsupportedVersionException) {
ClientResponse resp = new ClientResponse(request.makeHeader(builder.latestAllowedVersion()), request.callback(), request.destination(), request.createdTimeMs(), time.milliseconds(), false, unsupportedVersionException, null, null);
responses.add(resp);
}
iterator.remove();
return;
}
this.requests.add(request);
}
use of org.apache.kafka.common.requests.AbstractRequest in project kafka by apache.
the class AbstractCoordinatorTest method testWakeupAfterJoinGroupSent.
@Test
public void testWakeupAfterJoinGroupSent() throws Exception {
setupCoordinator();
mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
mockClient.prepareResponse(new MockClient.RequestMatcher() {
private int invocations = 0;
@Override
public boolean matches(AbstractRequest body) {
invocations++;
boolean isJoinGroupRequest = body instanceof JoinGroupRequest;
if (isJoinGroupRequest && invocations == 1)
// simulate wakeup before the request returns
throw new WakeupException();
return isJoinGroupRequest;
}
}, joinGroupFollowerResponse(1, memberId, leaderId, Errors.NONE));
mockClient.prepareResponse(syncGroupResponse(Errors.NONE));
AtomicBoolean heartbeatReceived = prepareFirstHeartbeat();
try {
coordinator.ensureActiveGroup();
fail("Should have woken up from ensureActiveGroup()");
} catch (WakeupException ignored) {
}
assertEquals(1, coordinator.onJoinPrepareInvokes);
assertEquals(0, coordinator.onJoinCompleteInvokes);
assertFalse(heartbeatReceived.get());
coordinator.ensureActiveGroup();
assertEquals(1, coordinator.onJoinPrepareInvokes);
assertEquals(1, coordinator.onJoinCompleteInvokes);
awaitFirstHeartbeat(heartbeatReceived);
}
Aggregations