Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
NEXUS-5372: Applied Igor's comments.
Browse files Browse the repository at this point in the history
Changes:
* V1 and V2 support removed (they were busted anyway)
* All the UTs now operate with much higher records that before (intentionally
exceeding the problematic 255 bytes message length)
* UTs made stricter about expectations, as all the new "persist" data are actually
two "segments" with 53+47=100 records
  • Loading branch information
cstamas committed Nov 21, 2012
1 parent b2c1f7a commit 4879784
Show file tree
Hide file tree
Showing 18 changed files with 1,902 additions and 162 deletions.
Expand Up @@ -46,12 +46,6 @@
*/ */
public class DefaultTimelinePersistor public class DefaultTimelinePersistor
{ {

private enum Version
{
V1, V2, V3;
}

// == V3 // == V3


private static final String V3_DATA_FILE_NAME_PREFIX = "timeline."; private static final String V3_DATA_FILE_NAME_PREFIX = "timeline.";
Expand All @@ -64,31 +58,6 @@ private enum Version
".", "\\." ) + "(\\d{4}-\\d{2}-\\d{2}\\.\\d{2}-\\d{2}-\\d{2}[+-]\\d{4})" + V3_DATA_FILE_NAME_SUFFIX.replace( ".", "\\." ) + "(\\d{4}-\\d{2}-\\d{2}\\.\\d{2}-\\d{2}-\\d{2}[+-]\\d{4})" + V3_DATA_FILE_NAME_SUFFIX.replace(
".", "\\." ) + "$" ); ".", "\\." ) + "$" );


// == V2

@Deprecated
private static final String V2_DATA_FILE_NAME_PREFIX = V3_DATA_FILE_NAME_PREFIX;

@Deprecated
private static final String V2_DATA_FILE_NAME_SUFFIX = ".dat";

@Deprecated
private static final String V2_DATA_FILE_NAME_DATE_FORMAT = V3_DATA_FILE_NAME_DATE_FORMAT;

@Deprecated
private static final Pattern V2_DATA_FILE_NAME_PATTERN = Pattern.compile( "^" + V2_DATA_FILE_NAME_PREFIX.replace(
".", "\\." ) + "(\\d{4}-\\d{2}-\\d{2}\\.\\d{2}-\\d{2}-\\d{2}[+-]\\d{4})" + V2_DATA_FILE_NAME_SUFFIX.replace(
".", "\\." ) + "$" );

// == V1

@Deprecated
private static final String V1_DATA_FILE_NAME_DATE_FORMAT = "yyyy-MM-dd.HH-mm-ss";

@Deprecated
private static final Pattern V1_DATA_FILE_NAME_PATTERN =
Pattern.compile( "^timeline\\.(\\d{4}-\\d{2}-\\d{2}\\.\\d{2}-\\d{2}-\\d{2})\\.dat$" );

// == // ==


private int rollingIntervalMillis; private int rollingIntervalMillis;
Expand Down Expand Up @@ -186,9 +155,7 @@ protected void readAllSinceDays( final int days, final TimelineCallback callback
{ {
public boolean accept( File dir, String fname ) public boolean accept( File dir, String fname )
{ {
return V3_DATA_FILE_NAME_PATTERN.matcher( fname ).matches() return V3_DATA_FILE_NAME_PATTERN.matcher( fname ).matches();
|| V2_DATA_FILE_NAME_PATTERN.matcher( fname ).matches()
|| V1_DATA_FILE_NAME_PATTERN.matcher( fname ).matches();
} }
} ); } );


