Skip to content

Commit

Permalink
Support newer JSON history format (#2001)
Browse files Browse the repository at this point in the history
  • Loading branch information
cretz authored Mar 5, 2024
1 parent b182d78 commit 0e4ef15
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public static WorkflowExecutionHistory fromJson(String serialized, String workfl
}

/**
* @return full json that can be used for replay and which is compatible with tctl
* @param prettyPrint Whether to pretty print the JSON.
* @return Full json that can be used for replay.
*/
public String toJson(boolean prettyPrint) {
return super.toJson(prettyPrint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ public static String protoJsonToHistoryFormatJson(String protoJson) {
}

public static String historyFormatJsonToProtoJson(String historyFormatJson) {
return convertEnumValues(historyFormatJson, ProtoEnumNameUtils::simplifiedToUniqueName);
return convertEnumValues(
historyFormatJson,
(enumName, prefix) -> {
// Only convert if the enum name isn't already converted
if (enumName.indexOf('_') >= 0) {
return enumName;
}
return ProtoEnumNameUtils.simplifiedToUniqueName(enumName, prefix);
});
}

private static String convertEnumValues(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,33 @@ private static void checkHistory(History history) {
}

/**
* @return full json that can be used for replay and which is compatible with tctl
* @param prettyPrint Whether to pretty print the JSON.
* @return Full json that can be used for replay.
*/
public String toJson(boolean prettyPrint) {
return toJson(prettyPrint, false);
}

/**
* @param prettyPrint Whether to pretty print the JSON.
* @param legacyFormat If true, will use the older-style protobuf enum formatting as done by
* Temporal tooling in the past. This is not recommended.
* @return Full JSON that can be used for replay.
*/
public String toJson(boolean prettyPrint, boolean legacyFormat) {
JsonFormat.Printer printer = JsonFormat.printer();
try {
String protoJson = printer.print(history);
String historyFormatJson = HistoryJsonUtils.protoJsonToHistoryFormatJson(protoJson);
if (legacyFormat) {
protoJson = HistoryJsonUtils.protoJsonToHistoryFormatJson(protoJson);
}

if (prettyPrint) {
@SuppressWarnings("deprecation")
JsonElement je = GSON_PARSER.parse(historyFormatJson);
JsonElement je = GSON_PARSER.parse(protoJson);
return GSON_PRETTY_PRINTER.toJson(je);
} else {
return historyFormatJson;
return protoJson;
}
} catch (InvalidProtocolBufferException e) {
throw new DataConverterException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package io.temporal.internal.common;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

import com.google.common.io.CharStreams;
import io.temporal.common.WorkflowExecutionHistory;
Expand Down Expand Up @@ -62,6 +62,7 @@ public void deserializeAndSerializeBackComplexHistory() throws IOException {
}

public void deserializeAndSerializeBack(String resourceName) throws IOException {
// Load legacy-format history
ClassLoader classLoader = WorkflowExecutionUtils.class.getClassLoader();
URL resource = classLoader.getResource(resourceName);
String historyUrl = resource.getFile();
Expand All @@ -70,10 +71,30 @@ public void deserializeAndSerializeBack(String resourceName) throws IOException
try (Reader reader = Files.newBufferedReader(historyFile.toPath(), UTF_8)) {
originalSerializedJsonHistory = CharStreams.toString(reader);
}
originalSerializedJsonHistory = originalSerializedJsonHistory.replace("\r\n", "\n");

// Confirm original history is legacy format
assertTrue(
originalSerializedJsonHistory.contains("\"eventType\": \"WorkflowExecutionStarted\""));
assertFalse(
originalSerializedJsonHistory.contains(
"\"eventType\": \"EVENT_TYPE_WORKFLOW_EXECUTION_STARTED\""));

WorkflowExecutionHistory history = WorkflowHistoryLoader.readHistoryFromResource(resourceName);

String serializedHistory = history.toJson(true);
assertEquals(originalSerializedJsonHistory, serializedHistory);
// Confirm serialized to old matches char-for-char
assertEquals(originalSerializedJsonHistory, history.toJson(true, true));

// Confirm can convert to new-format
String newFormatSerializedHistory = history.toJson(true);
assertFalse(newFormatSerializedHistory.contains("\"eventType\": \"WorkflowExecutionStarted\""));
assertTrue(
newFormatSerializedHistory.contains(
"\"eventType\": \"EVENT_TYPE_WORKFLOW_EXECUTION_STARTED\""));

// And that new format is parsed correctly
WorkflowExecutionHistory newFormatHistory =
WorkflowExecutionHistory.fromJson(newFormatSerializedHistory);
assertEquals(history.getHistory(), newFormatHistory.getHistory());
}
}

0 comments on commit 0e4ef15

Please sign in to comment.