Skip to content

Commit

Permalink
Pr comments and post rebase adaptations.
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko committed Nov 10, 2017
1 parent 2c8921c commit a0af80e
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 69 deletions.
Expand Up @@ -67,12 +67,12 @@ public void reportEvents()
if ( hits > 0 )
{
pageCacheTracer.hits( hits );
historicalHits = Math.addExact( historicalHits, hits );
historicalHits = historicalHits + hits;
}
if ( faults > 0 )
{
pageCacheTracer.faults( faults );
historicalFaults = Math.addExact( historicalFaults, faults );
historicalFaults = historicalFaults + faults;
}
if ( bytesRead > 0 )
{
Expand Down Expand Up @@ -100,13 +100,13 @@ public void reportEvents()
@Override
public long accumulatedHits()
{
return Math.addExact( historicalHits, hits );
return historicalHits + hits;
}

@Override
public long accumulatedFaults()
{
return Math.addExact( historicalFaults, faults );
return historicalFaults + faults;
}

private void reset()
Expand Down
Expand Up @@ -109,12 +109,20 @@ public interface KernelTransactionHandle
boolean isUnderlyingTransaction( KernelTransaction tx );

/**
* User transaction id of underlying transaction. User transaction id is positive long number.
* User transaction id of underlying transaction. User transaction id is a not negative long number.
* Should be unique across transactions.
* @return user transaction id
*/
long getUserTransactionId();

/**
* User transaction name of the underlying transaction.
* User transaction name consists of the name prefix and user transaction id.
* Should be unique across transactions.
* @return user transaction name
*/
String getUserTransactionName();

/**
* @return a list of all queries currently executing that use the underlying transaction
*/
Expand Down
Expand Up @@ -58,15 +58,15 @@ boolean isPlanning()

/**
* Is query waiting on a locks
* @return true if waiting on a locks, false otherwise
* @return true if waiting on locks, false otherwise
*/
boolean isWaitingOnLocks()
{
return false;
}

/**
* List of locks query is waiting on. Will be empty for all of the statuses except
* List of locks query is waiting on. Will be empty for all of the statuses except for {@link WaitingOnLock}.
* @return list of locks query is waiting on, empty list if query is not waiting.
*/
List<ActiveLock> waitingOnLocks()
Expand Down
Expand Up @@ -39,6 +39,8 @@
*/
class KernelTransactionImplementationHandle implements KernelTransactionHandle
{
private static final String USER_TRANSACTION_NAME_PREFIX = "transaction-";

private final long txReuseCount;
private final long lastTransactionIdWhenStarted;
private final long lastTransactionTimestampWhenStarted;
Expand Down Expand Up @@ -134,6 +136,12 @@ public long getUserTransactionId()
return userTransactionId;
}

@Override
public String getUserTransactionName()
{
return USER_TRANSACTION_NAME_PREFIX + getUserTransactionId();
}

@Override
public Stream<ExecutingQuery> executingQueries()
{
Expand Down
Expand Up @@ -87,7 +87,7 @@ public class KernelTransactions extends LifecycleAdapter implements Supplier<Ker
private final Supplier<ExplicitIndexTransactionState> explicitIndexTxStateSupplier;
private final SystemNanoClock clock;
private final ReentrantReadWriteLock newTransactionsLock = new ReentrantReadWriteLock();
private final MonotonicCounter userTransactionIdCounter = MonotonicCounter.newAtomicMonotonicCounter();
private final MonotonicCounter userTransactionIdCounter = MonotonicCounter.newCounter();

/**
* Used to enumerate all transactions in the system, active and idle ones.
Expand Down
Expand Up @@ -32,7 +32,7 @@

public class StackingQueryRegistrationOperations implements QueryRegistrationOperations
{
private final MonotonicCounter lastQueryId = MonotonicCounter.newAtomicMonotonicCounter();
private final MonotonicCounter lastQueryId = MonotonicCounter.newCounter();
private final SystemNanoClock clock;
private final CpuClock cpuClock;
private final HeapAllocation heapAllocation;
Expand Down
Expand Up @@ -21,29 +21,54 @@

import java.util.concurrent.atomic.AtomicLong;

/**
* Counter that produce sequence of incremental numbers
*/
public interface MonotonicCounter
{
long increment();

static MonotonicCounter newAtomicMonotonicCounter()
/**
* Create new counter with specified initial value
* @param initialValue initial value
* @return counter newly created counter
*/
static MonotonicCounter newCounter( int initialValue )
{
return new MonotonicCounter()
assert initialValue >= 0;
return new NaturalCounter( initialValue );
}

/**
* Create new counter with default 0 as its initial value
* @return counter newly created counter
*/
static MonotonicCounter newCounter()
{
return new NaturalCounter( 0 );
}

class NaturalCounter implements MonotonicCounter
{
private final AtomicLong value;

NaturalCounter( int initialValue )
{
private final AtomicLong value = new AtomicLong( 0L );
value = new AtomicLong( initialValue );
}

@Override
public long increment()
@Override
public long increment()
{
int initialValue;
int incrementedValue;
do
{
int initialValue;
int incrementedValue;
do
{
initialValue = value.intValue();
incrementedValue = initialValue < 0 ? 0 : initialValue + 1;
}
while ( !value.compareAndSet( initialValue, incrementedValue ) );
return incrementedValue;
initialValue = value.intValue();
incrementedValue = initialValue == Integer.MAX_VALUE ? 0 : initialValue + 1;
}
};
while ( !value.compareAndSet( initialValue, incrementedValue ) );
return incrementedValue;
}
}
}
Expand Up @@ -37,6 +37,7 @@
*/
public class TestKernelTransactionHandle implements KernelTransactionHandle
{
private static final String USER_TRANSACTION_NAME_PREFIX = "transaction-";
private final KernelTransaction tx;

public TestKernelTransactionHandle( KernelTransaction tx )
Expand Down Expand Up @@ -111,6 +112,12 @@ public long getUserTransactionId()
return tx.getTransactionId();
}

@Override
public String getUserTransactionName()
{
return USER_TRANSACTION_NAME_PREFIX + getUserTransactionId();
}

@Override
public Stream<ExecutingQuery> executingQueries()
{
Expand Down
@@ -0,0 +1,37 @@
/*
* 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.util;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class MonotonicCounterTest
{
@Test
public void counterProducePositiveNumbers()
{
MonotonicCounter monotonicCounter = MonotonicCounter.newCounter( Integer.MAX_VALUE - 10 );
for ( int i = 0; i < 100; i++ )
{
assertTrue( monotonicCounter.increment() >= 0 );
}
}
}
Expand Up @@ -168,7 +168,7 @@ private String describe( Set<KernelTransactionHandle> allBlockers )
StringJoiner stringJoiner = new StringJoiner( ", ", "[", "]" );
for ( KernelTransactionHandle blocker : allBlockers )
{
stringJoiner.add( TransactionId.ofTransactionHandle( blocker ) );
stringJoiner.add( blocker.getUserTransactionName() );
}
return stringJoiner.toString();
}
Expand Down

This file was deleted.

Expand Up @@ -68,7 +68,7 @@ public TransactionStatusResult( KernelTransactionHandle transaction,
TransactionDependenciesResolver transactionDependenciesResolver,
Map<KernelTransactionHandle,List<QuerySnapshot>> handleSnapshotsMap ) throws InvalidArgumentsException
{
this.transactionId = toUserTransactionId( transaction );
this.transactionId = transaction.getUserTransactionName();
this.username = transaction.securityContext().subject().username();
this.startTime = ProceduresTimeFormatHelper.formatTime( transaction.startTime() );
Optional<Status> terminationReason = transaction.terminationReason();
Expand Down Expand Up @@ -106,11 +106,6 @@ public TransactionStatusResult( KernelTransactionHandle transaction,
this.metaData = transaction.getMetaData();
}

private String toUserTransactionId( KernelTransactionHandle transaction )
{
return TransactionId.ofTransactionHandle( transaction );
}

private String getStatus( KernelTransactionHandle handle, Optional<Status> terminationReason,
TransactionDependenciesResolver transactionDependenciesResolver )
{
Expand Down
Expand Up @@ -169,7 +169,8 @@ private ExecutingQuery createExecutingQuery( long queryId )
{
return new ExecutingQuery( queryId, ClientConnectionInfo.EMBEDDED_CONNECTION, "test", "testQuey",
VirtualValues.EMPTY_MAP, Collections.emptyMap(), () -> 1L, PageCursorTracer.NULL,
Thread.currentThread(), Clocks.nanoClock(), CpuClock.NOT_AVAILABLE, HeapAllocation.NOT_AVAILABLE );
Thread.currentThread().getId(), Thread.currentThread().getName(),
Clocks.nanoClock(), CpuClock.NOT_AVAILABLE, HeapAllocation.NOT_AVAILABLE );
}

private static class TestKernelTransactionHandleWithLocks extends TestKernelTransactionHandle
Expand Down
Expand Up @@ -154,7 +154,8 @@ private QuerySnapshot createQuerySnapshot( long queryId )
private ExecutingQuery createExecutingQuery( long queryId )
{
return new ExecutingQuery( queryId, getTestConnectionInfo(), "testUser", "testQuery", VirtualValues.EMPTY_MAP,
Collections.emptyMap(), () -> 1L, PageCursorTracer.NULL, Thread.currentThread(),
Collections.emptyMap(), () -> 1L, PageCursorTracer.NULL,
Thread.currentThread().getId(), Thread.currentThread().getName(),
new CountingSystemNanoClock(), new CountingCpuClock(), new CountingHeapAllocation() );
}

Expand Down

0 comments on commit a0af80e

Please sign in to comment.