Skip to content

Commit

Permalink
Exclude non-persisted sessions from SO migration (elastic#96938)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Apr 13, 2021
1 parent 13c7885 commit b6ad959
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,22 @@ describe('ElasticIndex', () => {
type: 'tsvb-validation-telemetry',
},
},
{
bool: {
must: [
{
match: {
type: 'search-session',
},
},
{
match: {
'search-session.persisted': false,
},
},
],
},
},
],
},
},
Expand Down
57 changes: 40 additions & 17 deletions src/core/server/saved_objects/migrations/core/elastic_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,46 @@ export interface FullIndexInfo {
mappings: IndexMapping;
}

// When migrating from the outdated index we use a read query which excludes
// saved objects which are no longer used. These saved objects will still be
// kept in the outdated index for backup purposes, but won't be availble in
// the upgraded index.
export const excludeUnusedTypesQuery: estypes.QueryContainer = {
bool: {
must_not: [
// https://github.com/elastic/kibana/issues/91869
{
term: {
type: 'fleet-agent-events',
},
},
// https://github.com/elastic/kibana/issues/95617
{
term: {
type: 'tsvb-validation-telemetry',
},
},
// https://github.com/elastic/kibana/issues/96131
{
bool: {
must: [
{
match: {
type: 'search-session',
},
},
{
match: {
'search-session.persisted': false,
},
},
],
},
},
],
},
};

