use of org.json_voltpatches.JSONStringer in project voltdb by VoltDB.
the class TestDRConsumerDrIdTracker method testJsonSerialization.
@Test
public void testJsonSerialization() throws Exception {
tracker.append(5L, 5L, 0L, 0L);
tracker.append(15L, 20L, 0L, 0L);
DRConsumerDrIdTracker tracker2 = DRConsumerDrIdTracker.createPartitionTracker(17L, 0L, 0L, 0);
tracker2.append(20L, 25L, 0L, 0L);
tracker2.append(28L, 28L, 0L, 0L);
Map<Integer, DRConsumerDrIdTracker> perProducerPartitionTrackers = new HashMap<Integer, DRConsumerDrIdTracker>();
perProducerPartitionTrackers.put(0, tracker);
perProducerPartitionTrackers.put(1, tracker2);
Map<Integer, Map<Integer, DRConsumerDrIdTracker>> perSiteTrackers = new HashMap<Integer, Map<Integer, DRConsumerDrIdTracker>>();
// Insert trackers from cluster 20
perSiteTrackers.put(20, perProducerPartitionTrackers);
JSONObject trackersInJSON = ExtensibleSnapshotDigestData.serializeSiteConsumerDrIdTrackersToJSON(perSiteTrackers);
JSONStringer stringer = new JSONStringer();
stringer.object();
// ConsumerPartitionId
stringer.key("5");
stringer.value(trackersInJSON);
stringer.endObject();
String output = stringer.toString();
JSONObject allsiteInfo = new JSONObject(output);
JSONObject siteInfo = allsiteInfo.getJSONObject("5");
final Map<Integer, Map<Integer, DRConsumerDrIdTracker>> siteTrackers = ExtensibleSnapshotDigestData.buildConsumerSiteDrIdTrackersFromJSON(siteInfo);
DRConsumerDrIdTracker tracker3 = siteTrackers.get(20).get(0);
DRConsumerDrIdTracker tracker4 = siteTrackers.get(20).get(1);
assertTrue(tracker.getSafePointDrId() == tracker3.getSafePointDrId());
assertTrue(tracker.getLastSpUniqueId() == tracker3.getLastSpUniqueId());
assertTrue(tracker.getLastMpUniqueId() == tracker3.getLastMpUniqueId());
assertTrue(tracker.getDrIdRanges().equals(tracker3.getDrIdRanges()));
assertTrue(tracker2.getSafePointDrId() == tracker4.getSafePointDrId());
assertTrue(tracker2.getLastSpUniqueId() == tracker4.getLastSpUniqueId());
assertTrue(tracker2.getLastMpUniqueId() == tracker4.getLastMpUniqueId());
assertTrue(tracker2.getDrIdRanges().equals(tracker4.getDrIdRanges()));
}
use of org.json_voltpatches.JSONStringer in project voltdb by VoltDB.
the class ParameterSet method toJSONString.
@Override
public String toJSONString() {
JSONStringer js = new JSONStringer();
try {
js.array();
for (Object o : m_params) {
if (o instanceof Double) {
Double dval = (Double) o;
if (dval.isNaN()) {
js.value(dval.toString());
} else if (dval.isInfinite()) {
js.value(dval.toString());
} else
js.value(o);
} else {
js.value(o);
}
}
js.endArray();
} catch (JSONException e) {
e.printStackTrace();
throw new RuntimeException("Failed to serialize a parameter set to JSON.", e);
}
return js.toString();
}
use of org.json_voltpatches.JSONStringer in project voltdb by VoltDB.
the class Cartographer method sendLeaderChangeNotify.
// This message used to be sent by the SP or MP initiator when they accepted a promotion.
// For dev speed, we'll detect mastership changes here and construct and send this message to the
// local client interface so we can keep the CIs implementation
private void sendLeaderChangeNotify(long hsId, int partitionId) {
try {
JSONStringer stringer = new JSONStringer();
stringer.object();
stringer.keySymbolValuePair(JSON_PARTITION_ID, partitionId);
stringer.keySymbolValuePair(JSON_INITIATOR_HSID, hsId);
stringer.endObject();
BinaryPayloadMessage bpm = new BinaryPayloadMessage(new byte[0], stringer.toString().getBytes("UTF-8"));
int hostId = m_hostMessenger.getHostId();
m_hostMessenger.send(CoreUtils.getHSIdFromHostAndSite(hostId, HostMessenger.CLIENT_INTERFACE_SITE_ID), bpm);
} catch (Exception e) {
VoltDB.crashLocalVoltDB("Unable to propogate leader promotion to client interface.", true, e);
}
}
use of org.json_voltpatches.JSONStringer in project voltdb by VoltDB.
the class ChannelDistributer method asHostData.
/**
* Converts the given a {@link NavigableSet<ChannelSpec> set of channel specs}
* into a byte array with the content of a JSON document
*
* @param specs a a {@link NavigableSet<ChannelSpec> set of channel specs}
* @return a byte array
* @throws JSONException on JSON building failures
* @throws IllegalArgumentException on channel spec encoding failures
*/
static byte[] asHostData(NavigableSet<ChannelSpec> specs) throws JSONException, IllegalArgumentException {
JSONStringer js = new JSONStringer();
js.array();
for (ChannelSpec spec : specs) {
js.value(spec.asJSONValue());
}
js.endArray();
return js.toString().getBytes(StandardCharsets.UTF_8);
}
use of org.json_voltpatches.JSONStringer in project voltdb by VoltDB.
the class SnapshotSaveAPI method createSnapshotCompletionNode.
/**
* Create the completion node for the snapshot identified by the txnId. It
* assumes that all hosts will race to call this, so it doesn't fail if the
* node already exists.
*
* @param nonce Nonce of the snapshot
* @param txnId
* @param hostId The local host ID
* @param isTruncation Whether or not this is a truncation snapshot
* @param truncReqId Optional unique ID fed back to the monitor for identification
* @return true if the node is created successfully, false if the node already exists.
*/
public static ZKUtil.StringCallback createSnapshotCompletionNode(String path, String pathType, String nonce, long txnId, boolean isTruncation, String truncReqId) {
if (!(txnId > 0)) {
VoltDB.crashGlobalVoltDB("Txnid must be greather than 0", true, null);
}
byte[] nodeBytes = null;
try {
JSONStringer stringer = new JSONStringer();
stringer.object();
stringer.keySymbolValuePair("txnId", txnId);
stringer.keySymbolValuePair("isTruncation", isTruncation);
stringer.keySymbolValuePair("didSucceed", false);
stringer.keySymbolValuePair("hostCount", -1);
stringer.keySymbolValuePair(SnapshotUtil.JSON_PATH, path);
stringer.keySymbolValuePair(SnapshotUtil.JSON_PATH_TYPE, pathType);
stringer.keySymbolValuePair(SnapshotUtil.JSON_NONCE, nonce);
stringer.keySymbolValuePair("truncReqId", truncReqId);
stringer.key("exportSequenceNumbers").object().endObject();
stringer.endObject();
JSONObject jsonObj = new JSONObject(stringer.toString());
nodeBytes = jsonObj.toString(4).getBytes(Charsets.UTF_8);
} catch (Exception e) {
VoltDB.crashLocalVoltDB("Error serializing snapshot completion node JSON", true, e);
}
ZKUtil.StringCallback cb = new ZKUtil.StringCallback();
final String snapshotPath = VoltZK.completed_snapshots + "/" + txnId;
VoltDB.instance().getHostMessenger().getZK().create(snapshotPath, nodeBytes, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, cb, null);
return cb;
}
Aggregations