Skip to content

Commit 8bfa9f0

Browse files
committed
Refactor to combine policy config code
Function to read common fields in policy configurations are duplicated, so they are combined into a single module. Using more useful error codes as well instead of "internal error".
1 parent d5c66d0 commit 8bfa9f0

File tree

11 files changed

+60
-63
lines changed

11 files changed

+60
-63
lines changed

tsl/src/bgw_policy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(SOURCES
44
${CMAKE_CURRENT_SOURCE_DIR}/job.c
55
${CMAKE_CURRENT_SOURCE_DIR}/job_api.c
66
${CMAKE_CURRENT_SOURCE_DIR}/reorder_api.c
7+
${CMAKE_CURRENT_SOURCE_DIR}/policy_config.c
78
${CMAKE_CURRENT_SOURCE_DIR}/retention_api.c
89
${CMAKE_CURRENT_SOURCE_DIR}/policy_utils.c
910
${CMAKE_CURRENT_SOURCE_DIR}/policies_v2.c)

tsl/src/bgw_policy/compression_api.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "bgw_policy/job.h"
2020
#include "bgw_policy/job_api.h"
2121
#include "bgw_policy/policies_v2.h"
22-
#include "compression/api.h"
22+
#include "bgw_policy/policy_config.h"
2323
#include "errors.h"
2424
#include "guc.h"
2525
#include "hypertable.h"
@@ -53,21 +53,6 @@ policy_compression_get_maxchunks_per_job(const Jsonb *config)
5353
return (found && maxchunks > 0) ? maxchunks : 0;
5454
}
5555

