Skip to content

Commit

Permalink
事务封禁日志写入实验
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jan 29, 2025
1 parent f72ec17 commit 502a2a0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 61 deletions.
33 changes: 20 additions & 13 deletions src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.gson.JsonObject;
import com.j256.ormlite.misc.TransactionManager;
import inet.ipaddr.IPAddress;
import inet.ipaddr.format.util.DualIPv4v6Tries;
import io.javalin.util.JavalinBindException;
Expand Down Expand Up @@ -539,13 +540,13 @@ public void banWave() {
try (TimeoutProtect protect = new TimeoutProtect(ExceptedTime.ADD_BAN_ENTRY.getTimeout(), (t) -> {
log.error(tlUI(Lang.TIMING_ADD_BANS));
})) {
var banlistClone = List.copyOf(BAN_LIST.keySet());
Callable<Object> callable = () -> {
var banlistClone = List.copyOf(BAN_LIST.keySet());

downloaderBanDetailMap.forEach((downloader, details) -> {
try {
List<Torrent> relaunch = Collections.synchronizedList(new ArrayList<>());
details.forEach(detail -> {
protect.getService().submit(() -> {
downloaderBanDetailMap.forEach((downloader, details) -> {
try {
List<Torrent> relaunch = Collections.synchronizedList(new ArrayList<>());
details.forEach(detail -> protect.getService().submit(() -> {
try {
if (detail.result().action() == PeerAction.BAN || detail.result().action() == PeerAction.BAN_FOR_DISCONNECT) {
long actualBanDuration = banDuration;
Expand Down Expand Up @@ -573,14 +574,20 @@ public void banWave() {
} catch (Exception e) {
log.error(tlUI(Lang.BAN_PEER_EXCEPTION), e);
}
});
});
}));

needRelaunched.put(downloader, relaunch);
} catch (Exception e) {
log.error(tlUI(Lang.UNABLE_COMPLETE_PEER_BAN_TASK), e);
}
});
needRelaunched.put(downloader, relaunch);
} catch (Exception e) {
log.error(tlUI(Lang.UNABLE_COMPLETE_PEER_BAN_TASK), e);
}
});
return null;
};
if (laboratory.isExperimentActivated(Experiments.TRANSACTION_BATCH_BAN_HISTORY_WRITE.getExperiment())) {
TransactionManager.callInTransaction(databaseManager.getDataSource(), callable);
} else {
callable.call();
}
}
banWaveWatchDog.setLastOperation("Apply banlist");
// 如果需要,则应用更改封禁列表到下载器
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/ghostchu/peerbanhelper/lab/Experiments.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import java.util.List;

public enum Experiments {
IPFS(new Experiment("ipfs", List.of(0), new TranslationComponent(Lang.LAB_EXPERIMENT_IPFS_TITLE), new TranslationComponent(Lang.LAB_EXPERIMENT_IPFS_DESCRIPTION))),
IPFS(new Experiment("ipfs", List.of(), new TranslationComponent(Lang.LAB_EXPERIMENT_IPFS_TITLE), new TranslationComponent(Lang.LAB_EXPERIMENT_IPFS_DESCRIPTION))),
DNSJAVA(new Experiment("dnsjava", List.of(0, 1, 2), new TranslationComponent(Lang.LAB_EXPERIMENT_DNSJAVA_TITLE), new TranslationComponent(Lang.LAB_EXPERIMENT_DNSJAVA_DESCRIPTION))),
SQLITE_VACUUM(new Experiment("sqlite_vacuum", List.of(0, 1, 3, 5), new TranslationComponent(Lang.LAB_EXPERIMENT_SQLITE_VACUUM_TITLE), new TranslationComponent(Lang.LAB_EXPERIMENT_SQLITE_VACUUM_DESCRIPTION))),
FILL_MISSING_DATA_IN_TRAFFIC_SUMMARY(new Experiment("fill_missing_data_in_traffic_summary", Collections.emptyList(), new TranslationComponent(Lang.LAB_EXPERIMENT_FILL_MISSING_DATA_IN_TRAFFIC_SUMMARY_TITLE), new TranslationComponent(Lang.LAB_EXPERIMENT_FILL_MISSING_DATA_IN_TRAFFIC_SUMMARY_DESCRIPTION))),
TRANSACTION_BATCH_BAN_HISTORY_WRITE(new Experiment("transaction_batch_ban_history_write", List.of(2, 5), new TranslationComponent(Lang.LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE), new TranslationComponent(Lang.LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION))),
;

private final Experiment experiment;
Expand All @@ -22,4 +23,4 @@ public enum Experiments {
public Experiment getExperiment() {
return experiment;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,55 +88,52 @@ public void recordCheck() {

@Override
public void recordPeerBan(PeerAddress address, BanMetadata metadata) {
if(metadata.isBanForDisconnect()){
if (metadata.isBanForDisconnect()) {
return;
}
inMemory.recordPeerBan(address, metadata);
// 将数据库 IO 移动到虚拟线程上
Thread.ofVirtual().start(() -> {
try {
TorrentEntity torrentEntity = torrentDao.createIfNotExists(new TorrentEntity(
null,
metadata.getTorrent().getHash(),
metadata.getTorrent().getName(),
metadata.getTorrent().getSize(),
metadata.getTorrent().isPrivateTorrent()
));
ModuleEntity module = moduleDao.createIfNotExists(new ModuleEntity(
null,
metadata.getContext()
));
RuleEntity rule = ruleDao.createIfNotExists(new RuleEntity(
null,
module,
metadata.getRule()
));
historyDao.create(new HistoryEntity(
null,
new Timestamp(metadata.getBanAt()),
new Timestamp(metadata.getUnbanAt()),
address.getAddress().toNormalizedString(),
address.getPort(),
metadata.getPeer().getId(),
metadata.getPeer().getClientName(),
metadata.getPeer().getUploaded(),
metadata.getPeer().getDownloaded(),
metadata.getPeer().getProgress(),
torrentEntity,
rule,
metadata.getDescription(),
metadata.getPeer().getFlags() == null ? null : metadata.getPeer().getFlags().toString(),
metadata.getDownloader()
));
} catch (SQLException e) {
log.error(tlUI(Lang.DATABASE_SAVE_BUFFER_FAILED), e);
}
});
try {
TorrentEntity torrentEntity = torrentDao.createIfNotExists(new TorrentEntity(
null,
metadata.getTorrent().getHash(),
metadata.getTorrent().getName(),
metadata.getTorrent().getSize(),
metadata.getTorrent().isPrivateTorrent()
));
ModuleEntity module = moduleDao.createIfNotExists(new ModuleEntity(
null,
metadata.getContext()
));
RuleEntity rule = ruleDao.createIfNotExists(new RuleEntity(
null,
module,
metadata.getRule()
));
historyDao.create(new HistoryEntity(
null,
new Timestamp(metadata.getBanAt()),
new Timestamp(metadata.getUnbanAt()),
address.getAddress().toNormalizedString(),
address.getPort(),
metadata.getPeer().getId(),
metadata.getPeer().getClientName(),
metadata.getPeer().getUploaded(),
metadata.getPeer().getDownloaded(),
metadata.getPeer().getProgress(),
torrentEntity,
rule,
metadata.getDescription(),
metadata.getPeer().getFlags() == null ? null : metadata.getPeer().getFlags().toString(),
metadata.getDownloader()
));
} catch (SQLException e) {
log.error(tlUI(Lang.DATABASE_SAVE_BUFFER_FAILED), e);
}
}

@Override
public void recordPeerUnban(PeerAddress address, BanMetadata metadata) {
if(metadata.isBanForDisconnect()){
if (metadata.isBanForDisconnect()) {
return;
}
inMemory.recordPeerUnban(address, metadata);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ghostchu/peerbanhelper/text/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public enum Lang {
LAB_EXPERIMENT_FILL_MISSING_DATA_IN_TRAFFIC_SUMMARY_DESCRIPTION,
GUI_TABBED_WEBUI,
JCEF_BROWSER_UNSUPPORTED_PLATFORM,
JCEF_BROWSER_UNSUPPORTED_EXCEPTION, JCEF_DOWNLOAD_TITLE, JCEF_DOWNLOAD_DESCRIPTION, GUI_COMMON_CANCEL, JCEF_DOWNLOAD_UNZIP_DESCRIPTION, IPDB_DOWNLOAD_TITLE, IPDB_DOWNLOAD_DESCRIPTION, PBH_PLUS_THANKS_FOR_DONATION_GUI_TITLE, LAB_EXPERIMENT_MAC_FLATLAF_THEME_TITLE, LAB_EXPERIMENT_MAC_FLATLAF_THEME_DESCRIPTION, GUI_TITLE_DEBUG, DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_TITLE, DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_DESCRIPTION, ABOUT_VIEW_CREDIT, INCOMPATIBLE_BITNESS_TITLE, INCOMPATIBLE_BITNESS_DESCRIPTION, TITLE_INCOMPATIBLE_PLATFORM, INCOMPATIBLE_BITNESS_LOG;
JCEF_BROWSER_UNSUPPORTED_EXCEPTION, JCEF_DOWNLOAD_TITLE, JCEF_DOWNLOAD_DESCRIPTION, GUI_COMMON_CANCEL, JCEF_DOWNLOAD_UNZIP_DESCRIPTION, IPDB_DOWNLOAD_TITLE, IPDB_DOWNLOAD_DESCRIPTION, PBH_PLUS_THANKS_FOR_DONATION_GUI_TITLE, LAB_EXPERIMENT_MAC_FLATLAF_THEME_TITLE, LAB_EXPERIMENT_MAC_FLATLAF_THEME_DESCRIPTION, GUI_TITLE_DEBUG, DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_TITLE, DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_DESCRIPTION, ABOUT_VIEW_CREDIT, INCOMPATIBLE_BITNESS_TITLE, INCOMPATIBLE_BITNESS_DESCRIPTION, TITLE_INCOMPATIBLE_PLATFORM, INCOMPATIBLE_BITNESS_LOG, LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE, LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION;

public String getKey() {
return name();
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/lang/en-us/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,6 @@ DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_DESCRIPTION: |
This is usually caused by the downloader running in a Docker environment and using the `bridge` network mode. You must switch to the `host` network mode to avoid user-space forwarding rewriting the IP address information.
Note: You should correctly fix the IP issue. Simply removing internal network IP addresses from the ignore list will cause PeerBanHelper to incorrectly ban all inbound connections.
For assistance, please join the PeerBanHelper community. **This prompt will only appear once per downloader.**
For assistance, please join the PeerBanHelper community. **This prompt will only appear once per downloader.**
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE: "Enable transaction batch write ban history"
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION: "Use transactions to batch write to the database when recording ban history to improve performance and reduce random disk I/O operations."
4 changes: 3 additions & 1 deletion src/main/resources/lang/messages_fallback.yml
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,6 @@ INCOMPATIBLE_PLATFORM_DESCRIPTION: "当前平台未经测试且不受支持,
INCOMPATIBLE_BITNESS_TITLE: "32 位平台不受支持"
INCOMPATIBLE_BITNESS_DESCRIPTION: "在 32 位操作系统/JVM 上运行 PeerBanHelper 未经测试且不受支持,可能出现兼容性或稳定性问题。请**不要**向 PBH-BTN 报告任何在此平台上出现的任何兼容性故障。回退兼容性可能在未来随时被移除。此提示信息仅显示一次。"
INCOMPATIBLE_BITNESS_LOG: "在 32 位操作系统/JVM 上运行 PeerBanHelper 未经测试且不受支持,可能出现兼容性或稳定性问题。请**不要**向 PBH-BTN 报告任何在此平台上出现的任何兼容性故障。回退兼容性可能在未来随时被移除。"
TITLE_INCOMPATIBLE_PLATFORM: "⚠不受支持的平台"
TITLE_INCOMPATIBLE_PLATFORM: "⚠不受支持的平台"
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE: "启用事务批量写入封禁历史记录"
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION: "在记录封禁历史记录时使用事务以批量写入数据库以提高性能和减少随机磁盘 I/O 操作。"
4 changes: 3 additions & 1 deletion src/main/resources/lang/zh-cn/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,6 @@ DOWNLOADER_DOCKER_INCORRECT_NETWORK_DETECTED_DESCRIPTION: |
请注意:你应该正确修复 IP 问题,仅将内部网络 IP 地址从忽略列表中移除会导致 PeerBanHelper 错误地封禁全部入站连接。
如需帮助,请加入 PeerBanHelper 社区寻求帮助。**此提示在每个下载器上仅会提示一次。**
ABOUT_VIEW_CREDIT: "开发人员与社区贡献者..."
ABOUT_VIEW_CREDIT: "开发人员与社区贡献者..."
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_TITLE: "启用事务批量写入封禁历史记录"
LAB_EXPERIMENT_TRANSACTION_BATCH_BAN_HISTORY_WRITE_DESCRIPTION: "在记录封禁历史记录时使用事务以批量写入数据库以提高性能和减少随机磁盘 I/O 操作。"

0 comments on commit 502a2a0

Please sign in to comment.