Skip to content

Commit 8f6a793

Browse files
authored
CTR needs to support boot property styles for the tablePrefix property (spring-attic#5857)
* CTR needs to support boot property styles for the tablePrefix property * Add RelaxedNames support for identifying prefixes for Task Explorer schemas Also resolved a bug where if a user specified the prefix on CTR for the apps instead of the apps themselves those props would be ignored
1 parent aa863b5 commit 8f6a793

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

spring-cloud-dataflow-composed-task-runner/src/main/java/org/springframework/cloud/dataflow/composedtaskrunner/ComposedTaskRunnerConfiguration.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package org.springframework.cloud.dataflow.composedtaskrunner;
1818

1919
import javax.sql.DataSource;
20+
import java.util.ArrayList;
2021
import java.util.HashMap;
22+
import java.util.List;
2123
import java.util.Map;
24+
import java.util.Objects;
2225
import java.util.Set;
2326

2427
import org.slf4j.Logger;
@@ -31,8 +34,10 @@
3134
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
3235
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3336
import org.springframework.cloud.dataflow.composedtaskrunner.properties.ComposedTaskProperties;
37+
import org.springframework.cloud.dataflow.core.RelaxedNames;
3438
import org.springframework.cloud.dataflow.core.database.support.MultiSchemaTaskExecutionDaoFactoryBean;
3539
import org.springframework.cloud.dataflow.core.dsl.TaskParser;
40+
import org.springframework.cloud.dataflow.rest.util.DeploymentPropertiesUtils;
3641
import org.springframework.cloud.task.configuration.EnableTask;
3742
import org.springframework.cloud.task.listener.TaskExecutionListener;
3843
import org.springframework.cloud.task.repository.TaskExplorer;
@@ -102,17 +107,28 @@ private static void addTaskExplorer(
102107
String taskName
103108
) {
104109
logger.debug("addTaskExplorer:{}", taskName);
105-
String propertyName = String.format("app.%s.spring.cloud.task.tablePrefix", taskName);
106-
String prefix = properties.getComposedTaskAppProperties().get(propertyName);
107-
if (prefix == null) {
108-
prefix = env.getProperty(propertyName);
109-
}
110+
List<String> propertyNames = new ArrayList<>();
111+
RelaxedNames relaxedNames = RelaxedNames.forCamelCase("tablePrefix");
112+
relaxedNames.forEach(tablePrefix -> propertyNames.add(
113+
String.format("app.%s.spring.cloud.task.%s", taskName, tablePrefix)));
114+
Map<String, String> taskDeploymentProperties =
115+
DeploymentPropertiesUtils.parse(properties.getComposedTaskProperties());
116+
String prefix = propertyNames.stream()
117+
.map(propertyName -> {
118+
String prefixOfComposedTaskProperties = taskDeploymentProperties.get(propertyName);
119+
if(prefixOfComposedTaskProperties == null) {
120+
prefixOfComposedTaskProperties = properties.getComposedTaskAppProperties().get(propertyName);
121+
}
122+
return prefixOfComposedTaskProperties == null ? env.getProperty(propertyName) : prefixOfComposedTaskProperties;
123+
})
124+
.filter(Objects::nonNull)
125+
.findFirst().orElse(null);
110126
if (prefix != null) {
111127
TaskExecutionDaoFactoryBean factoryBean = new MultiSchemaTaskExecutionDaoFactoryBean(dataSource, prefix);
112-
logger.debug("taskExplorerContainer:adding:{}:{}", taskName, prefix);
128+
logger.info("taskExplorerContainer:adding:{}:{}", taskName, prefix);
113129
explorers.put(taskName, new SimpleTaskExplorer(factoryBean));
114130
} else {
115-
logger.warn("Cannot find {} in {} ", propertyName, properties.getComposedTaskAppProperties());
131+
logger.warn("Cannot find {} in {} ", propertyNames, properties.getComposedTaskAppProperties());
116132
}
117133
}
118134

spring-cloud-dataflow-composed-task-runner/src/test/java/org/springframework/cloud/dataflow/composedtaskrunner/ComposedTaskRunnerConfigurationNoPropertiesTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24+
import org.junit.jupiter.api.extension.ExtendWith;
2425
import org.springframework.batch.core.Job;
2526
import org.springframework.batch.core.JobExecution;
2627
import org.springframework.batch.core.JobParameters;
@@ -29,6 +30,8 @@
2930
import org.springframework.beans.factory.annotation.Autowired;
3031
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3132
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
33+
import org.springframework.boot.test.system.CapturedOutput;
34+
import org.springframework.boot.test.system.OutputCaptureExtension;
3235
import org.springframework.cloud.common.security.CommonSecurityAutoConfiguration;
3336
import org.springframework.cloud.dataflow.composedtaskrunner.configuration.DataFlowTestConfiguration;
3437
import org.springframework.cloud.dataflow.composedtaskrunner.properties.ComposedTaskProperties;
@@ -53,6 +56,7 @@
5356
StepBeanDefinitionRegistrar.class})
5457
@TestPropertySource(properties = {"graph=AAA && BBB && CCC", "max-wait-time=1000", "spring.cloud.task.name=foo"})
5558
@EnableAutoConfiguration(exclude = {CommonSecurityAutoConfiguration.class})
59+
@ExtendWith(OutputCaptureExtension.class)
5660
public class ComposedTaskRunnerConfigurationNoPropertiesTests {
5761

5862
@Autowired
@@ -69,7 +73,7 @@ public class ComposedTaskRunnerConfigurationNoPropertiesTests {
6973

7074
@Test
7175
@DirtiesContext
72-
public void testComposedConfiguration() throws Exception {
76+
public void testComposedConfiguration(CapturedOutput outputCapture) throws Exception {
7377
JobExecution jobExecution = this.jobRepository.createJobExecution(
7478
"ComposedTest", new JobParameters());
7579
TaskletStep ctrStep = context.getBean("AAA_0", TaskletStep.class);
@@ -85,5 +89,12 @@ public void testComposedConfiguration() throws Exception {
8589
Collections.emptyMap(),
8690
Arrays.asList("--spring.cloud.task.parent-execution-id=1", "--spring.cloud.task.parent-schema-target=boot2")
8791
);
92+
93+
String logEntries = outputCapture.toString();
94+
assertThat(logEntries).contains("Cannot find [app.AAA.spring.cloud.task.table-prefix, " +
95+
"app.AAA.spring.cloud.task.table_prefix, app.AAA.spring.cloud.task.tablePrefix, " +
96+
"app.AAA.spring.cloud.task.tableprefix, app.AAA.spring.cloud.task.TABLE-PREFIX, " +
97+
"app.AAA.spring.cloud.task.TABLE_PREFIX, app.AAA.spring.cloud.task.TABLEPREFIX]");
98+
assertThat(logEntries).doesNotContain("taskExplorerContainer:adding:");
8899
}
89100
}

spring-cloud-dataflow-composed-task-runner/src/test/java/org/springframework/cloud/dataflow/composedtaskrunner/ComposedTaskRunnerConfigurationWithPropertiesTests.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.junit.jupiter.api.Test;
2525

26+
import org.junit.jupiter.api.extension.ExtendWith;
2627
import org.springframework.batch.core.Job;
2728
import org.springframework.batch.core.JobExecution;
2829
import org.springframework.batch.core.JobParameters;
@@ -31,6 +32,8 @@
3132
import org.springframework.beans.factory.annotation.Autowired;
3233
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3334
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
35+
import org.springframework.boot.test.system.CapturedOutput;
36+
import org.springframework.boot.test.system.OutputCaptureExtension;
3437
import org.springframework.cloud.common.security.CommonSecurityAutoConfiguration;
3538
import org.springframework.cloud.dataflow.composedtaskrunner.configuration.DataFlowTestConfiguration;
3639
import org.springframework.cloud.dataflow.composedtaskrunner.properties.ComposedTaskProperties;
@@ -60,6 +63,7 @@
6063
"transaction-isolation-level=ISOLATION_READ_COMMITTED","spring.cloud.task.closecontext-enabled=true",
6164
"dataflow-server-uri=https://bar", "spring.cloud.task.name=ComposedTest","max-start-wait-time=1011"})
6265
@EnableAutoConfiguration(exclude = { CommonSecurityAutoConfiguration.class})
66+
@ExtendWith(OutputCaptureExtension.class)
6367
public class ComposedTaskRunnerConfigurationWithPropertiesTests {
6468

6569
@Autowired
@@ -78,12 +82,15 @@ public class ComposedTaskRunnerConfigurationWithPropertiesTests {
7882
ApplicationContext context;
7983

8084
protected static final String COMPOSED_TASK_PROPS = "app.ComposedTest-AAA.format=yyyy, "
81-
+ "app.ComposedTest-BBB.format=mm, "
82-
+ "deployer.ComposedTest-AAA.memory=2048m";
85+
+ "app.ComposedTest-AAA.spring.cloud.task.table-prefix=BOOT3_,"
86+
+ "app.ComposedTest-BBB.spring.cloud.task.tableprefix=BOOT3_,"
87+
+ "app.ComposedTest-CCC.spring.cloud.task.tablePrefix=BOOT3_,"
88+
+ "app.ComposedTest-BBB.format=mm, "
89+
+ "deployer.ComposedTest-AAA.memory=2048m";
8390

8491
@Test
8592
@DirtiesContext
86-
public void testComposedConfiguration() throws Exception {
93+
public void testComposedConfiguration(CapturedOutput outputCapture) throws Exception {
8794
assertThat(composedTaskProperties.isSkipTlsCertificateVerification()).isFalse();
8895

8996
JobExecution jobExecution = this.jobRepository.createJobExecution(
@@ -97,6 +104,8 @@ public void testComposedConfiguration() throws Exception {
97104
Map<String, String> props = new HashMap<>(1);
98105
props.put("format", "yyyy");
99106
props.put("memory", "2048m");
107+
props.put("spring.cloud.task.table-prefix", "BOOT3_");
108+
100109
assertThat(composedTaskProperties.getComposedTaskProperties()).isEqualTo(COMPOSED_TASK_PROPS);
101110
assertThat(composedTaskProperties.getMaxWaitTime()).isEqualTo(1010);
102111
assertThat(composedTaskProperties.getMaxStartWaitTime()).isEqualTo(1011);
@@ -110,6 +119,14 @@ public void testComposedConfiguration() throws Exception {
110119
args.add("--spring.cloud.task.parent-execution-id=1");
111120
args.add("--spring.cloud.task.parent-schema-target=boot2");
112121
Assert.notNull(job.getJobParametersIncrementer(), "JobParametersIncrementer must not be null.");
122+
113123
verify(taskOperations).launch("ComposedTest-AAA", props, args);
124+
125+
String logEntries = outputCapture.toString();
126+
assertThat(logEntries).contains("taskExplorerContainer:adding:ComposedTest-AAA:BOOT3_");
127+
128+
assertThat(logEntries).contains("taskExplorerContainer:adding:ComposedTest-BBB:BOOT3_");
129+
assertThat(logEntries).contains("taskExplorerContainer:adding:ComposedTest-CCC:BOOT3_");
130+
114131
}
115132
}

0 commit comments

Comments
 (0)