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

ESConfig: configurable Elasticsearch document types to allow splitting index #1296

Merged
merged 31 commits into from
Oct 28, 2024

Conversation

haarg
Copy link
Member

@haarg haarg commented Oct 23, 2024

This adds a central module to configure all of the document types stored in Elasticsearch, allowing a config file to override values. It then adapts the rest of the code to use this module.

In the past, everything stored in Elasticsearch was stored in one index, with multiple types. Initially this was the cpan index, but later this was changed to using cpan_v1_01 with cpan being an alias. The idea was that we could have different versions of the index on the same server, and copy or switch between them. The index used would be passed around the code, acting as a single configurable value that would impact all document types. Some newer document types have been stored in separate indexes, but not all parts of the codebase accommodate this well.

Going forward, we want every document type to be in a separate index. Initially this will be one type per index. Eventually, types will go away entirely. This is required to upgrade Elasticsearch. To facilitate this, this PR adds a new module MetaCPAN::ESConfig which lists all of the document types and which index and type they are using. These values can be overridden in the config file, meaning we can switch to a different Elasticsearch instance where the documents are stored in different places just by deploying a new config file.

Everything that was previously holding a ElasticSearchX::Model index instance has been updated to instead use the ElasticSearchX::Model instance directly. The new $model->doc($doc_type) method is a replacement for $index->type($type), allowing accessing types that are in different indexes.

Everything that was previously using a copied index name with a fixed type (often like $es->search(index => $self->index->name, 'file')) is now using the new es_doc_path sub (like $es->search(es_doc_path('file'))). This seemed the easiest way to integrate this throughout the codebase.

There is a new Catalyst::Model named ESModel. This replaces both the CPAN and User models. It allows access to MetaCPAN::Model, and thus everything using ElasticSearchX::Model. The CPAN vs User split was related to the index their data was stored in, and is thus no longer relevant. The new model has less magic as well.

Fixes #1297

@haarg haarg requested review from oalders and mickeyn October 23, 2024 08:31
@haarg haarg changed the title ESConfig: configurable Elasticsearch document types ESConfig: configurable Elasticsearch document types to allow splitting index Oct 23, 2024
@haarg haarg force-pushed the haarg/esconfig branch 6 times, most recently from 9a8d1f1 to 04fc747 Compare October 23, 2024 12:33
@haarg haarg marked this pull request as ready for review October 23, 2024 12:37
oalders
oalders previously approved these changes Oct 24, 2024
Copy link
Member

@oalders oalders left a comment

Choose a reason for hiding this comment

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

😅

@@ -0,0 +1,219 @@
use v5.20;
use warnings;
use experimental qw(signatures postderef);
Copy link
Member

Choose a reason for hiding this comment

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

👍🏻

mapping => 'MetaCPAN::Script::Mapping::CPAN::Release',
model => 'MetaCPAN::Document::Release',
},

Copy link
Member

Choose a reason for hiding this comment

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

Stray newline. Doesn't really matter.

Copy link
Member Author

Choose a reason for hiding this comment

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

This was an intentional separation between the "public" and "private" data. Could use a comment if it's going to remain.

Comment on lines +119 to +122
{
use Moo;
}
Copy link
Member

Choose a reason for hiding this comment

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

Why is this in its own block?

Copy link
Member Author

Choose a reason for hiding this comment

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

Moo, like Moose, applies use warnings, which re-enables experimental warnings.

Centralize Elasticsearch configuration in MetaCPAN::ESConfig. Allow
overridden values from the main config file.

This module is not meant to have any behavior aside from holding the
configuration.
ESConfig knows how to find mapping data. Use it to find the mapping data
as well as index configuration.

The mapping data should be able to be moved into json files rather than
json wrapped in a module. This can happen in the future.
The analysis set in MetaCPAN::Model wasn't used for anything directly,
generate the index deployment statements. The index settings we actually
use lives in MetaCPAN::Script::Mapping::DeployStatement, so the
declarations in MetaCPAN::Model were used for nothing.
Rather than searching for modules on disk, use the explicit
configuration in ESConfig to configure MetaCPAN::Model.
ElasticSearchX::Model ignores all errors in a ::Set package can't be
loaded, and uses a generic ElasticSearchX::Model::Document::Set object.

It's fine for the module to be missing, but compilation errors should be
reported.
PPI and thus Perl::Critic don't understand signatures, so the rule ends up prohibiting signatures as well
Allows getting a "type" object from a document name rather than needing
to specify an index and type.
Previously when trying to refresh indices, scripts would call
$self->index->refresh. This would refresh the "currently used" index.
That doesn't make any sense when splitting each type into its own index.
This was also using ElasticSearchX::Model, which we want to get rid of.

Instead, call ->indices->refresh via the Search::Elasticsearch object.
This will refresh all indices, which is fine for our purposes. In the
future, we could consider being more selective about which indices we
are refreshing, but this is no worse than the old behavior.
Rather than using the same index to find other types, find them via the
model. This means the types don't need to be in the same index.
Many parts of the code treated the index as the parent of all data, so
it was the thing being passed around. That will no be true in the
future.

Instead, ESConfig can give the path (index+type) of each named document
type. Convert most places passing around index to use es_doc_path.
Replaces the CPAN and User model classes. Removes magic namespace
creation. Just return the Search::Elasticsearch object, or the model
object.
Trying to count distribution documents before creating is vulnerable to
concurrency and consistency issues. Instead, use an upsert to create it.
We have two autocomplete end points. The old one is no longer used by
the front end. The new one uses the suggest API.

Rewrite both end points to use the suggest SPI, just returning data in
different forms.
Older versions expect the key "inline", newer expect "source".
use_dis_max is the default, and isn't supported in newer versions
@haarg
Copy link
Member Author

haarg commented Oct 27, 2024

Updated with various tweaks, fixes, and compatibility fixes for newer Elasticsearch.

This will now pass the tests using Elasticsearch 2 and Elasticsearch 5. It also works using either one index, or splitting the documents into separate indices.

With some updates to the mappings, it will also pass tests on Elasticsearch 6.

Copy link
Member

@oalders oalders left a comment

Choose a reason for hiding this comment

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

😅

@haarg haarg merged commit 2a7a80b into master Oct 28, 2024
1 check passed
@haarg haarg deleted the haarg/esconfig branch October 28, 2024 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Indexes with multiple types can no longer be created
2 participants