錯誤現象
[WARNING] rotating index 'news_delta': cur to old rename failed:
rename /dev/shm/sphinx/data/news_delta.spa to /dev/shm/sphinx/data/news_delta.old.spa failed: No such file or directory
根本原因
- 增量索引存放於
/dev/shm(tmpfs 記憶體檔案系統)。
- 機器重啟導致
/dev/shm 清空,原有索引檔案(.spa、.spd 等)遺失。
- 執行
indexer --rotate 時,Sphinx 試圖將當前索引重命名為 .old,再將 .new 重命名為正式索引。因原檔案不存在,rename 失敗,輪換中斷,搜尋服務仍使用舊索引(或無索引)。
解決步驟
# 1. 重建基礎索引檔案(不帶 --rotate)
/usr/local/coreseek/bin/indexer news_delta
/usr/local/coreseek/bin/indexer article_delta
# 2. 再次執行輪換(此時基礎檔案已存在)
/usr/local/coreseek/bin/indexer --rotate news_delta
/usr/local/coreseek/bin/indexer --rotate article_delta
# 輸出:rotating index 'news_delta': success
根治方案:開機自動重建腳本
# /usr/local/bin/rebuild_sphinx_deltas.sh
#!/bin/bash
echo "start: $(date '+%Y-%m-%d %H:%M:%S')"
cd /usr/local/coreseek/bin
./indexer news_delta
./indexer article_delta
./indexer --rotate news_delta
./indexer --rotate article_delta
echo "end: $(date '+%Y-%m-%d %H:%M:%S')"
# 加入 rc.local 或 systemd 服務
chmod +x /usr/local/bin/rebuild_sphinx_deltas.sh
echo "/usr/local/bin/rebuild_sphinx_deltas.sh" >> /etc/rc.local
# 或 systemd (推薦)
# /etc/systemd/system/sphinx-rebuild-deltas.service
[Unit]
Description=Rebuild Sphinx Delta Indexes on Boot
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/rebuild_sphinx_deltas.sh
StandardOutput=journal
[Install]
WantedBy=multi-user.target
# systemctl enable sphinx-rebuild-deltas
最佳實踐建議
- 避免將索引放在 /dev/shm:除非極度追求寫入速度且接受重啟重建成本。建議放在 SSD 持久化儲存(
/var/lib/sphinx)。
- 主索引 + 增量索引策略:主索引每日重建,增量索引每 5-15 分鐘更新。主索引佔用空間大,必須持久化。
- 監控輪換狀態:解析 indexer 輸出,失敗時告警(Zabbix/Promtail+Loki)。
- 備份索引:定期打包索引目錄異地備份,災難恢復快速恢復服務。