Skip to content

Commit 0296114

Browse files
authored
Define DbRetentionJob(Jdbi, DbRetentionConfig) (#2549)
* Define `DbRetentionJob(Jdbi, DbRetentionConfig)` Signed-off-by: wslulciuc <willy@datakin.com> * Define `DbRetentionJob(Jdbi, DbRetentionConfig)` Signed-off-by: wslulciuc <willy@datakin.com> * continued: Define `DbRetentionJob(Jdbi, DbRetentionConfig)` Signed-off-by: wslulciuc <willy@datakin.com> * continued: Resolve merge conflicts Signed-off-by: wslulciuc <willy@datakin.com> * Add oss license header Signed-off-by: wslulciuc <willy@datakin.com> --------- Signed-off-by: wslulciuc <willy@datakin.com>
1 parent de6fed8 commit 0296114

4 files changed

Lines changed: 145 additions & 24 deletions

File tree

api/src/main/java/marquez/MarquezApp.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,7 @@ public void run(@NonNull MarquezConfig config, @NonNull Environment env) {
137137
// Add scheduled jobs to lifecycle.
138138
if (config.hasDbRetentionPolicy()) {
139139
// Add job to apply retention policy to database.
140-
env.lifecycle()
141-
.manage(
142-
new DbRetentionJob(
143-
jdbi,
144-
config.getDbRetention().getFrequencyMins(),
145-
config.getDbRetention().getNumberOfRowsPerBatch(),
146-
config.getDbRetention().getRetentionDays()));
140+
env.lifecycle().manage(new DbRetentionJob(jdbi, config.getDbRetention()));
147141
}
148142
}
149143

api/src/main/java/marquez/jobs/DbRetentionConfig.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88
import static marquez.db.DbRetention.DEFAULT_NUMBER_OF_ROWS_PER_BATCH;
99
import static marquez.db.DbRetention.DEFAULT_RETENTION_DAYS;
1010

11+
import javax.validation.constraints.Positive;
12+
import lombok.AllArgsConstructor;
13+
import lombok.Builder;
1114
import lombok.Getter;
12-
import lombok.Setter;
15+
import lombok.NoArgsConstructor;
16+
import lombok.Value;
1317

1418
/** Configuration for {@link DbRetentionJob}. */
15-
public final class DbRetentionConfig {
19+
@Builder
20+
@NoArgsConstructor
21+
@AllArgsConstructor
22+
@Value
23+
public class DbRetentionConfig {
1624
public static final int DEFAULT_FREQUENCY_MINS = 15;
1725

18-
@Getter @Setter private int frequencyMins = DEFAULT_FREQUENCY_MINS;
19-
@Getter @Setter private int numberOfRowsPerBatch = DEFAULT_NUMBER_OF_ROWS_PER_BATCH;
20-
@Getter @Setter private int retentionDays = DEFAULT_RETENTION_DAYS;
26+
@Builder.Default @Getter @Positive int frequencyMins = DEFAULT_FREQUENCY_MINS;
27+
@Builder.Default @Getter @Positive int numberOfRowsPerBatch = DEFAULT_NUMBER_OF_ROWS_PER_BATCH;
28+
@Builder.Default @Getter @Positive int retentionDays = DEFAULT_RETENTION_DAYS;
2129
}

api/src/main/java/marquez/jobs/DbRetentionJob.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
package marquez.jobs;
77

8-
import static com.google.common.base.Preconditions.checkArgument;
9-
108
import com.google.common.util.concurrent.AbstractScheduledService;
119
import io.dropwizard.lifecycle.Managed;
1210
import java.time.Duration;
@@ -43,21 +41,17 @@ public class DbRetentionJob extends AbstractScheduledService implements Managed
4341
* of {@code retentionDays}.
4442
*/
4543
public DbRetentionJob(
46-
@NonNull final Jdbi jdbi,
47-
final int frequencyMins,
48-
final int numberOfRowsPerBatch,
49-
final int retentionDays) {
50-
checkArgument(frequencyMins > 0, "'frequencyMins' must be > 0");
51-
checkArgument(numberOfRowsPerBatch > 0, "'numberOfRowsPerBatch' must be > 0");
52-
checkArgument(retentionDays > 0, "'retentionDays' must be > 0");
53-
this.numberOfRowsPerBatch = numberOfRowsPerBatch;
54-
this.retentionDays = retentionDays;
44+
@NonNull final Jdbi jdbi, @NonNull final DbRetentionConfig dbRetentionConfig) {
45+
this.numberOfRowsPerBatch = dbRetentionConfig.getNumberOfRowsPerBatch();
46+
this.retentionDays = dbRetentionConfig.getRetentionDays();
5547

48+
// Open connection.
5649
this.jdbi = jdbi;
5750

5851
// Define fixed schedule with no delay.
5952
this.fixedRateScheduler =
60-
Scheduler.newFixedRateSchedule(NO_DELAY, Duration.ofMinutes(frequencyMins));
53+
Scheduler.newFixedRateSchedule(
54+
NO_DELAY, Duration.ofMinutes(dbRetentionConfig.getFrequencyMins()));
6155
}
6256

6357
@Override
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2018-2023 contributors to the Marquez project
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package marquez.jobs;
7+
8+
import static marquez.db.DbRetention.DEFAULT_NUMBER_OF_ROWS_PER_BATCH;
9+
import static marquez.db.DbRetention.DEFAULT_RETENTION_DAYS;
10+
import static marquez.jobs.DbRetentionConfig.DEFAULT_FREQUENCY_MINS;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
import java.util.Set;
14+
import javax.validation.ConstraintViolation;
15+
import javax.validation.Validation;
16+
import javax.validation.Validator;
17+
import org.junit.jupiter.api.Test;
18+
19+
/** The test suite for {@link DbRetentionConfig}. */
20+
public class DbRetentionConfigTest {
21+
private static final Validator VALIDATOR =
22+
Validation.buildDefaultValidatorFactory().getValidator();
23+
24+
@Test
25+
public void testNewDbRetentionConfig_withDefaultsOnly() {
26+
final DbRetentionConfig configWithDefaults = new DbRetentionConfig();
27+
28+
assertThat(configWithDefaults.getFrequencyMins()).isEqualTo(DEFAULT_FREQUENCY_MINS);
29+
assertThat(configWithDefaults.getNumberOfRowsPerBatch())
30+
.isEqualTo(DEFAULT_NUMBER_OF_ROWS_PER_BATCH);
31+
assertThat(configWithDefaults.getRetentionDays()).isEqualTo(DEFAULT_RETENTION_DAYS);
32+
}
33+
34+
@Test
35+
public void testNewDbRetentionConfig_overrideFrequencyMins() {
36+
final int frequencyMinsOverride = 5;
37+
final DbRetentionConfig configWithFrequencyMinsOverride =
38+
DbRetentionConfig.builder().frequencyMins(frequencyMinsOverride).build();
39+
40+
// No constraint violations.
41+
final Set<ConstraintViolation<DbRetentionConfig>> violations =
42+
VALIDATOR.validate(configWithFrequencyMinsOverride);
43+
assertThat(violations).isEmpty();
44+
45+
assertThat(configWithFrequencyMinsOverride.getFrequencyMins()).isEqualTo(frequencyMinsOverride);
46+
assertThat(configWithFrequencyMinsOverride.getNumberOfRowsPerBatch())
47+
.isEqualTo(DEFAULT_NUMBER_OF_ROWS_PER_BATCH);
48+
assertThat(configWithFrequencyMinsOverride.getRetentionDays())
49+
.isEqualTo(DEFAULT_RETENTION_DAYS);
50+
}
51+
52+
@Test
53+
public void testNewDbRetentionConfig_overrideNumberOfRowsPerBatch() {
54+
final int numberOfRowsPerBatchOverride = 25;
55+
final DbRetentionConfig configWithNumberOfRowsPerBatchOverride =
56+
DbRetentionConfig.builder().numberOfRowsPerBatch(numberOfRowsPerBatchOverride).build();
57+
58+
// No constraint violations.
59+
final Set<ConstraintViolation<DbRetentionConfig>> violations =
60+
VALIDATOR.validate(configWithNumberOfRowsPerBatchOverride);
61+
assertThat(violations).isEmpty();
62+
63+
assertThat(configWithNumberOfRowsPerBatchOverride.getFrequencyMins())
64+
.isEqualTo(DEFAULT_FREQUENCY_MINS);
65+
assertThat(configWithNumberOfRowsPerBatchOverride.getNumberOfRowsPerBatch())
66+
.isEqualTo(numberOfRowsPerBatchOverride);
67+
assertThat(configWithNumberOfRowsPerBatchOverride.getRetentionDays())
68+
.isEqualTo(DEFAULT_RETENTION_DAYS);
69+
}
70+
71+
@Test
72+
public void testNewDbRetentionConfig_overrideRetentionDays() {
73+
final int retentionDaysOverride = 14;
74+
final DbRetentionConfig configWithNumberOfRowsPerBatchOverride =
75+
DbRetentionConfig.builder().retentionDays(retentionDaysOverride).build();
76+
77+
// No constraint violations.
78+
final Set<ConstraintViolation<DbRetentionConfig>> violations =
79+
VALIDATOR.validate(configWithNumberOfRowsPerBatchOverride);
80+
assertThat(violations).isEmpty();
81+
82+
assertThat(configWithNumberOfRowsPerBatchOverride.getFrequencyMins())
83+
.isEqualTo(DEFAULT_FREQUENCY_MINS);
84+
assertThat(configWithNumberOfRowsPerBatchOverride.getNumberOfRowsPerBatch())
85+
.isEqualTo(DEFAULT_NUMBER_OF_ROWS_PER_BATCH);
86+
assertThat(configWithNumberOfRowsPerBatchOverride.getRetentionDays())
87+
.isEqualTo(retentionDaysOverride);
88+
}
89+
90+
@Test
91+
public void testNewDbRetentionConfig_negativeFrequencyMins() {
92+
final int negativeFrequencyMins = -5;
93+
94+
final DbRetentionConfig configWithNegativeFrequencyMins =
95+
DbRetentionConfig.builder().frequencyMins(negativeFrequencyMins).build();
96+
97+
final Set<ConstraintViolation<DbRetentionConfig>> violations =
98+
VALIDATOR.validate(configWithNegativeFrequencyMins);
99+
assertThat(violations).hasSize(1);
100+
}
101+
102+
@Test
103+
public void testNewDbRetentionConfig_negativeNumberOfRowsPerBatch() {
104+
final int negativeNumberOfRowsPerBatch = -25;
105+
106+
final DbRetentionConfig configWithNegativeNumberOfRowsPerBatch =
107+
DbRetentionConfig.builder().numberOfRowsPerBatch(negativeNumberOfRowsPerBatch).build();
108+
109+
final Set<ConstraintViolation<DbRetentionConfig>> violations =
110+
VALIDATOR.validate(configWithNegativeNumberOfRowsPerBatch);
111+
assertThat(violations).hasSize(1);
112+
}
113+
114+
@Test
115+
public void testNewDbRetentionConfig_negativeRetentionDays() {
116+
final int negativeRetentionDays = -14;
117+
118+
final DbRetentionConfig configWithNegativeRetentionDays =
119+
DbRetentionConfig.builder().retentionDays(negativeRetentionDays).build();
120+
121+
final Set<ConstraintViolation<DbRetentionConfig>> violations =
122+
VALIDATOR.validate(configWithNegativeRetentionDays);
123+
assertThat(violations).hasSize(1);
124+
}
125+
}

0 commit comments

Comments
 (0)