org.apache.kafka.common.requests.AbstractRequest Java Examples

The following examples show how to use org.apache.kafka.common.requests.AbstractRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaCommandDecoder.java    From kop with Apache License 2.0 6 votes vote down vote up
protected void writeAndFlushWhenInactiveChannel(Channel channel) {
    // loop from first responseFuture, and return them all
    while (requestsQueue != null && requestsQueue.peek() != null) {
        try {
            ResponseAndRequest pair = requestsQueue.remove();

            if (log.isDebugEnabled()) {
                log.debug("Channel Closing! Write kafka cmd responseFuture back to client. request: {}",
                    pair.getRequest().getHeader());
            }
            AbstractRequest request = pair.getRequest().getRequest();
            AbstractResponse apiResponse = request
                .getErrorResponse(new LeaderNotAvailableException("Channel is closing!"));
            pair.getResponseFuture().complete(apiResponse);

            ByteBuf result = responseToByteBuf(pair.getResponseFuture().get(), pair.getRequest());
            channel.writeAndFlush(result);
        } catch (Exception e) {
            // should not comes here.
            log.error("error to get Response ByteBuf:", e);
        }
    }
}
 
Example #2
Source File: KarelDbCoordinatorTest.java    From kareldb with Apache License 2.0 5 votes vote down vote up
@Test
public void testNormalJoinGroupLeader() {
    final String consumerId = LEADER_ID;

    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));

    // normal join group
    Map<String, KarelDbIdentity> memberInfo = Collections.singletonMap(consumerId, LEADER_INFO);
    client.prepareResponse(joinGroupLeaderResponse(1, consumerId, memberInfo, Errors.NONE));
    SyncGroupResponse syncGroupResponse = syncGroupResponse(
        KarelDbProtocol.Assignment.NO_ERROR,
        consumerId,
        LEADER_INFO,
        Errors.NONE
    );
    client.prepareResponse(new MockClient.RequestMatcher() {
        @Override
        public boolean matches(AbstractRequest body) {
            SyncGroupRequest sync = (SyncGroupRequest) body;
            return sync.data.memberId().equals(consumerId) &&
                sync.data.generationId() == 1 &&
                sync.groupAssignments().containsKey(consumerId);
        }
    }, syncGroupResponse);
    coordinator.ensureActiveGroup();

    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(0, rebalanceListener.revokedCount);
    assertEquals(1, rebalanceListener.assignedCount);
    assertFalse(rebalanceListener.assignments.get(0).failed());
    assertEquals(consumerId, rebalanceListener.assignments.get(0).leader());
    assertEquals(LEADER_INFO, rebalanceListener.assignments.get(0).leaderIdentity());
}
 
Example #3
Source File: KarelDbCoordinatorTest.java    From kareldb with Apache License 2.0 5 votes vote down vote up
@Test
public void testJoinGroupLeaderNoneEligible() {
    final String consumerId = LEADER_ID;

    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));

    Map<String, KarelDbIdentity> memberInfo = Collections.singletonMap(
        consumerId,
        INELIGIBLE_LEADER_INFO
    );
    client.prepareResponse(joinGroupLeaderResponse(1, consumerId, memberInfo, Errors.NONE));
    SyncGroupResponse syncGroupResponse = syncGroupResponse(
        KarelDbProtocol.Assignment.NO_ERROR,
        null,
        null,
        Errors.NONE
    );
    client.prepareResponse(new MockClient.RequestMatcher() {
        @Override
        public boolean matches(AbstractRequest body) {
            SyncGroupRequest sync = (SyncGroupRequest) body;
            return sync.data.memberId().equals(consumerId) &&
                sync.data.generationId() == 1 &&
                sync.groupAssignments().containsKey(consumerId);
        }
    }, syncGroupResponse);

    coordinator.ensureActiveGroup();

    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(0, rebalanceListener.revokedCount);
    assertEquals(1, rebalanceListener.assignedCount);
    // No leader isn't considered a failure
    assertFalse(rebalanceListener.assignments.get(0).failed());
    assertNull(rebalanceListener.assignments.get(0).leader());
    assertNull(rebalanceListener.assignments.get(0).leaderIdentity());
}
 
Example #4
Source File: KarelDbCoordinatorTest.java    From kareldb with Apache License 2.0 5 votes vote down vote up
@Test
public void testJoinGroupLeaderDuplicateUrls() {
    final String consumerId = LEADER_ID;

    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));

    Map<String, KarelDbIdentity> memberInfo = new HashMap<>();
    // intentionally duplicate info to get duplicate URLs
    memberInfo.put(LEADER_ID, LEADER_INFO);
    memberInfo.put(MEMBER_ID, LEADER_INFO);
    client.prepareResponse(joinGroupLeaderResponse(1, consumerId, memberInfo, Errors.NONE));
    SyncGroupResponse syncGroupResponse = syncGroupResponse(
        KarelDbProtocol.Assignment.DUPLICATE_URLS,
        null,
        null,
        Errors.NONE
    );
    client.prepareResponse(new MockClient.RequestMatcher() {
        @Override
        public boolean matches(AbstractRequest body) {
            SyncGroupRequest sync = (SyncGroupRequest) body;
            return sync.data.memberId().equals(consumerId) &&
                sync.data.generationId() == 1 &&
                sync.groupAssignments().containsKey(consumerId);
        }
    }, syncGroupResponse);

    coordinator.ensureActiveGroup();

    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(0, rebalanceListener.revokedCount);
    assertEquals(1, rebalanceListener.assignedCount);
    assertTrue(rebalanceListener.assignments.get(0).failed());
    assertNull(rebalanceListener.assignments.get(0).leader());
    assertNull(rebalanceListener.assignments.get(0).leaderIdentity());
}
 
