Commit a40f3c2c by Madhan Neethiraj

ATLAS-2312: Use ThreadLocal DateFormat objects to avoid simultaneous use from multiple threads

parent f89fa441
...@@ -180,7 +180,7 @@ public class HiveHookIT extends HiveITBase { ...@@ -180,7 +180,7 @@ public class HiveHookIT extends HiveITBase {
private void verifyTimestamps(Referenceable ref, String property, long expectedTime) throws ParseException { private void verifyTimestamps(Referenceable ref, String property, long expectedTime) throws ParseException {
//Verify timestamps. //Verify timestamps.
String createTimeStr = (String) ref.get(property); String createTimeStr = (String) ref.get(property);
Date createDate = AtlasBaseTypeDef.DATE_FORMATTER.parse(createTimeStr); Date createDate = AtlasBaseTypeDef.getDateFormatter().parse(createTimeStr);
Assert.assertNotNull(createTimeStr); Assert.assertNotNull(createTimeStr);
if (expectedTime > 0) { if (expectedTime > 0) {
......
...@@ -39,6 +39,7 @@ import javax.xml.bind.annotation.XmlSeeAlso; ...@@ -39,6 +39,7 @@ import javax.xml.bind.annotation.XmlSeeAlso;
import org.apache.atlas.model.PList; import org.apache.atlas.model.PList;
import org.apache.atlas.model.SearchFilter.SortType; import org.apache.atlas.model.SearchFilter.SortType;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
...@@ -58,6 +59,7 @@ public class AtlasStruct implements Serializable { ...@@ -58,6 +59,7 @@ public class AtlasStruct implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static final String SERIALIZED_DATE_FORMAT_STR = "yyyyMMdd-HH:mm:ss.SSS-Z"; public static final String SERIALIZED_DATE_FORMAT_STR = "yyyyMMdd-HH:mm:ss.SSS-Z";
@Deprecated
public static final DateFormat DATE_FORMATTER = new SimpleDateFormat(SERIALIZED_DATE_FORMAT_STR); public static final DateFormat DATE_FORMATTER = new SimpleDateFormat(SERIALIZED_DATE_FORMAT_STR);
private String typeName; private String typeName;
...@@ -266,7 +268,7 @@ public class AtlasStruct implements Serializable { ...@@ -266,7 +268,7 @@ public class AtlasStruct implements Serializable {
if (value == null) { if (value == null) {
sb.append(value); sb.append(value);
} else { } else {
sb.append(DATE_FORMATTER.format(value)); sb.append(AtlasBaseTypeDef.getDateFormatter().format(value));
} }
return sb; return sb;
......
...@@ -123,13 +123,31 @@ public abstract class AtlasBaseTypeDef implements java.io.Serializable { ...@@ -123,13 +123,31 @@ public abstract class AtlasBaseTypeDef implements java.io.Serializable {
ATLAS_TYPE_OBJECT_ID, ATLAS_TYPE_OBJECT_ID,
}; };
public static final String SERIALIZED_DATE_FORMAT_STR = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; public static final String SERIALIZED_DATE_FORMAT_STR = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
@Deprecated
public static final DateFormat DATE_FORMATTER = new SimpleDateFormat(SERIALIZED_DATE_FORMAT_STR); public static final DateFormat DATE_FORMATTER = new SimpleDateFormat(SERIALIZED_DATE_FORMAT_STR);
static { static {
DATE_FORMATTER.setTimeZone(TimeZone.getTimeZone("UTC")); DATE_FORMATTER.setTimeZone(TimeZone.getTimeZone("UTC"));
} }
public static DateFormat getDateFormatter() {
return THREAD_LOCAL_DATE_FORMAT.get();
}
private static final ThreadLocal<DateFormat> THREAD_LOCAL_DATE_FORMAT = new ThreadLocal<DateFormat>() {
@Override
public DateFormat initialValue() {
DateFormat ret = new SimpleDateFormat(SERIALIZED_DATE_FORMAT_STR);
ret.setTimeZone(TimeZone.getTimeZone("UTC"));
return ret;
}
};
private final TypeCategory category; private final TypeCategory category;
private String guid = null; private String guid = null;
private String createdBy = null; private String createdBy = null;
...@@ -390,7 +408,7 @@ public abstract class AtlasBaseTypeDef implements java.io.Serializable { ...@@ -390,7 +408,7 @@ public abstract class AtlasBaseTypeDef implements java.io.Serializable {
if (value == null) { if (value == null) {
sb.append(value); sb.append(value);
} else { } else {
sb.append(DATE_FORMATTER.format(value)); sb.append(getDateFormatter().format(value));
} }
return sb; return sb;
......
...@@ -479,7 +479,7 @@ public class AtlasBuiltInTypes { ...@@ -479,7 +479,7 @@ public class AtlasBuiltInTypes {
return new Date(((Number) obj).longValue()); return new Date(((Number) obj).longValue());
} else { } else {
try { try {
return AtlasBaseTypeDef.DATE_FORMATTER.parse(obj.toString()); return AtlasBaseTypeDef.getDateFormatter().parse(obj.toString());
} catch (ParseException excp) { } catch (ParseException excp) {
try { // try to read it as a number try { // try to read it as a number
long longDate = Long.valueOf(obj.toString()); long longDate = Long.valueOf(obj.toString());
......
...@@ -39,8 +39,6 @@ import java.io.IOException; ...@@ -39,8 +39,6 @@ import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Map;
public class AtlasJson { public class AtlasJson {
...@@ -187,7 +185,7 @@ public class AtlasJson { ...@@ -187,7 +185,7 @@ public class AtlasJson {
@Override @Override
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException { public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (value != null) { if (value != null) {
jgen.writeString(AtlasBaseTypeDef.DATE_FORMATTER.format(value)); jgen.writeString(AtlasBaseTypeDef.getDateFormatter().format(value));
} }
} }
} }
...@@ -201,7 +199,7 @@ public class AtlasJson { ...@@ -201,7 +199,7 @@ public class AtlasJson {
if (value != null) { if (value != null) {
try { try {
ret = AtlasBaseTypeDef.DATE_FORMATTER.parse(value); ret = AtlasBaseTypeDef.getDateFormatter().parse(value);
} catch (ParseException excp) { } catch (ParseException excp) {
} }
} }
......
...@@ -236,7 +236,7 @@ public class Id implements Serializable { ...@@ -236,7 +236,7 @@ public class Id implements Serializable {
} }
try { try {
return AtlasBaseTypeDef.DATE_FORMATTER.parse(val.toString()); return AtlasBaseTypeDef.getDateFormatter().parse(val.toString());
} catch (ParseException excp) { } catch (ParseException excp) {
// ignore // ignore
} }
......
...@@ -46,7 +46,7 @@ public class TestAtlasDateType { ...@@ -46,7 +46,7 @@ public class TestAtlasDateType {
private final Object[] invalidValues = { "12ab", "abcd", "-12ab", }; private final Object[] invalidValues = { "12ab", "abcd", "-12ab", };
private final Date now = new Date(); private final Date now = new Date();
private final String strNow = AtlasBaseTypeDef.DATE_FORMATTER.format(now); private final String strNow = AtlasBaseTypeDef.getDateFormatter().format(now);
@Test @Test
public void testDateTypeDefaultValue() { public void testDateTypeDefaultValue() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment