org.apache.kafka.common.protocol.ApiKeys Java Examples

The following examples show how to use org.apache.kafka.common.protocol.ApiKeys. 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: BrokerApiVersion.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static short getListOffsetsVersion() {
	
	if ( apiVersions == null)
		return -1;
	
	// 现在代码最多支持到7
	short version = 2;
	ApiVersion apiVersion = apiVersions.get(ApiKeys.LIST_OFFSETS.id);
	if (apiVersion.maxVersion < version) {
		version = apiVersion.maxVersion;
	} else if (apiVersion.minVersion > version) {
		version = -1;
	}

	return version;
}
 
Example #2
Source File: BrokerApiVersion.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static short getConsumerVersion() {
	
	if ( apiVersions == null)
		return -1;
	
	// 现在代码最多支持到7
	short version = 7;
	ApiVersion apiVersion = apiVersions.get(ApiKeys.FETCH.id);
	if (apiVersion.maxVersion < version) {
		version = apiVersion.maxVersion;
	} else if (apiVersion.minVersion > version) {
		version = -1;
	}
	
	return version;
}
 
Example #3
Source File: BrokerApiVersion.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static short getProduceVersion() {
	
	if ( apiVersions == null)
		return -1;
	
	// 现在代码最多支持到5
	short version = 5;
	ApiVersion apiVersion = apiVersions.get(ApiKeys.PRODUCE.id);
	if (apiVersion.maxVersion < version){
		version =  apiVersion.maxVersion;
	} else if (apiVersion.minVersion > version) {
		version = -1;
	}
	
	return version;
}
 
Example #4
Source File: GroupCoordinator.java    From kop with Apache License 2.0 6 votes vote down vote up
public KeyValue<Errors, Map<TopicPartition, PartitionData>> handleFetchOffsets(
    String groupId,
    Optional<List<TopicPartition>> partitions
) {
    return validateGroupStatus(groupId, ApiKeys.OFFSET_FETCH).map(errors ->
        new KeyValue<Errors, Map<TopicPartition, PartitionData>>(
            errors,
            new HashMap<>()
        )
    ).orElseGet(() ->
        new KeyValue<>(
            Errors.NONE,
            groupManager.getOffsets(groupId, partitions)
        )
    );
}
 
Example #5
Source File: KafkaRequestHandlerTest.java    From kop with Apache License 2.0 6 votes vote down vote up
@Test
public void testByteBufToRequest() {
    int correlationId = 7777;
    String clientId = "KopClientId";

    ApiVersionsRequest apiVersionsRequest = new ApiVersionsRequest.Builder().build();
    RequestHeader header = new RequestHeader(
        ApiKeys.API_VERSIONS,
        ApiKeys.API_VERSIONS.latestVersion(),
        clientId,
        correlationId);

    // 1. serialize request into ByteBuf
    ByteBuffer serializedRequest = apiVersionsRequest.serialize(header);
    int size = serializedRequest.remaining();
    ByteBuf inputBuf = Unpooled.buffer(size);
    inputBuf.writeBytes(serializedRequest);

    // 2. turn Bytebuf into KafkaHeaderAndRequest.
    KafkaHeaderAndRequest request = handler.byteBufToRequest(inputBuf);

    // 3. verify byteBufToRequest works well.
    assertEquals(request.getHeader().toStruct(), header.toStruct());
    assertTrue(request.getRequest() instanceof ApiVersionsRequest);
}
 
Example #6
Source File: KafkaRequestHandler.java    From kop with Apache License 2.0 6 votes vote down vote up
protected ApiVersionsResponse overloadDefaultApiVersionsResponse() {
    List<ApiVersionsResponse.ApiVersion> versionList = new ArrayList<>();
    for (ApiKeys apiKey : ApiKeys.values()) {
        if (apiKey.minRequiredInterBrokerMagic <= RecordBatch.CURRENT_MAGIC_VALUE) {
            switch (apiKey) {
                case FETCH:
                    // V4 added MessageSets responses. We need to make sure RecordBatch format is not used
                    versionList.add(new ApiVersionsResponse.ApiVersion((short) 1, (short) 4,
                            apiKey.latestVersion()));
                    break;
                case LIST_OFFSETS:
                    // V0 is needed for librdkafka
                    versionList.add(new ApiVersionsResponse.ApiVersion((short) 2, (short) 0,
                            apiKey.latestVersion()));
                    break;
                default:
                    versionList.add(new ApiVersionsResponse.ApiVersion(apiKey));
            }
        }
    }
    return new ApiVersionsResponse(0, Errors.NONE, versionList);
}
 
