-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab06f4a
commit 4e81c6c
Showing
39 changed files
with
1,518 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Apply the java-library plugin to add support for Java Library | ||
plugins { | ||
id 'java' | ||
id 'com.github.sherter.google-java-format' | ||
} | ||
dependencies{ | ||
compile project(":wedpr-core-utils") | ||
} | ||
googleJavaFormat { | ||
//toolVersion = '1.7' | ||
options style: 'AOSP' | ||
source = sourceSets*.allJava | ||
include '**/*.java' | ||
//source = *.allJava | ||
} |
32 changes: 32 additions & 0 deletions
32
wedpr-components/spi/src/main/java/com/webank/wedpr/components/spi/plugin/SPIInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2017-2025 [webank-wedpr] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
package com.webank.wedpr.components.spi.plugin; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class SPIInfo { | ||
private String name; | ||
private Integer priority = 0; | ||
|
||
public SPIInfo(String name) { | ||
this.name = name; | ||
} | ||
|
||
public SPIInfo(String name, Integer priority) { | ||
this.name = name; | ||
this.priority = priority; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
wedpr-components/spi/src/main/java/com/webank/wedpr/components/spi/plugin/SPILoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2017-2025 [webank-wedpr] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
package com.webank.wedpr.components.spi.plugin; | ||
|
||
import com.webank.wedpr.core.utils.WeDPRException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
import lombok.SneakyThrows; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class SPILoader<T extends SPIObject> { | ||
private static final Logger logger = LoggerFactory.getLogger(SPILoader.class); | ||
private final Map<String, T> spiObjectMap = new HashMap<>(); | ||
|
||
@SneakyThrows(Exception.class) | ||
public SPILoader(Class<T> spiClass) { | ||
for (T spiObject : ServiceLoader.load(spiClass)) { | ||
// load new spi object | ||
if (!spiObjectMap.containsKey(spiObject.getSpiInfo().getName())) { | ||
spiObjectMap.put(spiObject.getSpiInfo().getName(), spiObject); | ||
continue; | ||
} | ||
// spi conflict, load the higher priority object | ||
T existedSPIObject = spiObjectMap.get(spiObject.getSpiInfo().getName()); | ||
if (existedSPIObject.equals(spiObject)) { | ||
String errorMsg = | ||
String.format( | ||
"load spi failed for conflict, there are two spi objects with same name '%s' and priority '%s'", | ||
spiObject.getSpiInfo().getName(), | ||
spiObject.getSpiInfo().getPriority()); | ||
logger.error(errorMsg); | ||
throw new WeDPRException(errorMsg); | ||
} | ||
if (existedSPIObject.compareTo(spiObject) > 0) { | ||
logger.info( | ||
"load new spi object with higher priority, name: {}, oldPriority: {}, newPriority: {}", | ||
spiObject.getSpiInfo().getName(), | ||
existedSPIObject.getSpiInfo().getPriority(), | ||
spiObject.getSpiInfo().getPriority()); | ||
spiObjectMap.put(spiObject.getSpiInfo().getName(), spiObject); | ||
continue; | ||
} | ||
logger.info( | ||
"Ignore spi object with lower priority, name: {}, priority: {}, currentPriority: {}", | ||
spiObject.getSpiInfo().getName(), | ||
spiObject.getSpiInfo().getPriority(), | ||
existedSPIObject.getSpiInfo().getPriority()); | ||
} | ||
} | ||
|
||
public Map<String, T> getSpiObjectMap() { | ||
return spiObjectMap; | ||
} | ||
|
||
public T getSPIObjectByName(String spiName) { | ||
if (!spiObjectMap.containsKey(spiName)) { | ||
return null; | ||
} | ||
return spiObjectMap.get(spiName); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
wedpr-components/spi/src/main/java/com/webank/wedpr/components/spi/plugin/SPIObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2017-2025 [webank-wedpr] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
package com.webank.wedpr.components.spi.plugin; | ||
|
||
public class SPIObject implements Comparable<SPIObject> { | ||
private SPIInfo spiInfo; | ||
|
||
public SPIObject(SPIInfo spiInfo) { | ||
this.spiInfo = spiInfo; | ||
} | ||
|
||
public SPIObject() {} | ||
|
||
public SPIInfo getSpiInfo() { | ||
return spiInfo; | ||
} | ||
|
||
public void setSpiInfo(SPIInfo spiInfo) { | ||
this.spiInfo = spiInfo; | ||
} | ||
|
||
@Override | ||
public int compareTo(SPIObject o) { | ||
return spiInfo.getPriority().compareTo(o.getSpiInfo().getPriority()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Apply the java-library plugin to add support for Java Library | ||
plugins { | ||
id 'java' | ||
id 'com.github.sherter.google-java-format' | ||
} | ||
dependencies{ | ||
compile project(":wedpr-core-utils") | ||
compile project(":wedpr-components-spi") | ||
} | ||
googleJavaFormat { | ||
//toolVersion = '1.7' | ||
options style: 'AOSP' | ||
source = sourceSets*.allJava | ||
include '**/*.java' | ||
//source = *.allJava | ||
} |
108 changes: 108 additions & 0 deletions
108
...plugin/api/src/main/java/com/webank/wedpr/components/task/plugin/api/CommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2017-2025 [webank-wedpr] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
package com.webank.wedpr.components.task.plugin.api; | ||
|
||
import com.webank.wedpr.components.task.plugin.api.model.CommandTaskConfig; | ||
import com.webank.wedpr.components.task.plugin.api.model.CommandTaskExecutionContext; | ||
import com.webank.wedpr.components.task.plugin.api.model.CommandTaskResponse; | ||
import com.webank.wedpr.components.task.plugin.api.shell.ShellBuilder; | ||
import com.webank.wedpr.core.utils.Common; | ||
import com.webank.wedpr.core.utils.Constant; | ||
import com.webank.wedpr.core.utils.WeDPRException; | ||
import java.util.concurrent.TimeUnit; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class CommandExecutor implements WorkerExecutor { | ||
private static final Logger logger = LoggerFactory.getLogger(CommandExecutor.class); | ||
protected final CommandTaskExecutionContext context; | ||
|
||
public CommandExecutor(CommandTaskExecutionContext taskExecutionContext) { | ||
this.context = taskExecutionContext; | ||
} | ||
|
||
@Override | ||
public CommandTaskResponse run(Object builder) throws Exception { | ||
ShellBuilder shellBuilder = (ShellBuilder) builder; | ||
// set the builder information | ||
shellBuilder.context(context); | ||
// set the system envs | ||
if (!CommandTaskConfig.getSystemEnvFiles().isEmpty()) { | ||
CommandTaskConfig.getSystemEnvFiles().forEach(shellBuilder::appendSystemEnv); | ||
} | ||
// set the environment | ||
if (StringUtils.isNotBlank(context.getEnvironmentConfig())) { | ||
shellBuilder.appendCustomEnv(context.getEnvironmentConfig()); | ||
} | ||
long remainTime = -1; | ||
if (this.context.getTaskTimeoutMs() > 0) { | ||
remainTime = (System.currentTimeMillis() - this.context.getStartTime()); | ||
if (remainTime < 0) { | ||
throw new WeDPRException("task execution timeout"); | ||
} | ||
} | ||
// build the launcher | ||
CommandTaskResponse taskResponse = new CommandTaskResponse(this.context.getTaskID()); | ||
Process process = shellBuilder.build().execute(); | ||
taskResponse.setProcess(process); | ||
this.context.setProcess(process); | ||
int processId = Common.getProcessId(process); | ||
taskResponse.setProcessId(processId); | ||
this.context.setProcessId(processId); | ||
|
||
logger.info("bootstrap process start, process id: {}", processId); | ||
// wait for finish | ||
boolean exitNormally = Boolean.FALSE; | ||
if (remainTime > 0) { | ||
exitNormally = process.waitFor(remainTime, TimeUnit.MILLISECONDS); | ||
} else { | ||
exitNormally = (process.waitFor() == 0 ? true : false); | ||
} | ||
if (exitNormally) { | ||
taskResponse.setExitCode(process.exitValue()); | ||
} else { | ||
// kill the command | ||
logger.error( | ||
"process has failure, over the task timeout configuration {}, processId: {}, ready to kill", | ||
context.getTaskTimeoutMs(), | ||
processId); | ||
kill(); | ||
taskResponse.setExitCode(Constant.WEDPR_FAILED); | ||
} | ||
logger.info( | ||
"execute process finished, executePath: {}, process: {}, exitCode: {}, exitNormally: {}", | ||
this.context.getExecutePath(), | ||
processId, | ||
process.exitValue(), | ||
exitNormally); | ||
return taskResponse; | ||
} | ||
|
||
@Override | ||
public void kill() throws Exception { | ||
if (this.context.getProcess() == null) { | ||
return; | ||
} | ||
logger.info("Ready to kill process: {}", this.context.getProcessId()); | ||
this.context.getProcess().destroy(); | ||
if (!this.context | ||
.getProcess() | ||
.waitFor(CommandTaskConfig.getKillDefaultTimeoutSeconds(), TimeUnit.SECONDS)) { | ||
this.context.getProcess().destroyForcibly(); | ||
} | ||
logger.info("Success kill process: {}", this.context.getProcessId()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ask-plugin/api/src/main/java/com/webank/wedpr/components/task/plugin/api/TaskBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2017-2025 [webank-wedpr] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
package com.webank.wedpr.components.task.plugin.api; | ||
|
||
public interface TaskBuilder { | ||
public abstract TaskInterface createTask(TaskExecutionContext taskExecutionContext); | ||
} |
24 changes: 24 additions & 0 deletions
24
...gin/api/src/main/java/com/webank/wedpr/components/task/plugin/api/TaskBuilderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2017-2025 [webank-wedpr] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
package com.webank.wedpr.components.task.plugin.api; | ||
|
||
import com.webank.wedpr.components.spi.plugin.SPIObject; | ||
|
||
public abstract class TaskBuilderFactory extends SPIObject { | ||
|
||
public abstract String getName(); | ||
|
||
public abstract TaskBuilder createTaskBuilder(); | ||
} |
Oops, something went wrong.