Skip to content

Commit

Permalink
Repackage policy & config center api to governance-api
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Jan 15, 2025
1 parent 222da27 commit 852cec4
Show file tree
Hide file tree
Showing 55 changed files with 238 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package com.jd.live.agent.core.bootstrap;

import com.jd.live.agent.core.bootstrap.ApplicationListener.ApplicationListenerAdapter;
import com.jd.live.agent.core.config.ConfigCenter;
import com.jd.live.agent.core.config.Configurator;
import com.jd.live.agent.core.event.AgentEvent;
import com.jd.live.agent.core.event.Publisher;
import com.jd.live.agent.core.extension.annotation.Extension;
Expand All @@ -36,19 +34,6 @@ public class ApplicationBootstrapListener extends ApplicationListenerAdapter {
@Inject(Publisher.SYSTEM)
private Publisher<AgentEvent> publisher;

@Inject(value = ConfigCenter.COMPONENT_CONFIG_CENTER, component = true, nullable = true)
private ConfigCenter configCenter;

@Override
public void onEnvironmentPrepared(ApplicationBootstrapContext context, ApplicationEnvironment environment) {
if (configCenter != null) {
Configurator configurator = configCenter.getConfigurator();
if (configurator != null) {
environment.addFirst(new LivePropertySource(configurator));
}
}
}

@Override
public void onStarted(ApplicationContext context) {
publisher.offer(AgentEvent.onApplicationStarted("Application is started"));
Expand All @@ -64,23 +49,4 @@ public void onStop(ApplicationContext context) {
publisher.offer(AgentEvent.onApplicationStop("Application is stopping"));
}

private static class LivePropertySource implements ApplicationPropertySource {

private final Configurator configurator;

LivePropertySource(Configurator configurator) {
this.configurator = configurator;
}

@Override
public String getProperty(String name) {
Object property = configurator.getProperty(name);
return property == null ? null : property.toString();
}

@Override
public String getName() {
return configurator.getName();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* 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.jd.live.agent.core.service;

/**
* An interface that allows a service to be aware of and interact with its supervisor.
*/
public interface ServiceSupervisorAware {

/**
* Sets the service supervisor for this service.
*
* @param serviceSupervisor The service supervisor to be set.
*/
void setup(ServiceSupervisor serviceSupervisor);

}

Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
import com.jd.live.agent.core.parser.ObjectParser;
import com.jd.live.agent.core.plugin.PluginManager;
import com.jd.live.agent.core.plugin.PluginSupervisor;
import com.jd.live.agent.core.service.PolicyService;
import com.jd.live.agent.core.service.ServiceManager;
import com.jd.live.agent.core.service.ServiceSupervisor;
import com.jd.live.agent.core.service.ServiceSupervisorAware;
import com.jd.live.agent.core.util.Close;
import com.jd.live.agent.core.util.network.Ipv4;
import com.jd.live.agent.core.util.option.CascadeOption;
Expand All @@ -84,7 +84,6 @@
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;

import static com.jd.live.agent.core.extension.condition.ConditionMatcher.DEPEND_ON_LOADER;

Expand Down Expand Up @@ -182,12 +181,8 @@ public class Bootstrap implements AgentLifecycle {
*/
private ServiceManager serviceManager;

private ConfigCenter configCenter;

private ApplicationListener applicationListener;

private PolicyWatcherSupervisor policyWatcherSupervisor;

/**
* Supervises plugins, handling their lifecycle.
*/
Expand Down Expand Up @@ -278,12 +273,10 @@ public void install() {
//depend on agentConfig
throw new InitializeException("the jvm version is not supported enhancement.");
}
policyWatcherSupervisor = createPolicyWatcherSupervisor();
createSourceSuppliers(); // depend on configWatcher
createSourceSuppliers();
serviceManager = createServiceManager(); //depend on extensionManager & classLoaderManager & eventBus & sourceSuppliers
addPolicyWatcher(); // depend on serviceManager & configSupervisor
configCenter = createConfigCenter();
applicationListener = new ApplicationListenerWrapper(createApplicationListeners());
setupServiceManager(); // inject to source supplier
applicationListener = new ApplicationListenerWrapper(createApplicationListeners()); // depend on source suppliers & serviceManager
byteSupplier = createByteSupplier();
pluginManager = createPluginManager(); //depend on context & extensionManager & classLoaderManager & byteSupplier
commandManager = createCommandManager();
Expand Down Expand Up @@ -545,8 +538,6 @@ private Injector createInjector() {
ctx.add(ExtensionManager.COMPONENT_EXTENSION_MANAGER, extensionManager);
ctx.add(ServiceSupervisor.COMPONENT_SERVICE_SUPERVISOR, serviceManager);
ctx.add(ApplicationListener.COMPONENT_APPLICATION_LISTENER, applicationListener);
ctx.add(ConfigCenter.COMPONENT_CONFIG_CENTER, configCenter);
ctx.add(PolicyWatcherSupervisor.COMPONENT_CONFIG_SUPERVISOR, policyWatcherSupervisor);
ctx.add(Timer.COMPONENT_TIMER, timer);
ctx.add(EventBus.COMPONENT_EVENT_BUS, eventBus);
ctx.add(Resourcer.COMPONENT_RESOURCER, classLoaderManager == null ? null : classLoaderManager.getPluginLoaders());
Expand Down Expand Up @@ -576,32 +567,18 @@ private ServiceManager createServiceManager() {
return result;
}

private PolicyWatcherManager createPolicyWatcherSupervisor() {
return new PolicyWatcherManager();
}

private void addPolicyWatcher() {
serviceManager.service(service -> {
if (service instanceof PolicyService) {
policyWatcherSupervisor.addWatcher((PolicyService) service);
private void setupServiceManager() {
for (InjectSourceSupplier supplier : sourceSuppliers) {
if (supplier instanceof ServiceSupervisorAware) {
((ServiceSupervisorAware) supplier).setup(serviceManager);
}
});
}
}

private List<ApplicationListener> createApplicationListeners() {
return extensionManager.getOrLoadExtensible(ApplicationListener.class, classLoaderManager.getCoreImplLoader()).getExtensions();
}

private ConfigCenter createConfigCenter() {
AtomicReference<ConfigCenter> result = new AtomicReference<>();
serviceManager.service(service -> {
if (service instanceof ConfigCenter) {
result.set((ConfigCenter) service);
}
});
return result.get();
}

private PluginSupervisor createPluginManager() {
return new PluginManager(instrumentation, agentConfig.getPluginConfig(), agentPath, extensionManager,
classLoaderManager.getPluginLoaders(), byteSupplier, conditionMatcher);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* 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.jd.live.agent.governance.bootstrap;

import com.jd.live.agent.core.bootstrap.ApplicationBootstrapContext;
import com.jd.live.agent.core.bootstrap.ApplicationEnvironment;
import com.jd.live.agent.core.bootstrap.ApplicationListener;
import com.jd.live.agent.core.bootstrap.ApplicationListener.ApplicationListenerAdapter;
import com.jd.live.agent.core.bootstrap.ApplicationPropertySource;
import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.governance.subscription.config.ConfigCenter;
import com.jd.live.agent.governance.subscription.config.Configurator;

/**
* An extension that prepares config for the application.
*
* @since 1.6.0
*/
@Injectable
@Extension(value = "ConfigPreparation", order = ApplicationListener.ORDER_BOOTSTRAP)
public class ConfigPreparation extends ApplicationListenerAdapter {

@Inject(value = ConfigCenter.COMPONENT_CONFIG_CENTER, component = true, nullable = true)
private ConfigCenter configCenter;

@Override
public void onEnvironmentPrepared(ApplicationBootstrapContext context, ApplicationEnvironment environment) {
if (configCenter != null) {
Configurator configurator = configCenter.getConfigurator();
if (configurator != null) {
environment.addFirst(new LivePropertySource(configurator));
}
}
}

private static class LivePropertySource implements ApplicationPropertySource {

private final Configurator configurator;

LivePropertySource(Configurator configurator) {
this.configurator = configurator;
}

@Override
public String getProperty(String name) {
Object property = configurator.getProperty(name);
return property == null ? null : property.toString();
}

@Override
public String getName() {
return configurator.getName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ public class PolicyPreparation extends ApplicationListenerAdapter {
public void onStarted(ApplicationContext context) {
policySupervisor.waitReady();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.jd.live.agent.governance.config;

import com.jd.live.agent.core.config.ConfigName;
import com.jd.live.agent.governance.subscription.config.ConfigName;
import lombok.Getter;
import lombok.Setter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jd.live.agent.core.config;
package com.jd.live.agent.governance.config;

import lombok.Getter;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.jd.live.agent.governance.policy;

import com.jd.live.agent.core.config.PolicyWatcher;
import com.jd.live.agent.core.config.PolicyWatcherSupervisor;
import com.jd.live.agent.core.event.AgentEvent;
import com.jd.live.agent.core.event.AgentEvent.EventType;
import com.jd.live.agent.core.event.Event;
Expand All @@ -31,7 +29,8 @@
import com.jd.live.agent.core.instance.AppService;
import com.jd.live.agent.core.instance.Application;
import com.jd.live.agent.core.parser.ObjectParser;
import com.jd.live.agent.core.service.PolicyService;
import com.jd.live.agent.core.service.ServiceSupervisor;
import com.jd.live.agent.core.service.ServiceSupervisorAware;
import com.jd.live.agent.core.util.Futures;
import com.jd.live.agent.core.util.time.Timer;
import com.jd.live.agent.governance.config.*;
Expand All @@ -48,20 +47,25 @@
import com.jd.live.agent.governance.invoke.filter.RouteFilter;
import com.jd.live.agent.governance.invoke.loadbalance.LoadBalancer;
import com.jd.live.agent.governance.invoke.matcher.TagMatcher;
import com.jd.live.agent.governance.policy.listener.LaneSpaceListener;
import com.jd.live.agent.governance.policy.listener.LiveSpaceListener;
import com.jd.live.agent.governance.policy.listener.ServiceListener;
import com.jd.live.agent.governance.policy.variable.UnitFunction;
import com.jd.live.agent.governance.policy.variable.VariableFunction;
import com.jd.live.agent.governance.policy.variable.VariableParser;
import com.jd.live.agent.governance.service.PolicyService;
import com.jd.live.agent.governance.subscription.config.ConfigCenter;
import com.jd.live.agent.governance.subscription.policy.PolicyWatcher;
import com.jd.live.agent.governance.subscription.policy.PolicyWatcherManager;
import com.jd.live.agent.governance.subscription.policy.PolicyWatcherSupervisor;
import com.jd.live.agent.governance.subscription.policy.listener.LaneSpaceListener;
import com.jd.live.agent.governance.subscription.policy.listener.LiveSpaceListener;
import com.jd.live.agent.governance.subscription.policy.listener.ServiceListener;
import lombok.Getter;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import static com.jd.live.agent.core.config.PolicyWatcher.*;
import static com.jd.live.agent.governance.subscription.policy.PolicyWatcher.*;

/**
* PolicyManager
Expand All @@ -71,7 +75,7 @@
*/
@Injectable
@Extension(value = "PolicyManager", order = InjectSourceSupplier.ORDER_POLICY_MANAGER)
public class PolicyManager implements PolicySupervisor, InjectSourceSupplier, ExtensionInitializer, InvocationContext {
public class PolicyManager implements PolicySupervisor, InjectSourceSupplier, ExtensionInitializer, InvocationContext, ServiceSupervisorAware {

private final AtomicReference<GovernancePolicy> policy = new AtomicReference<>();

Expand All @@ -93,9 +97,6 @@ public class PolicyManager implements PolicySupervisor, InjectSourceSupplier, Ex
@Inject(Application.COMPONENT_APPLICATION)
private Application application;

@Inject(PolicyWatcherSupervisor.COMPONENT_CONFIG_SUPERVISOR)
private PolicyWatcherSupervisor configSupervisor;

@Inject(ObjectParser.JSON)
private ObjectParser objectParser;

Expand Down Expand Up @@ -177,6 +178,10 @@ public class PolicyManager implements PolicySupervisor, InjectSourceSupplier, Ex

private List<String> serviceSyncers;

private ConfigCenter configCenter;

private final PolicyWatcherSupervisor policyWatcherSupervisor = new PolicyWatcherManager();

private final AtomicBoolean warmup = new AtomicBoolean(false);

@Override
Expand Down Expand Up @@ -232,6 +237,7 @@ public void apply(InjectSource source) {
source.add(PolicySupervisor.COMPONENT_POLICY_SUPPLIER, this);
source.add(InvocationContext.COMPONENT_INVOCATION_CONTEXT, this);
source.add(Propagation.COMPONENT_PROPAGATION, propagation);
source.add(ConfigCenter.COMPONENT_CONFIG_CENTER, configCenter);
if (governanceConfig != null) {
source.add(GovernanceConfig.COMPONENT_GOVERNANCE_CONFIG, governanceConfig);
source.add(ServiceConfig.COMPONENT_SERVICE_CONFIG, governanceConfig.getServiceConfig());
Expand Down Expand Up @@ -306,9 +312,21 @@ public void initialize() {
}
}
});
configSupervisor.addListener(TYPE_LIVE_SPACE, new LiveSpaceListener(this, objectParser));
configSupervisor.addListener(TYPE_LANE_SPACE, new LaneSpaceListener(this, objectParser));
configSupervisor.addListener(TYPE_SERVICE_SPACE, new ServiceListener(this, objectParser, policyPublisher));

policyWatcherSupervisor.addListener(TYPE_LIVE_SPACE, new LiveSpaceListener(this, objectParser));
policyWatcherSupervisor.addListener(TYPE_LANE_SPACE, new LaneSpaceListener(this, objectParser));
policyWatcherSupervisor.addListener(TYPE_SERVICE_SPACE, new ServiceListener(this, objectParser, policyPublisher));
}

@Override
public void setup(ServiceSupervisor serviceSupervisor) {
serviceSupervisor.service(service -> {
if (service instanceof PolicyService) {
policyWatcherSupervisor.addWatcher((PolicyService) service);
} else if (service instanceof ConfigCenter) {
configCenter = (ConfigCenter) service;
}
});
}

/**
Expand Down Expand Up @@ -351,7 +369,7 @@ private Propagation buildPropagation() {
*/
private List<String> getServiceSyncers() {
List<String> result = new ArrayList<>();
List<PolicyWatcher> watchers = configSupervisor.getWatchers();
List<PolicyWatcher> watchers = policyWatcherSupervisor.getWatchers();
if (watchers != null) {
for (PolicyWatcher service : watchers) {
if (service instanceof PolicyService) {
Expand Down
Loading

0 comments on commit 852cec4

Please sign in to comment.