Skip to content

Commit 35c9100

Browse files
mvanhornolegz
authored andcommitted
GH-1534 Isolate per-lookup output content type from the cached function wrapper
A singleton lookup that specifies output MIME types now returns a separate FunctionInvocationWrapper with those types. The wrapper reuses the same target, preserves composition, conversion, and header settings, and has its own invocation state. Lookups without MIME types still return the cached singleton wrapper. Regression tests cover concurrent lookups, isolation of conversion settings, and JSON output from a composed function. Resolves #1534 Resolves #1557 Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Signed-off-by: Oleg Zhurakousky <oleg.zhurakousky@broadcom.com>
1 parent 3fdd515 commit 35c9100

2 files changed

Lines changed: 138 additions & 5 deletions

File tree

‎spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,13 @@ <T> T doLookup(Class<?> type, String functionDefinition, String[] expectedOutput
251251
}
252252
if (function != null) {
253253
if (!ObjectUtils.isEmpty(expectedOutputMimeTypes)) {
254-
function.expectedOutputContentType = expectedOutputMimeTypes;
254+
// GH-1534: a cached singleton is shared, so this lookup gets its own view.
255+
if (function.isSingleton) {
256+
function = new FunctionInvocationWrapper(function, expectedOutputMimeTypes);
257+
}
258+
else {
259+
function.expectedOutputContentType = expectedOutputMimeTypes;
260+
}
255261
}
256262
}
257263
else if (logger.isDebugEnabled()) {
@@ -536,6 +542,26 @@ public class FunctionInvocationWrapper implements Function<Object, Object>, Cons
536542
}
537543
}
538544

545+
/*
546+
* Per-lookup view of a cached singleton. Thread-locals and {@code message} come from
547+
* the constructor above so this view does not share them with the cached wrapper.
548+
*/
549+
private FunctionInvocationWrapper(FunctionInvocationWrapper source, String[] expectedOutputMimeTypes) {
550+
this(source.functionDefinition, source.target, source.inputType, source.outputType);
551+
this.inputType = source.inputType;
552+
this.composed = source.composed;
553+
this.postProcessor = source.postProcessor;
554+
this.skipInputConversion = source.skipInputConversion;
555+
this.skipOutputConversion = source.skipOutputConversion;
556+
this.skipInputConversionCallback = source.skipInputConversionCallback;
557+
this.enhancer = source.enhancer;
558+
this.propagateInputHeaders = source.propagateInputHeaders;
559+
this.isSingleton = source.isSingleton;
560+
this.wrappedBiConsumer = source.wrappedBiConsumer;
561+
this.isPojoFunction = source.isPojoFunction;
562+
this.expectedOutputContentType = expectedOutputMimeTypes;
563+
}
564+
539565
public int hashCode() {
540566
return this.functionDefinition.hashCode();
541567
}

‎spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistryTests.java‎

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,118 @@ public void testCachingOfFunction() {
401401
new JacksonMapper(new ObjectMapper()));
402402
catalog.register(registration);
403403

