Skip to content

Commit

Permalink
Apply ConfigSource
Browse files Browse the repository at this point in the history
  • Loading branch information
mytang0 committed Apr 24, 2024
1 parent 4b15ed9 commit 54046dd
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package xyz.mytang0.brook.common.extension;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.mytang0.brook.common.annotation.OrderComparator;
import xyz.mytang0.brook.common.extension.injector.ExtensionInjector;
import xyz.mytang0.brook.common.extension.loading.LoadingStrategy;
import xyz.mytang0.brook.common.utils.Holder;
import xyz.mytang0.brook.common.utils.ReflectUtils;
import xyz.mytang0.brook.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
Expand Down
47 changes: 22 additions & 25 deletions brook-core/src/main/java/xyz/mytang0/brook/core/FlowExecutor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package xyz.mytang0.brook.core;

import com.google.common.base.Joiner;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import xyz.mytang0.brook.common.configuration.Configuration;
import xyz.mytang0.brook.common.constants.Delimiter;
import xyz.mytang0.brook.common.context.FlowContext;
Expand All @@ -26,6 +32,7 @@
import xyz.mytang0.brook.core.exception.TerminateException;
import xyz.mytang0.brook.core.execution.ExecutionProperties;
import xyz.mytang0.brook.core.lock.FlowLockFacade;
import xyz.mytang0.brook.core.lock.LockProperties;
import xyz.mytang0.brook.core.metadata.MetadataFacade;
import xyz.mytang0.brook.core.metadata.MetadataProperties;
import xyz.mytang0.brook.core.monitor.DelayedTaskMonitor;
Expand All @@ -34,17 +41,12 @@
import xyz.mytang0.brook.spi.cache.FlowCache;
import xyz.mytang0.brook.spi.cache.FlowCacheFactory;
import xyz.mytang0.brook.spi.computing.EngineActuator;
import xyz.mytang0.brook.spi.config.Configurator;
import xyz.mytang0.brook.spi.execution.ExecutionDAO;
import xyz.mytang0.brook.spi.executor.ExecutorFactory;
import xyz.mytang0.brook.spi.metadata.MetadataService;
import xyz.mytang0.brook.spi.queue.QueueService;
import xyz.mytang0.brook.spi.task.FlowTask;
import com.google.common.base.Joiner;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ValidationException;
import java.util.ArrayList;
Expand All @@ -64,6 +66,7 @@
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import static java.util.Objects.requireNonNull;
import static xyz.mytang0.brook.core.constants.FlowConstants.DEFAULT_ENGINE_TYPE;
import static xyz.mytang0.brook.core.constants.FlowConstants.DEFAULT_TIMEOUT_MS;
import static xyz.mytang0.brook.core.constants.FlowConstants.LOCK_TRY_TIME_MS;
Expand All @@ -72,7 +75,6 @@
import static xyz.mytang0.brook.core.exception.FlowErrorCode.FLOW_EXECUTION_ERROR;
import static xyz.mytang0.brook.core.exception.FlowErrorCode.FLOW_NOT_EXIST;
import static xyz.mytang0.brook.core.exception.FlowErrorCode.TASK_NOT_EXIST;
import static xyz.mytang0.brook.core.executor.ExecutorEnum.ASYNC_EXECUTOR;
import static xyz.mytang0.brook.core.executor.ExecutorEnum.FLOW_STARTER;
import static xyz.mytang0.brook.core.utils.ParameterUtils.flowContext;
import static xyz.mytang0.brook.core.utils.ParameterUtils.getFlowInput;
Expand All @@ -81,7 +83,6 @@
import static xyz.mytang0.brook.core.utils.ParameterUtils.getTaskInput;
import static xyz.mytang0.brook.core.utils.ParameterUtils.getTaskOutput;
import static xyz.mytang0.brook.core.utils.QueueUtils.getTaskDelayQueueName;
import static java.util.Objects.requireNonNull;

