Skip to content

Commit

Permalink
Merge 3.2 into 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko committed Nov 15, 2017
2 parents c0b3ded + 879a3f7 commit 008a1e0
Show file tree
Hide file tree
Showing 20 changed files with 752 additions and 18 deletions.
30 changes: 29 additions & 1 deletion community/io/src/main/java/org/neo4j/io/ByteUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
*/
package org.neo4j.io;

import java.util.Locale;

import static java.lang.String.format;

/**
* A ByteUnit is a unit for a quantity of bytes.
* <p>
Expand All @@ -43,7 +47,11 @@ public enum ByteUnit
GibiByte( 3, "GiB" ),
TebiByte( 4, "TiB" ),
PebiByte( 5, "PiB" ),
ExbiByte( 6, "EiB" ),;
ExbiByte( 6, "EiB" );

public static final long ONE_KIBI_BYTE = ByteUnit.KibiByte.toBytes( 1 );
public static final long ONE_MEBI_BYTE = ByteUnit.MebiByte.toBytes( 1 );
public static final long ONE_GIBI_BYTE = ByteUnit.GibiByte.toBytes( 1 );

private static final long EIC_MULTIPLIER = 1024;

Expand Down Expand Up @@ -166,4 +174,24 @@ public static long exbiBytes( long exbibytes )
{
return ExbiByte.toBytes( exbibytes );
}

public static String bytesToString( long bytes )
{
if ( bytes > ONE_GIBI_BYTE )
{
return format( Locale.ROOT, "%.4g%s", bytes / (double) ONE_GIBI_BYTE, GibiByte.shortName );
}
else if ( bytes > ONE_MEBI_BYTE )
{
return format( Locale.ROOT, "%.4g%s", bytes / (double) ONE_MEBI_BYTE, MebiByte.shortName );
}
else if ( bytes > ONE_KIBI_BYTE )
{
return format( Locale.ROOT, "%.4g%s", bytes / (double) ONE_KIBI_BYTE, KibiByte.shortName );
}
else
{
return bytes + Byte.shortName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,14 @@ private void flushAllPages( IOLimiter limiter ) throws IOException
{
try ( MajorFlushEvent cacheFlush = pageCacheTracer.beginCacheFlush() )
{
FlushEventOpportunity flushOpportunity = cacheFlush.flushEventOpportunity();
FileMapping fileMapping = mappedFiles;
while ( fileMapping != null )
{
fileMapping.pagedFile.flushAndForceInternal( flushOpportunity, false, limiter );
try ( MajorFlushEvent fileFlush = pageCacheTracer.beginFileFlush( fileMapping.pagedFile.swapper ) )
{
FlushEventOpportunity flushOpportunity = fileFlush.flushEventOpportunity();
fileMapping.pagedFile.flushAndForceInternal( flushOpportunity, false, limiter );
}
fileMapping = fileMapping.next;
}
syncDevice();
Expand Down
17 changes: 17 additions & 0 deletions community/io/src/test/java/org/neo4j/io/ByteUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class ByteUnitTest
Expand Down Expand Up @@ -149,4 +150,20 @@ public void unitsAsBytes() throws Exception
assertThat( ByteUnit.pebiBytes( 1 ), is( 1125899906842624L ) );
assertThat( ByteUnit.exbiBytes( 1 ), is( 1152921504606846976L ) );
}

@Test
public void bytesToString()
{
assertEquals( "1B", ByteUnit.bytesToString( 1 ) );
assertEquals( "10B", ByteUnit.bytesToString( 10 ) );
assertEquals( "1000B", ByteUnit.bytesToString( 1000 ) );
assertEquals( "1.001KiB", ByteUnit.bytesToString( 1025 ) );
assertEquals( "10.01KiB", ByteUnit.bytesToString( 10250 ) );
assertEquals( "100.1KiB", ByteUnit.bytesToString( 102500 ) );
assertEquals( "1001KiB", ByteUnit.bytesToString( 1025000 ) );
assertEquals( "9.775MiB", ByteUnit.bytesToString( 10250000 ) );
assertEquals( "97.75MiB", ByteUnit.bytesToString( 102500000 ) );
assertEquals( "977.5MiB", ByteUnit.bytesToString( 1025000000 ) );
assertEquals( "9.546GiB", ByteUnit.bytesToString( 10250000000L) );
}
}
57 changes: 57 additions & 0 deletions community/kernel/src/main/java/org/neo4j/helpers/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

public final class TimeUtil
{
public static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.SECONDS;
Expand Down Expand Up @@ -68,6 +76,55 @@ public final class TimeUtil
}
};

public static String nanosToString( long nanos )
{
assert nanos >= 0;
long nanoSeconds = nanos;
StringBuilder timeString = new StringBuilder();

long days = DAYS.convert( nanoSeconds, NANOSECONDS );
if ( days > 0 )
{
nanoSeconds -= DAYS.toNanos( days );
timeString.append( days ).append( "d" );
}
long hours = HOURS.convert( nanoSeconds, NANOSECONDS );
if ( hours > 0 )
{
nanoSeconds -= HOURS.toNanos( hours );
timeString.append( hours ).append( "h" );
}
long minutes = MINUTES.convert( nanoSeconds, NANOSECONDS );
if ( minutes > 0 )
{
nanoSeconds -= MINUTES.toNanos( minutes );
timeString.append( minutes ).append( "m" );
}
long seconds = SECONDS.convert( nanoSeconds, NANOSECONDS );
if ( seconds > 0 )
{
nanoSeconds -= SECONDS.toNanos( seconds );
timeString.append( seconds ).append( "s" );
}
long milliseconds = MILLISECONDS.convert( nanoSeconds, NANOSECONDS );
if ( milliseconds > 0 )
{
nanoSeconds -= MILLISECONDS.toNanos( milliseconds );
timeString.append( milliseconds ).append( "ms" );
}
long microseconds = MICROSECONDS.convert( nanoSeconds, NANOSECONDS );
if ( microseconds > 0 )
{
nanoSeconds -= MICROSECONDS.toNanos( microseconds );
timeString.append( microseconds ).append( "μs" );
}
if ( nanoSeconds > 0 || timeString.length() == 0 )
{
timeString.append( nanoSeconds ).append( "ns" );
}
return timeString.toString();
}

private TimeUtil()
{
throw new AssertionError(); // no instances
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public PlatformModule( File providedStoreDir, Config config, DatabaseInfo databa

String desiredImplementationName = config.get( GraphDatabaseFacadeFactory.Configuration.tracer );
tracers = dependencies.satisfyDependency( new Tracers( desiredImplementationName,
logging.getInternalLog( Tracers.class ), monitors, jobScheduler ) );
logging.getInternalLog( Tracers.class ), monitors, jobScheduler, clock ) );
dependencies.satisfyDependency( tracers.pageCacheTracer );
dependencies.satisfyDependency( firstImplementor(
LogRotationMonitor.class, tracers.transactionTracer, LogRotationMonitor.NULL ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.neo4j.kernel.impl.transaction.tracing.TransactionTracer;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.Log;
import org.neo4j.time.SystemNanoClock;

/**
* The default TracerFactory, when nothing else is otherwise configured.
Expand All @@ -40,7 +42,8 @@ public String getImplementationName()
}

@Override
public PageCacheTracer createPageCacheTracer( Monitors monitors, JobScheduler jobScheduler )
public PageCacheTracer createPageCacheTracer( Monitors monitors, JobScheduler jobScheduler, SystemNanoClock clock,
Log log )
{
return new DefaultPageCacheTracer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.neo4j.kernel.impl.transaction.tracing.TransactionTracer;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.Log;
import org.neo4j.time.SystemNanoClock;

/**
* A TracerFactory determines the implementation of the tracers, that a database should use. Each implementation has
Expand All @@ -46,9 +48,11 @@ public interface TracerFactory
*
* @param monitors the monitoring manager
* @param jobScheduler a scheduler for async jobs
* @param clock system nano clock
* @param log log
* @return The created instance.
*/
PageCacheTracer createPageCacheTracer( Monitors monitors, JobScheduler jobScheduler );
PageCacheTracer createPageCacheTracer( Monitors monitors, JobScheduler jobScheduler, SystemNanoClock clock, Log log );

