Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MB-16181: Add collections.set_manifest support
Add a method which will accept the new manifest and apply it to all active vbuckets. The latest manifest is saved in memory and also used for when any VB is set to active Change-Id: Ic6a339bc5af279d105b679f528ff3675d1f16ac7 Reviewed-on: http://review.couchbase.org/77436 Reviewed-by: Dave Rigby <daver@couchbase.com> Tested-by: Build Bot <build@couchbase.com>
- Loading branch information
1 parent
f137afd
commit 126ed5a
Showing
8 changed files
with
251 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ | ||
/* | ||
* Copyright 2017 Couchbase, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "collections/manager.h" | ||
#include "collections/manifest.h" | ||
#include "kv_bucket.h" | ||
#include "vbucket.h" | ||
|
||
Collections::Manager::Manager() : current(std::make_unique<Manifest>()) { | ||
} | ||
|
||
cb::engine_error Collections::Manager::update(KVBucket& bucket, | ||
const std::string& json) { | ||
std::unique_lock<std::mutex> ul(lock, std::try_to_lock); | ||
if (!ul.owns_lock()) { | ||
// Make concurrent updates fail, in realiy there should only be one | ||
// admin connection making changes. | ||
return cb::engine_error(cb::engine_errc::temporary_failure, | ||
"Collections::Manager::update already locked"); | ||
} | ||
|
||
std::unique_ptr<Manifest> newManifest; | ||
// Construct a newManifest (will throw if JSON was illegal) | ||
try { | ||
newManifest = std::make_unique<Manifest>(json); | ||
} catch (std::exception& e) { | ||
LOG(EXTENSION_LOG_NOTICE, | ||
"Collections::Manager::update can't construct manifest e.what:%s", | ||
e.what()); | ||
return cb::engine_error( | ||
cb::engine_errc::invalid_arguments, | ||
"Collections::Manager::update manifest json invalid:" + json); | ||
} | ||
|
||
// Validate manifest revision is increasing | ||
if (newManifest->getRevision() <= current->getRevision()) { | ||
return cb::engine_error( | ||
cb::engine_errc::invalid_arguments, | ||
"Collections::Manager::update manifest revision:" + | ||
std::to_string(current->getRevision()) + " json:" + | ||
json); | ||
} | ||
|
||
current = std::move(newManifest); | ||
|
||
for (int i = 0; i < bucket.getVBuckets().getSize(); i++) { | ||
auto vb = bucket.getVBuckets().getBucket(i); | ||
|
||
if (vb && vb->getState() == vbucket_state_active) { | ||
vb->updateFromManifest(*current); | ||
} | ||
} | ||
|
||
return cb::engine_error(cb::engine_errc::success, | ||
"Collections::Manager::update"); | ||
} | ||
|
||
void Collections::Manager::update(VBucket& vb) const { | ||
// Lock manager updates | ||
std::lock_guard<std::mutex> ul(lock); | ||
vb.updateFromManifest(*current); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ | ||
/* | ||
* Copyright 2017 Couchbase, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <memcached/engine_error.h> | ||
|
||
#include <memory> | ||
#include <mutex> | ||
|
||
class KVBucket; | ||
class VBucket; | ||
|
||
namespace Collections { | ||
|
||
class Manifest; | ||
|
||
/** | ||
* Collections::Manager provides some bucket level management functions | ||
* such as the code which enables the MCBP set_collections command. | ||
*/ | ||
class Manager { | ||
public: | ||
Manager(); | ||
|
||
/** | ||
* Update the bucket with the latest JSON collections manifest. | ||
* | ||
* Locks the Manager and prevents concurrent updates, concurrent updates | ||
* are failed with TMPFAIL as in reality there should be 1 admin connection. | ||
* | ||
* @param bucket the bucket receiving a set-collections command. | ||
* @param json the json manifest form a set-collections command. | ||
* @returns engine_error indicating why the update failed. | ||
*/ | ||
cb::engine_error update(KVBucket& bucket, const std::string& json); | ||
|
||
/** | ||
* Update the vbucket's manifest with the current Manifest | ||
* The Manager is locked to prevent current changing whilst this update | ||
* occurs. | ||
*/ | ||
void update(VBucket& vb) const; | ||
|
||
private: | ||
mutable std::mutex lock; | ||
|
||
/// Store the most recent (current) manifest received | ||
std::unique_ptr<Manifest> current; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters