Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shutdown index population jobs during indexing service shutdown
Shutdown indexing population jobs by canceling them and waiting to finish ongoing population before closing all indexes down. This change prevent ongoing index populations to throw exceptions because of already closed indexes. Also change order of stop/shutdown for label scan and indexing service since indexes can use label scan store for indexes population
- Loading branch information
1 parent
f3150b0
commit 2a3fb78
Showing
6 changed files
with
243 additions
and
11 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
...ty/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexPopulationJobController.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,91 @@ | ||
/* | ||
* 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.index; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.neo4j.scheduler.JobScheduler; | ||
|
||
import static org.neo4j.scheduler.JobScheduler.Groups.indexPopulation; | ||
|
||
public class IndexPopulationJobController | ||
{ | ||
private final Set<IndexPopulationJob> populationJobs = | ||
Collections.newSetFromMap( new ConcurrentHashMap<IndexPopulationJob,Boolean>() ); | ||
private final JobScheduler scheduler; | ||
|
||
IndexPopulationJobController( JobScheduler scheduler ) | ||
{ | ||
this.scheduler = scheduler; | ||
} | ||
|
||
public void stop() throws ExecutionException, InterruptedException | ||
{ | ||
for ( IndexPopulationJob job : populationJobs ) | ||
{ | ||
job.cancel().get(); | ||
} | ||
} | ||
|
||
public void startIndexPopulation( IndexPopulationJob job ) | ||
{ | ||
populationJobs.add( job ); | ||
scheduler.schedule( indexPopulation, new IndexPopulationJobWrapper( job, this ) ); | ||
} | ||
|
||
public void indexPopulationCompleted( IndexPopulationJob populationJob ) | ||
{ | ||
populationJobs.remove( populationJob ); | ||
} | ||
|
||
Set<IndexPopulationJob> getPopulationJobs() | ||
{ | ||
return populationJobs; | ||
} | ||
|
||
private static class IndexPopulationJobWrapper implements Runnable | ||
{ | ||
private IndexPopulationJob indexPopulationJob; | ||
private IndexPopulationJobController jobController; | ||
|
||
IndexPopulationJobWrapper( IndexPopulationJob indexPopulationJob, | ||
IndexPopulationJobController jobController ) | ||
{ | ||
this.indexPopulationJob = indexPopulationJob; | ||
this.jobController = jobController; | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
try | ||
{ | ||
indexPopulationJob.run(); | ||
} | ||
finally | ||
{ | ||
jobController.indexPopulationCompleted( indexPopulationJob ); | ||
} | ||
} | ||
} | ||
} |
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
92 changes: 92 additions & 0 deletions
92
...ernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexPopulationJobControllerTest.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,92 @@ | ||
/* | ||
* 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.index; | ||
|
||
|
||
import org.junit.Test; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.neo4j.test.OnDemandJobScheduler; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class IndexPopulationJobControllerTest | ||
{ | ||
|
||
private final OnDemandJobScheduler executer = new OnDemandJobScheduler(); | ||
private final IndexPopulationJobController jobController = new IndexPopulationJobController( executer ); | ||
|
||
@Test | ||
public void trackPopulationJobs() | ||
{ | ||
assertThat( jobController.getPopulationJobs(), is( empty() ) ); | ||
|
||
IndexPopulationJob populationJob = mock( IndexPopulationJob.class ); | ||
jobController.startIndexPopulation( populationJob ); | ||
assertThat( jobController.getPopulationJobs(), hasSize( 1 ) ); | ||
|
||
IndexPopulationJob populationJob2 = mock( IndexPopulationJob.class ); | ||
jobController.startIndexPopulation( populationJob2 ); | ||
assertThat( jobController.getPopulationJobs(), hasSize( 2 ) ); | ||
} | ||
|
||
@Test | ||
public void stopOngoingPopulationJobs() throws ExecutionException, InterruptedException | ||
{ | ||
IndexPopulationJob populationJob = getIndexPopulationJob(); | ||
IndexPopulationJob populationJob2 = getIndexPopulationJob(); | ||
jobController.startIndexPopulation( populationJob ); | ||
jobController.startIndexPopulation( populationJob2 ); | ||
|
||
jobController.stop(); | ||
|
||
verify( populationJob ).cancel(); | ||
verify( populationJob2 ).cancel(); | ||
} | ||
|
||
@Test | ||
public void untrackFinishedPopulations() | ||
{ | ||
IndexPopulationJob populationJob = getIndexPopulationJob(); | ||
jobController.startIndexPopulation( populationJob ); | ||
|
||
assertThat( jobController.getPopulationJobs(), hasSize( 1 ) ); | ||
|
||
executer.runJob(); | ||
|
||
assertThat( jobController.getPopulationJobs(), hasSize( 0 ) ); | ||
verify( populationJob ).run(); | ||
} | ||
|
||
private IndexPopulationJob getIndexPopulationJob() | ||
{ | ||
IndexPopulationJob populationJob = mock( IndexPopulationJob.class ); | ||
when( populationJob.cancel() ).thenReturn( CompletableFuture.completedFuture( null ) ); | ||
return populationJob; | ||
} | ||
} |
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