-
Notifications
You must be signed in to change notification settings - Fork 11.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ISSUE#9097] Add new command to check async task status in broker. #9162
base: develop
Are you sure you want to change the base?
Changes from 1 commit
bf4a370
2637c95
97bcdeb
690acb0
ce88393
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
.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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make sure [ add and remove object in ArrayList ] in map.compute to avoid concurrent errors There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
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; | ||
}); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
import java.util.concurrent.SynchronousQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
import org.apache.commons.collections.CollectionUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.rocketmq.acl.AccessValidator; | ||
|
@@ -56,6 +57,7 @@ | |
import org.apache.rocketmq.auth.authorization.exception.AuthorizationException; | ||
import org.apache.rocketmq.auth.authorization.model.Acl; | ||
import org.apache.rocketmq.auth.authorization.model.Resource; | ||
import org.apache.rocketmq.broker.AdminAsyncTaskManager; | ||
import org.apache.rocketmq.broker.BrokerController; | ||
import org.apache.rocketmq.broker.auth.converter.AclConverter; | ||
import org.apache.rocketmq.broker.auth.converter.UserConverter; | ||
|
@@ -72,6 +74,7 @@ | |
import org.apache.rocketmq.broker.plugin.BrokerAttachedPlugin; | ||
import org.apache.rocketmq.broker.subscription.SubscriptionGroupManager; | ||
import org.apache.rocketmq.broker.transaction.queue.TransactionalMessageUtil; | ||
import org.apache.rocketmq.common.AsyncTask; | ||
import org.apache.rocketmq.common.BoundaryType; | ||
import org.apache.rocketmq.common.BrokerConfig; | ||
import org.apache.rocketmq.common.CheckRocksdbCqWriteResult; | ||
|
@@ -149,6 +152,8 @@ | |
import org.apache.rocketmq.remoting.protocol.body.TopicList; | ||
import org.apache.rocketmq.remoting.protocol.body.UnlockBatchRequestBody; | ||
import org.apache.rocketmq.remoting.protocol.body.UserInfo; | ||
import org.apache.rocketmq.remoting.protocol.header.CheckAsyncTaskStatusRequestHeader; | ||
import org.apache.rocketmq.remoting.protocol.header.CheckAsyncTaskStatusResponseHeader; | ||
import org.apache.rocketmq.remoting.protocol.header.CheckRocksdbCqWriteProgressRequestHeader; | ||
import org.apache.rocketmq.remoting.protocol.header.CloneGroupOffsetRequestHeader; | ||
import org.apache.rocketmq.remoting.protocol.header.ConsumeMessageDirectlyResultRequestHeader; | ||
|
@@ -245,9 +250,11 @@ public class AdminBrokerProcessor implements NettyRequestProcessor { | |
protected final BrokerController brokerController; | ||
protected Set<String> configBlackList = new HashSet<>(); | ||
private final ExecutorService asyncExecuteWorker = new ThreadPoolExecutor(0, 4, 60L, TimeUnit.SECONDS, new SynchronousQueue<>()); | ||
private final AdminAsyncTaskManager asyncTaskManager; | ||
|
||
public AdminBrokerProcessor(final BrokerController brokerController) { | ||
this.brokerController = brokerController; | ||
this.asyncTaskManager = new AdminAsyncTaskManager(); | ||
initConfigBlackList(); | ||
} | ||
|
||
|
@@ -415,6 +422,8 @@ public RemotingCommand processRequest(ChannelHandlerContext ctx, | |
return this.listAcl(ctx, request); | ||
case RequestCode.POP_ROLLBACK: | ||
return this.transferPopToFsStore(ctx, request); | ||
case RequestCode.CHECK_ASYNC_TASK_STATUS: | ||
return this.checkAsyncTaskStatus(ctx, request); | ||
default: | ||
return getUnknownCmdResponse(ctx, request); | ||
} | ||
|
@@ -487,15 +496,16 @@ private RemotingCommand updateAndGetGroupForbidden(ChannelHandlerContext ctx, Re | |
private RemotingCommand checkRocksdbCqWriteProgress(ChannelHandlerContext ctx, RemotingCommand request) { | ||
CheckRocksdbCqWriteResult result = new CheckRocksdbCqWriteResult(); | ||
result.setCheckStatus(CheckRocksdbCqWriteResult.CheckStatus.CHECK_IN_PROGRESS.getValue()); | ||
Runnable runnable = () -> { | ||
|
||
CompletableFuture<CheckRocksdbCqWriteResult> future = CompletableFuture.supplyAsync(() -> { | ||
try { | ||
CheckRocksdbCqWriteResult checkResult = doCheckRocksdbCqWriteProgress(ctx, request); | ||
LOGGER.info("checkRocksdbCqWriteProgress result: {}", JSON.toJSONString(checkResult)); | ||
return doCheckRocksdbCqWriteProgress(ctx, request); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep log There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} catch (Exception e) { | ||
LOGGER.error("checkRocksdbCqWriteProgress error", e); | ||
throw new CompletionException(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
}; | ||
asyncExecuteWorker.submit(runnable); | ||
}, asyncExecuteWorker); | ||
|
||
asyncTaskManager.createTask("checkRocksdbCqWriteProgress", future); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return task id There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
RemotingCommand response = RemotingCommand.createResponseCommand(null); | ||
response.setCode(ResponseCode.SUCCESS); | ||
response.setBody(JSON.toJSONBytes(result)); | ||
|
@@ -3597,4 +3607,31 @@ private RemotingCommand transferPopToFsStore(ChannelHandlerContext ctx, Remoting | |
} | ||
return response; | ||
} | ||
|
||
private RemotingCommand checkAsyncTaskStatus(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { | ||
final CheckAsyncTaskStatusRequestHeader requestHeader = request.decodeCommandCustomHeader(CheckAsyncTaskStatusRequestHeader.class); | ||
List<String> taskIds = asyncTaskManager.getTaskIdsByName(requestHeader.getTaskName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider the order of the task There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return the status of the newly created task first? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
if (CollectionUtils.isEmpty(taskIds)) { | ||
throw new RemotingCommandException("taskName: " + requestHeader.getTaskName() + " not found"); | ||
} | ||
|
||
try { | ||
int maxResults = Math.min(requestHeader.getMaxLimit(), 200); | ||
Integer filterStatus = requestHeader.getTaskStatus(); | ||
|
||
List<AsyncTask> asyncTasks = taskIds.stream() | ||
.map(asyncTaskManager::getTaskStatus) | ||
.filter(task -> filterStatus == null || task.getStatus() == filterStatus) | ||
.limit(maxResults) | ||
.collect(Collectors.toList()); | ||
|
||
RemotingCommand response = RemotingCommand.createResponseCommand(CheckAsyncTaskStatusResponseHeader.class); | ||
response.setCode(ResponseCode.SUCCESS); | ||
response.setBody(JSON.toJSONBytes(asyncTasks)); | ||
return response; | ||
} catch (Exception e) { | ||
LOGGER.error("checkAsyncTaskStatus error", e); | ||
return RemotingCommand.createResponseCommand(ResponseCode.SYSTEM_ERROR, e.getMessage()); | ||
} | ||
} | ||
} |
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"; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if using map to manage task, maybe need a thread to clean up the expired task regularly. Using Caffeine LoadingCache may be better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done