Skip to content

Commit 8f50689

Browse files
zackureySanne
authored andcommitted
HSEARCH-926 and a test that verifies multiple types sharing the same index still only result in one InfinispanDirectoryProvider getting created for the index.
1 parent 4e8d124 commit 8f50689

File tree

7 files changed

+352
-20
lines changed

7 files changed

+352
-20
lines changed

hibernate-search-infinispan/src/test/java/org/hibernate/search/infinispan/ClusterTestHelper.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.hibernate.search.infinispan;
2121

2222
import java.util.List;
23+
import java.util.Set;
2324

2425
import junit.framework.AssertionFailedError;
2526

@@ -50,7 +51,7 @@ public class ClusterTestHelper {
5051
* join the existing nodes.
5152
* @return a started FullTextSessionBuilder
5253
*/
53-
public static FullTextSessionBuilder createClusterNode() {
54+
public static FullTextSessionBuilder createClusterNode(Set<Class<?>> entityTypes) {
5455
FullTextSessionBuilder node = new FullTextSessionBuilder()
5556
.setProperty( "hibernate.search.default.directory_provider", "infinispan" )
5657
// fragment on every 7 bytes: don't use this on a real case!
@@ -65,15 +66,17 @@ public static FullTextSessionBuilder createClusterNode() {
6566
.setProperty(
6667
Environment.CONNECTION_PROVIDER,
6768
org.hibernate.search.infinispan.ClusterSharedConnectionProvider.class.getName()
68-
)
69-
.addAnnotatedClass( SimpleEmail.class );
69+
);
70+
for(Class<?> entityType : entityTypes){
71+
node.addAnnotatedClass( entityType );
72+
}
7073
return node.build();
7174
}
7275

7376
/**
7477
* Wait some time for the cluster to form
7578
*/
76-
public static void waitMembersCount(FullTextSessionBuilder node, int expectedSize) {
79+
public static void waitMembersCount(FullTextSessionBuilder node, Class<?> entityType, int expectedSize) {
7780
int currentSize = 0;
7881
int loopCounter = 0;
7982
while ( currentSize < expectedSize ) {
@@ -83,7 +86,7 @@ public static void waitMembersCount(FullTextSessionBuilder node, int expectedSiz
8386
catch ( InterruptedException e ) {
8487
throw new AssertionFailedError( e.getMessage() );
8588
}
86-
currentSize = clusterSize( node );
89+
currentSize = clusterSize( node, entityType);
8790
if ( loopCounter > 200 ) {
8891
throw new AssertionFailedError( "timeout while waiting for all nodes to join in cluster" );
8992
}
@@ -95,10 +98,10 @@ public static void waitMembersCount(FullTextSessionBuilder node, int expectedSiz
9598
* @param node the FullTextSessionBuilder representing the current node
9699
* @return the number of nodes as seen by the current node
97100
*/
98-
public static int clusterSize(FullTextSessionBuilder node) {
101+
public static int clusterSize(FullTextSessionBuilder node, Class<?> entityType) {
99102
SearchFactoryIntegrator searchFactory = (SearchFactoryIntegrator) node.getSearchFactory();
100-
EntityIndexBinder<SimpleEmail> mailIndexBinding = searchFactory.getIndexBindingForEntity( SimpleEmail.class );
101-
DirectoryBasedIndexManager indexManager = (DirectoryBasedIndexManager) mailIndexBinding.getIndexManagers()[0];
103+
EntityIndexBinder<?> indexBinding = searchFactory.getIndexBindingForEntity( entityType );
104+
DirectoryBasedIndexManager indexManager = (DirectoryBasedIndexManager) indexBinding.getIndexManagers()[0];
102105
InfinispanDirectoryProvider directoryProvider = (InfinispanDirectoryProvider) indexManager.getDirectoryProvider();
103106
EmbeddedCacheManager cacheManager = directoryProvider.getCacheManager();
104107
List<Address> members = cacheManager.getMembers();

hibernate-search-infinispan/src/test/java/org/hibernate/search/infinispan/LiveRunningTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.hibernate.search.infinispan.ClusterTestHelper.createClusterNode;
2525
import static org.hibernate.search.infinispan.ClusterTestHelper.waitMembersCount;
2626

27+
import java.util.HashSet;
2728
import java.util.LinkedList;
2829
import java.util.List;
2930

@@ -52,8 +53,9 @@ public class LiveRunningTest {
5253

5354
private static final int TEST_RUNS = 17;
5455
private static final int MAX_SLAVES = 5;
56+
private static HashSet<Class<?>> entityTypes;
5557

56-
private final FullTextSessionBuilder master = createClusterNode();
58+
private final FullTextSessionBuilder master = createClusterNode(entityTypes);
5759
private final List<FullTextSessionBuilder> slaves = new LinkedList<FullTextSessionBuilder>();
5860

5961
private boolean growCluster = true;
@@ -85,7 +87,7 @@ private void assertViews() {
8587
}
8688

8789
private void assertView(FullTextSessionBuilder node) {
88-
assertEquals( slaves.size() + 1 , clusterSize( node ) );
90+
assertEquals( slaves.size() + 1 , clusterSize( node, SimpleEmail.class ) );
8991
FullTextSession session = node.openFullTextSession();
9092
try {
9193
FullTextQuery fullTextQuery = session.createFullTextQuery( new MatchAllDocsQuery() );
@@ -103,7 +105,7 @@ private void adjustSlavesNumber(int i) {
103105
growCluster = false;
104106
}
105107
else {
106-
slaves.add( createClusterNode() );
108+
slaves.add( createClusterNode(entityTypes) );
107109
}
108110
}
109111
else {
@@ -136,14 +138,16 @@ private void writeOnMaster() {
136138

137139
private void waitForAllJoinsCompleted() {
138140
int expectedSize = slaves.size() + 1;
139-
waitMembersCount( master, expectedSize );
141+
waitMembersCount( master, SimpleEmail.class, expectedSize );
140142
for (FullTextSessionBuilder slave : slaves) {
141-
waitMembersCount( slave, expectedSize );
143+
waitMembersCount( slave, SimpleEmail.class, expectedSize );
142144
}
143145
}
144146

145147
@BeforeClass
146148
public static void prepareConnectionPool() {
149+
entityTypes = new HashSet<Class<?>>();
150+
entityTypes.add( SimpleEmail.class );
147151
ClusterSharedConnectionProvider.realStart();
148152
}
149153

hibernate-search-infinispan/src/test/java/org/hibernate/search/infinispan/TwoNodesTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.hibernate.search.infinispan.ClusterTestHelper.createClusterNode;
2929
import static org.hibernate.search.infinispan.ClusterTestHelper.waitMembersCount;
3030

31+
import java.util.HashSet;
3132
import java.util.List;
3233

3334
import org.apache.lucene.search.Query;
@@ -59,10 +60,11 @@ public class TwoNodesTest {
5960

6061
FullTextSessionBuilder nodea;
6162
FullTextSessionBuilder nodeb;
63+
HashSet<Class<?>> entityTypes;
6264

6365
@Test
6466
public void testSomething() {
65-
assertEquals( 2, clusterSize( nodea ) );
67+
assertEquals( 2, clusterSize( nodea, SimpleEmail.class ) );
6668
// index an entity:
6769
{
6870
FullTextSession fullTextSession = nodea.openFullTextSession();
@@ -77,16 +79,16 @@ public void testSomething() {
7779
// verify nodeb is able to find it:
7880
verifyNodeSeesUpdatedIndex( nodeb );
7981
// now start a new node, it will join the cluster and receive the current index state:
80-
FullTextSessionBuilder nodeC = createClusterNode();
81-
assertEquals( 3, clusterSize( nodea ) );
82+
FullTextSessionBuilder nodeC = createClusterNode(entityTypes);
83+
assertEquals( 3, clusterSize( nodea, SimpleEmail.class ) );
8284
try {
8385
// verify the new node is able to perform the same searches:
8486
verifyNodeSeesUpdatedIndex(nodeC);
8587
}
8688
finally {
8789
nodeC.close();
8890
}
89-
assertEquals( 2, clusterSize( nodea ) );
91+
assertEquals( 2, clusterSize( nodea, SimpleEmail.class ) );
9092
verifyNodeSeesUpdatedIndex( nodea );
9193
verifyNodeSeesUpdatedIndex( nodeb );
9294
}
@@ -116,9 +118,11 @@ private void verifyNodeSeesUpdatedIndex(FullTextSessionBuilder node) {
116118

117119
@Before
118120
public void setUp() throws Exception {
119-
nodea = createClusterNode();
120-
nodeb = createClusterNode();
121-
waitMembersCount( nodea, 2 );
121+
entityTypes = new HashSet<Class<?>>();
122+
entityTypes.add( SimpleEmail.class );
123+
nodea = createClusterNode(entityTypes);
124+
nodeb = createClusterNode(entityTypes);
125+
waitMembersCount( nodea, SimpleEmail.class, 2 );
122126
}
123127

124128
@After
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat, Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
25+
package org.hibernate.search.infinispan.sharedIndex;
26+
27+
import javax.persistence.Column;
28+
import javax.persistence.Entity;
29+
import javax.persistence.GeneratedValue;
30+
import javax.persistence.Id;
31+
32+
import org.hibernate.search.annotations.Analyze;
33+
import org.hibernate.search.annotations.Field;
34+
35+
@Entity
36+
public abstract class Device {
37+
38+
public Device(String manufacturer, String model, String serialNumber) {
39+
this.manufacturer = manufacturer;
40+
this.model = model;
41+
this.serialNumber = serialNumber;
42+
}
43+
44+
@Id
45+
@GeneratedValue
46+
public Long id;
47+
48+
@Field(analyze=Analyze.NO)
49+
@Column(name = "mfg")
50+
public String manufacturer = "";
51+
52+
@Field(analyze=Analyze.NO)
53+
@Column(name = "model")
54+
public String model = "";
55+
56+
@Field(analyze=Analyze.NO)
57+
@Column(name = "serial_number")
58+
public String serialNumber;
59+
60+
@Override
61+
public int hashCode() {
62+
final int prime = 31;
63+
int result = 1;
64+
result = prime * result + ( ( manufacturer == null ) ? 0 : manufacturer.hashCode() );
65+
result = prime * result + ( ( model == null ) ? 0 : model.hashCode() );
66+
result = prime * result + ( ( serialNumber == null ) ? 0 : serialNumber.hashCode() );
67+
return result;
68+
}
69+
70+
@Override
71+
public boolean equals(Object obj) {
72+
if ( this == obj )
73+
return true;
74+
if ( obj == null )
75+
return false;
76+
if ( !( obj instanceof Device ) )
77+
return false;
78+
Device other = (Device) obj;
79+
80+
if ( manufacturer == null ) {
81+
if ( other.manufacturer != null )
82+
return false;
83+
}
84+
else if ( !manufacturer.equals( other.manufacturer ) )
85+
return false;
86+
if ( model == null ) {
87+
if ( other.model != null )
88+
return false;
89+
}
90+
else if ( !model.equals( other.model ) )
91+
return false;
92+
if ( serialNumber == null ) {
93+
if ( other.serialNumber != null )
94+
return false;
95+
}
96+
else if ( !serialNumber.equals( other.serialNumber ) )
97+
return false;
98+
return true;
99+
}
100+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat, Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
25+
package org.hibernate.search.infinispan.sharedIndex;
26+
27+
import javax.persistence.Entity;
28+
29+
import org.hibernate.search.annotations.Indexed;
30+
31+
@Entity
32+
@Indexed(index = "device")
33+
public class Robot extends Device {
34+
35+
public Robot() {
36+
super( "Galactic", "Infinity1000", null );
37+
}
38+
39+
public Robot(String serialNumber) {
40+
super( "Galactic", "Infinity1000", serialNumber );
41+
}
42+
}

0 commit comments

Comments
 (0)