Skip to content

Commit

Permalink
Add logging for errors when closing fulltext index
Browse files Browse the repository at this point in the history
  • Loading branch information
ragadeeshu committed Sep 12, 2017
1 parent 45fee03 commit 2c47c0c
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 82 deletions.
Expand Up @@ -67,7 +67,7 @@ public FulltextFactory( FileSystemAbstraction fileSystem, File storeDir, Analyze
* @param provider The provider to register with
* @throws IOException
*/
public void createFulltextIndex( String identifier, FulltextProvider.FULLTEXT_INDEX_TYPE type, List<String> properties, FulltextProvider provider )
public void createFulltextIndex( String identifier, FulltextProvider.FulltextIndexType type, List<String> properties, FulltextProvider provider )
throws IOException
{
LuceneIndexStorageBuilder storageBuilder = LuceneIndexStorageBuilder.create();
Expand Down
Expand Up @@ -27,6 +27,8 @@
import java.util.stream.Collectors;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.logging.Log;

/**
* Provider class that manages and provides fulltext indices. This is the main entry point for the fulltext addon.
Expand All @@ -35,16 +37,18 @@ public class FulltextProvider implements AutoCloseable
{
private static FulltextProvider instance;
private final GraphDatabaseService db;
private Log log;
private final FulltextTransactionEventUpdater fulltextTransactionEventUpdater;
private boolean closed;
private Set<String> nodeProperties;
private Set<String> relationshipProperties;
private Map<String,LuceneFulltext> nodeIndices;
private Map<String,LuceneFulltext> relationshipIndices;

private FulltextProvider( GraphDatabaseService db )
private FulltextProvider( GraphDatabaseService db, Log log )
{
this.db = db;
this.log = log;
closed = false;
fulltextTransactionEventUpdater = new FulltextTransactionEventUpdater( this );
db.registerTransactionEventHandler( fulltextTransactionEventUpdater );
Expand All @@ -57,13 +61,14 @@ private FulltextProvider( GraphDatabaseService db )
/**
* Fetch the current instance of the provider. If there is none, create one associated with the given database.
* @param db Database used for eventual creation of the provider.
* @param logService
* @return The instance for the given database.
*/
public static synchronized FulltextProvider instance( GraphDatabaseService db )
public static synchronized FulltextProvider instance( GraphDatabaseService db, LogService logService )
{
if ( instance == null || instance.closed )
{
instance = new FulltextProvider( db );
instance = new FulltextProvider( db, logService.getInternalLog( FulltextProvider.class ) );
}
return instance;
}
Expand All @@ -86,7 +91,7 @@ public synchronized void close()
}
catch ( IOException e )
{
e.printStackTrace();
log.error( "Unable to close fulltext node index.", e );
}
} );
relationshipIndices.values().forEach( luceneFulltextIndex ->
Expand All @@ -97,7 +102,7 @@ public synchronized void close()
}
catch ( IOException e )
{
e.printStackTrace();
log.error( "Unable to close fulltext relationship index.", e );
}
} );
}
Expand All @@ -106,7 +111,7 @@ public synchronized void close()
synchronized void register( LuceneFulltext fulltextIndex ) throws IOException
{
fulltextIndex.open();
if ( fulltextIndex.getType() == FULLTEXT_INDEX_TYPE.NODES )
if ( fulltextIndex.getType() == FulltextIndexType.NODES )
{
nodeIndices.put( fulltextIndex.getIdentifier(), fulltextIndex );
nodeProperties.addAll( fulltextIndex.getProperties() );
Expand Down Expand Up @@ -145,9 +150,9 @@ Set<WritableFulltext> writableRelationshipIndices()
* @return A {@link ReadOnlyFulltext} for the index, or null if no such index is found.
* @throws IOException
*/
public ReadOnlyFulltext getReader( String identifier, FULLTEXT_INDEX_TYPE type ) throws IOException
public ReadOnlyFulltext getReader( String identifier, FulltextIndexType type ) throws IOException
{
if ( type == FULLTEXT_INDEX_TYPE.NODES )
if ( type == FulltextIndexType.NODES )
{
return nodeIndices.get( identifier ).getIndexReader();
}
Expand All @@ -160,7 +165,7 @@ public ReadOnlyFulltext getReader( String identifier, FULLTEXT_INDEX_TYPE type )
/**
* Fulltext index type.
*/
public enum FULLTEXT_INDEX_TYPE
public enum FulltextIndexType
{
NODES
{
Expand Down
Expand Up @@ -25,29 +25,24 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.neo4j.helpers.TaskCoordinator;
import org.neo4j.kernel.api.impl.index.AbstractLuceneIndex;
import org.neo4j.kernel.api.impl.index.partition.AbstractIndexPartition;
import org.neo4j.kernel.api.impl.index.partition.IndexPartitionFactory;
import org.neo4j.kernel.api.impl.index.partition.PartitionSearcher;
import org.neo4j.kernel.api.impl.index.storage.PartitionedIndexStorage;
import org.neo4j.kernel.api.impl.schema.writer.PartitionedIndexWriter;

import static java.util.Collections.singletonMap;

class LuceneFulltext extends AbstractLuceneIndex
{
private final Analyzer analyzer;
private final String identifier;
private final FulltextProvider.FULLTEXT_INDEX_TYPE type;
private final FulltextProvider.FulltextIndexType type;
private final Set<String> properties;

LuceneFulltext( PartitionedIndexStorage indexStorage, IndexPartitionFactory partitionFactory, List<String> properties, Analyzer analyzer,
String identifier, FulltextProvider.FULLTEXT_INDEX_TYPE type )
String identifier, FulltextProvider.FulltextIndexType type )
{
super( indexStorage, partitionFactory );
this.properties = Collections.unmodifiableSet( new HashSet<>( properties ) );
Expand Down Expand Up @@ -98,7 +93,7 @@ ReadOnlyFulltext getIndexReader() throws IOException
return hasSinglePartition( partitions ) ? createSimpleReader( partitions ) : createPartitionedReader( partitions );
}

FulltextProvider.FULLTEXT_INDEX_TYPE getType()
FulltextProvider.FulltextIndexType getType()
{
return type;
}
Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.neo4j.kernel.api.impl.fulltext.FulltextFactory;
import org.neo4j.kernel.api.impl.fulltext.FulltextProvider;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;

Expand All @@ -41,14 +42,17 @@ class BloomKernelExtension extends LifecycleAdapter
private final FileSystemAbstraction fileSystemAbstraction;
private final GraphDatabaseService db;
private final Procedures procedures;
private LogService logService;

BloomKernelExtension( FileSystemAbstraction fileSystemAbstraction, File storeDir, Config config, GraphDatabaseService db, Procedures procedures )
BloomKernelExtension( FileSystemAbstraction fileSystemAbstraction, File storeDir, Config config, GraphDatabaseService db, Procedures procedures,
LogService logService )
{
this.storeDir = storeDir;
this.config = config;
this.fileSystemAbstraction = fileSystemAbstraction;
this.db = db;
this.procedures = procedures;
this.logService = logService;
}

@Override
Expand All @@ -57,15 +61,15 @@ public void init() throws IOException, ProcedureException
List<String> properties = config.get( LoadableBloomFulltextConfig.bloom_indexed_properties );
Analyzer analyzer = getAnalyzer();

FulltextProvider provider = FulltextProvider.instance( db );
FulltextProvider provider = FulltextProvider.instance( db, logService );
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemAbstraction, storeDir, analyzer );
String bloomNodes = "bloomNodes";
fulltextFactory.createFulltextIndex( bloomNodes, FulltextProvider.FULLTEXT_INDEX_TYPE.NODES, properties, provider );
fulltextFactory.createFulltextIndex( bloomNodes, FulltextProvider.FulltextIndexType.NODES, properties, provider );
String bloomRelationships = "bloomRelationships";
fulltextFactory.createFulltextIndex( bloomRelationships, FulltextProvider.FULLTEXT_INDEX_TYPE.RELATIONSHIPS, properties, provider );
fulltextFactory.createFulltextIndex( bloomRelationships, FulltextProvider.FulltextIndexType.RELATIONSHIPS, properties, provider );

procedures.register( new BloomProcedure( FulltextProvider.FULLTEXT_INDEX_TYPE.NODES, bloomNodes, provider ) );
procedures.register( new BloomProcedure( FulltextProvider.FULLTEXT_INDEX_TYPE.RELATIONSHIPS, bloomRelationships, provider ) );
procedures.register( new BloomProcedure( FulltextProvider.FulltextIndexType.NODES, bloomNodes, provider ) );
procedures.register( new BloomProcedure( FulltextProvider.FulltextIndexType.RELATIONSHIPS, bloomRelationships, provider ) );
}

private Analyzer getAnalyzer()
Expand All @@ -86,6 +90,6 @@ private Analyzer getAnalyzer()
@Override
public void shutdown() throws Exception
{
FulltextProvider.instance( db ).close();
FulltextProvider.instance( db, logService ).close();
}
}
Expand Up @@ -23,6 +23,7 @@
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.extension.KernelExtensionFactory;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.kernel.impl.spi.KernelContext;
import org.neo4j.kernel.lifecycle.Lifecycle;
Expand All @@ -47,6 +48,8 @@ public interface Dependencies
FileSystemAbstraction fileSystem();

Procedures procedures();

LogService logService();
}

BloomKernelExtensionFactory()
Expand All @@ -58,6 +61,6 @@ public interface Dependencies
public Lifecycle newInstance( KernelContext context, Dependencies dependencies ) throws Throwable
{
return new BloomKernelExtension( dependencies.fileSystem(), context.storeDir(), dependencies.getConfig(), dependencies.db(),
dependencies.procedures() );
dependencies.procedures(), dependencies.logService() );
}
}
Expand Up @@ -44,15 +44,15 @@ public class BloomProcedure extends CallableProcedure.BasicProcedure
private static final String[] PROCEDURE_NAMESPACE = {"db", "fulltext"};
private final String identifier;
private final FulltextProvider provider;
private final FulltextProvider.FULLTEXT_INDEX_TYPE type;
private final FulltextProvider.FulltextIndexType type;

