Skip to content

Commit 11dd3a8

Browse files
committed
concord-server-db: option to host process log objects in another DB
1 parent 8b36038 commit 11dd3a8

File tree

19 files changed

+613
-319
lines changed

19 files changed

+613
-319
lines changed

server/db/pom.xml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
<properties>
1717
<db.image>library/postgres:10.4-alpine</db.image>
1818
<db.baseDir>${project.build.directory}/db</db.baseDir>
19-
<db.changeLogPath>com/walmartlabs/concord/server/db/liquibase.xml</db.changeLogPath>
19+
<mainDb.changeLogPath>com/walmartlabs/concord/server/db/mainDb.xml</mainDb.changeLogPath>
20+
<logDb.changeLogPath>com/walmartlabs/concord/server/db/logDb.xml</logDb.changeLogPath>
2021
<db.host>localhost</db.host>
2122
<db.username>postgres</db.username>
2223
<db.password>q1</db.password>
@@ -164,14 +165,27 @@
164165
<version>${liquibase.version}</version>
165166
<executions>
166167
<execution>
168+
<id>main-db</id>
167169
<phase>generate-sources</phase>
168170
<goals>
169171
<goal>update</goal>
170172
</goals>
173+
<configuration>
174+
<changeLogFile>src/main/resources/${mainDb.changeLogPath}</changeLogFile>
175+
</configuration>
176+
</execution>
177+
<execution>
178+
<id>log-db</id>
179+
<phase>generate-sources</phase>
180+
<goals>
181+
<goal>update</goal>
182+
</goals>
183+
<configuration>
184+
<changeLogFile>src/main/resources/${logDb.changeLogPath}</changeLogFile>
185+
</configuration>
171186
</execution>
172187
</executions>
173188
<configuration>
174-
<changeLogFile>src/main/resources/${db.changeLogPath}</changeLogFile>
175189
<driver>org.postgresql.Driver</driver>
176190
<url>jdbc:postgresql://${db.host}:${db.port}/postgres</url>
177191
<username>${db.username}</username>

server/db/src/main/java/com/walmartlabs/concord/db/DatabaseModule.java

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727

2828
import javax.inject.Singleton;
2929
import javax.sql.DataSource;
30+
import java.lang.annotation.Annotation;
3031
import java.util.Comparator;
3132
import java.util.Set;
33+
import java.util.stream.Collectors;
3234

3335
import static com.google.inject.multibindings.Multibinder.newSetBinder;
3436