Example #7
Source File: KafkaRequestHandlerTest.java    From kop with Apache License 2.0 5 votes vote down vote up
@Test
public void testResponseToByteBuf() throws Exception {
    int correlationId = 7777;
    String clientId = "KopClientId";

    ApiVersionsRequest apiVersionsRequest = new ApiVersionsRequest.Builder().build();
    RequestHeader requestHeader = new RequestHeader(
        ApiKeys.API_VERSIONS,
        ApiKeys.API_VERSIONS.latestVersion(),
        clientId,
        correlationId);

    KafkaHeaderAndRequest kopRequest = new KafkaHeaderAndRequest(
        requestHeader,
        apiVersionsRequest,
        Unpooled.buffer(20),
        null);

    ApiVersionsResponse apiVersionsResponse = ApiVersionsResponse.defaultApiVersionsResponse();
    KafkaHeaderAndResponse kopResponse = KafkaHeaderAndResponse.responseForRequest(
        kopRequest, apiVersionsResponse);

    // 1. serialize response into ByteBuf
    ByteBuf serializedResponse = handler.responseToByteBuf(kopResponse.getResponse(), kopRequest);

    // 2. verify responseToByteBuf works well.
    ByteBuffer byteBuffer = serializedResponse.nioBuffer();
    ResponseHeader responseHeader = ResponseHeader.parse(byteBuffer);
    assertEquals(responseHeader.correlationId(), correlationId);

    ApiVersionsResponse parsedResponse = ApiVersionsResponse.parse(
        byteBuffer, kopResponse.getApiVersion());

    assertEquals(parsedResponse.apiVersions().size(), apiVersionsResponse.apiVersions().size());
}
 
Example #8
Source File: DefaultKafkaClusterProxy.java    From kafka-message-tool with MIT License 5 votes vote down vote up
private void printApiVersionForNode(Node node, List<ApiVersionsResponse.ApiVersion> apiVersions) {
    StringBuilder builder = new StringBuilder();
    builder.append(String.format("%n### Api version for node %s ###%n", node));
    apiVersions.forEach(version -> {
        builder.append(String.format("ApiKey '%s', min:%d .. max:%d%n", ApiKeys.forId(version.apiKey),
                                     version.minVersion,
                                     version.maxVersion));
    });
    Logger.debug(builder.toString());
}
 
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: SessionHandler.java    From DataLink with Apache License 2.0 5 votes vote down vote up
/**
 * Top-level method that handles all requests and multiplexes to the right api
 */
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
    Request request = new Request(ctx, buffer.toByteBuffer());

    switch (ApiKeys.forId(request.getRequestId())) {
        case METADATA:
            handleMetadataRequest(ctx, request);
            break;
        case GROUP_COORDINATOR:
            handleGroupCoordinatorRequest(ctx, request);
            break;
        case JOIN_GROUP:
            handleJoinGroupRequest(ctx, request);
            break;
        case HEARTBEAT:
            handleHeartbeatRequest(ctx, request);
            break;
        case SYNC_GROUP:
            handleSyncGroupRequest(ctx, request);
            break;
        case LEAVE_GROUP:
            handleLeaveGroupRequest(ctx, request);
            break;
        default:
            throw new DatalinkException("Unknown api code " + request.getRequestId());
    }
}
 
Example #11
Source File: GroupCoordinator.java    From kop with Apache License 2.0 5 votes vote down vote up
private Optional<Errors> validateGroupStatus(String groupId,
                                             ApiKeys api) {
    if (!isValidGroupId(groupId, api)) {
        return Optional.of(Errors.INVALID_GROUP_ID);
    } else if (!isActive.get()) {
        return Optional.of(Errors.COORDINATOR_NOT_AVAILABLE);
    } else if (groupManager.isGroupLoading(groupId)) {
        return Optional.of(Errors.COORDINATOR_LOAD_IN_PROGRESS);
    } else if (!groupManager.isGroupLocal(groupId)) {
        return Optional.of(Errors.NOT_COORDINATOR);
    } else {
        return Optional.empty();
    }
}
 
Example #12
Source File: GroupCoordinator.java    From kop with Apache License 2.0 5 votes vote down vote up
private boolean isValidGroupId(String groupId,
                               ApiKeys api) {
    switch (api) {
        case OFFSET_COMMIT:
        case OFFSET_FETCH:
        case DESCRIBE_GROUPS:
        case DELETE_GROUPS:
            // For backwards compatibility, we support the offset commit APIs for the empty groupId, and also
            // in DescribeGroups and DeleteGroups so that users can view and delete state of all groups.
            return groupId != null;
        default:
            return groupId != null && !groupId.isEmpty();

    }
}
 