56-
int32
57-
policy_compression_get_hypertable_id(const Jsonb *config)
58-
{
59-
bool found;
60-
int32 hypertable_id =
61-
ts_jsonb_get_int32_field(config, POL_COMPRESSION_CONF_KEY_HYPERTABLE_ID, &found);
62-
63-
if (!found)
64-
ereport(ERROR,
65-
(errcode(ERRCODE_INTERNAL_ERROR),
66-
errmsg("could not find hypertable_id in config for job")));
67-
68-
return hypertable_id;
69-
}
70-
7156
int64
7257
policy_recompression_get_recompress_after_int(const Jsonb *config)
7358
{
@@ -296,7 +281,7 @@ policy_compression_add_internal(Oid user_rel_oid, Datum compress_after_datum,
296281
JsonbParseState *parse_state = NULL;
297282

298283
pushJsonbValue(&parse_state, WJB_BEGIN_OBJECT, NULL);
299-
ts_jsonb_add_int32(parse_state, POL_COMPRESSION_CONF_KEY_HYPERTABLE_ID, hypertable->fd.id);
284+
ts_jsonb_add_int32(parse_state, POLICY_CONFIG_KEY_HYPERTABLE_ID, hypertable->fd.id);
300285
validate_compress_after_type(dim, partitioning_type, compress_after_type);
301286

302287
if (use_access_method != USE_AM_NULL)

tsl/src/bgw_policy/continuous_aggregate_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ policy_continuous_aggregate_get_mat_hypertable_id(const Jsonb *config)
4343

4444
if (!found)
4545
ereport(ERROR,
46-
(errcode(ERRCODE_INTERNAL_ERROR),
46+
(errcode(ERRCODE_SQL_JSON_MEMBER_NOT_FOUND),
4747
errmsg("could not find \"%s\" in config for job",
4848
POL_REFRESH_CONF_KEY_MAT_HYPERTABLE_ID)));
4949

tsl/src/bgw_policy/job.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "bgw_policy/chunk_stats.h"
3434
#include "bgw_policy/compression_api.h"
3535
#include "bgw_policy/continuous_aggregate_api.h"
36+
#include "bgw_policy/policy_config.h"
3637
#include "bgw_policy/policy_utils.h"
3738
#include "bgw_policy/reorder_api.h"
3839
#include "bgw_policy/retention_api.h"
@@ -266,7 +267,7 @@ policy_reorder_execute(int32 job_id, Jsonb *config)
266267
void
267268
policy_reorder_read_and_validate_config(Jsonb *config, PolicyReorderData *policy)
268269
{
269-
int32 htid = policy_reorder_get_hypertable_id(config);
270+
int32 htid = policy_config_get_hypertable_id(config);
270271
Hypertable *ht = ts_hypertable_get_by_id(htid);
271272

272273
if (!ht)
@@ -319,7 +320,7 @@ policy_retention_read_and_validate_config(Jsonb *config, PolicyRetentionData *po
319320
interval_getter = policy_retention_get_drop_after_interval;
320321
bool use_creation_time = false;
321322

322-
object_relid = ts_hypertable_id_to_relid(policy_retention_get_hypertable_id(config), false);
323+
object_relid = ts_hypertable_id_to_relid(policy_config_get_hypertable_id(config), false);
323324
hypertable = ts_hypertable_cache_get_cache_and_entry(object_relid, CACHE_FLAG_NONE, &hcache);
324325

325326
open_dim = get_open_dimension_for_hypertable(hypertable, false);
@@ -527,8 +528,7 @@ policy_refresh_cagg_read_and_validate_config(Jsonb *config, PolicyContinuousAggD
527528
void
528529
policy_compression_read_and_validate_config(Jsonb *config, PolicyCompressionData *policy_data)
529530
{
530-
Oid table_relid =
531-
ts_hypertable_id_to_relid(policy_compression_get_hypertable_id(config), false);
531+
Oid table_relid = ts_hypertable_id_to_relid(policy_config_get_hypertable_id(config), false);
532532
Cache *hcache;
533533
Hypertable *hypertable =
534534
ts_hypertable_cache_get_cache_and_entry(table_relid, CACHE_FLAG_NONE, &hcache);
@@ -542,8 +542,7 @@ policy_compression_read_and_validate_config(Jsonb *config, PolicyCompressionData
542542
void
543543
policy_recompression_read_and_validate_config(Jsonb *config, PolicyCompressionData *policy_data)
544544
{
545-
Oid table_relid =
546-
ts_hypertable_id_to_relid(policy_compression_get_hypertable_id(config), false);
545+
Oid table_relid = ts_hypertable_id_to_relid(policy_config_get_hypertable_id(config), false);
547546
Cache *hcache;
548547
Hypertable *hypertable =
549548
ts_hypertable_cache_get_cache_and_entry(table_relid, CACHE_FLAG_NONE, &hcache);

tsl/src/bgw_policy/policies_v2.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
#define POLICY_COMPRESSION_PROC_NAME "policy_compression"
2828
#define POLICY_COMPRESSION_CHECK_NAME "policy_compression_check"
29-
#define POL_COMPRESSION_CONF_KEY_HYPERTABLE_ID "hypertable_id"
3029
#define POL_COMPRESSION_CONF_KEY_COMPRESS_AFTER "compress_after"
3130
#define POL_COMPRESSION_CONF_KEY_MAXCHUNKS_TO_COMPRESS "maxchunks_to_compress"
3231
#define POL_COMPRESSION_CONF_KEY_COMPRESS_CREATED_BEFORE "compress_created_before"
@@ -37,11 +36,9 @@
3736

3837
#define POLICY_RETENTION_PROC_NAME "policy_retention"
3938
#define POLICY_RETENTION_CHECK_NAME "policy_retention_check"
40-
#define POL_RETENTION_CONF_KEY_HYPERTABLE_ID "hypertable_id"
4139
#define POL_RETENTION_CONF_KEY_DROP_AFTER "drop_after"
4240
#define POL_RETENTION_CONF_KEY_DROP_CREATED_BEFORE "drop_created_before"
4341

44-
#define SHOW_POLICY_KEY_HYPERTABLE_ID "hypertable_id"
4542
#define SHOW_POLICY_KEY_POLICY_NAME "policy_name"
4643
#define SHOW_POLICY_KEY_REFRESH_INTERVAL "refresh_interval"
4744
#define SHOW_POLICY_KEY_REFRESH_START_OFFSET "refresh_start_offset"

tsl/src/bgw_policy/policy_config.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file and its contents are licensed under the Timescale License.
3+
* Please see the included NOTICE for copyright information and
4+
* LICENSE-TIMESCALE for a copy of the license.
5+
*/
6+
7+
/*
8+
* Module with common functions, variables, and constants for working with
9+
* policy configurations.
10+
*/
11+
12+
#include <postgres.h>
13+
14+
#include "jsonb_utils.h"
15+
#include "policy_config.h"
16+
17+
int32
18+
policy_config_get_hypertable_id(const Jsonb *config)
19+
{
20+
bool found;
21+
int32 hypertable_id = ts_jsonb_get_int32_field(config, POLICY_CONFIG_KEY_HYPERTABLE_ID, &found);
22+
23+
if (!found)
24+
ereport(ERROR,
25+
(errcode(ERRCODE_SQL_JSON_MEMBER_NOT_FOUND),
26+
errmsg("could not find hypertable_id in config for job")));
27+
28+
return hypertable_id;
29+
}

tsl/src/bgw_policy/policy_config.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* This file and its contents are licensed under the Timescale License.
3+
* Please see the included NOTICE for copyright information and
4+
* LICENSE-TIMESCALE for a copy of the license.
5+
*/
6+
#pragma once
7+
8+
/*
9+
* Common functions, variables, and constants for working with policy
10+
* configurations.
11+
*/
12+
13+
#include <postgres.h>
14+
#include <utils/jsonb.h>
15+
16+
#define POLICY_CONFIG_KEY_HYPERTABLE_ID "hypertable_id"
17+
18+
extern int32 policy_config_get_hypertable_id(const Jsonb *config);

tsl/src/bgw_policy/reorder_api.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
#include "bgw/timer.h"
2525
#include "bgw_policy/job.h"
2626
#include "bgw_policy/job_api.h"
27+
#include "bgw_policy/policy_config.h"
2728
#include "bgw_policy/reorder_api.h"
28-
#include "errors.h"
2929
#include "guc.h"
3030
#include "hypertable.h"
31-
#include "reorder.h"
3231
#include "utils.h"
3332
/*
3433
* Default scheduled interval for reorder jobs should be 1/2 of the default chunk length.
@@ -48,26 +47,11 @@
4847
#define DEFAULT_RETRY_PERIOD \
4948
DatumGetIntervalP(DirectFunctionCall3(interval_in, CStringGetDatum("5 min"), InvalidOid, -1))
5049

51-
#define CONFIG_KEY_HYPERTABLE_ID "hypertable_id"
5250
#define CONFIG_KEY_INDEX_NAME "index_name"
5351

5452
#define POLICY_REORDER_PROC_NAME "policy_reorder"
5553
#define POLICY_REORDER_CHECK_NAME "policy_reorder_check"
5654

57-
int32
58-
policy_reorder_get_hypertable_id(const Jsonb *config)
59-
{
60-
bool found;
61-
int32 hypertable_id = ts_jsonb_get_int32_field(config, CONFIG_KEY_HYPERTABLE_ID, &found);
62-
63-
if (!found)
64-
ereport(ERROR,
65-
(errcode(ERRCODE_INTERNAL_ERROR),
66-
errmsg("could not find hypertable_id in config for job")));
67-
68-
return hypertable_id;
69-
}
70-
7155
char *
7256
policy_reorder_get_index_name(const Jsonb *config)
7357
{
@@ -262,7 +246,7 @@ policy_reorder_add(PG_FUNCTION_ARGS)
262246
JsonbParseState *parse_state = NULL;
263247

264248
pushJsonbValue(&parse_state, WJB_BEGIN_OBJECT, NULL);
265-
ts_jsonb_add_int32(parse_state, CONFIG_KEY_HYPERTABLE_ID, hypertable_id);
249+
ts_jsonb_add_int32(parse_state, POLICY_CONFIG_KEY_HYPERTABLE_ID, hypertable_id);
266250
ts_jsonb_add_str(parse_state, CONFIG_KEY_INDEX_NAME, NameStr(*index_name));
267251
JsonbValue *result = pushJsonbValue(&parse_state, WJB_END_OBJECT, NULL);
268252
Jsonb *config = JsonbValueToJsonb(result);

tsl/src/bgw_policy/reorder_api.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ extern Datum policy_reorder_remove(PG_FUNCTION_ARGS);
1313
extern Datum policy_reorder_proc(PG_FUNCTION_ARGS);
1414
extern Datum policy_reorder_check(PG_FUNCTION_ARGS);
1515

16-
extern int32 policy_reorder_get_hypertable_id(const Jsonb *config);
1716
extern char *policy_reorder_get_index_name(const Jsonb *config);

tsl/src/bgw_policy/retention_api.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "bgw/timer.h"
2020
#include "bgw_policy/job.h"
2121
#include "bgw_policy/policies_v2.h"
22+
#include "bgw_policy/policy_config.h"
2223
#include "chunk.h"
2324
#include "dimension.h"
2425
#include "errors.h"
@@ -60,21 +61,6 @@ policy_retention_check(PG_FUNCTION_ARGS)
6061
PG_RETURN_VOID();
6162
}
6263

63-
int32
64-
policy_retention_get_hypertable_id(const Jsonb *config)
65-
{
66-
bool found;
67-
int32 hypertable_id =
68-
ts_jsonb_get_int32_field(config, POL_RETENTION_CONF_KEY_HYPERTABLE_ID, &found);
69-
70-
if (!found)
71-
ereport(ERROR,
72-
(errcode(ERRCODE_INTERNAL_ERROR),
73-
errmsg("could not find hypertable_id in config for job")));
74-
75-
return hypertable_id;
76-
}
77-
7864
int64
7965
policy_retention_get_drop_after_int(const Jsonb *config)
8066
{
@@ -287,7 +273,7 @@ policy_retention_add_internal(Oid ht_oid, Oid window_type, Datum window_datum,
287273
JsonbParseState *parse_state = NULL;
288274

289275
pushJsonbValue(&parse_state, WJB_BEGIN_OBJECT, NULL);
290-
ts_jsonb_add_int32(parse_state, POL_RETENTION_CONF_KEY_HYPERTABLE_ID, hypertable->fd.id);
276+
ts_jsonb_add_int32(parse_state, POLICY_CONFIG_KEY_HYPERTABLE_ID, hypertable->fd.id);
291277

292278
switch (window_type)
293279
{

0 commit comments

Comments
 (0)