summaryrefslogtreecommitdiff
path: root/src/mongo/util
diff options
context:
space:
mode:
authorSpencer T Brody <spencer@10gen.com>2013-09-04 19:35:32 -0400
committerSpencer T Brody <spencer@10gen.com>2013-09-04 19:35:32 -0400
commit4d905c46a1cbc6f96bdd0522d0bd08f0c6c3f1c0 (patch)
tree9bb2421f3ce2042316c313ef6b8d1f0b8d12012a /src/mongo/util
parentdd3a6cf40e5259ad0ce5b3eb41fae9cd76f6bf95 (diff)
downloadmongo-4d905c46a1cbc6f96bdd0522d0bd08f0c6c3f1c0.tar.gz
Revert "SERVER-9818 allocate socket file descriptors before opening datafiles"
This reverts commit 607ae1384b26ebcb5e275a845f9806104852257f.
Diffstat (limited to 'src/mongo/util')
-rw-r--r--src/mongo/util/net/listen.cpp64
-rw-r--r--src/mongo/util/net/listen.h14
-rw-r--r--src/mongo/util/net/message_server.h1
-rw-r--r--src/mongo/util/net/message_server_port.cpp4
4 files changed, 40 insertions, 43 deletions
diff --git a/src/mongo/util/net/listen.cpp b/src/mongo/util/net/listen.cpp
index da51a2b6543..6ab0219e835 100644
--- a/src/mongo/util/net/listen.cpp
+++ b/src/mongo/util/net/listen.cpp
@@ -98,8 +98,7 @@ namespace mongo {
}
Listener::Listener(const string& name, const string &ip, int port, bool logConnect )
- : _port(port), _name(name), _ip(ip), _setupSocketsSuccessful(false),
- _logConnect(logConnect), _elapsedTime(0) {
+ : _port(port), _name(name), _ip(ip), _logConnect(logConnect), _elapsedTime(0) {
#ifdef MONGO_SSL
_ssl = getSSLManager();
#endif
@@ -110,11 +109,8 @@ namespace mongo {
_timeTracker = 0;
}
- void Listener::setupSockets() {
- checkTicketNumbers();
- _mine = ipToAddrs(_ip.c_str(), _port, (!cmdLine.noUnixSocket && useUnixSockets()));
-
- for (vector<SockAddr>::const_iterator it=_mine.begin(), end=_mine.end(); it != end; ++it) {
+ bool Listener::_setupSockets( const vector<SockAddr>& mine , vector<SOCKET>& socks ) {
+ for (vector<SockAddr>::const_iterator it=mine.begin(), end=mine.end(); it != end; ++it) {
const SockAddr& me = *it;
SOCKET sock = ::socket(me.getType(), SOCK_STREAM, 0);
@@ -152,7 +148,7 @@ namespace mongo {
if ( x == EADDRINUSE )
error() << " addr already in use" << endl;
closesocket(sock);
- return;
+ return false;
}
#if !defined(_WIN32)
@@ -167,28 +163,33 @@ namespace mongo {
if ( ::listen(sock, 128) != 0 ) {
error() << "listen(): listen() failed " << errnoWithDescription() << endl;
closesocket(sock);
- return;
+ return false;
}
ListeningSockets::get()->add( sock );
- _socks.push_back(sock);
+ socks.push_back(sock);
}
- _setupSocketsSuccessful = true;
+ return true;
}
#if !defined(_WIN32)
void Listener::initAndListen() {
- if (!_setupSocketsSuccessful) {
- return;
+ checkTicketNumbers();
+ vector<SOCKET> socks;
+
+ {
+ vector<SockAddr> mine = ipToAddrs(_ip.c_str(), _port, (!cmdLine.noUnixSocket && useUnixSockets()));
+ if ( ! _setupSockets( mine , socks ) )
+ return;
}
SOCKET maxfd = 0; // needed for select()
- for ( unsigned i=0; i<_socks.size(); i++ ) {
- if ( _socks[i] > maxfd )
- maxfd = _socks[i];
+ for ( unsigned i=0; i<socks.size(); i++ ) {
+ if ( socks[i] > maxfd )
+ maxfd = socks[i];
}
if ( maxfd >= FD_SETSIZE ) {
@@ -208,7 +209,7 @@ namespace mongo {
fd_set fds[1];
FD_ZERO(fds);
- for (vector<SOCKET>::iterator it=_socks.begin(), end=_socks.end(); it != end; ++it) {
+ for (vector<SOCKET>::iterator it=socks.begin(), end=socks.end(); it != end; ++it) {
FD_SET(*it, fds);
}
@@ -244,7 +245,7 @@ namespace mongo {
_elapsedTime += ret; // assume 1ms to grab connection. very rough
#endif
- for (vector<SOCKET>::iterator it=_socks.begin(), end=_socks.end(); it != end; ++it) {
+ for (vector<SOCKET>::iterator it=socks.begin(), end=socks.end(); it != end; ++it) {
if (! (FD_ISSET(*it, fds)))
continue;
SockAddr from;
@@ -348,8 +349,13 @@ namespace mongo {
};
void Listener::initAndListen() {
- if (!_setupSocketsSuccessful) {
- return;
+ checkTicketNumbers();
+ vector<SOCKET> socks;
+
+ {
+ vector<SockAddr> mine = ipToAddrs(_ip.c_str(), _port, false);
+ if ( ! _setupSockets( mine , socks ) )
+ return;
}
#ifdef MONGO_SSL
@@ -359,11 +365,11 @@ namespace mongo {
#endif
OwnedPointerVector<EventHolder> eventHolders;
- boost::scoped_array<WSAEVENT> events(new WSAEVENT[_socks.size()]);
+ boost::scoped_array<WSAEVENT> events(new WSAEVENT[socks.size()]);
// Populate events array with an event for each socket we are watching
- for (size_t count = 0; count < _socks.size(); ++count) {
+ for (size_t count = 0; count < socks.size(); ++count) {
EventHolder* ev(new EventHolder);
eventHolders.mutableVector().push_back(ev);
events[count] = ev->get();
@@ -371,8 +377,8 @@ namespace mongo {
while ( ! inShutdown() ) {
// Turn on listening for accept-ready sockets
- for (size_t count = 0; count < _socks.size(); ++count) {
- int status = WSAEventSelect(_socks[count], events[count], FD_ACCEPT | FD_CLOSE);
+ for (size_t count = 0; count < socks.size(); ++count) {
+ int status = WSAEventSelect(socks[count], events[count], FD_ACCEPT | FD_CLOSE);
if (status == SOCKET_ERROR) {
const int mongo_errno = WSAGetLastError();
error() << "Windows WSAEventSelect returned "
@@ -382,7 +388,7 @@ namespace mongo {
}
// Wait till one of them goes active, or we time out
- DWORD result = WSAWaitForMultipleEvents(_socks.size(),
+ DWORD result = WSAWaitForMultipleEvents(socks.size(),
events.get(),
FALSE, // don't wait for all the events
10, // timeout, in ms
@@ -404,7 +410,7 @@ namespace mongo {
DWORD eventIndex = result - WSA_WAIT_EVENT_0;
WSANETWORKEVENTS networkEvents;
// Extract event details, and clear event for next pass
- int status = WSAEnumNetworkEvents(_socks[eventIndex],
+ int status = WSAEnumNetworkEvents(socks[eventIndex],
events[eventIndex],
&networkEvents);
if (status == SOCKET_ERROR) {
@@ -431,7 +437,7 @@ namespace mongo {
continue;
}
- status = WSAEventSelect(_socks[eventIndex], NULL, 0);
+ status = WSAEventSelect(socks[eventIndex], NULL, 0);
if (status == SOCKET_ERROR) {
const int mongo_errno = WSAGetLastError();
error() << "Windows WSAEventSelect returned "
@@ -439,10 +445,10 @@ namespace mongo {
continue;
}
- disableNonblockingMode(_socks[eventIndex]);
+ disableNonblockingMode(socks[eventIndex]);
SockAddr from;
- int s = accept(_socks[eventIndex], from.raw(), &from.addressSize);
+ int s = accept(socks[eventIndex], from.raw(), &from.addressSize);
if ( s < 0 ) {
int x = errno; // so no global issues
if ( x == ECONNABORTED || x == EBADF ) {
diff --git a/src/mongo/util/net/listen.h b/src/mongo/util/net/listen.h
index da3a99170e7..01f2a4e9fd4 100644
--- a/src/mongo/util/net/listen.h
+++ b/src/mongo/util/net/listen.h
@@ -52,12 +52,6 @@ namespace mongo {
*/
long long getMyElapsedTimeMillis() const { return _elapsedTime; }
- /**
- * Allocate sockets for the listener and set _setupSocketsSuccessful to true
- * iff the process was successful.
- */
- void setupSockets();
-
void setAsTimeTracker() {
_timeTracker = this;
}
@@ -75,11 +69,8 @@ namespace mongo {
}
private:
- std::vector<SockAddr> _mine;
- std::vector<SOCKET> _socks;
std::string _name;
std::string _ip;
- bool _setupSocketsSuccessful;
bool _logConnect;
long long _elapsedTime;
@@ -87,6 +78,11 @@ namespace mongo {
SSLManagerInterface* _ssl;
#endif
+ /**
+ * @return true iff everything went ok
+ */
+ bool _setupSockets( const std::vector<SockAddr>& mine , std::vector<SOCKET>& socks );
+
void _logListen( int port , bool ssl );
static const Listener* _timeTracker;
diff --git a/src/mongo/util/net/message_server.h b/src/mongo/util/net/message_server.h
index ed6cf96de44..a33014641b2 100644
--- a/src/mongo/util/net/message_server.h
+++ b/src/mongo/util/net/message_server.h
@@ -61,7 +61,6 @@ namespace mongo {
virtual ~MessageServer() {}
virtual void run() = 0;
virtual void setAsTimeTracker() = 0;
- virtual void setupSockets() = 0;
};
// TODO use a factory here to decide between port and asio variations
diff --git a/src/mongo/util/net/message_server_port.cpp b/src/mongo/util/net/message_server_port.cpp
index 00bed7f7443..eeec3cc3263 100644
--- a/src/mongo/util/net/message_server_port.cpp
+++ b/src/mongo/util/net/message_server_port.cpp
@@ -126,10 +126,6 @@ namespace mongo {
Listener::setAsTimeTracker();
}
- virtual void setupSockets() {
- Listener::setupSockets();
- }
-
void run() {
initAndListen();
}