-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Wed Aug 22, 2012 8:38 am 
Newbie

Joined: Wed Aug 22, 2012 8:32 am
Posts: 2
Hi, I have a multi-tenant hibernate app that works well, even with hibernate search. The only problem is that I cannot use MassIndexer to reindex my data. It seems that it internally opens its own sessions but ignores the multi-tenancy.

To obtain the session in my application I do this:
sessionFactory.withOptions().tenantIdentifier(tenantId).openSession()

But upon calling FullTextSessions's createIndexer I am getting this:
org.hibernate.HibernateException: SessionFactory configured for multi-tenancy, but no tenant identifier specified

Is there a way around this? Or am I doing something wrong?


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Fri Aug 24, 2012 9:28 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

no I think you are right - this is an issue. Can you create an issue for it - https://hibernate.onjira.com/browse/HSEARCH


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Wed Aug 06, 2014 11:10 am 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
I'm trying to use the following code without success:
Code:
FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession(
                        hibernateEntityManagerFactory.getSessionFactory()
                              .withOptions().tenantIdentifier(myTenantIdentifier)
                              .openSession()
                        );
MassIndexer massindexer = fullTextSession.createIndexer();
massindexer.startAndWait();


When the session is opened the "resolveCurrentTenantIdentifier()" method is catched.
This is my CurrentTenantIdentifierResolver class:
Code:

public class HibernateTenantIdentifierResolver implements CurrentTenantIdentifierResolver {

   @Override
   public String resolveCurrentTenantIdentifier() {
      TenancyContext tenancyContext = TenancyContextHolder.getContext();
      Tenant tenant = tenancyContext.getTenant();
      if (tenant == null)
         return DEFAULTSERVICE;
      else
         return tenant.getServiceDBName();
   }

   @Override
   public boolean validateExistingCurrentSessions() {
      return false;
   }



I expect that the tenantIdentifier could be fixed by withOptions().tenantIdentifier(myTenantIdentifier), but it does not work!
Is it a bug? Can you help me?

hibernate version: 4.3.5.Final
hibernate search version: 5.0.0.Alpha4
spring version: 4.0.6.RELEASE


thanks,
dario
(italy)


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Fri Aug 15, 2014 6:34 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi Dario,
yes that is a bug. I opened an issue on JIRA as it seems the previous poster didn't report it: https://hibernate.atlassian.net/browse/HSEARCH-1649

Do you think you might want to try fixing it? I'm happy to help you getting started, especially since you're using Hibernate Search 5, this is a good time to make such changes.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Wed Sep 10, 2014 5:57 am 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
Hi Sanne,
in this period i have no time available to try to resolve the bug. But we need of that feature!
So in the next weeks, if the issue is not already resolved, I'll try to fix from the source code; for this scope I will need your help!


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Mon Sep 22, 2014 9:03 am 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
Hi Sanne,
we have solved our problem using a InheritableThreadLocal as TenancyContextHolder! In the next days we continue to test it.

I'm sorry I cannot collaborate with you to fix the bug inside the hibernate search source code. I'm working hard for a software (using hibernate search 5) and I have yet many issue to resolve.

Regards,
Dario


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Mon Sep 22, 2014 10:10 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi Dario,
no worries and thanks for the update! I'll see what we can do to fix this properly in future, sorry I don't have time for this myself right now.

And please let me know how your experience is going with Hibernate Search 5, I'm eager to get some feedback on it.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Fri Jan 23, 2015 7:18 am 
Newbie

Joined: Fri Jan 23, 2015 6:41 am
Posts: 2
Dear Darioc,

We are facing a similar kind if issue and couldn't come up with an apt methodology to circumvent the same.
It seems from your post that you had solved your problem using a InheritableThreadLocal as TenancyContextHolder! Did the same work for you?

Could you please elaborate a bit more on the design you had chosen for the resolve? It would be really great if you could post or send me an email regarding the relevant piece of code.


Thanks,
Arnab


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Mon Jan 26, 2015 7:58 am 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
Hi Arnab,
the TenancyContextHolder keeps a ThreadLocal with an custom object with the database informations.

So when the main thread of the MassIndexer starts it fills the database information into thread local object; the future requests of the MassIndexer will use the same object.

The relevant spring configuration:

Code:
<!-- jpa entity manager factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:jpaPropertyMap-ref="hibernateSearchPropertyMap" depends-on="multiTenantProvider;tenantIdResolver">
...
</bean>

<!-- hibernate4 multi-tenancy -->
<bean id="multiTenantProvider" class="it.area.mrs.spring.tenancy.HibernateMultiTenancyConnectionProvider"
   depends-on="dataSourceWrapper" lazy-init="false"></bean>
<bean id="tenantIdResolver" class="it.area.mrs.spring.tenancy.HibernateTenantIdentifierResolver"></bean>

<util:map id="hibernateSearchPropertyMap">
  <!-- hibernate tenancy -->
  <entry key="hibernate.tenant_identifier_resolver" value-ref="tenantIdResolver"/>
  <entry key="hibernate.multi_tenant_connection_provider" value-ref="multiTenantProvider"/>
...
</util:map>



The relevant java code:

Code:
public class HibernateTenantIdentifierResolver implements org.hibernate.context.spi.CurrentTenantIdentifierResolver {

