healchow commented on code in PR #4813:
URL: https://github.com/apache/inlong/pull/4813#discussion_r909481250


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/mq/DeletePulsarResourceTaskListener.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.inlong.manager.service.mq;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.inlong.manager.common.enums.ClusterType;
+import org.apache.inlong.manager.common.exceptions.WorkflowListenerException;
+import org.apache.inlong.manager.common.pojo.cluster.InlongClusterInfo;
+import org.apache.inlong.manager.common.pojo.cluster.pulsar.PulsarClusterInfo;
+import org.apache.inlong.manager.common.pojo.group.InlongGroupInfo;
+import org.apache.inlong.manager.common.pojo.group.pulsar.InlongPulsarInfo;
+import 
org.apache.inlong.manager.common.pojo.workflow.form.GroupResourceProcessForm;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.inlong.manager.service.cluster.InlongClusterService;
+import org.apache.inlong.manager.service.group.InlongGroupService;
+import org.apache.inlong.manager.service.mq.util.PulsarOperator;
+import org.apache.inlong.manager.service.mq.util.PulsarUtils;
+import org.apache.inlong.manager.workflow.WorkflowContext;
+import org.apache.inlong.manager.workflow.event.ListenerResult;
+import org.apache.inlong.manager.workflow.event.task.QueueOperateListener;
+import org.apache.inlong.manager.workflow.event.task.TaskEvent;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * Delete Pulsar tenant, namespace and topic after group is operated to delete
+ */
+@Slf4j
+@Component
+public class DeletePulsarResourceTaskListener implements QueueOperateListener {
+
+    @Autowired
+    private InlongGroupService groupService;
+    @Autowired
+    private InlongClusterService clusterService;
+    @Autowired
+    private PulsarOperator pulsarOperator;
+
+    @Override
+    public TaskEvent event() {
+        return TaskEvent.COMPLETE;
+    }
+
+    @Override
+    public ListenerResult listen(WorkflowContext context) throws Exception {
+        GroupResourceProcessForm form = (GroupResourceProcessForm) 
context.getProcessForm();
+        String groupId = form.getInlongGroupId();
+        log.info("begin to delete pulsar resource for groupId={}", groupId);
+        InlongGroupInfo groupInfo = groupService.get(groupId);
+        if (groupInfo == null) {
+            throw new WorkflowListenerException("inlong group or pulsar 
cluster not found for groupId=" + groupId);
+        }
+        try {
+            InlongPulsarInfo pulsarInfo = (InlongPulsarInfo) groupInfo;
+            InlongClusterInfo clusterInfo = 
clusterService.getOne(groupInfo.getInlongClusterTag(), null,
+                    ClusterType.CLS_PULSAR);
+            deletePulsarProcess(pulsarInfo, (PulsarClusterInfo) clusterInfo);
+        } catch (Exception e) {
+            log.error("delete pulsar resource error for groupId={}", groupId, 
e);
+            throw new WorkflowListenerException("delete pulsar resource error 
for groupId=" + groupId);
+        }
+
+        log.info("success to delete pulsar resource for groupId={}", groupId);
+        return ListenerResult.success();
+    }
+
+    /**
+     * Delete Pulsar namespace forcefully
+     */
+    private void deletePulsarProcess(InlongPulsarInfo pulsarInfo, 
PulsarClusterInfo pulsarCluster) throws Exception {
+        String groupId = pulsarInfo.getInlongGroupId();
+        log.info("begin to delete pulsar resource for groupId={} in 
cluster={}", groupId, pulsarCluster);
+
+        String namespace = pulsarInfo.getMqResource();
+        Preconditions.checkNotNull(namespace, "pulsar namespace cannot be 
empty for groupId=" + groupId);
+        try (PulsarAdmin pulsarAdmin = 
PulsarUtils.getPulsarAdmin(pulsarCluster)) {
+            String tenant = pulsarCluster.getTenant();
+            // create pulsar namespace
+            pulsarOperator.forceDeleteNamespace(pulsarAdmin, tenant, 
namespace);

Review Comment:
   The namespace may be used by another user, delete namespace is a dangerous 
operation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to