/clients/src/main/java/org/apache/kafka/common/requests/AlterReplicaLogDirsResponse.java

https://github.com/linkedin/kafka · Java · 146 lines · 94 code · 20 blank · 32 comment · 4 complexity · 0208e368c9df0d11644d76defdade15e MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.kafka.common.requests;
  18. import org.apache.kafka.common.TopicPartition;
  19. import org.apache.kafka.common.protocol.ApiKeys;
  20. import org.apache.kafka.common.protocol.Errors;
  21. import org.apache.kafka.common.protocol.types.ArrayOf;
  22. import org.apache.kafka.common.protocol.types.Field;
  23. import org.apache.kafka.common.protocol.types.Schema;
  24. import org.apache.kafka.common.protocol.types.Struct;
  25. import org.apache.kafka.common.utils.CollectionUtils;
  26. import java.nio.ByteBuffer;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import static org.apache.kafka.common.protocol.CommonFields.ERROR_CODE;
  32. import static org.apache.kafka.common.protocol.CommonFields.PARTITION_ID;
  33. import static org.apache.kafka.common.protocol.CommonFields.THROTTLE_TIME_MS;
  34. import static org.apache.kafka.common.protocol.CommonFields.TOPIC_NAME;
  35. public class AlterReplicaLogDirsResponse extends AbstractResponse {
  36. // request level key names
  37. private static final String TOPICS_KEY_NAME = "topics";
  38. // topic level key names
  39. private static final String PARTITIONS_KEY_NAME = "partitions";
  40. private static final Schema ALTER_REPLICA_LOG_DIRS_RESPONSE_V0 = new Schema(
  41. THROTTLE_TIME_MS,
  42. new Field(TOPICS_KEY_NAME, new ArrayOf(new Schema(
  43. TOPIC_NAME,
  44. new Field(PARTITIONS_KEY_NAME, new ArrayOf(new Schema(
  45. PARTITION_ID,
  46. ERROR_CODE)))))));
  47. /**
  48. * The version number is bumped to indicate that on quota violation brokers send out responses before throttling.
  49. */
  50. private static final Schema ALTER_REPLICA_LOG_DIRS_RESPONSE_V1 = ALTER_REPLICA_LOG_DIRS_RESPONSE_V0;
  51. public static Schema[] schemaVersions() {
  52. return new Schema[]{ALTER_REPLICA_LOG_DIRS_RESPONSE_V0, ALTER_REPLICA_LOG_DIRS_RESPONSE_V1};
  53. }
  54. /**
  55. * Possible error code:
  56. *
  57. * LOG_DIR_NOT_FOUND (57)
  58. * KAFKA_STORAGE_ERROR (56)
  59. * REPLICA_NOT_AVAILABLE (9)
  60. * UNKNOWN (-1)
  61. */
  62. private final Map<TopicPartition, Errors> responses;
  63. private final int throttleTimeMs;
  64. public AlterReplicaLogDirsResponse(Struct struct) {
  65. throttleTimeMs = struct.get(THROTTLE_TIME_MS);
  66. responses = new HashMap<>();
  67. for (Object topicStructObj : struct.getArray(TOPICS_KEY_NAME)) {
  68. Struct topicStruct = (Struct) topicStructObj;
  69. String topic = topicStruct.get(TOPIC_NAME);
  70. for (Object partitionStructObj : topicStruct.getArray(PARTITIONS_KEY_NAME)) {
  71. Struct partitionStruct = (Struct) partitionStructObj;
  72. int partition = partitionStruct.get(PARTITION_ID);
  73. Errors error = Errors.forCode(partitionStruct.get(ERROR_CODE));
  74. responses.put(new TopicPartition(topic, partition), error);
  75. }
  76. }
  77. }
  78. /**
  79. * Constructor for version 0.
  80. */
  81. public AlterReplicaLogDirsResponse(int throttleTimeMs, Map<TopicPartition, Errors> responses) {
  82. this.throttleTimeMs = throttleTimeMs;
  83. this.responses = responses;
  84. }
  85. @Override
  86. protected Struct toStruct(short version) {
  87. Struct struct = new Struct(ApiKeys.ALTER_REPLICA_LOG_DIRS.responseSchema(version));
  88. struct.set(THROTTLE_TIME_MS, throttleTimeMs);
  89. Map<String, Map<Integer, Errors>> responsesByTopic = CollectionUtils.groupPartitionDataByTopic(responses);
  90. List<Struct> topicStructArray = new ArrayList<>();
  91. for (Map.Entry<String, Map<Integer, Errors>> responsesByTopicEntry : responsesByTopic.entrySet()) {
  92. Struct topicStruct = struct.instance(TOPICS_KEY_NAME);
  93. topicStruct.set(TOPIC_NAME, responsesByTopicEntry.getKey());
  94. List<Struct> partitionStructArray = new ArrayList<>();
  95. for (Map.Entry<Integer, Errors> responsesByPartitionEntry : responsesByTopicEntry.getValue().entrySet()) {
  96. Struct partitionStruct = topicStruct.instance(PARTITIONS_KEY_NAME);
  97. Errors response = responsesByPartitionEntry.getValue();
  98. partitionStruct.set(PARTITION_ID, responsesByPartitionEntry.getKey());
  99. partitionStruct.set(ERROR_CODE, response.code());
  100. partitionStructArray.add(partitionStruct);
  101. }
  102. topicStruct.set(PARTITIONS_KEY_NAME, partitionStructArray.toArray());
  103. topicStructArray.add(topicStruct);
  104. }
  105. struct.set(TOPICS_KEY_NAME, topicStructArray.toArray());
  106. return struct;
  107. }
  108. @Override
  109. public int throttleTimeMs() {
  110. return throttleTimeMs;
  111. }
  112. public Map<TopicPartition, Errors> responses() {
  113. return this.responses;
  114. }
  115. @Override
  116. public Map<Errors, Integer> errorCounts() {
  117. return errorCounts(responses.values());
  118. }
  119. public static AlterReplicaLogDirsResponse parse(ByteBuffer buffer, short version) {
  120. return new AlterReplicaLogDirsResponse(ApiKeys.ALTER_REPLICA_LOG_DIRS.responseSchema(version).read(buffer));
  121. }
  122. @Override
  123. public boolean shouldClientThrottle(short version) {
  124. return version >= 1;
  125. }
  126. }