/**
* Creates a procedure for querying the bloom fulltext addon.
* @param type The type of fulltext index to be queried.
* @param identifier The identifier of the fulltext index.
* @param provider The provider of the fulltext index.
*/
public BloomProcedure( FulltextProvider.FULLTEXT_INDEX_TYPE type, String identifier, FulltextProvider provider )
public BloomProcedure( FulltextProvider.FulltextIndexType type, String identifier, FulltextProvider provider )
{
super( procedureSignature( new QualifiedName( PROCEDURE_NAMESPACE, PROCEDURE_NAME + type ) ).in( "terms",
Neo4jTypes.NTList( Neo4jTypes.NTString ) ).out( OUTPUT_NAME, Neo4jTypes.NTInteger ).description(
Expand Down
Expand Up @@ -30,6 +30,8 @@
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.logging.NullLogService;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.test.rule.DatabaseRule;
import org.neo4j.test.rule.EmbeddedDatabaseRule;
Expand All @@ -39,11 +41,12 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.neo4j.kernel.api.impl.fulltext.FulltextProvider.FULLTEXT_INDEX_TYPE;
import static org.neo4j.kernel.api.impl.fulltext.FulltextProvider.FulltextIndexType;

public class FulltextAnalyzerTest
{
private static final Label LABEL = Label.label( "label" );
private static final LogService LOG_SERVICE = NullLogService.getInstance();
@ClassRule
public static FileSystemRule fileSystemRule = new DefaultFileSystemRule();
@ClassRule
Expand All @@ -56,9 +59,9 @@ public void shouldBeAbleToSpecifyEnglishAnalyzer() throws Exception
{
GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI();
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), new EnglishAnalyzer() );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
try ( FulltextProvider provider = FulltextProvider.instance( db, LOG_SERVICE ) )
{
fulltextFactory.createFulltextIndex( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );
fulltextFactory.createFulltextIndex( "bloomNodes", FulltextIndexType.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand All @@ -74,7 +77,7 @@ public void shouldBeAbleToSpecifyEnglishAnalyzer() throws Exception
tx.success();
}

try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES ) )
try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", FulltextIndexType.NODES ) )
{

assertFalse( reader.query( "and" ).hasNext() );
Expand All @@ -92,9 +95,9 @@ public void shouldBeAbleToSpecifySwedishAnalyzer() throws Exception
{
GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI();
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), new SwedishAnalyzer() );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
try ( FulltextProvider provider = FulltextProvider.instance( db, LOG_SERVICE ) )
{
fulltextFactory.createFulltextIndex( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );
fulltextFactory.createFulltextIndex( "bloomNodes", FulltextIndexType.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand All @@ -110,7 +113,7 @@ public void shouldBeAbleToSpecifySwedishAnalyzer() throws Exception
tx.success();
}

try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES ) )
try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", FulltextIndexType.NODES ) )
{
assertEquals( firstID, reader.query( "and" ).next() );
assertEquals( firstID, reader.query( "in" ).next() );
Expand Down

0 comments on commit 2c47c0c

Please sign in to comment.