Skip to content
This repository has been archived by the owner on Jan 23, 2020. It is now read-only.

Commit

Permalink
Fixes jackson handling of Writer.close(), updates small-*.json data
Browse files Browse the repository at this point in the history
  • Loading branch information
sagemintblue committed Apr 4, 2013
1 parent 59a347a commit a459d2a
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 1,360 deletions.
Expand Up @@ -32,6 +32,8 @@
import java.util.SortedMap;
import java.util.concurrent.ConcurrentSkipListMap;

import com.google.common.collect.Maps;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -58,9 +60,8 @@ public class InMemoryStatsService implements StatsReadService, StatsWriteService
private static final String DUMP_DAG_FILE_PARAM = "ambrose.write.dag.file";
private static final String DUMP_EVENTS_FILE_PARAM = "ambrose.write.events.file";

private Map<String, DAGNode<Job>> dagNodeNameMap = new HashMap<String, DAGNode<Job>>();
private SortedMap<Integer, Event> eventMap =
new ConcurrentSkipListMap<Integer, Event>();
private Map<String, DAGNode<Job>> dagNodeNameMap = Maps.newHashMap();
private SortedMap<Integer, Event> eventMap = new ConcurrentSkipListMap<Integer, Event>();

private Writer dagWriter = null;
private Writer eventsWriter = null;
Expand Down Expand Up @@ -115,26 +116,23 @@ public synchronized void pushEvent(String workflowId, Event event) throws IOExce

private void writeJsonDagNodenameMapToDisk(Map<String, DAGNode<Job>> dagNodeNameMap) throws IOException {
if (dagWriter != null && dagNodeNameMap != null) {
Collection<DAGNode<Job>> nodes = dagNodeNameMap.values();
JSONUtil.writeJson(dagWriter, nodes.toArray(new DAGNode[dagNodeNameMap.size()]));
JSONUtil.writeJson(dagWriter, dagNodeNameMap.values());
}
}

private void writeJsonEventToDisk(Event event) throws IOException {
if (eventsWriter != null && event != null) {
eventsWriter.append(!eventWritten ? "[ " : ", ");
eventsWriter.write(!eventWritten ? "[ " : ", ");
JSONUtil.writeJson(eventsWriter, event);
eventsWriter.flush();
eventWritten = true;
}
}

public void flushJsonToDisk() throws IOException {

if (dagWriter != null) { dagWriter.close(); }

if (eventsWriter != null) {
if (eventWritten) { eventsWriter.append("]\n"); }
if (eventWritten) { eventsWriter.write(" ]\n"); }
eventsWriter.close();
}
}
Expand Down
31 changes: 20 additions & 11 deletions common/src/main/java/com/twitter/ambrose/util/JSONUtil.java
Expand Up @@ -25,6 +25,7 @@
import java.nio.charset.Charset;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -37,14 +38,6 @@
* @author billg
*/
public class JSONUtil {
private static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}

/**
* Writes object to the writer as JSON using Jackson and adds a new-line before flushing.
*
Expand All @@ -54,12 +47,15 @@ public class JSONUtil {
*/
public static void writeJson(Writer writer, Object object) throws IOException {
mapper.writeValue(writer, object);
writer.write("\n");
writer.flush();
}

public static void writeJson(String fileName, Object object) throws IOException {
JSONUtil.writeJson(new PrintWriter(fileName), object);
Writer writer = new PrintWriter(fileName);
try {
JSONUtil.writeJson(writer, object);
} finally {
writer.close();
}
}

public static Object readJson(String json, TypeReference<?> type) throws IOException {
Expand All @@ -76,4 +72,17 @@ public static String readFile(String path) throws IOException {
stream.close();
}
}

private static final ObjectMapper mapper = new ObjectMapper();

static {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);
mapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
mapper.disable(SerializationFeature.CLOSE_CLOSEABLE);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
}
30 changes: 15 additions & 15 deletions common/src/main/resources/web/data/small-dag.json
@@ -1,25 +1,25 @@
[ {
"name" : "scope-44",
"name" : "scope-148",
"job" : {
"aliases" : [ "JOINED", "L", "R" ],
"features" : [ "HASH_JOIN" ],
"runtime" : "pig"
"runtime" : "pig",
"aliases" : [ "1-115", "1-116", "enemies_users_histogram", "friends_users_histogram", "masochists", "narcissists", "user", "user_counts" ],
"features" : [ "MULTI_QUERY", "COMBINER" ]
},
"successorNames" : [ "scope-48" ]
"successorNames" : [ "scope-168" ]
}, {
"name" : "scope-48",
"name" : "scope-168",
"job" : {
"aliases" : [ "ORDERED" ],
"features" : [ "SAMPLER" ],
"runtime" : "pig"
"runtime" : "pig",
"aliases" : [ "1-117", "user_enemy", "user_enemy2", "user_enemy_enemy" ],
"features" : [ "HASH_JOIN" ]
},
"successorNames" : [ "scope-63" ]
"successorNames" : [ "scope-172" ]
}, {
"name" : "scope-63",
"name" : "scope-172",
"job" : {
"aliases" : [ "ORDERED" ],
"features" : [ "ORDER_BY" ],
"runtime" : "pig"
"runtime" : "pig",
"aliases" : [ "1-118", "1-119", "1-120", "name", "user_enemies_of_enemies" ],
"features" : [ "GROUP_BY", "DISTINCT" ]
},
"successorNames" : [ ]
} ]
} ]

0 comments on commit a459d2a

Please sign in to comment.