Skip to content

Commit

Permalink
SERVER-19720 harmonize handling of std::error_code and ErrorCodes in …
Browse files Browse the repository at this point in the history
…NetworkInterfaceASIO
  • Loading branch information
amidvidy committed Aug 13, 2015
1 parent 8753f23 commit 870efac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/mongo/executor/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ env.Library(
'network_interface_asio_operation.cpp',
],
LIBDEPS=[
'$BUILD_DIR/mongo/base/system_error',
'$BUILD_DIR/mongo/client/authentication',
'$BUILD_DIR/mongo/db/auth/authcommon',
'$BUILD_DIR/mongo/rpc/rpc',
Expand Down
12 changes: 5 additions & 7 deletions src/mongo/executor/async_secure_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@

#include "mongo/executor/async_secure_stream.h"

#include "mongo/base/system_error.h"
#include "mongo/config.h"
#include "mongo/util/net/ssl_manager.h"
#include "mongo/util/log.h"
#include "mongo/util/net/ssl_manager.h"

#ifdef MONGO_CONFIG_SSL

Expand All @@ -52,7 +53,7 @@ void AsyncSecureStream::connect(const asio::ip::tcp::resolver::iterator endpoint
asio::async_connect(_stream.lowest_layer(),
std::move(endpoints),
[this](std::error_code ec, asio::ip::tcp::resolver::iterator iter) {
if (ec) {
if (ec != ErrorCodes::OK) {
return _userHandler(ec);
}
return _handleConnect(ec, std::move(iter));
Expand All @@ -70,7 +71,7 @@ void AsyncSecureStream::read(asio::mutable_buffer buffer, StreamHandler&& stream
void AsyncSecureStream::_handleConnect(std::error_code ec, asio::ip::tcp::resolver::iterator iter) {
_stream.async_handshake(decltype(_stream)::client,
[this, iter](std::error_code ec) {
if (ec) {
if (ec != ErrorCodes::OK) {
return _userHandler(ec);
}
return _handleHandshake(ec, iter->host_name());
Expand All @@ -82,11 +83,8 @@ void AsyncSecureStream::_handleHandshake(std::error_code ec, const std::string&
getSSLManager()->parseAndValidatePeerCertificate(_stream.native_handle(), hostName);
if (!certStatus.isOK()) {
warning() << certStatus.getStatus();
return _userHandler(
// TODO: fix handling of std::error_code w.r.t. codes used by Status
std::error_code(certStatus.getStatus().code(), std::generic_category()));
}
_userHandler(std::error_code());
_userHandler(make_error_code(certStatus.getStatus().code()));
}

} // namespace executor
Expand Down
3 changes: 2 additions & 1 deletion src/mongo/executor/network_interface_asio.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <unordered_map>

#include "mongo/base/status.h"
#include "mongo/base/system_error.h"
#include "mongo/executor/network_interface.h"
#include "mongo/executor/remote_command_request.h"
#include "mongo/executor/remote_command_response.h"
Expand Down Expand Up @@ -225,7 +226,7 @@ class NetworkInterfaceASIO final : public NetworkInterface {
if (op->canceled())
return _completeOperation(op,
Status(ErrorCodes::CallbackCanceled, "Callback canceled"));
if (ec)
if (ec != ErrorCodes::OK)
return _networkErrorCallback(op, ec);

handler();
Expand Down
21 changes: 12 additions & 9 deletions src/mongo/executor/network_interface_asio_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
#include <type_traits>
#include <utility>

#include "mongo/executor/async_stream_interface.h"
#include "mongo/db/dbmessage.h"
#include "mongo/db/jsobj.h"
#include "mongo/executor/async_stream_interface.h"
#include "mongo/rpc/factory.h"
#include "mongo/rpc/protocol.h"
#include "mongo/rpc/reply_interface.h"
Expand Down Expand Up @@ -95,19 +95,16 @@ void asyncRecvMessageBody(AsyncStreamInterface& stream,
static_assert(
IsNetworkHandler<Handler>::value,
"Handler passed to asyncRecvMessageBody does not conform to NetworkHandler concept");
// TODO: This error code should be more meaningful.
std::error_code ec;

// validate message length
int len = header->constView().getMessageLength();
if (len == 542393671) {
LOG(3) << "attempt to access MongoDB over HTTP on the native driver port.";
return handler(ec, 0);
return handler(make_error_code(ErrorCodes::ProtocolError), 0);
} else if (static_cast<size_t>(len) < sizeof(MSGHEADER::Value) ||
static_cast<size_t>(len) > MaxMessageSizeBytes) {
warning() << "recv(): message len " << len << " is invalid. "
<< "Min " << sizeof(MSGHEADER::Value) << " Max: " << MaxMessageSizeBytes;
return handler(ec, 0);
return handler(make_error_code(ErrorCodes::InvalidLength), 0);
}

int z = (len + 1023) & 0xfffffc00;
Expand Down Expand Up @@ -220,7 +217,13 @@ void NetworkInterfaceASIO::_completedOpCallback(AsyncOp* op) {

void NetworkInterfaceASIO::_networkErrorCallback(AsyncOp* op, const std::error_code& ec) {
LOG(3) << "networking error occurred";
_completeOperation(op, Status(ErrorCodes::HostUnreachable, ec.message()));
if (ec.category() == mongoErrorCategory()) {
// If we get a Mongo error code, we can preserve it.
_completeOperation(op, Status(ErrorCodes::fromInt(ec.value()), ec.message()));
} else {
// If we get an asio or system error, we just convert it to a network error.
_completeOperation(op, Status(ErrorCodes::HostUnreachable, ec.message()));
}
}

// NOTE: This method may only be called by ASIO threads
Expand Down Expand Up @@ -252,7 +255,7 @@ void NetworkInterfaceASIO::_asyncRunCommand(AsyncCommand* cmd, NetworkOpHandler
// Step 3
auto recvHeaderCallback = [this, cmd, handler, recvMessageCallback](std::error_code ec,
size_t bytes) {
if (ec)
if (ec != ErrorCodes::OK)
return handler(ec, bytes);

// validate response id
Expand All @@ -272,7 +275,7 @@ void NetworkInterfaceASIO::_asyncRunCommand(AsyncCommand* cmd, NetworkOpHandler
// Step 2
auto sendMessageCallback = [this, cmd, handler, recvHeaderCallback](std::error_code ec,
size_t bytes) {
if (ec)
if (ec != ErrorCodes::OK)
return handler(ec, bytes);

asyncRecvMessageHeader(cmd->conn().stream(), &cmd->header(), std::move(recvHeaderCallback));
Expand Down

0 comments on commit 870efac

Please sign in to comment.