Skip to content

Commit b4016f0

Browse files
Patrick Johnsonjxblum
authored andcommitted
Enable Function Executions to return results from all servers.
Refer to DATAGEODE-295. Resolves spring-projectsgh-37.
1 parent 5104b7b commit b4016f0

File tree

6 files changed

+52
-22
lines changed

6 files changed

+52
-22
lines changed

spring-data-geode/src/main/java/org/springframework/data/gemfire/function/execution/AbstractFunctionExecution.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@
4040
* @author David Turanski
4141
* @author John Blum
4242
* @author Patrick Johnson
43+
<<<<<<< HEAD
4344
* @see java.util.concurrent.TimeUnit
4445
* @see org.apache.geode.cache.execute.Execution
4546
* @see org.apache.geode.cache.execute.Function
4647
* @see org.apache.geode.cache.execute.FunctionService
4748
* @see org.apache.geode.cache.execute.ResultCollector
49+
=======
50+
>>>>>>> ae69760bf... DATAGEODE-295 - Functions return results from all servers.
4851
*/
4952
@SuppressWarnings("unused")
5053
abstract class AbstractFunctionExecution {

spring-data-geode/src/main/java/org/springframework/data/gemfire/function/execution/GemfireFunctionProxyFactoryBean.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
package org.springframework.data.gemfire.function.execution;
1414

1515
import java.lang.reflect.Method;
16+
import java.util.function.Function;
1617
import java.util.stream.StreamSupport;
1718

1819
import org.springframework.aop.framework.ProxyFactory;
1920
import org.springframework.aop.support.AopUtils;
2021
import org.springframework.beans.factory.FactoryBean;
22+
import org.springframework.data.gemfire.function.annotation.OnServers;
2123
import org.springframework.data.gemfire.support.AbstractFactoryBeanSupport;
24+
import org.springframework.lang.NonNull;
25+
import org.springframework.lang.Nullable;
2226
import org.springframework.util.Assert;
2327
import org.springframework.util.ClassUtils;
2428

@@ -43,24 +47,25 @@ public class GemfireFunctionProxyFactoryBean extends AbstractFactoryBeanSupport<
4347

4448
private final Class<?> functionExecutionInterface;
4549

46-
private FunctionExecutionMethodMetadata<MethodMetadata> methodMetadata;
50+
private final FunctionExecutionMethodMetadata<MethodMetadata> methodMetadata;
4751

4852
private final GemfireFunctionOperations gemfireFunctionOperations;
4953

5054
private volatile Object functionExecutionProxy;
5155

5256
/**
53-
* Constructs a new instance of the {@link GemfireFunctionProxyFactoryBean} initialized with the given
57+
* Constructs a new instance of {@link GemfireFunctionProxyFactoryBean} initialized with the given
5458
* {@link Class Function Excution Interface} and {@link GemfireFunctionOperations}.
5559
*
56-
* @param functionExecutionInterface {@link Class Function Execution Interface} to proxy.
57-
* @param gemfireFunctionOperations Template class used to delegate the Function invocation.
58-
* @see org.springframework.data.gemfire.function.execution.GemfireFunctionOperations
60+
* @param functionExecutionInterface {@link Class Function Execution Interface} to proxy;
61+
* must not be {@literal null}.
62+
* @param gemfireFunctionOperations template used to execute the {@link Function}.
5963
* @throws IllegalArgumentException if the {@link Class Function Execution Interface} is {@literal null}
6064
* or the {@link Class Function Execution Type} is not an actual interface.
65+
* @see org.springframework.data.gemfire.function.execution.GemfireFunctionOperations
6166
*/
62-
public GemfireFunctionProxyFactoryBean(Class<?> functionExecutionInterface,
63-
GemfireFunctionOperations gemfireFunctionOperations) {
67+
public GemfireFunctionProxyFactoryBean(@NonNull Class<?> functionExecutionInterface,
68+
@NonNull GemfireFunctionOperations gemfireFunctionOperations) {
6469

6570
Assert.notNull(functionExecutionInterface, "Function Execution Interface must not be null");
6671

@@ -73,8 +78,11 @@ public GemfireFunctionProxyFactoryBean(Class<?> functionExecutionInterface,
7378
this.methodMetadata = new DefaultFunctionExecutionMethodMetadata(functionExecutionInterface);
7479
}
7580

81+
/**
82+
* @inheritDoc
83+
*/
7684
@Override
77-
public ClassLoader getBeanClassLoader() {
85+
public @NonNull ClassLoader getBeanClassLoader() {
7886

7987
ClassLoader beanClassLoader = super.getBeanClassLoader();
8088

@@ -85,16 +93,19 @@ protected Class<?> getFunctionExecutionInterface() {
8593
return this.functionExecutionInterface;
8694
}
8795

88-
protected FunctionExecutionMethodMetadata<MethodMetadata> getFunctionExecutionMethodMetadata() {
96+
protected @NonNull FunctionExecutionMethodMetadata<MethodMetadata> getFunctionExecutionMethodMetadata() {
8997
return this.methodMetadata;
9098
}
9199

92-
protected GemfireFunctionOperations getGemfireFunctionOperations() {
100+
protected @NonNull GemfireFunctionOperations getGemfireFunctionOperations() {
93101
return this.gemfireFunctionOperations;
94102
}
95103

104+
/**
105+
* @inheritDoc
106+
*/
96107
@Override
97-
public Object invoke(MethodInvocation invocation) {
108+
public @Nullable Object invoke(@NonNull MethodInvocation invocation) {
98109

99110
if (AopUtils.isToStringMethod(invocation.getMethod())) {
100111
return String.format("Function Proxy for interface [%s]", getFunctionExecutionInterface().getName());
@@ -107,13 +118,19 @@ public Object invoke(MethodInvocation invocation) {
107118
return resolveResult(invocation, result);
108119
}
109120

110-
protected Object invokeFunction(Method method, Object[] args) {
121+
protected @Nullable Object invokeFunction(@NonNull Method method, @NonNull Object[] args) {
122+
123+
GemfireFunctionOperations template = getGemfireFunctionOperations();
111124

112-
String functionId = getFunctionExecutionMethodMetadata()
113-
.getMethodMetadata(method)
114-
.getFunctionId();
125+
String functionId = getFunctionExecutionMethodMetadata().getMethodMetadata(method).getFunctionId();
115126

116-
return getGemfireFunctionOperations().execute(functionId, args);
127+
return isFunctionExecutionForAllServers(method)
128+
? template.execute(functionId, args)
129+
: template.executeAndExtract(functionId, args);
130+
}
131+
132+
private boolean isFunctionExecutionForAllServers(Method method) {
133+
return method.getDeclaringClass().isAnnotationPresent(OnServers.class);
117134
}
118135

119136
protected Object resolveResult(MethodInvocation invocation, Object result) {
@@ -147,6 +164,9 @@ protected boolean isIterable(Object value) {
147164
return value instanceof Iterable;
148165
}
149166

167+
/**
168+
* @inheritDoc
169+
*/
150170
@Override
151171
public Object getObject() throws Exception {
152172

@@ -158,6 +178,9 @@ public Object getObject() throws Exception {
158178
return this.functionExecutionProxy;
159179
}
160180

181+
/**
182+
* @inheritDoc
183+
*/
161184
@Override
162185
public Class<?> getObjectType() {
163186
return getFunctionExecutionInterface();

spring-data-geode/src/test/java/org/springframework/data/gemfire/function/execution/onservers/AllServersAdminFunctions.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,11 @@
1616
*/
1717
package org.springframework.data.gemfire.function.execution.onservers;
1818

19+
import java.util.List;
20+
1921
import org.springframework.data.gemfire.function.annotation.FunctionId;
2022
import org.springframework.data.gemfire.function.annotation.OnServers;
2123

22-
import java.util.List;
23-
2424
/**
2525
* @author Patrick Johnson
2626
*/
@@ -29,4 +29,5 @@ public interface AllServersAdminFunctions {
2929

3030
@FunctionId("GetAllMetricsFunction")
3131
List<List<Metric>> getAllMetrics();
32+
3233
}

spring-data-geode/src/test/java/org/springframework/data/gemfire/function/execution/onservers/FunctionsReturnResultsFromAllServersIntegrationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -113,6 +113,7 @@ static class MetricsFunctionServerProcess {
113113
private static final int DEFAULT_CACHE_SERVER_PORT = 40404;
114114

115115
private static final String CACHE_SERVER_PORT_PROPERTY = "spring.data.gemfire.cache.server.port";
116+
private static final String GEMFIRE_LOG_LEVEL = "error";
116117
private static final String GEMFIRE_NAME = "MetricsServer" + getCacheServerPort();
117118

118119
public static void main(String[] args) throws Exception {
@@ -123,6 +124,7 @@ private static Cache newGemFireCache() {
123124

124125
return new CacheFactory()
125126
.set("name", GEMFIRE_NAME)
127+
.set("log-level", GEMFIRE_LOG_LEVEL)
126128
.create();
127129
}
128130

spring-data-geode/src/test/java/org/springframework/data/gemfire/function/execution/onservers/Metric.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-data-geode/src/test/java/org/springframework/data/gemfire/function/execution/onservers/SingleServerAdminFunctions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,4 +29,5 @@ public interface SingleServerAdminFunctions {
2929

3030
@FunctionId("GetAllMetricsFunction")
3131
List<Metric> getAllMetrics();
32+
3233
}

0 commit comments

Comments
 (0)