@@ -47,46 +49,83 @@ public DatabaseModule(boolean migrateDb) {
4749
@Override
4850
public void configure(Binder binder) {
4951
newSetBinder(binder, DatabaseChangeLogProvider.class).addBinding().to(MainDBChangeLogProvider.class);
52+
newSetBinder(binder, DatabaseChangeLogProvider.class).addBinding().to(LogDBChangeLogProvider.class);
5053
}
5154

5255
@Provides
5356
@MainDB
5457
@Singleton
55-
public DataSource appDataSource(@MainDB DatabaseConfiguration cfg,
58+
public DataSource mainDbDataSource(@MainDB DatabaseConfiguration cfg,
59+
MetricRegistry metricRegistry,
60+
Set<DatabaseChangeLogProvider> changeLogProviders) {
61+
62+
var ds = DataSourceUtils.createDataSource(cfg, "app" /* not called "main" for backward compatibility */, cfg.username(), cfg.password(), metricRegistry);
63+
if (migrateDb) {
64+
migrateDb(changeLogProviders, ds, cfg, MainDB.class);
65+
}
66+
return ds;
67+
}
68+
69+
@Provides
70+
@LogDB
71+
@Singleton
72+
public DataSource logDataSource(@LogDB DatabaseConfiguration cfg,
5673
MetricRegistry metricRegistry,
5774
Set<DatabaseChangeLogProvider> changeLogProviders) {
5875

59-
DataSource ds = DataSourceUtils.createDataSource(cfg, "app", cfg.username(), cfg.password(), metricRegistry);
60-
76+
var ds = DataSourceUtils.createDataSource(cfg, "log", cfg.username(), cfg.password(), metricRegistry);
6177
if (migrateDb) {
62-
changeLogProviders.stream()
63-
// can't inject a set of objects with the same qualifier, filter manually
64-
.filter(p -> p.getClass().getAnnotation(MainDB.class) != null)
65-
.sorted(Comparator.comparingInt(DatabaseChangeLogProvider::order))
66-
.forEach(p -> DataSourceUtils.migrateDb(ds, p, cfg.changeLogParameters()));
78+
migrateDb(changeLogProviders, ds, cfg, LogDB.class);
6779
}
68-
6980
return ds;
7081
}
7182

7283
@Provides
7384
@JsonStorageDB
7485
@Singleton
75-
public DataSource inventoryDataSource(@JsonStorageDB DatabaseConfiguration cfg, MetricRegistry metricRegistry) {
86+
public DataSource jsonStorageDbDataSource(@JsonStorageDB DatabaseConfiguration cfg, MetricRegistry metricRegistry) {
7687
return DataSourceUtils.createDataSource(cfg, "inventory", cfg.username(), cfg.password(), metricRegistry);
7788
}
7889

7990
@Provides
8091
@MainDB
8192
@Singleton
82-
public Configuration appJooqConfiguration(@MainDB DataSource ds) {
93+
public Configuration mainDbJooqConfiguration(@MainDB DataSource ds) {
94+
return DataSourceUtils.createJooqConfiguration(ds);
95+
}
96+
97+
@Provides
98+
@LogDB
99+
@Singleton
100+
public Configuration logDbJooqConfiguration(@LogDB DataSource ds) {
83101
return DataSourceUtils.createJooqConfiguration(ds);
84102
}
85103

86104
@Provides
87105
@JsonStorageDB
88106
@Singleton
89-
public Configuration inventoryJooqConfiguration(@JsonStorageDB DataSource ds) {
107+
public Configuration jsonStorageDbJooqConfiguration(@JsonStorageDB DataSource ds) {
90108
return DataSourceUtils.createJooqConfiguration(ds);
91109
}
110+
111+
private static void migrateDb(Set<DatabaseChangeLogProvider> changeLogProviders,
112+
DataSource ds,
113+
DatabaseConfiguration cfg,
114+
Class<? extends Annotation> annotation) {
115+
116+
var providers = changeLogProviders.stream()
117+
// can't inject a set of objects with the same qualifier, filter manually
118+
.filter(p -> p.getClass().getAnnotation(annotation) != null)
119+
.sorted(Comparator.comparingInt(DatabaseChangeLogProvider::order))
120+
.toList();
121+
122+
if (providers.isEmpty()) {
123+
// classpath issue or a bug?
124+
var availableProviders = changeLogProviders.stream().map(DatabaseChangeLogProvider::getChangeLogPath).sorted();
125+
throw new IllegalStateException("Can't find a DatabaseChangeLogProvider for %s (most likely a bug). Available providers: %s"
126+
.formatted(annotation.getName(), availableProviders.collect(Collectors.joining(", "))));
127+
}
128+
129+
providers.forEach(p -> DataSourceUtils.migrateDb(ds, p, cfg.changeLogParameters()));
130+
}
92131
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.walmartlabs.concord.db;
2+
3+
/*-
4+
* *****
5+
* Concord
6+
* -----
7+
* Copyright (C) 2017 - 2019 Walmart Inc.
8+
* -----
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =====
21+
*/
22+
23+
import javax.inject.Qualifier;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
27+
@Retention(RetentionPolicy.RUNTIME)
28+
@Qualifier
29+
public @interface LogDB {
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.walmartlabs.concord.db;
2+
3+
/*-
4+
* *****
5+
* Concord
6+
* -----
7+
* Copyright (C) 2017 - 2024 Walmart Inc.
8+
* -----
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =====
21+
*/
22+
23+
@LogDB
24+
public class LogDBChangeLogProvider implements DatabaseChangeLogProvider {
25+
26+
@Override
27+
public String getChangeLogPath() {
28+
return "com/walmartlabs/concord/server/db/logDb.xml";
29+
}
30+
31+
@Override
32+
public int order() {
33+
// we expect the log DB to be migrated after the main DB
34+
return 1;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "log-db";
40+
}
41+
}

server/db/src/main/java/com/walmartlabs/concord/db/MainDBChangeLogProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ public class MainDBChangeLogProvider implements DatabaseChangeLogProvider {
2525

2626
@Override
2727
public String getChangeLogPath() {
28-
return "com/walmartlabs/concord/server/db/liquibase.xml";
28+
return "com/walmartlabs/concord/server/db/mainDb.xml";
2929
}
3030

3131
@Override
3232
public int order() {
33-
// we expect the server's DB to be migrated first
33+
// we expect the main DB to be migrated first
3434
return 0;
3535
}
3636

3737
@Override
3838
public String toString() {
39-
return "server-db";
39+
return "main-db";
4040
}
4141
}

0 commit comments

Comments
 (0)