-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LockingStatementOperations.java
572 lines (511 loc) · 22.8 KB
/
LockingStatementOperations.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.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;
import org.apache.commons.lang3.mutable.MutableInt;
import java.util.Iterator;
import java.util.function.Function;
import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema.RelationshipPropertyDescriptor;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.constraints.NodePropertyConstraint;
import org.neo4j.kernel.api.constraints.NodePropertyExistenceConstraint;
import org.neo4j.kernel.api.constraints.PropertyConstraint;
import org.neo4j.kernel.api.constraints.RelationshipPropertyConstraint;
import org.neo4j.kernel.api.constraints.RelationshipPropertyExistenceConstraint;
import org.neo4j.kernel.api.constraints.UniquenessConstraint;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException;
import org.neo4j.kernel.api.exceptions.KernelException;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.legacyindex.AutoIndexingKernelException;
import org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException;
import org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException;
import org.neo4j.kernel.api.exceptions.schema.ConstraintValidationKernelException;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException;
import org.neo4j.kernel.api.exceptions.schema.DropIndexFailureException;
import org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.properties.DefinedProperty;
import org.neo4j.kernel.api.properties.Property;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.impl.api.operations.EntityReadOperations;
import org.neo4j.kernel.impl.api.operations.EntityWriteOperations;
import org.neo4j.kernel.impl.api.operations.LockOperations;
import org.neo4j.kernel.impl.api.operations.SchemaReadOperations;
import org.neo4j.kernel.impl.api.operations.SchemaStateOperations;
import org.neo4j.kernel.impl.api.operations.SchemaWriteOperations;
import org.neo4j.kernel.impl.locking.ResourceTypes;
import org.neo4j.storageengine.api.lock.ResourceType;
import org.neo4j.storageengine.api.schema.PopulationProgress;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static org.neo4j.kernel.impl.locking.ResourceTypes.schemaResource;
import static org.neo4j.unsafe.impl.internal.dragons.FeatureToggles.flag;
public class LockingStatementOperations implements
EntityWriteOperations,
SchemaReadOperations,
SchemaWriteOperations,
SchemaStateOperations,
LockOperations
{
private static final boolean SCHEMA_WRITES_DISABLE =
flag( LockingStatementOperations.class, "schemaWritesDisable", false );
private final EntityReadOperations entityReadDelegate;
private final EntityWriteOperations entityWriteDelegate;
private final SchemaReadOperations schemaReadDelegate;
private final SchemaWriteOperations schemaWriteDelegate;
private final SchemaStateOperations schemaStateDelegate;
public LockingStatementOperations(
EntityReadOperations entityReadDelegate,
EntityWriteOperations entityWriteDelegate,
SchemaReadOperations schemaReadDelegate,
SchemaWriteOperations schemaWriteDelegate,
SchemaStateOperations schemaStateDelegate )
{
this.entityReadDelegate = entityReadDelegate;
this.entityWriteDelegate = entityWriteDelegate;
this.schemaReadDelegate = schemaReadDelegate;
this.schemaWriteDelegate = schemaWriteDelegate;
this.schemaStateDelegate = schemaStateDelegate;
}
@Override
public boolean nodeAddLabel( KernelStatement state, long nodeId, int labelId )
throws ConstraintValidationKernelException, EntityNotFoundException
{
// TODO (BBC, 22/11/13):
// In order to enforce constraints we need to check whether this change violates constraints; we therefore need
// the schema lock to ensure that our view of constraints is consistent.
//
// We would like this locking to be done naturally when ConstraintEnforcingEntityOperations calls
// SchemaReadOperations#constraintsGetForLabel, but the SchemaReadOperations object that
// ConstraintEnforcingEntityOperations has a reference to does not lock because of the way the cake is
// constructed.
//
// It would be cleaner if the schema and data cakes were separated so that the SchemaReadOperations object used
// by ConstraintEnforcingEntityOperations included the full cake, with locking included.
acquireSharedSchemaLock( state );
acquireExclusiveNodeLock( state, nodeId );
state.assertOpen();
return entityWriteDelegate.nodeAddLabel( state, nodeId, labelId );
}
@Override
public boolean nodeRemoveLabel( KernelStatement state, long nodeId, int labelId ) throws EntityNotFoundException
{
acquireExclusiveNodeLock( state, nodeId );
state.assertOpen();
return entityWriteDelegate.nodeRemoveLabel( state, nodeId, labelId );
}
@Override
public NewIndexDescriptor indexCreate( KernelStatement state, NodePropertyDescriptor descriptor )
throws AlreadyIndexedException, AlreadyConstrainedException
{
acquireExclusiveSchemaLock( state );
state.assertOpen();
return schemaWriteDelegate.indexCreate( state, descriptor );
}
@Override
public void indexDrop( KernelStatement state, NewIndexDescriptor descriptor ) throws DropIndexFailureException
{
acquireExclusiveSchemaLock( state );
state.assertOpen();
schemaWriteDelegate.indexDrop( state, descriptor );
}
@Override
public void uniqueIndexDrop( KernelStatement state, NewIndexDescriptor descriptor ) throws DropIndexFailureException
{
acquireExclusiveSchemaLock( state );
state.assertOpen();
schemaWriteDelegate.uniqueIndexDrop( state, descriptor );
}
@Override
public <K, V> V schemaStateGetOrCreate( KernelStatement state, K key, Function<K,V> creator )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaStateDelegate.schemaStateGetOrCreate( state, key, creator );
}
@Override
public <K> boolean schemaStateContains( KernelStatement state, K key )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaStateDelegate.schemaStateContains( state, key );
}
@Override
public void schemaStateFlush( KernelStatement state )
{
acquireSharedSchemaLock( state );
state.assertOpen();
schemaStateDelegate.schemaStateFlush( state );
}
@Override
public Iterator<NewIndexDescriptor> indexesGetForLabel( KernelStatement state, int labelId )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.indexesGetForLabel( state, labelId );
}
@Override
public NewIndexDescriptor indexGetForLabelAndPropertyKey( KernelStatement state, NodePropertyDescriptor descriptor )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.indexGetForLabelAndPropertyKey( state, descriptor );
}
@Override
public Iterator<NewIndexDescriptor> indexesGetAll( KernelStatement state )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.indexesGetAll( state );
}
@Override
public InternalIndexState indexGetState( KernelStatement state, NewIndexDescriptor descriptor )
throws IndexNotFoundKernelException
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.indexGetState( state, descriptor );
}
@Override
public PopulationProgress indexGetPopulationProgress( KernelStatement state, NewIndexDescriptor descriptor )
throws IndexNotFoundKernelException
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.indexGetPopulationProgress( state, descriptor );
}
@Override
public long indexSize( KernelStatement state, NewIndexDescriptor descriptor ) throws IndexNotFoundKernelException
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.indexSize( state, descriptor );
}
@Override
public double indexUniqueValuesPercentage( KernelStatement state,
NewIndexDescriptor descriptor ) throws IndexNotFoundKernelException
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.indexUniqueValuesPercentage( state, descriptor );
}
@Override
public Long indexGetOwningUniquenessConstraintId( KernelStatement state, NewIndexDescriptor index ) throws SchemaRuleNotFoundException
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.indexGetOwningUniquenessConstraintId( state, index );
}
@Override
public long indexGetCommittedId( KernelStatement state, NewIndexDescriptor index )
throws SchemaRuleNotFoundException
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.indexGetCommittedId( state, index );
}
@Override
public Iterator<NewIndexDescriptor> uniqueIndexesGetForLabel( KernelStatement state, int labelId )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.uniqueIndexesGetForLabel( state, labelId );
}
@Override
public Iterator<NewIndexDescriptor> uniqueIndexesGetAll( KernelStatement state )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.uniqueIndexesGetAll( state );
}
@Override
public void nodeDelete( KernelStatement state, long nodeId )
throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
acquireExclusiveNodeLock( state, nodeId );
state.assertOpen();
entityWriteDelegate.nodeDelete( state, nodeId );
}
@Override
public int nodeDetachDelete( final KernelStatement state, final long nodeId ) throws KernelException
{
final MutableInt count = new MutableInt();
TwoPhaseNodeForRelationshipLocking locking = new TwoPhaseNodeForRelationshipLocking( entityReadDelegate,
relId -> {
state.assertOpen();
try
{
entityWriteDelegate.relationshipDelete( state, relId );
count.increment();
}
catch ( EntityNotFoundException e )
{
// it doesn't matter...
}
} );
locking.lockAllNodesAndConsumeRelationships( nodeId, state );
state.assertOpen();
entityWriteDelegate.nodeDetachDelete( state, nodeId );
return count.intValue();
}
@Override
public long nodeCreate( KernelStatement statement )
{
return entityWriteDelegate.nodeCreate( statement );
}
@Override
public long relationshipCreate( KernelStatement state,
int relationshipTypeId,
long startNodeId,
long endNodeId )
throws EntityNotFoundException
{
acquireSharedSchemaLock( state );
lockRelationshipNodes( state, startNodeId, endNodeId );
return entityWriteDelegate.relationshipCreate( state, relationshipTypeId, startNodeId, endNodeId );
}
@Override
public void relationshipDelete( final KernelStatement state, long relationshipId )
throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
entityReadDelegate.relationshipVisit(state, relationshipId, new RelationshipVisitor<RuntimeException>() {
@Override
public void visit(long relId, int type, long startNode, long endNode) {
lockRelationshipNodes(state, startNode, endNode);
}
});
acquireExclusiveRelationshipLock( state, relationshipId );
state.assertOpen();
entityWriteDelegate.relationshipDelete(state, relationshipId);
}
private void lockRelationshipNodes( KernelStatement state, long startNodeId, long endNodeId )
{
// Order the locks to lower the risk of deadlocks with other threads creating/deleting rels concurrently
acquireExclusiveNodeLock( state, min( startNodeId, endNodeId ) );
if ( startNodeId != endNodeId )
{
acquireExclusiveNodeLock( state, max( startNodeId, endNodeId ) );
}
}
@Override
public UniquenessConstraint uniquePropertyConstraintCreate( KernelStatement state,NodePropertyDescriptor descriptor )
throws CreateConstraintFailureException, AlreadyConstrainedException, AlreadyIndexedException
{
acquireExclusiveSchemaLock( state );
state.assertOpen();
return schemaWriteDelegate.uniquePropertyConstraintCreate( state, descriptor );
}
@Override
public NodePropertyExistenceConstraint nodePropertyExistenceConstraintCreate( KernelStatement state,
NodePropertyDescriptor descriptor ) throws AlreadyConstrainedException, CreateConstraintFailureException
{
acquireExclusiveSchemaLock( state );
state.assertOpen();
return schemaWriteDelegate.nodePropertyExistenceConstraintCreate( state, descriptor );
}
@Override
public RelationshipPropertyExistenceConstraint relationshipPropertyExistenceConstraintCreate( KernelStatement state,
RelationshipPropertyDescriptor descriptor ) throws AlreadyConstrainedException, CreateConstraintFailureException
{
acquireExclusiveSchemaLock( state );
state.assertOpen();
return schemaWriteDelegate.relationshipPropertyExistenceConstraintCreate( state, descriptor );
}
@Override
public Iterator<NodePropertyConstraint> constraintsGetForLabelAndPropertyKey( KernelStatement state,
NodePropertyDescriptor descriptor )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.constraintsGetForLabelAndPropertyKey( state, descriptor );
}
@Override
public Iterator<NodePropertyConstraint> constraintsGetForLabel( KernelStatement state, int labelId )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.constraintsGetForLabel( state, labelId );
}
@Override
public Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipTypeAndPropertyKey(
KernelStatement state,
RelationshipPropertyDescriptor descriptor )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.constraintsGetForRelationshipTypeAndPropertyKey( state, descriptor );
}
@Override
public Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipType( KernelStatement state,
int typeId )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.constraintsGetForRelationshipType( state, typeId );
}
@Override
public Iterator<PropertyConstraint> constraintsGetAll( KernelStatement state )
{
acquireSharedSchemaLock( state );
state.assertOpen();
return schemaReadDelegate.constraintsGetAll( state );
}
@Override
public void constraintDrop( KernelStatement state, NodePropertyConstraint constraint )
throws DropConstraintFailureException
{
acquireExclusiveSchemaLock( state );
state.assertOpen();
schemaWriteDelegate.constraintDrop( state, constraint );
}
@Override
public void constraintDrop( KernelStatement state, RelationshipPropertyConstraint constraint )
throws DropConstraintFailureException
{
acquireExclusiveSchemaLock( state );
state.assertOpen();
schemaWriteDelegate.constraintDrop( state, constraint );
}
@Override
public Property nodeSetProperty( KernelStatement state, long nodeId, DefinedProperty property )
throws ConstraintValidationKernelException, EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
// TODO (BBC, 22/11/13):
// In order to enforce constraints we need to check whether this change violates constraints; we therefore need
// the schema lock to ensure that our view of constraints is consistent.
//
// We would like this locking to be done naturally when ConstraintEnforcingEntityOperations calls
// SchemaReadOperations#constraintsGetForLabel, but the SchemaReadOperations object that
// ConstraintEnforcingEntityOperations has a reference to does not lock because of the way the cake is
// constructed.
//
// It would be cleaner if the schema and data cakes were separated so that the SchemaReadOperations object used
// by ConstraintEnforcingEntityOperations included the full cake, with locking included.
acquireSharedSchemaLock( state );
acquireExclusiveNodeLock( state, nodeId );
state.assertOpen();
return entityWriteDelegate.nodeSetProperty( state, nodeId, property );
}
@Override
public Property nodeRemoveProperty( KernelStatement state, long nodeId, int propertyKeyId )
throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
acquireExclusiveNodeLock( state, nodeId );
state.assertOpen();
return entityWriteDelegate.nodeRemoveProperty( state, nodeId, propertyKeyId );
}
@Override
public Property relationshipSetProperty( KernelStatement state,
long relationshipId,
DefinedProperty property ) throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
acquireExclusiveRelationshipLock( state, relationshipId );
state.assertOpen();
return entityWriteDelegate.relationshipSetProperty( state, relationshipId, property );
}
@Override
public Property relationshipRemoveProperty( KernelStatement state,
long relationshipId,
int propertyKeyId ) throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
acquireExclusiveRelationshipLock( state, relationshipId );
state.assertOpen();
return entityWriteDelegate.relationshipRemoveProperty( state, relationshipId, propertyKeyId );
}
@Override
public Property graphSetProperty( KernelStatement state, DefinedProperty property )
{
state.locks().optimistic().acquireExclusive( state.lockTracer(), ResourceTypes.GRAPH_PROPS, ResourceTypes.graphPropertyResource() );
state.assertOpen();
return entityWriteDelegate.graphSetProperty( state, property );
}
@Override
public Property graphRemoveProperty( KernelStatement state, int propertyKeyId )
{
state.locks().optimistic().acquireExclusive( state.lockTracer(), ResourceTypes.GRAPH_PROPS, ResourceTypes.graphPropertyResource() );
state.assertOpen();
return entityWriteDelegate.graphRemoveProperty( state, propertyKeyId );
}
@Override
public void acquireExclusive( KernelStatement state, ResourceType resourceType, long resourceId )
{
state.locks().pessimistic().acquireExclusive( state.lockTracer(), resourceType, resourceId );
state.assertOpen();
}
@Override
public void acquireShared( KernelStatement state, ResourceType resourceType, long resourceId )
{
state.locks().pessimistic().acquireShared( state.lockTracer(), resourceType, resourceId );
state.assertOpen();
}
@Override
public void releaseExclusive( KernelStatement state, ResourceType type, long resourceId )
{
state.locks().pessimistic().releaseExclusive( type, resourceId );
state.assertOpen();
}
@Override
public void releaseShared( KernelStatement state, ResourceType type, long resourceId )
{
state.locks().pessimistic().releaseShared( type, resourceId );
state.assertOpen();
}
// === TODO Below is unnecessary delegate methods
@Override
public String indexGetFailure( Statement state, NewIndexDescriptor descriptor )
throws IndexNotFoundKernelException
{
return schemaReadDelegate.indexGetFailure( state, descriptor );
}
private void acquireExclusiveNodeLock( KernelStatement state, long nodeId )
{
if ( !state.hasTxStateWithChanges() || !state.txState().nodeIsAddedInThisTx( nodeId ) )
{
state.locks().optimistic().acquireExclusive( state.lockTracer(), ResourceTypes.NODE, nodeId );
}
}
private void acquireExclusiveRelationshipLock( KernelStatement state, long relationshipId )
{
if ( !state.hasTxStateWithChanges() || !state.txState().relationshipIsAddedInThisTx( relationshipId ) )
{
state.locks().optimistic().acquireExclusive( state.lockTracer(), ResourceTypes.RELATIONSHIP, relationshipId );
}
}
private void acquireSharedSchemaLock( KernelStatement state )
{
if ( !SCHEMA_WRITES_DISABLE )
{
state.locks().optimistic().acquireShared( state.lockTracer(), ResourceTypes.SCHEMA, schemaResource() );
}
}
private void acquireExclusiveSchemaLock( KernelStatement state )
{
if ( SCHEMA_WRITES_DISABLE )
{
throw new IllegalStateException( "Schema modifications have been disabled via feature toggle" );
}
else
{
state.locks().optimistic().acquireExclusive( state.lockTracer(), ResourceTypes.SCHEMA, schemaResource() );
}
}
}