-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NonUniqueIndexTest.java
195 lines (180 loc) · 8.36 KB
/
NonUniqueIndexTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.index.impl.lucene.explicit;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.util.List;
import java.util.concurrent.locks.LockSupport;
import org.neo4j.collection.PrimitiveLongCollections;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.facade.GraphDatabaseFacadeFactory;
import org.neo4j.graphdb.factory.GraphDatabaseFactoryState;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.factory.module.CommunityEditionModule;
import org.neo4j.graphdb.factory.module.PlatformModule;
import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector;
import org.neo4j.internal.kernel.api.IndexQuery;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.api.impl.schema.LuceneIndexProviderFactory;
import org.neo4j.kernel.api.impl.schema.NativeLuceneFusionIndexProviderFactory10;
import org.neo4j.kernel.api.impl.schema.NativeLuceneFusionIndexProviderFactory20;
import org.neo4j.kernel.api.index.IndexAccessor;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
import org.neo4j.kernel.impl.factory.DatabaseInfo;
import org.neo4j.kernel.impl.factory.OperationalMode;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.logging.NullLogService;
import org.neo4j.kernel.impl.scheduler.CentralJobScheduler;
import org.neo4j.logging.LogProvider;
import org.neo4j.scheduler.Group;
import org.neo4j.scheduler.JobHandle;
import org.neo4j.storageengine.api.schema.IndexReader;
import org.neo4j.test.rule.PageCacheAndDependenciesRule;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import static java.util.Collections.singletonList;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.SchemaIndex.LUCENE10;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.SchemaIndex.NATIVE10;
import static org.neo4j.kernel.api.schema.SchemaDescriptorFactory.forLabel;
import static org.neo4j.kernel.api.schema.index.IndexDescriptorFactory.forSchema;
public class NonUniqueIndexTest
{
@Rule
public PageCacheAndDependenciesRule resources =
new PageCacheAndDependenciesRule( DefaultFileSystemRule::new, NonUniqueIndexTest.class );
@Test
public void concurrentIndexPopulationAndInsertsShouldNotProduceDuplicates() throws Exception
{
// Given
Config config = Config.defaults();
GraphDatabaseService db = newEmbeddedGraphDatabaseWithSlowJobScheduler( config );
// When
try ( Transaction tx = db.beginTx() )
{
db.schema().indexFor( label( "SomeLabel" ) ).on( "key" ).create();
tx.success();
}
Node node;
try ( Transaction tx = db.beginTx() )
{
node = db.createNode( label( "SomeLabel" ) );
node.setProperty( "key", "value" );
tx.success();
}
// Await index population before shutdown, because db.shutdown won't await it. After shutdown this test verifies
// the index directly through the index provider so it must have been populated for that to work properly.
try ( Transaction tx = db.beginTx() )
{
db.schema().awaitIndexesOnline( 1, MINUTES );
tx.success();
}
db.shutdown();
// Then
assertThat( nodeIdsInIndex( config, 1, "value" ), equalTo( singletonList( node.getId() ) ) );
}
private GraphDatabaseService newEmbeddedGraphDatabaseWithSlowJobScheduler( Config config )
{
GraphDatabaseFactoryState graphDatabaseFactoryState = new GraphDatabaseFactoryState();
graphDatabaseFactoryState.setUserLogProvider( NullLogService.getInstance().getUserLogProvider() );
return new GraphDatabaseFacadeFactory( DatabaseInfo.COMMUNITY, CommunityEditionModule::new )
{
@Override
protected PlatformModule createPlatform( File storeDir, Config config, Dependencies dependencies )
{
return new PlatformModule( storeDir, config, databaseInfo, dependencies )
{
@Override
protected CentralJobScheduler createJobScheduler()
{
return newSlowJobScheduler();
}
@Override
protected LogService createLogService( LogProvider userLogProvider )
{
return NullLogService.getInstance();
}
};
}
}.newFacade( resources.directory().storeDir(), config,
graphDatabaseFactoryState.databaseDependencies() );
}
private static CentralJobScheduler newSlowJobScheduler()
{
return new CentralJobScheduler()
{
@Override
public JobHandle schedule( Group group, Runnable job )
{
return super.schedule( group, slowRunnable( job ) );
}
};
}
private static Runnable slowRunnable( final Runnable target )
{
return () ->
{
LockSupport.parkNanos( 100_000_000L );
target.run();
};
}
private List<Long> nodeIdsInIndex( Config config, int indexId, String value ) throws Exception
{
PageCache pageCache = resources.pageCache();
File databaseDirectory = resources.directory().databaseDir();
FileSystemAbstraction fs = resources.fileSystem();
IndexProvider.Monitor monitor = IndexProvider.Monitor.EMPTY;
OperationalMode operationalMode = OperationalMode.single;
IndexProvider indexProvider = selectIndexProvider( pageCache, databaseDirectory, fs, monitor, config, operationalMode );
IndexSamplingConfig samplingConfig = new IndexSamplingConfig( config );
try ( IndexAccessor accessor = indexProvider.getOnlineAccessor(
forSchema( forLabel( 0, 0 ), indexProvider.getProviderDescriptor() ).withId( indexId ), samplingConfig );
IndexReader reader = accessor.newReader() )
{
return PrimitiveLongCollections.asList( reader.query( IndexQuery.exact( 1, value ) ) );
}
}
private static IndexProvider selectIndexProvider( PageCache pageCache, File databaseDirectory, FileSystemAbstraction fs, IndexProvider.Monitor monitor,
Config config, OperationalMode operationalMode )
{
String defaultSchemaProvider = config.get( GraphDatabaseSettings.default_schema_provider );
RecoveryCleanupWorkCollector recoveryCleanupWorkCollector = RecoveryCleanupWorkCollector.IMMEDIATE;
if ( LUCENE10.providerIdentifier().equals( defaultSchemaProvider ) )
{
return LuceneIndexProviderFactory
.newInstance( pageCache, databaseDirectory, fs, monitor, config, operationalMode, recoveryCleanupWorkCollector );
}
else if ( NATIVE10.providerIdentifier().equals( defaultSchemaProvider ) )
{
return NativeLuceneFusionIndexProviderFactory10
.create( pageCache, databaseDirectory, fs, monitor, config, operationalMode, recoveryCleanupWorkCollector );
}
return NativeLuceneFusionIndexProviderFactory20
.create( pageCache, databaseDirectory, fs, monitor, config, operationalMode, recoveryCleanupWorkCollector );
}
}