Browse Source

[C++] [userver] bump userver commit, tweak PG once more (#8345)

* [C++] [userver] bump userver commit, tweak PG once more

* bump userver commit, add a dedicated timers thread, increase amount of ev threads

* fix dynamic_config_fallback

* increase cache update period
itrofimow 2 years ago
parent
commit
4454fa6d8d

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

@@ -5,7 +5,7 @@ RUN apt install -y lsb-release wget software-properties-common gnupg && \
 
 WORKDIR /src
 RUN git clone https://github.com/userver-framework/userver.git && \
-    cd userver && git checkout ce195952bf3050a1f410c274b1c4bc13887957b0
+    cd userver && git checkout 9ca143ebe99c70be532ed4dde386a165a833181d
 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

@@ -5,7 +5,7 @@ RUN apt install -y lsb-release wget software-properties-common gnupg && \
 
 WORKDIR /src
 RUN git clone https://github.com/userver-framework/userver.git && \
-    cd userver && git checkout ce195952bf3050a1f410c274b1c4bc13887957b0
+    cd userver && git checkout 9ca143ebe99c70be532ed4dde386a165a833181d
 COPY userver_benchmark/ ./
 RUN mkdir build && cd build && \
     cmake -DUSERVER_IS_THE_ROOT_PROJECT=0 -DUSERVER_FEATURE_CRYPTOPP_BLAKE2=0 \

+ 5 - 4
frameworks/C++/userver/userver_benchmark/controllers/multiple_queries/handler.cpp

@@ -35,21 +35,22 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow(
 }
 
 userver::formats::json::Value Handler::GetResponse(int queries) const {
-  boost::container::small_vector<db_helpers::WorldTableRow, 500> result(
-      queries);
+  boost::container::small_vector<db_helpers::WorldTableRow, 20> result(queries);
   for (auto& value : result) {
     value.id = db_helpers::GenerateRandomId();
   }
 
   {
     const auto lock = semaphore_.Acquire();
+
+    auto trx = pg_->Begin(db_helpers::kClusterHostType, {});
     for (auto& value : result) {
-      value.random_number = pg_->Execute(db_helpers::kClusterHostType,
-                                         db_helpers::kSelectRowQuery, value.id)
+      value.random_number = trx.Execute(db_helpers::kSelectRowQuery, value.id)
                                 .AsSingleRow<db_helpers::WorldTableRow>(
                                     userver::storages::postgres::kRowTag)
                                 .random_number;
     }
+    trx.Commit();
   }
 
   return userver::formats::json::ValueBuilder{result}.ExtractValue();

+ 22 - 20
frameworks/C++/userver/userver_benchmark/controllers/updates/handler.cpp

@@ -53,30 +53,32 @@ userver::formats::json::Value Handler::GetResponse(int queries) const {
   std::sort(values.begin(), values.end(),
             [](const auto& lhs, const auto& rhs) { return lhs.id < rhs.id; });
 
-  const auto lock = semaphore_.Acquire();
-  auto transaction = pg_->Begin(db_helpers::kClusterHostType, {});
-  for (auto& value : values) {
-    value.random_number = pg_->Execute(db_helpers::kClusterHostType,
-                                       db_helpers::kSelectRowQuery, value.id)
-                              .AsSingleRow<db_helpers::WorldTableRow>(
-                                  userver::storages::postgres::kRowTag)
-                              .random_number;
-  }
+  boost::container::small_vector<db_helpers::WorldTableRow, 20> result;
 
-  auto json_result =
-      userver::formats::json::ValueBuilder{values}.ExtractValue();
+  {
+    const auto lock = semaphore_.Acquire();
 
-  for (auto& value : values) {
-    value.random_number = db_helpers::GenerateRandomValue();
-  }
+    auto trx = pg_->Begin(db_helpers::kClusterHostType, {});
+    for (auto& value : values) {
+      value.random_number = trx.Execute(db_helpers::kSelectRowQuery, value.id)
+                                .AsSingleRow<db_helpers::WorldTableRow>(
+                                    userver::storages::postgres::kRowTag)
+                                .random_number;
+    }
 
-  userver::storages::postgres::io::SplitContainerByColumns(values,
-                                                           values.size())
-      .Perform([this](const auto&... args) {
-        pg_->Execute(db_helpers::kClusterHostType, update_query_, args...);
-      });
+    // We copy values here (and hope compiler optimizes it into one memcpy call)
+    // to not serialize into json within transaction
+    result.assign(values.begin(), values.end());
+
+    for (auto& value : values) {
+      value.random_number = db_helpers::GenerateRandomValue();
+    }
+
+    trx.ExecuteDecomposeBulk(update_query_, values, values.size());
+    trx.Commit();
+  }
 
-  return json_result;
+  return userver::formats::json::ValueBuilder{values}.ExtractValue();
 }
 
 }  // namespace userver_techempower::updates

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

@@ -2,9 +2,8 @@
   "USERVER_CACHES": {},
   "USERVER_CANCEL_HANDLE_REQUEST_BY_DEADLINE": false,
   "POSTGRES_CONGESTION_CONTROL_SETTINGS": {},
-  "POSTGRES_DEADLINE_PROPAGATION_ENABLED": false,
-  "POSTGRES_CONNECTION_PIPELINE_MODE_ENABLED": false,
-  "POSTGRES_CONNECTION_PIPELINE_EXPERIMENT": 0,
+  "POSTGRES_DEADLINE_PROPAGATION_VERSION": 0,
+  "POSTGRES_CONNECTION_PIPELINE_EXPERIMENT": 5,
   "USERVER_CHECK_AUTH_IN_HANDLERS": false,
   "USERVER_DUMPS": {},
   "USERVER_HTTP_PROXY": "",
@@ -45,8 +44,7 @@
 
   "POSTGRES_STATEMENT_METRICS_SETTINGS": {},
   "POSTGRES_CONNLIMIT_MODE_AUTO_ENABLED": false,
-  "POSTGRES_DEADLINE_PROPAGATION_VERSION": 0,
-  "POSTGRES_CONNECTION_PIPELINE_ENABLED": false,
+  "USERVER_DEADLINE_PROPAGATION_ENABLED": false,
   "POSTGRES_CONNECTION_POOL_SETTINGS": {},
   "POSTGRES_CONNECTION_SETTINGS": {},
   "POSTGRES_DEFAULT_COMMAND_CONTROL": {

+ 11 - 3
frameworks/C++/userver/userver_configs/static_config.yaml

@@ -1,7 +1,8 @@
 # yaml
 components_manager:
     event_thread_pool:
-        threads: 3
+        threads: 5
+        dedicated_timer_threads: 1
     coro_pool:
         initial_size: 10000              # Preallocate 10000 coroutines at startup.
         max_size: 300000                 # Do not keep more than 300000 preallocated coroutines.
@@ -11,7 +12,7 @@ components_manager:
 
         main-task-processor:            # Make a task processor for CPU-bound couroutine tasks.
             thread_name: main-worker    # OS will show the threads of this task processor with 'main-worker' prefix.
-            worker_threads: 25
+            worker_threads: 23
             guess-cpu-limit: true
 
         fs-task-processor:              # Make a separate task processor for filesystem bound tasks.
@@ -102,8 +103,9 @@ components_manager:
 
         world-pg-cache:
             pgcomponent: hello-world-db
+            chunk-size: 0 # fetch all rows at once
             update-types: only-full
-            update-interval: 1s
+            update-interval: 60s
             update-correction: 50ms
 
         cached-queries-handler:
@@ -118,3 +120,9 @@ components_manager:
             decompress_request: false
             task_processor: main-task-processor
 
+userver_experiments:
+ # please note that this doesn't actually fully enable pipelining (which is forbidden by TFB rules):
+ # all it does is it pipelines first BEGIN, SELECT SET_CONFIG, <first execute> of a transaction, all other
+ # operations are still performed with a round-trip to a server (basically every pipeline contains a single operation).
+ - pg-connection-pipeline-enabled
+