Example #13
Source File: GroupCoordinator.java    From kop with Apache License 2.0 5 votes vote down vote up
public KeyValue<Errors, GroupSummary> handleDescribeGroup(String groupId) {
    return validateGroupStatus(groupId, ApiKeys.DESCRIBE_GROUPS).map(error ->
        new KeyValue<>(error, GroupCoordinator.EmptyGroup)
    ).orElseGet(() ->
        groupManager.getGroup(groupId)
            .map(group ->
                group.inLock(() -> new KeyValue<>(Errors.NONE, group.summary())
                ))
            .orElseGet(() -> new KeyValue<>(Errors.NONE, GroupCoordinator.DeadGroup))
    );
}
 
Example #14
Source File: GroupCoordinator.java    From kop with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Map<TopicPartition, Errors>> handleTxnCommitOffsets(
    String groupId,
    long producerId,
    short producerEpoch,
    Map<TopicPartition, OffsetAndMetadata> offsetMetadata
) {
    return validateGroupStatus(groupId, ApiKeys.TXN_OFFSET_COMMIT).map(error ->
        CompletableFuture.completedFuture(
            CoreUtils.mapValue(
                offsetMetadata,
                ignored -> error
            )
        )
    ).orElseGet(() -> {
        GroupMetadata group = groupManager.getGroup(groupId).orElseGet(() ->
            groupManager.addGroup(new GroupMetadata(groupId, Empty))
        );
        return doCommitOffsets(
            group,
            NoMemberId,
            NoGeneration,
            producerId,
            producerEpoch,
            offsetMetadata
        );
    });
}
 
Example #15
Source File: GroupCoordinator.java    From kop with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Errors> handleLeaveGroup(
    String groupId,
    String memberId
) {
    return validateGroupStatus(groupId, ApiKeys.LEAVE_GROUP).map(error ->
        CompletableFuture.completedFuture(error)
    ).orElseGet(() -> {
        return groupManager.getGroup(groupId).map(group -> {
            return group.inLock(() -> {
                if (group.is(Dead) || !group.has(memberId)) {
                    return CompletableFuture.completedFuture(Errors.UNKNOWN_MEMBER_ID);
                } else {
                    MemberMetadata member = group.get(memberId);
                    removeHeartbeatForLeavingMember(group, member);
                    if (log.isDebugEnabled()) {
                        log.debug("Member {} in group {} has left, removing it from the group",
                            member.memberId(), group.groupId());
                    }
                    removeMemberAndUpdateGroup(group, member);
                    return CompletableFuture.completedFuture(Errors.NONE);
                }
            });
        }).orElseGet(() -> {
            // if the group is marked as dead, it means some other thread has just removed the group
            // from the coordinator metadata; this is likely that the group has migrated to some other
            // coordinator OR the group is in a transient unstable phase. Let the consumer to retry
            // joining without specified consumer id,
            return CompletableFuture.completedFuture(Errors.UNKNOWN_MEMBER_ID);
        });
    });
}
 
Example #16
Source File: GroupCoordinator.java    From kop with Apache License 2.0 5 votes vote down vote up
public void handleSyncGroup(String groupId,
                            int generation,
                            String memberId,
                            Map<String, byte[]> groupAssignment,
                            BiConsumer<byte[], Errors> responseCallback) {
    Optional<Errors> errorsOpt = validateGroupStatus(groupId, ApiKeys.SYNC_GROUP);
    if (errorsOpt.isPresent()) {
        Errors error = errorsOpt.get();
        if (Errors.COORDINATOR_LOAD_IN_PROGRESS == error) {
            // The coordinator is loading, which means we've lost the state of the active rebalance and the
            // group will need to start over at JoinGroup. By returning rebalance in progress, the consumer
            // will attempt to rejoin without needing to rediscover the coordinator. Note that we cannot
            // return COORDINATOR_LOAD_IN_PROGRESS since older clients do not expect the error.
            responseCallback.accept(new byte[0], Errors.REBALANCE_IN_PROGRESS);
        } else {
            responseCallback.accept(new byte[0], error);
        }
    } else {
        Optional<GroupMetadata> groupOpt = groupManager.getGroup(groupId);
        if (groupOpt.isPresent()) {
            doSyncGroup(
                groupOpt.get(),
                generation,
                memberId,
                groupAssignment,
                responseCallback
            );
        } else {
            responseCallback.accept(new byte[0], Errors.UNKNOWN_MEMBER_ID);
        }
    }
}
 
Example #17
Source File: NodeApiVersionsInfo.java    From kafka-message-tool with MIT License 4 votes vote down vote up
public boolean doesApiSupportDescribeConfig() {
    return isApiSupportedById(ApiKeys.DESCRIBE_CONFIGS.id);
}
 
Example #18
Source File: ApiVersionsResponse.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ApiVersion(ApiKeys apiKey) {
    this(apiKey.id, apiKey.oldestVersion(), apiKey.latestVersion());
}