-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
526 additions
and
0 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
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
62 changes: 62 additions & 0 deletions
62
...ity/kernel/src/main/java/org/neo4j/kernel/impl/api/KernelTransactionMonitorScheduler.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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 java.util.concurrent.TimeUnit; | ||
|
||
import org.neo4j.kernel.impl.util.JobScheduler; | ||
import org.neo4j.kernel.lifecycle.LifecycleAdapter; | ||
|
||
import static org.neo4j.kernel.impl.util.JobScheduler.Groups.transactionTimeoutMonitor; | ||
|
||
public class KernelTransactionMonitorScheduler extends LifecycleAdapter | ||
{ | ||
private final KernelTransactionTimeoutMonitor kernelTransactionTimeoutMonitor; | ||
private final JobScheduler scheduler; | ||
private final long checkIntervalMillis; | ||
private JobScheduler.JobHandle monitorJobHandle; | ||
|
||
public KernelTransactionMonitorScheduler( KernelTransactionTimeoutMonitor kernelTransactionTimeoutMonitor, | ||
JobScheduler scheduler, long checkIntervalMillis ) | ||
{ | ||
this.kernelTransactionTimeoutMonitor = kernelTransactionTimeoutMonitor; | ||
this.scheduler = scheduler; | ||
this.checkIntervalMillis = checkIntervalMillis; | ||
} | ||
|
||
@Override | ||
public void start() throws Throwable | ||
{ | ||
if (checkIntervalMillis > 0) | ||
{ | ||
monitorJobHandle = scheduler.scheduleRecurring( transactionTimeoutMonitor, kernelTransactionTimeoutMonitor, | ||
checkIntervalMillis, TimeUnit.MILLISECONDS ); | ||
} | ||
} | ||
|
||
@Override | ||
public void stop() throws Throwable | ||
{ | ||
if ( monitorJobHandle != null ) | ||
{ | ||
monitorJobHandle.cancel( true ); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...unity/kernel/src/main/java/org/neo4j/kernel/impl/api/KernelTransactionTimeoutMonitor.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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 java.time.Clock; | ||
import java.util.Set; | ||
|
||
import org.neo4j.kernel.api.KernelTransactionHandle; | ||
import org.neo4j.kernel.api.exceptions.Status; | ||
import org.neo4j.kernel.impl.logging.LogService; | ||
import org.neo4j.logging.Log; | ||
|
||
/** | ||
* Transaction monitor that check transactions with a configured timeout for expiration. | ||
* In case if transaction timed out it will be terminated. | ||
*/ | ||
public class KernelTransactionTimeoutMonitor implements Runnable | ||
{ | ||
private final KernelTransactions kernelTransactions; | ||
private final Clock clock; | ||
private final Log log; | ||
|
||
public KernelTransactionTimeoutMonitor( KernelTransactions kernelTransactions, Clock clock, LogService logService ) | ||
{ | ||
this.kernelTransactions = kernelTransactions; | ||
this.clock = clock; | ||
this.log = logService.getInternalLog( KernelTransactionTimeoutMonitor.class ); | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
Set<KernelTransactionHandle> activeTransactions = kernelTransactions.activeTransactions(); | ||
long now = clock.millis(); | ||
for ( KernelTransactionHandle activeTransaction : activeTransactions ) | ||
{ | ||
long transactionTimeoutMillis = activeTransaction.timeoutMillis(); | ||
if ( transactionTimeoutMillis > 0 ) | ||
{ | ||
if ( isTransactionExpired( activeTransaction, now, transactionTimeoutMillis ) ) | ||
{ | ||
if ( activeTransaction.markForTermination( Status.Transaction.TransactionTimedOut ) ) | ||
{ | ||
log.warn( "Transaction %s timeout.", activeTransaction ); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private boolean isTransactionExpired( KernelTransactionHandle activeTransaction, long nowMillis, | ||
long transactionTimeoutMillis ) | ||
{ | ||
return nowMillis > (activeTransaction.startTime() + transactionTimeoutMillis); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
...kernel/src/test/java/org/neo4j/kernel/impl/api/KernelTransactionMonitorSchedulerTest.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.junit.Test; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.neo4j.kernel.impl.util.JobScheduler; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyZeroInteractions; | ||
|
||
public class KernelTransactionMonitorSchedulerTest | ||
{ | ||
|
||
private final JobScheduler scheduler = mock( JobScheduler.class ); | ||
private final KernelTransactionTimeoutMonitor transactionTimeoutMonitor = mock( KernelTransactionTimeoutMonitor.class ); | ||
|
||
@Test | ||
public void scheduleRecurringMonitorJobIfConfigured() throws Throwable | ||
{ | ||
KernelTransactionMonitorScheduler transactionMonitorScheduler = createMonitorScheduler(1); | ||
transactionMonitorScheduler.start(); | ||
|
||
verify( scheduler).scheduleRecurring( JobScheduler.Groups.transactionTimeoutMonitor, transactionTimeoutMonitor, 1, TimeUnit | ||
.MILLISECONDS ); | ||
} | ||
|
||
@Test | ||
public void doNotScheduleMonitorJobIfDisabled() throws Throwable | ||
{ | ||
KernelTransactionMonitorScheduler transactionMonitorScheduler = createMonitorScheduler( 0 ); | ||
transactionMonitorScheduler.start(); | ||
|
||
verifyZeroInteractions( scheduler); | ||
} | ||
|
||
private KernelTransactionMonitorScheduler createMonitorScheduler( long checkInterval ) | ||
{ | ||
return new KernelTransactionMonitorScheduler( transactionTimeoutMonitor, scheduler, checkInterval ); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...ity/kernel/src/test/java/org/neo4j/kernel/impl/api/KernelTransactionTimeoutMonitorIT.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* 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.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.neo4j.graphdb.Node; | ||
import org.neo4j.graphdb.Transaction; | ||
import org.neo4j.graphdb.factory.GraphDatabaseSettings; | ||
import org.neo4j.kernel.api.KernelTransaction; | ||
import org.neo4j.kernel.api.security.SecurityContext; | ||
import org.neo4j.kernel.impl.coreapi.InternalTransaction; | ||
import org.neo4j.test.rule.DatabaseRule; | ||
import org.neo4j.test.rule.EmbeddedDatabaseRule; | ||
|
||
public class KernelTransactionTimeoutMonitorIT | ||
{ | ||
@Rule | ||
public DatabaseRule database = new EmbeddedDatabaseRule() | ||
.withSetting( GraphDatabaseSettings.transaction_monitor_check_interval, "100ms" ); | ||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
private static final int NODE_ID = 0; | ||
private ExecutorService executor; | ||
|
||
@Before | ||
public void setUp() throws Exception | ||
{ | ||
executor = Executors.newSingleThreadExecutor(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception | ||
{ | ||
executor.shutdown(); | ||
} | ||
|
||
@Test( timeout = 30_000 ) | ||
public void terminateExpiredTransaction() throws Exception | ||
{ | ||
try ( Transaction transaction = database.beginTx() ) | ||
{ | ||
database.createNode(); | ||
transaction.success(); | ||
} | ||
|
||
expectedException.expectMessage( "The transaction has been terminated." ); | ||
|
||
try ( Transaction transaction = database.beginTx() ) | ||
{ | ||
Node nodeById = database.getNodeById( NODE_ID ); | ||
nodeById.setProperty( "a", "b" ); | ||
executor.submit( startAnotherTransaction() ).get(); | ||
} | ||
} | ||
|
||
private Runnable startAnotherTransaction() | ||
{ | ||
return () -> { | ||
try ( InternalTransaction transaction = database | ||
.beginTransaction( KernelTransaction.Type.implicit, SecurityContext.AUTH_DISABLED, 1, | ||
TimeUnit.SECONDS ) ) | ||
{ | ||
Node node = database.getNodeById( NODE_ID ); | ||
node.setProperty( "c", "d" ); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.