   @Override
   public String resolveCurrentTenantIdentifier() {
      TenancyContext tenancyContext = TenancyContextHolder.getContext();
      Tenant tenant = tenancyContext.getTenant();
      return tenant.getServiceDBName();
   }

   @Override
   public boolean validateExistingCurrentSessions() {
      return false;
   }

}


Code:
public class TenancyContextHolder {

   private static TenancyContextHolderStrategy strategy = new ThreadLocalTenancyContextHolderStrategy();
   public static TenancyContext getContext() {
      return strategy.getContext();
   }
}


Code:

public class ThreadLocalTenancyContextHolderStrategy {
   private static final InheritableThreadLocal<TenancyContext> contextHolder = new InheritableThreadLocal<TenancyContext>();

   public TenancyContext getContext() {
      TenancyContext ctx = contextHolder.get();

      if (ctx == null) {
         ctx = createEmptyContext();
         contextHolder.set(ctx);
      }

      return ctx;
   }
}


Code:
public class TenancyContext {
   private final String identity;
   private final Object data;
....
}


That's all.

Dario


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Thu Jan 29, 2015 7:44 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Thanks for all details Dario.
We're now tracking this as https://hibernate.atlassian.net/browse/HSEARCH-1649

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Thu Jan 29, 2015 8:05 am 
Newbie

Joined: Fri Jan 23, 2015 6:41 am
Posts: 2
Thanks a ton Dario, I will try this approach and let you know.


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Fri Jan 30, 2015 7:33 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
If I'm not wrong using this approach entities are still added to the same index (the tenant is not used).

I was wondering if your use case is not affected by this or maybe you solve it some way.

Eventually, would you prefer if would be possible to separate the data in the index for the different tenants?


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Fri Jan 30, 2015 10:22 am 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
Maybe I didn't understand.
With the proposed solution all data are indexed in the configurated tenant (needed to retrieve data from database).


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Fri Jan 30, 2015 1:11 pm 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Indexing the data is fine, the mass indexer will read the data from the db of the tenant you have specified.
But if you run the mass indexer twice for two different tenants on the same entity class, the same index will be updated.

This means that a query on that indexed entity will return results from both tenants.
I'm not sure if this is a problem.


Top
 Profile  
 
 Post subject: Re: MassIndexer incompatible with hibernate multi-tenancy?
PostPosted: Fri Jan 30, 2015 1:49 pm 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
Hi Davide,
I have enabled the dynamic sharding on hibernate seach that read the right tenant and it writes the appropriate file index.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.