mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
refactor: optimize NetUtils
This commit is contained in:
parent
6de5e83a2f
commit
61aecc6354
@ -94,6 +94,14 @@ public class NetUtils {
|
|||||||
return LOCALHOST_VALUE;
|
return LOCALHOST_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 隔离调用 scope,核心场景才能直接调用 getLocalHost,方便查看使用点
|
||||||
|
* @return IP
|
||||||
|
*/
|
||||||
|
public static String getLocalHost4Test() {
|
||||||
|
return getLocalHost();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find first valid IP from local network card
|
* Find first valid IP from local network card
|
||||||
*
|
*
|
||||||
|
@ -12,19 +12,19 @@ public class NetUtilsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOrigin() {
|
public void testOrigin() {
|
||||||
System.out.println(NetUtils.getLocalHost());
|
System.out.println(NetUtils.getLocalHost4Test());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPreferredNetworkInterface() {
|
public void testPreferredNetworkInterface() {
|
||||||
System.setProperty(PowerJobDKey.PREFERRED_NETWORK_INTERFACE, "en5");
|
System.setProperty(PowerJobDKey.PREFERRED_NETWORK_INTERFACE, "en5");
|
||||||
System.out.println(NetUtils.getLocalHost());
|
System.out.println(NetUtils.getLocalHost4Test());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIgnoredNetworkInterface() {
|
public void testIgnoredNetworkInterface() {
|
||||||
System.setProperty(PowerJobDKey.IGNORED_NETWORK_INTERFACE_REGEX, "utun.|llw.");
|
System.setProperty(PowerJobDKey.IGNORED_NETWORK_INTERFACE_REGEX, "utun.|llw.");
|
||||||
System.out.println(NetUtils.getLocalHost());
|
System.out.println(NetUtils.getLocalHost4Test());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -123,14 +123,14 @@ public class VerificationProcessor extends CommonBasicProcessor implements MapRe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ProcessResult preProcess(TaskContext context) throws Exception {
|
public ProcessResult preProcess(TaskContext context) throws Exception {
|
||||||
context.getOmsLogger().info("start to preProcess, current worker IP is {}.", NetUtils.getLocalHost());
|
context.getOmsLogger().info("start to preProcess, current worker IP is {}.", NetUtils.getLocalHost4Test());
|
||||||
return new ProcessResult(true, "preProcess successfully!");
|
return new ProcessResult(true, "preProcess successfully!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ProcessResult postProcess(TaskContext context, List<TaskResult> taskResults) throws Exception {
|
public ProcessResult postProcess(TaskContext context, List<TaskResult> taskResults) throws Exception {
|
||||||
OmsLogger omsLogger = context.getOmsLogger();
|
OmsLogger omsLogger = context.getOmsLogger();
|
||||||
omsLogger.info("start to postProcess, current worker IP is {}.", NetUtils.getLocalHost());
|
omsLogger.info("start to postProcess, current worker IP is {}.", NetUtils.getLocalHost4Test());
|
||||||
omsLogger.info("====== All Node's Process Result ======");
|
omsLogger.info("====== All Node's Process Result ======");
|
||||||
taskResults.forEach(r -> omsLogger.info("taskId:{},success:{},result:{}", r.getTaskId(), r.isSuccess(), r.getResult()));
|
taskResults.forEach(r -> omsLogger.info("taskId:{},success:{},result:{}", r.getTaskId(), r.isSuccess(), r.getResult()));
|
||||||
return new ProcessResult(true, "postProcess successfully!");
|
return new ProcessResult(true, "postProcess successfully!");
|
||||||
|
@ -6,6 +6,8 @@ import org.springframework.beans.factory.DisposableBean;
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
import tech.powerjob.server.common.aware.ServerInfoAware;
|
||||||
|
import tech.powerjob.server.common.module.ServerInfo;
|
||||||
import tech.powerjob.server.extension.dfs.DFsService;
|
import tech.powerjob.server.extension.dfs.DFsService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -15,8 +17,9 @@ import tech.powerjob.server.extension.dfs.DFsService;
|
|||||||
* @since 2023/7/28
|
* @since 2023/7/28
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public abstract class AbstractDFsService implements DFsService, ApplicationContextAware, DisposableBean {
|
public abstract class AbstractDFsService implements DFsService, ApplicationContextAware, ServerInfoAware, DisposableBean {
|
||||||
|
|
||||||
|
protected ServerInfo serverInfo;
|
||||||
protected ApplicationContext applicationContext;
|
protected ApplicationContext applicationContext;
|
||||||
|
|
||||||
public AbstractDFsService() {
|
public AbstractDFsService() {
|
||||||
@ -38,4 +41,9 @@ public abstract class AbstractDFsService implements DFsService, ApplicationConte
|
|||||||
log.info("[DFsService] invoke [{}]'s setApplicationContext", this.getClass().getName());
|
log.info("[DFsService] invoke [{}]'s setApplicationContext", this.getClass().getName());
|
||||||
init(applicationContext);
|
init(applicationContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setServerInfo(ServerInfo serverInfo) {
|
||||||
|
this.serverInfo = serverInfo;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ public class MySqlSeriesDfsService extends AbstractDFsService {
|
|||||||
deleteByLocation(fileLocation);
|
deleteByLocation(fileLocation);
|
||||||
|
|
||||||
Map<String, Object> meta = Maps.newHashMap();
|
Map<String, Object> meta = Maps.newHashMap();
|
||||||
meta.put("_server_", NetUtils.getLocalHost());
|
meta.put("_server_", serverInfo.getIp());
|
||||||
meta.put("_local_file_path_", storeRequest.getLocalFile().getAbsolutePath());
|
meta.put("_local_file_path_", storeRequest.getLocalFile().getAbsolutePath());
|
||||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(Files.newInputStream(storeRequest.getLocalFile().toPath()));
|
BufferedInputStream bufferedInputStream = new BufferedInputStream(Files.newInputStream(storeRequest.getLocalFile().toPath()));
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ public class ServerController implements ServerInfoAware {
|
|||||||
@GetMapping("/hello")
|
@GetMapping("/hello")
|
||||||
public ResultDTO<JSONObject> ping(@RequestParam(required = false) boolean debug) {
|
public ResultDTO<JSONObject> ping(@RequestParam(required = false) boolean debug) {
|
||||||
JSONObject res = new JSONObject();
|
JSONObject res = new JSONObject();
|
||||||
res.put("localHost", NetUtils.getLocalHost());
|
res.put("localHost", serverInfo.getIp());
|
||||||
res.put("serverInfo", serverInfo);
|
res.put("serverInfo", serverInfo);
|
||||||
res.put("serverTime", CommonUtils.formatTime(System.currentTimeMillis()));
|
res.put("serverTime", CommonUtils.formatTime(System.currentTimeMillis()));
|
||||||
res.put("serverTimeTs", System.currentTimeMillis());
|
res.put("serverTimeTs", System.currentTimeMillis());
|
||||||
|
@ -53,7 +53,7 @@ public class RepositoryTest {
|
|||||||
public void testBatchLock() {
|
public void testBatchLock() {
|
||||||
List<OmsLockDO> locks = Lists.newArrayList();
|
List<OmsLockDO> locks = Lists.newArrayList();
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
OmsLockDO lockDO = new OmsLockDO("lock" + i, NetUtils.getLocalHost(), 10000L);
|
OmsLockDO lockDO = new OmsLockDO("lock" + i, NetUtils.getLocalHost4Test(), 10000L);
|
||||||
locks.add(lockDO);
|
locks.add(lockDO);
|
||||||
}
|
}
|
||||||
omsLockRepository.saveAll(locks);
|
omsLockRepository.saveAll(locks);
|
||||||
@ -63,7 +63,7 @@ public class RepositoryTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testDeleteLock() {
|
public void testDeleteLock() {
|
||||||
String lockName = "test-lock";
|
String lockName = "test-lock";
|
||||||
OmsLockDO lockDO = new OmsLockDO(lockName, NetUtils.getLocalHost(), 10000L);
|
OmsLockDO lockDO = new OmsLockDO(lockName, NetUtils.getLocalHost4Test(), 10000L);
|
||||||
omsLockRepository.save(lockDO);
|
omsLockRepository.save(lockDO);
|
||||||
omsLockRepository.deleteByLockName(lockName);
|
omsLockRepository.deleteByLockName(lockName);
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ public class BroadcastProcessorDemo implements BroadcastProcessor {
|
|||||||
@Override
|
@Override
|
||||||
public ProcessResult preProcess(TaskContext context) {
|
public ProcessResult preProcess(TaskContext context) {
|
||||||
System.out.println("===== BroadcastProcessorDemo#preProcess ======");
|
System.out.println("===== BroadcastProcessorDemo#preProcess ======");
|
||||||
context.getOmsLogger().info("BroadcastProcessorDemo#preProcess, current host: {}", NetUtils.getLocalHost());
|
context.getOmsLogger().info("BroadcastProcessorDemo#preProcess, current host: {}", NetUtils.getLocalHost4Test());
|
||||||
if ("rootFailed".equals(context.getJobParams())) {
|
if ("rootFailed".equals(context.getJobParams())) {
|
||||||
return new ProcessResult(false, "console need failed");
|
return new ProcessResult(false, "console need failed");
|
||||||
} else {
|
} else {
|
||||||
@ -36,7 +36,7 @@ public class BroadcastProcessorDemo implements BroadcastProcessor {
|
|||||||
public ProcessResult process(TaskContext taskContext) throws Exception {
|
public ProcessResult process(TaskContext taskContext) throws Exception {
|
||||||
OmsLogger logger = taskContext.getOmsLogger();
|
OmsLogger logger = taskContext.getOmsLogger();
|
||||||
System.out.println("===== BroadcastProcessorDemo#process ======");
|
System.out.println("===== BroadcastProcessorDemo#process ======");
|
||||||
logger.info("BroadcastProcessorDemo#process, current host: {}", NetUtils.getLocalHost());
|
logger.info("BroadcastProcessorDemo#process, current host: {}", NetUtils.getLocalHost4Test());
|
||||||
long sleepTime = 1000;
|
long sleepTime = 1000;
|
||||||
try {
|
try {
|
||||||
sleepTime = Long.parseLong(taskContext.getJobParams());
|
sleepTime = Long.parseLong(taskContext.getJobParams());
|
||||||
@ -50,7 +50,7 @@ public class BroadcastProcessorDemo implements BroadcastProcessor {
|
|||||||
@Override
|
@Override
|
||||||
public ProcessResult postProcess(TaskContext context, List<TaskResult> taskResults) {
|
public ProcessResult postProcess(TaskContext context, List<TaskResult> taskResults) {
|
||||||
System.out.println("===== BroadcastProcessorDemo#postProcess ======");
|
System.out.println("===== BroadcastProcessorDemo#postProcess ======");
|
||||||
context.getOmsLogger().info("BroadcastProcessorDemo#postProcess, current host: {}, taskResult: {}", NetUtils.getLocalHost(), taskResults);
|
context.getOmsLogger().info("BroadcastProcessorDemo#postProcess, current host: {}, taskResult: {}", NetUtils.getLocalHost4Test(), taskResults);
|
||||||
return new ProcessResult(true, "success");
|
return new ProcessResult(true, "success");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ public class CommonTest {
|
|||||||
|
|
||||||
TaskTrackerStartTaskReq req = new TaskTrackerStartTaskReq();
|
TaskTrackerStartTaskReq req = new TaskTrackerStartTaskReq();
|
||||||
|
|
||||||
req.setTaskTrackerAddress(NetUtils.getLocalHost() + ":27777");
|
req.setTaskTrackerAddress(NetUtils.getLocalHost4Test() + ":27777");
|
||||||
req.setInstanceInfo(instanceInfo);
|
req.setInstanceInfo(instanceInfo);
|
||||||
|
|
||||||
req.setTaskId("0");
|
req.setTaskId("0");
|
||||||
|
@ -40,7 +40,7 @@ public class PersistenceServiceTest {
|
|||||||
task.setFailedCnt(0);
|
task.setFailedCnt(0);
|
||||||
task.setStatus(TaskStatus.WORKER_RECEIVED.getValue());
|
task.setStatus(TaskStatus.WORKER_RECEIVED.getValue());
|
||||||
task.setTaskName("ROOT_TASK");
|
task.setTaskName("ROOT_TASK");
|
||||||
task.setAddress(NetUtils.getLocalHost());
|
task.setAddress(NetUtils.getLocalHost4Test());
|
||||||
task.setLastModifiedTime(System.currentTimeMillis());
|
task.setLastModifiedTime(System.currentTimeMillis());
|
||||||
task.setCreatedTime(System.currentTimeMillis());
|
task.setCreatedTime(System.currentTimeMillis());
|
||||||
task.setLastReportTime(System.currentTimeMillis());
|
task.setLastReportTime(System.currentTimeMillis());
|
||||||
@ -70,7 +70,7 @@ public class PersistenceServiceTest {
|
|||||||
task.setFailedCnt(0);
|
task.setFailedCnt(0);
|
||||||
task.setStatus(TaskStatus.WORKER_RECEIVED.getValue());
|
task.setStatus(TaskStatus.WORKER_RECEIVED.getValue());
|
||||||
task.setTaskName("ROOT_TASK");
|
task.setTaskName("ROOT_TASK");
|
||||||
task.setAddress(NetUtils.getLocalHost());
|
task.setAddress(NetUtils.getLocalHost4Test());
|
||||||
task.setLastModifiedTime(System.currentTimeMillis());
|
task.setLastModifiedTime(System.currentTimeMillis());
|
||||||
task.setCreatedTime(System.currentTimeMillis());
|
task.setCreatedTime(System.currentTimeMillis());
|
||||||
task.setLastReportTime(System.currentTimeMillis());
|
task.setLastReportTime(System.currentTimeMillis());
|
||||||
@ -93,14 +93,14 @@ public class PersistenceServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testUpdateLostTasks() throws Exception {
|
public void testUpdateLostTasks() throws Exception {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
boolean success = taskPersistenceService.updateLostTasks(10086L, Lists.newArrayList(NetUtils.getLocalHost()), true);
|
boolean success = taskPersistenceService.updateLostTasks(10086L, Lists.newArrayList(NetUtils.getLocalHost4Test()), true);
|
||||||
System.out.println("updateLostTasks: " + success);
|
System.out.println("updateLostTasks: " + success);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAllUnFinishedTaskByAddress() throws Exception {
|
public void testGetAllUnFinishedTaskByAddress() throws Exception {
|
||||||
System.out.println("=============== testGetAllUnFinishedTaskByAddress ===============");
|
System.out.println("=============== testGetAllUnFinishedTaskByAddress ===============");
|
||||||
List<TaskDO> res = taskPersistenceService.getAllUnFinishedTaskByAddress(10086L, NetUtils.getLocalHost());
|
List<TaskDO> res = taskPersistenceService.getAllUnFinishedTaskByAddress(10086L, NetUtils.getLocalHost4Test());
|
||||||
System.out.println(res);
|
System.out.println(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ public class TestUtils {
|
|||||||
|
|
||||||
req.setJobId(1L);
|
req.setJobId(1L);
|
||||||
req.setInstanceId(10086L);
|
req.setInstanceId(10086L);
|
||||||
req.setAllWorkerAddress(Lists.newArrayList(NetUtils.getLocalHost() + ":" + RemoteConstant.DEFAULT_WORKER_PORT));
|
req.setAllWorkerAddress(Lists.newArrayList(NetUtils.getLocalHost4Test() + ":" + RemoteConstant.DEFAULT_WORKER_PORT));
|
||||||
|
|
||||||
req.setJobParams("JobParams");
|
req.setJobParams("JobParams");
|
||||||
req.setInstanceParams("InstanceParams");
|
req.setInstanceParams("InstanceParams");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user