Skip to content
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

<perf>(xatransaction): Improve the xaTransaction list interface #596

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void setPaths(Set<String> paths) {

public static class ListXATransactionsRequest {
private int size;
private String path;
private Map<String, Long> offsets = Collections.synchronizedMap(new HashMap<>());

public int getSize() {
Expand All @@ -75,6 +76,14 @@ public Map<String, Long> getOffsets() {
public void setOffsets(Map<String, Long> offsets) {
this.offsets = offsets;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}
}

@Override
Expand Down Expand Up @@ -229,6 +238,8 @@ public void handle(
host.getAccountManager().getAdminUA(),
xaRequest.getData().getOffsets(),
xaRequest.getData().getSize(),
xaRequest.getData().getPath(),
xaRequest.getVersion(),
(exception, xaTransactionListResponse) -> {
if (logger.isDebugEnabled()) {
logger.debug(
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/webank/wecross/restserver/Versions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public class Versions {

private static final String versionList[] = {"0.1", "0.2", "1"};
private static final String versionList[] = {"0.1", "0.2", "1", "1.4"};

public static String currentVersion = "1";
public static String currentVersion = "1.4";
kyonRay marked this conversation as resolved.
Show resolved Hide resolved

public static Boolean checkVersion(String version) {
for (String v : versionList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,44 @@ public void asyncGetXATransaction(
}
}

private ListXAReduceCallback getListXACallback(
int count, Map<String, Long> offsets, int size, ListXATransactionsCallback callback) {
List<XAResponse.ChainErrorMessage> errors =
Collections.synchronizedList(new LinkedList<>());
Map<String, Long> nextOffsets = new ConcurrentHashMap<>(offsets);

return (chain, listXAResponse) -> {

// first time to list, reset offsets
if (nextOffsets.get(chain) == -1) {
nextOffsets.put(chain, listXAResponse.getTotal() - 1);
}

XATransactionListResponse response = new XATransactionListResponse();
if (Objects.nonNull(listXAResponse.getChainErrorMessage())) {
errors.add(listXAResponse.getChainErrorMessage());
if (!errors.isEmpty()) {
XAResponse xaResponse = new XAResponse();
xaResponse.setStatus(-1);
xaResponse.setChainErrorMessages(errors);
response.setXaResponse(xaResponse);
}
} else {
response.setXaList(listXAResponse.getXaTransactions());
// update offsets
Long nextOffset =
nextOffsets.get(chain) - listXAResponse.getXaTransactions().size();
nextOffsets.put(chain, nextOffset);
response.setNextOffsets(nextOffsets);
if (nextOffsets.get(chain) == -1) {
response.setFinished(true);
}
response.recoverUsername(accountManager);
}
callback.onResponse(null, response);
};
}

public interface ListXATransactionsCallback {
void onResponse(WeCrossException e, XATransactionListResponse xaTransactionListResponse);
}
Expand Down Expand Up @@ -728,6 +766,8 @@ public void asyncListXATransactions(
UniversalAccount ua,
Map<String, Long> offsets,
int size,
String chainPath,
String version,
ListXATransactionsCallback callback) {

try {
Expand All @@ -742,7 +782,12 @@ public void asyncListXATransactions(

XATransactionListResponse response = new XATransactionListResponse();

List<Path> chainPaths = setToList(zoneManager.getAllChainsInfo(false).keySet());
List<Path> chainPaths = new ArrayList<>();
if (Objects.isNull(chainPath) || chainPath.isEmpty()) {
chainPaths = setToList(zoneManager.getAllChainsInfo(false).keySet());
} else {
chainPaths.add(Path.decode(chainPath));
}

int chainNum = chainPaths.size();
if (chainNum == 0) {
Expand All @@ -761,8 +806,15 @@ public void asyncListXATransactions(
}
}

ListXAReduceCallback reduceCallback =
getListXAReduceCallback(offsets.size(), offsets, size, callback);
ListXAReduceCallback reduceCallback = null;
kyonRay marked this conversation as resolved.
Show resolved Hide resolved
if (version.equals("1.4")) {
// Remove sort operation callback
reduceCallback = getListXACallback(offsets.size(), offsets, size, callback);
} else {
// has sort operation callback
reduceCallback = getListXAReduceCallback(offsets.size(), offsets, size, callback);
}

for (String chain : offsets.keySet()) {
if (!requireIgnore || offsets.get(chain) != -1L) {
asyncListXATransactions(
Expand Down
Loading