添加快照日志
This commit is contained in:
1 parent
547a04d12b
commit
b84675df3c
4 files changed
+133
-63
No files matched your search
@@ -80,14 +80,18 @@ public class RentalChain {
|
||||
json.writeValueAsString(body)))
|
||||
.build();
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
// 不记录完整URL、认证头或响应原文;状态码足以区分限流、路径错误和服务故障。
|
||||
log.warn("租赁RPC返回非成功状态 path={} httpStatus={}", path, response.code());
|
||||
}
|
||||
boolean fullBlock = path.endsWith("/getnowblock") || path.endsWith("/getblockbynum")
|
||||
|| path.endsWith("/gettransactioninfobyblocknum");
|
||||
return json.readTree(RentalHttp.body(response,
|
||||
(fullBlock ? 16 : 1) * 1024 * 1024));
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error(ex.getMessage(), ex);
|
||||
throw new IllegalStateException("链上查询失败,保留原业务状态 " + base + path);
|
||||
log.error("租赁RPC查询失败 path={} diagnostic={}", path, RentalDiagnostics.describe(ex));
|
||||
throw new IllegalStateException("链上查询失败,保留原业务状态", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,67 +122,100 @@ public class RentalChain {
|
||||
}
|
||||
|
||||
public Map<String, Object> snapshot(String network, String address) {
|
||||
JsonNode beforeBlock = call(network, "/wallet/getnowblock", map());
|
||||
long before = block(beforeBlock);
|
||||
JsonNode resources =
|
||||
call(
|
||||
network,
|
||||
"/wallet/getaccountresource",
|
||||
map("address", address, "visible", true));
|
||||
long energy = delegatable(network, address, 1);
|
||||
long bandwidth = delegatable(network, address, 0);
|
||||
JsonNode afterBlock = call(network, "/wallet/getnowblock", map());
|
||||
long after = block(afterBlock);
|
||||
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()),
|
||||
"快照跨区块或本金查询不完整,请重试");
|
||||
Map<String, Object> result =
|
||||
map(
|
||||
"snapshot_block_height",
|
||||
after,
|
||||
"energy_delegatable_sun",
|
||||
energy,
|
||||
"bandwidth_delegatable_sun",
|
||||
bandwidth,
|
||||
"energy_available",
|
||||
Math.max(
|
||||
0,
|
||||
resources.path("EnergyLimit").asLong()
|
||||
- resources.path("EnergyUsed").asLong()),
|
||||
"bandwidth_available",
|
||||
Math.max(
|
||||
0,
|
||||
resources.path("NetLimit").asLong()
|
||||
- resources.path("NetUsed").asLong()),
|
||||
"total_energy_limit",
|
||||
resources.path("TotalEnergyLimit").asLong(),
|
||||
"total_energy_weight",
|
||||
resources.path("TotalEnergyWeight").asLong(),
|
||||
"total_bandwidth_limit",
|
||||
resources.path("TotalNetLimit").asLong(),
|
||||
"total_bandwidth_weight",
|
||||
resources.path("TotalNetWeight").asLong(),
|
||||
"snapshot_at",
|
||||
now(),
|
||||
"snapshot_expires_at",
|
||||
after(15));
|
||||
require(
|
||||
number(result, "total_energy_limit") > 0
|
||||
&& number(result, "total_energy_weight") > 0
|
||||
&& number(result, "total_bandwidth_limit") > 0
|
||||
&& number(result, "total_bandwidth_weight") > 0,
|
||||
"全网参数不完整");
|
||||
if (embeddedHistory && historyService != null) {
|
||||
// 复用本次夹读,不再为每个订单额外请求全网资源参数;不读取旧rent_wallet快照。
|
||||
try {
|
||||
historyService.getObject().offerParameters(network, beforeBlock, afterBlock, resources);
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("钱包全局参数采样未入库 network={},独立采样器将继续重试", network);
|
||||
long started = System.nanoTime();
|
||||
String stage = "BEFORE_BLOCK";
|
||||
try {
|
||||
JsonNode beforeBlock = call(network, "/wallet/getnowblock", map());
|
||||
long before = block(beforeBlock);
|
||||
stage = "ACCOUNT_RESOURCE";
|
||||
JsonNode resources =
|
||||
call(
|
||||
network,
|
||||
"/wallet/getaccountresource",
|
||||
map("address", address, "visible", true));
|
||||
stage = "ENERGY_DELEGATABLE";
|
||||
long energy = delegatable(network, address, 1);
|
||||
stage = "BANDWIDTH_DELEGATABLE";
|
||||
long bandwidth = delegatable(network, address, 0);
|
||||
stage = "AFTER_BLOCK";
|
||||
JsonNode afterBlock = call(network, "/wallet/getnowblock", map());
|
||||
long after = block(afterBlock);
|
||||
stage = "BLOCK_CONSISTENCY";
|
||||
log.debug("租赁快照区块夹读 network={} address={} beforeHeight={} afterHeight={} sameBlockId={} elapsedMs={}",
|
||||
network, address, before, after,
|
||||
beforeBlock.path("blockID").asText().equalsIgnoreCase(afterBlock.path("blockID").asText()),
|
||||
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started));
|
||||
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={}"
|
||||
+ " reason=BLOCK_CHANGED_OR_INVALID;本次不更新,禁止混用不同区块资源",
|
||||
network, address, before, after);
|
||||
}
|
||||
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()),
|
||||
"快照跨区块或本金查询不完整,请重试");
|
||||
Map<String, Object> result =
|
||||
map(
|
||||
"snapshot_block_height",
|
||||
after,
|
||||
"energy_delegatable_sun",
|
||||
energy,
|
||||
"bandwidth_delegatable_sun",
|
||||
bandwidth,
|
||||
"energy_available",
|
||||
Math.max(
|
||||
0,
|
||||
resources.path("EnergyLimit").asLong()
|
||||
- resources.path("EnergyUsed").asLong()),
|
||||
"bandwidth_available",
|
||||
Math.max(
|
||||
0,
|
||||
resources.path("NetLimit").asLong()
|
||||
- resources.path("NetUsed").asLong()),
|
||||
"total_energy_limit",
|
||||
resources.path("TotalEnergyLimit").asLong(),
|
||||
"total_energy_weight",
|
||||
resources.path("TotalEnergyWeight").asLong(),
|
||||
"total_bandwidth_limit",
|
||||
resources.path("TotalNetLimit").asLong(),
|
||||
"total_bandwidth_weight",
|
||||
resources.path("TotalNetWeight").asLong(),
|
||||
"snapshot_at",
|
||||
now(),
|
||||
"snapshot_expires_at",
|
||||
after(15));
|
||||
stage = "GLOBAL_PARAMETERS";
|
||||
if (number(result, "total_energy_limit") <= 0 || number(result, "total_energy_weight") <= 0
|
||||
|| number(result, "total_bandwidth_limit") <= 0 || number(result, "total_bandwidth_weight") <= 0) {
|
||||
log.warn("租赁快照全网参数不完整 network={} address={} energyLimit={} energyWeight={}"
|
||||
+ " bandwidthLimit={} bandwidthWeight={}", network, address,
|
||||
result.get("total_energy_limit"), result.get("total_energy_weight"),
|
||||
result.get("total_bandwidth_limit"), result.get("total_bandwidth_weight"));
|
||||
}
|
||||
require(
|
||||
number(result, "total_energy_limit") > 0
|
||||
&& number(result, "total_energy_weight") > 0
|
||||
&& number(result, "total_bandwidth_limit") > 0
|
||||
&& number(result, "total_bandwidth_weight") > 0,
|
||||
"全网参数不完整");
|
||||
if (embeddedHistory && historyService != null) {
|
||||
// 复用本次夹读,不再为每个订单额外请求全网资源参数;不读取旧rent_wallet快照。
|
||||
try {
|
||||
historyService.getObject().offerParameters(network, beforeBlock, afterBlock, resources);
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("钱包全局参数采样未入库 network={},独立采样器将继续重试", network);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("租赁链上快照采集失败 network={} address={} stage={} elapsedMs={} diagnostic={}",
|
||||
network, address, stage, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started),
|
||||
RentalDiagnostics.describe(ex));
|
||||
throw ex;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** protobuf省略零字段的空对象表示0;显式错误和非整数绝不能当作0。 */
|
||||
|
||||
@@ -50,6 +50,16 @@ public class RentalTasks {
|
||||
tx(
|
||||
() -> {
|
||||
Map<String, Object> current = store.get("wallet", id, true);
|
||||
if (number(previous, "version") != number(current, "version")
|
||||
|| number(snapshot, "snapshot_block_height") < number(current, "snapshot_min_block_height")
|
||||
|| (current.get("snapshot_block_height") != null
|
||||
&& number(snapshot, "snapshot_block_height") < number(current, "snapshot_block_height"))) {
|
||||
log.warn("租赁钱包快照写入被拒绝 walletId={} network={} reason=STALE_OR_CONCURRENT"
|
||||
+ " previousVersion={} currentVersion={} sampledHeight={} storedHeight={} minHeight={}",
|
||||
id, current.get("network"), previous.get("version"), current.get("version"),
|
||||
snapshot.get("snapshot_block_height"), current.get("snapshot_block_height"),
|
||||
current.get("snapshot_min_block_height"));
|
||||
}
|
||||
require(
|
||||
number(previous, "version") == number(current, "version")
|
||||
&& number(snapshot, "snapshot_block_height")
|
||||
|
||||
@@ -43,6 +43,7 @@ public class RentalWorkers {
|
||||
"任务推送间隔必须为500到60000毫秒");
|
||||
LOG.info("租赁交付调度配置 dispatchIntervalMs={} pushIntervalMs={}",
|
||||
dispatchIntervalMs, pushIntervalMs);
|
||||
LOG.info("租赁快照定时扫描已启用 intervalMs=15000 batchSize=20,刷新ACTIVE且快照缺失或过期的钱包");
|
||||
}
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
@@ -158,28 +159,46 @@ public class RentalWorkers {
|
||||
});
|
||||
}
|
||||
|
||||
/** 仅刷新ACTIVE且缺失/过期的快照;失败不推进快照时间,避免将旧数据伪装成新数据。 */
|
||||
@Scheduled(fixedDelay = 15000)
|
||||
public void snapshots() {
|
||||
// 按主键轮转,不能让最早20个故障RPC钱包永久阻塞后续供应钱包。
|
||||
List<Map<String, Object>> rows =
|
||||
store.rows(
|
||||
"SELECT id FROM rent_wallet WHERE id>? AND status='ACTIVE' AND"
|
||||
"SELECT id,node_id,network,snapshot_at,snapshot_expires_at FROM rent_wallet WHERE id>? AND status='ACTIVE' AND"
|
||||
+ " (snapshot_expires_at IS NULL OR snapshot_expires_at<=?) ORDER BY id"
|
||||
+ " LIMIT 20",
|
||||
walletCursor,
|
||||
now());
|
||||
if (rows.isEmpty()) {
|
||||
LOG.debug("租赁快照扫描无候选 cursor={},下一轮从头扫描;仅处理ACTIVE且快照过期的钱包", walletCursor);
|
||||
walletCursor = 0;
|
||||
return;
|
||||
}
|
||||
long batchStarted = System.nanoTime();
|
||||
int succeeded = 0;
|
||||
for (Map<String, Object> row : rows) {
|
||||
walletCursor = number(row, "id");
|
||||
long started = System.nanoTime();
|
||||
try {
|
||||
tasks.refresh(walletCursor);
|
||||
succeeded++;
|
||||
LOG.info("租赁钱包快照刷新成功 walletId={} nodeId={} network={} previousSnapshotAt={} elapsedMs={}",
|
||||
walletCursor, row.get("node_id"), row.get("network"), row.get("snapshot_at"),
|
||||
java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started));
|
||||
} catch (Exception ex) {
|
||||
LOG.warn("租赁钱包快照暂不可用,walletId={}", walletCursor);
|
||||
// 异常原文可能包含RPC地址、认证参数或SQL值,只输出安全因果链和代码位置。
|
||||
LOG.warn("租赁钱包快照暂不可用 walletId={} nodeId={} network={} previousSnapshotAt={}"
|
||||
+ " previousExpiresAt={} elapsedMs={} diagnostic={};保留旧快照及原时间,后续轮次重试",
|
||||
walletCursor, row.get("node_id"), row.get("network"), row.get("snapshot_at"),
|
||||
row.get("snapshot_expires_at"),
|
||||
java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started),
|
||||
RentalDiagnostics.describe(ex));
|
||||
}
|
||||
}
|
||||
LOG.info("租赁快照扫描完成 candidates={} succeeded={} failed={} cursor={} elapsedMs={}",
|
||||
rows.size(), succeeded, rows.size() - succeeded, walletCursor,
|
||||
java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - batchStarted));
|
||||
}
|
||||
|
||||
private void each(String sql, Consumer<Long> action) {
|
||||
|
||||
Reference in new issue
Block a user