Skip to content

Commit

Permalink
Merge pull request #8511 from MishaDemianenko/3.2-label-scan-view
Browse files Browse the repository at this point in the history
Close reader that used to count number of entries in the label scan index.
  • Loading branch information
burqen committed Dec 15, 2016
2 parents 353af6b + 2ba3c6e commit 86c6132
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import org.neo4j.kernel.impl.index.IndexConfigStore;
import org.neo4j.kernel.impl.locking.LockGroup;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.storageengine.api.StoreFileMetadata;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.id.BufferedIdController;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.id.DefaultIdController;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.id.IdController;
Expand Down Expand Up @@ -117,6 +116,7 @@
import org.neo4j.storageengine.api.StorageCommand;
import org.neo4j.storageengine.api.StorageEngine;
import org.neo4j.storageengine.api.StorageStatement;
import org.neo4j.storageengine.api.StoreFileMetadata;
import org.neo4j.storageengine.api.StoreReadLayer;
import org.neo4j.storageengine.api.TransactionApplicationMode;
import org.neo4j.storageengine.api.lock.ResourceLocker;
Expand Down Expand Up @@ -222,7 +222,7 @@ public RecordStorageEngine(
labelScanStore = labelScanStoreProvider.getLabelScanStore();

schemaIndexProviderMap = new DefaultSchemaIndexProviderMap( indexProvider );
indexStoreView = new DynamicIndexStoreView( labelScanStore, lockService, neoStores );
indexStoreView = new DynamicIndexStoreView( labelScanStore, lockService, neoStores, logProvider );
indexingService = IndexingServiceFactory.createIndexingService( config, scheduler, schemaIndexProviderMap,
indexStoreView, tokenNameLookup,
Iterators.asList( new SchemaStorage( neoStores.getSchemaStore() ).allIndexRules() ), logProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
import java.util.stream.IntStream;

import org.neo4j.helpers.collection.Visitor;
import org.neo4j.kernel.api.labelscan.AllEntriesLabelScanReader;
import org.neo4j.kernel.api.labelscan.LabelScanStore;
import org.neo4j.kernel.api.labelscan.NodeLabelUpdate;
import org.neo4j.kernel.impl.api.index.NodePropertyUpdates;
import org.neo4j.kernel.impl.api.index.StoreScan;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.counts.CountsTracker;
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;
import org.neo4j.register.Registers;
import org.neo4j.unsafe.impl.internal.dragons.FeatureToggles;

Expand All @@ -48,12 +51,15 @@ public class DynamicIndexStoreView extends NeoStoreIndexStoreView

private final LabelScanStore labelScanStore;
private final CountsTracker counts;
private final Log log;

public DynamicIndexStoreView( LabelScanStore labelScanStore, LockService locks, NeoStores neoStores )
public DynamicIndexStoreView( LabelScanStore labelScanStore, LockService locks, NeoStores neoStores,
LogProvider logProvider )
{
super( locks, neoStores );
this.counts = neoStores.getCounts();
this.labelScanStore = labelScanStore;
this.log = logProvider.getLog( getClass() );
}

@Override
Expand All @@ -71,12 +77,24 @@ public <FAILURE extends Exception> StoreScan<FAILURE> visitNodes( int[] labelIds

private boolean useAllNodeStoreScan( int[] labelIds )
{
return ArrayUtils.isEmpty( labelIds ) || isEmptyLabelScanStore() || isNumberOfLabeledNodesExceedThreshold( labelIds );
try
{
return ArrayUtils.isEmpty( labelIds ) || isEmptyLabelScanStore() ||
isNumberOfLabeledNodesExceedThreshold( labelIds );
}
catch ( Exception e )
{
log.error( "Can not determine number of labeled nodes, falling back to all nodes scan.", e );
return true;
}
}

private boolean isEmptyLabelScanStore()
private boolean isEmptyLabelScanStore() throws Exception
{
return labelScanStore.allNodeLabelRanges().maxCount() == 0;
try ( AllEntriesLabelScanReader nodeLabelRanges = labelScanStore.allNodeLabelRanges() )
{
return nodeLabelRanges.maxCount() == 0;
}
}

private boolean isNumberOfLabeledNodesExceedThreshold( int[] labelIds )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.neo4j.kernel.impl.store.counts.CountsTracker;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.RecordLoad;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.register.Register;
import org.neo4j.register.Registers;
import org.neo4j.storageengine.api.schema.LabelScanReader;
Expand All @@ -48,6 +49,7 @@
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class DynamicIndexStoreViewTest
Expand Down Expand Up @@ -83,8 +85,8 @@ public void visitAllNodesWhenThresholdReached() throws Exception
mockLabelNodeCount( countStore, 2 );
mockLabelNodeCount( countStore, 3 );

DynamicIndexStoreView storeView =
new DynamicIndexStoreView( labelScanStore, LockService.NO_LOCK_SERVICE, neoStores );
DynamicIndexStoreView storeView = new DynamicIndexStoreView( labelScanStore, LockService.NO_LOCK_SERVICE,
neoStores, NullLogProvider.getInstance() );

StoreScan<Exception> storeScan = storeView
.visitNodes( new int[]{1, 2, 3}, propertyKeyIdFilter, propertyUpdateVisitor, labelUpdateVisitor );
Expand All @@ -95,6 +97,17 @@ public void visitAllNodesWhenThresholdReached() throws Exception
.getRecord( anyLong(), any( NodeRecord.class ), any( RecordLoad.class ) );
}

@Test
public void closeAllEntriesScanReaderWhenCheckingLabelScanStore() throws Exception
{
DynamicIndexStoreView storeView = new DynamicIndexStoreView( labelScanStore, LockService.NO_LOCK_SERVICE,
neoStores, NullLogProvider.getInstance() );

storeView.visitNodes( new int[]{1, 2, 3}, propertyKeyIdFilter, propertyUpdateVisitor, labelUpdateVisitor );

verify( nodeLabelRanges ).close();
}

@Test
public void visitOnlyLabeledNodesWhenThresholdNotReached() throws Exception
{
Expand All @@ -110,8 +123,8 @@ public void visitOnlyLabeledNodesWhenThresholdNotReached() throws Exception
mockLabelNodeCount( countStore, 2 );
mockLabelNodeCount( countStore, 6 );

DynamicIndexStoreView storeView =
new DynamicIndexStoreView( labelScanStore, LockService.NO_LOCK_SERVICE, neoStores );
DynamicIndexStoreView storeView = new DynamicIndexStoreView( labelScanStore, LockService.NO_LOCK_SERVICE,
neoStores, NullLogProvider.getInstance() );

StoreScan<Exception> storeScan = storeView
.visitNodes( new int[]{2, 6}, propertyKeyIdFilter, propertyUpdateVisitor, labelUpdateVisitor );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private class DynamicIndexStoreViewWrapper extends DynamicIndexStoreView
DynamicIndexStoreViewWrapper( LabelScanStore labelScanStore, LockService locks, NeoStores neoStores,
List<NodePropertyUpdate> updates )
{
super( labelScanStore, locks, neoStores );
super( labelScanStore, locks, neoStores, NullLogProvider.getInstance() );
this.updates = updates;
}

Expand Down

0 comments on commit 86c6132

Please sign in to comment.