added assume from running directly

This commit is contained in:
Greg Wilkins 2017-08-10 11:34:13 +10:00
parent 7b5d3b2b5a
commit 39ea66368c
4 changed files with 78 additions and 42 deletions

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.server;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedByInterruptException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -34,6 +35,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.ByteBufferPool;
@ -48,6 +50,7 @@ import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.Locker;
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
import org.eclipse.jetty.util.thread.Scheduler;
@ -137,8 +140,10 @@ import org.eclipse.jetty.util.thread.Scheduler;
public abstract class AbstractConnector extends ContainerLifeCycle implements Connector, Dumpable
{
protected final Logger LOG = Log.getLogger(AbstractConnector.class);
// Order is important on server side, so we use a LinkedHashMap
private final Map<String, ConnectionFactory> _factories = new LinkedHashMap<>();
private final Locker _locker = new Locker();
private final Condition _setAccepting = _locker.newCondition();
private final Map<String, ConnectionFactory> _factories = new LinkedHashMap<>(); // Order is important on server side, so we use a LinkedHashMap
private final Server _server;
private final Executor _executor;
private final Scheduler _scheduler;
@ -146,12 +151,13 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
private final Thread[] _acceptors;
private final Set<EndPoint> _endpoints = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final Set<EndPoint> _immutableEndPoints = Collections.unmodifiableSet(_endpoints);
private volatile CountDownLatch _stopping;
private CountDownLatch _stopping;
private long _idleTimeout = 30000;
private String _defaultProtocol;
private ConnectionFactory _defaultConnectionFactory;
private String _name;
private int _acceptorPriorityDelta=-2;
private boolean _accepting = true;
/**
@ -283,7 +289,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
protected void interruptAcceptors()
{
synchronized (this)
try (Locker.Lock lock = _locker.lockIfNotHeld())
{
for (Thread thread : _acceptors)
{
@ -327,7 +333,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
public void join(long timeout) throws InterruptedException
{
synchronized (this)
try (Locker.Lock lock = _locker.lock())
{
for (Thread thread : _acceptors)
if (thread != null)
@ -342,15 +348,31 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
/**
* @return Is the connector accepting new connections
*/
protected boolean isAccepting()
public boolean isAccepting()
{
return isRunning();
try (Locker.Lock lock = _locker.lock())
{
return _accepting;
}
}
public void setAccepting(boolean accepting)
{
try (Locker.Lock lock = _locker.lock())
{
_accepting=accepting;
if (accepting)
_setAccepting.signalAll();
else
interruptAcceptors();
}
}
@Override
public ConnectionFactory getConnectionFactory(String protocol)
{
synchronized (_factories)
try (Locker.Lock lock = _locker.lock())
{
return _factories.get(StringUtil.asciiToLowerCase(protocol));
}
@ -359,7 +381,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
@Override
public <T> T getConnectionFactory(Class<T> factoryType)
{
synchronized (_factories)
try (Locker.Lock lock = _locker.lock())
{
for (ConnectionFactory f : _factories.values())
if (factoryType.isAssignableFrom(f.getClass()))
@ -370,7 +392,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
public void addConnectionFactory(ConnectionFactory factory)
{
synchronized (_factories)
try (Locker.Lock lock = _locker.lock())
{
Set<ConnectionFactory> to_remove = new HashSet<>();
for (String key:factory.getProtocols())
@ -409,7 +431,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
public void addFirstConnectionFactory(ConnectionFactory factory)
{
synchronized (_factories)
try (Locker.Lock lock = _locker.lock())
{
List<ConnectionFactory> existings = new ArrayList<>(_factories.values());
_factories.clear();
@ -422,7 +444,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
public void addIfAbsentConnectionFactory(ConnectionFactory factory)
{
synchronized (_factories)
try (Locker.Lock lock = _locker.lock())
{
String key=StringUtil.asciiToLowerCase(factory.getProtocol());
if (_factories.containsKey(key))
@ -444,7 +466,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
public ConnectionFactory removeConnectionFactory(String protocol)
{
synchronized (_factories)
try (Locker.Lock lock = _locker.lock())
{
ConnectionFactory factory= _factories.remove(StringUtil.asciiToLowerCase(protocol));
removeBean(factory);
@ -455,7 +477,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
@Override
public Collection<ConnectionFactory> getConnectionFactories()
{
synchronized (_factories)
try (Locker.Lock lock = _locker.lock())
{
return _factories.values();
}
@ -463,7 +485,7 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
public void setConnectionFactories(Collection<ConnectionFactory> factories)
{
synchronized (_factories)
try (Locker.Lock lock = _locker.lock())
{
List<ConnectionFactory> existing = new ArrayList<>(_factories.values());
for (ConnectionFactory factory: existing)
@ -538,14 +560,23 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
return getConnectionFactory(_defaultProtocol);
}
protected boolean handleAcceptFailure(Throwable previous, Throwable current)
protected boolean handleAcceptFailure(Throwable ex)
{
if (isAccepting())
if (isRunning())
{
if (previous == null)
LOG.warn(current);
else
LOG.debug(current);
if (ex instanceof InterruptedException)
{
LOG.debug(ex);
return true;
}
if (ex instanceof ClosedByInterruptException)
{
LOG.debug(ex);
return false;
}
LOG.warn(ex);
try
{
// Arbitrary sleep to avoid spin looping.
@ -556,12 +587,13 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
}
catch (Throwable x)
{
return false;
LOG.ignore(x);
}
return false;
}
else
{
LOG.ignore(current);
LOG.ignore(ex);
return false;
}
}
@ -595,19 +627,28 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
try
{
Throwable exception = null;
while (isAccepting())
while (isRunning())
{
try (Locker.Lock lock = _locker.lock())
{
if (!_accepting)
{
_setAccepting.await();
continue;
}
}
catch (InterruptedException e)
{
continue;
}
try
{
accept(_id);
exception = null;
}
catch (Throwable x)
{
if (handleAcceptFailure(exception, x))
exception = x;
else
if (!handleAcceptFailure(x))
break;
}
}

View File

@ -96,8 +96,7 @@ public abstract class AbstractNetworkConnector extends AbstractConnector impleme
@Override
public void close()
{
// Interrupting is often sufficient to close the channel
interruptAcceptors();
setAccepting(false);
}
@ -107,13 +106,7 @@ public abstract class AbstractNetworkConnector extends AbstractConnector impleme
close();
return super.shutdown();
}
@Override
protected boolean isAccepting()
{
return super.isAccepting() && isOpen();
}
@Override
public String toString()
{

View File

@ -344,14 +344,14 @@ public class ServerConnector extends AbstractNetworkConnector
@Override
public void close()
{
super.close();
ServerSocketChannel serverChannel = _acceptChannel;
_acceptChannel = null;
if (serverChannel != null)
{
removeBean(serverChannel);
// If the interrupt did not close it, we should close it
if (serverChannel.isOpen())
{
try
@ -364,7 +364,6 @@ public class ServerConnector extends AbstractNetworkConnector
}
}
}
// super.close();
_localPort = -2;
}

View File

@ -27,6 +27,7 @@ import java.nio.file.Paths;
import org.eclipse.jetty.toolchain.test.JDK;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
public class TypeUtilTest
@ -130,7 +131,9 @@ public class TypeUtilTest
@Test
public void testGetLocationOfClass() throws Exception
{
Path mavenRepoPath = Paths.get( System.getProperty( "mavenRepoPath" ) );
String mavenRepoPathProperty = System.getProperty( "mavenRepoPath");
Assume.assumeNotNull(mavenRepoPathProperty);
Path mavenRepoPath = Paths.get( mavenRepoPathProperty );
String mavenRepo = mavenRepoPath.toFile().getPath().replaceAll("\\\\", "/");
// Classes from maven dependencies