Browse Source

[C++] [userver] bump userver commit and tweak PG pools once more (#8163)

* bump userver commit and tweak PG pools once more

* try to further optimize PG
itrofimow 2 years ago
parent
commit
1d7fe5dacf

+ 1 - 1
frameworks/C++/userver/userver-bare.dockerfile

@@ -3,7 +3,7 @@ RUN apt install -y libnghttp2-dev
 
 WORKDIR /src
 RUN git clone https://github.com/userver-framework/userver.git && \
-    cd userver && git checkout 7932d3f014926fc1502057a6833064e0d745b66c
+    cd userver && git checkout 040b0bd001a078ee023aabe987e5ec7cc8ed5b6c
 COPY userver_benchmark/ ./
 RUN mkdir build && cd build && \
     cmake -DUSERVER_IS_THE_ROOT_PROJECT=0 -DUSERVER_FEATURE_CRYPTOPP_BLAKE2=0 \

+ 1 - 1
frameworks/C++/userver/userver.dockerfile

@@ -3,7 +3,7 @@ RUN apt install -y libnghttp2-dev
 
 WORKDIR /src
 RUN git clone https://github.com/userver-framework/userver.git && \
-    cd userver && git checkout 7932d3f014926fc1502057a6833064e0d745b66c
+    cd userver && git checkout 040b0bd001a078ee023aabe987e5ec7cc8ed5b6c
 COPY userver_benchmark/ ./
 RUN mkdir build && cd build && \
     cmake -DUSERVER_IS_THE_ROOT_PROJECT=0 -DUSERVER_FEATURE_CRYPTOPP_BLAKE2=0 \

+ 21 - 10
frameworks/C++/userver/userver_benchmark/controllers/multiple_queries/handler.cpp

@@ -30,18 +30,29 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow(
 }
 
 userver::formats::json::Value Handler::GetResponse(int queries) const {
-  boost::container::small_vector<int, 500> random_ids(queries);
-  std::generate(random_ids.begin(), random_ids.end(),
-                db_helpers::GenerateRandomId);
-
-  boost::container::small_vector<db_helpers::WorldTableRow, 500> result{};
-  for (auto id : random_ids) {
-    result.push_back(pg_->Execute(db_helpers::kClusterHostType,
-                                  db_helpers::kSelectRowQuery, id)
-                         .AsSingleRow<db_helpers::WorldTableRow>(
-                             userver::storages::postgres::kRowTag));
+  boost::container::small_vector<db_helpers::WorldTableRow, 500> result(
+      queries);
+  for (auto& value : result) {
+    value.id = db_helpers::GenerateRandomId();
   }
 
+  // even though this adds a round-trip for Begin/Commit we expect this to be
+  // faster due to the pool semaphore contention reduction - now we have a
+  // connection for ourselves until we are done with it, otherwise we would
+  // likely wait on the semaphore with every new query.
+  auto transaction = pg_->Begin(
+      db_helpers::kClusterHostType,
+      userver::storages::postgres::TransactionOptions{
+          userver::storages::postgres::TransactionOptions::Mode::kReadOnly});
+  for (auto& value : result) {
+    value.random_number =
+        transaction.Execute(db_helpers::kSelectRowQuery, value.id)
+            .AsSingleRow<db_helpers::WorldTableRow>(
+                userver::storages::postgres::kRowTag)
+            .random_number;
+  }
+  transaction.Commit();
+
   return userver::formats::json::ValueBuilder{result}.ExtractValue();
 }
 

+ 28 - 17
frameworks/C++/userver/userver_benchmark/controllers/updates/handler.cpp

@@ -43,27 +43,38 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow(
 }
 
 userver::formats::json::Value Handler::GetResponse(int queries) const {
-  std::vector<int> random_ids(queries);
-  std::generate(random_ids.begin(), random_ids.end(),
-                db_helpers::GenerateRandomId);
-  std::sort(random_ids.begin(), random_ids.end());
-
-  boost::container::small_vector<db_helpers::WorldTableRow, 500> result{};
-  for (auto id : random_ids) {
-    result.push_back(pg_->Execute(db_helpers::kClusterHostType,
-                                  db_helpers::kSelectRowQuery, id)
-                         .AsSingleRow<db_helpers::WorldTableRow>(
-                             userver::storages::postgres::kRowTag));
+  // userver's PG doesn't accept boost::small_vector as an input, sadly
+  std::vector<db_helpers::WorldTableRow> values(queries);
+  for (auto& value : values) {
+    value.id = db_helpers::GenerateRandomId();
+  }
+  // we have to sort ids to not deadlock in update
+  std::sort(values.begin(), values.end(),
+            [](const auto& lhs, const auto& rhs) { return lhs.id < rhs.id; });
+
+  // even though this adds a round-trip for Begin/Commit we expect this to be
+  // faster due to the pool semaphore contention reduction - now we have a
+  // connection for ourselves until we are done with it, otherwise we would
+  // likely wait on the semaphore with every new query.
+  auto transaction = pg_->Begin(db_helpers::kClusterHostType, {});
+  for (auto& value : values) {
+    value.random_number =
+        transaction.Execute(db_helpers::kSelectRowQuery, value.id)
+            .AsSingleRow<db_helpers::WorldTableRow>(
+                userver::storages::postgres::kRowTag)
+            .random_number;
   }
 
-  std::vector<int> random_numbers(queries);
-  std::generate(random_numbers.begin(), random_numbers.end(),
-                db_helpers::GenerateRandomValue);
+  auto json_result =
+      userver::formats::json::ValueBuilder{values}.ExtractValue();
 
-  pg_->Execute(db_helpers::kClusterHostType, update_query_, random_ids,
-               random_numbers);
+  for (auto& value : values) {
+    value.random_number = db_helpers::GenerateRandomValue();
+  }
+  transaction.ExecuteDecomposeBulk(update_query_, values, values.size());
+  transaction.Commit();
 
-  return userver::formats::json::ValueBuilder{result}.ExtractValue();
+  return json_result;
 }
 
 }  // namespace userver_techempower::updates

+ 5 - 0
frameworks/C++/userver/userver_configs/dynamic_config_fallback.json

@@ -23,6 +23,10 @@
     "cancel-request": false,
     "update-timeout": false
   },
+  "BAGGAGE_SETTINGS": {
+    "allowed_keys": []
+  },
+  "USERVER_BAGGAGE_ENABLED": false,
   "USERVER_TASK_PROCESSOR_QOS": {
     "default-service": {
       "default-task-processor": {
@@ -36,6 +40,7 @@
   },
 
   "POSTGRES_STATEMENT_METRICS_SETTINGS": {},
+  "POSTGRES_CONNLIMIT_MODE_AUTO_ENABLED": false,
   "POSTGRES_CONNECTION_PIPELINE_ENABLED": false,
   "POSTGRES_CONNECTION_POOL_SETTINGS": {},
   "POSTGRES_CONNECTION_SETTINGS": {},

+ 1 - 1
frameworks/C++/userver/userver_configs/static_config.yaml

@@ -76,7 +76,7 @@ components_manager:
             dbalias: hello_world
             blocking_task_processor: fs-task-processor
             min_pool_size: 75
-            max_pool_size: 520
+            max_pool_size: 125
             max_queue_size: 512
             connecting_limit: 15