Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change deleteByNamespace to include legacy URL aliases #115459

Merged

Conversation

jportner
Copy link
Contributor

@jportner jportner commented Oct 18, 2021

Resolves #115422.

Now when you delete the contents of a space (deleteByNamespace), you also delete any legacy URL aliases for that space 👏

@jportner jportner added v8.0.0 release_note:skip Skip the PR/issue when compiling release notes v7.16.0 labels Oct 18, 2021
Copy link
Contributor Author

@jportner jportner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Author's notes for reviewers

@@ -17,6 +17,7 @@ const legacyUrlAliasType: SavedObjectsType = {
properties: {
sourceId: { type: 'keyword' },
targetType: { type: 'keyword' },
targetNamespace: { type: 'keyword' },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add this field to the mappings because we are now querying on it in the SOR

Comment on lines -783 to +788
const typesToUpdate = allTypes.filter((type) => !this._registry.isNamespaceAgnostic(type));
const typesToUpdate = [
...allTypes.filter((type) => !this._registry.isNamespaceAgnostic(type)),
LEGACY_URL_ALIAS_TYPE,
];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legacy URL aliases are space-agnostic by design (this made it easier for us to query/etc), but each one is only designed to be used in a single space.

TL;DR I had to add this in typesToUpdate.

Comment on lines 790 to 795
// Construct kueryNode to filter legacy URL aliases (these space-agnostic objects do not use root-level "namespace/s" fields)
const { buildNode } = esKuery.nodeTypes.function;
const targetNamespaceField = `${LEGACY_URL_ALIAS_TYPE}.targetNamespace`;
const match1 = buildNode('is', targetNamespaceField, namespace);
const match2 = buildNode('not', buildNode('exists', targetNamespaceField));
const kueryNode = buildNode('or', [match1, match2]);
Copy link
Contributor Author

@jportner jportner Oct 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kueryNode matches a result with one of two conditions:

  1. The object's legacy-url-alias.targetNamespace field equals the current space, or
  2. The object doesn't have a legacy-url-alias.targetNamespace field at all (e.g., it is not a legacy URL alias) The object is not the legacy-url-alias type

This seemed to be the least messy way to ensure that all alias objects and all non-alias objects in this space are deleted in a single ES call.

If it is a legacy URL alias, it follows this code path in the painless script below:

if (!ctx._source.containsKey('namespaces')) {
  ctx.op = "delete";
}

Edit: changed the second match condition in 9be4f61 based on the suggestion in #115459 (comment)

Comment on lines -806 to +818
namespaces: namespace ? [namespace] : undefined,
namespaces: [namespace],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespace is always defined (see L780-L782 above), so I removed this unnecessary ternary operator

@@ -65,47 +66,37 @@ export function deleteTestSuiteFactory(
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
Copy link
Contributor Author

@jportner jportner Oct 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spaces API integration test fixtures already include three legacy URL aliases:

  1. One in space_1
  2. One in space_2
  3. One in other_space (that doesn't actually exist)

Each test deletes space_2, so the agg buckets should reflect that aliases (1) and (3) still exist afterwards 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to memorialize these remarks as code comments so the next soul to wander in here has this context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, done in 9be4f61

{ key: 'visualization', doc_count: 3 },
{ key: 'dashboard', doc_count: 2 },
{ key: 'index-pattern', doc_count: 1 },
{ key: 'legacy-url-alias', doc_count: 1 },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RE: comment above, alias (1) still exists

countByType: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [{ key: 'legacy-url-alias', doc_count: 1 }], // this alias is in a non-existent space (for other test suites)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RE: comment above, alias (3) still exists

@kibanamachine
Copy link
Contributor

💛 Build succeeded, but was flaky


Test Failures

Kibana Pipeline / general / X-Pack API Integration Tests.x-pack/test/api_integration/apis/ml/jobs/categorization_field_examples·ts.apis Machine Learning jobs Categorization example endpoint - invalid, too many tokens.

Link to Jenkins

Standard Out

Failed Tests Reporter:
  - Test has not failed recently on tracked branches

[00:00:00]     │
[00:00:00]       └-: apis
[00:00:00]         └-> "before all" hook in "apis"
[00:10:07]         └-: Machine Learning
[00:10:07]           └-> "before all" hook in "Machine Learning"
[00:10:07]           └-> "before all" hook in "Machine Learning"
[00:10:07]             │ debg creating role ft_ml_source
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_ml_source]
[00:10:07]             │ debg creating role ft_ml_source_readonly
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_ml_source_readonly]
[00:10:07]             │ debg creating role ft_ml_dest
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_ml_dest]
[00:10:07]             │ debg creating role ft_ml_dest_readonly
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_ml_dest_readonly]
[00:10:07]             │ debg creating role ft_ml_ui_extras
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_ml_ui_extras]
[00:10:07]             │ debg creating role ft_default_space_ml_all
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_default_space_ml_all]
[00:10:07]             │ debg creating role ft_default_space1_ml_all
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_default_space1_ml_all]
[00:10:07]             │ debg creating role ft_all_spaces_ml_all
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_all_spaces_ml_all]
[00:10:07]             │ debg creating role ft_default_space_ml_read
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_default_space_ml_read]
[00:10:07]             │ debg creating role ft_default_space1_ml_read
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_default_space1_ml_read]
[00:10:07]             │ debg creating role ft_all_spaces_ml_read
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_all_spaces_ml_read]
[00:10:07]             │ debg creating role ft_default_space_ml_none
[00:10:07]             │ info [o.e.x.s.a.r.TransportPutRoleAction] [node-01] added role [ft_default_space_ml_none]
[00:10:07]             │ debg creating user ft_ml_poweruser
[00:10:08]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_poweruser]
[00:10:08]             │ debg created user ft_ml_poweruser
[00:10:08]             │ debg creating user ft_ml_poweruser_spaces
[00:10:08]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_poweruser_spaces]
[00:10:08]             │ debg created user ft_ml_poweruser_spaces
[00:10:08]             │ debg creating user ft_ml_poweruser_space1
[00:10:08]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_poweruser_space1]
[00:10:08]             │ debg created user ft_ml_poweruser_space1
[00:10:08]             │ debg creating user ft_ml_poweruser_all_spaces
[00:10:08]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_poweruser_all_spaces]
[00:10:08]             │ debg created user ft_ml_poweruser_all_spaces
[00:10:08]             │ debg creating user ft_ml_viewer
[00:10:08]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_viewer]
[00:10:08]             │ debg created user ft_ml_viewer
[00:10:08]             │ debg creating user ft_ml_viewer_spaces
[00:10:08]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_viewer_spaces]
[00:10:08]             │ debg created user ft_ml_viewer_spaces
[00:10:08]             │ debg creating user ft_ml_viewer_space1
[00:10:08]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_viewer_space1]
[00:10:08]             │ debg created user ft_ml_viewer_space1
[00:10:08]             │ debg creating user ft_ml_viewer_all_spaces
[00:10:08]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_viewer_all_spaces]
[00:10:08]             │ debg created user ft_ml_viewer_all_spaces
[00:10:08]             │ debg creating user ft_ml_unauthorized
[00:10:08]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_unauthorized]
[00:10:08]             │ debg created user ft_ml_unauthorized
[00:10:08]             │ debg creating user ft_ml_unauthorized_spaces
[00:10:09]             │ info [o.e.x.s.a.u.TransportPutUserAction] [node-01] added user [ft_ml_unauthorized_spaces]
[00:10:09]             │ debg created user ft_ml_unauthorized_spaces
[00:14:14]           └-: jobs
[00:14:14]             └-> "before all" hook in "jobs"
[00:14:14]             └-: Categorization example endpoint - 
[00:14:14]               └-> "before all" hook for "valid with good number of tokens"
[00:14:14]               └-> "before all" hook for "valid with good number of tokens"
[00:14:14]                 │ info [x-pack/test/functional/es_archives/ml/categorization] Loading "mappings.json"
[00:14:14]                 │ info [x-pack/test/functional/es_archives/ml/categorization] Loading "data.json.gz"
[00:14:14]                 │ info [o.e.c.m.MetadataCreateIndexService] [node-01] [ft_categorization] creating index, cause [api], templates [], shards [1]/[0]
[00:14:14]                 │ info [x-pack/test/functional/es_archives/ml/categorization] Created index "ft_categorization"
[00:14:14]                 │ debg [x-pack/test/functional/es_archives/ml/categorization] "ft_categorization" settings {"index":{"number_of_replicas":"0","number_of_shards":"1"}}
[00:14:15]                 │ info [x-pack/test/functional/es_archives/ml/categorization] Indexed 1501 docs into "ft_categorization"
[00:14:15]                 │ debg applying update to kibana config: {"dateFormat:tz":"UTC"}
[00:14:16]               └-> valid with good number of tokens
[00:14:16]                 └-> "before each" hook: global before each for "valid with good number of tokens"
[00:14:16]                 └- ✓ pass  (143ms)
[00:14:16]               └-> invalid, too many tokens.
[00:14:16]                 └-> "before each" hook: global before each for "invalid, too many tokens."
[00:14:16]                 │ info [r.suppressed] [node-01] path: /_analyze, params: {}
[00:14:16]                 │      org.elasticsearch.transport.RemoteTransportException: [node-01][127.0.0.1:6371][indices:admin/analyze[s]]
[00:14:16]                 │      Caused by: java.lang.IllegalStateException: The number of tokens produced by calling _analyze has exceeded the allowed maximum of [10000]. This limit can be set by changing the [index.analyze.max_token_count] index level setting.
[00:14:16]                 │      	at org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction$TokenCounter.increment(TransportAnalyzeAction.java:397) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction.simpleAnalyze(TransportAnalyzeAction.java:229) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction.analyze(TransportAnalyzeAction.java:204) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction.analyze(TransportAnalyzeAction.java:122) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction.shardOperation(TransportAnalyzeAction.java:110) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction.shardOperation(TransportAnalyzeAction.java:62) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.action.support.single.shard.TransportSingleShardAction.lambda$asyncShardOperation$0(TransportSingleShardAction.java:99) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:47) [elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:62) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:737) [elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26) [elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
[00:14:16]                 │      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
[00:14:16]                 │      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
[00:14:16]                 │      	at java.lang.Thread.run(Thread.java:833) [?:?]
[00:14:16]                 └- ✖ fail: apis Machine Learning jobs Categorization example endpoint -  invalid, too many tokens.
[00:14:16]                 │       Error: expected 'partially_valid' to sort of equal 'invalid'
[00:14:16]                 │       + expected - actual
[00:14:16]                 │ 
[00:14:16]                 │       -partially_valid
[00:14:16]                 │       +invalid
[00:14:16]                 │       
[00:14:16]                 │       at Assertion.assert (/dev/shm/workspace/parallel/7/kibana/node_modules/@kbn/expect/expect.js:100:11)
[00:14:16]                 │       at Assertion.eql (/dev/shm/workspace/parallel/7/kibana/node_modules/@kbn/expect/expect.js:244:8)
[00:14:16]                 │       at Context.<anonymous> (test/api_integration/apis/ml/jobs/categorization_field_examples.ts:302:44)
[00:14:16]                 │       at runMicrotasks (<anonymous>)
[00:14:16]                 │       at processTicksAndRejections (node:internal/process/task_queues:96:5)
[00:14:16]                 │       at Object.apply (/dev/shm/workspace/parallel/7/kibana/node_modules/@kbn/test/target_node/functional_test_runner/lib/mocha/wrap_function.js:87:16)
[00:14:16]                 │ 
[00:14:16]                 │ 

Stack Trace

Error: expected 'partially_valid' to sort of equal 'invalid'
    at Assertion.assert (/dev/shm/workspace/parallel/7/kibana/node_modules/@kbn/expect/expect.js:100:11)
    at Assertion.eql (/dev/shm/workspace/parallel/7/kibana/node_modules/@kbn/expect/expect.js:244:8)
    at Context.<anonymous> (test/api_integration/apis/ml/jobs/categorization_field_examples.ts:302:44)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at Object.apply (/dev/shm/workspace/parallel/7/kibana/node_modules/@kbn/test/target_node/functional_test_runner/lib/mocha/wrap_function.js:87:16) {
  actual: 'partially_valid',
  expected: 'invalid',
  showDiff: true
}

Metrics [docs]

Saved Objects .kibana field count

Every field in each saved object type adds overhead to Elasticsearch. Kibana needs to keep the total field count below Elasticsearch's default limit of 1000 fields. Only specify field mappings for the fields you wish to search on or query. See https://www.elastic.co/guide/en/kibana/master/development-plugin-saved-objects.html#_mappings

id before after diff
legacy-url-alias 5 6 +1

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@jportner
Copy link
Contributor Author

Flaky ML test failure is certainly unrelated, I'll say this is ready for review 👍

@jportner jportner marked this pull request as ready for review October 19, 2021 00:17
@jportner jportner requested review from a team as code owners October 19, 2021 00:17
src/core/server/saved_objects/service/lib/repository.ts Outdated Show resolved Hide resolved
@@ -65,47 +66,37 @@ export function deleteTestSuiteFactory(
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to memorialize these remarks as code comments so the next soul to wander in here has this context

@jportner
Copy link
Contributor Author

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Saved Objects .kibana field count

Every field in each saved object type adds overhead to Elasticsearch. Kibana needs to keep the total field count below Elasticsearch's default limit of 1000 fields. Only specify field mappings for the fields you wish to search on or query. See https://www.elastic.co/guide/en/kibana/master/development-plugin-saved-objects.html#_mappings

id before after diff
legacy-url-alias 5 6 +1

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Comment on lines +791 to +794
const { buildNode } = esKuery.nodeTypes.function;
const match1 = buildNode('is', `${LEGACY_URL_ALIAS_TYPE}.targetNamespace`, namespace);
const match2 = buildNode('not', buildNode('is', 'type', LEGACY_URL_ALIAS_TYPE));
const kueryNode = buildNode('or', [match1, match2]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈 (combined with the script param that removes documents not having a namespaces property)

@jportner jportner added the auto-backport Deprecated - use backport:version if exact versions are needed label Oct 19, 2021
@jportner jportner merged commit e4fb118 into elastic:master Oct 19, 2021
@jportner jportner deleted the issue-115422-improve-space-deletion-cleanup branch October 19, 2021 14:44
@kibanamachine
Copy link
Contributor

💔 Backport failed

Status Branch Result
7.x Commit could not be cherrypicked due to conflicts

To backport manually run:
node scripts/backport --pr 115459

@jportner jportner removed the v7.16.0 label Oct 19, 2021
@jportner
Copy link
Contributor Author

The Spaces API integration tests have changed in master but those changes were not backported to 7.x, which caused merge conflicts when attempting to backport this PR. Since this functionality isn't needed in 7.x (there are no aliases until 8.0) and the changes to the SOR are really minor, I think it's safe just to not backport this PR.

@jportner jportner added backport:skip This commit does not require backporting and removed auto-backport Deprecated - use backport:version if exact versions are needed labels Oct 19, 2021
@pgayvallet
Copy link
Contributor

Seems good to me

jloleysens added a commit to jloleysens/kibana that referenced this pull request Oct 19, 2021
…-link-to-kibana-app

* 'master' of github.com:elastic/kibana: (30 commits)
  Fix potential error from undefined (elastic#115562)
  [App Search, Crawler] Fix validation step panel padding/whitespace (elastic#115542)
  [Cases][Connectors] ServiceNow ITOM: MVP (elastic#114125)
  Change default session idle timeout to 8 hours. (elastic#115565)
  Upgrade EUI to v39.1.1 (elastic#114732)
  [App Search] Wired up organic results on Curation Suggestions view (elastic#114717)
  [i18n] remove i18n html extractor (elastic#115004)
  [Logs/Metrics UI] Add deprecated field configuration to Deprecations API (elastic#115103)
  [Transform] Add alerting rules management to Transform UI (elastic#115363)
  Update UI links to Fleet and Agent docs (elastic#115295)
  [ML] Adding ability to change data view in advanced job wizard (elastic#115191)
  Change deleteByNamespace to include legacy URL aliases (elastic#115459)
  [Unified Integrations] Remove and cleanup add data views (elastic#115424)
  [Discover] Show ignored field values (elastic#115040)
  [ML] Stop reading the ml.max_open_jobs node attribute (elastic#115524)
  [Discover] Improve doc viewer code in Discover (elastic#114759)
  [Security Solutions] Adds security detection rule actions as importable and exportable (elastic#115243)
  [Security Solution] [Platform] Migrate legacy actions whenever user interacts with the rule (elastic#115101)
  [Fleet] Add telemetry for integration cards (elastic#115413)
  🐛 Fix single percentile case when ES is returning no buckets (elastic#115214)
  ...

# Conflicts:
#	x-pack/plugins/reporting/public/management/__snapshots__/report_listing.test.tsx.snap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting buildkite-ci release_note:skip Skip the PR/issue when compiling release notes v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve space deletion cleanup
4 participants