Convert functions etc. after context starts

The app deployer now has to reach into the function contexts and
extract a catalog and call its methods reflectively.
This commit is contained in:
Dave Syer
2017-01-04 10:43:18 +00:00
parent 13774abe39
commit 91717ec9a6
9 changed files with 327 additions and 258 deletions

View File

@@ -43,14 +43,14 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/admin")
public class FunctionAdminController implements CommandLineRunner {
private final FunctionExtractingAppDeployer deployer;
private final FunctionExtractingFunctionCatalog deployer;
private Map<String, String> deployed = new LinkedHashMap<>();
private Map<String, String> names = new LinkedHashMap<>();
@Autowired
public FunctionAdminController(FunctionExtractingAppDeployer deployer) {
public FunctionAdminController(FunctionExtractingFunctionCatalog deployer) {
this.deployer = deployer;
}

View File

@@ -1,183 +0,0 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* 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 org.springframework.cloud.function.deployer;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.cloud.deployer.thin.ThinJarAppDeployer;
import org.springframework.cloud.deployer.thin.ThinJarAppWrapper;
import org.springframework.cloud.function.registry.FunctionCatalog;
import org.springframework.util.MethodInvoker;
import org.springframework.util.ReflectionUtils;
public class FunctionExtractingAppDeployer extends ThinJarAppDeployer
implements FunctionCatalog {
private static final Log logger = LogFactory
.getLog(FunctionExtractingAppDeployer.class);
private final Map<String, Function<?, ?>> functions = new HashMap<>();
private final Map<String, Consumer<?>> consumers = new HashMap<>();
private final Map<String, Supplier<?>> suppliers = new HashMap<>();
public FunctionExtractingAppDeployer() {
this("thin", "slim");
}
public FunctionExtractingAppDeployer(String name, String... profiles) {
super(name, profiles);
}
@SuppressWarnings("unchecked")
@Override
public <T> Consumer<T> lookupConsumer(String name) {
return (Consumer<T>) consumers.get(name);
}
@SuppressWarnings("unchecked")
@Override
public <T, R> Function<T, R> lookupFunction(String name) {
return (Function<T, R>) functions.get(name);
}
@Override
public <T, R> Function<T, R> composeFunction(String... functionNames) {
Function<T, R> function = this.lookupFunction(functionNames[0]);
for (int i = 1; i < functionNames.length; i++) {
function = function.andThen(this.lookupFunction(functionNames[i]));
}
return function;
}
@SuppressWarnings("unchecked")
@Override
public <T> Supplier<T> lookupSupplier(String name) {
return (Supplier<T>) suppliers.get(name);
}
@Override
public String deploy(AppDeploymentRequest request) {
String id = super.deploy(request);
functions.putAll(functions(id));
suppliers.putAll(suppliers(id));
consumers.putAll(consumers(id));
return id;
}
@Override
public void undeploy(String id) {
super.undeploy(id);
for (String name : functions(id).keySet()) {
functions.remove(name);
}
for (String name : suppliers(id).keySet()) {
suppliers.remove(name);
}
for (String name : consumers(id).keySet()) {
consumers.remove(name);
}
}
private Map<String, Function<?, ?>> functions(String id) {
Map<String, Function<?, ?>> map = new HashMap<>();
ThinJarAppWrapper wrapper = getWrapper(id);
if (wrapper == null) {
return map;
}
try {
@SuppressWarnings("unchecked")
Map<String, ? extends Function<?, ?>> result = (Map<String, ? extends Function<?, ?>>) getBeans(
wrapper, Function.class);
map.putAll(result);
}
catch (Exception e) {
throw new IllegalStateException("Cannot extract functions", e);
}
logger.info("Loaded functions: " + map.keySet());
return map;
}
private Map<String, Consumer<?>> consumers(String id) {
Map<String, Consumer<?>> map = new HashMap<>();
ThinJarAppWrapper wrapper = getWrapper(id);
if (wrapper == null) {
return map;
}
try {
@SuppressWarnings("unchecked")
Map<String, ? extends Consumer<?>> result = (Map<String, ? extends Consumer<?>>) getBeans(
wrapper, Consumer.class);
map.putAll(result);
}
catch (Exception e) {
throw new IllegalStateException("Cannot extract consumers", e);
}
logger.info("Loaded consumers: " + map.keySet());
return map;
}
private Map<String, Supplier<?>> suppliers(String id) {
Map<String, Supplier<?>> map = new HashMap<>();
ThinJarAppWrapper wrapper = getWrapper(id);
if (wrapper == null) {
return map;
}
try {
@SuppressWarnings("unchecked")
Map<String, ? extends Supplier<?>> result = (Map<String, ? extends Supplier<?>>) getBeans(
wrapper, Supplier.class);
map.putAll(result);
}
catch (Exception e) {
throw new IllegalStateException("Cannot extract suppliers", e);
}
logger.info("Loaded suppliers: " + map.keySet());
return map;
}
private <T> Map<String, ? extends T> getBeans(ThinJarAppWrapper wrapper,
Class<T> type) throws IllegalAccessException, ClassNotFoundException,
NoSuchMethodException, InvocationTargetException {
Object app = findContext(wrapper);
MethodInvoker invoker = new MethodInvoker();
invoker.setTargetObject(app);
invoker.setTargetMethod("getBeansOfType");
invoker.setArguments(new Object[] { type });
invoker.prepare();
@SuppressWarnings("unchecked")
Map<String, T> result = (Map<String, T>) invoker.invoke();
return result;
}
private Object findContext(ThinJarAppWrapper wrapper) {
Object app = wrapper.getApp();
Field field = ReflectionUtils.findField(app.getClass(), "context");
ReflectionUtils.makeAccessible(field);
app = ReflectionUtils.getField(field, app);
return app;
}
}

View File

@@ -26,13 +26,13 @@ import org.springframework.context.annotation.Configuration;
*
*/
@Configuration
@ConditionalOnClass(FunctionExtractingAppDeployer.class)
@ConditionalOnClass(FunctionExtractingFunctionCatalog.class)
@AutoConfigureBefore(DefaultFunctionRegistryAutoConfiguration.class)
public class FunctionExtractingAutoConfiguration {
@Bean
public FunctionExtractingAppDeployer functionCatalog() {
return new FunctionExtractingAppDeployer();
public FunctionExtractingFunctionCatalog functionCatalog() {
return new FunctionExtractingFunctionCatalog();
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* 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 org.springframework.cloud.function.deployer;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.cloud.deployer.thin.ThinJarAppDeployer;
import org.springframework.cloud.function.registry.FunctionCatalog;
import org.springframework.util.MethodInvoker;
public class FunctionExtractingFunctionCatalog implements FunctionCatalog {
private final Set<String> deployed = new HashSet<>();
private ThinJarAppDeployer deployer;
public FunctionExtractingFunctionCatalog() {
this("thin", "slim");
}
public FunctionExtractingFunctionCatalog(String name, String... profiles) {
deployer = new ThinJarAppDeployer(name, profiles);
}
@SuppressWarnings("unchecked")
@Override
public <T> Consumer<T> lookupConsumer(String name) {
return (Consumer<T>) find(name, "lookupConsumer");
}
@SuppressWarnings("unchecked")
@Override
public <T, R> Function<T, R> lookupFunction(String name) {
return (Function<T, R>) find(name, "lookupFunction");
}
@Override
public <T, R> Function<T, R> composeFunction(String... functionNames) {
Function<T, R> function = this.lookupFunction(functionNames[0]);
for (int i = 1; i < functionNames.length; i++) {
function = function.andThen(this.lookupFunction(functionNames[i]));
}
return function;
}
@SuppressWarnings("unchecked")
@Override
public <T> Supplier<T> lookupSupplier(String name) {
return (Supplier<T>) find(name, "lookupSupplier");
}
public String deploy(AppDeploymentRequest request) {
String id = deployer.deploy(request);
deployed.add(id);
return id;
}
public void undeploy(String id) {
deployer.undeploy(id);
deployed.remove(id);
}
private Object find(String name, String method) {
for (String id : deployed) {
Object catalog = deployer.getBean(id, FunctionCatalog.class);
if (catalog == null) {
continue;
}
try {
MethodInvoker invoker = new MethodInvoker();
invoker.setTargetObject(catalog);
invoker.setTargetMethod(method);
invoker.setArguments(new Object[] { name });
invoker.prepare();
Object result = invoker.invoke();
if (result != null) {
return result;
}
}
catch (Exception e) {
throw new IllegalStateException("Cannot extract catalog", e);
}
}
return null;
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* 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 org.springframework.cloud.function.deployer;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.boot.loader.thin.ArchiveUtils;
import org.springframework.boot.loader.tools.LogbackInitializer;
import org.springframework.cloud.deployer.spi.app.DeploymentState;
import org.springframework.cloud.deployer.spi.core.AppDefinition;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.cloud.deployer.thin.ThinJarAppDeployer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
@RunWith(Parameterized.class)
public class FunctionAppDeployerTests {
static {
LogbackInitializer.initialize();
}
private static ThinJarAppDeployer deployer = new ThinJarAppDeployer();
@BeforeClass
public static void skip() {
try {
ArchiveUtils.getArchiveRoot(ArchiveUtils.getArchive(
"maven://org.springframework.cloud:spring-cloud-function-web:1.0.0.BUILD-SNAPSHOT"));
}
catch (Exception e) {
Assume.assumeNoException(
"Could not locate jar for tests. Please build spring-cloud-function locally first.",
e);
}
}
@Parameterized.Parameters
public static List<Object[]> data() {
// Repeat a couple of times to ensure it's consistent
return Arrays.asList(new Object[2][0]);
}
@Test
public void web() throws Exception {
String first = deploy(
"maven://org.springframework.cloud:spring-cloud-function-web:1.0.0.BUILD-SNAPSHOT",
"--web.path=/words", "--function.name=uppercase");
// Deployment is blocking so it either failed or succeeded.
assertThat(deployer.status(first).getState()).isEqualTo(DeploymentState.deployed);
deployer.undeploy(first);
}
@Test
public void stream() throws Exception {
String first = deploy(
"maven://org.springframework.cloud:spring-cloud-function-stream:1.0.0.BUILD-SNAPSHOT",
"--spring.cloud.stream.bindings.input.destination=words",
"--spring.cloud.stream.bindings.output.destination=uppercaseWords",
"--function.name=uppercase");
// Deployment is blocking so it either failed or succeeded.
assertThat(deployer.status(first).getState()).isEqualTo(DeploymentState.deployed);
deployer.undeploy(first);
}
private String deploy(String jarName, String... args) throws Exception {
Resource resource = new FileSystemResource(
ArchiveUtils.getArchiveRoot(ArchiveUtils.getArchive(jarName)));
AppDefinition definition = new AppDefinition(resource.getFilename(),
Collections.emptyMap());
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource,
Collections.emptyMap(), Arrays.asList(args));
String deployed = deployer.deploy(request);
return deployed;
}
}

View File

@@ -29,7 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*
*/
public class FunctionExtractingAppDeployerIntegrationTests {
public class FunctionExtractingFunctionCatalogIntegrationTests {
private static ConfigurableApplicationContext context;
private static int port;
@@ -37,7 +37,7 @@ public class FunctionExtractingAppDeployerIntegrationTests {
@BeforeClass
public static void open() {
port = SocketUtils.findAvailableTcpPort();
System.setProperty("debug", "true");
// System.setProperty("debug", "true");
context = new ApplicationRunner().start("--server.port=" + port);
}

View File

@@ -25,7 +25,6 @@ import org.junit.rules.ExpectedException;
import org.springframework.boot.loader.thin.ArchiveUtils;
import org.springframework.boot.loader.tools.LogbackInitializer;
import org.springframework.cloud.deployer.spi.app.DeploymentState;
import org.springframework.cloud.deployer.spi.core.AppDefinition;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.core.io.FileSystemResource;
@@ -39,7 +38,7 @@ import reactor.core.publisher.Flux;
* @author Dave Syer
*
*/
public class FunctionExtractingAppDeployerTests {
public class FunctionExtractingFunctionCatalogTests {
private static String id;
@@ -47,7 +46,7 @@ public class FunctionExtractingAppDeployerTests {
LogbackInitializer.initialize();
}
private static FunctionExtractingAppDeployer deployer = new FunctionExtractingAppDeployer();
private static FunctionExtractingFunctionCatalog deployer = new FunctionExtractingFunctionCatalog();
@Rule
public ExpectedException expected = ExpectedException.none();
@@ -58,7 +57,6 @@ public class FunctionExtractingAppDeployerTests {
id = deploy("maven://com.example:function-sample:1.0.0.BUILD-SNAPSHOT");
// "--debug");
}
assertThat(deployer.status(id).getState()).isEqualTo(DeploymentState.deployed);
}
@Test