Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[esArchiver] actually re-delete the .kibana index if we lose recreate…
Browse files Browse the repository at this point in the history
… race (elastic#72354)

Co-authored-by: spalger <spalger@users.noreply.github.com>
(cherry picked from commit ef875cf)
  • Loading branch information
Spencer authored and spalger committed Jul 17, 2020
1 parent 5891ecc commit 3468ae5
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/es_archiver/lib/indices/create_index_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import { Transform, Readable } from 'stream';
import { inspect } from 'util';

import { get, once } from 'lodash';
import { Client } from 'elasticsearch';
import { ToolingLog } from '@kbn/dev-utils';

Expand Down Expand Up @@ -54,7 +53,7 @@ export function createCreateIndexStream({
// If we're trying to import Kibana index docs, we need to ensure that
// previous indices are removed so we're starting w/ a clean slate for
// migrations. This only needs to be done once per archive load operation.
const deleteKibanaIndicesOnce = once(deleteKibanaIndices);
let kibanaIndexAlreadyDeleted = false;

async function handleDoc(stream: Readable, record: DocRecord) {
if (skipDocsFromIndices.has(record.value.index)) {
Expand All @@ -73,8 +72,9 @@ export function createCreateIndexStream({

async function attemptToCreate(attemptNumber = 1) {
try {
if (isKibana) {
await deleteKibanaIndicesOnce({ client, stats, log });
if (isKibana && !kibanaIndexAlreadyDeleted) {
await deleteKibanaIndices({ client, stats, log });
kibanaIndexAlreadyDeleted = true;
}

await client.indices.create({
Expand All @@ -94,6 +94,7 @@ export function createCreateIndexStream({
err?.body?.error?.reason?.includes('index exists with the same name as the alias') &&
attemptNumber < 3
) {
kibanaIndexAlreadyDeleted = false;
const aliasStr = inspect(aliases);
log.info(
`failed to create aliases [${aliasStr}] because ES indicated an index/alias already exists, trying again`
Expand All @@ -102,10 +103,7 @@ export function createCreateIndexStream({
return;
}

if (
get(err, 'body.error.type') !== 'resource_already_exists_exception' ||
attemptNumber >= 3
) {
if (err?.body?.error?.type !== 'resource_already_exists_exception' || attemptNumber >= 3) {
throw err;
}

Expand Down

0 comments on commit 3468ae5

Please sign in to comment.