Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
0cca2912
Commit
0cca2912
authored
Jul 20, 2021
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Instrument sub-transactions created using ApplicationRecord
parent
cf6e08a6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
93 additions
and
2 deletions
+93
-2
app/models/application_record.rb
app/models/application_record.rb
+13
-0
config/feature_flags/ops/active_record_subtransactions_counter.yml
...ature_flags/ops/active_record_subtransactions_counter.yml
+8
-0
lib/gitlab/database/load_balancing/host.rb
lib/gitlab/database/load_balancing/host.rb
+2
-2
lib/gitlab/database/metrics.rb
lib/gitlab/database/metrics.rb
+26
-0
spec/models/application_record_spec.rb
spec/models/application_record_spec.rb
+44
-0
No files found.
app/models/application_record.rb
View file @
0cca2912
...
...
@@ -86,4 +86,17 @@ class ApplicationRecord < ActiveRecord::Base
values
=
enum_mod
.
definition
.
transform_values
{
|
v
|
v
[
:value
]
}
enum
(
enum_mod
.
key
=>
values
)
end
def
self
.
transaction
(
**
options
,
&
block
)
if
options
[
:requires_new
]
&&
track_subtransactions?
::
Gitlab
::
Database
::
Metrics
.
subtransactions_increment
(
self
.
name
)
end
super
(
**
options
,
&
block
)
end
def
self
.
track_subtransactions?
::
Feature
.
enabled?
(
:active_record_subtransactions_counter
,
type: :ops
,
default_enabled: :yaml
)
&&
connection
.
transaction_open?
end
end
config/feature_flags/ops/active_record_subtransactions_counter.yml
0 → 100644
View file @
0cca2912
---
name
:
active_record_subtransactions_counter
introduced_by_url
:
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/66477
rollout_issue_url
:
milestone
:
'
14.1'
type
:
ops
group
:
group::pipeline execution
default_enabled
:
false
lib/gitlab/database/load_balancing/host.rb
View file @
0cca2912
...
...
@@ -42,9 +42,9 @@ module Gitlab
# timeout - The time after which the pool should be forcefully
# disconnected.
def
disconnect!
(
timeout
=
120
)
start_time
=
Metrics
::
System
.
monotonic_time
start_time
=
::
Gitlab
::
Metrics
::
System
.
monotonic_time
while
(
Metrics
::
System
.
monotonic_time
-
start_time
)
<=
timeout
while
(
::
Gitlab
::
Metrics
::
System
.
monotonic_time
-
start_time
)
<=
timeout
break
if
pool
.
connections
.
none?
(
&
:in_use?
)
sleep
(
2
)
...
...
lib/gitlab/database/metrics.rb
0 → 100644
View file @
0cca2912
# frozen_string_literal: true
module
Gitlab
module
Database
class
Metrics
extend
::
Gitlab
::
Utils
::
StrongMemoize
class
<<
self
def
subtransactions_increment
(
model_name
)
subtransactions_counter
.
increment
(
model:
model_name
)
end
private
def
subtransactions_counter
strong_memoize
(
:subtransactions_counter
)
do
name
=
:gitlab_active_record_subtransactions_total
comment
=
'Total amount of subtransactions created by ActiveRecord'
::
Gitlab
::
Metrics
.
counter
(
name
,
comment
)
end
end
end
end
end
end
spec/models/application_record_spec.rb
View file @
0cca2912
...
...
@@ -105,6 +105,50 @@ RSpec.describe ApplicationRecord do
end
end
describe
'.transaction'
,
:delete
do
it
'opens a new transaction'
do
expect
(
described_class
.
connection
.
transaction_open?
).
to
be
false
Project
.
transaction
do
expect
(
Project
.
connection
.
transaction_open?
).
to
be
true
Project
.
transaction
(
requires_new:
true
)
do
expect
(
Project
.
connection
.
transaction_open?
).
to
be
true
end
end
end
it
'does not increment a counter when a transaction is not nested'
do
expect
(
described_class
.
connection
.
transaction_open?
).
to
be
false
expect
(
::
Gitlab
::
Database
::
Metrics
)
.
not_to
receive
(
:subtransactions_increment
)
Project
.
transaction
do
expect
(
Project
.
connection
.
transaction_open?
).
to
be
true
end
Project
.
transaction
(
requires_new:
true
)
do
expect
(
Project
.
connection
.
transaction_open?
).
to
be
true
end
end
it
'increments a counter when a nested transaction is created'
do
expect
(
described_class
.
connection
.
transaction_open?
).
to
be
false
expect
(
::
Gitlab
::
Database
::
Metrics
)
.
to
receive
(
:subtransactions_increment
)
.
with
(
'Project'
)
.
once
Project
.
transaction
do
Project
.
transaction
(
requires_new:
true
)
do
expect
(
Project
.
connection
.
transaction_open?
).
to
be
true
end
end
end
end
describe
'.with_fast_read_statement_timeout'
do
context
'when the query runs faster than configured timeout'
do
it
'executes the query without error'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment