Search in sources :

Example 26 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class AbstractCypherResource method executeStatementsInNewTransaction.

@POST
public Response executeStatementsInNewTransaction(InputEventStream inputEventStream, @Context HttpServletRequest request, @Context HttpHeaders headers) {
    try (var memoryTracker = createMemoryTracker()) {
        InputEventStream inputStream = ensureNotNull(inputEventStream);
        var graphDatabaseAPI = httpTransactionManager.getGraphDatabaseAPI(databaseName);
        return graphDatabaseAPI.map(databaseAPI -> {
            if (isDatabaseNotAvailable(databaseAPI)) {
                return createNonAvailableDatabaseResponse(inputStream.getParameters());
            }
            memoryTracker.allocateHeap(Invocation.SHALLOW_SIZE);
            final TransactionFacade transactionFacade = httpTransactionManager.createTransactionFacade(databaseAPI, memoryTracker);
            TransactionHandle transactionHandle = createNewTransactionHandle(transactionFacade, request, headers, memoryTracker, false);
            Invocation invocation = new Invocation(log, transactionHandle, uriScheme.txCommitUri(transactionHandle.getId()), memoryPool, inputStream, false);
            OutputEventStreamImpl outputStream = new OutputEventStreamImpl(inputStream.getParameters(), transactionHandle, uriScheme, invocation::execute);
            return Response.created(transactionHandle.uri()).entity(outputStream).build();
        }).orElse(createNonExistentDatabaseResponse(inputStream.getParameters()));
    }
}
Also used : PathParam(javax.ws.rs.PathParam) Neo4jError(org.neo4j.server.rest.Neo4jError) Log(org.neo4j.logging.Log) TransactionUriScheme(org.neo4j.server.http.cypher.format.api.TransactionUriScheme) Status(org.neo4j.kernel.api.exceptions.Status) Path(javax.ws.rs.Path) LoginContext(org.neo4j.internal.kernel.api.security.LoginContext) LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Map(java.util.Map) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) MemoryTracker(org.neo4j.memory.MemoryTracker) DELETE(javax.ws.rs.DELETE) HttpHeaderUtils.getTransactionTimeout(org.neo4j.server.web.HttpHeaderUtils.getTransactionTimeout) Collections.emptyMap(java.util.Collections.emptyMap) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) MemoryPool(org.neo4j.memory.MemoryPool) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) AuthorizedRequestWrapper(org.neo4j.server.rest.dbms.AuthorizedRequestWrapper) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) POST(javax.ws.rs.POST)

Example 27 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class TxStateTransactionDataSnapshotIT method countChangedRelationshipInTransactionStateSnapshot.

@Test
void countChangedRelationshipInTransactionStateSnapshot() {
    long relationshipIdToChange;
    int attachedPropertySize = (int) ByteUnit.mebiBytes(1);
    int doublePropertySize = attachedPropertySize * 2;
    final String property = "a";
    final String doubleProperty = "b";
    try (Transaction transaction = database.beginTx()) {
        var start = transaction.createNode();
        var end = transaction.createNode();
        var relationship = start.createRelationshipTo(end, withName("relType"));
        relationship.setProperty(property, randomAscii(attachedPropertySize));
        relationship.setProperty(doubleProperty, randomAscii(doublePropertySize));
        relationshipIdToChange = relationship.getId();
        transaction.commit();
    }
    try (Transaction transaction = database.beginTx()) {
        var relationship = transaction.getRelationshipById(relationshipIdToChange);
        relationship.setProperty(doubleProperty, randomAscii(attachedPropertySize));
        relationship.removeProperty(property);
        var kernelTransaction = getKernelTransaction(transaction);
        var transactionState = kernelTransaction.txState();
        final MemoryTracker memoryTracker = kernelTransaction.memoryTracker();
        // reset to count only snapshot memory
        var trackingData = resetMemoryTracker(memoryTracker);
        try (var snapshot = new TxStateTransactionDataSnapshot(transactionState, kernelTransaction.newStorageReader(), kernelTransaction)) {
            assertThat(memoryTracker.usedNativeMemory()).isZero();
            assertThat(memoryTracker.estimatedHeapMemory()).isGreaterThanOrEqualTo(emptySnapshotSize + (attachedPropertySize + doublePropertySize) + (2 * RelationshipPropertyEntryView.SHALLOW_SIZE));
        } finally {
            restoreMemoryTracker(memoryTracker, trackingData);
        }
    }
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test)

Example 28 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class TxStateTransactionDataSnapshotIT method countEmptySnapshotSize.

private long countEmptySnapshotSize() {
    try (Transaction transaction = database.beginTx()) {
        var kernelTransaction = getKernelTransaction(transaction);
        var transactionState = kernelTransaction.txState();
        final MemoryTracker memoryTracker = kernelTransaction.memoryTracker();
        // reset to count only snapshot memory
        resetMemoryTracker(memoryTracker);
        try (var snapshot = new TxStateTransactionDataSnapshot(transactionState, kernelTransaction.newStorageReader(), kernelTransaction)) {
            return memoryTracker.estimatedHeapMemory();
        }
    }
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) MemoryTracker(org.neo4j.memory.MemoryTracker)

Example 29 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class MultipleIndexPopulator method createStoreScan.

StoreScan createStoreScan(PageCacheTracer cacheTracer) {
    int[] entityTokenIds = entityTokenIds();
    int[] propertyKeyIds = propertyKeyIds();
    IntPredicate propertyKeyIdFilter = propertyKeyId -> contains(propertyKeyIds, propertyKeyId);
    StoreScan storeScan;
    if (type == EntityType.RELATIONSHIP) {
        storeScan = storeView.visitRelationships(entityTokenIds, propertyKeyIdFilter, createPropertyScanConsumer(), createTokenScanConsumer(), false, true, cacheTracer, memoryTracker);
        storeScan = new LoggingStoreScan(storeScan, false);
    } else {
        storeScan = storeView.visitNodes(entityTokenIds, propertyKeyIdFilter, createPropertyScanConsumer(), createTokenScanConsumer(), false, true, cacheTracer, memoryTracker);
        storeScan = new LoggingStoreScan(storeScan, true);
    }
    storeScan.setPhaseTracker(phaseTracker);
    return storeScan;
}
Also used : Arrays(java.util.Arrays) Log(org.neo4j.logging.Log) CursorContext(org.neo4j.io.pagecache.context.CursorContext) TokenNameLookup(org.neo4j.common.TokenNameLookup) IndexType(org.neo4j.internal.schema.IndexType) IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) Config(org.neo4j.configuration.Config) Value(org.neo4j.values.storable.Value) IntPredicate(java.util.function.IntPredicate) IndexStatisticsStore(org.neo4j.kernel.impl.api.index.stats.IndexStatisticsStore) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) IndexPopulationFailedKernelException(org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException) Map(java.util.Map) PageCacheTracer(org.neo4j.io.pagecache.tracing.PageCacheTracer) VisibleForTesting(org.neo4j.util.VisibleForTesting) JobMonitoringParams(org.neo4j.scheduler.JobMonitoringParams) PopulationProgress(org.neo4j.internal.kernel.api.PopulationProgress) IndexEntryUpdate(org.neo4j.storageengine.api.IndexEntryUpdate) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) List(java.util.List) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) EntityType(org.neo4j.common.EntityType) Queue(java.util.Queue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IntStream(java.util.stream.IntStream) JobHandle(org.neo4j.scheduler.JobHandle) InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) LogProvider(org.neo4j.logging.LogProvider) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) EntityUpdates(org.neo4j.storageengine.api.EntityUpdates) ThrowingConsumer(org.neo4j.function.ThrowingConsumer) IndexPopulationFailure.failure(org.neo4j.kernel.impl.api.index.IndexPopulationFailure.failure) GraphDatabaseInternalSettings(org.neo4j.configuration.GraphDatabaseInternalSettings) JobScheduler(org.neo4j.scheduler.JobScheduler) MemoryTracker(org.neo4j.memory.MemoryTracker) NodePropertyAccessor(org.neo4j.storageengine.api.NodePropertyAccessor) IndexSample(org.neo4j.kernel.api.index.IndexSample) LOOKUP(org.neo4j.internal.schema.IndexType.LOOKUP) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Group(org.neo4j.scheduler.Group) Subject(org.neo4j.common.Subject) ArrayIterate.contains(org.eclipse.collections.impl.utility.ArrayIterate.contains) FlipFailedKernelException(org.neo4j.kernel.api.exceptions.index.FlipFailedKernelException) IOException(java.io.IOException) TokenIndexEntryUpdate(org.neo4j.storageengine.api.TokenIndexEntryUpdate) AtomicLong(java.util.concurrent.atomic.AtomicLong) SchemaDescriptorSupplier(org.neo4j.internal.schema.SchemaDescriptorSupplier) Pair(org.neo4j.internal.helpers.collection.Pair) SchemaState(org.neo4j.internal.schema.SchemaState) IntPredicate(java.util.function.IntPredicate)

Example 30 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class DistinctSet method createDistinctSet.

public static <T extends Measurable> DistinctSet<T> createDistinctSet(MemoryTracker memoryTracker) {
    MemoryTracker scopedMemoryTracker = memoryTracker.getScopedMemoryTracker();
    scopedMemoryTracker.allocateHeap(SHALLOW_SIZE + SCOPED_MEMORY_TRACKER_SHALLOW_SIZE);
    return new DistinctSet<>(scopedMemoryTracker);
}
Also used : MemoryTracker(org.neo4j.memory.MemoryTracker)

Aggregations

MemoryTracker (org.neo4j.memory.MemoryTracker)44 Test (org.junit.jupiter.api.Test)22 LocalMemoryTracker (org.neo4j.memory.LocalMemoryTracker)10 CursorContext (org.neo4j.io.pagecache.context.CursorContext)9 Value (org.neo4j.values.storable.Value)6 Transaction (org.neo4j.graphdb.Transaction)5 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)5 Map (java.util.Map)4 Optional (java.util.Optional)4 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)4 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)4 Log (org.neo4j.logging.Log)4 ValueTuple (org.neo4j.values.storable.ValueTuple)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 Collections.emptyMap (java.util.Collections.emptyMap)3 Objects.requireNonNullElse (java.util.Objects.requireNonNullElse)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2