1
0
Fork 0

Cluster Methods now use the OperationOptions of transactions. And now reacts to these options accordingly incl returnNew/returnOld

This commit is contained in:
Michael Hackstein 2016-03-31 10:52:24 +02:00
parent 118cc6c141
commit 618c855c85
4 changed files with 74 additions and 161 deletions

View File

@ -514,16 +514,16 @@ int countOnCoordinator(std::string const& dbname, std::string const& collname,
////////////////////////////////////////////////////////////////////////////////
int createDocumentOnCoordinator(
std::string const& dbname, std::string const& collname, bool waitForSync,
VPackSlice const& slice, std::map<std::string, std::string> const& headers,
std::string const& dbname, std::string const& collname,
arangodb::OperationOptions const& options, VPackSlice const& slice,
std::map<std::string, std::string> const& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
std::map<std::string, std::string>& resultHeaders,
std::string& resultBody) {
std::unique_ptr<TRI_json_t> json(
arangodb::basics::VelocyPackHelper::velocyPackToJson(slice));
return createDocumentOnCoordinator(dbname, collname, waitForSync, json,
headers, responseCode, resultHeaders,
resultBody);
return createDocumentOnCoordinator(dbname, collname, options, json, headers,
responseCode, resultHeaders, resultBody);
}
////////////////////////////////////////////////////////////////////////////////
@ -531,7 +531,7 @@ int createDocumentOnCoordinator(
////////////////////////////////////////////////////////////////////////////////
int createDocumentOnCoordinator(
std::string const& dbname, std::string const& collname, bool waitForSync,
std::string const& dbname, std::string const& collname, arangodb::OperationOptions const& options,
std::unique_ptr<TRI_json_t>& json,
std::map<std::string, std::string> const& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
@ -600,9 +600,11 @@ int createDocumentOnCoordinator(
arangodb::rest::HttpRequest::HTTP_REQUEST_POST,
"/_db/" + StringUtils::urlEncode(dbname) + "/_api/document?collection=" +
StringUtils::urlEncode(shardID) + "&waitForSync=" +
(waitForSync ? "true" : "false"),
(options.waitForSync ? "true" : "false") + "&returnNew=" +
(options.returnNew ? "true" : "false") + "&returnOld=" +
(options.returnOld ? "true" : "false"),
body, headers, 60.0);
int commError = handleGeneralCommErrors(res.get());
if (commError != TRI_ERROR_NO_ERROR) {
return commError;
@ -621,7 +623,8 @@ int createDocumentOnCoordinator(
int deleteDocumentOnCoordinator(
std::string const& dbname, std::string const& collname,
std::string const& key, TRI_voc_rid_t const rev, bool waitForSync,
std::string const& key, TRI_voc_rid_t const rev,
arangodb::OperationOptions const& options,
std::unique_ptr<std::map<std::string, std::string>>& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
std::map<std::string, std::string>& resultHeaders,
@ -673,9 +676,10 @@ int deleteDocumentOnCoordinator(
arangodb::rest::HttpRequest::HTTP_REQUEST_DELETE,
"/_db/" + dbname + "/_api/document/" + StringUtils::urlEncode(shardID) +
"/" + StringUtils::urlEncode(key) + "?waitForSync=" +
(waitForSync ? "true" : "false"),
(options.waitForSync ? "true" : "false") + "&returnOld=" +
(options.returnOld ? "true" : "false"),
"", *headers, 60.0);
int error = handleGeneralCommErrors(res.get());
if (error != TRI_ERROR_NO_ERROR) {
return error;
@ -700,7 +704,8 @@ int deleteDocumentOnCoordinator(
"/_db/" + StringUtils::urlEncode(dbname) +
"/_api/document/" + StringUtils::urlEncode(p.first) +
"/" + StringUtils::urlEncode(key) + "?waitForSync=" +
(waitForSync ? "true" : "false"),
(options.waitForSync ? "true" : "false") +
"&returnOld=" + (options.returnOld ? "true" : "false"),
std::shared_ptr<std::string const>(), headersCopy, nullptr,
60.0);
}
@ -1295,9 +1300,7 @@ int getFilteredEdgesOnCoordinator(
int modifyDocumentOnCoordinator(
std::string const& dbname, std::string const& collname,
std::string const& key, TRI_voc_rid_t const rev,
bool waitForSync, bool isPatch,
bool keepNull, // only counts for isPatch == true
bool mergeObjects, // only counts for isPatch == true
arangodb::OperationOptions const& options, bool isPatch,
VPackSlice const& slice,
std::unique_ptr<std::map<std::string, std::string>>& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
@ -1305,9 +1308,9 @@ int modifyDocumentOnCoordinator(
std::string& resultBody) {
std::unique_ptr<TRI_json_t> json(
arangodb::basics::VelocyPackHelper::velocyPackToJson(slice));
return modifyDocumentOnCoordinator(
dbname, collname, key, rev, waitForSync, isPatch, keepNull,
mergeObjects, json, headers, responseCode, resultHeaders, resultBody);
return modifyDocumentOnCoordinator(dbname, collname, key, rev, options,
isPatch, json, headers, responseCode,
resultHeaders, resultBody);
}
////////////////////////////////////////////////////////////////////////////////
@ -1317,9 +1320,7 @@ int modifyDocumentOnCoordinator(
int modifyDocumentOnCoordinator(
std::string const& dbname, std::string const& collname,
std::string const& key, TRI_voc_rid_t const rev,
bool waitForSync, bool isPatch,
bool keepNull, // only counts for isPatch == true
bool mergeObjects, // only counts for isPatch == true
arangodb::OperationOptions const& options, bool isPatch,
std::unique_ptr<TRI_json_t>& json,
std::unique_ptr<std::map<std::string, std::string>>& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
@ -1371,16 +1372,16 @@ int modifyDocumentOnCoordinator(
// Some stuff to prepare cluster-internal requests:
std::string revstr;
if (rev != 0) {
if (!options.ignoreRevs && rev != 0) {
headers->emplace("if-match", StringUtils::itoa(rev));
}
arangodb::rest::HttpRequest::HttpRequestType reqType;
if (isPatch) {
reqType = arangodb::rest::HttpRequest::HTTP_REQUEST_PATCH;
if (!keepNull) {
if (!options.keepNull) {
revstr += "&keepNull=false";
}
if (mergeObjects) {
if (options.mergeObjects) {
revstr += "&mergeObjects=true";
} else {
revstr += "&mergeObjects=false";
@ -1388,6 +1389,13 @@ int modifyDocumentOnCoordinator(
} else {
reqType = arangodb::rest::HttpRequest::HTTP_REQUEST_PUT;
}
if (options.returnNew) {
revstr += "&returnNew=true";
}
if (options.returnOld) {
revstr += "&returnOld=true";
}
auto body = std::make_shared<std::string const>(
std::string(JsonHelper::toString(json.get())));
@ -1404,7 +1412,7 @@ int modifyDocumentOnCoordinator(
"/_db/" + StringUtils::urlEncode(dbname) + "/_api/document/" +
StringUtils::urlEncode(shardID) + "/" +
StringUtils::urlEncode(key) + "?waitForSync=" +
(waitForSync ? "true" : "false") + revstr,
(options.waitForSync ? "true" : "false") + revstr,
*(body.get()), *headers, 60.0);
int error = handleGeneralCommErrors(res.get());
@ -1435,7 +1443,7 @@ int modifyDocumentOnCoordinator(
"/_db/" + StringUtils::urlEncode(dbname) +
"/_api/document/" + StringUtils::urlEncode(p.first) +
"/" + StringUtils::urlEncode(key) + "?waitForSync=" +
(waitForSync ? "true" : "false") + revstr,
(options.waitForSync ? "true" : "false") + revstr,
body, headersCopy, nullptr, 60.0);
}
// Now listen to the results:
@ -1462,89 +1470,6 @@ int modifyDocumentOnCoordinator(
// the DBserver could have reported an error.
}
////////////////////////////////////////////////////////////////////////////////
/// @brief creates an edge in a coordinator
////////////////////////////////////////////////////////////////////////////////
int createEdgeOnCoordinator(
std::string const& dbname, std::string const& collname, bool waitForSync,
std::unique_ptr<TRI_json_t>& json, char const* from, char const* to,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
std::map<std::string, std::string>& resultHeaders,
std::string& resultBody) {
// Set a few variables needed for our work:
ClusterInfo* ci = ClusterInfo::instance();
ClusterComm* cc = ClusterComm::instance();
// First determine the collection ID from the name:
std::shared_ptr<CollectionInfo> collinfo =
ci->getCollection(dbname, collname);
if (collinfo->empty()) {
return TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND;
}
std::string collid = StringUtils::itoa(collinfo->id());
// Sort out the _key attribute:
// The user is allowed to specify _key, provided that _key is the one
// and only sharding attribute, because in this case we can delegate
// the responsibility to make _key attributes unique to the responsible
// shard. Otherwise, we ensure uniqueness here and now by taking a
// cluster-wide unique number. Note that we only know the sharding
// attributes a bit further down the line when we have determined
// the responsible shard.
TRI_json_t* subjson = TRI_LookupObjectJson(json.get(), "_key");
bool userSpecifiedKey = false;
std::string _key;
if (subjson == nullptr) {
// The user did not specify a key, let's create one:
uint64_t uid = ci->uniqid();
_key = arangodb::basics::StringUtils::itoa(uid);
TRI_Insert3ObjectJson(TRI_UNKNOWN_MEM_ZONE, json.get(), "_key",
TRI_CreateStringReferenceJson(
TRI_UNKNOWN_MEM_ZONE, _key.c_str(), _key.size()));
} else {
userSpecifiedKey = true;
}
// Now find the responsible shard:
bool usesDefaultShardingAttributes;
ShardID shardID;
int error = ci->getResponsibleShard(collid, json.get(), true, shardID,
usesDefaultShardingAttributes);
if (error == TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND) {
return TRI_ERROR_CLUSTER_SHARD_GONE;
}
// Now perform the above mentioned check:
if (userSpecifiedKey && !usesDefaultShardingAttributes) {
return TRI_ERROR_CLUSTER_MUST_NOT_SPECIFY_KEY;
}
std::string body = JsonHelper::toString(json.get());
// Send a synchronous request to that shard using ClusterComm:
std::map<std::string, std::string> headers;
auto res = cc->syncRequest(
"", TRI_NewTickServer(), "shard:" + shardID,
arangodb::rest::HttpRequest::HTTP_REQUEST_POST,
"/_db/" + dbname + "/_api/edge?collection=" +
StringUtils::urlEncode(shardID) + "&waitForSync=" +
(waitForSync ? "true" : "false") + "&from=" +
StringUtils::urlEncode(from) + "&to=" + StringUtils::urlEncode(to),
body, headers, 60.0);
int commError = handleGeneralCommErrors(res.get());
if (commError != TRI_ERROR_NO_ERROR) {
return commError;
}
responseCode = static_cast<arangodb::rest::HttpResponse::HttpResponseCode>(
res->result->getHttpReturnCode());
resultHeaders = res->result->getHeaderFields();
resultBody.assign(res->result->getBody().c_str(),
res->result->getBody().length());
return TRI_ERROR_NO_ERROR;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief flush Wal on all DBservers
////////////////////////////////////////////////////////////////////////////////

View File

@ -45,6 +45,8 @@ namespace traverser {
class TraverserExpression;
}
struct OperationOptions;
////////////////////////////////////////////////////////////////////////////////
/// @brief merge headers of a DB server response into the current response
////////////////////////////////////////////////////////////////////////////////
@ -101,8 +103,8 @@ int countOnCoordinator(std::string const& dbname, std::string const& collname,
////////////////////////////////////////////////////////////////////////////////
int createDocumentOnCoordinator(
std::string const& dbname, std::string const& collname, bool waitForSync,
arangodb::velocypack::Slice const& slice,
std::string const& dbname, std::string const& collname,
OperationOptions const& options, arangodb::velocypack::Slice const& slice,
std::map<std::string, std::string> const& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
std::map<std::string, std::string>& resultHeaders, std::string& resultBody);
@ -112,8 +114,8 @@ int createDocumentOnCoordinator(
////////////////////////////////////////////////////////////////////////////////
int createDocumentOnCoordinator(
std::string const& dbname, std::string const& collname, bool waitForSync,
std::unique_ptr<TRI_json_t>& json,
std::string const& dbname, std::string const& collname,
OperationOptions const& options, std::unique_ptr<TRI_json_t>& json,
std::map<std::string, std::string> const& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
std::map<std::string, std::string>& resultHeaders, std::string& resultBody);
@ -124,7 +126,8 @@ int createDocumentOnCoordinator(
int deleteDocumentOnCoordinator(
std::string const& dbname, std::string const& collname,
std::string const& key, TRI_voc_rid_t const rev, bool waitForSync,
std::string const& key, TRI_voc_rid_t const rev,
OperationOptions const& options,
std::unique_ptr<std::map<std::string, std::string>>& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
std::map<std::string, std::string>& resultHeaders, std::string& resultBody);
@ -194,9 +197,7 @@ int getFilteredEdgesOnCoordinator(
int modifyDocumentOnCoordinator(
std::string const& dbname, std::string const& collname,
std::string const& key, TRI_voc_rid_t const rev,
bool waitForSync, bool isPatch,
bool keepNull, // only counts for isPatch == true
bool mergeObjects, // only counts for isPatch == true
OperationOptions const& options, bool isPatch,
arangodb::velocypack::Slice const& slice,
std::unique_ptr<std::map<std::string, std::string>>& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
@ -209,24 +210,12 @@ int modifyDocumentOnCoordinator(
int modifyDocumentOnCoordinator(
std::string const& dbname, std::string const& collname,
std::string const& key, TRI_voc_rid_t const rev,
bool waitForSync, bool isPatch,
bool keepNull, // only counts for isPatch == true
bool mergeObjects, // only counts for isPatch == true
OperationOptions const& options, bool isPatch,
std::unique_ptr<TRI_json_t>& json,
std::unique_ptr<std::map<std::string, std::string>>& headers,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
std::map<std::string, std::string>& resultHeaders, std::string& resultBody);
////////////////////////////////////////////////////////////////////////////////
/// @brief creates an edge in a coordinator
////////////////////////////////////////////////////////////////////////////////
int createEdgeOnCoordinator(
std::string const& dbname, std::string const& collname, bool waitForSync,
std::unique_ptr<TRI_json_t>& json, char const* from, char const* to,
arangodb::rest::HttpResponse::HttpResponseCode& responseCode,
std::map<std::string, std::string>& resultHeaders, std::string& resultBody);
////////////////////////////////////////////////////////////////////////////////
/// @brief truncate a cluster collection on a coordinator
////////////////////////////////////////////////////////////////////////////////

View File

@ -1156,8 +1156,8 @@ OperationResult Transaction::insertCoordinator(std::string const& collectionName
std::string resultBody;
int res = arangodb::createDocumentOnCoordinator(
_vocbase->_name, collectionName, options.waitForSync,
value, headers, responseCode, resultHeaders, resultBody);
_vocbase->_name, collectionName, options, value, headers, responseCode,
resultHeaders, resultBody);
if (res == TRI_ERROR_NO_ERROR) {
if (responseCode == arangodb::rest::HttpResponse::ACCEPTED ||
@ -1304,10 +1304,9 @@ OperationResult Transaction::updateCoordinator(std::string const& collectionName
= options.ignoreRevs ? 0 : TRI_ExtractRevisionId(newValue);
int res = arangodb::modifyDocumentOnCoordinator(
_vocbase->_name, collectionName, key, expectedRevision,
options.waitForSync, true /* isPatch */,
options.keepNull, options.mergeObjects, newValue,
headers, responseCode, resultHeaders, resultBody);
_vocbase->_name, collectionName, key, expectedRevision, options,
true /* isPatch */, newValue, headers, responseCode, resultHeaders,
resultBody);
if (res == TRI_ERROR_NO_ERROR) {
if (responseCode == arangodb::rest::HttpResponse::ACCEPTED ||
@ -1391,10 +1390,9 @@ OperationResult Transaction::replaceCoordinator(std::string const& collectionNam
= options.ignoreRevs ? 0 : TRI_ExtractRevisionId(newValue);
int res = arangodb::modifyDocumentOnCoordinator(
_vocbase->_name, collectionName, key, expectedRevision,
options.waitForSync, false /* isPatch */,
false /* keepNull */, false /* mergeObjects */, newValue,
headers, responseCode, resultHeaders, resultBody);
_vocbase->_name, collectionName, key, expectedRevision, options,
false /* isPatch */, newValue, headers, responseCode, resultHeaders,
resultBody);
if (res == TRI_ERROR_NO_ERROR) {
if (responseCode == arangodb::rest::HttpResponse::ACCEPTED ||
@ -1567,9 +1565,8 @@ OperationResult Transaction::removeCoordinator(std::string const& collectionName
}
int res = arangodb::deleteDocumentOnCoordinator(
_vocbase->_name, collectionName, key, expectedRevision,
options.waitForSync,
headers, responseCode, resultHeaders, resultBody);
_vocbase->_name, collectionName, key, expectedRevision, options, headers,
responseCode, resultHeaders, resultBody);
if (res == TRI_ERROR_NO_ERROR) {
if (responseCode == arangodb::rest::HttpResponse::OK ||

View File

@ -2343,24 +2343,26 @@ function DatabaseDocumentSuiteReturnStuff () {
////////////////////////////////////////////////////////////////////////////////
testNewFeatures : function () {
var x = collection.insert({Hallo: 12}, { silent: true });
assertEqual(true, x);
x = collection.insert([{Hallo: 13}], { silent: true });
assertEqual(true, x);
x = collection.insert({Hallo:14});
var y = collection.replace(x._key, {Hallo:15}, { silent: true });
assertEqual(true, y);
y = db._replace(x._id, {Hallo: 16}, {silent: true});
assertEqual(true, y);
y = collection.update(x._key, {Hallo:17}, { silent: true });
assertEqual(true, y);
y = db._update(x._id, {Hallo:18}, { silent: true });
assertEqual(true, y);
y = collection.remove(x._key, { silent: true });
assertEqual(true, y);
x = collection.insert({Hallo:19});
y = db._remove(x._id, {silent: true});
assertEqual(true, y);
if (! require("@arangodb/cluster").isCluster()) {
var x = collection.insert({Hallo: 12}, { silent: true });
assertEqual(true, x);
x = collection.insert([{Hallo: 13}], { silent: true });
assertEqual(true, x);
x = collection.insert({Hallo:14});
var y = collection.replace(x._key, {Hallo:15}, { silent: true });
assertEqual(true, y);
y = db._replace(x._id, {Hallo: 16}, {silent: true});
assertEqual(true, y);
y = collection.update(x._key, {Hallo:17}, { silent: true });
assertEqual(true, y);
y = db._update(x._id, {Hallo:18}, { silent: true });
assertEqual(true, y);
y = collection.remove(x._key, { silent: true });
assertEqual(true, y);
x = collection.insert({Hallo:19});
y = db._remove(x._id, {silent: true});
assertEqual(true, y);
}
}
};
}