refactor: optimize NetUtils

This commit is contained in:
tjq 2024-02-08 16:08:39 +08:00
parent 6de5e83a2f
commit 61aecc6354
11 changed files with 35 additions and 19 deletions

View File

@ -94,6 +94,14 @@ public class NetUtils {
return LOCALHOST_VALUE;
}
/**
* 隔离调用 scope核心场景才能直接调用 getLocalHost方便查看使用点
* @return IP
*/
public static String getLocalHost4Test() {
return getLocalHost();
}
/**
* Find first valid IP from local network card
*

View File

@ -12,19 +12,19 @@ public class NetUtilsTest {
@Test
public void testOrigin() {
System.out.println(NetUtils.getLocalHost());
System.out.println(NetUtils.getLocalHost4Test());
}
@Test
public void testPreferredNetworkInterface() {
System.setProperty(PowerJobDKey.PREFERRED_NETWORK_INTERFACE, "en5");
System.out.println(NetUtils.getLocalHost());
System.out.println(NetUtils.getLocalHost4Test());
}
@Test
public void testIgnoredNetworkInterface() {
System.setProperty(PowerJobDKey.IGNORED_NETWORK_INTERFACE_REGEX, "utun.|llw.");
System.out.println(NetUtils.getLocalHost());
System.out.println(NetUtils.getLocalHost4Test());
}
}

View File

@ -123,14 +123,14 @@ public class VerificationProcessor extends CommonBasicProcessor implements MapRe
@Override
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!");
}
@Override
public ProcessResult postProcess(TaskContext context, List<TaskResult> taskResults) throws Exception {
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 ======");
taskResults.forEach(r -> omsLogger.info("taskId:{},success:{},result:{}", r.getTaskId(), r.isSuccess(), r.getResult()));
return new ProcessResult(true, "postProcess successfully!");

View File

@ -6,6 +6,8 @@ import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
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;
/**
@ -15,8 +17,9 @@ import tech.powerjob.server.extension.dfs.DFsService;
* @since 2023/7/28
*/
@Slf4j
public abstract class AbstractDFsService implements DFsService, ApplicationContextAware, DisposableBean {
public abstract class AbstractDFsService implements DFsService, ApplicationContextAware, ServerInfoAware, DisposableBean {
protected ServerInfo serverInfo;
protected ApplicationContext applicationContext;
public AbstractDFsService() {
@ -38,4 +41,9 @@ public abstract class AbstractDFsService implements DFsService, ApplicationConte
log.info("[DFsService] invoke [{}]'s setApplicationContext", this.getClass().getName());
init(applicationContext);
}
@Override
public void setServerInfo(ServerInfo serverInfo) {
this.serverInfo = serverInfo;
}
}

View File

@ -139,7 +139,7 @@ public class MySqlSeriesDfsService extends AbstractDFsService {
deleteByLocation(fileLocation);
Map<String, Object> meta = Maps.newHashMap();
meta.put("_server_", NetUtils.getLocalHost());
meta.put("_server_", serverInfo.getIp());
meta.put("_local_file_path_", storeRequest.getLocalFile().getAbsolutePath());
BufferedInputStream bufferedInputStream = new BufferedInputStream(Files.newInputStream(storeRequest.getLocalFile().toPath()));

View File

@ -69,7 +69,7 @@ public class ServerController implements ServerInfoAware {
@GetMapping("/hello")
public ResultDTO<JSONObject> ping(@RequestParam(required = false) boolean debug) {
JSONObject res = new JSONObject();
res.put("localHost", NetUtils.getLocalHost());
res.put("localHost", serverInfo.getIp());
res.put("serverInfo", serverInfo);
res.put("serverTime", CommonUtils.formatTime(System.currentTimeMillis()));
res.put("serverTimeTs", System.currentTimeMillis());

View File

@ -53,7 +53,7 @@ public class RepositoryTest {
public void testBatchLock() {
List<OmsLockDO> locks = Lists.newArrayList();
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);
}
omsLockRepository.saveAll(locks);
@ -63,7 +63,7 @@ public class RepositoryTest {
@Test
public void testDeleteLock() {
String lockName = "test-lock";
OmsLockDO lockDO = new OmsLockDO(lockName, NetUtils.getLocalHost(), 10000L);
OmsLockDO lockDO = new OmsLockDO(lockName, NetUtils.getLocalHost4Test(), 10000L);
omsLockRepository.save(lockDO);
omsLockRepository.deleteByLockName(lockName);
}

View File

@ -24,7 +24,7 @@ public class BroadcastProcessorDemo implements BroadcastProcessor {
@Override
public ProcessResult preProcess(TaskContext context) {
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())) {
return new ProcessResult(false, "console need failed");
} else {
@ -36,7 +36,7 @@ public class BroadcastProcessorDemo implements BroadcastProcessor {
public ProcessResult process(TaskContext taskContext) throws Exception {
OmsLogger logger = taskContext.getOmsLogger();
System.out.println("===== BroadcastProcessorDemo#process ======");
logger.info("BroadcastProcessorDemo#process, current host: {}", NetUtils.getLocalHost());
logger.info("BroadcastProcessorDemo#process, current host: {}", NetUtils.getLocalHost4Test());
long sleepTime = 1000;
try {
sleepTime = Long.parseLong(taskContext.getJobParams());
@ -50,7 +50,7 @@ public class BroadcastProcessorDemo implements BroadcastProcessor {
@Override
public ProcessResult postProcess(TaskContext context, List<TaskResult> taskResults) {
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");
}
}

View File

@ -33,7 +33,7 @@ public class CommonTest {
TaskTrackerStartTaskReq req = new TaskTrackerStartTaskReq();
req.setTaskTrackerAddress(NetUtils.getLocalHost() + ":27777");
req.setTaskTrackerAddress(NetUtils.getLocalHost4Test() + ":27777");
req.setInstanceInfo(instanceInfo);
req.setTaskId("0");

View File

@ -40,7 +40,7 @@ public class PersistenceServiceTest {
task.setFailedCnt(0);
task.setStatus(TaskStatus.WORKER_RECEIVED.getValue());
task.setTaskName("ROOT_TASK");
task.setAddress(NetUtils.getLocalHost());
task.setAddress(NetUtils.getLocalHost4Test());
task.setLastModifiedTime(System.currentTimeMillis());
task.setCreatedTime(System.currentTimeMillis());
task.setLastReportTime(System.currentTimeMillis());
@ -70,7 +70,7 @@ public class PersistenceServiceTest {
task.setFailedCnt(0);
task.setStatus(TaskStatus.WORKER_RECEIVED.getValue());
task.setTaskName("ROOT_TASK");
task.setAddress(NetUtils.getLocalHost());
task.setAddress(NetUtils.getLocalHost4Test());
task.setLastModifiedTime(System.currentTimeMillis());
task.setCreatedTime(System.currentTimeMillis());
task.setLastReportTime(System.currentTimeMillis());
@ -93,14 +93,14 @@ public class PersistenceServiceTest {
@Test
public void testUpdateLostTasks() throws Exception {
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);
}
@Test
public void testGetAllUnFinishedTaskByAddress() throws Exception {
System.out.println("=============== testGetAllUnFinishedTaskByAddress ===============");
List<TaskDO> res = taskPersistenceService.getAllUnFinishedTaskByAddress(10086L, NetUtils.getLocalHost());
List<TaskDO> res = taskPersistenceService.getAllUnFinishedTaskByAddress(10086L, NetUtils.getLocalHost4Test());
System.out.println(res);
}

View File

@ -21,7 +21,7 @@ public class TestUtils {
req.setJobId(1L);
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.setInstanceParams("InstanceParams");