-
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
1 parent
de4e48d
commit c869383
Showing
12 changed files
with
500 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
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,66 @@ | ||
/* | ||
* 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 org.apache.rocketmq.common.AsyncTask; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class AdminAsyncTaskManager { | ||
|
||
private static final Map<String, AsyncTask> ASYNC_TASK_MAP = new ConcurrentHashMap<>(); | ||
|
||
private static final Map<String, List<String>> TASK_NAME_TO_IDS_MAP = new ConcurrentHashMap<>(); | ||
|
||
public static String createTask(String taskName) { | ||
String taskId = UUID.randomUUID().toString(); | ||
ASYNC_TASK_MAP.put(taskId, new AsyncTask(taskName, taskId)); | ||
TASK_NAME_TO_IDS_MAP.computeIfAbsent(taskName, k -> new ArrayList<>()).add(taskId); | ||
return taskId; | ||
} | ||
|
||
public static List<String> getTaskIdsByName(String taskName) { | ||
return TASK_NAME_TO_IDS_MAP.getOrDefault(taskName, Collections.emptyList()); | ||
} | ||
|
||
public static AsyncTask getTaskStatus(String taskId) { | ||
return ASYNC_TASK_MAP.get(taskId); | ||
} | ||
|
||
public static void updateTaskStatus(String taskId, int status, String result) { | ||
AsyncTask task = ASYNC_TASK_MAP.get(taskId); | ||
if (task != null) { | ||
task.setStatus(status); | ||
task.setResult(result); | ||
} | ||
} | ||
|
||
public static void removeTask(String taskId) { | ||
AsyncTask task = ASYNC_TASK_MAP.remove(taskId); | ||
if (task != null) { | ||
TASK_NAME_TO_IDS_MAP.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
81 changes: 81 additions & 0 deletions
81
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,81 @@ | ||
/* | ||
* 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; | ||
|
||
public class AsyncTask { | ||
|
||
private String taskName; | ||
|
||
private String taskId; | ||
|
||
private int status; | ||
|
||
private Date createTime; | ||
|
||
private String result; | ||
|
||
public AsyncTask(String taskName, String taskId) { | ||
this.taskName = taskName; | ||
this.taskId = taskId; | ||
this.status = TaskStatus.INIT.getValue(); | ||
this.createTime = new Date(); | ||
this.result = null; | ||
} | ||
|
||
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; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
common/src/main/java/org/apache/rocketmq/common/TaskStatus.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,46 @@ | ||
/* | ||
* 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; | ||
|
||
public enum TaskStatus { | ||
|
||
INIT(0, "Initialized"), | ||
|
||
IN_PROGRESS(1, "In Progress"), | ||
|
||
ERROR(2, "Error"), | ||
|
||
SUCCESS(3, "Success"); | ||
|
||
private final int value; | ||
|
||
private final String desc; | ||
|
||
TaskStatus(int value, String desc) { | ||
this.value = value; | ||
this.desc = desc; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
public String getDesc() { | ||
return desc; | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
.../java/org/apache/rocketmq/remoting/protocol/header/CheckAsyncTaskStatusRequestHeader.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,46 @@ | ||
/* | ||
* 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.remoting.protocol.header; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.rocketmq.common.action.Action; | ||
import org.apache.rocketmq.common.action.RocketMQAction; | ||
import org.apache.rocketmq.remoting.CommandCustomHeader; | ||
import org.apache.rocketmq.remoting.exception.RemotingCommandException; | ||
import org.apache.rocketmq.remoting.protocol.RequestCode; | ||
|
||
@RocketMQAction(value = RequestCode.CHECK_ASYNC_TASK_STATUS, action = Action.GET) | ||
public class CheckAsyncTaskStatusRequestHeader implements CommandCustomHeader { | ||
|
||
private String taskName; | ||
|
||
@Override | ||
public void checkFields() throws RemotingCommandException { | ||
if (StringUtils.isBlank(taskName)) { | ||
throw new RemotingCommandException("taskName cannot be null or blank"); | ||
} | ||
} | ||
|
||
public String getTaskName() { | ||
return taskName; | ||
} | ||
|
||
public void setTaskName(String taskId) { | ||
this.taskName = taskId; | ||
} | ||
} |
Oops, something went wrong.