-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtest_progress_stats.py
More file actions
202 lines (159 loc) · 6.1 KB
/
Copy pathtest_progress_stats.py
File metadata and controls
202 lines (159 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# (C) Datadog, Inc. 2023-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import pytest
from datadog_checks.postgres.util import (
ANALYZE_PROGRESS_METRICS,
CLUSTER_VACUUM_PROGRESS_METRICS,
INDEX_PROGRESS_METRICS,
VACUUM_PROGRESS_METRICS,
VACUUM_PROGRESS_METRICS_LT_17,
)
from .common import DB_NAME, _get_expected_tags, _iterate_metric_name
from .utils import (
_wait_for_value,
kill_session,
kill_vacuum,
lock_table,
requires_over_12,
requires_over_13,
requires_over_17,
requires_under_17,
run_query_thread,
run_vacuum_thread,
)
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures('dd_environment')]
def _check_analyze_progress(check, pg_instance, table):
thread = run_vacuum_thread(pg_instance, f'ANALYZE {table}')
# Wait for vacuum to be reported
_wait_for_value(
pg_instance,
lower_threshold=0,
query="SELECT count(*) from pg_stat_progress_analyze",
)
# Collect metrics
check.check(pg_instance)
# Kill vacuum and cleanup thread
kill_vacuum(pg_instance)
thread.join()
@requires_over_13
def test_analyze_progress_inherited(aggregator, integration_check, pg_instance):
check = integration_check(pg_instance)
_check_analyze_progress(check, pg_instance, 'test_part')
expected_tags = _get_expected_tags(check, pg_instance) + [
'child_relation:test_part1',
'phase:acquiring inherited sample rows',
'table:test_part',
f'db:{DB_NAME}',
]
for metric_name in _iterate_metric_name(ANALYZE_PROGRESS_METRICS):
aggregator.assert_metric(metric_name, count=1, tags=expected_tags)
@requires_over_13
def test_analyze_progress(aggregator, integration_check, pg_instance):
check = integration_check(pg_instance)
_check_analyze_progress(check, pg_instance, 'test_part1')
expected_tags = _get_expected_tags(check, pg_instance) + [
'phase:acquiring sample rows',
'table:test_part1',
f'db:{DB_NAME}',
]
for metric_name in _iterate_metric_name(ANALYZE_PROGRESS_METRICS):
aggregator.assert_metric(metric_name, count=1, tags=expected_tags)
@requires_over_17
def test_vacuum_progress(aggregator, integration_check, pg_instance):
check = integration_check(pg_instance)
# Start vacuum
thread = run_vacuum_thread(pg_instance, 'VACUUM (DISABLE_PAGE_SKIPPING) test_part1')
# Wait for vacuum to be reported
_wait_for_value(
pg_instance,
lower_threshold=0,
query="SELECT count(*) from pg_stat_progress_vacuum",
)
# Collect metrics
check.check(pg_instance)
# Kill vacuum and cleanup thread
kill_vacuum(pg_instance)
thread.join()
expected_tags = _get_expected_tags(check, pg_instance) + [
'phase:scanning heap',
'table:test_part1',
f'db:{DB_NAME}',
]
for metric_name in _iterate_metric_name(VACUUM_PROGRESS_METRICS):
aggregator.assert_metric(metric_name, count=1, tags=expected_tags)
@requires_over_12
@requires_under_17
def test_vacuum_progress_lt_17(aggregator, integration_check, pg_instance):
check = integration_check(pg_instance)
# Start vacuum
thread = run_vacuum_thread(pg_instance, 'VACUUM (DISABLE_PAGE_SKIPPING) test_part1')
# Wait for vacuum to be reported
_wait_for_value(
pg_instance,
lower_threshold=0,
query="SELECT count(*) from pg_stat_progress_vacuum",
)
# Collect metrics
check.check(pg_instance)
# Kill vacuum and cleanup thread
kill_vacuum(pg_instance)
thread.join()
expected_tags = _get_expected_tags(check, pg_instance) + [
'phase:scanning heap',
'table:test_part1',
f'db:{DB_NAME}',
]
for metric_name in _iterate_metric_name(VACUUM_PROGRESS_METRICS_LT_17):
aggregator.assert_metric(metric_name, count=1, tags=expected_tags)
@requires_over_12
def test_index_progress(aggregator, integration_check, pg_instance):
check = integration_check(pg_instance)
# Keep test_part locked to prevent create index concurrently from finishing
conn = lock_table(pg_instance, 'test_part1', 'ROW EXCLUSIVE')
# Start vacuum in a thread
thread = run_query_thread(pg_instance, 'CREATE INDEX CONCURRENTLY test_progress_index ON test_part1 (id);')
# Wait for blocked created index to appear
_wait_for_value(
pg_instance,
lower_threshold=0,
query="select count(*) FROM pg_stat_progress_create_index where lockers_total=1;",
)
# Gather metrics
check.check(pg_instance)
# Kill the create index
kill_session(pg_instance, 'CREATE INDEX')
# Cleanup connection and thread
conn.close()
thread.join()
# Check metrics
expected_tags = _get_expected_tags(check, pg_instance) + [
'command:CREATE INDEX CONCURRENTLY',
'index:test_progress_index',
'phase:waiting for writers before build',
'table:test_part1',
f'db:{DB_NAME}',
]
for metric_name in _iterate_metric_name(INDEX_PROGRESS_METRICS):
aggregator.assert_metric(metric_name, count=1, tags=expected_tags)
aggregator.assert_metric('postgresql.create_index.lockers_total', count=1, value=1, tags=expected_tags)
@requires_over_12
def test_cluster_vacuum_progress(aggregator, integration_check, pg_instance):
check = integration_check(pg_instance)
# Keep pg_class lock to block vacuum full during initilizing phase
conn = lock_table(pg_instance, 'pg_catalog.pg_class', 'EXCLUSIVE')
# Start vacuum in a thread
thread = run_vacuum_thread(pg_instance, 'VACUUM FULL personsdup1')
_wait_for_value(pg_instance, lower_threshold=0, query="select count(*) FROM pg_stat_progress_cluster;")
check.check(pg_instance)
# Cleanup connection and thread
conn.close()
thread.join()
expected_tags = _get_expected_tags(check, pg_instance) + [
'phase:initializing',
'command:VACUUM FULL',
'table:personsdup1',
f'db:{DB_NAME}',
]
for metric_name in _iterate_metric_name(CLUSTER_VACUUM_PROGRESS_METRICS):
aggregator.assert_metric(metric_name, count=1, tags=expected_tags)