/**
* A slight enhancement to indices.get, that adds indexName, and validates that the
* index mappings are somewhat what we expect.
Expand Down Expand Up @@ -69,23 +109,6 @@ export function reader(
const scroll = scrollDuration;
let scrollId: string | undefined;

// When migrating from the outdated index we use a read query which excludes
// saved object types which are no longer used. These saved objects will
// still be kept in the outdated index for backup purposes, but won't be
// availble in the upgraded index.
const EXCLUDE_UNUSED_TYPES = [
'fleet-agent-events', // https://github.com/elastic/kibana/issues/91869
'tsvb-validation-telemetry', // https://github.com/elastic/kibana/issues/95617
];

const excludeUnusedTypesQuery = {
bool: {
must_not: EXCLUDE_UNUSED_TYPES.map((type) => ({
term: { type },
})),
},
};

const nextBatch = () =>
scrollId !== undefined
? client.scroll<SearchResponse<SavedObjectsRawDocSource>>({
Expand Down
1 change: 1 addition & 0 deletions src/core/server/saved_objects/migrations/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export type { LogFn, SavedObjectsMigrationLogger } from './migration_logger';
export type { MigrationResult, MigrationStatus } from './migration_coordinator';
export { createMigrationEsClient } from './migration_es_client';
export type { MigrationEsClient } from './migration_es_client';
export { excludeUnusedTypesQuery } from './elastic_index';
15 changes: 5 additions & 10 deletions src/core/server/saved_objects/migrationsv2/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { errors as EsErrors } from '@elastic/elasticsearch';
import type { ElasticsearchClientError, ResponseError } from '@elastic/elasticsearch/lib/errors';
import { pipe } from 'fp-ts/lib/pipeable';
import { flow } from 'fp-ts/lib/function';
import { QueryContainer } from '@elastic/eui/src/components/search_bar/query/ast_to_es_query_dsl';
import { ElasticsearchClient } from '../../../elasticsearch';
import { IndexMapping } from '../../mappings';
import { SavedObjectsRawDoc, SavedObjectsRawDocSource } from '../../serialization';
Expand Down Expand Up @@ -440,9 +439,9 @@ export const reindex = (
requireAlias: boolean,
/* When reindexing we use a source query to exclude saved objects types which
* are no longer used. These saved objects will still be kept in the outdated
* index for backup purposes, but won't be availble in the upgraded index.
* index for backup purposes, but won't be available in the upgraded index.
*/
unusedTypesToExclude: Option.Option<string[]>
unusedTypesQuery: Option.Option<estypes.QueryContainer>
): TaskEither.TaskEither<RetryableEsClientError, ReindexResponse> => () => {
return client
.reindex({
Expand All @@ -457,14 +456,10 @@ export const reindex = (
// Set reindex batch size
size: BATCH_SIZE,
// Exclude saved object types
query: Option.fold<string[], QueryContainer | undefined>(
query: Option.fold<estypes.QueryContainer, estypes.QueryContainer | undefined>(
() => undefined,
(types) => ({
bool: {
must_not: types.map((type) => ({ term: { type } })),
},
})
)(unusedTypesToExclude),
(query) => query
)(unusedTypesQuery),
},
dest: {
index: targetIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,20 @@ describe('migration actions', () => {
]
`);
});
it('resolves right and excludes all unusedTypesToExclude documents', async () => {
it('resolves right and excludes all documents not matching the unusedTypesQuery', async () => {
const res = (await reindex(
client,
'existing_index_with_docs',
'reindex_target_excluded_docs',
Option.none,
false,
Option.some(['f_agent_event', 'another_unused_type'])
Option.of({
bool: {
must_not: ['f_agent_event', 'another_unused_type'].map((type) => ({
term: { type },
})),
},
})
)()) as Either.Right<ReindexResponse>;
const task = waitForReindexTask(client, res.right.taskId, '10s');
await expect(task()).resolves.toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,40 @@ describe('migrationsStateActionMachine', () => {
},
},
},
"unusedTypesToExclude": Object {
"unusedTypesQuery": Object {
"_tag": "Some",
"value": Array [
"fleet-agent-events",
"tsvb-validation-telemetry",
],
"value": Object {
"bool": Object {
"must_not": Array [
Object {
"term": Object {
"type": "fleet-agent-events",
},
},
Object {
"term": Object {
"type": "tsvb-validation-telemetry",
},
},
Object {
"bool": Object {
"must": Array [
Object {
"match": Object {
"type": "search-session",
},
},
Object {
"match": Object {
"search-session.persisted": false,
},
},
],
},
},
],
},
},
},
"versionAlias": ".my-so-index_7.11.0",
"versionIndex": ".my-so-index_7.11.0_001",
Expand Down Expand Up @@ -317,12 +345,40 @@ describe('migrationsStateActionMachine', () => {
},
},
},
"unusedTypesToExclude": Object {
"unusedTypesQuery": Object {
"_tag": "Some",
"value": Array [
"fleet-agent-events",
"tsvb-validation-telemetry",
],
"value": Object {
"bool": Object {
"must_not": Array [
Object {
"term": Object {
"type": "fleet-agent-events",
},
},
Object {
"term": Object {
"type": "tsvb-validation-telemetry",
},
},
Object {
"bool": Object {
"must": Array [
Object {
"match": Object {
"type": "search-session",
},
},
Object {
"match": Object {
"search-session.persisted": false,
},
},
],
},
},
],
},
},
},
"versionAlias": ".my-so-index_7.11.0",
"versionIndex": ".my-so-index_7.11.0_001",
Expand Down Expand Up @@ -470,12 +526,40 @@ describe('migrationsStateActionMachine', () => {
},
},
},
"unusedTypesToExclude": Object {
"unusedTypesQuery": Object {
"_tag": "Some",
"value": Array [
"fleet-agent-events",
"tsvb-validation-telemetry",
],
"value": Object {
"bool": Object {
"must_not": Array [
Object {
"term": Object {
"type": "fleet-agent-events",
},
},
Object {
"term": Object {
"type": "tsvb-validation-telemetry",
},
},
Object {
"bool": Object {
"must": Array [
Object {
"match": Object {
"type": "search-session",
},
},
Object {
"match": Object {
"search-session.persisted": false,
},
},
],
},
},
],
},
},
},
"versionAlias": ".my-so-index_7.11.0",
"versionIndex": ".my-so-index_7.11.0_001",
Expand Down Expand Up @@ -533,12 +617,40 @@ describe('migrationsStateActionMachine', () => {
},
},
},
"unusedTypesToExclude": Object {
"unusedTypesQuery": Object {
"_tag": "Some",
"value": Array [
"fleet-agent-events",
"tsvb-validation-telemetry",
],
"value": Object {
"bool": Object {
"must_not": Array [
Object {
"term": Object {
"type": "fleet-agent-events",
},
},
Object {
"term": Object {
"type": "tsvb-validation-telemetry",
},
},
Object {
"bool": Object {
"must": Array [
Object {
"match": Object {
"type": "search-session",
},
},
Object {
"match": Object {
"search-session.persisted": false,
},
},
],
},
},
],
},
},
},
"versionAlias": ".my-so-index_7.11.0",
"versionIndex": ".my-so-index_7.11.0_001",
Expand Down
Loading

0 comments on commit b6ad959

Please sign in to comment.