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

[hibernate-search] Support injecting runtime properties in custom IndexLayoutStrategy implementation #41429

Closed
cthiebault opened this issue Jun 25, 2024 · 5 comments · Fixed by #43361
Labels
area/hibernate-search Hibernate Search kind/enhancement New feature or request
Milestone

Comments

@cthiebault
Copy link

Description

Following #41384, I try to implement a custom IndexLayoutStrategy to prefix the index name with a config property defined in application.properties.

@SearchExtension
public class PrefixIndexLayoutStrategy implements IndexLayoutStrategy {

    @ConfigProperty(name = "index-prefix")
    String prefix;

    @Override
    public String createInitialElasticsearchIndexName(String hibernateSearchIndexName) {
        return prefix + hibernateSearchIndexName + "-000001";
    }
    [...]

when I run my application (prod profile in k8s), I get

A runtime config property value differs from the value that was injected during the static intialization phase:
- the runtime value of 'index-prefix' is [dev-] but the value [null] was injected into myapp.PrefixIndexLayoutStrategy()

If that's intentional then annotate the injected field/parameter with @io.quarkus.runtime.annotations.StaticInitSafe to eliminate the false positive.

Quoting @yrodiere from #41384 (comment):

I believe the index layout strategy is used by Hibernate Search at static init, so you can't use runtime properties in there.
I'd suggest opening a feature request to make the index layout strategy a runtime component (which makes sense)
FWIW it'll probably require changes to Hibernate Search itself, since it currently creates index names very (too?) early,
for no good reason that I can see (the names aren't used until runtime):
https://github.com/hibernate/hibernate-search/blob/0b9e3f0d87250daa800f10223bcc81cdb0da773a/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/impl/ElasticsearchBackendImpl.java#L206

Implementation ideas

No response

@cthiebault cthiebault added the kind/enhancement New feature or request label Jun 25, 2024
@quarkus-bot quarkus-bot bot added the area/hibernate-search Hibernate Search label Jun 25, 2024
Copy link

quarkus-bot bot commented Jun 25, 2024

/cc @gsmet (hibernate-search), @marko-bekhta (hibernate-search), @yrodiere (hibernate-search)

@yrodiere
Copy link
Member

Thanks.

Note a workaround would be to annotate your injection point with @StaticInitSafe, and to build your app separately for each environment:

./mvn clean package -Dquarkus.profile=prod
./mvn clean package -Dquarkus.profile=staging

Then each env will use the value you set for that property -- assuming it's set at build time.

@cthiebault
Copy link
Author

But in that case I would need to publish a different artefact (Docker image in my case) per environment?

As a workaround, I'm using an environment variable to set the prefix... I don't like to mix config in application.properties and env variables, but it works!

@yrodiere
Copy link
Member

yrodiere commented Jul 1, 2024

FWIW it'll probably require changes to Hibernate Search itself, since it currently creates index names very (too?) early,
for no good reason that I can see (the names aren't used until runtime):
https://github.com/hibernate/hibernate-search/blob/0b9e3f0d87250daa800f10223bcc81cdb0da773a/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/impl/ElasticsearchBackendImpl.java#L206

I opened https://hibernate.atlassian.net/browse/HSEARCH-5190 to make a fix possible in Quarkus in the future.

But in that case I would need to publish a different artefact (Docker image in my case) per environment?

Yes.

As a workaround, I'm using an environment variable to set the prefix... I don't like to mix config in application.properties and env variables, but it works!

I suspect you're being lucky and this might break in the future, but it's better than nothing :)

@yrodiere
Copy link
Member

yrodiere commented Jul 16, 2024

https://hibernate.atlassian.net/browse/HSEARCH-5189 moved the creation of the index layout strategy to "runtime", so I will try fix this in #41359 by moving the config to runtime as well.

EDIT: Though someone might need to remind me :]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-search Hibernate Search kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants