Skip to content

Commit 773c210

Browse files
committed
Normalized hostname by concatenating scenarioName with index number
1 parent db0f3ca commit 773c210

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

test/dubbo-scenario-builder/src/main/java/org/apache/dubbo/scenario/builder/ConfigurationImpl.java

+50-12
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public ConfigurationImpl() throws IOException, ConfigureFileNotFoundException {
125125
}
126126
}
127127

128-
this.configuration = loadCaseConfiguration(configureFile);
128+
this.configuration = loadCaseConfiguration(configureFile, scenarioName);
129129

130130
//set scenario timeout
131131
if (this.configuration.getTimeout() > 0) {
@@ -163,7 +163,7 @@ private boolean isDebug() {
163163
return debugPatterns != null && debugPatterns.size() > 0;
164164
}
165165

166-
private CaseConfiguration loadCaseConfiguration(String configureFile) throws IOException {
166+
private CaseConfiguration loadCaseConfiguration(String configureFile, String scenarioName) throws IOException {
167167
// read 'props'
168168
String configYaml = FileUtil.readFully(configureFile);
169169
CaseConfiguration tmpConfiguration = parseConfiguration(configYaml);
@@ -210,7 +210,7 @@ private CaseConfiguration loadCaseConfiguration(String configureFile) throws IOE
210210
caseConfiguration.setServices(newServices);
211211
}
212212

213-
fillupServices(caseConfiguration);
213+
fillupServices(caseConfiguration, scenarioName);
214214
return caseConfiguration;
215215
}
216216

@@ -252,9 +252,15 @@ private CaseConfiguration parseConfiguration(String configYaml) {
252252
return new Yaml().loadAs(configYaml, CaseConfiguration.class);
253253
}
254254

255-
private void fillupServices(CaseConfiguration caseConfiguration) throws IOException {
255+
private void fillupServices(CaseConfiguration caseConfiguration, String scenarioName) throws IOException {
256256
List<String> caseSystemProps = caseConfiguration.getSystemProps();
257257
int index = 0;
258+
//normalize hostname by concatenating scenarioName with index number, hostname length should less than 64.
259+
Map<String, String> hostnameMap = new HashMap<>();
260+
for (String serviceName : caseConfiguration.getServices().keySet()) {
261+
hostnameMap.put(serviceName, scenarioName + index++);
262+
}
263+
index = 0;
258264
for (Map.Entry<String, ServiceComponent> entry : caseConfiguration.getServices().entrySet()) {
259265
String serviceName = entry.getKey();
260266
ServiceComponent service = entry.getValue();
@@ -280,7 +286,7 @@ private void fillupServices(CaseConfiguration caseConfiguration) throws IOExcept
280286

281287
//set wait ports
282288
if (isNotEmpty(service.getWaitPortsBeforeRun())) {
283-
String str = convertAddrPortsToString(service.getWaitPortsBeforeRun());
289+
String str = convertAddrPortsToString(service.getWaitPortsBeforeRun(), hostnameMap);
284290
setEnv(service, ENV_WAIT_PORTS_BEFORE_RUN, str);
285291
}
286292

@@ -351,7 +357,7 @@ private void fillupServices(CaseConfiguration caseConfiguration) throws IOExcept
351357
//set check ports
352358
if (isNotEmpty(service.getCheckPorts())) {
353359
addHealthcheck = true;
354-
String str = convertAddrPortsToString(service.getCheckPorts());
360+
String str = convertAddrPortsToString(service.getCheckPorts(), hostnameMap);
355361
setEnv(service, ENV_CHECK_PORTS, str);
356362
}
357363

@@ -395,7 +401,7 @@ private void fillupServices(CaseConfiguration caseConfiguration) throws IOExcept
395401

396402
//set wait ports
397403
if (isNotEmpty(service.getWaitPortsBeforeRun())) {
398-
String str = convertAddrPortsToString(service.getWaitPortsBeforeRun());
404+
String str = convertAddrPortsToString(service.getWaitPortsBeforeRun(), hostnameMap);
399405
setEnv(service, ENV_WAIT_PORTS_BEFORE_RUN, str);
400406
}
401407

@@ -423,7 +429,7 @@ private void fillupServices(CaseConfiguration caseConfiguration) throws IOExcept
423429
//set check ports
424430
if (isNotEmpty(service.getCheckPorts())) {
425431
addHealthcheck = true;
426-
String str = convertAddrPortsToString(service.getCheckPorts());
432+
String str = convertAddrPortsToString(service.getCheckPorts(), hostnameMap);
427433
setEnv(service, ENV_CHECK_PORTS, str);
428434
}
429435

@@ -436,7 +442,8 @@ private void fillupServices(CaseConfiguration caseConfiguration) throws IOExcept
436442

437443
// set hostname to serviceId if absent
438444
if (StringUtils.isBlank(service.getHostname())) {
439-
service.setHostname(serviceName);
445+
//use normalized hostname.
446+
service.setHostname(hostnameMap.get(serviceName));
440447
}
441448

442449
//set jvmFlags
@@ -445,6 +452,27 @@ private void fillupServices(CaseConfiguration caseConfiguration) throws IOExcept
445452
appendEnv(service, ENV_JAVA_OPTS, str);
446453
}
447454

455+
//revise hostname properties of service
456+
List<String> serviceSystemProps = service.getSystemProps();
457+
if (serviceSystemProps != null) {
458+
List<String> revisedServiceSystemProps = new ArrayList<>();
459+
serviceSystemProps.forEach(serviceProp -> {
460+
String[] strs = serviceProp.split("=");
461+
if (strs.length == 2) {
462+
String hostname = hostnameMap.get(strs[1]);
463+
if (null != hostname) {
464+
//use normalized hostname.
465+
revisedServiceSystemProps.add(strs[0] + "=" + hostname);
466+
} else {
467+
revisedServiceSystemProps.add(serviceProp);
468+
}
469+
} else {
470+
revisedServiceSystemProps.add(serviceProp);
471+
}
472+
});
473+
service.setSystemProps(revisedServiceSystemProps);
474+
}
475+
448476
//set systemProps
449477
List<String> systemProps = mergeSystemProps(caseSystemProps, service.getSystemProps());
450478
if (isNotEmpty(systemProps)) {
@@ -526,13 +554,23 @@ private boolean isNotEmpty(List<String> list) {
526554
return list != null && list.size() > 0;
527555
}
528556

529-
private String convertAddrPortsToString(List<String> addrPorts) {
557+
private String convertAddrPortsToString(List<String> addrPorts, Map<String, String> hostnameMap) {
530558
StringBuilder sb = new StringBuilder();
531559
for (String addrPort : addrPorts) {
532-
if (!addrPort.contains(":")) {
560+
int idx = addrPort.indexOf(":");
561+
if (idx > 0) {
562+
String hostname = hostnameMap.get(addrPort.substring(0, idx));
563+
if (null != hostname) {
564+
// use normalized hostname.
565+
sb.append(hostname).append(addrPort.substring(idx));
566+
} else {
567+
sb.append(addrPort);
568+
}
569+
} else {
533570
addrPort = "127.0.0.1" + ":" + addrPort;
571+
sb.append(addrPort);
534572
}
535-
sb.append(addrPort).append(";");
573+
sb.append(";");
536574
}
537575
return sb.toString();
538576
}

0 commit comments

Comments
 (0)