@Override public ClusterableServer create(final ServerAddress serverAddress, final ServerListener serverListener, final ClusterClock clusterClock) { ConnectionPool connectionPool = new DefaultConnectionPool(new ServerId(clusterId, serverAddress), new InternalStreamConnectionFactory(streamFactory, credentialList, applicationName, mongoDriverInformation, compressorList, commandListener), connectionPoolSettings); // no credentials, compressor list, or command listener for the server monitor factory ServerMonitorFactory serverMonitorFactory = new DefaultServerMonitorFactory(new ServerId(clusterId, serverAddress), serverSettings, clusterClock, new InternalStreamConnectionFactory(heartbeatStreamFactory, Collections.<MongoCredentialWithCache>emptyList(), applicationName, mongoDriverInformation, Collections.<MongoCompressor>emptyList(), null), connectionPool); return new DefaultServer(new ServerId(clusterId, serverAddress), clusterSettings.getMode(), connectionPool, new DefaultConnectionFactory(), serverMonitorFactory, serverListener, commandListener, clusterClock); }
/** * Gets the server address. * * @return the server address */ public ServerAddress getServerAddress() { return connectionId.getServerId().getAddress(); }
String getMBeanObjectName(final ServerId serverId) { String name = format("org.mongodb.driver:type=ConnectionPool,clusterId=%s,host=%s,port=%s", ensureValidValue(serverId.getClusterId().getValue()), ensureValidValue(serverId.getAddress().getHost()), serverId.getAddress().getPort()); if (serverId.getClusterId().getDescription() != null) { name = format("%s,description=%s", name, ensureValidValue(serverId.getClusterId().getDescription())); } return name; }
private PoolMetric(final ServerId serverId) { final String clusterId = serverId.getClusterId().getValue(); poolSizeGauge = DittoMetrics.gauge(metricName + POOL_PREFIX + POOL_SIZE) .tag(CLUSTER_ID_TAG, clusterId); poolSizeGauge.set(0L); checkOutCountGauge = DittoMetrics.gauge(metricName + POOL_PREFIX + CHECKED_OUT_COUNT) .tag(CLUSTER_ID_TAG, clusterId); checkOutCountGauge.set(0L); waitQueueGauge = DittoMetrics.gauge(metricName + POOL_PREFIX + WAIT_QUEUE_SIZE) .tag(CLUSTER_ID_TAG, clusterId); waitQueueGauge.set(0L); }
@Override public int hashCode() { int result = serverId.hashCode(); result = 31 * result + localValue; result = 31 * result + (serverValue != null ? serverValue.hashCode() : 0); return result; }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ConnectionId that = (ConnectionId) o; if (localValue != that.localValue) { return false; } if (!serverId.equals(that.serverId)) { return false; } if (serverValue != null ? !serverValue.equals(that.serverValue) : that.serverValue != null) { return false; } return true; }
private PoolMetric(final ServerId serverId) { final String clusterId = serverId.getClusterId().getValue(); poolSizeGauge = DittoMetrics.gauge(metricName + POOL_PREFIX + POOL_SIZE) .tag(CLUSTER_ID_TAG, clusterId); poolSizeGauge.set(0L); checkOutCountGauge = DittoMetrics.gauge(metricName + POOL_PREFIX + CHECKED_OUT_COUNT) .tag(CLUSTER_ID_TAG, clusterId); checkOutCountGauge.set(0L); waitQueueGauge = DittoMetrics.gauge(metricName + POOL_PREFIX + WAIT_QUEUE_SIZE) .tag(CLUSTER_ID_TAG, clusterId); waitQueueGauge.set(0L); }
@Override public int hashCode() { int result = serverId.hashCode(); result = 31 * result + localValue; result = 31 * result + (serverValue != null ? serverValue.hashCode() : 0); return result; }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ConnectionId that = (ConnectionId) o; if (localValue != that.localValue) { return false; } if (!serverId.equals(that.serverId)) { return false; } if (serverValue != null ? !serverValue.equals(that.serverValue) : that.serverValue != null) { return false; } return true; }
private MongoTimeoutException createTimeoutException() { return new MongoTimeoutException(format("Timed out after %d ms while waiting for a connection to server %s.", settings.getMaxWaitTime(MILLISECONDS), serverId.getAddress())); }
DefaultServerMonitor(final ServerId serverId, final ServerSettings serverSettings, final ClusterClock clusterClock, final ChangeListener<ServerDescription> serverStateListener, final InternalConnectionFactory internalConnectionFactory, final ConnectionPool connectionPool) { this.serverSettings = notNull("serverSettings", serverSettings); this.serverId = notNull("serverId", serverId); this.serverMonitorListener = getServerMonitorListener(serverSettings); this.clusterClock = notNull("clusterClock", clusterClock); this.serverStateListener = serverStateListener; this.internalConnectionFactory = notNull("internalConnectionFactory", internalConnectionFactory); this.connectionPool = connectionPool; monitor = new ServerMonitorRunnable(); monitorThread = new Thread(monitor, "cluster-" + this.serverId.getClusterId() + "-" + this.serverId.getAddress()); monitorThread.setDaemon(true); isClosed = false; }
@Override public ClusterableServer create(final ServerAddress serverAddress, final ServerListener serverListener, final ClusterClock clusterClock) { ConnectionPool connectionPool = new DefaultConnectionPool(new ServerId(clusterId, serverAddress), new InternalStreamConnectionFactory(streamFactory, credentialList, applicationName, mongoDriverInformation, compressorList, commandListener), connectionPoolSettings); // no credentials, compressor list, or command listener for the server monitor factory ServerMonitorFactory serverMonitorFactory = new DefaultServerMonitorFactory(new ServerId(clusterId, serverAddress), serverSettings, clusterClock, new InternalStreamConnectionFactory(heartbeatStreamFactory, Collections.<MongoCredentialWithCache>emptyList(), applicationName, mongoDriverInformation, Collections.<MongoCompressor>emptyList(), null), connectionPool); return new DefaultServer(new ServerId(clusterId, serverAddress), clusterSettings.getMode(), connectionPool, new DefaultConnectionFactory(), serverMonitorFactory, serverListener, commandListener, clusterClock); }
ConnectionPoolStatistics(final ConnectionPoolOpenedEvent event) { serverAddress = event.getServerId().getAddress(); settings = event.getSettings(); }
String getMBeanObjectName(final ServerId serverId) { String name = format("org.mongodb.driver:type=ConnectionPool,clusterId=%s,host=%s,port=%s", ensureValidValue(serverId.getClusterId().getValue()), ensureValidValue(serverId.getAddress().getHost()), serverId.getAddress().getPort()); if (serverId.getClusterId().getDescription() != null) { name = format("%s,description=%s", name, ensureValidValue(serverId.getClusterId().getDescription())); } return name; }
EmbeddedInternalConnection(final Pointer instancePointer, final CommandListener commandListener, final BsonDocument clientMetadataDocument) { this.clientStatusPointer = MongoDBCAPIHelper.createStatusPointer(); this.clientPointer = MongoDBCAPIHelper.create_client(instancePointer, clientStatusPointer); this.wrapped = new InternalStreamConnection(new ServerId(new ClusterId(), new ServerAddress()), new StreamFactory() { @Override public Stream create(final ServerAddress serverAddress) { return new EmbeddedInternalStream(); } }, Collections.<MongoCompressor>emptyList(), commandListener, new InternalStreamConnectionInitializer(Collections.<Authenticator>emptyList(), clientMetadataDocument, Collections.<MongoCompressor>emptyList())); }
/** * If there was a socket exception that wasn't some form of interrupted read, increment the generation count so that any connections * created prior will be discarded. * * @param connection the connection that generated the exception * @param t the exception */ private void incrementGenerationOnSocketException(final InternalConnection connection, final Throwable t) { if (t instanceof MongoSocketException && !(t instanceof MongoSocketReadTimeoutException)) { if (LOGGER.isWarnEnabled()) { LOGGER.warn(format("Got socket exception on connection [%s] to %s. All connections to %s will be closed.", getId(connection), serverId.getAddress(), serverId.getAddress())); } invalidate(); } }
DefaultServerMonitor(final ServerId serverId, final ServerSettings serverSettings, final ClusterClock clusterClock, final ChangeListener<ServerDescription> serverStateListener, final InternalConnectionFactory internalConnectionFactory, final ConnectionPool connectionPool) { this.serverSettings = notNull("serverSettings", serverSettings); this.serverId = notNull("serverId", serverId); this.serverMonitorListener = getServerMonitorListener(serverSettings); this.clusterClock = notNull("clusterClock", clusterClock); this.serverStateListener = serverStateListener; this.internalConnectionFactory = notNull("internalConnectionFactory", internalConnectionFactory); this.connectionPool = connectionPool; monitor = new ServerMonitorRunnable(); monitorThread = new Thread(monitor, "cluster-" + this.serverId.getClusterId() + "-" + this.serverId.getAddress()); monitorThread.setDaemon(true); isClosed = false; }
private void logStateChange(final ServerDescription previousServerDescription, final ServerDescription currentServerDescription) { if (shouldLogStageChange(previousServerDescription, currentServerDescription)) { if (currentServerDescription.getException() != null) { LOGGER.info(format("Exception in monitor thread while connecting to server %s", serverId.getAddress()), currentServerDescription.getException()); } else { LOGGER.info(format("Monitor thread successfully connected to server with description %s", currentServerDescription)); } } }
@Override public void close() { // All but the first call is a no-op if (!isClosed.getAndSet(true)) { if (!DefaultConnectionPool.this.closed) { connectionPoolListener.connectionCheckedIn(new ConnectionCheckedInEvent(getId(wrapped))); if (LOGGER.isTraceEnabled()) { LOGGER.trace(format("Checked in connection [%s] to server %s", getId(wrapped), serverId.getAddress())); } } pool.release(wrapped, wrapped.isClosed() || shouldPrune(wrapped)); } }
@Override public void open() { isTrue("Open already called", stream == null); stream = streamFactory.create(serverId.getAddress()); try { stream.open(); description = connectionInitializer.initialize(this); opened.set(true); sendCompressor = findSendCompressor(description); LOGGER.info(format("Opened connection [%s] to %s", getId(), serverId.getAddress())); } catch (Throwable t) { close(); if (t instanceof MongoException) { throw (MongoException) t; } else { throw new MongoException(t.toString(), t); } } }