Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It throws when you try to take exclusive locks to catch misusage early. This should generally be writing queries and if they are aimed at a read replica then we want to catch and throw early. If someone uses an exclusive lock in a reading query then they should fix that and move over to use a shared lock instead.
- Loading branch information
1 parent
bb7cbab
commit 4fe8727
Showing
5 changed files
with
213 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...ustering/src/main/java/org/neo4j/causalclustering/readreplica/ReadReplicaLockManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,125 @@ | |||
/* | |||
* 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 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.causalclustering.readreplica; | |||
|
|||
import java.util.stream.Stream; | |||
|
|||
import org.neo4j.kernel.api.exceptions.ReadOnlyDbException; | |||
import org.neo4j.kernel.impl.locking.ActiveLock; | |||
import org.neo4j.kernel.impl.locking.LockTracer; | |||
import org.neo4j.kernel.impl.locking.Locks; | |||
import org.neo4j.storageengine.api.lock.AcquireLockTimeoutException; | |||
import org.neo4j.storageengine.api.lock.ResourceType; | |||
|
|||
public class ReadReplicaLockManager implements Locks | |||
{ | |||
@Override | |||
public Locks.Client newClient() | |||
{ | |||
return new Client(); | |||
} | |||
|
|||
@Override | |||
public void accept( Visitor visitor ) | |||
{ | |||
} | |||
|
|||
@Override | |||
public void close() | |||
{ | |||
} | |||
|
|||
private class Client implements Locks.Client | |||
{ | |||
@Override | |||
public void acquireShared( LockTracer tracer, ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException | |||
{ | |||
} | |||
|
|||
@Override | |||
public void acquireExclusive( LockTracer tracer, ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException | |||
{ | |||
throw new RuntimeException( new ReadOnlyDbException() ); | |||
} | |||
|
|||
@Override | |||
public boolean tryExclusiveLock( ResourceType resourceType, long resourceId ) | |||
{ | |||
throw new RuntimeException( new ReadOnlyDbException() ); | |||
} | |||
|
|||
@Override | |||
public boolean trySharedLock( ResourceType resourceType, long resourceId ) | |||
{ | |||
return false; | |||
} | |||
|
|||
@Override | |||
public boolean reEnterShared( ResourceType resourceType, long resourceId ) | |||
{ | |||
return false; | |||
} | |||
|
|||
@Override | |||
public boolean reEnterExclusive( ResourceType resourceType, long resourceId ) | |||
{ | |||
throw new IllegalStateException( "Should never happen" ); | |||
} | |||
|
|||
@Override | |||
public void releaseShared( ResourceType resourceType, long resourceId ) | |||
{ | |||
} | |||
|
|||
@Override | |||
public void releaseExclusive( ResourceType resourceType, long resourceId ) | |||
{ | |||
throw new IllegalStateException( "Should never happen" ); | |||
} | |||
|
|||
@Override | |||
public void stop() | |||
{ | |||
} | |||
|
|||
@Override | |||
public void close() | |||
{ | |||
} | |||
|
|||
@Override | |||
public int getLockSessionId() | |||
{ | |||
return 0; | |||
} | |||
|
|||
@Override | |||
public Stream<? extends ActiveLock> activeLocks() | |||
{ | |||
return Stream.empty(); | |||
} | |||
|
|||
@Override | |||
public long activeLockCount() | |||
{ | |||
return 0; | |||
} | |||
} | |||
} |
71 changes: 71 additions & 0 deletions
71
...ring/src/test/java/org/neo4j/causalclustering/readreplica/ReadReplicaLockManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,71 @@ | |||
/* | |||
* 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 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.causalclustering.readreplica; | |||
|
|||
import org.junit.Test; | |||
|
|||
import org.neo4j.kernel.impl.locking.Locks; | |||
|
|||
import static org.junit.Assert.assertEquals; | |||
import static org.neo4j.kernel.api.exceptions.Status.General.ForbiddenOnReadOnlyDatabase; | |||
import static org.neo4j.kernel.api.exceptions.Status.statusCodeOf; | |||
import static org.neo4j.kernel.impl.locking.LockTracer.NONE; | |||
import static org.neo4j.kernel.impl.locking.ResourceTypes.NODE; | |||
|
|||
public class ReadReplicaLockManagerTest | |||
{ | |||
private ReadReplicaLockManager lockManager = new ReadReplicaLockManager(); | |||
private Locks.Client lockClient = lockManager.newClient(); | |||
|
|||
@Test | |||
public void shouldThrowOnAcquireExclusiveLock() throws Exception | |||
{ | |||
try | |||
{ | |||
// when | |||
lockClient.acquireExclusive( NONE, NODE, 1 ); | |||
} | |||
catch ( Exception e ) | |||
{ | |||
assertEquals( ForbiddenOnReadOnlyDatabase, statusCodeOf( e ) ); | |||
} | |||
} | |||
|
|||
@Test | |||
public void shouldThrowOnTryAcquireExclusiveLock() throws Exception | |||
{ | |||
try | |||
{ | |||
// when | |||
lockClient.tryExclusiveLock( NODE, 1 ); | |||
} | |||
catch ( Exception e ) | |||
{ | |||
assertEquals( ForbiddenOnReadOnlyDatabase, statusCodeOf( e ) ); | |||
} | |||
} | |||
|
|||
@Test | |||
public void shouldAcceptSharedLocks() throws Exception | |||
{ | |||
lockClient.acquireShared( NONE, NODE, 1 ); | |||
lockClient.trySharedLock( NODE, 1 ); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters