From 97ca4430d97b782a3b95fc56805d51f4ebd19377 Mon Sep 17 00:00:00 2001 From: Jerry Wang Date: Wed, 10 Jan 2024 00:03:50 +0800 Subject: [PATCH] remove threadlocal for DESERIALIZATION_CONTEXT and use copy instead (#29952) Co-authored-by: jierwang --- .../sdk/options/PipelineOptionsFactory.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java index 555f50bc823b..0b0ef88cf655 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java @@ -500,15 +500,12 @@ Class getProxyClass() { new ObjectMapper() .registerModules(ObjectMapper.findModules(ReflectHelpers.findClassLoader())); - private static final ThreadLocal DESERIALIZATION_CONTEXT = - ThreadLocal.withInitial( - () -> - new DefaultDeserializationContext.Impl( - MAPPER.getDeserializationContext().getFactory()) - .createInstance( - MAPPER.getDeserializationConfig(), - new TokenBuffer(MAPPER, false).asParser(), - new InjectableValues.Std())); + private static final DefaultDeserializationContext DESERIALIZATION_CONTEXT = + new DefaultDeserializationContext.Impl(MAPPER.getDeserializationContext().getFactory()) + .createInstance( + MAPPER.getDeserializationConfig(), + new TokenBuffer(MAPPER, false).asParser(), + new InjectableValues.Std()); static final DefaultSerializerProvider SERIALIZER_PROVIDER = new DefaultSerializerProvider.Impl() @@ -1731,7 +1728,7 @@ private static JsonDeserializer computeDeserializerForMethod(Method meth BeanProperty prop = createBeanProperty(method); AnnotatedMember annotatedMethod = prop.getMember(); - DefaultDeserializationContext context = DESERIALIZATION_CONTEXT.get(); + DefaultDeserializationContext context = DESERIALIZATION_CONTEXT.copy(); Object maybeDeserializerClass = context.getAnnotationIntrospector().findDeserializer(annotatedMethod); @@ -1803,7 +1800,7 @@ static Object deserializeNode(JsonNode node, Method method) throws IOException { parser.nextToken(); JsonDeserializer jsonDeserializer = getDeserializerForMethod(method); - return jsonDeserializer.deserialize(parser, DESERIALIZATION_CONTEXT.get()); + return jsonDeserializer.deserialize(parser, DESERIALIZATION_CONTEXT.copy()); } /**