在 Elasticsearch 等使用 Netty 的項目中,特定 Linux 內核與 JDK 6 環境下,NIO Selector 發生「空輪詢」:selector.select() 在無事件時立即返回 0,導致 CPU 佔用率飆升至 100%。
Oracle Bug ID: 6403933,已在較新 JDK 版本修復。
設置系統屬性啟用 Netty 的繞過邏輯:
System.setProperty("org.elasticsearch.common.netty.epollBugWorkaround", "true");
核心邏輯:檢測到空輪詢時,取消當前 Selector 上的鍵,創建新 Selector 並重新註冊 Channel,避開損壞的 Selector。
if (selectionKey != null) {
selectionKey.cancel(); // 取消舊鍵
temporarySelector.selectNow(); // 清理 cancelled keys
}
// 隨後創建新 Selector 並重新註冊
此模式在 Netty 的 NioEventLoop 中自動處理,開發者無需手動干預。
selector.selectNow() 返回 0 次數),及時發現異常。