Skip to content

Commit 363401b

Browse files
committed
JAVA-836: Changed logic for logging of up and down servers. Now the background thread logs on every state change, which will reduce log spamming dramatically.
1 parent 289edc6 commit 363401b

File tree

5 files changed

+38
-49
lines changed

5 files changed

+38
-49
lines changed

src/main/com/mongodb/ConnectionStatus.java

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616
package com.mongodb;
1717

18-
import java.io.IOException;
1918
import java.util.ArrayList;
2019
import java.util.List;
2120
import java.util.logging.Level;
2221
import java.util.logging.Logger;
2322

23+
import static com.mongodb.ConnectionStatus.UpdatableNode.ConnectionState.Connected;
24+
import static com.mongodb.ConnectionStatus.UpdatableNode.ConnectionState.Connecting;
25+
import static com.mongodb.ConnectionStatus.UpdatableNode.ConnectionState.Unconnected;
26+
2427
/**
2528
* Base class for classes that manage connections to mongo instances as background tasks.
2629
*/
@@ -171,68 +174,53 @@ public BackgroundUpdater(final String name) {
171174
}
172175

173176
static abstract class UpdatableNode {
177+
178+
enum ConnectionState {
179+
Connecting, Connected, Unconnected
180+
}
181+
174182
UpdatableNode(final ServerAddress addr, Mongo mongo, MongoOptions mongoOptions) {
175183
this._addr = addr;
176184
this._mongo = mongo;
177185
this._mongoOptions = mongoOptions;
178186
this._port = new DBPort(addr, null, mongoOptions);
179187
}
180188

189+
public boolean isOk() {
190+
return _connectionState == Connected;
191+
}
192+
181193
public CommandResult update() {
182-
CommandResult res = null;
183194
try {
184195
long start = System.nanoTime();
185-
res = _port.runCommand(_mongo.getDB("admin"), isMasterCmd);
196+
CommandResult res = _port.runCommand(_mongo.getDB("admin"), isMasterCmd);
197+
186198
long end = System.nanoTime();
187199
float newPingMS = (end - start) / 1000000F;
188-
if (!successfullyContacted)
200+
if (_connectionState != Connected) {
189201
_pingTimeMS = newPingMS;
190-
else
202+
}
203+
else {
191204
_pingTimeMS = _pingTimeMS + ((newPingMS - _pingTimeMS) / latencySmoothFactor);
192-
193-
getLogger().log(Level.FINE, "Latency to " + _addr + " actual=" + newPingMS + " smoothed=" + _pingTimeMS);
194-
195-
successfullyContacted = true;
196-
197-
if (res == null) {
198-
throw new MongoInternalException("Invalid null value returned from isMaster");
199205
}
200206

201-
if (!_ok) {
202-
getLogger().log(Level.INFO, "Server seen up: " + _addr);
203-
}
204-
_ok = true;
207+
_maxBsonObjectSize = res.getInt("maxBsonObjectSize", Bytes.MAX_OBJECT_SIZE);
205208

206-
// max size was added in 1.8
207-
if (res.containsField("maxBsonObjectSize")) {
208-
_maxBsonObjectSize = (Integer) res.get("maxBsonObjectSize");
209-
} else {
210-
_maxBsonObjectSize = Bytes.MAX_OBJECT_SIZE;
211-
}
212-
} catch (Exception e) {
213-
if (!((_ok) ? true : (Math.random() > 0.1))) {
214-
return res;
209+
if (_connectionState != Connected) {
210+
_connectionState = Connected;
211+
getLogger().log(Level.INFO, "Server seen up: " + _addr);
215212
}
216213

217-
final StringBuilder logError = (new StringBuilder("Server seen down: ")).append(_addr);
218-
219-
if (e instanceof IOException) {
220-
221-
logError.append(" - ").append(IOException.class.getName());
222-
223-
if (e.getMessage() != null) {
224-
logError.append(" - message: ").append(e.getMessage());
225-
}
226-
227-
getLogger().log(Level.WARNING, logError.toString());
214+
getLogger().log(Level.FINE, "Latency to " + _addr + " actual=" + newPingMS + " smoothed=" + _pingTimeMS);
228215

229-
} else {
230-
getLogger().log(Level.WARNING, logError.toString(), e);
216+
return res;
217+
} catch (Exception e) {
218+
if (_connectionState != Unconnected) {
219+
_connectionState = Unconnected;
220+
getLogger().log(Level.WARNING, String.format("Server seen down: %s", _addr), e);
231221
}
232-
_ok = false;
222+
return null;
233223
}
234-
235-
return res;
236224
}
237225

238226
protected abstract Logger getLogger();
@@ -243,10 +231,9 @@ public CommandResult update() {
243231

244232
DBPort _port; // we have our own port so we can set different socket options and don't have to worry about the pool
245233

246-
boolean successfullyContacted = false;
247-
boolean _ok = false;
248234
float _pingTimeMS = 0;
249235
int _maxBsonObjectSize;
236+
ConnectionState _connectionState = Connecting;
250237
}
251238

252239
}

src/main/com/mongodb/DynamicConnectionStatus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void run() {
112112
while (!Thread.interrupted()) {
113113
try {
114114
node.update();
115-
if (node._ok) {
115+
if (node.isOk()) {
116116
notifyOfOkNode(node);
117117
return;
118118
}

src/main/com/mongodb/MongosStatus.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void run() {
6464
MongosNode bestThisPass = null;
6565
for (MongosNode cur : mongosNodes) {
6666
cur.update();
67-
if (cur._ok) {
67+
if (cur.isOk()) {
6868
if (bestThisPass == null || (cur._pingTimeMS < bestThisPass._pingTimeMS)) {
6969
bestThisPass = cur;
7070
}
@@ -109,7 +109,7 @@ private synchronized void setPreferred(final MongosNode bestThisPass) {
109109
if (bestThisPass == null) {
110110
preferred = null;
111111
} else {
112-
preferred = new Node(bestThisPass._pingTimeMS, bestThisPass._addr, bestThisPass._maxBsonObjectSize, bestThisPass._ok);
112+
preferred = new Node(bestThisPass._pingTimeMS, bestThisPass._addr, bestThisPass._maxBsonObjectSize, bestThisPass.isOk());
113113
}
114114
notifyAll();
115115
}

src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ private void updateAddr() {
601601

602602
void update(Set<UpdatableReplicaSetNode> seenNodes) {
603603
CommandResult res = update();
604-
if (res == null || !_ok) {
604+
if (res == null || !isOk()) {
605605
return;
606606
}
607607

@@ -769,7 +769,7 @@ public synchronized void updateAll(){
769769
private List<ReplicaSetNode> createNodeList() {
770770
List<ReplicaSetNode> nodeList = new ArrayList<ReplicaSetNode>(_all.size());
771771
for (UpdatableReplicaSetNode cur : _all) {
772-
nodeList.add(new ReplicaSetNode(cur._addr, cur._names, cur._setName, cur._pingTimeMS, cur._ok, cur._isMaster, cur._isSecondary, cur._tags, cur._maxBsonObjectSize));
772+
nodeList.add(new ReplicaSetNode(cur._addr, cur._names, cur._setName, cur._pingTimeMS, cur.isOk(), cur._isMaster, cur._isSecondary, cur._tags, cur._maxBsonObjectSize));
773773
}
774774
return nodeList;
775775
}

src/test/com/mongodb/ReplicaSetStatusDomainModelTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.concurrent.atomic.AtomicReference;
1212
import java.util.logging.Logger;
1313

14+
import static com.mongodb.ConnectionStatus.UpdatableNode.ConnectionState.Connected;
15+
1416
/**
1517
* Copyright (c) 2008 - 2011 10gen, Inc. <http://10gen.com>
1618
* <p/>
@@ -240,7 +242,7 @@ private void addNodeToLists(String address, boolean isSecondary, float pingTime,
240242
ServerAddress serverAddress = new ServerAddress(address);
241243
ReplicaSetStatus.UpdatableReplicaSetNode updatableNode
242244
= new ReplicaSetStatus.UpdatableReplicaSetNode(serverAddress, updatableNodes, _logger, null, _mongoOptions, _lastPrimarySignal);
243-
updatableNode._ok = true;
245+
updatableNode._connectionState = Connected;
244246
updatableNode._pingTimeMS = pingTime;
245247
updatableNode._isSecondary = isSecondary;
246248
updatableNode._isMaster = !isSecondary;

0 commit comments

Comments
 (0)