fix(rag): add self-healing recovery for corrupted Lucene index
This commit is contained in:
@@ -109,6 +109,24 @@ object RagService {
|
|||||||
@Volatile
|
@Volatile
|
||||||
private var _embeddingStore: LuceneEmbeddingStore? = null
|
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
|
val currentEmbeddingStore: LuceneEmbeddingStore
|
||||||
get() {
|
get() {
|
||||||
_embeddingStore?.let { return it }
|
_embeddingStore?.let { return it }
|
||||||
@@ -117,6 +135,7 @@ object RagService {
|
|||||||
val path = Paths.get("db/lucene_idx")
|
val path = Paths.get("db/lucene_idx")
|
||||||
java.nio.file.Files.createDirectories(path)
|
java.nio.file.Files.createDirectories(path)
|
||||||
checkEmbeddingModelConsistency()
|
checkEmbeddingModelConsistency()
|
||||||
|
try {
|
||||||
val directory = MMapDirectory(path)
|
val directory = MMapDirectory(path)
|
||||||
_directory = directory
|
_directory = directory
|
||||||
val store = LuceneEmbeddingStore.builder()
|
val store = LuceneEmbeddingStore.builder()
|
||||||
@@ -124,13 +143,26 @@ object RagService {
|
|||||||
.build()
|
.build()
|
||||||
_embeddingStore = store
|
_embeddingStore = store
|
||||||
store
|
store
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("🚨 [RagService] 루씬 저장소 로드 중 손상 감지 (${e.message}). 즉시 자동 복구를 진행합니다.")
|
||||||
|
resetLuceneStore()
|
||||||
|
_embeddingStore ?: error("루씬 인덱스 자동 복구 실패: ${e.message}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun active() {
|
fun active() {
|
||||||
if (UrlCacheManager.isInitialized()) return
|
if (UrlCacheManager.isInitialized()) return
|
||||||
|
try {
|
||||||
UrlCacheManager.initialize(currentEmbeddingStore, currentEmbeddingModel)
|
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 segment = TextSegment.from(chunk, metadata)
|
||||||
val embedding = currentEmbeddingModel.embed(segment).content()
|
val embedding = currentEmbeddingModel.embed(segment).content()
|
||||||
|
try {
|
||||||
currentEmbeddingStore.add(embedding, segment)
|
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()
|
.build()
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
if (isCorruptException(e)) {
|
||||||
|
println("🚨 [RagService] 최근 뉴스 검색 중 인덱스 손상 감지 (${e.message}). 즉시 자동 복구를 수행합니다.")
|
||||||
|
resetLuceneStore()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val hasHotNews = searchResult?.matches()?.any { match ->
|
val hasHotNews = searchResult?.matches()?.any { match ->
|
||||||
@@ -385,6 +435,7 @@ object RagService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun isUrlAlreadyIndexed(url: String): Boolean {
|
fun isUrlAlreadyIndexed(url: String): Boolean {
|
||||||
|
return try {
|
||||||
val filter = MetadataFilterBuilder.metadataKey("link").isEqualTo(url)
|
val filter = MetadataFilterBuilder.metadataKey("link").isEqualTo(url)
|
||||||
val searchRequest = EmbeddingSearchRequest.builder()
|
val searchRequest = EmbeddingSearchRequest.builder()
|
||||||
.filter(filter)
|
.filter(filter)
|
||||||
@@ -392,7 +443,14 @@ object RagService {
|
|||||||
.build()
|
.build()
|
||||||
|
|
||||||
val result = currentEmbeddingStore.search(searchRequest)
|
val result = currentEmbeddingStore.search(searchRequest)
|
||||||
return result.matches().isNotEmpty()
|
result.matches().isNotEmpty()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
if (isCorruptException(e)) {
|
||||||
|
println("🚨 [RagService] URL 중복 체크 중 인덱스 손상 감지 (${e.message}). 즉시 자동 복구를 수행합니다.")
|
||||||
|
resetLuceneStore()
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ollama 활성 여부에 따라 동적으로 11434 또는 LLM_PORT 반환
|
// Ollama 활성 여부에 따라 동적으로 11434 또는 LLM_PORT 반환
|
||||||
|
|||||||
Reference in New Issue
Block a user