Skip to content

Commit

Permalink
Added Config to toggle verbose dbms.killQueries and moved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eebus committed Aug 24, 2017
1 parent 53dfb5d commit 747069d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 39 deletions.
Expand Up @@ -567,6 +567,11 @@ public enum LabelIndex
public static final Setting<String> procedure_unrestricted = public static final Setting<String> procedure_unrestricted =
setting( "dbms.security.procedures.unrestricted", Settings.STRING, "" ); setting( "dbms.security.procedures.unrestricted", Settings.STRING, "" );


@Description("If dbms.killQueries give a verbose output, with information about whitch querys where not found." )
public static final Setting<Boolean> kill_query_verbose =
setting( "dbms.procedures.kill_query_verbose", BOOLEAN, FALSE );


@Description( "Whether or not to release the exclusive schema lock is while building uniqueness constraints index" ) @Description( "Whether or not to release the exclusive schema lock is while building uniqueness constraints index" )
@Internal @Internal
public static final Setting<Boolean> release_schema_lock_while_building_constraint = setting( public static final Setting<Boolean> release_schema_lock_while_building_constraint = setting(
Expand Down
Expand Up @@ -31,6 +31,7 @@


import org.neo4j.function.UncaughtCheckedException; import org.neo4j.function.UncaughtCheckedException;
import org.neo4j.graphdb.DependencyResolver; import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.security.AuthorizationViolationException; import org.neo4j.graphdb.security.AuthorizationViolationException;
import org.neo4j.helpers.collection.Pair; import org.neo4j.helpers.collection.Pair;
import org.neo4j.kernel.api.KernelTransaction; import org.neo4j.kernel.api.KernelTransaction;
Expand All @@ -44,6 +45,7 @@
import org.neo4j.kernel.api.proc.UserFunctionSignature; import org.neo4j.kernel.api.proc.UserFunctionSignature;
import org.neo4j.kernel.api.query.ExecutingQuery; import org.neo4j.kernel.api.query.ExecutingQuery;
import org.neo4j.kernel.api.security.SecurityContext; import org.neo4j.kernel.api.security.SecurityContext;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.api.KernelTransactions; import org.neo4j.kernel.impl.api.KernelTransactions;
import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge; import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge;
import org.neo4j.kernel.impl.proc.Procedures; import org.neo4j.kernel.impl.proc.Procedures;
Expand Down Expand Up @@ -322,7 +324,8 @@ public Stream<QueryTerminationResult> killQuery( @Name( "id" ) String idText )


Set<Pair<KernelTransactionHandle,ExecutingQuery>> querys = Set<Pair<KernelTransactionHandle,ExecutingQuery>> querys =
getActiveTransactions( tx -> executingQueriesWithId( queryId, tx ) ).collect( toSet() ); getActiveTransactions( tx -> executingQueriesWithId( queryId, tx ) ).collect( toSet() );
if ( querys.isEmpty() ) Config configs = resolver.resolveDependency( Config.class );
if ( configs.get( GraphDatabaseSettings.kill_query_verbose ) && querys.isEmpty() )
{ {
return Stream.<QueryTerminationResult>builder() return Stream.<QueryTerminationResult>builder()
.add( new QueryFailedTerminationResult( fromExternalString( idText ) ) ).build(); .add( new QueryFailedTerminationResult( fromExternalString( idText ) ) ).build();
Expand Down Expand Up @@ -350,8 +353,9 @@ public Stream<QueryTerminationResult> killQueries( @Name( "ids" ) List<String> i


Set<QueryTerminationResult> terminatedQuerys = getActiveTransactions( tx -> executingQueriesWithIds( queryIds, tx ) ).map( Set<QueryTerminationResult> terminatedQuerys = getActiveTransactions( tx -> executingQueriesWithIds( queryIds, tx ) ).map(
catchThrown( InvalidArgumentsException.class, this::killQueryTransaction ) ).collect( toSet() ); catchThrown( InvalidArgumentsException.class, this::killQueryTransaction ) ).collect( toSet() );
Config configs = resolver.resolveDependency( Config.class );


if ( terminatedQuerys.size() != idTexts.size() ) if ( configs.get( GraphDatabaseSettings.kill_query_verbose ) && terminatedQuerys.size() != idTexts.size() )
{ {
for ( String id : idTexts ) for ( String id : idTexts )
{ {
Expand Down
Expand Up @@ -418,43 +418,6 @@ public void queryWaitingForLocksShouldBeKilledBeforeLocksAreReleased() throws Th
tx1.closeAndAssertSuccess(); tx1.closeAndAssertSuccess();
} }


@Test
public void shouldGiveNiceMessageAtFailWhenTryingToKill() throws Throwable
{
String query = "CALL dbms.killQuery('query-9999999999')";
Map<String,String> expected = new HashMap<>();
expected.put( "queryId", "query-9999999999" );
expected.put( "username", "n/a" );
expected.put( "message", "No Query found with this id" );
assertSuccess( adminSubject, query, r -> assertThat(r.next(), equalTo( expected )));
}

@Test
public void shouldGiveNiceMessageAtFailWhenTryingToKillMoreThenOne() throws Throwable
{
//Given
String query = "CALL dbms.killQueries(['query-9999999999', 'query-9999999989'])";

//Expect
Set<Map<String,String>> expected = new HashSet<>();
Map<String,String> firstResultExpected = new HashMap<>();
firstResultExpected.put( "queryId", "query-9999999989" );
firstResultExpected.put( "username", "n/a" );
firstResultExpected.put( "message", "No Query found with this id" );
Map<String,String> secoundResultExpected = new HashMap<>();
secoundResultExpected.put( "queryId", "query-9999999999" );
secoundResultExpected.put( "username", "n/a" );
secoundResultExpected.put( "message", "No Query found with this id" );
expected.add( firstResultExpected );
expected.add( secoundResultExpected );

//Then
assertSuccess( adminSubject, query, r -> {
Set<Map<String,Object>> actual = r.stream().collect( toSet() );
assertThat( actual, equalTo( expected ) );
} );
}

@Test @Test
public void shouldKillQueryAsAdmin() throws Throwable public void shouldKillQueryAsAdmin() throws Throwable
{ {
Expand Down
Expand Up @@ -19,9 +19,11 @@
*/ */
package org.neo4j.server.security.enterprise.auth; package org.neo4j.server.security.enterprise.auth;


import org.junit.Assert;
import org.junit.Test; import org.junit.Test;


import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
Expand All @@ -37,9 +39,12 @@
import org.neo4j.server.security.enterprise.configuration.SecuritySettings; import org.neo4j.server.security.enterprise.configuration.SecuritySettings;


import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.mockito.internal.util.collections.Sets.newSet; import static org.mockito.internal.util.collections.Sets.newSet;
import static org.neo4j.helpers.collection.MapUtil.genericMap; import static org.neo4j.helpers.collection.MapUtil.genericMap;
Expand Down Expand Up @@ -306,6 +311,54 @@ public void shouldShowAllowedRolesWhenListingFunctions() throws Throwable
assertListProceduresHasRoles( readSubject, expected, call ); assertListProceduresHasRoles( readSubject, expected, call );
} }


@Test
public void shouldGiveNiceMessageAtFailWhenTryingToKill() throws Throwable
{
configuredSetup( stringMap( GraphDatabaseSettings.kill_query_verbose.name() , "true" ) );

String query = "CALL dbms.killQuery('query-9999999999')";
Map<String,String> expected = new HashMap<>();
expected.put( "queryId", "query-9999999999" );
expected.put( "username", "n/a" );
expected.put( "message", "No Query found with this id" );
assertSuccess( adminSubject, query, r -> Assert.assertThat(r.next(), equalTo( expected )));
}

@Test
public void shouldNotGiveNiceMessageAtFailWhenTryingToKillWhenConfigured() throws Throwable
{
super.setUp();
String query = "CALL dbms.killQuery('query-9999999999')";
assertSuccess( adminSubject, query, r -> Assert.assertThat(r.hasNext(), is(false)));
}

@Test
public void shouldGiveNiceMessageAtFailWhenTryingToKillMoreThenOne() throws Throwable
{
//Given
configuredSetup( stringMap( GraphDatabaseSettings.kill_query_verbose.name() , "true" ) );
String query = "CALL dbms.killQueries(['query-9999999999', 'query-9999999989'])";

//Expect
Set<Map<String,String>> expected = new HashSet<>();
Map<String,String> firstResultExpected = new HashMap<>();
firstResultExpected.put( "queryId", "query-9999999989" );
firstResultExpected.put( "username", "n/a" );
firstResultExpected.put( "message", "No Query found with this id" );
Map<String,String> secoundResultExpected = new HashMap<>();
secoundResultExpected.put( "queryId", "query-9999999999" );
secoundResultExpected.put( "username", "n/a" );
secoundResultExpected.put( "message", "No Query found with this id" );
expected.add( firstResultExpected );
expected.add( secoundResultExpected );

//Then
assertSuccess( adminSubject, query, r -> {
Set<Map<String,Object>> actual = r.stream().collect( toSet() );
Assert.assertThat( actual, equalTo( expected ) );
} );
}

private void assertListProceduresHasRoles( S subject, Map<String,Set<String>> expected, String call ) private void assertListProceduresHasRoles( S subject, Map<String,Set<String>> expected, String call )
{ {
assertSuccess( subject, call, itr -> assertSuccess( subject, call, itr ->
Expand Down

0 comments on commit 747069d

Please sign in to comment.