Skip to content

Commit

Permalink
Addressed comments and added some IT for the new monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Dec 4, 2017
1 parent ae92664 commit 72c1a72
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 23 deletions.
Expand Up @@ -37,7 +37,7 @@ public class Format
public static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone( "UTC" );

private static final String[] BYTE_SIZES = { "B", "kB", "MB", "GB" };
private static final String[] COUNT_SIZES = { "", "k", "M", "bn", "tr" };
private static final String[] COUNT_SIZES = { "", "k", "M", "G", "T" };

public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSSZ";
public static final String TIME_FORMAT = "HH:mm:ss.SSS";
Expand Down
Expand Up @@ -280,8 +280,7 @@ private <E extends InputEntity> long[] sample( Iterable<DataFactory<E>> dataFact
while ( dataItems.hasNext() )
{
CharReadable stream = dataItems.next();
// TODO for now only a single 1MB chunk from the start of each file is sampled.
// more samples from other places in each file could also be sampled.
// A maximum of 1MB chunk from the start of each file is sampled.
try ( CharSeeker dataStream = charSeeker( stream, config, true ) ) // sample it
{
if ( header == null )
Expand Down
Expand Up @@ -21,6 +21,8 @@

import java.io.InputStream;

import static org.neo4j.unsafe.impl.batchimport.staging.HumanUnderstandableExecutionMonitor.NO_MONITOR;

/**
* Common {@link ExecutionMonitor} implementations.
*/
Expand All @@ -40,7 +42,7 @@ public static ExecutionMonitor defaultVisible( InputStream in )
{
ProgressRestoringMonitor monitor = new ProgressRestoringMonitor();
return new MultiExecutionMonitor(
new HumanUnderstandableExecutionMonitor( System.out, monitor ),
new HumanUnderstandableExecutionMonitor( System.out, NO_MONITOR, monitor ),
new OnDemandDetailsExecutionMonitor( System.out, in, monitor ) );
}

Expand Down
Expand Up @@ -59,10 +59,40 @@
public class HumanUnderstandableExecutionMonitor implements ExecutionMonitor
{
public interface Monitor
{
void progress( ImportStage stage, int percent );
}

public static final Monitor NO_MONITOR = new Monitor()
{
@Override
public void progress( ImportStage stage, int percent )
{ // empty
}
};

public interface ExternalMonitor
{
boolean somethingElseBrokeMyNiceOutput();
}

public static final ExternalMonitor NO_EXTERNAL_MONITOR = new ExternalMonitor()
{
@Override
public boolean somethingElseBrokeMyNiceOutput()
{
return false;
}
};

enum ImportStage
{
nodeImport,
relationshipImport,
linking,
postProcessing;
}

private static final String ESTIMATED_REQUIRED_MEMORY_USAGE = "Estimated required memory usage";
private static final String ESTIMATED_DISK_SPACE_USAGE = "Estimated disk space usage";
private static final String ESTIMATED_NUMBER_OF_RELATIONSHIP_PROPERTIES = "Estimated number of relationship properties";
Expand All @@ -76,17 +106,20 @@ public interface Monitor
// assigned later on
private final PrintStream out;
private final Monitor monitor;
private final ExternalMonitor externalMonitor;
private DependencyResolver dependencyResolver;

// progress of current stage
private long goal;
private long stashedProgress;
private long progress;
private ImportStage currentStage;

public HumanUnderstandableExecutionMonitor( PrintStream out, Monitor monitor )
public HumanUnderstandableExecutionMonitor( PrintStream out, Monitor monitor, ExternalMonitor externalMonitor )
{
this.out = out;
this.monitor = monitor;
this.externalMonitor = externalMonitor;
}

@Override
Expand Down Expand Up @@ -216,7 +249,7 @@ ESTIMATED_REQUIRED_MEMORY_USAGE, bytes(
long goal = idMapper.needsPreparation()
? (long) (numberOfNodes + weighted( IdMapperPreparationStage.NAME, numberOfNodes * 4 ))
: numberOfNodes;
initializeProgress( goal );
initializeProgress( goal, ImportStage.nodeImport );
}

private void initializeRelationshipImport( Estimates estimates, IdMapper idMapper, BatchingNeoStores neoStores )
Expand All @@ -230,7 +263,7 @@ ESTIMATED_DISK_SPACE_USAGE, bytes(
ESTIMATED_REQUIRED_MEMORY_USAGE, bytes(
baselineMemoryRequirement( neoStores ) +
totalMemoryUsageOf( idMapper ) ) );
initializeProgress( numberOfRelationships );
initializeProgress( numberOfRelationships, ImportStage.relationshipImport );
}

private void initializeLinking( BatchingNeoStores neoStores,
Expand All @@ -249,7 +282,8 @@ ESTIMATED_REQUIRED_MEMORY_USAGE, bytes(
initializeProgress(
relationshipRecordIdCount + // node degrees
actualRelationshipCount * 2 + // start/end forwards, see RelationshipLinkingProgress
actualRelationshipCount * 2 // start/end backwards, see RelationshipLinkingProgress
actualRelationshipCount * 2, // start/end backwards, see RelationshipLinkingProgress
ImportStage.linking
);
}

Expand All @@ -267,19 +301,22 @@ private void initializeMisc( BatchingNeoStores neoStores, DataStatistics distrib
groupCount + // Write groups
groupCount + // Node --> Group
actualNodeCount + // Node counts
relationshipRecordIdCount ); // Relationship counts
relationshipRecordIdCount, // Relationship counts
ImportStage.postProcessing
);
}

private static long defensivelyPadMemoryEstimate( long bytes )
{
return (long) (bytes * 1.1);
}

private void initializeProgress( long goal )
private void initializeProgress( long goal, ImportStage stage )
{
this.goal = goal;
this.stashedProgress = 0;
this.progress = 0;
this.currentStage = stage;
}

private void updateProgress( long progress )
Expand All @@ -304,7 +341,9 @@ private void updateProgress( long progress )

if ( currentLine < line || currentDotOnLine == dotsPerLine() )
{
out.println( format( " %s", linePercentage( currentLine ) ) );
int percentage = percentage( currentLine );
out.println( format( " %s%%", percentage ) );
monitor.progress( currentStage, percentage );
currentLine++;
if ( currentLine == lines() )
{
Expand All @@ -318,10 +357,9 @@ private void updateProgress( long progress )
this.progress = max( this.progress, progress );
}

private static String linePercentage( int line )
private static int percentage( int line )
{
int percentage = (line + 1) * PERCENTAGES_PER_LINE;
return percentage + "%";
return (line + 1) * PERCENTAGES_PER_LINE;
}

private void printDots( int from, int target )
Expand Down Expand Up @@ -401,7 +439,7 @@ public void check( StageExecution execution )

private void reprintProgressIfNecessary()
{
if ( monitor.somethingElseBrokeMyNiceOutput() )
if ( externalMonitor.somethingElseBrokeMyNiceOutput() )
{
long prevProgress = this.progress;
long prevStashedProgress = this.stashedProgress;
Expand Down
Expand Up @@ -89,7 +89,6 @@ public OnDemandDetailsExecutionMonitor( PrintStream out, InputStream in, Monitor
this.monitor = monitor;
this.actions.put( "i", Pair.of( "Print more detailed information", this::printDetails ) );
this.actions.put( "c", Pair.of( "Print more detailed information about current stage", this::printDetailsForCurrentStage ) );
this.actions.put( "gc", Pair.of( "Trigger GC", this::triggerGC ) );
this.gcMonitor = new MeasureDoNothing( "Importer GC monitor", gcBlockTime, 100, 200 );
}

Expand Down Expand Up @@ -182,11 +181,6 @@ private static void printIndented( PrintStream out, String string )
out.println( "\t" + string );
}

private void triggerGC()
{
System.gc();
}

private void reactToUserInput()
{
try
Expand Down
Expand Up @@ -19,15 +19,16 @@
*/
package org.neo4j.unsafe.impl.batchimport.staging;

import org.neo4j.unsafe.impl.batchimport.staging.HumanUnderstandableExecutionMonitor.Monitor;
import org.neo4j.unsafe.impl.batchimport.staging.HumanUnderstandableExecutionMonitor.ExternalMonitor;
import org.neo4j.unsafe.impl.batchimport.staging.OnDemandDetailsExecutionMonitor.Monitor;

/**
* Monitor connecting a {@link HumanUnderstandableExecutionMonitor} and {@link OnDemandDetailsExecutionMonitor},
* their monitors at least for the sole purpose of notifying {@link HumanUnderstandableExecutionMonitor} about when
* there are other output interfering with it's nice progress printing. If something else gets printed it can restore its
* progress from 0..current.
*/
public class ProgressRestoringMonitor implements Monitor, org.neo4j.unsafe.impl.batchimport.staging.OnDemandDetailsExecutionMonitor.Monitor
public class ProgressRestoringMonitor implements ExternalMonitor, Monitor
{
private volatile boolean detailsPrinted;

Expand Down
@@ -0,0 +1,111 @@
/*
* 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.unsafe.impl.batchimport.staging;

import org.junit.Rule;
import org.junit.Test;

import java.util.EnumMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.neo4j.csv.reader.Extractors;
import org.neo4j.kernel.impl.logging.NullLogService;
import org.neo4j.test.rule.PageCacheAndDependenciesRule;
import org.neo4j.test.rule.RandomRule;
import org.neo4j.test.rule.SuppressOutput;
import org.neo4j.unsafe.impl.batchimport.ParallelBatchImporter;
import org.neo4j.unsafe.impl.batchimport.input.Collector;
import org.neo4j.unsafe.impl.batchimport.input.DataGeneratorInput;
import org.neo4j.unsafe.impl.batchimport.input.Input;
import org.neo4j.unsafe.impl.batchimport.input.SimpleDataGenerator;
import org.neo4j.unsafe.impl.batchimport.input.csv.IdType;
import org.neo4j.unsafe.impl.batchimport.staging.HumanUnderstandableExecutionMonitor.ImportStage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.neo4j.kernel.configuration.Config.defaults;
import static org.neo4j.kernel.impl.store.format.standard.Standard.LATEST_RECORD_FORMATS;
import static org.neo4j.unsafe.impl.batchimport.AdditionalInitialIds.EMPTY;
import static org.neo4j.unsafe.impl.batchimport.Configuration.DEFAULT;
import static org.neo4j.unsafe.impl.batchimport.input.DataGeneratorInput.bareboneNodeHeader;
import static org.neo4j.unsafe.impl.batchimport.input.DataGeneratorInput.bareboneRelationshipHeader;
import static org.neo4j.unsafe.impl.batchimport.input.csv.IdType.INTEGER;
import static org.neo4j.unsafe.impl.batchimport.staging.HumanUnderstandableExecutionMonitor.NO_EXTERNAL_MONITOR;

public class HumanUnderstandableExecutionMonitorIT
{
private static final long NODE_COUNT = 1_000;
private static final long RELATIONSHIP_COUNT = 10_000;

@Rule
public final RandomRule random = new RandomRule();

@Rule
public final PageCacheAndDependenciesRule storage = new PageCacheAndDependenciesRule();

@Rule
public final SuppressOutput suppressOutput = SuppressOutput.suppressAll();

@Test
public void shouldReportProgressOfNodeImport() throws Exception
{
// given
CapturingMonitor progress = new CapturingMonitor();
HumanUnderstandableExecutionMonitor monitor = new HumanUnderstandableExecutionMonitor( System.out, progress, NO_EXTERNAL_MONITOR );
IdType idType = INTEGER;
SimpleDataGenerator generator = new SimpleDataGenerator( bareboneNodeHeader( idType, new Extractors( ';' ) ),
bareboneRelationshipHeader( idType, new Extractors( ';' ) ), random.seed(), NODE_COUNT, 1, 1, idType, 0, 0 );
Input input = new DataGeneratorInput( NODE_COUNT, RELATIONSHIP_COUNT, generator.nodes(), generator.relationships(), idType,
Collector.EMPTY );

// when
new ParallelBatchImporter( storage.directory().absolutePath(), storage.fileSystem(), storage.pageCache(), DEFAULT,
NullLogService.getInstance(), monitor, EMPTY, defaults(), LATEST_RECORD_FORMATS ).doImport( input );

// then
progress.assertAllProgressReachedEnd();
}

private static class CapturingMonitor implements HumanUnderstandableExecutionMonitor.Monitor
{
final EnumMap<ImportStage,AtomicInteger> progress = new EnumMap<>( ImportStage.class );

@Override
public void progress( ImportStage stage, int percent )
{
if ( percent > 100 )
{
fail( "Expected percentage to be 0..100% but was " + percent );
}

AtomicInteger stageProgress = progress.computeIfAbsent( stage, s -> new AtomicInteger() );
int previous = stageProgress.getAndSet( percent );
if ( previous > percent )
{
fail( "Progress should go forwards only, but went from " + previous + " to " + percent );
}
}

void assertAllProgressReachedEnd()
{
assertEquals( ImportStage.values().length, progress.size() );
progress.values().forEach( p -> assertEquals( 100, p.get() ) );
}
}
}

0 comments on commit 72c1a72

Please sign in to comment.