Skip to content

Commit

Permalink
HADOOP-19231. Add JacksonUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Jul 21, 2024
1 parent e48cd0e commit 8910fd1
Show file tree
Hide file tree
Showing 67 changed files with 369 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import org.apache.hadoop.security.alias.CredentialProvider.CredentialEntry;
import org.apache.hadoop.security.alias.CredentialProviderFactory;
import org.apache.hadoop.thirdparty.com.google.common.base.Strings;
import org.apache.hadoop.util.JacksonUtil;
import org.apache.hadoop.util.Preconditions;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.StringInterner;
Expand Down Expand Up @@ -3770,7 +3771,7 @@ public static void dumpConfiguration(Configuration config,
throw new IllegalArgumentException("Property " +
propertyName + " not found");
} else {
JsonFactory dumpFactory = new JsonFactory();
final JsonFactory dumpFactory = JacksonUtil.createBasicJsonFactory();
JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
dumpGenerator.writeStartObject();
dumpGenerator.writeFieldName("property");
Expand Down Expand Up @@ -3809,7 +3810,7 @@ public static void dumpConfiguration(Configuration config,
*/
public static void dumpConfiguration(Configuration config,
Writer out) throws IOException {
JsonFactory dumpFactory = new JsonFactory();
final JsonFactory dumpFactory = JacksonUtil.createBasicJsonFactory();
JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
dumpGenerator.writeStartObject();
dumpGenerator.writeFieldName("properties");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenSelector;
import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL;
import org.apache.hadoop.util.HttpExceptionUtils;
import org.apache.hadoop.util.JacksonUtil;
import org.apache.hadoop.util.JsonSerialization;
import org.apache.hadoop.util.KMSUtil;
import org.apache.http.client.utils.URIBuilder;
Expand Down Expand Up @@ -129,6 +130,13 @@ public class KMSClientProvider extends KeyProvider implements CryptoExtension,
+ "authentication.retry-count";
public static final int DEFAULT_AUTH_RETRY = 1;

/**
* It is more performant to reuse ObjectMapper instances but keeping the instance
* private makes it harder for someone to reconfigure it which might have unwanted
* side effects.
*/
private static final ObjectMapper OBJECT_MAPPER = JacksonUtil.createBasicObjectMapper();

private final ValueQueue<EncryptedKeyVersion> encKeyVersionQueue;

private KeyProviderDelegationTokenExtension.DelegationTokenExtension
Expand Down Expand Up @@ -592,11 +600,10 @@ private <T> T call(HttpURLConnection conn, Object jsonOutput,
&& conn.getContentType().trim().toLowerCase()
.startsWith(APPLICATION_JSON_MIME)
&& klass != null) {
ObjectMapper mapper = new ObjectMapper();
InputStream is = null;
try {
is = conn.getInputStream();
ret = mapper.readValue(is, klass);
ret = OBJECT_MAPPER.readValue(is, klass);
} finally {
IOUtils.closeStream(is);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.fasterxml.jackson.databind.ObjectWriter;

import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.util.JacksonUtil;
import org.apache.hadoop.util.Preconditions;
import org.apache.hadoop.thirdparty.com.google.common.util.concurrent.AtomicDoubleArray;
import org.apache.commons.lang3.exception.ExceptionUtils;
Expand Down Expand Up @@ -146,7 +147,7 @@ public class DecayRpcScheduler implements RpcScheduler,
public static final Logger LOG =
LoggerFactory.getLogger(DecayRpcScheduler.class);

private static final ObjectWriter WRITER = new ObjectMapper().writer();
private static final ObjectWriter WRITER = JacksonUtil.createBasicWriter();

// Track the decayed and raw (no decay) number of calls for each schedulable
// identity from all previous decay windows: idx 0 for decayed call cost and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
import org.apache.hadoop.security.token.SecretManager.InvalidToken;
import org.apache.hadoop.security.token.TokenIdentifier;
import org.apache.hadoop.util.ExitUtil;
import org.apache.hadoop.util.JacksonUtil;
import org.apache.hadoop.util.ProtoUtil;
import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.util.Time;
Expand All @@ -130,7 +131,6 @@
import org.apache.hadoop.tracing.TraceScope;
import org.apache.hadoop.tracing.Tracer;
import org.apache.hadoop.tracing.TraceUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hadoop.classification.VisibleForTesting;

import org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
Expand Down Expand Up @@ -3843,9 +3843,8 @@ public int getNumOpenConnections() {
* @return Get the NumOpenConnections/User.
*/
public String getNumOpenConnectionsPerUser() {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper
return JacksonUtil.createBasicObjectMapper()
.writeValueAsString(connectionManager.getUserToConnectionsMap());
} catch (IOException ignored) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.apache.hadoop.util.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -146,7 +147,7 @@ public class JMXJsonServlet extends HttpServlet {
public void init() throws ServletException {
// Retrieve the MBean server
mBeanServer = ManagementFactory.getPlatformMBeanServer();
jsonFactory = new JsonFactory();
jsonFactory = JacksonUtil.createBasicJsonFactory();
}

protected boolean isInstrumentationAccessAllowed(HttpServletRequest request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.apache.hadoop.util.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,7 +47,7 @@ public class MetricsJsonBuilder extends MetricsRecordBuilder {
private Map<String, Object> innerMetrics = new LinkedHashMap<>();

private static final ObjectWriter WRITER =
new ObjectMapper().writer();
JacksonUtil.createBasicWriter();

/**
* Build an instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenSecretManager;
import org.apache.hadoop.util.HttpExceptionUtils;
import org.apache.hadoop.util.JacksonUtil;
import org.apache.hadoop.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -165,7 +166,7 @@ public void initTokenManager(Properties config) {
@VisibleForTesting
public void initJsonFactory(Properties config) {
boolean hasFeature = false;
JsonFactory tmpJsonFactory = new JsonFactory();
JsonFactory tmpJsonFactory = JacksonUtil.createBasicJsonFactory();

for (Map.Entry entry : config.entrySet()) {
String key = (String)entry.getKey();
Expand Down Expand Up @@ -335,7 +336,7 @@ public boolean managementOperation(AuthenticationToken token,
if (map != null) {
response.setContentType(MediaType.APPLICATION_JSON);
Writer writer = response.getWriter();
ObjectMapper jsonMapper = new ObjectMapper(jsonFactory);
ObjectMapper jsonMapper = JacksonUtil.createObjectMapper(jsonFactory);
jsonMapper.writeValue(writer, map);
writer.write(ENTER);
writer.flush();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.apache.hadoop.util;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.json.JsonMapper;

/**
* Utility for sharing code related to Jackson usage in Hadoop.
*
* @since 3.5.0
*/
public class JacksonUtil {

private static final JsonFactory DEFAULT_JSON_FACTORY = createBasicJsonFactory();
private static final ObjectMapper DEFAULT_OBJECT_MAPPER = createBasicObjectMapper();

/**
* Creates a new {@link JsonFactory} instance with basic configuration.
*
* @return an {@link JsonFactory} with basic configuration
*/
public static JsonFactory createBasicJsonFactory() {
// do not expose DEFAULT_JSON_FACTORY because we don't want anyone to access it and modify it
return new JsonFactory();
}

/**
* Creates a new {@link ObjectMapper} instance with basic configuration.
*
* @return an {@link ObjectMapper} with basic configuration
*/
public static ObjectMapper createBasicObjectMapper() {
// do not expose DEFAULT_OBJECT_MAPPER because we don't want anyone to access it and modify it
return JsonMapper.builder(DEFAULT_JSON_FACTORY).build();
}

/**
* Creates a new {@link ObjectMapper} instance based on the configuration
* in the input {@link JsonFactory}.
*
* @param jsonFactory a pre-configured {@link JsonFactory}
* @return an {@link ObjectMapper} with configuration set by the input {@link JsonFactory}.
*/
public static ObjectMapper createObjectMapper(final JsonFactory jsonFactory) {
return JsonMapper.builder(jsonFactory).build();
}

/**
* Creates a new {@link ObjectReader} for the provided type.
*
* @param type a class instance
* @return an {@link ObjectReader} with basic configuration
*/
public static ObjectReader createReaderFor(final Class<?> type) {
return DEFAULT_OBJECT_MAPPER.readerFor(type);
}

/**
* Creates a new {@link ObjectReader} for the provided type.
*
* @param type a {@link JavaType} instance
* @return an {@link ObjectReader} with basic configuration
*/
public static ObjectReader createReaderFor(final JavaType type) {
return DEFAULT_OBJECT_MAPPER.readerFor(type);
}

/**
* Creates a new {@link ObjectWriter} with basic configuration.
*
* @return an {@link ObjectWriter} with basic configuration
*/
public static ObjectWriter createBasicWriter() {
return DEFAULT_OBJECT_MAPPER.writer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ public class JsonSerialization<T> {
private final Class<T> classType;
private final ObjectMapper mapper;

private static final ObjectWriter WRITER =
new ObjectMapper().writerWithDefaultPrettyPrinter();
private static final ObjectWriter WRITER = JacksonUtil
.createBasicObjectMapper()
.writerWithDefaultPrettyPrinter();

private static final ObjectReader MAP_READER =
new ObjectMapper().readerFor(Map.class);
private static final ObjectReader MAP_READER = JacksonUtil.createReaderFor(Map.class);

/**
* @return an ObjectWriter which pretty-prints its output
Expand All @@ -106,7 +106,7 @@ public JsonSerialization(Class<T> classType,
boolean failOnUnknownProperties, boolean pretty) {
Preconditions.checkArgument(classType != null, "null classType");
this.classType = classType;
this.mapper = new ObjectMapper();
this.mapper = JacksonUtil.createBasicObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
failOnUnknownProperties);
mapper.configure(SerializationFeature.INDENT_OUTPUT, pretty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.util.JacksonUtil;

import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
Expand All @@ -38,7 +39,7 @@
@Consumes(MediaType.APPLICATION_JSON)
@InterfaceAudience.Private
public class KMSJSONReader implements MessageBodyReader<Object> {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final ObjectMapper MAPPER = JacksonUtil.createBasicObjectMapper();

@Override
public boolean isReadable(Class<?> type, Type genericType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import org.apache.hadoop.util.JacksonUtil;
import org.apache.hadoop.util.Preconditions;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
Expand All @@ -35,9 +36,9 @@
@InterfaceStability.Unstable
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class DiskBalancerWorkItem {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final ObjectMapper MAPPER = JacksonUtil.createBasicObjectMapper();
private static final ObjectReader READER =
new ObjectMapper().readerFor(DiskBalancerWorkItem.class);
JacksonUtil.createReaderFor(DiskBalancerWorkItem.class);

private long startTime;
private long secondsElapsed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.hadoop.util.JacksonUtil;
import org.apache.hadoop.util.Preconditions;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
Expand All @@ -39,13 +40,13 @@
@InterfaceAudience.Private
@InterfaceStability.Unstable
public class DiskBalancerWorkStatus {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final ObjectMapper MAPPER = JacksonUtil.createBasicObjectMapper();
private static final ObjectMapper MAPPER_WITH_INDENT_OUTPUT =
new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
JacksonUtil.createBasicObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
private static final ObjectReader READER_WORKSTATUS =
new ObjectMapper().readerFor(DiskBalancerWorkStatus.class);
private static final ObjectReader READER_WORKENTRY = new ObjectMapper()
.readerFor(defaultInstance().constructCollectionType(List.class,
JacksonUtil.createReaderFor(DiskBalancerWorkStatus.class);
private static final ObjectReader READER_WORKENTRY = JacksonUtil.createReaderFor(
defaultInstance().constructCollectionType(List.class,
DiskBalancerWorkEntry.class));

private final List<DiskBalancerWorkEntry> currentState;
Expand Down
Loading

0 comments on commit 8910fd1

Please sign in to comment.