-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SlaveLocksClient.java
489 lines (445 loc) · 16.3 KB
/
SlaveLocksClient.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
/*
* Copyright (c) 2002-2018 "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 Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.ha.lock;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongList;
import org.neo4j.com.ComException;
import org.neo4j.com.RequestContext;
import org.neo4j.com.Response;
import org.neo4j.graphdb.TransientDatabaseFailureException;
import org.neo4j.kernel.AvailabilityGuard;
import org.neo4j.kernel.AvailabilityGuard.UnavailableException;
import org.neo4j.kernel.DeadlockDetectedException;
import org.neo4j.kernel.ha.com.RequestContextFactory;
import org.neo4j.kernel.ha.com.master.Master;
import org.neo4j.kernel.impl.locking.ActiveLock;
import org.neo4j.kernel.impl.locking.LockClientStoppedException;
import org.neo4j.kernel.impl.locking.LockTracer;
import org.neo4j.kernel.impl.locking.LockWaitEvent;
import org.neo4j.kernel.impl.locking.Locks;
import org.neo4j.kernel.impl.locking.ResourceTypes;
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;
import org.neo4j.storageengine.api.lock.AcquireLockTimeoutException;
import org.neo4j.storageengine.api.lock.ResourceType;
import static org.neo4j.kernel.impl.locking.LockType.READ;
import static org.neo4j.kernel.impl.locking.LockType.WRITE;
/**
* The slave locks client is responsible for managing locks on behalf of some actor on a slave machine. An actor
* could be a transaction or some other job that runs in the database.
* <p/>
* The client maintains a local "real" lock client, backed by some regular Locks implementation, but it also coordinates
* with the master for certain types of locks. If you grab a lock on a node, for instance, this class will grab a
* cluster-global lock by talking to the master machine, and then grab that same lock locally before returning.
*/
class SlaveLocksClient implements Locks.Client
{
private final Master master;
private final Locks.Client client;
private final Locks localLockManager;
private final RequestContextFactory requestContextFactory;
private final AvailabilityGuard availabilityGuard;
// Using atomic ints to avoid creating garbage through boxing.
private final Log log;
private boolean initialized;
private volatile boolean stopped;
SlaveLocksClient( Master master, Locks.Client local, Locks localLockManager,
RequestContextFactory requestContextFactory, AvailabilityGuard availabilityGuard, LogProvider logProvider )
{
this.master = master;
this.client = local;
this.localLockManager = localLockManager;
this.requestContextFactory = requestContextFactory;
this.availabilityGuard = availabilityGuard;
this.log = logProvider.getLog( getClass() );
}
@Override
public void acquireShared( LockTracer tracer, ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException
{
assertNotStopped();
long[] newResourceIds = firstTimeSharedLocks( resourceType, resourceIds );
if ( newResourceIds.length > 0 )
{
try
{
acquireSharedOnMasterFiltered( tracer, resourceType, newResourceIds );
}
catch ( Throwable failure )
{
if ( resourceIds != newResourceIds )
{
releaseShared( resourceType, resourceIds, newResourceIds );
}
throw failure;
}
for ( long resourceId : newResourceIds )
{
if ( !client.trySharedLock( resourceType, resourceId ) )
{
throw new LocalDeadlockDetectedException(
client, localLockManager, resourceType, resourceId, READ );
}
}
}
}
@Override
public void acquireExclusive( LockTracer tracer, ResourceType resourceType, long... resourceIds ) throws
AcquireLockTimeoutException
{
assertNotStopped();
long[] newResourceIds = firstTimeExclusiveLocks( resourceType, resourceIds );
if ( newResourceIds.length > 0 )
{
try ( LockWaitEvent event = tracer.waitForLock( true, resourceType, newResourceIds ) )
{
acquireExclusiveOnMaster( resourceType, newResourceIds );
}
catch ( Throwable failure )
{
if ( resourceIds != newResourceIds )
{
releaseExclusive( resourceType, resourceIds, newResourceIds );
}
throw failure;
}
for ( long resourceId : newResourceIds )
{
if ( !client.tryExclusiveLock( resourceType, resourceId ) )
{
throw new LocalDeadlockDetectedException(
client, localLockManager, resourceType, resourceId, WRITE );
}
}
}
}
@Override
public boolean tryExclusiveLock( ResourceType resourceType, long resourceId )
{
throw newUnsupportedDirectTryLockUsageException();
}
@Override
public boolean trySharedLock( ResourceType resourceType, long resourceId )
{
throw newUnsupportedDirectTryLockUsageException();
}
@Override
public boolean reEnterShared( ResourceType resourceType, long resourceId )
{
throw new UnsupportedOperationException();
}
@Override
public boolean reEnterExclusive( ResourceType resourceType, long resourceId )
{
throw new UnsupportedOperationException();
}
@Override
public void releaseShared( ResourceType resourceType, long... resourceIds )
{
assertNotStopped();
client.releaseShared( resourceType, resourceIds );
}
@Override
public void releaseExclusive( ResourceType resourceType, long... resourceIds )
{
assertNotStopped();
client.releaseExclusive( resourceType, resourceIds );
}
@Override
public void prepare()
{
client.prepare();
}
@Override
public void stop()
{
client.stop();
stopLockSessionOnMaster();
stopped = true;
}
@Override
public void close()
{
client.close();
if ( initialized )
{
if ( !stopped )
{
closeLockSessionOnMaster();
stopped = true;
}
initialized = false;
}
}
@Override
public int getLockSessionId()
{
assertNotStopped();
return initialized ? client.getLockSessionId() : -1;
}
@Override
public Stream<? extends ActiveLock> activeLocks()
{
return client.activeLocks();
}
@Override
public long activeLockCount()
{
return client.activeLockCount();
}
/**
* In order to prevent various indexes collisions on master during transaction commit that originate on one of the
* slaves we need to grab same locks on {@link ResourceTypes#LABEL} and {@link ResourceTypes#RELATIONSHIP_TYPE}
* that
* where obtained on origin. To be able to do that and also prevent shared locks to be propagates to master in cases
* of
* read only transactions we need to postpone obtaining them till we know that we participating in a
* transaction that performs modifications.
*
* @param tracer lock tracer
*/
void acquireDeferredSharedLocks( LockTracer tracer )
{
assertNotStopped();
Map<ResourceType,PrimitiveLongList> deferredLocksMap = new HashMap<>();
List<? extends ActiveLock> activeLocks = client.activeLocks()
.filter( activeLock -> ActiveLock.SHARED_MODE.equals( activeLock.mode() ) )
.filter( this::isLabelOrRelationshipType ).collect( Collectors.toList() );
for ( ActiveLock activeLock : activeLocks )
{
Function<ResourceType,PrimitiveLongList> listCreator = resourceType -> Primitive.longList();
deferredLocksMap.computeIfAbsent( activeLock.resourceType(), listCreator )
.add( activeLock.resourceId() );
}
deferredLocksMap.forEach( ( type, ids ) -> lockResourcesOnMaster( tracer, type, ids ) );
}
private void lockResourcesOnMaster( LockTracer tracer, ResourceType type, PrimitiveLongList ids )
{
long[] resourceIds = ids.toArray();
try ( LockWaitEvent event = tracer.waitForLock( false, type, resourceIds ) )
{
acquireSharedOnMaster( type, resourceIds );
}
}
private boolean isLabelOrRelationshipType( ActiveLock activeLock )
{
return (activeLock.resourceType() == ResourceTypes.LABEL) ||
(activeLock.resourceType() == ResourceTypes.RELATIONSHIP_TYPE);
}
private void stopLockSessionOnMaster()
{
try
{
endLockSessionOnMaster( false );
}
catch ( Throwable t )
{
log.warn( "Unable to stop lock session on master", t );
}
}
private void closeLockSessionOnMaster()
{
endLockSessionOnMaster( true );
}
private void endLockSessionOnMaster( boolean success )
{
try ( Response<Void> ignored = master.endLockSession( newRequestContextFor( client ), success ) )
{
// Lock session is closed on master at this point
}
catch ( ComException e )
{
throw new DistributedLockFailureException(
"Failed to end the lock session on the master (which implies releasing all held locks)",
master, e );
}
}
private long[] firstTimeSharedLocks( ResourceType resourceType, long[] resourceIds )
{
int cursor = 0;
for ( int i = 0; i < resourceIds.length; i++ )
{
if ( !client.reEnterShared( resourceType, resourceIds[i] ) )
{
resourceIds[cursor++] = resourceIds[i];
}
}
if ( cursor == 0 )
{
return PrimitiveLongCollections.EMPTY_LONG_ARRAY;
}
return cursor == resourceIds.length ? resourceIds : Arrays.copyOf( resourceIds, cursor );
}
private long[] firstTimeExclusiveLocks( ResourceType resourceType, long[] resourceIds )
{
int cursor = 0;
for ( int i = 0; i < resourceIds.length; i++ )
{
if ( !client.reEnterExclusive( resourceType, resourceIds[i] ) )
{
resourceIds[cursor++] = resourceIds[i];
}
}
if ( cursor == 0 )
{
return PrimitiveLongCollections.EMPTY_LONG_ARRAY;
}
return cursor == resourceIds.length ? resourceIds : Arrays.copyOf( resourceIds, cursor );
}
private void releaseShared( ResourceType resourceType, long[] resourceIds, long[] excludedIds )
{
for ( int i = 0, j = 0; i < resourceIds.length; i++ )
{
if ( resourceIds[i] == excludedIds[j] )
{
j++;
}
else
{
client.releaseShared( resourceType, resourceIds[i] );
}
}
}
private void releaseExclusive( ResourceType resourceType, long[] resourceIds, long[] excludedIds )
{
for ( int i = 0, j = 0; i < resourceIds.length; i++ )
{
if ( resourceIds[i] == excludedIds[j] )
{
j++;
}
else
{
client.releaseShared( resourceType, resourceIds[i] );
}
}
}
private void acquireSharedOnMasterFiltered( LockTracer lockTracer, ResourceType resourceType, long... resourceIds )
{
if ( (resourceType == ResourceTypes.INDEX_ENTRY) ||
(resourceType == ResourceTypes.LABEL) ||
(resourceType == ResourceTypes.RELATIONSHIP_TYPE) )
{
return;
}
try ( LockWaitEvent event = lockTracer.waitForLock( false, resourceType, resourceIds ) )
{
acquireSharedOnMaster( resourceType, resourceIds );
}
}
private void acquireSharedOnMaster( ResourceType resourceType, long[] resourceIds )
{
makeSureTxHasBeenInitialized();
RequestContext requestContext = newRequestContextFor( this );
try ( Response<LockResult> response = master.acquireSharedLock( requestContext, resourceType, resourceIds ) )
{
receiveLockResponse( response );
}
catch ( ComException e )
{
throw new DistributedLockFailureException( "Cannot get shared lock(s) on master", master, e );
}
}
private void acquireExclusiveOnMaster( ResourceType resourceType, long... resourceIds )
{
makeSureTxHasBeenInitialized();
RequestContext requestContext = newRequestContextFor( this );
try ( Response<LockResult> response = master.acquireExclusiveLock( requestContext, resourceType, resourceIds ) )
{
receiveLockResponse( response );
}
catch ( ComException e )
{
throw new DistributedLockFailureException( "Cannot get exclusive lock(s) on master", master, e );
}
}
private void receiveLockResponse( Response<LockResult> response )
{
LockResult result = response.response();
switch ( result.getStatus() )
{
case DEAD_LOCKED:
throw new DeadlockDetectedException( result.getMessage() );
case NOT_LOCKED:
throw new UnsupportedOperationException( result.toString() );
case OK_LOCKED:
break;
default:
throw new UnsupportedOperationException( result.toString() );
}
}
private void makeSureTxHasBeenInitialized()
{
try
{
availabilityGuard.checkAvailable();
}
catch ( UnavailableException e )
{
throw new TransientDatabaseFailureException( "Database not available", e );
}
if ( !initialized )
{
try ( Response<Void> ignored = master.newLockSession( newRequestContextFor( client ) ) )
{
// Lock session is initialized on master at this point
}
catch ( Exception exception )
{
// Temporary wrapping, we should review the exception structure of the Locks API to allow this to
// not use runtime exceptions here.
ComException e;
if ( exception instanceof ComException )
{
e = (ComException) exception;
}
else
{
e = new ComException( exception );
}
throw new DistributedLockFailureException( "Failed to start a new lock session on master", master, e );
}
initialized = true;
}
}
private RequestContext newRequestContextFor( Locks.Client client )
{
return requestContextFactory.newRequestContext( client.getLockSessionId() );
}
private void assertNotStopped()
{
if ( stopped )
{
throw new LockClientStoppedException( this );
}
}
private UnsupportedOperationException newUnsupportedDirectTryLockUsageException()
{
return new UnsupportedOperationException(
"Distributed tryLocks are not supported. They only work with local lock managers." );
}
}