Skip to content

Commit

Permalink
Add delete topic api (opensource4you#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
g1geordie authored Jul 30, 2022
1 parent 814c5d8 commit c68fbdd
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/org/astraea/app/admin/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ default Map<String, Config> topics() {
/** @return the topic name and its configurations. */
Map<String, Config> topics(Set<String> topicNames);

/** delete topics by topic names */
void deleteTopics(Set<String> topicNames);

/** @return all partitions */
default Set<TopicPartition> partitions() {
return partitions(topicNames());
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/astraea/app/admin/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ public Set<String> topicNames() {
() -> admin.listTopics(new ListTopicsOptions().listInternal(true)).names().get());
}

@Override
public void deleteTopics(Set<String> topicNames) {
Utils.packException(() -> admin.deleteTopics(topicNames).all().get());
}

@Override
public Map<Integer, Config> brokers(Set<Integer> brokerIds) {
return Utils.packException(
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/org/astraea/app/web/TopicHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ public Response post(PostRequest request) {
return new TopicInfo(request.value(TOPIC_NAME_KEY), List.of(), Map.of());
}

@Override
public Response delete(String topic, Map<String, String> queries) {
admin.deleteTopics(Set.of(topic));
return Response.OK;
}

static class Topics implements Response {
final Collection<TopicInfo> topics;

Expand Down
23 changes: 23 additions & 0 deletions app/src/test/java/org/astraea/app/admin/AdminTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1134,4 +1134,27 @@ void testDeleteRecord() {
Assertions.assertEquals(0, offsets.get(TopicPartition.of(topicName, 2)).earliest());
}
}

@Test
void testDeleteTopic() {
var topicNames =
IntStream.range(0, 4).mapToObj(x -> Utils.randomString(10)).collect(Collectors.toList());

try (var admin = Admin.of(bootstrapServers())) {
topicNames.forEach(
x -> admin.creator().topic(x).numberOfPartitions(3).numberOfReplicas((short) 3).create());

admin.deleteTopics(Set.of(topicNames.get(0), topicNames.get(1)));
var latestTopicNames = admin.topicNames();
Assertions.assertFalse(latestTopicNames.contains(topicNames.get(0)));
Assertions.assertFalse(latestTopicNames.contains(topicNames.get(1)));
Assertions.assertTrue(latestTopicNames.contains(topicNames.get(2)));
Assertions.assertTrue(latestTopicNames.contains(topicNames.get(3)));

admin.deleteTopics(Set.of(topicNames.get(3)));
latestTopicNames = admin.topicNames();
Assertions.assertFalse(latestTopicNames.contains(topicNames.get(3)));
Assertions.assertTrue(latestTopicNames.contains(topicNames.get(2)));
}
}
}
24 changes: 24 additions & 0 deletions app/src/test/java/org/astraea/app/web/TopicHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.astraea.app.admin.Admin;
import org.astraea.app.common.Utils;
import org.astraea.app.service.RequireBrokerCluster;
Expand Down Expand Up @@ -193,4 +195,26 @@ void testRemainingConfigs() {
PostRequest.of(Map.of(TopicHandler.TOPIC_NAME_KEY, "abc", "key", "value")))
.size());
}

@Test
void testDeleteTopic() {
var topicNames =
IntStream.range(0, 3).mapToObj(x -> Utils.randomString(10)).collect(Collectors.toList());
try (Admin admin = Admin.of(bootstrapServers())) {
var handler = new TopicHandler(admin);
topicNames.forEach(
x -> admin.creator().topic(x).numberOfPartitions(3).numberOfReplicas((short) 3).create());

handler.delete(topicNames.get(0), Map.of());
var latestTopicNames = admin.topicNames();
Assertions.assertFalse(latestTopicNames.contains(topicNames.get(0)));
Assertions.assertTrue(latestTopicNames.contains(topicNames.get(1)));
Assertions.assertTrue(latestTopicNames.contains(topicNames.get(2)));

handler.delete(topicNames.get(2), Map.of());
latestTopicNames = admin.topicNames();
Assertions.assertFalse(latestTopicNames.contains(topicNames.get(2)));
Assertions.assertTrue(latestTopicNames.contains(topicNames.get(1)));
}
}
}
10 changes: 10 additions & 0 deletions docs/web_server/web_api_topics_chinese.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,13 @@ JSON Response 範例
}
}
```
## 刪除 topic
```shell
DELETE /topics/{topicName}
```

cURL 範例

```shell
curl -X DELETE "http://localhost:8001/topics/mytopic"
```

0 comments on commit c68fbdd

Please sign in to comment.