Example #5
Source File: KarelDbCoordinatorTest.java    From kareldb with Apache License 2.0 5 votes vote down vote up
@Test
public void testNormalJoinGroupFollower() {
    final String consumerId = MEMBER_ID;

    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));

    // normal join group
    client.prepareResponse(joinGroupFollowerResponse(1, consumerId, LEADER_ID, Errors.NONE));
    SyncGroupResponse syncGroupResponse = syncGroupResponse(
        KarelDbProtocol.Assignment.NO_ERROR,
        LEADER_ID,
        LEADER_INFO,
        Errors.NONE
    );
    client.prepareResponse(new MockClient.RequestMatcher() {
        @Override
        public boolean matches(AbstractRequest body) {
            SyncGroupRequest sync = (SyncGroupRequest) body;
            return sync.data.memberId().equals(consumerId) &&
                sync.data.generationId() == 1 &&
                sync.groupAssignments().isEmpty();
        }
    }, syncGroupResponse);
    coordinator.ensureActiveGroup();

    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(0, rebalanceListener.revokedCount);
    assertEquals(1, rebalanceListener.assignedCount);
    assertFalse(rebalanceListener.assignments.get(0).failed());
    assertEquals(LEADER_ID, rebalanceListener.assignments.get(0).leader());
    assertEquals(LEADER_INFO, rebalanceListener.assignments.get(0).leaderIdentity());
}
 
Example #6
Source File: KafkaRequestHandler.java    From kop with Apache License 2.0 5 votes vote down vote up
protected void handleInactive(KafkaHeaderAndRequest kafkaHeaderAndRequest,
                              CompletableFuture<AbstractResponse> resultFuture) {
    AbstractRequest request = kafkaHeaderAndRequest.getRequest();
    AbstractResponse apiResponse = request.getErrorResponse(new LeaderNotAvailableException("Channel is closing!"));

    log.error("Kafka API {} is send to a closing channel", kafkaHeaderAndRequest.getHeader().apiKey());

    resultFuture.complete(apiResponse);
}
 
Example #7
Source File: KafkaCommandDecoder.java    From kop with Apache License 2.0 5 votes vote down vote up
KafkaHeaderAndRequest(RequestHeader header,
                      AbstractRequest request,
                      ByteBuf buffer,
                      SocketAddress remoteAddress) {
    this.header = header;
    this.request = request;
    this.buffer = buffer.retain();
    this.remoteAddress = remoteAddress;
}
 
Example #8
Source File: KafkaApisTest.java    From kop with Apache License 2.0 5 votes vote down vote up
private KafkaHeaderAndRequest createFetchRequest(int maxResponseBytes,
                                                 int maxPartitionBytes,
                                                 List<TopicPartition> topicPartitions,
                                                 Map<TopicPartition, Long> offsetMap) {

    AbstractRequest.Builder builder = FetchRequest.Builder
        .forConsumer(Integer.MAX_VALUE, 0, createPartitionMap(maxPartitionBytes, topicPartitions, offsetMap))
        .setMaxBytes(maxResponseBytes);

    return buildRequest(builder);
}
 
Example #9
Source File: Request.java    From DataLink with Apache License 2.0 5 votes vote down vote up
Request(ChannelHandlerContext ctx, ByteBuffer buffer) {
    this.requestId = buffer.getShort();

    buffer.rewind();
    header = RequestHeader.parse(buffer);

    if (header.apiKey() == ApiKeys.API_VERSIONS.id && !Protocol.apiVersionSupported(header.apiKey(), header.apiVersion())) {
        body = new ApiVersionsRequest();
    } else {
        body = AbstractRequest.getRequest(header.apiKey(), header.apiVersion(), buffer);
    }

    this.clientAddress = ((InetSocketAddress) ctx.getChannel().getRemoteAddress()).getAddress();
}
 
Example #10
Source File: KafkaCommandDecoder.java    From kop with Apache License 2.0 4 votes vote down vote up
public AbstractRequest getRequest() {
    return this.request;
}
 
Example #11
Source File: KafkaApisTest.java    From kop with Apache License 2.0 4 votes vote down vote up
private KafkaHeaderAndRequest createTopicMetadataRequest(List<String> topics) {
    AbstractRequest.Builder builder = new MetadataRequest.Builder(topics, true);
    return buildRequest(builder);
}
 
Example #12
Source File: Request.java    From DataLink with Apache License 2.0 4 votes vote down vote up
AbstractRequest getBody() {
    return body;
}
 
Example #13
Source File: KafkaApiRequest.java    From kafka-utilities with Apache License 2.0 4 votes vote down vote up
public void sendApiRequest(final Node node, final AbstractRequest.Builder<?> requestBuilder){
    this.clientResponse  = this.networkClient.send(node, requestBuilder);
}
 
Example #14
Source File: KafkaNodeClient.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
public ClientRequest newClientRequest(AbstractRequest.Builder<?> requestBuilder) {

		if (client == null || node == null)
			return null;
		
		ClientRequest clientRequest = client.newClientRequest(node.idString(), requestBuilder, 1000L, true);
		return clientRequest;

	}