void _pickInitial() throws MongoException { if (_curAddress != null) return; // we need to just get a server to query for ismaster _pickCurrent(); try { _logger.info("current address beginning of _pickInitial: " + _curAddress); DBObject im = isMasterCmd(); if (_isMaster(im)) return; synchronized (_allHosts) { Collections.shuffle(_allHosts); for (ServerAddress a : _allHosts) { if (_curAddress == a) continue; _logger.info("remote [" + _curAddress + "] -> [" + a + "]"); _set(a); im = isMasterCmd(); if (_isMaster(im)) return; _logger.severe("switched to: " + a + " but isn't master"); } throw new MongoException("can't find master"); } } catch (Exception e) { _logger.log(Level.SEVERE, "can't pick initial master, using random one", e); } }
/** * Returns all supported capture sizes. * * @return an array of capture sizes, in bytes, never <code>null</code>. */ public Integer[] getCaptureSizes() { final String rawValue = this.properties.get(DEVICE_CAPTURESIZES); final String[] values = rawValue.split(",\\s*"); final List<Integer> result = new ArrayList<Integer>(); for (String value : values) { result.add(Integer.valueOf(value.trim())); } Collections.sort( result, NumberUtils.<Integer>createNumberComparator(false /* aSortAscending */)); return result.toArray(new Integer[result.size()]); }
public void actionPerformed(ActionEvent e) { Iterator toInterp = Collections.singleton(desc).iterator(); JMenuItem jmi = (JMenuItem) e.getSource(); Iterator marks = mediator.getMarkerModel().getMarkersWithLabel(jmi.getText()); if (marks.hasNext()) { ChronicleMarker marker = (ChronicleMarker) marks.next(); Instant to = marker.getWhen(); Instant from = mediator.getMajorMoment(); mediator.getPropagator().interpolateDescriptors(toInterp, from, to); } }
ReplicaSetStatus(Mongo mongo, List<ServerAddress> initial) { _mongo = mongo; _all = Collections.synchronizedList(new ArrayList<Node>()); for (ServerAddress addr : initial) { _all.add(new Node(addr)); } _nextResolveTime = System.currentTimeMillis() + inetAddrCacheMS; _updater = new Updater(); _updater.start(); }
static class Holder { Holder(MongoOptions options) { _options = options; } DBPortPool get(InetSocketAddress addr) { DBPortPool p = _pools.get(addr); if (p != null) return p; synchronized (_pools) { p = _pools.get(addr); if (p != null) { return p; } p = new DBPortPool(addr, _options); _pools.put(addr, p); String name = "com.mongodb:type=ConnectionPool,host=" + addr.toString().replace(':', '_'); try { ObjectName on = new ObjectName(name); if (_server.isRegistered(on)) { _server.unregisterMBean(on); Bytes.LOGGER.log( Level.INFO, "multiple Mongo instances for same host, jmx numbers might be off"); } _server.registerMBean(p, on); } catch (JMException e) { Bytes.LOGGER.log(Level.WARNING, "jmx registration error, continuing", e); } catch (java.security.AccessControlException e) { Bytes.LOGGER.log(Level.WARNING, "jmx registration error, continuing", e); } } return p; } void close() { synchronized (_pools) { for (DBPortPool p : _pools.values()) { p.close(); } } } final MongoOptions _options; final Map<InetSocketAddress, DBPortPool> _pools = Collections.synchronizedMap(new HashMap<InetSocketAddress, DBPortPool>()); final MBeanServer _server = ManagementFactory.getPlatformMBeanServer(); }
/** * A TaskTracker wants to know the physical locations of completed, but not yet closed, tasks. * This exists so the reduce task thread can locate map task outputs. */ public synchronized MapOutputLocation[] locateMapOutputs( String taskId, String[][] mapTasksNeeded) { ArrayList v = new ArrayList(); for (int i = 0; i < mapTasksNeeded.length; i++) { for (int j = 0; j < mapTasksNeeded[i].length; j++) { TaskInProgress tip = (TaskInProgress) taskidToTIPMap.get(mapTasksNeeded[i][j]); if (tip != null && tip.isComplete(mapTasksNeeded[i][j])) { String trackerId = (String) taskidToTrackerMap.get(mapTasksNeeded[i][j]); TaskTrackerStatus tracker; synchronized (taskTrackers) { tracker = (TaskTrackerStatus) taskTrackers.get(trackerId); } v.add(new MapOutputLocation(mapTasksNeeded[i][j], tracker.getHost(), tracker.getPort())); break; } } } // randomly shuffle results to load-balance map output requests Collections.shuffle(v); return (MapOutputLocation[]) v.toArray(new MapOutputLocation[v.size()]); }
private void _pickCurrent() throws MongoException { if (_allHosts == null) throw new MongoException( "got master/slave issue but not in master/slave mode on the client side"); synchronized (_allHosts) { Collections.shuffle(_allHosts); for (int i = 0; i < _allHosts.size(); i++) { ServerAddress a = _allHosts.get(i); if (a == _curAddress) continue; if (_curAddress != null) { _logger.info("switching from [" + _curAddress + "] to [" + a + "]"); } _set(a); return; } } throw new MongoException("couldn't find a new host to swtich too"); }
public class DBPort { public static final int PORT = 27017; static final boolean USE_NAGLE = false; static final long CONN_RETRY_TIME_MS = 15000; public DBPort(InetSocketAddress addr) throws IOException { this(addr, null, new MongoOptions()); } DBPort(InetSocketAddress addr, DBPortPool pool, MongoOptions options) throws IOException { _options = options; _addr = addr; _pool = pool; _hashCode = _addr.hashCode(); _logger = Logger.getLogger(_rootLogger.getName() + "." + addr.toString()); } /** @param response will get wiped */ DBMessage call(DBMessage msg, ByteDecoder decoder) throws IOException { return go(msg, decoder); } void say(DBMessage msg) throws IOException { go(msg, null); } private synchronized DBMessage go(DBMessage msg, ByteDecoder decoder) throws IOException { if (_sock == null) _open(); { ByteBuffer out = msg.prepare(); while (out.remaining() > 0) _sock.write(out); } if (_pool != null) _pool._everWorked = true; if (decoder == null) return null; ByteBuffer response = decoder._buf; if (response.position() != 0) throw new IllegalArgumentException(); int read = 0; while (read < DBMessage.HEADER_LENGTH) read += _read(response); int len = response.getInt(0); if (len <= DBMessage.HEADER_LENGTH) throw new IllegalArgumentException("db sent invalid length: " + len); if (len > response.capacity()) throw new IllegalArgumentException( "db message size is too big (" + len + ") " + "max is (" + response.capacity() + ")"); response.limit(len); while (read < len) read += _read(response); if (read != len) throw new RuntimeException("something is wrong"); response.flip(); return new DBMessage(response); } public synchronized void ensureOpen() throws IOException { if (_sock != null) return; _open(); } void _open() throws IOException { long sleepTime = 100; final long start = System.currentTimeMillis(); while (true) { IOException lastError = null; try { _sock = SocketChannel.open(); _socket = _sock.socket(); _socket.connect(_addr, _options.connectTimeout); _socket.setTcpNoDelay(!USE_NAGLE); _socket.setSoTimeout(_options.socketTimeout); _in = _socket.getInputStream(); return; } catch (IOException ioe) { // TODO - erh to fix lastError = new IOException( "couldn't connect to [" + // _addr + "] bc:" + lastError , lastError ); lastError = new IOException("couldn't connect to [" + _addr + "] bc:" + ioe); _logger.log(Level.INFO, "connect fail to : " + _addr, ioe); } if (!_options.autoConnectRetry || (_pool != null && !_pool._everWorked)) throw lastError; long sleptSoFar = System.currentTimeMillis() - start; if (sleptSoFar >= CONN_RETRY_TIME_MS) throw lastError; if (sleepTime + sleptSoFar > CONN_RETRY_TIME_MS) sleepTime = CONN_RETRY_TIME_MS - sleptSoFar; _logger.severe( "going to sleep and retry. total sleep time after = " + (sleptSoFar + sleptSoFar) + "ms this time:" + sleepTime + "ms"); ThreadUtil.sleep(sleepTime); sleepTime *= 2; } } public int hashCode() { return _hashCode; } public String host() { return _addr.toString(); } public String toString() { return "{DBPort " + host() + "}"; } protected void finalize() { if (_sock != null) { try { _sock.close(); } catch (Exception e) { // don't care } _in = null; _socket = null; _sock = null; } } void checkAuth(DB db) { if (db._username == null) return; if (_authed.containsKey(db)) return; if (_inauth) return; _inauth = true; try { if (db.reauth()) { _authed.put(db, true); return; } } finally { _inauth = false; } throw new MongoInternalException("can't reauth!"); } private int _read(ByteBuffer buf) throws IOException { int x = _in.read(buf.array(), buf.position(), buf.remaining()); if (x < 0) throw new IOException("connection to server closed unexpectedly"); buf.position(buf.position() + x); return x; } final int _hashCode; final InetSocketAddress _addr; final DBPortPool _pool; final MongoOptions _options; final Logger _logger; private SocketChannel _sock; private Socket _socket; private InputStream _in; private boolean _inauth = false; private Map<DB, Boolean> _authed = Collections.synchronizedMap(new WeakHashMap<DB, Boolean>()); private static Logger _rootLogger = Logger.getLogger("com.mongodb.port"); }
public void actionPerformed(ActionEvent e) { Iterator toInterp = Collections.singleton(desc).iterator(); ViperViewMediator m = getMediator(); InterpQuery iq = new InterpQuery(toInterp, m); iq.setVisible(true); }
class Node { Node(ServerAddress addr) { _addr = addr; _port = new DBPort(addr, null, _mongoOptions); _names.add(addr.toString()); } private void updateAddr() { try { if (_addr.updateInetAddr()) { // address changed, need to use new ports _port = new DBPort(_addr, null, _mongoOptions); _mongo.getConnector().updatePortPool(_addr); } } catch (UnknownHostException ex) { _logger.log(Level.WARNING, null, ex); } } synchronized void update() { update(null); } synchronized void update(Set<Node> seenNodes) { try { long start = System.currentTimeMillis(); CommandResult res = _port.runCommand(_mongo.getDB("admin"), _isMasterCmd); _lastCheck = System.currentTimeMillis(); _pingTime = _lastCheck - start; if (res == null) { _ok = false; return; } _ok = true; _isMaster = res.getBoolean("ismaster", false); _isSecondary = res.getBoolean("secondary", false); _lastPrimarySignal = res.getString("primary"); if (res.containsField("hosts")) { for (Object x : (List) res.get("hosts")) { String host = x.toString(); Node node = _addIfNotHere(host); if (node != null && seenNodes != null) seenNodes.add(node); } } if (res.containsField("passives")) { for (Object x : (List) res.get("passives")) { String host = x.toString(); Node node = _addIfNotHere(host); if (node != null && seenNodes != null) seenNodes.add(node); } } if (_isMaster) { // max size was added in 1.8 if (res.containsField("maxBsonObjectSize")) maxBsonObjectSize = ((Integer) res.get("maxBsonObjectSize")).intValue(); else maxBsonObjectSize = Bytes.MAX_OBJECT_SIZE; } } catch (MongoException e) { Throwable root = e; if (e.getCause() != null) root = e.getCause(); _logger.log(Level.FINE, "node down: " + _addr + " " + root); _ok = false; } catch (Exception e) { _logger.log(Level.SEVERE, "can't update node: " + _addr, e); _ok = false; } if (!_isMaster) return; try { DB db = _mongo.getDB("local"); _port.checkAuth(db); DBObject config = _port.findOne(db, "system.replset", new BasicDBObject()); if (config == null) { // probably a replica pair // TODO: add this in when pairs are really gone // _logger.log( Level.SEVERE , "no replset config!" ); } else if (config.get("$err") != null && UNAUTHENTICATED_ERROR_CODE == (Integer) config.get("code")) { _logger.log( Level.WARNING, "Replica Set updater cannot get results, call authenticate on 'local' or 'admin' db"); } else { String setName = config.get("_id").toString(); if (_setName == null) { _setName = setName; _logger = Logger.getLogger(_rootLogger.getName() + "." + setName); } else if (!_setName.equals(setName)) { _logger.log(Level.SEVERE, "mis match set name old: " + _setName + " new: " + setName); return; } // TODO: look at members } } catch (MongoException e) { if (_setName != null) { // this probably means the master is busy, so going to ignore } else { _logger.log(Level.SEVERE, "can't get initial config from node: " + _addr, e); } } catch (Exception e) { _logger.log(Level.SEVERE, "unexpected error getting config from node: " + _addr, e); } } public boolean master() { return _ok && _isMaster; } public boolean secondary() { return _ok && _isSecondary; } public String toString() { StringBuilder buf = new StringBuilder(); buf.append("Replica Set Node: ").append(_addr).append("\n"); buf.append("\t ok \t").append(_ok).append("\n"); buf.append("\t ping \t").append(_pingTime).append("\n"); buf.append("\t master \t").append(_isMaster).append("\n"); buf.append("\t secondary \t").append(_isSecondary).append("\n"); buf.append("\t priority \t").append(_priority).append("\n"); return buf.toString(); } final ServerAddress _addr; final Set<String> _names = Collections.synchronizedSet(new HashSet<String>()); DBPort _port; // we have our own port so we can set different socket options and don't have to // owrry about the pool boolean _ok = false; long _lastCheck = 0; long _pingTime = 0; boolean _isMaster = false; boolean _isSecondary = false; double _priority = 0; }
// make sure Vects are disposed private static class TestVect extends ArrayVect1 { private static final long serialVersionUID = 1L; /** Visible only for tests. */ public static int max = 0; /** Visible only for tests. */ public static int total = 0; /** Visible only for tests. */ public static Map<Object, String> undisposed = Collections.synchronizedMap(new HashMap<Object, String>()); /** Visible only for tests. */ public String identity = "default"; @Override public void add(double scaleThis, double scaleOther, VectConst other) { assertSameType(other); super.add(scaleThis, scaleOther, other); } @Override public void project(double scaleThis, double scaleOther, VectConst other) { TestVect tv = (TestVect) other; if (!identity.equals(tv.identity)) { projectWasTested = true; } super.add(scaleThis, scaleOther, other); } @Override public double dot(VectConst other) { assertSameType(other); return super.dot(other); } private void assertSameType(VectConst other) { TestVect tv = (TestVect) other; if (!identity.equals(tv.identity)) { throw new IllegalArgumentException("different types"); } } /** * Constructor. * * @param data * @param variance * @param identity */ public TestVect(double[] data, double variance, String identity) { super(data, variance); this.identity = identity; remember(this); } @Override public TestVect clone() { TestVect result = (TestVect) super.clone(); remember(result); return result; } private void remember(Object tv) { // remember where allocated synchronized (undisposed) { java.io.StringWriter sw = new java.io.StringWriter(); java.io.PrintWriter pw = new java.io.PrintWriter(sw); new Exception("This vector was never disposed").printStackTrace(pw); pw.flush(); undisposed.put(tv, sw.toString()); // LOG.info("**********************************************"); // LOG.info(sw.toString()); max = Math.max(max, undisposed.size()); total += 1; if (undisposed.size() > 12 && !printedUndisposed) { LOG.severe("**********************************************"); LOG.severe(getTraces()); LOG.severe("**********************************************"); printedUndisposed = true; } } } @Override public void dispose() { synchronized (undisposed) { super.dispose(); undisposed.remove(this); } } /** * View traces for debugging * * @return printable version of traces */ public static String getTraces() { StringBuilder sb = new StringBuilder(); for (String s : undisposed.values()) { sb.append(s); sb.append("\n"); } return sb.toString(); } }
/** Gets the list of parsed symbols. */ public List<String> getSymbols() { return Collections.unmodifiableList(this.symbols); }