forked from I2P_Developers/i2p.i2p
Findbugs:
- Fix several 'increment of volatile is not atomic' all over Remaining: UDP PeerState.java, to be checked in separately - Comment out all of unused MessageStateMonitor
This commit is contained in:
@ -6,6 +6,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.I2PException;
|
||||
@ -40,7 +41,7 @@ class ConnectionManager {
|
||||
private final Map<Long, PingRequest> _pendingPings;
|
||||
private volatile boolean _throttlersInitialized;
|
||||
private final ConnectionOptions _defaultOptions;
|
||||
private volatile int _numWaiting;
|
||||
private final AtomicInteger _numWaiting = new AtomicInteger();
|
||||
private long _soTimeout;
|
||||
private volatile ConnThrottler _minuteThrottler;
|
||||
private volatile ConnThrottler _hourThrottler;
|
||||
@ -299,24 +300,24 @@ class ConnectionManager {
|
||||
long expiration = _context.clock().now() + opts.getConnectTimeout();
|
||||
if (opts.getConnectTimeout() <= 0)
|
||||
expiration = _context.clock().now() + DEFAULT_STREAM_DELAY_MAX;
|
||||
_numWaiting++;
|
||||
_numWaiting.incrementAndGet();
|
||||
while (true) {
|
||||
long remaining = expiration - _context.clock().now();
|
||||
if (remaining <= 0) {
|
||||
_log.logAlways(Log.WARN, "Refusing to connect since we have exceeded our max of "
|
||||
+ _defaultOptions.getMaxConns() + " connections");
|
||||
_numWaiting--;
|
||||
_numWaiting.decrementAndGet();
|
||||
return null;
|
||||
}
|
||||
|
||||
if (locked_tooManyStreams()) {
|
||||
int max = _defaultOptions.getMaxConns();
|
||||
// allow a full buffer of pending/waiting streams
|
||||
if (_numWaiting > max) {
|
||||
if (_numWaiting.get() > max) {
|
||||
_log.logAlways(Log.WARN, "Refusing connection since we have exceeded our max of "
|
||||
+ max + " and there are " + _numWaiting
|
||||
+ " waiting already");
|
||||
_numWaiting--;
|
||||
_numWaiting.decrementAndGet();
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -346,8 +347,14 @@ class ConnectionManager {
|
||||
if (opts.getConnectDelay() <= 0) {
|
||||
con.waitForConnect();
|
||||
}
|
||||
if (_numWaiting > 0)
|
||||
_numWaiting--;
|
||||
// safe decrement
|
||||
for (;;) {
|
||||
int n = _numWaiting.get();
|
||||
if (n <= 0)
|
||||
break;
|
||||
if (_numWaiting.compareAndSet(n, n - 1))
|
||||
break;
|
||||
}
|
||||
|
||||
_context.statManager().addRateData("stream.connectionCreated", 1, 0);
|
||||
return con;
|
||||
|
@ -36,9 +36,9 @@ class MessageInputStream extends InputStream {
|
||||
private final List<ByteArray> _readyDataBlocks;
|
||||
private int _readyDataBlockIndex;
|
||||
/** highest message ID used in the readyDataBlocks */
|
||||
private volatile long _highestReadyBlockId;
|
||||
private long _highestReadyBlockId;
|
||||
/** highest overall message ID */
|
||||
private volatile long _highestBlockId;
|
||||
private long _highestBlockId;
|
||||
/**
|
||||
* Message ID (Long) to ByteArray for blocks received
|
||||
* out of order when there are lower IDs not yet
|
||||
@ -76,16 +76,18 @@ class MessageInputStream extends InputStream {
|
||||
* @return highest data block ID completely received or -1 for none
|
||||
*/
|
||||
public long getHighestReadyBockId() {
|
||||
// not synchronized as it doesnt hurt to read a too-low value
|
||||
return _highestReadyBlockId;
|
||||
synchronized (_dataLock) {
|
||||
return _highestReadyBlockId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return highest data block ID received or -1 for none
|
||||
*/
|
||||
public long getHighestBlockId() {
|
||||
// not synchronized as it doesnt hurt to read a too-low value
|
||||
return _highestBlockId;
|
||||
synchronized (_dataLock) {
|
||||
return _highestBlockId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,7 +24,7 @@ class PacketLocal extends Packet implements MessageOutputStream.WriteStatus {
|
||||
private final Destination _to;
|
||||
private SessionKey _keyUsed;
|
||||
private final long _createdOn;
|
||||
private volatile int _numSends;
|
||||
private final AtomicInteger _numSends = new AtomicInteger();
|
||||
private volatile long _lastSend;
|
||||
private long _acceptedOn;
|
||||
/** LOCKING: this */
|
||||
@ -99,9 +99,10 @@ class PacketLocal extends Packet implements MessageOutputStream.WriteStatus {
|
||||
public void prepare() {
|
||||
if (_connection != null)
|
||||
_connection.getInputStream().updateAcks(this);
|
||||
if (_numSends > 0) {
|
||||
int numSends = _numSends.get();
|
||||
if (numSends > 0) {
|
||||
// so we can debug to differentiate resends
|
||||
setOptionalDelay(_numSends * 1000);
|
||||
setOptionalDelay(numSends * 1000);
|
||||
setFlag(FLAG_DELAY_REQUESTED);
|
||||
}
|
||||
}
|
||||
@ -109,7 +110,7 @@ class PacketLocal extends Packet implements MessageOutputStream.WriteStatus {
|
||||
public long getCreatedOn() { return _createdOn; }
|
||||
public long getLifetime() { return _context.clock().now() - _createdOn; }
|
||||
public void incrementSends() {
|
||||
_numSends++;
|
||||
_numSends.incrementAndGet();
|
||||
_lastSend = _context.clock().now();
|
||||
}
|
||||
|
||||
@ -152,7 +153,7 @@ class PacketLocal extends Packet implements MessageOutputStream.WriteStatus {
|
||||
else
|
||||
return (int)(_ackOn - _createdOn);
|
||||
}
|
||||
public int getNumSends() { return _numSends; }
|
||||
public int getNumSends() { return _numSends.get(); }
|
||||
public long getLastSend() { return _lastSend; }
|
||||
|
||||
/** @return null if not bound */
|
||||
@ -166,7 +167,7 @@ class PacketLocal extends Packet implements MessageOutputStream.WriteStatus {
|
||||
final int cnt = _nackCount.incrementAndGet();
|
||||
SimpleTimer2.TimedEvent evt = _resendEvent;
|
||||
if (cnt >= Connection.FAST_RETRANSMIT_THRESHOLD && evt != null && (!_retransmitted) &&
|
||||
(_numSends == 1 || _lastSend < _context.clock().now() - 4*1000)) { // Don't fast retx if we recently resent it
|
||||
(_numSends.get() == 1 || _lastSend < _context.clock().now() - 4*1000)) { // Don't fast retx if we recently resent it
|
||||
_retransmitted = true;
|
||||
evt.reschedule(0);
|
||||
// the predicate used to be '+', changing to '-' --zab
|
||||
@ -174,13 +175,13 @@ class PacketLocal extends Packet implements MessageOutputStream.WriteStatus {
|
||||
if (_log.shouldLog(Log.DEBUG)) {
|
||||
final String log = String.format("%s nacks and retransmits. Criteria: nacks=%d, retransmitted=%b,"+
|
||||
" numSends=%d, lastSend=%d, now=%d",
|
||||
toString(), cnt, _retransmitted, _numSends, _lastSend, _context.clock().now());
|
||||
toString(), cnt, _retransmitted, _numSends.get(), _lastSend, _context.clock().now());
|
||||
_log.debug(log);
|
||||
}
|
||||
} else if (_log.shouldLog(Log.DEBUG)) {
|
||||
final String log = String.format("%s nack but no retransmit. Criteria: nacks=%d, retransmitted=%b,"+
|
||||
" numSends=%d, lastSend=%d, now=%d",
|
||||
toString(), cnt, _retransmitted, _numSends, _lastSend, _context.clock().now());
|
||||
toString(), cnt, _retransmitted, _numSends.get(), _lastSend, _context.clock().now());
|
||||
_log.debug(log);
|
||||
}
|
||||
}
|
||||
@ -203,8 +204,9 @@ class PacketLocal extends Packet implements MessageOutputStream.WriteStatus {
|
||||
buf.append(" ack after ").append(getAckTime());
|
||||
}
|
||||
|
||||
if (_numSends > 1)
|
||||
buf.append(" sent ").append(_numSends).append(" times");
|
||||
int numSends = _numSends.get();
|
||||
if (numSends > 1)
|
||||
buf.append(" sent ").append(numSends).append(" times");
|
||||
|
||||
if (isFlagSet(FLAG_SYNCHRONIZE |
|
||||
FLAG_CLOSE |
|
||||
|
Reference in New Issue
Block a user