-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue with Jackson 2.15: Can not write a field name, expecting a value #15564
Changes from all commits
b45acec
b5bec56
cd80471
c8dee90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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.logstash.log; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import com.fasterxml.jackson.databind.util.ClassUtil; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.jruby.RubyBasicObject; | ||
import org.jruby.RubyMethod; | ||
import org.jruby.RubyString; | ||
import org.jruby.exceptions.NameError; | ||
import org.logstash.RubyUtil; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Default serializer for {@link org.jruby.RubyBasicObject} since Jackson can't handle that type natively. | ||
* Arrays, Collections and Maps are delegated to the default Jackson's serializer - which might end-up invoking this | ||
* serializer for its elements. | ||
* Values which inspect method is implemented and owned by the LogStash module will be serialized using this method return. | ||
* If none of the above conditions match, it gets the serialized value by invoking the Ruby's {@code to_s} method, falling back | ||
* to {@link RubyBasicObject#to_s()} and {@link RubyBasicObject#anyToString()} in case of errors. | ||
*/ | ||
public final class RubyBasicObjectSerializer extends StdSerializer<RubyBasicObject> { | ||
|
||
private static final long serialVersionUID = -5557562960691452054L; | ||
private static final Logger LOGGER = LogManager.getLogger(RubyBasicObjectSerializer.class); | ||
private static final String METHOD_INSPECT = "inspect"; | ||
private static final String METHOD_TO_STRING = "to_s"; | ||
|
||
public RubyBasicObjectSerializer() { | ||
super(RubyBasicObject.class); | ||
} | ||
|
||
@Override | ||
public void serialize(final RubyBasicObject value, final JsonGenerator gen, final SerializerProvider provider) throws IOException { | ||
final Optional<JsonSerializer<Object>> serializer = findTypeSerializer(value, provider); | ||
if (serializer.isPresent()) { | ||
try { | ||
serializer.get().serialize(value, gen, provider); | ||
return; | ||
} catch (IOException e) { | ||
LOGGER.debug("Failed to serialize value type {} using default serializer {}", value.getClass(), serializer.get().getClass(), e); | ||
} | ||
} | ||
|
||
if (isCustomInspectMethodDefined(value)) { | ||
try { | ||
gen.writeString(value.callMethod(METHOD_INSPECT).asJavaString()); | ||
return; | ||
} catch (Exception e) { | ||
LOGGER.debug("Failed to serialize value type {} using the custom `inspect` method", value.getMetaClass(), e); | ||
} | ||
} | ||
|
||
try { | ||
gen.writeString(value.callMethod(METHOD_TO_STRING).asJavaString()); | ||
return; | ||
} catch (Exception e) { | ||
LOGGER.debug("Failed to serialize value type {} using `to_s` method", value.getMetaClass(), e); | ||
} | ||
|
||
try { | ||
gen.writeString(value.to_s().asJavaString()); | ||
} catch (Exception e) { | ||
LOGGER.debug("Failed to serialize value type {} using `RubyBasicObject#to_s()` method", value.getMetaClass(), e); | ||
gen.writeString(value.anyToString().asJavaString()); | ||
} | ||
} | ||
|
||
private Optional<JsonSerializer<Object>> findTypeSerializer(final RubyBasicObject value, final SerializerProvider provider) { | ||
if (ClassUtil.isCollectionMapOrArray(value.getClass())) { | ||
try { | ||
// Delegates the serialization to the Jackson's default serializers, which might | ||
// end up using this serializer for its elements. | ||
return Optional.ofNullable(provider.findTypedValueSerializer(value.getJavaClass(), false, null)); | ||
} catch (JsonMappingException e) { | ||
// Ignored | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
private boolean isCustomInspectMethodDefined(final RubyBasicObject value) { | ||
try { | ||
final Object candidate = value.method(RubyString.newString(RubyUtil.RUBY, METHOD_INSPECT)); | ||
return candidate instanceof RubyMethod && ((RubyMethod) candidate).owner(RubyUtil.RUBY.getCurrentContext()).toString().toLowerCase().startsWith("logstash"); | ||
Comment on lines
+110
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, do we know if this is costly performance wise when there's a lot of log entries? If necessary we could consider some memoization/caching strategy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, invoking JRuby's In terms of overall performance, the Considering the WDYT? Any suggestions? |
||
} catch (NameError e) { | ||
return false; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.logstash; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory; | ||
import com.fasterxml.jackson.databind.ser.Serializers; | ||
import com.fasterxml.jackson.databind.type.TypeFactory; | ||
import org.jruby.RubyBasicObject; | ||
import org.junit.Test; | ||
import org.logstash.log.RubyBasicObjectSerializer; | ||
|
||
import java.util.LinkedList; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.logstash.ObjectMappers.RUBY_BASIC_OBJECT_SERIALIZERS_MODULE_ID; | ||
import static org.logstash.ObjectMappers.RUBY_SERIALIZERS_MODULE_ID; | ||
|
||
public class ObjectMappersTest { | ||
|
||
@Test | ||
public void testLog4jOMRegisterRubySerializersModule() { | ||
assertTrue(ObjectMappers.LOG4J_JSON_MAPPER.getRegisteredModuleIds().contains(RUBY_SERIALIZERS_MODULE_ID)); | ||
} | ||
|
||
@Test | ||
public void testLog4jOMRegisterRubyBasicObjectSerializersModule() { | ||
assertTrue(ObjectMappers.LOG4J_JSON_MAPPER.getRegisteredModuleIds().contains(RUBY_BASIC_OBJECT_SERIALIZERS_MODULE_ID)); | ||
} | ||
|
||
@Test | ||
public void testLog4jOMRegisterRubyBasicObjectSerializersFirst() { | ||
final ObjectMapper mapper = ObjectMappers.LOG4J_JSON_MAPPER; | ||
final BeanSerializerFactory factory = (BeanSerializerFactory) mapper.getSerializerFactory(); | ||
|
||
final LinkedList<Serializers> list = new LinkedList<>(); | ||
for (Serializers serializer : factory.getFactoryConfig().serializers()) { | ||
list.add(serializer); | ||
} | ||
|
||
// RubyBasicObjectSerializer + Log4jJsonModule | ||
assertTrue(list.size() > 1); | ||
|
||
final Serializers rubyBasicObjectSerializer = list.get(list.size() - 2); | ||
final JavaType valueType = TypeFactory.defaultInstance().constructType(RubyBasicObject.class); | ||
final JsonSerializer<?> found = rubyBasicObjectSerializer.findSerializer(mapper.getSerializationConfig(), valueType, null); | ||
|
||
assertNotNull(found); | ||
assertTrue("RubyBasicObjectSerializer must be registered before others non-default serializers", found instanceof RubyBasicObjectSerializer); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any thoughts on the performance impact of this change? if
isValueSafeToWrite
is false frequently then creating a new generator, writer and buffer recycler could get expensive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On my local tests (1k fields), It had a minor/same performance impact compared to using only the main generator. I've also tried to reuse the
BufferRecycler
, but it didn't save many cycles as it needs to invoke thegetAndClear()
method in every iteration. TheSegmentedStringWriter
also had similar results to a JDKStringWriter
.