/**
* Create a new TransactionTracer instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.Log;
import org.neo4j.time.SystemNanoClock;

/**
* <h1>Tracers</h1>
Expand Down Expand Up @@ -115,7 +116,8 @@ public class Tracers
* @param monitors the monitoring manager
* @param jobScheduler a scheduler for async jobs
*/
public Tracers( String desiredImplementationName, Log msgLog, Monitors monitors, JobScheduler jobScheduler )
public Tracers( String desiredImplementationName, Log msgLog, Monitors monitors, JobScheduler jobScheduler,
SystemNanoClock clock )
{
if ( "null".equalsIgnoreCase( desiredImplementationName ) )
{
Expand Down Expand Up @@ -153,7 +155,7 @@ public Tracers( String desiredImplementationName, Log msgLog, Monitors monitors,
}

pageCursorTracerSupplier = foundFactory.createPageCursorTracerSupplier( monitors, jobScheduler );
pageCacheTracer = foundFactory.createPageCacheTracer( monitors, jobScheduler );
pageCacheTracer = foundFactory.createPageCacheTracer( monitors, jobScheduler, clock, msgLog );
transactionTracer = foundFactory.createTransactionTracer( monitors, jobScheduler );
checkPointTracer = foundFactory.createCheckPointTracer( monitors, jobScheduler );
lockTracer = foundFactory.createLockTracer( monitors, jobScheduler );
Expand Down
44 changes: 44 additions & 0 deletions community/kernel/src/test/java/org/neo4j/helpers/TimeUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.helpers;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.neo4j.helpers.TimeUtil.nanosToString;

public class TimeUtilTest
{
@Test
public void formatNanosToString()
{
assertEquals( "1ns", nanosToString( 1 ) );
assertEquals( "10ns", nanosToString( 10 ) );
assertEquals( "100ns", nanosToString( 100 ) );
assertEquals( "1μs", nanosToString( 1000 ) );
assertEquals( "10μs100ns", nanosToString( 10100 ) );
assertEquals( "101μs10ns", nanosToString( 101010 ) );
assertEquals( "10ms101μs10ns", nanosToString( 10101010 ) );
assertEquals( "1s20ms304μs50ns", nanosToString( 1020304050 ) );
assertEquals( "1m42s30ms405μs60ns", nanosToString( 102030405060L ) );
assertEquals( "2h50m3s40ms506μs70ns", nanosToString( 10203040506070L ) );
assertEquals( "11d19h25m4s50ms607μs80ns", nanosToString( 1020304050607080L ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
import org.neo4j.test.OtherThreadExecutor;
import org.neo4j.test.Race;
import org.neo4j.test.rule.concurrent.OtherThreadRule;
import org.neo4j.time.Clocks;
import org.neo4j.time.SystemNanoClock;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.locks.LockSupport.parkNanos;
Expand Down Expand Up @@ -107,7 +109,7 @@ public class KernelTransactionsTest
public final ExpectedException expectedException = ExpectedException.none();

private static final long TEST_TIMEOUT = 10_000;
private static final Clock clock = Clock.systemUTC();
private static final SystemNanoClock clock = Clocks.nanoClock();
private static AvailabilityGuard availabilityGuard;

@Before
Expand Down Expand Up @@ -529,7 +531,8 @@ private static KernelTransactions newKernelTransactions( Locks locks, StorageEng
TransactionIdStore transactionIdStore = mock( TransactionIdStore.class );
when( transactionIdStore.getLastCommittedTransaction() ).thenReturn( new TransactionId( 0, 0, 0 ) );

Tracers tracers = new Tracers( "null", NullLog.getInstance(), new Monitors(), mock( JobScheduler.class ) );
Tracers tracers = new Tracers( "null", NullLog.getInstance(), new Monitors(), mock( JobScheduler.class ),
clock );
StatementLocksFactory statementLocksFactory = new SimpleStatementLocksFactory( locks );

StatementOperationParts statementOperations = mock( StatementOperationParts.class );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.AssertableLogProvider;
import org.neo4j.logging.Log;
import org.neo4j.time.Clocks;
import org.neo4j.time.SystemNanoClock;

import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
Expand All @@ -43,6 +45,7 @@ public class TracersTest
{
private final AssertableLogProvider logProvider = new AssertableLogProvider();
private final JobScheduler jobScheduler = mock( JobScheduler.class );
private final SystemNanoClock clock = Clocks.nanoClock();
private final Monitors monitors = new Monitors();

private Log log;
Expand All @@ -57,7 +60,7 @@ public void setUp()
@Test
public void mustProduceNullImplementationsWhenRequested() throws Exception
{
Tracers tracers = new Tracers( "null", log, monitors, jobScheduler );
Tracers tracers = createTracers( "null" );
assertThat( tracers.pageCacheTracer, is( PageCacheTracer.NULL ) );
assertThat( tracers.pageCursorTracerSupplier, is( PageCursorTracerSupplier.NULL ) );
assertThat( tracers.transactionTracer, is( TransactionTracer.NULL ) );
Expand All @@ -67,7 +70,7 @@ public void mustProduceNullImplementationsWhenRequested() throws Exception
@Test
public void mustProduceNullImplementationsWhenRequestedIgnoringCase() throws Exception
{
Tracers tracers = new Tracers( "NuLl", log, monitors, jobScheduler );
Tracers tracers = createTracers( "NuLl" );
assertThat( tracers.pageCacheTracer, is( PageCacheTracer.NULL ) );
assertThat( tracers.pageCursorTracerSupplier, is( PageCursorTracerSupplier.NULL ) );
assertThat( tracers.transactionTracer, is( TransactionTracer.NULL ) );
Expand All @@ -77,35 +80,40 @@ public void mustProduceNullImplementationsWhenRequestedIgnoringCase() throws Exc
@Test
public void mustProduceDefaultImplementationForNullConfiguration() throws Exception
{
Tracers tracers = new Tracers( null, log, monitors, jobScheduler );
Tracers tracers = createTracers( null );
assertDefaultImplementation( tracers );
assertNoWarning();
}

@Test
public void mustProduceDefaultImplementationWhenRequested() throws Exception
{
Tracers tracers = new Tracers( "default", log, monitors, jobScheduler );
Tracers tracers = createTracers( "default" );
assertDefaultImplementation( tracers );
assertNoWarning();
}

@Test
public void mustProduceDefaultImplementationWhenRequestedIgnoringCase() throws Exception
{
Tracers tracers = new Tracers( "DeFaUlT", log, monitors, jobScheduler );
Tracers tracers = createTracers( "DeFaUlT" );
assertDefaultImplementation( tracers );
assertNoWarning();
}

@Test
public void mustProduceDefaultImplementationWhenRequestingUnknownImplementation() throws Exception
{
Tracers tracers = new Tracers( "there's nothing like this", log, monitors, jobScheduler );
Tracers tracers = createTracers( "there's nothing like this" );
assertDefaultImplementation( tracers );
assertWarning( "there's nothing like this" );
}

private Tracers createTracers( String s )
{
return new Tracers( s, log, monitors, jobScheduler, clock );
}

private void assertDefaultImplementation( Tracers tracers )
{
assertThat( tracers.pageCacheTracer, instanceOf( DefaultPageCacheTracer.class ) );
Expand Down

0 comments on commit 008a1e0

Please sign in to comment.