From 1ece130f100e0e625821eb665e81902bcf459b7b Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Tue, 17 Nov 2026 17:02:56 +0900 Subject: [PATCH] fix(rag): add self-healing recovery for corrupted Lucene index --- src/main/kotlin/network/RagService.kt | 90 ++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/network/RagService.kt b/src/main/kotlin/network/RagService.kt index 8729313..680dae5 100644 --- a/src/main/kotlin/network/RagService.kt +++ b/src/main/kotlin/network/RagService.kt @@ -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 λ°˜ν™˜