From d6234717d5ec4a6dd0dd7548fd2057ea85b2465e Mon Sep 17 00:00:00 2001 From: Alfredo Date: Fri, 20 Sep 2019 14:55:21 -0300 Subject: [PATCH] Fix for ES7 --- .../plugins/elasticsearch/elasticsearch_plugin.cpp | 10 +++++++++- libraries/plugins/es_objects/es_objects.cpp | 11 +++++++++-- libraries/utilities/elasticsearch.cpp | 14 ++++++++++++++ .../include/graphene/utilities/elasticsearch.hpp | 1 + 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp b/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp index 92a5f41f19..0ac5adf33d 100644 --- a/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp +++ b/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp @@ -74,6 +74,7 @@ class elasticsearch_plugin_impl std::string bulk_line; std::string index_name; bool is_sync = false; + bool esge7 = false; private: bool add_elasticsearch( const account_id_type account_id, const optional& oho, const uint32_t block_number ); const account_transaction_history_object& addNewEntry(const account_statistics_object& stats_obj, @@ -286,6 +287,7 @@ bool elasticsearch_plugin_impl::add_elasticsearch( const account_id_type account const auto &stats_obj = getStatsObject(account_id); const auto &ath = addNewEntry(stats_obj, account_id, oho); growStats(stats_obj, ath); + if(block_number > _elasticsearch_start_es_after_block) { createBulkLine(ath); prepareBulk(ath.id); @@ -354,7 +356,8 @@ void elasticsearch_plugin_impl::prepareBulk(const account_transaction_history_id const std::string _id = fc::json::to_string(ath_id); fc::mutable_variant_object bulk_header; bulk_header["_index"] = index_name; - bulk_header["_type"] = "data"; + if(!esge7) + bulk_header["_type"] = "data"; bulk_header["_id"] = fc::to_string(ath_id.space_id) + "." + fc::to_string(ath_id.type_id) + "." + fc::to_string(ath_id.instance.value); prepare = graphene::utilities::createBulk(bulk_header, std::move(bulk_line)); @@ -513,6 +516,11 @@ void elasticsearch_plugin::plugin_startup() if(!graphene::utilities::checkES(es)) FC_THROW_EXCEPTION(fc::exception, "ES database is not up in url ${url}", ("url", my->_elasticsearch_node_url)); + + const auto es_version = graphene::utilities::getVersion(es); + if(std::stoi(es_version.substr(0,1)) >= 7) + my->esge7 = true; + ilog("elasticsearch ACCOUNT HISTORY: plugin_startup() begin"); } diff --git a/libraries/plugins/es_objects/es_objects.cpp b/libraries/plugins/es_objects/es_objects.cpp index 79b9f1cdba..9c94962ed7 100644 --- a/libraries/plugins/es_objects/es_objects.cpp +++ b/libraries/plugins/es_objects/es_objects.cpp @@ -71,6 +71,7 @@ class es_objects_plugin_impl uint32_t block_number; fc::time_point_sec block_time; + bool esge7 = false; private: template @@ -224,7 +225,8 @@ void es_objects_plugin_impl::remove_from_database( object_id_type id, std::strin fc::mutable_variant_object delete_line; delete_line["_id"] = string(id); delete_line["_index"] = _es_objects_index_prefix + index; - delete_line["_type"] = "data"; + if(!esge7) + delete_line["_type"] = "data"; fc::mutable_variant_object final_delete_line; final_delete_line["delete"] = delete_line; prepare.push_back(fc::json::to_string(final_delete_line)); @@ -238,7 +240,8 @@ void es_objects_plugin_impl::prepareTemplate(T blockchain_object, string index_n { fc::mutable_variant_object bulk_header; bulk_header["_index"] = _es_objects_index_prefix + index_name; - bulk_header["_type"] = "data"; + if(!esge7) + bulk_header["_type"] = "data"; if(_es_objects_keep_only_current) { bulk_header["_id"] = string(blockchain_object.id); @@ -403,6 +406,10 @@ void es_objects_plugin::plugin_startup() if(!graphene::utilities::checkES(es)) FC_THROW_EXCEPTION(fc::exception, "ES database is not up in url ${url}", ("url", my->_es_objects_elasticsearch_url)); ilog("elasticsearch OBJECTS: plugin_startup() begin"); + + const auto es_version = graphene::utilities::getVersion(es); + if(std::stoi(es_version.substr(0,1)) >= 7) + my->esge7 = true; } } } \ No newline at end of file diff --git a/libraries/utilities/elasticsearch.cpp b/libraries/utilities/elasticsearch.cpp index 0b94de50c9..ed51be1c60 100644 --- a/libraries/utilities/elasticsearch.cpp +++ b/libraries/utilities/elasticsearch.cpp @@ -49,6 +49,20 @@ bool checkES(ES& es) return true; } + +const std::string getVersion(ES& es) +{ + graphene::utilities::CurlRequest curl_request; + curl_request.handler = es.curl; + curl_request.url = es.elasticsearch_url; + curl_request.auth = es.auth; + curl_request.type = "GET"; + + fc::variant response = fc::json::from_string(doCurl(curl_request)); + + return response["version"]["number"].as_string(); +} + const std::string simpleQuery(ES& es) { graphene::utilities::CurlRequest curl_request; diff --git a/libraries/utilities/include/graphene/utilities/elasticsearch.hpp b/libraries/utilities/include/graphene/utilities/elasticsearch.hpp index e8790c1ea5..4ff9ffe460 100644 --- a/libraries/utilities/include/graphene/utilities/elasticsearch.hpp +++ b/libraries/utilities/include/graphene/utilities/elasticsearch.hpp @@ -56,6 +56,7 @@ namespace graphene { namespace utilities { bool SendBulk(ES&& es); const std::vector createBulk(const fc::mutable_variant_object& bulk_header, std::string&& data); bool checkES(ES& es); + const std::string getVersion(ES& es); const std::string simpleQuery(ES& es); bool deleteAll(ES& es); bool handleBulkResponse(long http_code, const std::string& CurlReadBuffer);