feat: worker use random server address #953

This commit is contained in:
tjq 2024-08-11 11:32:28 +08:00
parent 4527454a7c
commit fea1974014
4 changed files with 65 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.function.Supplier;
@ -129,6 +130,16 @@ public class CommonUtils {
throw new PowerJobException(msg);
}
}
if (obj instanceof Collection) {
if (CollectionUtils.isEmpty((Collection<?>) obj)) {
throw new PowerJobException(msg);
}
}
if (obj instanceof Map) {
if (MapUtils.isEmpty((Map<?, ?>) obj)) {
throw new PowerJobException(msg);
}
}
return obj;
}

View File

@ -75,4 +75,12 @@ public class MapUtils {
return null;
}
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
}

View File

@ -0,0 +1,39 @@
package tech.powerjob.common.utils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.jupiter.api.Test;
import tech.powerjob.common.exception.PowerJobException;
import java.util.Collections;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
/**
* CommonUtilsTest
*
* @author tjq
* @since 2024/8/11
*/
class CommonUtilsTest {
@Test
void testRequireNonNull() {
assertThrowsExactly(PowerJobException.class, () -> CommonUtils.requireNonNull(null, "NULL_OBJ"));
assertThrowsExactly(PowerJobException.class, () -> CommonUtils.requireNonNull("", "EMPTY_STR"));
assertThrowsExactly(PowerJobException.class, () -> CommonUtils.requireNonNull(Lists.newArrayList(), "EMPTY_COLLECTION"));
assertThrowsExactly(PowerJobException.class, () -> CommonUtils.requireNonNull(Collections.emptyMap(), "EMPTY_MAP"));
Map<String, Object> map = Maps.newHashMap();
map.put("1", 1);
CommonUtils.requireNonNull(1, "NORMAL");
CommonUtils.requireNonNull("1", "NORMAL");
CommonUtils.requireNonNull(Lists.newArrayList("1"), "NORMAL");
CommonUtils.requireNonNull(map, "NORMAL");
}
}

View File

@ -55,7 +55,7 @@ public class PowerJobWorker {
public PowerJobWorker(PowerJobWorkerConfig config) {
this.workerRuntime = new WorkerRuntime();
this.remoteEngine = new PowerJobRemoteEngine();
workerRuntime.setWorkerConfig(config);
workerRuntime.setWorkerConfig(reConfig(config));
}
public void init() throws Exception {
@ -147,6 +147,12 @@ public class PowerJobWorker {
}
}
private PowerJobWorkerConfig reConfig(PowerJobWorkerConfig config) {
CommonUtils.requireNonNull(config.getServerAddress(), "ServerAddress can't be null or empty!");
Collections.shuffle(config.getServerAddress());
return config;
}
private ProcessorLoader buildProcessorLoader(WorkerRuntime runtime) {
List<ProcessorFactory> customPF = Optional.ofNullable(runtime.getWorkerConfig().getProcessorFactoryList()).orElse(Collections.emptyList());
List<ProcessorFactory> finalPF = Lists.newArrayList(customPF);