Expand Down Expand Up @@ -265,11 +232,8 @@ else if ( filePtr >= result.size() )
else else
{ {
final File file = result.get( filePtr ); final File file = result.get( filePtr );
// V1 and V2 uses same format, so for reading telling is it V3 or less is enough
final Version version =
V3_DATA_FILE_NAME_PATTERN.matcher( file.getName() ).matches() ? Version.V3 : Version.V2;
// jump to next file // jump to next file
currentIterator = readFile( version, file ); currentIterator = readFile( file );
filePtr++; filePtr++;
continue; continue;
} }
Expand All @@ -283,33 +247,19 @@ else if ( filePtr >= result.size() )
* @param file * @param file
* @return * @return
*/ */
protected Iterator<TimelineRecord> readFile( Version version, File file ) protected Iterator<TimelineRecord> readFile( File file )
{ {
final ArrayList<TimelineRecord> result = new ArrayList<TimelineRecord>(); final ArrayList<TimelineRecord> result = new ArrayList<TimelineRecord>();
InputStream in = null; InputStream in = null;
try try
{ {
in = new FileInputStream( file ); in = new FileInputStream( file );
if ( Version.V3 == version ) // V3 uses delimited format
{ TimelineRecord rec = fromProto( TimeLineRecordProtos.TimeLineRecord.parseDelimitedFrom( in ) );
// V3 uses delimited format while ( rec != null )
TimelineRecord rec = fromProto( TimeLineRecordProtos.TimeLineRecord.parseDelimitedFrom( in ) );
while ( rec != null )
{
result.add( rec );
rec = fromProto( TimeLineRecordProtos.TimeLineRecord.parseDelimitedFrom( in ) );
}
}
else
{ {
// V1 and V2 suffers from NEXUS-5372 result.add( rec );
while ( in.available() > 0 ) rec = fromProto( TimeLineRecordProtos.TimeLineRecord.parseDelimitedFrom( in ) );
{
int length = in.read();
byte[] bytes = new byte[length];
in.read( bytes, 0, length );
result.add( fromProto( TimeLineRecordProtos.TimeLineRecord.parseFrom( bytes ) ) );
}
} }
} }
catch ( Exception e ) catch ( Exception e )
Expand Down Expand Up @@ -366,34 +316,6 @@ protected long getTimestampedFileNameTimestamp( final File file )
} }
} }


final Matcher v2FnMatcher = V2_DATA_FILE_NAME_PATTERN.matcher( file.getName() );
if ( v2FnMatcher.matches() )
{
final String datePattern = v2FnMatcher.group( 1 );
try
{
return new SimpleDateFormat( V2_DATA_FILE_NAME_DATE_FORMAT ).parse( datePattern ).getTime();
}
catch ( ParseException e )
{
// silently go to next try
}
}

final Matcher v1FnMatcher = V1_DATA_FILE_NAME_PATTERN.matcher( file.getName() );
if ( v1FnMatcher.matches() )
{
final String datePattern = v1FnMatcher.group( 1 );
try
{
return new SimpleDateFormat( V1_DATA_FILE_NAME_DATE_FORMAT ).parse( datePattern ).getTime();
}
catch ( ParseException e )
{
// silently go to next try
}
}

// fallback to lastModified // fallback to lastModified
return file.lastModified(); return file.lastModified();
} }
Expand Down
Expand Up @@ -25,6 +25,17 @@
public abstract class AbstractTimelineTestCase public abstract class AbstractTimelineTestCase
extends InjectedTestCase extends InjectedTestCase
{ {

private static final String loremIpsum =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur rutrum urna ac est sagittis in lacinia "
+ "tortor porta. Maecenas accumsan hendrerit nulla vel lobortis. Phasellus purus sapien, fermentum non "
+ "aliquet vitae, vulputate a nulla. Nunc a diam eget augue accumsan suscipit nec at tellus. Donec sit "
+ "amet tellus mi, vitae gravida justo. Nulla iaculis ullamcorper sodales. Pellentesque ut viverra tellus. "
+ "Pellentesque quis lacus velit. Nullam nec orci id ante pharetra dictum. In tincidunt fringilla metus, "
+ "id faucibus ligula condimentum id. Aenean augue odio, auctor fermentum sodales non, gravida sit amet "
+ "diam. Maecenas ac dolor at lorem ullamcorper pretium convallis ut felis. Mauris ut nisi ut leo "
+ "fringilla auctor sed et lectus.";

protected DefaultTimeline timeline; protected DefaultTimeline timeline;


@Override @Override
Expand Down Expand Up @@ -68,18 +79,20 @@ protected TimelineRecord createTimelineRecord( final long ts )
data.put( "k1", "v1" ); data.put( "k1", "v1" );
data.put( "k2", "v2" ); data.put( "k2", "v2" );
data.put( "k3", "v3" ); data.put( "k3", "v3" );
data.put( "k4", loremIpsum );
return createTimelineRecord( ts, "type", "subType", data ); return createTimelineRecord( ts, "type", "subType", data );
} }


