Commit ffd08a2f authored by Vaastav Anand's avatar Vaastav Anand

Add instrumentation for ReviewStorageService

parent c9463758
......@@ -13,6 +13,8 @@
#include "../../gen-cpp/ReviewStorageService.h"
#include "../logger.h"
#include "../tracing.h"
#include <xtrace/xtrace.h>
#include <xtrace/baggage.h>
namespace media_service {
......@@ -42,6 +44,15 @@ void ReviewStorageHandler::StoreReview(
const Review &review,
const std::map<std::string, std::string> & carrier) {
std::map<std::string, std::string>::const_iterator baggage_it = carrier.find("baggage");
if (baggage_it != carrier.end()) {
SET_CURRENT_BAGGAGE(Baggage::deserialize(baggage_it->second));
}
if (!XTrace::IsTracing()) {
XTrace::StartTrace("ReviewStorageHandler");
}
XTRACE("ReviewStorageHandler::StoreReview", {{"RequestID", std::to_string(req_id)}});
// Initialize a span
TextMapReader reader(carrier);
std::map<std::string, std::string> writer_text_map;
......@@ -58,6 +69,7 @@ void ReviewStorageHandler::StoreReview(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to pop a client from MongoDB pool";
XTRACE("Failed to pop a client from MongoDB pool");
throw se;
}
......@@ -67,6 +79,7 @@ void ReviewStorageHandler::StoreReview(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to create collection user from DB user";
XTRACE("Failed to create collection user from DB user");
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
throw se;
}
......@@ -81,15 +94,18 @@ void ReviewStorageHandler::StoreReview(
BSON_APPEND_INT64(new_doc, "req_id", review.req_id);
bson_error_t error;
XTRACE("MongoInsertReview start");
auto insert_span = opentracing::Tracer::Global()->StartSpan(
"MongoInsertReview", { opentracing::ChildOf(&span->context()) });
bool plotinsert = mongoc_collection_insert_one (
collection, new_doc, nullptr, nullptr, &error);
insert_span->Finish();
XTRACE("MongoInsertReview finish");
if (!plotinsert) {
LOG(error) << "Error: Failed to insert review to MongoDB: "
<< error.message;
XTRACE("Error: Failed to insert review to MongoDB: " + std::string(error.message));
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = error.message;
......@@ -104,6 +120,8 @@ void ReviewStorageHandler::StoreReview(
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
span->Finish();
XTRACE("ReviewStorageHandler::StoreReview complete");
DELETE_CURRENT_BAGGAGE();
}
void ReviewStorageHandler::ReadReviews(
std::vector<Review> & _return,
......@@ -111,6 +129,15 @@ void ReviewStorageHandler::ReadReviews(
const std::vector<int64_t> &review_ids,
const std::map<std::string, std::string> &carrier) {
std::map<std::string, std::string>::const_iterator baggage_it = carrier.find("baggage");
if (baggage_it != carrier.end()) {
SET_CURRENT_BAGGAGE(Baggage::deserialize(baggage_it->second));
}
if (!XTrace::IsTracing()) {
XTrace::StartTrace("ReviewStorageHandler");
}
XTRACE("ReviewStorageHandler::ReadReviews", {{"RequestID", std::to_string(req_id)}});
// Initialize a span
TextMapReader reader(carrier);
std::map<std::string, std::string> writer_text_map;
......@@ -130,6 +157,7 @@ void ReviewStorageHandler::ReadReviews(
ServiceException se;
se.errorCode = ErrorCode::SE_THRIFT_HANDLER_ERROR;
se.message = "Post_ids are duplicated";
XTRACE("Post_ids are duplicated");
throw se;
}
std::map<int64_t, Review> return_map;
......@@ -140,6 +168,7 @@ void ReviewStorageHandler::ReadReviews(
ServiceException se;
se.errorCode = ErrorCode::SE_MEMCACHED_ERROR;
se.message = "Failed to pop a client from memcached pool";
XTRACE("Failed to pop a client from memcached pool");
throw se;
}
......@@ -160,6 +189,7 @@ void ReviewStorageHandler::ReadReviews(
if (memcached_rc != MEMCACHED_SUCCESS) {
LOG(error) << "Cannot get review-ids of request " << req_id << ": "
<< memcached_strerror(memcached_client, memcached_rc);
XTRACE("Cannot get review-ids of requqest " + std::to_string(req_id) + ": " + std::string(memcached_strerror(memcached_client, memcached_rc)));
ServiceException se;
se.errorCode = ErrorCode::SE_MEMCACHED_ERROR;
se.message = memcached_strerror(memcached_client, memcached_rc);
......@@ -172,6 +202,7 @@ void ReviewStorageHandler::ReadReviews(
char *return_value;
size_t return_value_length;
uint32_t flags;
XTRACE("MemcachedMget start");
auto get_span = opentracing::Tracer::Global()->StartSpan(
"MemcachedMget", { opentracing::ChildOf(&span->context()) });
......@@ -181,6 +212,7 @@ void ReviewStorageHandler::ReadReviews(
&return_value_length, &flags, &memcached_rc);
if (return_value == nullptr) {
LOG(debug) << "Memcached mget finished";
XTRACE("Memcached mget finished");
break;
}
if (memcached_rc != MEMCACHED_SUCCESS) {
......@@ -191,6 +223,7 @@ void ReviewStorageHandler::ReadReviews(
ServiceException se;
se.errorCode = ErrorCode::SE_MEMCACHED_ERROR;
se.message = "Cannot get reviews of request " + std::to_string(req_id);
XTRACE("Cannot get reviews of request " + std::to_string(req_id));
throw se;
}
Review new_review;
......@@ -207,8 +240,10 @@ void ReviewStorageHandler::ReadReviews(
review_ids_not_cached.erase(new_review.review_id);
free(return_value);
LOG(debug) << "Review: " << new_review.review_id << " found in memcached";
XTRACE("Review: " + std::to_string(new_review.review_id) + " found in memcached");
}
get_span->Finish();
XTRACE("MemcachedMget finish");
memcached_quit(memcached_client);
memcached_pool_push(_memcached_client_pool, memcached_client);
for (int i = 0; i < review_ids.size(); ++i) {
......@@ -218,6 +253,7 @@ void ReviewStorageHandler::ReadReviews(
delete[] key_sizes;
std::vector<std::future<void>> set_futures;
std::vector<Baggage> set_baggages;
std::map<int64_t, std::string> review_json_map;
// Find the rest in MongoDB
......@@ -228,6 +264,7 @@ void ReviewStorageHandler::ReadReviews(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to pop a client from MongoDB pool";
XTRACE("Failed to pop a client from MongoDB pool");
throw se;
}
auto collection = mongoc_client_get_collection(
......@@ -236,6 +273,7 @@ void ReviewStorageHandler::ReadReviews(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to create collection user from DB user";
XTRACE("Failed to create collection user from DB user");
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
throw se;
}
......@@ -257,6 +295,7 @@ void ReviewStorageHandler::ReadReviews(
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(
collection, query, nullptr, nullptr);
const bson_t *doc;
XTRACE("MongoFindPosts start");
auto find_span = opentracing::Tracer::Global()->StartSpan(
"MongoFindPosts", {opentracing::ChildOf(&span->context())});
while (true) {
......@@ -279,6 +318,7 @@ void ReviewStorageHandler::ReadReviews(
bson_free(review_json_char);
}
find_span->Finish();
XTRACE("MongoFindPosts complete");
bson_error_t error;
if (mongoc_cursor_error(cursor, &error)) {
LOG(warning) << error.message;
......@@ -289,6 +329,7 @@ void ReviewStorageHandler::ReadReviews(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = error.message;
XTRACE(std::string(error.message));
throw se;
}
bson_destroy(query);
......@@ -297,7 +338,10 @@ void ReviewStorageHandler::ReadReviews(
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
// upload reviews to memcached
Baggage set_baggage = BRANCH_CURRENT_BAGGAGE();
set_baggages.emplace_back(set_baggage);
set_futures.emplace_back(std::async(std::launch::async, [&]() {
BAGGAGE(set_baggage);
memcached_return_t _rc;
auto _memcached_client = memcached_pool_pop(
_memcached_client_pool, true, &_rc);
......@@ -306,8 +350,10 @@ void ReviewStorageHandler::ReadReviews(
ServiceException se;
se.errorCode = ErrorCode::SE_MEMCACHED_ERROR;
se.message = "Failed to pop a client from memcached pool";
XTRACE("Failed to pop a client from memcached pool");
throw se;
}
XTRACE("Memcached SetPost start");
auto set_span = opentracing::Tracer::Global()->StartSpan(
"MmcSetPost", {opentracing::ChildOf(&span->context())});
for (auto & it : review_json_map) {
......@@ -323,12 +369,16 @@ void ReviewStorageHandler::ReadReviews(
}
memcached_pool_push(_memcached_client_pool, _memcached_client);
set_span->Finish();
XTRACE("Memcached SetPost finish");
}));
}
if (return_map.size() != review_ids.size()) {
try {
for (auto &it : set_futures) { it.get(); }
for (std::vector<int>::size_type i=0; i < set_futures.size(); i++) {
set_futures[i].get();
JOIN_CURRENT_BAGGAGE(set_baggages[i]);
}
} catch (...) {
LOG(warning) << "Failed to set reviews to memcached";
}
......@@ -336,6 +386,7 @@ void ReviewStorageHandler::ReadReviews(
ServiceException se;
se.errorCode = ErrorCode::SE_THRIFT_HANDLER_ERROR;
se.message = "review storage service: return set incomplete";
XTRACE("Return set incomplete");
throw se;
}
......@@ -344,10 +395,17 @@ void ReviewStorageHandler::ReadReviews(
}
try {
for (auto &it : set_futures) { it.get(); }
for (std::vector<int>::size_type i=0; i < set_futures.size(); i++) {
set_futures[i].get();
JOIN_CURRENT_BAGGAGE(set_baggages[i]);
}
} catch (...) {
LOG(warning) << "Failed to set reviews to memcached";
XTRACE("Failed to set reviews to memcached");
}
XTRACE("ReviewStorageService::ReadReviews complete");
DELETE_CURRENT_BAGGAGE();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment