You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@onami.apache.org by ra...@apache.org on 2013/03/14 19:21:50 UTC
svn commit: r1456593 - in
/incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup:
WarmUpModule.java WarmUpTask.java WarmUper.java
Author: randgalt
Date: Thu Mar 14 18:21:50 2013
New Revision: 1456593
URL: http://svn.apache.org/r1456593
Log:
Some more doc and some last minute refactoring
Modified:
incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpModule.java
incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpTask.java
incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUper.java
Modified: incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpModule.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpModule.java?rev=1456593&r1=1456592&r2=1456593&view=diff
==============================================================================
--- incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpModule.java (original)
+++ incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpModule.java Thu Mar 14 18:21:50 2013
@@ -30,6 +30,9 @@ import java.util.concurrent.TimeUnit;
import static com.google.inject.matcher.Matchers.any;
+/**
+ * The module for preparing for warm ups.
+ */
public class WarmUpModule<A extends Annotation>
extends AbstractModule
{
@@ -37,6 +40,12 @@ public class WarmUpModule<A extends Anno
private static final long DEFAULT_WAIT_MS = TimeUnit.DAYS.toMillis( Integer.MAX_VALUE ); // essentially forever
+ /**
+ * Creates a new module which register methods annotated with input annotation on methods
+ * in types filtered by the input matcher.
+ *
+ * @param stage the annotation to be searched.
+ */
public WarmUpModule( Class<A> stage )
{
this( stage, any() );
@@ -46,7 +55,7 @@ public class WarmUpModule<A extends Anno
* Creates a new module which register methods annotated with input annotation on methods
* in types filtered by the input matcher.
*
- * @param stage the <i>Dispose</i> annotation to be searched.
+ * @param stage the annotation to be searched.
* @param typeMatcher the filter for injectee types.
*/
public WarmUpModule( Class<A> stage, Matcher<? super TypeLiteral<?>> typeMatcher )
@@ -57,22 +66,44 @@ public class WarmUpModule<A extends Anno
stager ).build();
}
+ /**
+ * Return a new standard warm up module
+ *
+ * @return warm up module
+ */
public static WarmUpModule<WarmUp> newWarmUpModule()
{
return new WarmUpModule<WarmUp>( WarmUp.class );
}
+ /**
+ * Return a new standard warm up module
+ *
+ * @param typeMatcher the filter for injectee types.
+ * @return warm up module
+ */
public static WarmUpModule<WarmUp> newWarmUpModule( Matcher<? super TypeLiteral<?>> typeMatcher )
{
return new WarmUpModule<WarmUp>( WarmUp.class, typeMatcher );
}
+ /**
+ * Allows one to create WarmUpModule with builder pattern.
+ *
+ * @return builder for WarmUpModule.
+ */
public static Builder<WarmUp> builder()
{
WarmUper<WarmUp> stager = new WarmUper<WarmUp>( WarmUp.class, DEFAULT_WAIT_MS );
return new Builder<WarmUp>( WarmUp.class, stager ).withTypeMapper( stager );
}
+ /**
+ * Allows one to create WarmUpModule with builder pattern.
+ *
+ * @param stage the annotation to be searched.
+ * @return builder for WarmUpModule.
+ */
public static <A extends Annotation> Builder<A> builder( Class<A> stage )
{
WarmUper<A> stager = new WarmUper<A>( stage, DEFAULT_WAIT_MS );
Modified: incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpTask.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpTask.java?rev=1456593&r1=1456592&r2=1456593&view=diff
==============================================================================
--- incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpTask.java (original)
+++ incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUpTask.java Thu Mar 14 18:21:50 2013
@@ -34,6 +34,9 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
+/**
+ * Fork-join task that performs the warm ups
+ */
class WarmUpTask
extends RecursiveAction
{
@@ -45,6 +48,14 @@ class WarmUpTask
private final ConcurrentMap<TypeLiteral<?>, WarmUpTask> inProgress;
+ static final TypeLiteral<?> ROOT = new TypeLiteral<Object>(){};
+
+ /**
+ * @param stageHandler the stage handler passed to {@link org.apache.onami.lifecycle.core.Stager#stage(org.apache.onami.lifecycle.core.StageHandler)}
+ * @param typeLiteral the type associated with the object being warmed up
+ * @param reverseLookup the full list of types-to-stagers that were registered
+ * @param inProgress which tasks are already warming up (to avoid duplicates)
+ */
WarmUpTask( StageHandler stageHandler, TypeLiteral<?> typeLiteral,
Map<TypeLiteral<?>, Set<Stageable>> reverseLookup, ConcurrentMap<TypeLiteral<?>, WarmUpTask> inProgress )
{
@@ -58,8 +69,9 @@ class WarmUpTask
protected void compute()
{
List<WarmUpTask> tasksToJoin = new ArrayList<WarmUpTask>();
- if ( typeLiteral == null )
+ if ( typeLiteral == ROOT )
{
+ // this is the root task - just start all the known tasks
computeRoot( tasksToJoin );
}
else
@@ -67,11 +79,14 @@ class WarmUpTask
internalCompute( tasksToJoin );
}
+ // wait for dependent tasks to finish
for ( WarmUpTask task : tasksToJoin )
{
task.join();
}
+ // finally do the execution
+
Set<Stageable> stageables = reverseLookup.get( typeLiteral );
if ( stageables != null )
{
@@ -89,11 +104,6 @@ class WarmUpTask
WarmUpTask warmUpTask = new WarmUpTask( stageHandler, typeLiteral, reverseLookup, inProgress );
startTask( tasksToJoin, warmUpTask );
}
-
- for ( WarmUpTask task : tasksToJoin )
- {
- task.join();
- }
}
private void internalCompute( List<WarmUpTask> tasksToJoin )
@@ -132,6 +142,9 @@ class WarmUpTask
List<Dependency<?>> dependencies = injectionPoint.getDependencies();
for ( Dependency<?> dependency : dependencies )
{
+ // create a task for any dependencies. Note: even if the dependency isn't
+ // a registered stager it must be created as a task as its dependencies
+ // may be stagers
TypeLiteral<?> dependencyTypeLiteral = dependency.getKey().getTypeLiteral();
childTasks.add(
new WarmUpTask( stageHandler, dependencyTypeLiteral, reverseLookup, inProgress ) );
Modified: incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUper.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUper.java?rev=1456593&r1=1456592&r2=1456593&view=diff
==============================================================================
--- incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUper.java (original)
+++ incubator/onami/trunk/lifecycle/warmup/src/main/java/org/apache/onami/lifecycle/warmup/WarmUper.java Thu Mar 14 18:21:50 2013
@@ -37,6 +37,10 @@ import java.util.concurrent.ConcurrentMa
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
+/**
+ * A {@link Stager} that handles the warm up process. For Warm Up, you
+ * <strong>must</strong> use WarmUper.
+ */
public class WarmUper<A extends Annotation>
implements Stager<A>, StageableTypeMapper<A>
{
@@ -47,6 +51,13 @@ public class WarmUper<A extends Annotati
private volatile long maxMs;
+ /**
+ * @param stage the annotation to mark this stage
+ * @param maxMs when {@link #stage()} is called, this is the maximum time
+ * to wait for warmups to complete. If time expires, a
+ * {@link TimeoutException} (wrapped in a {@link RuntimeException}
+ * is thrown.
+ */
public WarmUper( Class<A> stage, long maxMs )
{
this.stage = stage;
@@ -55,7 +66,9 @@ public class WarmUper<A extends Annotati
/**
* When the warm up is staged, it will wait until this maximum time for warm ups to finish.
- * The default is to wait forever
+ * The default is to wait forever. If time expires, a
+ * {@link TimeoutException} (wrapped in a {@link RuntimeException}
+ * is thrown.
*
* @param maxWait max time to wait
* @param unit time unit
@@ -65,6 +78,9 @@ public class WarmUper<A extends Annotati
this.maxMs = unit.toMillis( maxWait );
}
+ /**
+ * {@inheritDoc}
+ */
public <I> void registerType( Stageable stageable, TypeLiteral<I> parentType )
{
Set<Stageable> newList = Collections.newSetFromMap( new ConcurrentHashMap<Stageable, Boolean>() );
@@ -74,16 +90,25 @@ public class WarmUper<A extends Annotati
}
+ /**
+ * {@inheritDoc}
+ */
public void register( Stageable stageable )
{
// this is a NOP for warm up. Use registerType instead
}
+ /**
+ * {@inheritDoc}
+ */
public void stage()
{
stage( new NoOpStageHandler() );
}
+ /**
+ * {@inheritDoc}
+ */
public void stage( StageHandler stageHandler )
{
Map<TypeLiteral<?>, Set<Stageable>> localCopy = new HashMap<TypeLiteral<?>, Set<Stageable>>();
@@ -92,7 +117,7 @@ public class WarmUper<A extends Annotati
ForkJoinPool forkJoinPool = new ForkJoinPool();
ConcurrentMap<TypeLiteral<?>, WarmUpTask> inProgress = new ConcurrentHashMap<TypeLiteral<?>, WarmUpTask>();
- forkJoinPool.submit( new WarmUpTask( stageHandler, null, localCopy, inProgress ) );
+ forkJoinPool.submit( new WarmUpTask( stageHandler, WarmUpTask.ROOT, localCopy, inProgress ) );
forkJoinPool.shutdown();
try
@@ -111,6 +136,9 @@ public class WarmUper<A extends Annotati
}
+ /**
+ * {@inheritDoc}
+ */
public Class<A> getStage()
{
return stage;