-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE #9097]Add new command to check async task status in broker.
- Loading branch information
Showing
13 changed files
with
1,054 additions
and
6 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
broker/src/main/java/org/apache/rocketmq/broker/AdminAsyncTaskManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* 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.rocketmq.broker; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.apache.rocketmq.common.AsyncTask; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import java.util.concurrent.TimeUnit; | ||
import org.apache.rocketmq.common.TaskStatus; | ||
|
||
public class AdminAsyncTaskManager { | ||
|
||
// taskId -> AsyncTask | ||
private final Cache<String, AsyncTask> asyncTaskCache; | ||
|
||
// taskName -> taskId | ||
private final ConcurrentHashMap<String, List<String>> taskNameToIdsMap; | ||
|
||
public AdminAsyncTaskManager() { | ||
this.asyncTaskCache = Caffeine.newBuilder() | ||
.expireAfterWrite(30, TimeUnit.MINUTES) | ||
.maximumSize(10000) | ||
.build(); | ||
|
||
this.taskNameToIdsMap = new ConcurrentHashMap<>(); | ||
} | ||
|
||
/** | ||
* Creates a new asynchronous task with a unique taskId. | ||
* | ||
* @param taskName The name of the task. | ||
* @param future The CompletableFuture representing the asynchronous task. | ||
* @return The generated taskId. | ||
*/ | ||
public String createTask(String taskName, CompletableFuture<?> future) { | ||
String taskId = UUID.randomUUID().toString(); | ||
AsyncTask task = new AsyncTask(taskName, taskId, future); | ||
|
||
asyncTaskCache.put(taskId, task); | ||
taskNameToIdsMap.computeIfAbsent(taskName, k -> new ArrayList<>()).add(taskId); | ||
|
||
future.whenComplete((result, throwable) -> { | ||
if (throwable != null) { | ||
task.setStatus(TaskStatus.ERROR.getValue()); | ||
task.setResult(throwable.getMessage()); | ||
} else { | ||
task.setStatus(TaskStatus.SUCCESS.getValue()); | ||
task.setResult(JSON.toJSONString(result)); | ||
} | ||
}); | ||
|
||
return taskId; | ||
} | ||
|
||
/** | ||
* Get all taskIds associated with a given task name. | ||
* | ||
* @param taskName The name of the task. | ||
* @return List of taskIds for the given task name. | ||
*/ | ||
public List<String> getTaskIdsByName(String taskName) { | ||
return taskNameToIdsMap.getOrDefault(taskName, Collections.emptyList()); | ||
} | ||
|
||
/** | ||
* Get the status of a specific task. | ||
* | ||
* @param taskId The unique identifier of the task. | ||
* @return The AsyncTask object, or null if not found. | ||
*/ | ||
public AsyncTask getTaskStatus(String taskId) { | ||
return asyncTaskCache.getIfPresent(taskId); | ||
} | ||
|
||
/** | ||
* Update the status and result of a specific task. | ||
* | ||
* @param taskId The unique identifier of the task. | ||
* @param status The new status of the task. | ||
* @param result The result of the task. | ||
*/ | ||
public void updateTaskStatus(String taskId, int status, String result) { | ||
AsyncTask task = asyncTaskCache.getIfPresent(taskId); | ||
if (task != null) { | ||
task.setStatus(status); | ||
task.setResult(result); | ||
asyncTaskCache.put(taskId, task); | ||
} | ||
} | ||
|
||
/** | ||
* Remove a specific task from the cache and mappings. | ||
* | ||
* @param taskId The unique identifier of the task. | ||
*/ | ||
public void removeTask(String taskId) { | ||
AsyncTask task = asyncTaskCache.getIfPresent(taskId); | ||
if (task != null) { | ||
asyncTaskCache.invalidate(taskId); | ||
taskNameToIdsMap.computeIfPresent(task.getTaskName(), (k, v) -> { | ||
v.remove(taskId); | ||
return v.isEmpty() ? null : v; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
common/src/main/java/org/apache/rocketmq/common/AsyncTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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.rocketmq.common; | ||
|
||
import java.util.Date; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class AsyncTask { | ||
|
||
private String taskName; | ||
|
||
private String taskId; | ||
|
||
private int status; | ||
|
||
private Date createTime; | ||
|
||
private String result; | ||
|
||
private final CompletableFuture<?> future; | ||
|
||
public AsyncTask(String taskName, String taskId, CompletableFuture<?> future) { | ||
this.taskName = taskName; | ||
this.taskId = taskId; | ||
this.status = TaskStatus.INIT.getValue(); | ||
this.createTime = new Date(); | ||
this.result = null; | ||
this.future = future; | ||
} | ||
|
||
public String getTaskName() { | ||
return taskName; | ||
} | ||
|
||
public void setTaskName(String taskName) { | ||
this.taskName = taskName; | ||
} | ||
|
||
public int getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(int status) { | ||
this.status = status; | ||
} | ||
|
||
public String getResult() { | ||
return result; | ||
} | ||
|
||
public void setResult(String result) { | ||
this.result = result; | ||
} | ||
|
||
public Date getCreateTime() { | ||
return createTime; | ||
} | ||
|
||
public void setCreateTime(Date createTime) { | ||
this.createTime = createTime; | ||
} | ||
|
||
public String getTaskId() { | ||
return taskId; | ||
} | ||
|
||
public void setTaskId(String taskId) { | ||
this.taskId = taskId; | ||
} | ||
|
||
public CompletableFuture<?> getFuture() { | ||
return future; | ||
} | ||
|
||
public static String getDescFromStatus(int status) { | ||
for (TaskStatus taskStatus : TaskStatus.values()) { | ||
if (taskStatus.getValue() == status) { | ||
return taskStatus.getDesc(); | ||
} | ||
} | ||
return "unknown"; | ||
} | ||
} |
Oops, something went wrong.