404-
FunctionInvocationWrapper instanceA = catalog.lookup("echo", "application/json");
405-
FunctionInvocationWrapper instanceb = catalog.lookup("echo", "text/plain");
406-
FunctionInvocationWrapper instanceC = catalog.lookup("echo", "foo/bar");
404+
FunctionInvocationWrapper json = catalog.lookup("echo", "application/json");
405+
FunctionInvocationWrapper text = catalog.lookup("echo", "text/plain");
406+
FunctionInvocationWrapper other = catalog.lookup("echo", "foo/bar");
407+
FunctionInvocationWrapper cached = catalog.lookup("echo");
408+
409+
assertThat(json).isNotSameAs(text).isNotSameAs(other).isNotSameAs(cached);
410+
assertThat(text).isNotSameAs(other);
411+
assertThat((String[]) ReflectionTestUtils.getField(json, "expectedOutputContentType"))
412+
.containsExactly("application/json");
413+
assertThat((String[]) ReflectionTestUtils.getField(text, "expectedOutputContentType"))
414+
.containsExactly("text/plain");
415+
assertThat((String[]) ReflectionTestUtils.getField(other, "expectedOutputContentType"))
416+
.containsExactly("foo/bar");
417+
assertThat(ReflectionTestUtils.getField(cached, "expectedOutputContentType")).isNull();
418+
419+
@SuppressWarnings("unchecked")
420+
Map<String, FunctionInvocationWrapper> definitions = (Map<String, FunctionInvocationWrapper>) ReflectionTestUtils
421+
.getField(catalog, "wrappedFunctionDefinitions");
422+
assertThat(definitions).hasSize(1);
423+
assertThat(definitions.get("echo")).isSameAs(cached);
424+
425+
json.setSkipOutputConversion(true);
426+
assertThat(json.isSkipOutputConversion()).isTrue();
427+
assertThat(cached.isSkipOutputConversion()).isFalse();
428+
}
429+
430+
@SuppressWarnings("unchecked")
431+
@Test
432+
public void testConcurrentLookupsKeepExpectedOutputContentType() throws Exception {
433+
SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter,
434+
new JacksonMapper(new ObjectMapper()));
435+
catalog.register(new FunctionRegistration<>(new UpperCase(), "uppercase").type(UpperCase.class));
436+
437+
ExecutorService executor = Executors.newFixedThreadPool(2);
438+
CountDownLatch startGate = new CountDownLatch(1);
439+
try {
440+
CompletableFuture<FunctionInvocationWrapper> jsonLookup = CompletableFuture.supplyAsync(() -> {
441+
try {
442+
assertThat(startGate.await(5, TimeUnit.SECONDS)).isTrue();
443+
}
444+
catch (InterruptedException ex) {
445+
Thread.currentThread().interrupt();
446+
throw new IllegalStateException(ex);
447+
}
448+
return catalog.lookup("uppercase", "application/json");
449+
}, executor);
450+
CompletableFuture<FunctionInvocationWrapper> textLookup = CompletableFuture.supplyAsync(() -> {
451+
try {
452+
assertThat(startGate.await(5, TimeUnit.SECONDS)).isTrue();
453+
}
454+
catch (InterruptedException ex) {
455+
Thread.currentThread().interrupt();
456+
throw new IllegalStateException(ex);
457+
}
458+
return catalog.lookup("uppercase", "text/plain");
459+
}, executor);
460+
startGate.countDown();
461+
FunctionInvocationWrapper json = jsonLookup.get(5, TimeUnit.SECONDS);
462+
FunctionInvocationWrapper text = textLookup.get(5, TimeUnit.SECONDS);
463+
464+
assertThat(json).isNotSameAs(text);
465+
assertThat((String[]) ReflectionTestUtils.getField(json, "expectedOutputContentType"))
466+
.containsExactly("application/json");
467+
assertThat((String[]) ReflectionTestUtils.getField(text, "expectedOutputContentType"))
468+
.containsExactly("text/plain");
469+
470+
Message<String> singleValueMessage = MessageBuilder.withPayload("\"ricky\"").build();
471+
Object result = json.apply(singleValueMessage);
472+
assertThat(result).isInstanceOf(Message.class);
473+
assertThat(((Message<byte[]>) result).getPayload()).isEqualTo("\"RICKY\"".getBytes());
474+
475+
FunctionInvocationWrapper cached = catalog.lookup("uppercase");
476+
assertThat(cached).isNotSameAs(json).isNotSameAs(text);
477+
assertThat(ReflectionTestUtils.getField(cached, "expectedOutputContentType")).isNull();
478+
Map<String, FunctionInvocationWrapper> definitions = (Map<String, FunctionInvocationWrapper>) ReflectionTestUtils
479+
.getField(catalog, "wrappedFunctionDefinitions");
480+
assertThat(definitions).hasSize(1);
481+
assertThat(definitions.get("uppercase")).isSameAs(cached);
482+
}
483+
finally {
484+
executor.shutdownNow();
485+
}
486+
}
407487

408-
assertThat(instanceA).isSameAs(instanceb).isSameAs(instanceC);
488+
@SuppressWarnings("unchecked")
489+
@Test
490+
public void testComposedLookupWithMimeTypeKeepsComposition() {
491+
SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter,
492+
new JacksonMapper(new ObjectMapper()));
493+
catalog.register(new FunctionRegistration<>(new UpperCase(), "uppercase").type(UpperCase.class));
494+
catalog.register(new FunctionRegistration<>(new Reverse(), "reverse").type(Reverse.class));
495+
496+
FunctionInvocationWrapper composed = catalog.lookup("uppercase|reverse", "application/json");
497+
FunctionInvocationWrapper cached = catalog.lookup("uppercase|reverse");
498+
499+
assertThat(composed.isComposed()).isTrue();
500+
assertThat(composed).isNotSameAs(cached);
501+
assertThat(composed.getTarget()).isSameAs(cached.getTarget());
502+
assertThat((String[]) ReflectionTestUtils.getField(composed, "expectedOutputContentType"))
503+
.containsExactly("application/json");
504+
assertThat(ReflectionTestUtils.getField(cached, "expectedOutputContentType")).isNull();
505+
506+
Object result = composed.apply("ab");
507+
assertThat(result).isInstanceOf(Message.class);
508+
assertThat(((Message<byte[]>) result).getPayload()).isEqualTo("\"BA\"".getBytes());
509+
510+
assertThat((Object) catalog.lookup("not-a-function", "application/json")).isNull();
511+
Map<String, FunctionInvocationWrapper> definitions = (Map<String, FunctionInvocationWrapper>) ReflectionTestUtils
512+
.getField(catalog, "wrappedFunctionDefinitions");
513+
assertThat(definitions).hasSize(1);
514+
assertThat(definitions).doesNotContainKey("not-a-function");
515+
assertThat(definitions.get("uppercase|reverse")).isSameAs(cached);
409516
}
410517

411518
@Test

0 commit comments

Comments
 (0)