Skip to content

Commit

Permalink
SERVER-9518 Implement functions in mongos and mongod for locking auth…
Browse files Browse the repository at this point in the history
…orization data
  • Loading branch information
stbrody committed Sep 11, 2013
1 parent 260079c commit a688dfe
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/mongo/db/auth/authz_manager_external_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,14 @@ namespace mongo {
* to authorization, namely the admin.system.users, admin.system.roles, and
* admin.system.version collections. This serializes all writers to the authorization
* documents, but does not impact readers.
* This can only be called when already in the AuthorizationManager's _lock.
*/
virtual bool tryAcquireAuthzUpdateLock() = 0;

/**
* Releases the lock guarding modifications to persistent authorization data, which must
* already be held.
* This can only be called when already in the AuthorizationManager's _lock.
*/
virtual void releaseAuthzUpdateLock() = 0;

Expand Down
7 changes: 5 additions & 2 deletions src/mongo/db/auth/authz_manager_external_state_d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

#include "mongo/db/auth/authz_manager_external_state_d.h"

#include <string>
#include <boost/thread/mutex.hpp>

#include "mongo/base/status.h"
#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/user_name.h"
Expand Down Expand Up @@ -232,11 +235,11 @@ namespace {
}

bool AuthzManagerExternalStateMongod::tryAcquireAuthzUpdateLock() {
fassertFailed(17099);
return _authzDataUpdateLock.try_lock();
}

void AuthzManagerExternalStateMongod::releaseAuthzUpdateLock() {
fassertFailed(17100);
return _authzDataUpdateLock.unlock();
}

} // namespace mongo
4 changes: 4 additions & 0 deletions src/mongo/db/auth/authz_manager_external_state_d.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#pragma once

#include <boost/thread/mutex.hpp>
#include <string>

#include "mongo/base/disallow_copying.h"
Expand Down Expand Up @@ -86,6 +87,9 @@ namespace mongo {
virtual Status _findUser(const string& usersNamespace,
const BSONObj& query,
BSONObj* result);

private:
boost::mutex _authzDataUpdateLock;
};

} // namespace mongo
22 changes: 20 additions & 2 deletions src/mongo/db/auth/authz_manager_external_state_s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@

#include "mongo/db/auth/authz_manager_external_state_s.h"

#include <boost/scoped_ptr.hpp>
#include <string>

#include "mongo/client/dbclientinterface.h"
#include "mongo/client/distlock.h"
#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/user_name.h"
#include "mongo/db/jsobj.h"
#include "mongo/s/config.h"
#include "mongo/s/type_database.h"
#include "mongo/s/grid.h"
#include "mongo/util/assert_util.h"
Expand All @@ -46,6 +49,7 @@ namespace {
}

AuthzManagerExternalStateMongos::AuthzManagerExternalStateMongos() {}

AuthzManagerExternalStateMongos::~AuthzManagerExternalStateMongos() {}

namespace {
Expand Down Expand Up @@ -259,11 +263,25 @@ namespace {
}

bool AuthzManagerExternalStateMongos::tryAcquireAuthzUpdateLock() {
fassertFailed(17109);
if (_authzDataUpdateLock.get()) {
return false;
}
_authzDataUpdateLock.reset(new ScopedDistributedLock(
configServer.getConnectionString(), "authorizationData"));

std::string errmsg;
if (!_authzDataUpdateLock->tryAcquire(&errmsg)) {
warning() <<
"Error while attempting to acquire distributed lock for user modification: " <<
errmsg << endl;
_authzDataUpdateLock.reset();
return false;
}
return true;
}

void AuthzManagerExternalStateMongos::releaseAuthzUpdateLock() {
fassertFailed(17110);
_authzDataUpdateLock.reset();
}

} // namespace mongo
5 changes: 5 additions & 0 deletions src/mongo/db/auth/authz_manager_external_state_s.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@

#pragma once

#include <boost/scoped_ptr.hpp>
#include <string>

#include "mongo/base/disallow_copying.h"
#include "mongo/base/status.h"
#include "mongo/client/distlock.h"
#include "mongo/db/auth/authz_manager_external_state.h"
#include "mongo/db/auth/user_name.h"

Expand Down Expand Up @@ -86,6 +88,9 @@ namespace mongo {
virtual Status _findUser(const string& usersNamespace,
const BSONObj& query,
BSONObj* result);

private:
scoped_ptr<ScopedDistributedLock> _authzDataUpdateLock;
};

} // namespace mongo

0 comments on commit a688dfe

Please sign in to comment.