From 046295d301ea54f083cf342db9833ad50d724be9 Mon Sep 17 00:00:00 2001
From: Stan Hu <stanhu@gmail.com>
Date: Thu, 18 Jun 2020 22:54:06 -0700
Subject: [PATCH] Fix Gitaly duration timings for conflict and search RPCs

For many Gitaly RPCs, previously the `gitaly_duration_s` log timings
only accounted for the initial request/response time. We now measure the
total time it takes to consume the streaming response for the following
RPCs:

1. ListConflictFiles
2. SearchFilesByContent

Part of https://gitlab.com/gitlab-org/gitlab/-/issues/30334
---
 .../sh-fix-gitaly-conflicts-service-duration.yml   |  5 +++++
 lib/gitlab/gitaly_client/conflicts_service.rb      |  6 +++---
 lib/gitlab/gitaly_client/repository_service.rb     | 14 +++++++++-----
 3 files changed, 17 insertions(+), 8 deletions(-)
 create mode 100644 changelogs/unreleased/sh-fix-gitaly-conflicts-service-duration.yml

diff --git a/changelogs/unreleased/sh-fix-gitaly-conflicts-service-duration.yml b/changelogs/unreleased/sh-fix-gitaly-conflicts-service-duration.yml
new file mode 100644
index 00000000000000..0e9e9945052a39
--- /dev/null
+++ b/changelogs/unreleased/sh-fix-gitaly-conflicts-service-duration.yml
@@ -0,0 +1,5 @@
+---
+title: Fix Gitaly duration timings for conflicts and search RPCs
+merge_request: 34909
+author:
+type: other
diff --git a/lib/gitlab/gitaly_client/conflicts_service.rb b/lib/gitlab/gitaly_client/conflicts_service.rb
index f7eb4b45197ebb..3f3c0eb6b4aac6 100644
--- a/lib/gitlab/gitaly_client/conflicts_service.rb
+++ b/lib/gitlab/gitaly_client/conflicts_service.rb
@@ -20,9 +20,9 @@ def list_conflict_files
           our_commit_oid: @our_commit_oid,
           their_commit_oid: @their_commit_oid
         )
-        response = GitalyClient.call(@repository.storage, :conflicts_service, :list_conflict_files, request, timeout: GitalyClient.long_timeout)
-
-        GitalyClient::ConflictFilesStitcher.new(response, @gitaly_repo)
+        GitalyClient.streaming_call(@repository.storage, :conflicts_service, :list_conflict_files, request, timeout: GitalyClient.long_timeout) do |response|
+          GitalyClient::ConflictFilesStitcher.new(response, @gitaly_repo)
+        end
       end
 
       def conflicts?
diff --git a/lib/gitlab/gitaly_client/repository_service.rb b/lib/gitlab/gitaly_client/repository_service.rb
index f74c9ea4192547..f20568acde96ac 100644
--- a/lib/gitlab/gitaly_client/repository_service.rb
+++ b/lib/gitlab/gitaly_client/repository_service.rb
@@ -334,9 +334,9 @@ def search_files_by_name(ref, query)
 
       def search_files_by_content(ref, query, options = {})
         request = Gitaly::SearchFilesByContentRequest.new(repository: @gitaly_repo, ref: ref, query: query)
-        response = GitalyClient.call(@storage, :repository_service, :search_files_by_content, request, timeout: GitalyClient.default_timeout)
-
-        search_results_from_response(response, options)
+        GitalyClient.streaming_call(@storage, :repository_service, :search_files_by_content, request, timeout: GitalyClient.default_timeout) do |response|
+          search_results_from_response(response, options)
+        end
       end
 
       def disconnect_alternates
@@ -403,14 +403,18 @@ def search_results_from_response(gitaly_response, options = {})
 
       def gitaly_fetch_stream_to_file(save_path, rpc_name, request_class, timeout)
         request = request_class.new(repository: @gitaly_repo)
-        response = GitalyClient.call(
+        GitalyClient.streaming_call(
           @storage,
           :repository_service,
           rpc_name,
           request,
           timeout: timeout
-        )
+        ) do |response|
+          write_stream_to_file(response, save_path)
+        end
+      end
 
+      def write_stream_to_file(response, save_path)
         File.open(save_path, 'wb') do |f|
           response.each do |message|
             f.write(message.data)
-- 
GitLab