Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ public long writeTo(GatheringByteChannel channel) throws IOException {
remaining -= written;
return written;
}

// Used only by BlockingChannel, so we may be able to get rid of this when/if we get rid of BlockingChannel
public long writeCompletelyTo(GatheringByteChannel channel) throws IOException {
long totalWritten = 0L;
while (!completed()) {
long written = writeTo(channel);
totalWritten += written;
}
return totalWritten;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
*/
package org.apache.kafka.common.protocol;

import static org.apache.kafka.common.protocol.types.Type.BYTES;
import static org.apache.kafka.common.protocol.types.Type.INT16;
import static org.apache.kafka.common.protocol.types.Type.INT32;
import static org.apache.kafka.common.protocol.types.Type.INT64;
import static org.apache.kafka.common.protocol.types.Type.STRING;

import org.apache.kafka.common.protocol.types.ArrayOf;
import org.apache.kafka.common.protocol.types.Field;
import org.apache.kafka.common.protocol.types.Schema;

import static org.apache.kafka.common.protocol.types.Type.*;

public class Protocol {

public static final Schema REQUEST_HEADER = new Schema(new Field("api_key", INT16, "The id of the request type."),
Expand Down Expand Up @@ -420,6 +416,27 @@ public class Protocol {
public static final Schema[] HEARTBEAT_REQUEST = new Schema[] {HEARTBEAT_REQUEST_V0};
public static final Schema[] HEARTBEAT_RESPONSE = new Schema[] {HEARTBEAT_RESPONSE_V0};

/* Replica api */
public static final Schema STOP_REPLICA_REQUEST_PARTITION_V0 = new Schema(new Field("topic", STRING, "The partition name."),
new Field("partition", INT32, "Topic partition id."));

public static final Schema STOP_REPLICA_REQUEST_V0 = new Schema(new Field("controller_id", INT32, "The controller id."),
new Field("controller_epoch", INT32, "The controller epoch."),
new Field("delete_partitions", INT8),
new Field("partitions",
new ArrayOf(STOP_REPLICA_REQUEST_PARTITION_V0)));

public static final Schema STOP_REPLICA_RESPONSE_PARTITION_V0 = new Schema(new Field("topic", STRING, "The partition name."),
new Field("partition", INT32, "Topic partition id."),
new Field("error_code", INT16));

public static final Schema STOP_REPLICA_RESPONSE_V0 = new Schema(new Field("error_code", INT16),
new Field("partitions",
new ArrayOf(STOP_REPLICA_RESPONSE_PARTITION_V0)));

public static final Schema[] STOP_REPLICA_REQUEST = new Schema[] {STOP_REPLICA_REQUEST_V0};
public static final Schema[] STOP_REPLICA_RESPONSE = new Schema[] {STOP_REPLICA_RESPONSE_V0};

/* an array of all requests and responses with all schema versions */
public static final Schema[][] REQUESTS = new Schema[ApiKeys.MAX_API_KEY + 1][];
public static final Schema[][] RESPONSES = new Schema[ApiKeys.MAX_API_KEY + 1][];
Expand All @@ -433,7 +450,7 @@ public class Protocol {
REQUESTS[ApiKeys.LIST_OFFSETS.id] = LIST_OFFSET_REQUEST;
REQUESTS[ApiKeys.METADATA.id] = METADATA_REQUEST;
REQUESTS[ApiKeys.LEADER_AND_ISR.id] = new Schema[] {};
REQUESTS[ApiKeys.STOP_REPLICA.id] = new Schema[] {};
REQUESTS[ApiKeys.STOP_REPLICA.id] = STOP_REPLICA_REQUEST;
REQUESTS[ApiKeys.UPDATE_METADATA_KEY.id] = new Schema[] {};
REQUESTS[ApiKeys.CONTROLLED_SHUTDOWN_KEY.id] = new Schema[] {};
REQUESTS[ApiKeys.OFFSET_COMMIT.id] = OFFSET_COMMIT_REQUEST;
Expand All @@ -442,12 +459,13 @@ public class Protocol {
REQUESTS[ApiKeys.JOIN_GROUP.id] = JOIN_GROUP_REQUEST;
REQUESTS[ApiKeys.HEARTBEAT.id] = HEARTBEAT_REQUEST;


RESPONSES[ApiKeys.PRODUCE.id] = PRODUCE_RESPONSE;
RESPONSES[ApiKeys.FETCH.id] = FETCH_RESPONSE;
RESPONSES[ApiKeys.LIST_OFFSETS.id] = LIST_OFFSET_RESPONSE;
RESPONSES[ApiKeys.METADATA.id] = METADATA_RESPONSE;
RESPONSES[ApiKeys.LEADER_AND_ISR.id] = new Schema[] {};
RESPONSES[ApiKeys.STOP_REPLICA.id] = new Schema[] {};
RESPONSES[ApiKeys.STOP_REPLICA.id] = STOP_REPLICA_RESPONSE;
RESPONSES[ApiKeys.UPDATE_METADATA_KEY.id] = new Schema[] {};
RESPONSES[ApiKeys.CONTROLLED_SHUTDOWN_KEY.id] = new Schema[] {};
RESPONSES[ApiKeys.OFFSET_COMMIT.id] = OFFSET_COMMIT_RESPONSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public static AbstractRequest getRequest(int requestId, int versionId, ByteBuffe
return JoinGroupRequest.parse(buffer, versionId);
case HEARTBEAT:
return HeartbeatRequest.parse(buffer, versionId);
case STOP_REPLICA:
return StopReplicaRequest.parse(buffer, versionId);
default:
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package org.apache.kafka.common.requests;

import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.protocol.ApiKeys;
import org.apache.kafka.common.protocol.Errors;
import org.apache.kafka.common.protocol.ProtoUtils;
import org.apache.kafka.common.protocol.types.Schema;
import org.apache.kafka.common.protocol.types.Struct;

import java.nio.ByteBuffer;
import java.util.*;

public class StopReplicaRequest extends AbstractRequest {
private static final Schema CURRENT_SCHEMA = ProtoUtils.currentRequestSchema(ApiKeys.STOP_REPLICA.id);

private static final String CONTROLLER_ID_KEY_NAME = "controller_id";
private static final String CONTROLLER_EPOCH_KEY_NAME = "controller_epoch";
private static final String DELETE_PARTITIONS_KEY_NAME = "delete_partitions";
private static final String PARTITIONS_KEY_NAME = "partitions";

private static final String TOPIC_KEY_NAME = "topic";
private static final String PARTITION_KEY_NAME = "partition";

private final int controllerId;
private final int controllerEpoch;
private final boolean deletePartitions;
private final Set<TopicPartition> partitions;

public StopReplicaRequest(int controllerId, int controllerEpoch, boolean deletePartitions, Set<TopicPartition> partitions) {
super(new Struct(CURRENT_SCHEMA));

struct.set(CONTROLLER_ID_KEY_NAME, controllerId);
struct.set(CONTROLLER_EPOCH_KEY_NAME, controllerEpoch);
struct.set(DELETE_PARTITIONS_KEY_NAME, deletePartitions ? (byte) 1 : (byte) 0);

List<Struct> partitionDatas = new ArrayList<>(partitions.size());
for (TopicPartition partition : partitions) {
Struct partitionData = struct.instance(PARTITIONS_KEY_NAME);
partitionData.set(TOPIC_KEY_NAME, partition.topic());
partitionData.set(PARTITION_KEY_NAME, partition.partition());
partitionDatas.add(partitionData);
}

struct.set(PARTITIONS_KEY_NAME, partitionDatas.toArray());

this.controllerId = controllerId;
this.controllerEpoch = controllerEpoch;
this.deletePartitions = deletePartitions;
this.partitions = partitions;
}

public StopReplicaRequest(Struct struct) {
super(struct);

partitions = new HashSet<>();
for (Object partitionDataObj : struct.getArray(PARTITIONS_KEY_NAME)) {
Struct partitionData = (Struct) partitionDataObj;
String topic = partitionData.getString(TOPIC_KEY_NAME);
int partition = partitionData.getInt(PARTITION_KEY_NAME);
partitions.add(new TopicPartition(topic, partition));
}

controllerId = struct.getInt(CONTROLLER_ID_KEY_NAME);
controllerEpoch = struct.getInt(CONTROLLER_EPOCH_KEY_NAME);
deletePartitions = ((byte) struct.get(DELETE_PARTITIONS_KEY_NAME)) != 0;
}

@Override
public AbstractRequestResponse getErrorResponse(int versionId, Throwable e) {
Map<TopicPartition, Short> responses = new HashMap<>(partitions.size());
for (TopicPartition partition : partitions) {
responses.put(partition, Errors.forException(e).code());
}

switch (versionId) {
case 0:
return new StopReplicaResponse(responses, Errors.NONE.code());
default:
throw new IllegalArgumentException(String.format("Version %d is not valid. Valid versions for %s are 0 to %d",
versionId, this.getClass().getSimpleName(), ProtoUtils.latestVersion(ApiKeys.STOP_REPLICA.id)));
}
}

public int controllerId() {
return controllerId;
}

public int controllerEpoch() {
return controllerEpoch;
}

public boolean deletePartitions() {
return deletePartitions;
}

public Set<TopicPartition> partitions() {
return partitions;
}

public static StopReplicaRequest parse(ByteBuffer buffer, int versionId) {
return new StopReplicaRequest(ProtoUtils.parseRequest(ApiKeys.STOP_REPLICA.id, versionId, buffer));
}

public static StopReplicaRequest parse(ByteBuffer buffer) {
return new StopReplicaRequest((Struct) CURRENT_SCHEMA.read(buffer));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package org.apache.kafka.common.requests;

import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.protocol.ApiKeys;
import org.apache.kafka.common.protocol.Errors;
import org.apache.kafka.common.protocol.ProtoUtils;
import org.apache.kafka.common.protocol.types.Schema;
import org.apache.kafka.common.protocol.types.Struct;

import java.nio.ByteBuffer;
import java.util.*;

public class StopReplicaResponse extends AbstractRequestResponse {
private static final Schema CURRENT_SCHEMA = ProtoUtils.currentResponseSchema(ApiKeys.STOP_REPLICA.id);

private static final String ERROR_CODE_KEY_NAME = "error_code";
private static final String PARTITIONS_KEY_NAME = "partitions";

private static final String PARTITIONS_TOPIC_KEY_NAME = "topic";
private static final String PARTITIONS_PARTITION_KEY_NAME = "partition";
private static final String PARTITIONS_ERROR_CODE_KEY_NAME = "error_code";

private final Map<TopicPartition, Short> responses;
private final short errorCode;

public StopReplicaResponse(Map<TopicPartition, Short> responses) {
this(responses, Errors.NONE.code());
}

public StopReplicaResponse(Map<TopicPartition, Short> responses, short errorCode) {
super(new Struct(CURRENT_SCHEMA));

struct.set(ERROR_CODE_KEY_NAME, errorCode);

List<Struct> responseDatas = new ArrayList<>(responses.size());
for (Map.Entry<TopicPartition, Short> response : responses.entrySet()) {
Struct partitionData = struct.instance(PARTITIONS_KEY_NAME);
TopicPartition partition = response.getKey();
partitionData.set(PARTITIONS_TOPIC_KEY_NAME, partition.topic());
partitionData.set(PARTITIONS_PARTITION_KEY_NAME, partition.partition());
partitionData.set(PARTITIONS_ERROR_CODE_KEY_NAME, response.getValue());
responseDatas.add(partitionData);
}

struct.set(PARTITIONS_KEY_NAME, responseDatas.toArray());
struct.set(ERROR_CODE_KEY_NAME, errorCode);

this.responses = responses;
this.errorCode = errorCode;
}

public StopReplicaResponse(Struct struct) {
super(struct);

responses = new HashMap<>();
for (Object responseDataObj : struct.getArray(PARTITIONS_KEY_NAME)) {
Struct responseData = (Struct) responseDataObj;
String topic = responseData.getString(PARTITIONS_TOPIC_KEY_NAME);
int partition = responseData.getInt(PARTITIONS_PARTITION_KEY_NAME);
short errorCode = responseData.getShort(PARTITIONS_ERROR_CODE_KEY_NAME);
responses.put(new TopicPartition(topic, partition), errorCode);
}

errorCode = struct.getShort(ERROR_CODE_KEY_NAME);
}

public Map<TopicPartition, Short> responses() {
return responses;
}

public short errorCode() {
return errorCode;
}

public static StopReplicaResponse parse(ByteBuffer buffer, int versionId) {
return new StopReplicaResponse(ProtoUtils.parseRequest(ApiKeys.STOP_REPLICA.id, versionId, buffer));
}

public static StopReplicaResponse parse(ByteBuffer buffer) {
return new StopReplicaResponse((Struct) CURRENT_SCHEMA.read(buffer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@

import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -63,7 +60,10 @@ public void testSerialization() throws Exception {
createOffsetFetchResponse(),
createProduceRequest(),
createProduceRequest().getErrorResponse(0, new UnknownServerException()),
createProduceResponse());
createProduceResponse(),
createStopReplicaRequest(),
createStopReplicaRequest().getErrorResponse(0, new UnknownServerException()),
createStopReplicaResponse());

for (AbstractRequestResponse req: requestResponseList) {
ByteBuffer buffer = ByteBuffer.allocate(req.sizeOf());
Expand Down Expand Up @@ -184,4 +184,16 @@ private AbstractRequestResponse createProduceResponse() {
responseData.put(new TopicPartition("test", 0), new ProduceResponse.PartitionResponse(Errors.NONE.code(), 10000));
return new ProduceResponse(responseData);
}

private AbstractRequest createStopReplicaRequest() {
Set<TopicPartition> partitions = new HashSet<>();
partitions.add(new TopicPartition("test", 0));
return new StopReplicaRequest(0, 1, true, partitions);
}

private AbstractRequestResponse createStopReplicaResponse() {
Map<TopicPartition, Short> responses = new HashMap<>();
responses.put(new TopicPartition("test", 0), Errors.NONE.code());
return new StopReplicaResponse(responses, Errors.NONE.code());
}
}
1 change: 0 additions & 1 deletion core/src/main/scala/kafka/api/RequestKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ object RequestKeys {
OffsetsKey -> ("Offsets", OffsetRequest.readFrom),
MetadataKey -> ("Metadata", TopicMetadataRequest.readFrom),
LeaderAndIsrKey -> ("LeaderAndIsr", LeaderAndIsrRequest.readFrom),
StopReplicaKey -> ("StopReplica", StopReplicaRequest.readFrom),
UpdateMetadataKey -> ("UpdateMetadata", UpdateMetadataRequest.readFrom),
ControlledShutdownKey -> ("ControlledShutdown", ControlledShutdownRequest.readFrom),
OffsetCommitKey -> ("OffsetCommit", OffsetCommitRequest.readFrom),
Expand Down
Loading