fix(rag): add self-healing recovery for corrupted Lucene index

This commit is contained in:
2026-11-17 17:02:56 +09:00
parent 03d5eaeed1
commit 1ece130f10
+74 -16
View File
@@ -109,6 +109,24 @@ object RagService {
@Volatile
private var _embeddingStore: LuceneEmbeddingStore? = null
fun isCorruptException(e: Throwable): Boolean {
var current: Throwable? = e
while (current != null) {
val name = current.javaClass.name
val msg = current.message ?: ""
if (name.contains("CorruptIndexException") ||
name.contains("NoSuchFileException") ||
name.contains("IndexNotFoundException") ||
msg.contains("CorruptIndexException") ||
msg.contains("NoSuchFileException")
) {
return true
}
current = current.cause
}
return false
}
val currentEmbeddingStore: LuceneEmbeddingStore
get() {
_embeddingStore?.let { return it }
@@ -117,20 +135,34 @@ object RagService {
val path = Paths.get("db/lucene_idx")
java.nio.file.Files.createDirectories(path)
checkEmbeddingModelConsistency()
val directory = MMapDirectory(path)
_directory = directory
val store = LuceneEmbeddingStore.builder()
.directory(directory)
.build()
_embeddingStore = store
store
try {
val directory = MMapDirectory(path)
_directory = directory
val store = LuceneEmbeddingStore.builder()
.directory(directory)
.build()
_embeddingStore = store
store
} catch (e: Exception) {
println("🚨 [RagService] 루씬 저장소 로드 중 손상 감지 (${e.message}). 즉시 자동 복구를 진행합니다.")
resetLuceneStore()
_embeddingStore ?: error("루씬 인덱스 자동 복구 실패: ${e.message}")
}
}
}
}
fun active() {
if (UrlCacheManager.isInitialized()) return
UrlCacheManager.initialize(currentEmbeddingStore, currentEmbeddingModel)
try {
UrlCacheManager.initialize(currentEmbeddingStore, currentEmbeddingModel)
} catch (e: Exception) {
if (isCorruptException(e)) {
println("🚨 [RagService] 캐시 초기화 중 인덱스 손상 감지. 자동 복구를 수행합니다.")
resetLuceneStore()
UrlCacheManager.initialize(currentEmbeddingStore, currentEmbeddingModel)
}
}
}
/**
@@ -182,7 +214,21 @@ object RagService {
val segment = TextSegment.from(chunk, metadata)
val embedding = currentEmbeddingModel.embed(segment).content()
currentEmbeddingStore.add(embedding, segment)
try {
currentEmbeddingStore.add(embedding, segment)
} catch (e: Exception) {
if (isCorruptException(e)) {
println("🚨 [RagService] 뉴스 색인 중 인덱스 손상 감지 (${e.message}). 즉시 자동 복구를 수행합니다.")
resetLuceneStore()
try {
currentEmbeddingStore.add(embedding, segment)
} catch (e2: Exception) {
println("❌ [RagService] 인덱스 재색인 실패: ${e2.message}")
}
} else {
println("⚠️ [RagService] 색인 중 오류: ${e.message}")
}
}
}
}
}
@@ -358,6 +404,10 @@ object RagService {
.build()
)
} catch (e: Exception) {
if (isCorruptException(e)) {
println("🚨 [RagService] 최근 뉴스 검색 중 인덱스 손상 감지 (${e.message}). 즉시 자동 복구를 수행합니다.")
resetLuceneStore()
}
}
val hasHotNews = searchResult?.matches()?.any { match ->
@@ -385,14 +435,22 @@ object RagService {
}
fun isUrlAlreadyIndexed(url: String): Boolean {
val filter = MetadataFilterBuilder.metadataKey("link").isEqualTo(url)
val searchRequest = EmbeddingSearchRequest.builder()
.filter(filter)
.maxResults(1)
.build()
return try {
val filter = MetadataFilterBuilder.metadataKey("link").isEqualTo(url)
val searchRequest = EmbeddingSearchRequest.builder()
.filter(filter)
.maxResults(1)
.build()
val result = currentEmbeddingStore.search(searchRequest)
return result.matches().isNotEmpty()
val result = currentEmbeddingStore.search(searchRequest)
result.matches().isNotEmpty()
} catch (e: Exception) {
if (isCorruptException(e)) {
println("🚨 [RagService] URL 중복 체크 중 인덱스 손상 감지 (${e.message}). 즉시 자동 복구를 수행합니다.")
resetLuceneStore()
}
false
}
}
// Ollama 활성 여부에 따라 동적으로 11434 또는 LLM_PORT 반환