Skip to content

Commit

Permalink
Noop lock manager for read replicas
Browse files Browse the repository at this point in the history
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
martinfurmanski committed Jul 17, 2017
1 parent bb7cbab commit 4fe8727
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 17 deletions.
Expand Up @@ -657,4 +657,19 @@ interface HasStatus
{
Status status();
}

static Status statusCodeOf( Throwable e )
{
do
{
if ( e instanceof Status.HasStatus )
{
return ((Status.HasStatus) e).status();
}
e = e.getCause();
}
while ( e != null );

return null;
}
}
Expand Up @@ -106,7 +106,6 @@
import org.neo4j.udc.UsageData;

import static org.neo4j.causalclustering.discovery.ResolutionResolverFactory.chooseResolver;
import static org.neo4j.kernel.impl.factory.CommunityEditionModule.createLockManager;

/**
* This implementation of {@link org.neo4j.kernel.impl.factory.EditionModule} creates the implementations of services
Expand Down Expand Up @@ -139,7 +138,7 @@ public class EnterpriseReadReplicaEditionModule extends EditionModule

GraphDatabaseFacade graphDatabaseFacade = platformModule.graphDatabaseFacade;

lockManager = dependencies.satisfyDependency( createLockManager( config, platformModule.clock, logging ) );
lockManager = dependencies.satisfyDependency( new ReadReplicaLockManager() );

statementLocksFactory = new StatementLocksFactorySelector( lockManager, config, logging ).select();

Expand Down
@@ -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;
}
}
}
@@ -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 );
}
}
Expand Up @@ -33,6 +33,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.neo4j.kernel.api.exceptions.Status.statusCodeOf;

public class UnavailableIT
{
Expand Down Expand Up @@ -90,19 +91,4 @@ public void shouldReturnUnavailableStatusWhenShutdown() throws Exception
assertEquals( Status.General.DatabaseUnavailable, statusCodeOf( e ) );
}
}

private Status statusCodeOf( Throwable e )
{
do
{
if ( e instanceof Status.HasStatus )
{
return ((Status.HasStatus) e).status();
}
e = e.getCause();
}
while ( e != null );

return null;
}
}

0 comments on commit 4fe8727

Please sign in to comment.