Handle indexer init mapping errors (#9577)

* Retry initMapping for FulltextAdapter

Signed-off-by: Nikolay Marchuk <nikolay.marchuk@hardcoreeng.com>

* Recreate elastic index when mapping is not compatible

Signed-off-by: Nikolay Marchuk <nikolay.marchuk@hardcoreeng.com>

* Fix formatting

Signed-off-by: Nikolay Marchuk <nikolay.marchuk@hardcoreeng.com>

---------

Signed-off-by: Nikolay Marchuk <nikolay.marchuk@hardcoreeng.com>
This commit is contained in:
Nikolay Marchuk
2025-07-21 18:22:42 +07:00
committed by GitHub
parent 8a3d67fd29
commit 6910b127ba
2 changed files with 26 additions and 5 deletions
+8 -1
View File
@@ -92,7 +92,14 @@ export class WorkspaceManager {
)
this.fulltextAdapter = await this.opt.config.fulltextAdapter.factory(this.opt.config.fulltextAdapter.url)
await this.fulltextAdapter.initMapping(this.ctx)
let adapterInitialized = false
while (!adapterInitialized) {
adapterInitialized = await this.fulltextAdapter.initMapping(this.ctx)
if (!adapterInitialized) {
this.ctx.warn('Failed to initialize indexer mapping, retrying in 5 s')
await new Promise((resolve) => setTimeout(resolve, 5000))
}
}
this.shutdownInterval = setInterval(() => {
for (const [k, v] of [...this.indexers.entries()]) {
+18 -4
View File
@@ -127,15 +127,29 @@ class ElasticAdapter implements FullTextAdapter {
)
const allIndexes = Object.keys(existingVersions.body)
const existingOldVersionIndices = allIndexes.filter((name) => name !== indexName)
if (existingOldVersionIndices.length > 0) {
const existsIndex = allIndexes.find((it) => it === indexName) !== undefined
let shouldDropExistingIndex = false
if (existsIndex) {
const mapping = await ctx.with('get-mapping', { indexName }, () =>
this.client.indices.getMapping({
index: indexName
})
)
for (const [propName, propType] of Object.entries(mappings.properties)) {
if (mapping.body[indexName]?.mappings.properties?.[propName]?.type !== propType.type) {
shouldDropExistingIndex = true
break
}
}
}
if (existingOldVersionIndices.length > 0 || shouldDropExistingIndex) {
await ctx.with('delete-old-index', {}, () =>
this.client.indices.delete({
index: existingOldVersionIndices
index: shouldDropExistingIndex ? allIndexes : existingOldVersionIndices
})
)
}
const existsIndex = allIndexes.find((it) => it === indexName) !== undefined
if (!existsIndex) {
if (!existsIndex || shouldDropExistingIndex) {
await ctx.with('create-index', { indexName }, () =>
this.client.indices.create({
index: indexName,