Search in sources :

Example 11 with JSONStringer

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()));
}
Also used : JSONObject(org.json_voltpatches.JSONObject) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) JSONStringer(org.json_voltpatches.JSONStringer) Test(org.junit.Test)

Example 12 with JSONStringer

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();
}
Also used : JSONException(org.json_voltpatches.JSONException) JSONObject(org.json_voltpatches.JSONObject) JSONStringer(org.json_voltpatches.JSONStringer)

Example 13 with JSONStringer

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);
    }
}
Also used : JSONStringer(org.json_voltpatches.JSONStringer) BinaryPayloadMessage(org.voltcore.messaging.BinaryPayloadMessage) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) JSONException(org.json_voltpatches.JSONException) ExecutionException(java.util.concurrent.ExecutionException)

Example 14 with JSONStringer

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);
}
Also used : JSONStringer(org.json_voltpatches.JSONStringer)

Example 15 with JSONStringer

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;
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONStringer(org.json_voltpatches.JSONStringer) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) JSONException(org.json_voltpatches.JSONException) ZKUtil(org.voltcore.zk.ZKUtil)

Aggregations

JSONStringer (org.json_voltpatches.JSONStringer)28 JSONException (org.json_voltpatches.JSONException)18 JSONObject (org.json_voltpatches.JSONObject)7 ExecutionException (java.util.concurrent.ExecutionException)6 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)6 IOException (java.io.IOException)5 Map (java.util.Map)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SocketException (java.net.SocketException)2 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 AbstractExpression (org.voltdb.expressions.AbstractExpression)2 SettingsException (org.voltdb.settings.SettingsException)2 ImmutableSortedMap (com.google_voltpatches.common.collect.ImmutableSortedMap)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 StringWriter (java.io.StringWriter)1 Inet4Address (java.net.Inet4Address)1 Inet6Address (java.net.Inet6Address)1