Skip to content

Commit

Permalink
SERVER-939 Update VersionType with new method of recording field pres…
Browse files Browse the repository at this point in the history
…ence and handling defaults
  • Loading branch information
sverch committed Jan 17, 2013
1 parent 588a593 commit 4d4f0c4
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/mongo/s/config_upgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ namespace mongo {

// Check that the mongo version of this process hasn't been excluded from the cluster
vector<MongoVersionRange> excludedRanges;
if (!MongoVersionRange::parseBSONArray(versionInfo.getExcludedRanges(),
if (!MongoVersionRange::parseBSONArray(versionInfo.getExcludingMongoVersions(),
&excludedRanges,
whyNot))
{
Expand Down
182 changes: 96 additions & 86 deletions src/mongo/s/type_config_version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "mongo/s/type_config_version.h"

#include "mongo/s/field_parser.h"
Expand All @@ -24,7 +23,7 @@ namespace mongo {

using mongoutils::str::stream;

const string VersionType::ConfigNS("config.version");
const std::string VersionType::ConfigNS = "config.version";

const BSONField<int> VersionType::minCompatibleVersion("minCompatibleVersion");
const BSONField<int> VersionType::currentVersion("currentVersion");
Expand All @@ -41,137 +40,148 @@ namespace mongo {
VersionType::~VersionType() {
}

bool VersionType::isValid(string* errMsg) const {

bool VersionType::isValid(std::string* errMsg) const {
std::string dummy;

if (errMsg == NULL) {
errMsg = &dummy;
}

if (_minVersion == -1) {
if (errMsg) {
*errMsg = stream() << "no version found";
}
// All the mandatory fields must be present.
if (!_isMinCompatibleVersionSet) {
*errMsg = stream() << "missing " << minCompatibleVersion.name() << " field";
return false;
}
if (!_isCurrentVersionSet) {
*errMsg = stream() << "missing " << currentVersion.name() << " field";
return false;
}

// Hardcoded 3 here because it's the last version without a cluster id
if (_currentVersion > 3 && !_clusterId.isSet()) {
if (errMsg) {
*errMsg = stream() << "no clusterId found";
}
if (_currentVersion > 3 && !_isClusterIdSet) {
*errMsg = stream() << "missing " << clusterId.name() << " field";
return false;
}

return true;
}

void VersionType::clear() {
_minVersion = -1;
_currentVersion = -1;
_clusterId = OID();
_excludes = BSONArray();
_upgradeId = OID();
_upgradeState = BSONObj();
BSONObj VersionType::toBSON() const {
BSONObjBuilder builder;

builder.append("_id", 1);
if (_isMinCompatibleVersionSet) {
builder.append(version_DEPRECATED(), _minCompatibleVersion);
builder.append(minCompatibleVersion(), _minCompatibleVersion);
}
if (_isCurrentVersionSet) builder.append(currentVersion(), _currentVersion);
if (_isExcludingMongoVersionsSet) {
builder.append(excludingMongoVersions(), _excludingMongoVersions);
}
if (_isClusterIdSet) builder.append(clusterId(), _clusterId);
if (_isUpgradeIdSet) {
builder.append(upgradeId(), _upgradeId);
builder.append(upgradeState(), _upgradeState);
}

return builder.obj();
}

bool VersionType::parseBSON(const BSONObj& source, string* errMsg) {
bool VersionType::parseBSON(BSONObj source, string* errMsg) {
clear();

string dummy;
std::string dummy;
if (!errMsg) errMsg = &dummy;

if (!FieldParser::extractNumber(source, minCompatibleVersion, -1, &_minVersion, errMsg)) return false;

if (_minVersion == -1) {
if (!FieldParser::extractNumber(source, version_DEPRECATED, -1, &_minVersion, errMsg)) return false;
FieldParser::FieldState fieldState;
fieldState = FieldParser::extractNumber(source, minCompatibleVersion,
-1, &_minCompatibleVersion, errMsg);
if (fieldState == FieldParser::FIELD_INVALID) return false;
_isMinCompatibleVersionSet = fieldState == FieldParser::FIELD_VALID;

if (!_isMinCompatibleVersionSet) {
fieldState = FieldParser::extractNumber(source, version_DEPRECATED,
-1, &_minCompatibleVersion, errMsg);
if (fieldState == FieldParser::FIELD_INVALID) return false;
_isMinCompatibleVersionSet = fieldState == FieldParser::FIELD_VALID;
}

if (!FieldParser::extractNumber(source, currentVersion, -1, &_currentVersion, errMsg)) return false;
fieldState = FieldParser::extractNumber(source, currentVersion,
-1, &_currentVersion, errMsg);
if (fieldState == FieldParser::FIELD_INVALID) return false;
_isCurrentVersionSet = fieldState == FieldParser::FIELD_VALID;

if (_currentVersion == -1) {
_currentVersion = _minVersion;
if (!_isCurrentVersionSet && _isMinCompatibleVersionSet) {
_currentVersion = _minCompatibleVersion;
_isCurrentVersionSet = true;
}

if (!FieldParser::extract(source, clusterId, OID(), &_clusterId, errMsg)) return false;
fieldState = FieldParser::extract(source, excludingMongoVersions,
BSONArray(), &_excludingMongoVersions, errMsg);
if (fieldState == FieldParser::FIELD_INVALID) return false;
_isExcludingMongoVersionsSet = fieldState == FieldParser::FIELD_VALID;

fieldState = FieldParser::extract(source, clusterId, OID(), &_clusterId, errMsg);
if (fieldState == FieldParser::FIELD_INVALID) return false;
_isClusterIdSet = fieldState == FieldParser::FIELD_VALID;

if (!FieldParser::extract(source, excludingMongoVersions, _excludes, &_excludes, errMsg)) return false;
fieldState = FieldParser::extract(source, upgradeId, OID(), &_upgradeId, errMsg);
if (fieldState == FieldParser::FIELD_INVALID) return false;
_isUpgradeIdSet = fieldState == FieldParser::FIELD_VALID;

if (!FieldParser::extract(source, upgradeId, OID(), &_upgradeId, errMsg)) return false;
if (!FieldParser::extract(source, upgradeState, BSONObj(), &_upgradeState, errMsg)) return false;
fieldState = FieldParser::extract(source, upgradeState, BSONObj(), &_upgradeState, errMsg);
if (fieldState == FieldParser::FIELD_INVALID) return false;
_isUpgradeStateSet = fieldState == FieldParser::FIELD_VALID;

return true;
}

BSONObj VersionType::toBSON() const {
void VersionType::clear() {

BSONObjBuilder bob;
bob << "_id" << 1;
bob << version_DEPRECATED(_minVersion);
bob << minCompatibleVersion(_minVersion);
bob << currentVersion(_currentVersion);
bob << clusterId(_clusterId);
bob << excludingMongoVersions(_excludes);

if (_upgradeId.isSet()) {
bob << upgradeId(_upgradeId);
bob << upgradeState(_upgradeState);
}
_minCompatibleVersion = -1;
_isMinCompatibleVersionSet = false;

return bob.obj();
}
_currentVersion = -1;
_isCurrentVersionSet = false;

string VersionType::toString() const {
return toBSON().toString();
}
_excludingMongoVersions = BSONArray();
_isExcludingMongoVersionsSet = false;

OID VersionType::getClusterId() const {
return _clusterId;
}
_clusterId = OID();
_isClusterIdSet = false;

void VersionType::setClusterId(const OID& clusterId) {
_clusterId = clusterId;
}
_upgradeId = OID();
_isUpgradeIdSet = false;

int VersionType::getMinCompatibleVersion() const {
return _minVersion;
}
_upgradeState = BSONObj();
_isUpgradeStateSet = false;

void VersionType::setMinCompatibleVersion(int version) {
_minVersion = version;
}

int VersionType::getCurrentVersion() const {
return _currentVersion;
}
void VersionType::cloneTo(VersionType* other) const {
other->clear();

void VersionType::setCurrentVersion(int version) {
_currentVersion = version;
}
other->_minCompatibleVersion = _minCompatibleVersion;
other->_isMinCompatibleVersionSet = _isMinCompatibleVersionSet;

OID VersionType::getUpgradeId() const {
return _upgradeId;
}
void VersionType::setUpgradeId(const OID& upgradeId) {
_upgradeId = upgradeId;
}
other->_currentVersion = _currentVersion;
other->_isCurrentVersionSet = _isCurrentVersionSet;

BSONObj VersionType::getUpgradeState() const {
return _upgradeState;
}
void VersionType::setUpgradeState(const BSONObj& upgradeState) {
_upgradeState = upgradeState;
}
other->_excludingMongoVersions = _excludingMongoVersions;
other->_isExcludingMongoVersionsSet = _isExcludingMongoVersionsSet;

void VersionType::cloneTo(VersionType* other) const {
other->_clusterId = _clusterId;
other->_isClusterIdSet = _isClusterIdSet;

other->clear();
other->_upgradeId = _upgradeId;
other->_isUpgradeIdSet = _isUpgradeIdSet;

other->_minVersion = _minVersion;
other->_currentVersion = _currentVersion;
other->_clusterId = _clusterId;
other->_excludes = _excludes;
other->_upgradeState = _upgradeState;
other->_isUpgradeStateSet = _isUpgradeStateSet;

}

std::string VersionType::toString() const {
return toBSON().toString();
}

} // namespace mongo
Loading

0 comments on commit 4d4f0c4

Please sign in to comment.