protected TimelineRecord createTimelineRecord( final long ts, final String type, final String subType, protected TimelineRecord createTimelineRecord( final long ts, final String type, final String subType,
final Map<String, String> data ) final Map<String, String> data )
{ {
return new TimelineRecord( ts, type, subType, data ); return new TimelineRecord( ts, type, subType, data );
} }


public static class AsList public static class AsList
implements TimelineCallback implements TimelineCallback
{ {

private final ArrayList<TimelineRecord> records = new ArrayList<TimelineRecord>(); private final ArrayList<TimelineRecord> records = new ArrayList<TimelineRecord>();


@Override @Override
Expand Down
Expand Up @@ -13,6 +13,7 @@
package org.sonatype.timeline; package org.sonatype.timeline;


import java.io.File; import java.io.File;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
Expand All @@ -26,6 +27,7 @@
public class TimelineTest public class TimelineTest
extends AbstractTimelineTestCase extends AbstractTimelineTestCase
{ {

protected File persistDirectory; protected File persistDirectory;


protected File indexDirectory; protected File indexDirectory;
Expand Down Expand Up @@ -109,21 +111,26 @@ public void testRepairIndexCouldNotRead()
FileUtils.copyDirectoryStructure( crashedPersistDir, persistDirectory ); FileUtils.copyDirectoryStructure( crashedPersistDir, persistDirectory );
FileUtils.copyDirectoryStructure( carshedIndexDir, indexDirectory ); FileUtils.copyDirectoryStructure( carshedIndexDir, indexDirectory );


timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory ) ); // as time passes, the timestamps in test persist files will fall out of default 30 day, so we say restore all

timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory,
Map<String, String> data = new HashMap<String, String>(); TimelineConfiguration.DEFAULT_ROLLING_INTERVAL_MILLIS,
data.put( "k1", "v1" ); Integer.MAX_VALUE ) );
data.put( "k2", "v2" );
data.put( "k3", "v3" );


Set<String> types = new HashSet<String>(); {
types.add( "typeA" ); // all records have this type, so we should have them as many as we asked for but max 100
AsList cb = new AsList(); // as many records are in test persist directory
timeline.retrieve( 0, 10, types, null, null, cb ); AsList cb = new AsList();
List<TimelineRecord> results = cb.getRecords(); timeline.retrieve( 0, 1000, Collections.singleton( "type" ), null, null, cb );

List<TimelineRecord> results = cb.getRecords();
assertEquals( 1, results.size() ); assertEquals( 100, results.size() );
assertEquals( data, results.get( 0 ).getData() ); }
{
// no records have this type, so we should have 0
AsList cb = new AsList();
timeline.retrieve( 0, 10, Collections.singleton( "badtype" ), null, null, cb );
List<TimelineRecord> results = cb.getRecords();
assertEquals( 0, results.size() );
}
} }


@Test @Test
Expand All @@ -135,11 +142,26 @@ public void testRepairIndexCouldNotRetrieve()
FileUtils.copyDirectoryStructure( crashedPersistDir, persistDirectory ); FileUtils.copyDirectoryStructure( crashedPersistDir, persistDirectory );
FileUtils.copyDirectoryStructure( carshedIndexDir, indexDirectory ); FileUtils.copyDirectoryStructure( carshedIndexDir, indexDirectory );


timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory ) ); // as time passes, the timestamps in test persist files will fall out of default 30 day, so we say restore all
timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory,
TimelineConfiguration.DEFAULT_ROLLING_INTERVAL_MILLIS,
Integer.MAX_VALUE ) );


