Runs the harvests of local candidates performed by multiple CandidateHarvesters for a given Component in parallel in order to achieve faster overall harvesting. Additionally, disables a CandidateHarvester for an Agent when it fails so that it does not slow down the overall harvesting.

This commit is contained in:
lubomir.marinov 2010-10-14 10:41:55 +00:00
parent 3e28058a07
commit 7ff7ed83e9
7 changed files with 522 additions and 52 deletions

View File

@ -82,11 +82,11 @@ public class Agent
= new HostCandidateHarvester();
/**
* The list of harvesters (i.e. STUN, TURN, and others) that the agent
* should use when gathering candidates for components.
* The set of harvesters (i.e. STUN, TURN, and others) that the agent should
* use when gathering candidates for components.
*/
private final List<CandidateHarvester> harvesters
= new ArrayList<CandidateHarvester>();
private final CandidateHarvesterSet harvesters
= new CandidateHarvesterSet();
/**
* We use the <tt>FoundationsRegistry</tt> to keep track of the foundations
@ -288,8 +288,12 @@ public class Agent
BindException
{
if(transport != Transport.UDP)
throw new IllegalArgumentException("This implementation does not "
+" currently support transport: " + transport);
{
throw new IllegalArgumentException(
"This implementation does not currently support transport: "
+ transport);
}
Component component = stream.createComponent(transport);
gatherCandidates(component, preferredPort, minPort, maxPort );
@ -314,7 +318,7 @@ public class Agent
* before giving up and throwinG an exception.
*
* @throws IllegalArgumentException if either <tt>minPort</tt> or
* <tt>maxPort</tt> is not a valid port number or if <tt>minPort >
* <tt>maxPort</tt> is not a valid port number or if <tt>minPort &gt;
* maxPort</tt>.
* @throws IOException if an error occurs while the underlying resolver lib
* is gathering candidates and we end up without even a single one.
@ -327,15 +331,11 @@ public class Agent
IOException
{
hostCandidateHarvester.harvest(
component, preferredPort, minPort, maxPort);
component,
preferredPort, minPort, maxPort);
//apply other harvesters here:
//todo: run harvesters in a parallel manner
synchronized(harvesters)
{
for (CandidateHarvester h : harvesters)
h.harvest(component);
}
//apply other harvesters here
harvesters.harvest(component);
computeFoundations(component);
@ -544,10 +544,7 @@ public class Agent
*/
public void addCandidateHarvester(CandidateHarvester harvester)
{
synchronized(harvesters)
{
harvesters.add(harvester);
}
harvesters.add(harvester);
}
/**

View File

@ -50,8 +50,8 @@ public class CheckList
* Contains {@link PropertyChangeListener}s registered with this {@link
* Agent} and following its changes of state.
*/
private List<PropertyChangeListener> stateListeners
= new LinkedList<PropertyChangeListener>();
private final List<PropertyChangeListener> stateListeners
= new LinkedList<PropertyChangeListener>();
/**
* The name of the {@link PropertyChangeEvent} that we use to deliver
@ -242,17 +242,14 @@ public class CheckList
*
* @return A <tt>String</tt> representation of this collection.
*/
@Override
public String toString()
{
StringBuffer buff = new StringBuffer("CheckList. (num pairs=");
buff.append(size() + ")\n");
StringBuilder buff = new StringBuilder("CheckList. (num pairs=");
buff.append(size()).append(")\n");
Iterator<CandidatePair> pairs = iterator();
while(pairs.hasNext())
{
buff.append(pairs.next().toString()).append("\n");
}
for (CandidatePair pair : this)
buff.append(pair).append("\n");
return buff.toString();
}
@ -306,12 +303,8 @@ public class CheckList
}
//now put the pairs we've selected in the Waiting state.
Iterator<CandidatePair> pairsIter = pairsToWait.values().iterator();
while(pairsIter.hasNext())
{
pairsIter.next().setStateWaiting();
}
for (CandidatePair pairToWait : pairsToWait.values())
pairToWait.setStateWaiting();
}
/**
@ -323,9 +316,7 @@ public class CheckList
{
//first, determine the pairs that we'd need to put in the waiting state.
for(CandidatePair pair : this)
{
pair.computePriority();
}
}
/**

View File

@ -92,8 +92,8 @@ public class IceMediaStream
* Contains {@link PropertyChangeListener}s registered with this {@link
* Agent} and following the various events it may be generating.
*/
private List<PropertyChangeListener> streamListeners
= new LinkedList<PropertyChangeListener>();
private final List<PropertyChangeListener> streamListeners
= new LinkedList<PropertyChangeListener>();
/**
* The maximum number of candidate pairs that we should have in our check
@ -160,6 +160,7 @@ public class IceMediaStream
*
* @return a <tt>String</tt> representation of this media stream.
*/
@Override
public String toString()
{
StringBuffer buff = new StringBuffer( "media stream:")
@ -167,11 +168,8 @@ public class IceMediaStream
buff.append(" (component count=").append(getComponentCount())
.append(")");
List<Component> components = getComponents();
for (Component cmp : components)
{
for (Component cmp : getComponents())
buff.append("\n").append(cmp);
}
return buff.toString();
}
@ -654,7 +652,7 @@ public class IceMediaStream
components.remove(pair.getParentComponent());
}
return components.size() == 0;
return components.isEmpty();
}
/**
@ -668,9 +666,7 @@ public class IceMediaStream
*/
protected boolean allComponentsHaveSelected()
{
List<Component> components = getComponents();
for(Component component : components)
for(Component component : getComponents())
if(component.getSelectedPair() == null)
return false;

View File

@ -7,6 +7,8 @@
*/
package org.ice4j.ice.harvest;
import java.util.*;
import org.ice4j.ice.*;
/**
@ -15,16 +17,26 @@ import org.ice4j.ice.*;
* org.ice4j.ice.Component}.
*
* @author Emil Ivov
* @author Lyubomir Marinov
*/
public interface CandidateHarvester
{
/**
* Gathers all candidate addresses of the type that this
* <tt>CandidateHarvester</tt>
* <tt>CandidateHarvester</tt> supports. The gathered candidate addresses
* are to be added by this <tt>CandidateHarvester</tt> to the specified
* <tt>Component</tt> using
* {@link Component#addLocalCandidate(LocalCandidate)} as soon as they are
* discovered.
*
* @param component the {@link Component} that we'd like to gather candidate
* addresses for.
* @return the <tt>LocalCandidate</tt>s gathered by this
* <tt>CandidateHarvester</tt>. Though they are to be added by this
* <tt>CandidateHarvester</tt> to the specified <tt>component</tt> as soon
* as they are discovered, they should also be returned in order to make
* sure that the gathering will be considered successful.
*/
public void harvest(Component component);
public Collection<LocalCandidate> harvest(Component component);
}

View File

@ -0,0 +1,448 @@
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.ice4j.ice.harvest;
import java.util.*;
import java.util.concurrent.*;
import org.ice4j.ice.*;
/**
* Implements {@link Set} of <tt>CandidateHarvester</tt>s which runs the
* gathering of candidate addresses performed by its elements in parallel.
*
* @author Lyubomir Marinov
*/
public class CandidateHarvesterSet
extends AbstractSet<CandidateHarvester>
{
/**
* The <tt>CandidateHarvester</tt>s which are the elements of this
* <tt>Set</tt>.
*/
private final Collection<CandidateHarvesterSetElement> elements
= new LinkedList<CandidateHarvesterSetElement>();
/**
* Initializes a new <tt>CandidateHarvesterSet</tt> instance.
*/
public CandidateHarvesterSet()
{
}
/**
* Adds a specific <tt>CandidateHarvester</tt> to this
* <tt>CandidateHarvesterSet</tt> and returns <tt>true</tt> if it is not
* already present. Otherwise, leaves this set unchanged and returns
* <tt>false</tt>.
*
* @param harvester the <tt>CandidateHarvester</tt> to be added to this
* <tt>CandidateHarvesterSet</tt>
* @return <tt>true</tt> if this <tt>CandidateHarvesterSet</tt> did not
* already contain the specified <tt>harvester</tt>; otherwise,
* <tt>false</tt>
* @see Set#add(Object)
*/
@Override
public boolean add(CandidateHarvester harvester)
{
synchronized (elements)
{
for (CandidateHarvesterSetElement element : elements)
if (element.harvesterEquals(harvester))
return false;
elements.add(new CandidateHarvesterSetElement(harvester));
return true;
}
}
/**
* Gathers candidate addresses for a specific <tt>Component</tt>.
* <tt>CandidateHarvesterSet</tt> delegates to the
* <tt>CandidateHarvester</tt>s which are its <tt>Set</tt> elements.
*
* @param component the <tt>Component</tt> to gather candidate addresses for
* @see CandidateHarvester#harvest(Component)
*/
public void harvest(Component component)
{
synchronized (elements)
{
harvest(
elements.iterator(),
component,
Executors.newCachedThreadPool());
}
}
/**
* Gathers candidate addresses for a specific <tt>Component</tt> using
* specific <tt>CandidateHarvester</tt>s.
*
* @param harvesters the <tt>CandidateHarvester</tt>s to gather candidate
* addresses for the specified <tt>Component</tt>
* @param component the <tt>Component</tt> to gather candidate addresses for
* @param executorService the <tt>ExecutorService</tt> to schedule the
* execution of the gathering of candidate addresses performed by the
* specified <tt>harvesters</tt>
*/
private void harvest(
final Iterator<CandidateHarvesterSetElement> harvesters,
final Component component,
ExecutorService executorService)
{
/**
* Represents a task to be executed by the specified executorService and
* to call {@link CandidateHarvester#harvestComponent)} on the specified
* harvesters.
*/
class CandidateHarvesterSetTask
implements Runnable
{
/**
* The <tt>CandidateHarvester</tt> on which
* {@link CandidateHarvester#harvest(Component)} is to be or is
* being called.
*/
private CandidateHarvesterSetElement harvester;
/**
* Initializes a new <tt>CandidateHarvesterSetTask</tt> which is to
* call {@link CandidateHarvester#harvest(Component)} on a specific
* harvester and then as many harvesters as possible.
*
* @param harvester the <tt>CandidateHarvester</tt> on which the
* new instance is to call
* <tt>CandidateHarvester#harvest(Component)</tt> first
*/
public CandidateHarvesterSetTask(
CandidateHarvesterSetElement harvester)
{
this.harvester = harvester;
}
/**
* Gets the <tt>CandidateHarvester</tt> on which
* {@link CandidateHarvester#harvest(Component)} is being called.
*
* @return the <tt>CandidateHarvester</tt> on which
* <tt>CandidateHarvester#harvest(Component)</tt> is being called
*/
public CandidateHarvesterSetElement getHarvester()
{
return harvester;
}
public void run()
{
while (true)
{
if (harvester.isEnabled())
{
try
{
harvester.harvest(component);
}
catch (Throwable t)
{
harvester.setEnabled(false);
if (t instanceof ThreadDeath)
throw (ThreadDeath) t;
}
}
/*
* CandidateHarvester#harvest(Component) has been called on
* the harvester and its success or failure has been noted.
* Now forget the harvester because any failure to continue
* execution is surely not its fault.
*/
harvester = null;
synchronized (harvesters)
{
if (harvesters.hasNext())
harvester = harvesters.next();
else
break;
}
}
}
}
/*
* Start asynchronously executing the
* CandidateHarvester#harvest(Component) method of the harvesters.
*/
Map<CandidateHarvesterSetTask, Future<?>> tasks
= new HashMap<CandidateHarvesterSetTask, Future<?>>();
while (true)
{
/*
* Find the next CandidateHarvester which is to start gathering
* candidates.
*/
CandidateHarvesterSetElement harvester;
synchronized (harvesters)
{
if (harvesters.hasNext())
harvester = harvesters.next();
else
break;
}
if (!harvester.isEnabled())
continue;
// Asynchronously start gathering candidates using the harvester.
CandidateHarvesterSetTask task
= new CandidateHarvesterSetTask(harvester);
tasks.put(task, executorService.submit(task));
}
/*
* Wait for all harvesters to be given a chance to execute their
* CandidateHarvester#harvest(Component) method.
*/
Iterator<Map.Entry<CandidateHarvesterSetTask, Future<?>>> taskIter
= tasks.entrySet().iterator();
while (taskIter.hasNext())
{
Map.Entry<CandidateHarvesterSetTask, Future<?>> task
= taskIter.next();
Future<?> future = task.getValue();
while (true)
{
try
{
future.get();
break;
}
catch (CancellationException ce)
{
/*
* It got cancelled so we cannot say that the fault is with
* its current harvester.
*/
break;
}
catch (ExecutionException ee)
{
/*
* A problem appeared during the execution of the task.
* CandidateHarvesterSetTask clears its harvester property
* for the purpose of determining whether the problem has
* appeared while working with a harvester.
*/
CandidateHarvesterSetElement harvester
= task.getKey().getHarvester();
if (harvester != null)
harvester.setEnabled(false);
break;
}
catch (InterruptedException ie)
{
continue;
}
}
taskIter.remove();
}
}
/**
* Returns an <tt>Iterator</tt> over the <tt>CandidateHarvester</tt>s which
* are elements in this <tt>CandidateHarvesterSet</tt>. The elements are
* returned in no particular order.
*
* @return an <tt>Iterator</tt> over the <tt>CandidateHarvester</tt>s which
* are elements in this <tt>CandidateHarvesterSet</tt>
* @see Set#iterator()
*/
public Iterator<CandidateHarvester> iterator()
{
final Iterator<CandidateHarvesterSetElement> elementIter
= elements.iterator();
return
new Iterator<CandidateHarvester>()
{
/**
* Determines whether this iteration has more elements.
*
* @return <tt>true</tt> if this iteration has more elements;
* otherwise, <tt>false</tt>
* @see Iterator#hasNext()
*/
public boolean hasNext()
{
return elementIter.hasNext();
}
/**
* Returns the next element in this iteration.
*
* @return the next element in this iteration
* @throws NoSuchElementException if this iteration has no more
* elements
* @see Iterator#next()
*/
public CandidateHarvester next()
throws NoSuchElementException
{
return elementIter.next().harvester;
}
/**
* Removes from the underlying <tt>CandidateHarvesterSet</tt>
* the last <tt>CandidateHarvester</tt> (element) returned by
* this <tt>Iterator</tt>. <tt>CandidateHarvestSet</tt> does not
* implement the <tt>remove</tt> operation at the time of this
* writing i.e. it always throws
* <tt>UnsupportedOperationException</tt>.
*
* @throws IllegalStateException if the <tt>next</tt> method has
* not yet been called, or the <tt>remove</tt> method has
* already been called after the last call to the <tt>next</tt>
* method
* @throws UnsupportedOperationException if the <tt>remove</tt>
* operation is not supported by this <tt>Iterator</tt>
* @see Iterator#remove()
*/
public void remove()
throws IllegalStateException,
UnsupportedOperationException
{
throw new UnsupportedOperationException("remove");
}
};
}
/**
* Returns the number of <tt>CandidateHarvester</tt>s which are elements in
* this <tt>CandidateHarvesterSet</tt>.
*
* @return the number of <tt>CandidateHarvester</tt>s which are elements in
* this <tt>CandidateHarvesterSet</tt>
* @see Set#size()
*/
public int size()
{
synchronized (elements)
{
return elements.size();
}
}
/**
* Represents a <tt>CandidateHarvester</tt> as an element in a
* <tt>CandidateHarvesterSet</tt>.
*/
private static class CandidateHarvesterSetElement
{
/**
* The indicator which determines whether
* {@link CandidateHarvester#harvest(Component)} is to be called on
* {@link #harvester}.
*/
private boolean enabled = true;
/**
* The <tt>CandidateHarvester</tt> which is an element in a
* <tt>CandidateHarvesterSet</tt>.
*/
private final CandidateHarvester harvester;
/**
* Initializes a new <tt>CandidateHarvesterSetElement</tt> instance
* which is to represent a specific <tt>CandidateHarvester</tt> as an
* element in a <tt>CandidateHarvesterSet</tt>.
*
* @param harvester the <tt>CandidateHarvester</tt> which is to be
* represented as an element in a <tt>CandidateHarvesterSet</tt> by the
* new instance
*/
public CandidateHarvesterSetElement(CandidateHarvester harvester)
{
this.harvester = harvester;
}
/**
* Calls {@link CandidateHarvester#harvest(Component)} on the associated
* <tt>CandidateHarvester</tt> if <tt>enabled</tt>.
*
* @param component the <tt>Component</tt> to gather candidates for
*/
public void harvest(Component component)
{
if (isEnabled())
{
Collection<LocalCandidate> candidates
= harvester.harvest(component);
/*
* If the CandidateHarvester has not gathered any candidates, it
* is considered failed and will not be used again in order to
* not risk it slowing down the overall harvesting.
*/
if ((candidates == null) || candidates.isEmpty())
setEnabled(false);
}
}
/**
* Determines whether the associated <tt>CandidateHarvester</tt> is
* considered to be the same as a specific <tt>CandidateHarvester</tt>.
*
* @param harvester the <tt>CandidateHarvester</tt> to be compared to
* the associated <tt>CandidateHarvester</tt>
* @return <tt>true</tt> if the associated <tt>CandidateHarvester</tt>
* is considered to be the same as the specified <tt>harvester</tt>;
* otherwise, <tt>false</tt>
*/
public boolean harvesterEquals(CandidateHarvester harvester)
{
return this.harvester.equals(harvester);
}
/**
* Gets the indicator which determines whether
* {@link CandidateHarvester#harvest(Component)} is to be called on the
* associated <tt>CandidateHarvester</tt>.
*
* @return <tt>true</tt> if
* <tt>CandidateHarvester#harvest(Component)</tt> is to be called on the
* associated <tt>CandidateHarvester</tt>; otherwise, <tt>false</tt>
*/
public boolean isEnabled()
{
return enabled;
}
/**
* Sets the indicator which determines whether
* {@link CandidateHarvester#harvest(Component)} is to be called on the
* associated <tt>CandidateHarvester</tt>.
*
* @param enabled <tt>true</tt> if
* <tt>CandidateHarvester#harvest(Component)</tt> is to be called on the
* associated <tt>CandidateHarvester</tt>; otherwise, <tt>false</tt>
*/
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
}
}

View File

@ -208,9 +208,11 @@ public class StunCandidateHarvester
* so that it would resolve them.
*
* @param component the {@link Component} that we'd like to gather candidate
* STUN <tt>Candidate</tt>s for.
* STUN <tt>Candidate</tt>s for
* @return the <tt>LocalCandidate</tt>s gathered by this
* <tt>CandidateHarvester</tt>
*/
public void harvest(Component component)
public Collection<LocalCandidate> harvest(Component component)
{
stunStack = component.getParentStream().getParentAgent().getStunStack();
@ -219,6 +221,29 @@ public class StunCandidateHarvester
startResolvingCandidate((HostCandidate) cand);
waitForResolutionEnd();
/*
* Report the LocalCandidates gathered by this CandidateHarvester so
* that the harvest is sure to be considered successful.
*/
Collection<LocalCandidate> candidates = new HashSet<LocalCandidate>();
synchronized (completedHarvests)
{
for (StunCandidateHarvest completedHarvest : completedHarvests)
{
LocalCandidate[] completedHarvestCandidates
= completedHarvest.getCandidates();
if ((completedHarvestCandidates != null)
&& (completedHarvestCandidates.length != 0))
{
candidates.addAll(
Arrays.asList(completedHarvestCandidates));
}
}
}
return candidates;
}
/**

View File

@ -367,7 +367,8 @@ class StunClientTransaction
stackCallback,
evt.getRawMessage(),
(Response) evt.getMessage(),
this.request, getTransactionID()));
this.request,
getTransactionID()));
}
/**