@Slf4j
public class FlowExecutor<T extends FlowTask> {
Expand All @@ -96,8 +97,6 @@ public class FlowExecutor<T extends FlowTask> {

private final ExecutorService flowStarter;

private final ExecutorService asyncExecutor;

private final FlowCacheFactory flowCacheFactory;

private final FlowAspect flowAspect;
Expand All @@ -111,17 +110,19 @@ public class FlowExecutor<T extends FlowTask> {
private final DelayedTaskMonitorProperties delayedTaskMonitorProperties;


public FlowExecutor(FlowLockFacade flowLockFacade,
FlowTaskRegistry<T> flowTaskRegistry,
QueueProperties queueProperties,
MetadataProperties metadataProperties,
ExecutionProperties executionProperties,
DelayedTaskMonitorProperties delayedTaskMonitorProperties) {
this.flowLockFacade = flowLockFacade;
public FlowExecutor(FlowTaskRegistry<T> flowTaskRegistry) {
Configurator configurator = ExtensionDirector
.getExtensionLoader(Configurator.class)
.getDefaultExtension();
this.flowLockFacade = new FlowLockFacade(
configurator.getConfig(LockProperties.class)
);
this.flowTaskRegistry = flowTaskRegistry;
this.flowAspect = new FlowAspect();
this.taskAspect = new TaskAspect();
this.metadataService = new MetadataFacade(metadataProperties);
this.metadataService = new MetadataFacade(
configurator.getConfig(MetadataProperties.class)
);
this.engineActuator = ExtensionDirector
.getExtensionLoader(EngineActuator.class)
.getDefaultExtension();
Expand All @@ -132,13 +133,9 @@ public FlowExecutor(FlowLockFacade flowLockFacade,
.getExtensionLoader(ExecutorFactory.class)
.getDefaultExtension()
.getExecutor(FLOW_STARTER);
this.asyncExecutor = ExtensionDirector
.getExtensionLoader(ExecutorFactory.class)
.getDefaultExtension()
.getExecutor(ASYNC_EXECUTOR);
this.queueProperties = queueProperties;
this.executionProperties = executionProperties;
this.delayedTaskMonitorProperties = delayedTaskMonitorProperties;
this.queueProperties = configurator.getConfig(QueueProperties.class);
this.executionProperties = configurator.getConfig(ExecutionProperties.class);
this.delayedTaskMonitorProperties = configurator.getConfig(DelayedTaskMonitorProperties.class);
DelayedTaskMonitor.init(this, flowLockFacade, delayedTaskMonitorProperties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ public <T> T bind(Class<T> type) {
@SuppressWarnings("unchecked")
private static <T> T bind(Class<T> type, String prefix, ConfigProperties annotation) {

T instance = Optional.ofNullable(bind(type, prefix))
.orElseGet(() -> newInstance(type));

// Validity verification.
if (annotation != null) {
Class<? extends ConfigValidator<T>> validator =
(Class<? extends ConfigValidator<T>>) annotation.validator();
if (!ConfigValidator.NULL.class.isAssignableFrom(validator)) {
Type generictype = validator.getGenericInterfaces()[0];
if (generictype instanceof ParameterizedType) {
generictype = ((ParameterizedType) generictype)
.getActualTypeArguments()[0];
if (generictype instanceof Class
&& type.isAssignableFrom((Class<?>) generictype)) {
validate(validator, instance);
}
}
}
}

return instance;
}

private static <T> T bind(Class<T> type, String prefix) {

// Already sorted according to @Order.
List<ConfigSource> configSources =
ExtensionDirector
Expand All @@ -89,23 +114,6 @@ private static <T> T bind(Class<T> type, String prefix, ConfigProperties annotat
);
}

// Validity verification.
if (annotation != null) {
Class<? extends ConfigValidator<T>> validator =
(Class<? extends ConfigValidator<T>>) annotation.validator();
if (!ConfigValidator.NULL.class.isAssignableFrom(validator)) {
Type generictype = validator.getGenericInterfaces()[0];
if (generictype instanceof ParameterizedType) {
generictype = ((ParameterizedType) generictype)
.getActualTypeArguments()[0];
if (generictype instanceof Class
&& type.isAssignableFrom((Class<?>) generictype)) {
validate(validator, instanceReference.get());
}
}
}
}

return instanceReference.get();
}

Expand Down Expand Up @@ -143,7 +151,7 @@ private static Object getFieldValue(List<ConfigSource> configSources, String pre
&& !ReflectUtils.isPrimitives(type)
&& !ReflectUtils.isJdkClass(type)) {

value = bind(type, propertyName, null);
value = bind(type, propertyName);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package xyz.mytang0.brook.core.execution;

import lombok.Data;
import xyz.mytang0.brook.spi.config.ConfigProperties;

@ConfigProperties(prefix = "brook.execution-dao")
@Data
public class ExecutionProperties {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package xyz.mytang0.brook.core.lock;

import lombok.Data;
import xyz.mytang0.brook.spi.config.ConfigProperties;

import java.time.Duration;

@ConfigProperties(prefix = "brook.lock")
@Data
public class LockProperties {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package xyz.mytang0.brook.core.metadata;

import lombok.Data;
import xyz.mytang0.brook.spi.config.ConfigProperties;

@ConfigProperties(prefix = "brook.metadata")
@Data
public class MetadataProperties {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package xyz.mytang0.brook.core.monitor;

import lombok.Data;
import xyz.mytang0.brook.spi.config.ConfigProperties;

import java.util.Set;

@ConfigProperties(prefix = "brook.delayed.task.monitor")
@Data
public class DelayedTaskMonitorProperties {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import lombok.Data;
import xyz.mytang0.brook.core.constants.FlowConstants;
import xyz.mytang0.brook.spi.config.ConfigProperties;

@ConfigProperties(prefix = "brook.queue")
@Data
public class QueueProperties {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import xyz.mytang0.brook.common.extension.ExtensionDirector;
import xyz.mytang0.brook.spi.config.ConfigProperties;
import xyz.mytang0.brook.spi.config.Configurator;

import java.nio.file.Paths;

Expand All @@ -12,15 +15,19 @@ public class FlowLogService {

private final FlowLogProperties properties;

public FlowLogService(FlowLogProperties properties) {
this.properties = properties;
public FlowLogService() {
this.properties = ExtensionDirector
.getExtensionLoader(Configurator.class)
.getDefaultExtension()
.getConfig(FlowLogProperties.class);
}

public String getLog(String id) {
return properties.getRoot()
+ id + properties.getSuffix();
}

@ConfigProperties(prefix = "brook.log")
@Data
public static class FlowLogProperties {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void testRefreshConfig9() {
Assert.assertEquals(properties.get("config8.name"), config8.getName());

Config6 config6 = config8.getConfig6();
Assert.assertNull(config6);
Assert.assertNotNull(config6);

String inner = "object";
Config6 origin = new Config6();
Expand All @@ -163,7 +163,7 @@ public void testGetConfig1() {
Assert.assertEquals(properties.get("config8.name"), config8.getName());

Config6 config6 = config8.getConfig6();
Assert.assertNull(config6);
Assert.assertNotNull(config6);

String inner = "object";
Config6 origin = new Config6();
Expand All @@ -172,7 +172,7 @@ public void testGetConfig1() {

Config8 newConfig8 = configurator.getConfig(Config8.class);
config6 = newConfig8.getConfig6();
Assert.assertNull(config6);
Assert.assertNotNull(config6);

Assert.assertEquals(config8, newConfig8);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package xyz.mytang0.brook.demo.controller;

import xyz.mytang0.brook.common.metadata.definition.FlowDef;
import xyz.mytang0.brook.core.metadata.MetadataFacade;
import xyz.mytang0.brook.core.metadata.MetadataProperties;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -11,6 +8,11 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.mytang0.brook.common.extension.ExtensionDirector;
import xyz.mytang0.brook.common.metadata.definition.FlowDef;
import xyz.mytang0.brook.core.metadata.MetadataFacade;
import xyz.mytang0.brook.core.metadata.MetadataProperties;
import xyz.mytang0.brook.spi.config.Configurator;

import javax.validation.Valid;

Expand All @@ -20,8 +22,13 @@ public class FlowMetadataController {

private final MetadataFacade metadataFacade;

public FlowMetadataController(MetadataProperties metadataProperties) {
this.metadataFacade = new MetadataFacade(metadataProperties);
public FlowMetadataController() {
this.metadataFacade = new MetadataFacade(
ExtensionDirector
.getExtensionLoader(Configurator.class)
.getDefaultExtension()
.getConfig(MetadataProperties.class)
);
}

@PostMapping
Expand Down
Loading

0 comments on commit 54046dd

Please sign in to comment.