AsList cb = new AsList(); {
timeline.retrieve( 0, 10, null, null, null, cb ); // all records have this type, so we should have them as many as we asked for but max 100
assertTrue( cb.getRecords().size() > 0 ); // as many records are in test persist directory
AsList cb = new AsList();
timeline.retrieve( 0, 1000, Collections.singleton( "type" ), null, null, cb );
List<TimelineRecord> results = cb.getRecords();
assertEquals( 100, results.size() );
}
{
// no records have this type, so we should have 0
AsList cb = new AsList();
timeline.retrieve( 0, 10, Collections.singleton( "badtype" ), null, null, cb );
List<TimelineRecord> results = cb.getRecords();
assertEquals( 0, results.size() );
}
} }


@Test @Test
Expand All @@ -152,7 +174,10 @@ public void testRepairIndexCouldNotAdd()
FileUtils.copyDirectoryStructure( persistDir, persistDirectory ); FileUtils.copyDirectoryStructure( persistDir, persistDirectory );
FileUtils.copyDirectoryStructure( goodIndexDir, indexDirectory ); FileUtils.copyDirectoryStructure( goodIndexDir, indexDirectory );


timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory ) ); // as time passes, the timestamps in test persist files will fall out of default 30 day, so we say restore all
timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory,
TimelineConfiguration.DEFAULT_ROLLING_INTERVAL_MILLIS,
Integer.MAX_VALUE ) );


{ {
// add, this should pass without any exception // add, this should pass without any exception
Expand All @@ -167,7 +192,10 @@ public void testRepairIndexCouldNotAdd()
timeline.stop(); timeline.stop();
cleanDirectory( indexDirectory ); cleanDirectory( indexDirectory );
FileUtils.copyDirectoryStructure( crashedIndexDir, indexDirectory ); FileUtils.copyDirectoryStructure( crashedIndexDir, indexDirectory );
timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory ) ); // as time passes, the timestamps in test persist files will fall out of default 30 day, so we say restore all
timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory,
TimelineConfiguration.DEFAULT_ROLLING_INTERVAL_MILLIS,
Integer.MAX_VALUE ) );


{ {
// add again, this should also pass without any exception // add again, this should also pass without any exception
Expand All @@ -189,16 +217,22 @@ public void testRepairIndexCouldNotPurge()
FileUtils.copyDirectoryStructure( persistDir, persistDirectory ); FileUtils.copyDirectoryStructure( persistDir, persistDirectory );
FileUtils.copyDirectoryStructure( goodIndexDir, indexDirectory ); FileUtils.copyDirectoryStructure( goodIndexDir, indexDirectory );


timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory ) ); // as time passes, the timestamps in test persist files will fall out of default 30 day, so we say restore all
timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory,
TimelineConfiguration.DEFAULT_ROLLING_INTERVAL_MILLIS,
Integer.MAX_VALUE ) );


assertTrue( timeline.purge( System.currentTimeMillis(), null, null, null ) > 0 ); // here, the "good" index really contains 6 records only
assertEquals( 6, timeline.purge( System.currentTimeMillis(), null, null, null ) );


// pretend that when timeline is running, the index is manually changed // pretend that when timeline is running, the index is manually changed
timeline.stop(); timeline.stop();
cleanDirectory( indexDirectory ); cleanDirectory( indexDirectory );
FileUtils.copyDirectoryStructure( crashedIndexDir, indexDirectory ); FileUtils.copyDirectoryStructure( crashedIndexDir, indexDirectory );
timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory ) ); // as time passes, the timestamps in test persist files will fall out of default 30 day, so we say restore all

timeline.start( new TimelineConfiguration( persistDirectory, indexDirectory,
assertTrue( timeline.purge( System.currentTimeMillis(), null, null, null ) > 0 ); TimelineConfiguration.DEFAULT_ROLLING_INTERVAL_MILLIS,
Integer.MAX_VALUE ) );
assertEquals( 100, timeline.purge( System.currentTimeMillis(), null, null, null ) );
} }
} }

This file was deleted.

This file was deleted.

0 comments on commit 4879784

Please sign in to comment.