快照修复 跨区块时重新采集整套数据,最多尝试 3 次。
每次重试重新查询全部数据,不能拼接不同尝试的结果。 记录每次耗时、前后高度及最终失败原因。 全部失败后保留旧快照,等待下一轮。 如果持续跨区块,则检查 RPC 延迟;仅靠重试无法解决长期慢响应。
This commit is contained in:
1 parent
b84675df3c
commit
41190a25ae
4 files changed
+197
-8
No files matched your search
@@ -22,6 +22,17 @@ import static com.ruoyi.system.rental.RentalValues.*;
|
||||
@Slf4j
|
||||
@Service
|
||||
public class RentalChain {
|
||||
/** 总尝试次数(含首次),仅区块夹读冲突触发整套重采集。 */
|
||||
private static final int SNAPSHOT_MAX_ATTEMPTS = 3;
|
||||
|
||||
/** 内部类型区分可重试的区块变化与RPC、参数及本金错误,禁止按异常文案判断。 */
|
||||
private static final class SnapshotBlockChangedException extends IllegalArgumentException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private SnapshotBlockChangedException() {
|
||||
super("快照区块不一致,整套重采集仍未通过");
|
||||
}
|
||||
}
|
||||
@Value("${rental.rpc.nile:https://nile.trongrid.io}")
|
||||
private String nile;
|
||||
|
||||
@@ -122,6 +133,32 @@ public class RentalChain {
|
||||
}
|
||||
|
||||
public Map<String, Object> snapshot(String network, String address) {
|
||||
long started = System.nanoTime();
|
||||
for (int attempt = 1; attempt <= SNAPSHOT_MAX_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
// 每次调用重新查询前区块、资源、两类本金、后区块,不复用失败尝试的数据。
|
||||
Map<String, Object> result = snapshotOnce(network, address, attempt);
|
||||
if (attempt > 1) {
|
||||
log.info("租赁快照重采集成功 network={} address={} attempt={} maxAttempts={}"
|
||||
+ " blockHeight={} totalElapsedMs={}",
|
||||
network, address, attempt, SNAPSHOT_MAX_ATTEMPTS, result.get("snapshot_block_height"),
|
||||
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started));
|
||||
}
|
||||
return result;
|
||||
} catch (SnapshotBlockChangedException ex) {
|
||||
if (attempt == SNAPSHOT_MAX_ATTEMPTS) {
|
||||
log.warn("租赁快照重采集耗尽 network={} address={} attempts={} totalElapsedMs={};保留旧快照,等待下一轮扫描",
|
||||
network, address, attempt, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started));
|
||||
throw ex;
|
||||
}
|
||||
log.info("租赁快照跨区块重试 network={} address={} nextAttempt={} maxAttempts={}",
|
||||
network, address, attempt + 1, SNAPSHOT_MAX_ATTEMPTS);
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("快照重采集未产生结果");
|
||||
}
|
||||
|
||||
private Map<String, Object> snapshotOnce(String network, String address, int attempt) {
|
||||
long started = System.nanoTime();
|
||||
String stage = "BEFORE_BLOCK";
|
||||
try {
|
||||
@@ -148,15 +185,14 @@ public class RentalChain {
|
||||
if (before < 0 || before != after
|
||||
|| !beforeBlock.path("blockID").asText().matches("[0-9a-fA-F]{64}")
|
||||
|| !beforeBlock.path("blockID").asText().equalsIgnoreCase(afterBlock.path("blockID").asText())) {
|
||||
log.warn("租赁快照区块不一致 network={} address={} beforeHeight={} afterHeight={}"
|
||||
log.warn("租赁快照区块不一致 network={} address={} attempt={} maxAttempts={} beforeHeight={} afterHeight={}"
|
||||
+ " reason=BLOCK_CHANGED_OR_INVALID;本次不更新,禁止混用不同区块资源",
|
||||
network, address, before, after);
|
||||
network, address, attempt, SNAPSHOT_MAX_ATTEMPTS, before, after);
|
||||
throw new SnapshotBlockChangedException();
|
||||
}
|
||||
require(
|
||||
before >= 0 && before == after && energy >= 0 && bandwidth >= 0
|
||||
&& beforeBlock.path("blockID").asText().matches("[0-9a-fA-F]{64}")
|
||||
&& beforeBlock.path("blockID").asText().equalsIgnoreCase(afterBlock.path("blockID").asText()),
|
||||
"快照跨区块或本金查询不完整,请重试");
|
||||
energy >= 0 && bandwidth >= 0,
|
||||
"快照本金查询不完整");
|
||||
Map<String, Object> result =
|
||||
map(
|
||||
"snapshot_block_height",
|
||||
@@ -211,8 +247,8 @@ public class RentalChain {
|
||||
}
|
||||
return result;
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("租赁链上快照采集失败 network={} address={} stage={} elapsedMs={} diagnostic={}",
|
||||
network, address, stage, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started),
|
||||
log.warn("租赁链上快照采集失败 network={} address={} attempt={} stage={} elapsedMs={} diagnostic={}",
|
||||
network, address, attempt, stage, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started),
|
||||
RentalDiagnostics.describe(ex));
|
||||
throw ex;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user