-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ConstraintIndexCreatorTest.java
405 lines (356 loc) · 18.3 KB
/
ConstraintIndexCreatorTest.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* 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.kernel.impl.api.constraints;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.neo4j.internal.kernel.api.IndexReference;
import org.neo4j.internal.kernel.api.Kernel;
import org.neo4j.internal.kernel.api.Modes;
import org.neo4j.internal.kernel.api.SchemaRead;
import org.neo4j.internal.kernel.api.SchemaWrite;
import org.neo4j.internal.kernel.api.Session;
import org.neo4j.internal.kernel.api.TokenRead;
import org.neo4j.internal.kernel.api.Transaction;
import org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException;
import org.neo4j.internal.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.internal.kernel.api.security.LoginContext;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException;
import org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException;
import org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException;
import org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.index.PropertyAccessor;
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.api.schema.index.IndexDescriptorFactory;
import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.impl.api.KernelTransactionImplementation;
import org.neo4j.kernel.impl.api.TransactionHeaderInformation;
import org.neo4j.kernel.impl.api.index.IndexProxy;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.impl.api.state.ConstraintIndexCreator;
import org.neo4j.kernel.impl.locking.ResourceTypes;
import org.neo4j.kernel.impl.locking.SimpleStatementLocks;
import org.neo4j.kernel.impl.transaction.TransactionHeaderInformationFactory;
import org.neo4j.storageengine.api.StorageEngine;
import org.neo4j.storageengine.api.StorageReader;
import org.neo4j.values.storable.Values;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
public class ConstraintIndexCreatorTest
{
private static final int PROPERTY_KEY_ID = 456;
private static final int LABEL_ID = 123;
private static final long INDEX_ID = 0L;
private final LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel( LABEL_ID, PROPERTY_KEY_ID );
private final IndexDescriptor index = TestIndexDescriptorFactory.uniqueForLabel( LABEL_ID, PROPERTY_KEY_ID );
private final IndexReference indexReference = TestIndexDescriptorFactory.uniqueForLabel( LABEL_ID, PROPERTY_KEY_ID );
private final SchemaRead schemaRead = schemaRead();
private final SchemaWrite schemaWrite = mock( SchemaWrite.class );
private final TokenRead tokenRead = mock( TokenRead.class );
@Test
public void shouldCreateIndexInAnotherTransaction() throws Exception
{
// given
StubKernel kernel = new StubKernel();
IndexProxy indexProxy = mock( IndexProxy.class );
IndexingService indexingService = mock( IndexingService.class );
when( indexingService.getIndexProxy( INDEX_ID ) ).thenReturn( indexProxy );
when( indexingService.getIndexProxy( descriptor ) ).thenReturn( indexProxy );
when( indexProxy.getDescriptor() ).thenReturn( index.withId( INDEX_ID ).withoutCapabilities() );
PropertyAccessor propertyAccessor = mock( PropertyAccessor.class );
ConstraintIndexCreator creator =
new ConstraintIndexCreator( () -> kernel, indexingService, propertyAccessor );
// when
long indexId = creator.createUniquenessConstraintIndex( createTransaction(), descriptor, Optional.empty() );
// then
assertEquals( INDEX_ID, indexId );
verify( schemaRead ).indexGetCommittedId( indexReference );
verify( schemaRead ).index( LABEL_ID, PROPERTY_KEY_ID );
verifyNoMoreInteractions( schemaRead );
verify( indexProxy ).awaitStoreScanCompleted();
}
@Test
public void shouldDropIndexIfPopulationFails() throws Exception
{
// given
StubKernel kernel = new StubKernel();
IndexingService indexingService = mock( IndexingService.class );
IndexProxy indexProxy = mock( IndexProxy.class );
when( indexingService.getIndexProxy( INDEX_ID ) ).thenReturn( indexProxy );
when( indexingService.getIndexProxy( descriptor ) ).thenReturn( indexProxy );
when( indexProxy.getDescriptor() ).thenReturn( index.withId( INDEX_ID ).withoutCapabilities() );
IndexEntryConflictException cause = new IndexEntryConflictException( 2, 1, Values.of( "a" ) );
doThrow( new IndexPopulationFailedKernelException( descriptor, "some index", cause ) )
.when( indexProxy ).awaitStoreScanCompleted();
PropertyAccessor propertyAccessor = mock( PropertyAccessor.class );
when( schemaRead.index( anyInt(), anyInt() ) )
.thenReturn(
IndexReference.NO_INDEX ) // first claim it doesn't exist, because it doesn't... so
// that it gets created
.thenReturn( indexReference ); // then after it failed claim it does exist
ConstraintIndexCreator creator =
new ConstraintIndexCreator( () -> kernel, indexingService, propertyAccessor );
// when
KernelTransactionImplementation transaction = createTransaction();
try
{
creator.createUniquenessConstraintIndex( transaction, descriptor, Optional.empty() );
fail( "expected exception" );
}
// then
catch ( UniquePropertyValueValidationException e )
{
assertEquals( "Existing data does not satisfy CONSTRAINT ON ( label[123]:label[123] ) " +
"ASSERT label[123].property[456] IS UNIQUE.", e.getMessage() );
}
assertEquals( 2, kernel.transactions.size() );
KernelTransactionImplementation tx1 = kernel.transactions.get( 0 );
SchemaDescriptor newIndex = index.schema();
verify( tx1 ).indexUniqueCreate( eq( newIndex ), eq( Optional.empty() ) );
verify( schemaRead ).indexGetCommittedId( indexReference );
verify( schemaRead, times( 2 ) ).index( LABEL_ID, PROPERTY_KEY_ID );
verifyNoMoreInteractions( schemaRead );
TransactionState tx2 = kernel.transactions.get( 1 ).txState();
verify( tx2 ).indexDoDrop( index );
verifyNoMoreInteractions( tx2 );
}
@Test
public void shouldDropIndexInAnotherTransaction() throws Exception
{
// given
StubKernel kernel = new StubKernel();
IndexingService indexingService = mock( IndexingService.class );
PropertyAccessor propertyAccessor = mock( PropertyAccessor.class );
ConstraintIndexCreator creator =
new ConstraintIndexCreator( () -> kernel, indexingService, propertyAccessor );
// when
creator.dropUniquenessConstraintIndex( index );
// then
assertEquals( 1, kernel.transactions.size() );
verify( kernel.transactions.get( 0 ).txState() ).indexDoDrop( index );
verifyZeroInteractions( indexingService );
}
@Test
public void shouldReleaseLabelLockWhileAwaitingIndexPopulation() throws Exception
{
// given
StubKernel kernel = new StubKernel();
IndexingService indexingService = mock( IndexingService.class );
PropertyAccessor propertyAccessor = mock( PropertyAccessor.class );
when( schemaRead.indexGetCommittedId( indexReference ) ).thenReturn( INDEX_ID );
IndexProxy indexProxy = mock( IndexProxy.class );
when( indexingService.getIndexProxy( anyLong() ) ).thenReturn( indexProxy );
when( indexingService.getIndexProxy( descriptor ) ).thenReturn( indexProxy );
when( schemaRead.index( LABEL_ID, PROPERTY_KEY_ID ) ).thenReturn( IndexReference.NO_INDEX );
ConstraintIndexCreator creator =
new ConstraintIndexCreator( () -> kernel, indexingService, propertyAccessor );
// when
KernelTransactionImplementation transaction = createTransaction();
creator.createUniquenessConstraintIndex( transaction, descriptor, Optional.empty() );
// then
verify( transaction.statementLocks().pessimistic() )
.releaseExclusive( ResourceTypes.LABEL, descriptor.getLabelId() );
verify( transaction.statementLocks().pessimistic() )
.acquireExclusive( transaction.lockTracer(), ResourceTypes.LABEL, descriptor.getLabelId() );
}
@Test
public void shouldReuseExistingOrphanedConstraintIndex() throws Exception
{
// given
IndexingService indexingService = mock( IndexingService.class );
StubKernel kernel = new StubKernel();
long orphanedConstraintIndexId = 111;
when( schemaRead.indexGetCommittedId( indexReference ) ).thenReturn( orphanedConstraintIndexId );
IndexProxy indexProxy = mock( IndexProxy.class );
when( indexingService.getIndexProxy( orphanedConstraintIndexId ) ).thenReturn( indexProxy );
PropertyAccessor propertyAccessor = mock( PropertyAccessor.class );
when( schemaRead.index( LABEL_ID, PROPERTY_KEY_ID ) ).thenReturn( indexReference );
when( schemaRead.indexGetOwningUniquenessConstraintId( indexReference ) )
.thenReturn( null ); // which means it has no owner
ConstraintIndexCreator creator =
new ConstraintIndexCreator( () -> kernel, indexingService, propertyAccessor );
// when
KernelTransactionImplementation transaction = createTransaction();
long indexId = creator.createUniquenessConstraintIndex( transaction, descriptor, Optional.empty() );
// then
assertEquals( orphanedConstraintIndexId, indexId );
assertEquals( "There should have been no need to acquire a statement to create the constraint index", 0,
kernel.transactions.size() );
verify( schemaRead ).indexGetCommittedId( indexReference );
verify( schemaRead ).index( LABEL_ID, PROPERTY_KEY_ID );
verify( schemaRead ).indexGetOwningUniquenessConstraintId( indexReference );
verifyNoMoreInteractions( schemaRead );
verify( indexProxy ).awaitStoreScanCompleted();
}
@Test
public void shouldFailOnExistingOwnedConstraintIndex() throws Exception
{
// given
IndexingService indexingService = mock( IndexingService.class );
StubKernel kernel = new StubKernel();
long constraintIndexId = 111;
long constraintIndexOwnerId = 222;
when( schemaRead.indexGetCommittedId( indexReference ) ).thenReturn( constraintIndexId );
IndexProxy indexProxy = mock( IndexProxy.class );
when( indexingService.getIndexProxy( constraintIndexId ) ).thenReturn( indexProxy );
PropertyAccessor propertyAccessor = mock( PropertyAccessor.class );
when( schemaRead.index( LABEL_ID, PROPERTY_KEY_ID ) ).thenReturn( indexReference );
when( schemaRead.indexGetOwningUniquenessConstraintId( indexReference ) )
.thenReturn( constraintIndexOwnerId ); // which means there's an owner
when( tokenRead.nodeLabelName( LABEL_ID ) ).thenReturn( "MyLabel" );
when( tokenRead.propertyKeyName( PROPERTY_KEY_ID ) ).thenReturn( "MyKey" );
ConstraintIndexCreator creator =
new ConstraintIndexCreator( () -> kernel, indexingService, propertyAccessor );
// when
try
{
KernelTransactionImplementation transaction = createTransaction();
creator.createUniquenessConstraintIndex( transaction, descriptor, Optional.empty() );
fail( "Should've failed" );
}
catch ( AlreadyConstrainedException e )
{
// THEN good
}
// then
assertEquals( "There should have been no need to acquire a statement to create the constraint index", 0,
kernel.transactions.size() );
verify( schemaRead ).index( LABEL_ID, PROPERTY_KEY_ID );
verify( schemaRead ).indexGetOwningUniquenessConstraintId( indexReference );
verifyNoMoreInteractions( schemaRead );
}
@Test
public void shouldCreateConstraintIndexForSpecifiedProvider() throws Exception
{
// given
IndexingService indexingService = mock( IndexingService.class );
StubKernel kernel = new StubKernel();
when( schemaRead.indexGetCommittedId( indexReference ) ).thenReturn( INDEX_ID );
IndexProxy indexProxy = mock( IndexProxy.class );
when( indexingService.getIndexProxy( INDEX_ID ) ).thenReturn( indexProxy );
when( indexingService.getIndexProxy( descriptor ) ).thenReturn( indexProxy );
PropertyAccessor propertyAccessor = mock( PropertyAccessor.class );
ConstraintIndexCreator creator = new ConstraintIndexCreator( () -> kernel, indexingService, propertyAccessor );
IndexProvider.Descriptor providerDescriptor = new IndexProvider.Descriptor( "Groovy", "1.2" );
// when
KernelTransactionImplementation transaction = createTransaction();
creator.createUniquenessConstraintIndex( transaction, descriptor, Optional.of( providerDescriptor.name() ) );
// then
assertEquals( 1, kernel.transactions.size() );
KernelTransactionImplementation transactionInstance = kernel.transactions.get( 0 );
verify( transactionInstance ).indexUniqueCreate( eq( descriptor ), eq( Optional.of( providerDescriptor.name() ) ) );
verify( schemaRead ).index( LABEL_ID, PROPERTY_KEY_ID );
verify( schemaRead ).indexGetCommittedId( any() );
verifyNoMoreInteractions( schemaRead );
}
private class StubKernel implements Kernel, Session
{
private final List<KernelTransactionImplementation> transactions = new ArrayList<>();
@Override
public Transaction beginTransaction() throws TransactionFailureException
{
return remember( createTransaction() );
}
@Override
public Transaction beginTransaction( Transaction.Type type ) throws TransactionFailureException
{
return remember( createTransaction() );
}
private KernelTransaction remember( KernelTransactionImplementation kernelTransaction )
{
transactions.add( kernelTransaction );
return kernelTransaction;
}
@Override
public Session beginSession( LoginContext loginContext )
{
return this;
}
@Override
public Modes modes()
{
return null;
}
@Override
public void close()
{
}
}
private SchemaRead schemaRead()
{
SchemaRead schemaRead = mock( SchemaRead.class );
when( schemaRead.index( LABEL_ID, PROPERTY_KEY_ID ) ).thenReturn( IndexReference.NO_INDEX );
try
{
when( schemaRead.indexGetCommittedId( indexReference ) ).thenReturn( INDEX_ID );
}
catch ( SchemaKernelException e )
{
throw new AssertionError( e );
}
return schemaRead;
}
private KernelTransactionImplementation createTransaction()
{
KernelTransactionImplementation transaction = mock( KernelTransactionImplementation.class );
try
{
TransactionHeaderInformation headerInformation = new TransactionHeaderInformation( -1, -1, new byte[0] );
TransactionHeaderInformationFactory headerInformationFactory = mock( TransactionHeaderInformationFactory.class );
when( headerInformationFactory.create() ).thenReturn( headerInformation );
StorageEngine storageEngine = mock( StorageEngine.class );
StorageReader storageReader = mock( StorageReader.class );
when( storageEngine.newReader() ).thenReturn( storageReader );
SimpleStatementLocks locks = new SimpleStatementLocks( mock( org.neo4j.kernel.impl.locking.Locks.Client.class ) );
when( transaction.statementLocks() ).thenReturn( locks );
when( transaction.tokenRead() ).thenReturn( tokenRead );
when( transaction.schemaRead() ).thenReturn( schemaRead );
when( transaction.schemaWrite() ).thenReturn( schemaWrite );
TransactionState transactionState = mock( TransactionState.class );
when( transaction.txState() ).thenReturn( transactionState );
when( transaction.indexUniqueCreate( any( SchemaDescriptor.class ), any( Optional.class ) ) ).thenAnswer(
i -> IndexDescriptorFactory.uniqueForSchema( i.getArgument( 0 ) ) );
}
catch ( InvalidTransactionTypeKernelException e )
{
fail( "Expected write transaction" );
}
return transaction;
}
}