Search in sources :

Example 66 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class WindowFunctionPlanNode method loadFromJSONObject.

/**
     * Deserialize a PartitionByPlanNode from JSON.  Since we don't need the
     * sort directions, and we don't serialize them in toJSONString, then we
     * can't in general get them here.
     */
@Override
public void loadFromJSONObject(JSONObject jobj, Database db) throws JSONException {
    helpLoadFromJSONObject(jobj, db);
    JSONArray jarray = jobj.getJSONArray(Members.AGGREGATE_COLUMNS.name());
    int size = jarray.length();
    for (int i = 0; i < size; i++) {
        // We only expect one of these for now.
        assert (i == 0);
        JSONObject tempObj = jarray.getJSONObject(i);
        m_aggregateTypes.add(ExpressionType.get(tempObj.getString(Members.AGGREGATE_TYPE.name())));
        m_aggregateOutputColumns.add(tempObj.getInt(Members.AGGREGATE_OUTPUT_COLUMN.name()));
        m_aggregateExpressions.add(AbstractExpression.loadFromJSONArrayChild(null, tempObj, Members.AGGREGATE_EXPRESSIONS.name(), null));
    }
    m_partitionByExpressions = AbstractExpression.loadFromJSONArrayChild(null, jobj, Members.PARTITIONBY_EXPRESSIONS.name(), null);
    m_orderByExpressions = new ArrayList<>();
    AbstractExpression.loadSortListFromJSONArray(m_orderByExpressions, null, jobj);
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONArray(org.json_voltpatches.JSONArray)

Example 67 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class HTTPUtils method responseFromJSON.

public static Response responseFromJSON(String jsonStr) throws JSONException, IOException {
    Response response = new Response();
    JSONObject jsonObj = new JSONObject(jsonStr);
    JSONArray resultsJson = jsonObj.getJSONArray("results");
    response.results = new VoltTable[resultsJson.length()];
    for (int i = 0; i < response.results.length; i++) {
        JSONObject tableJson = resultsJson.getJSONObject(i);
        response.results[i] = VoltTable.fromJSONObject(tableJson);
        System.out.println(response.results[i].toString());
    }
    if (jsonObj.isNull("status") == false) {
        response.status = (byte) jsonObj.getInt("status");
    }
    if (jsonObj.isNull("appstatus") == false) {
        response.appStatus = (byte) jsonObj.getInt("appstatus");
    }
    if (jsonObj.isNull("statusstring") == false) {
        response.statusString = jsonObj.getString("statusstring");
    }
    if (jsonObj.isNull("appstatusstring") == false) {
        response.appStatusString = jsonObj.getString("appstatusstring");
    }
    if (jsonObj.isNull("exception") == false) {
        response.exception = jsonObj.getString("exception");
    }
    return response;
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONArray(org.json_voltpatches.JSONArray)

Example 68 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class SnapshotUtil method CRCCheck.

/**
     *
     * This isn't just a CRC check. It also loads the file and returns it as
     * a JSON object.
     * Check if the CRC of the snapshot digest. Note that this only checks if
     * the CRC at the beginning of the digest file matches the CRC of the digest
     * file itself.
     *
     * @param f
     *            The snapshot digest file object
     * @return The table list as a string
     * @throws IOException
     *             If CRC does not match
     */
public static JSONObject CRCCheck(File f, VoltLogger logger) throws IOException {
    final FileInputStream fis = new FileInputStream(f);
    try {
        final BufferedInputStream bis = new BufferedInputStream(fis);
        ByteBuffer crcBuffer = ByteBuffer.allocate(4);
        if (4 != bis.read(crcBuffer.array())) {
            logger.warn("EOF while attempting to read CRC from snapshot digest " + f + " on host " + CoreUtils.getHostnameOrAddress());
            return null;
        }
        final int crc = crcBuffer.getInt();
        final InputStreamReader isr = new InputStreamReader(bis, StandardCharsets.UTF_8);
        CharArrayWriter caw = new CharArrayWriter();
        while (true) {
            int nextChar = isr.read();
            if (nextChar == -1) {
                break;
            }
            //digests
            if (nextChar == '\n') {
                break;
            }
            caw.write(nextChar);
        }
        /*
             * Try and parse the contents as a JSON object. If it succeeds then assume
             * it is a the new version of the digest file. It is unlikely the old version
             * will successfully parse as JSON because it starts with a number
             * instead of an open brace.
             */
        JSONObject obj = null;
        try {
            obj = new JSONObject(caw.toString());
        } catch (JSONException e) {
        //assume it is the old format
        }
        /*
             * Convert the old style file to a JSONObject so it can be presented
             * via a consistent interface.
             */
        if (obj == null) {
            String tableList = caw.toString();
            byte[] tableListBytes = tableList.getBytes(StandardCharsets.UTF_8);
            PureJavaCrc32 tableListCRC = new PureJavaCrc32();
            tableListCRC.update(tableListBytes);
            tableListCRC.update("\n".getBytes(StandardCharsets.UTF_8));
            final int calculatedValue = (int) tableListCRC.getValue();
            if (crc != calculatedValue) {
                logger.warn("CRC of snapshot digest " + f + " did not match digest contents");
                return null;
            }
            String[] tableNames = tableList.split(",");
            long txnId = Long.valueOf(tableNames[0]);
            obj = new JSONObject();
            try {
                obj.put("version", 0);
                obj.put("txnId", txnId);
                for (int ii = 1; ii < tableNames.length; ii++) {
                    obj.append("tables", tableNames[ii]);
                }
            } catch (JSONException e) {
                logger.warn("Exception parsing JSON of digest " + f, e);
                return null;
            }
            return obj;
        } else {
            /*
                 * Verify the CRC and then return the data as a JSON object.
                 */
            String tableList = caw.toString();
            byte[] tableListBytes = tableList.getBytes(StandardCharsets.UTF_8);
            PureJavaCrc32 tableListCRC = new PureJavaCrc32();
            tableListCRC.update(tableListBytes);
            final int calculatedValue = (int) tableListCRC.getValue();
            if (crc != calculatedValue) {
                logger.warn("CRC of snapshot digest " + f + " did not match digest contents");
                return null;
            }
            return obj;
        }
    } catch (Exception e) {
        logger.warn("Exception while parsing snapshot digest " + f, e);
        return null;
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException e) {
        }
    }
}
Also used : PureJavaCrc32(org.apache.hadoop_voltpatches.util.PureJavaCrc32) InputStreamReader(java.io.InputStreamReader) JSONException(org.json_voltpatches.JSONException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) CharArrayWriter(java.io.CharArrayWriter) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json_voltpatches.JSONException) ForwardClientException(org.voltdb.SnapshotDaemon.ForwardClientException) IOException(java.io.IOException) JSONObject(org.json_voltpatches.JSONObject) BufferedInputStream(java.io.BufferedInputStream)

Example 69 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class StreamSnapshotRequestConfig method parseStreams.

private ImmutableList<Stream> parseStreams(JSONObject jsData) {
    ImmutableList.Builder<Stream> builder = ImmutableList.builder();
    try {
        JSONArray streamArray = jsData.getJSONArray("streams");
        for (int i = 0; i < streamArray.length(); i++) {
            JSONObject streamObj = streamArray.getJSONObject(i);
            Integer newPartition = null;
            if (!streamObj.isNull("newPartition")) {
                newPartition = Integer.parseInt(streamObj.getString("newPartition"));
            }
            Stream config = new Stream(parseStreamPairs(streamObj), newPartition);
            builder.add(config);
        }
    } catch (JSONException e) {
        SNAP_LOG.warn("Failed to parse stream snapshot request config", e);
    }
    return builder.build();
}
Also used : JSONObject(org.json_voltpatches.JSONObject) ImmutableList(com.google_voltpatches.common.collect.ImmutableList) JSONArray(org.json_voltpatches.JSONArray) JSONException(org.json_voltpatches.JSONException)

Example 70 with JSONObject

use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.

the class TestCartographer method testSPMasterChange.

@Test
public void testSPMasterChange() throws Exception {
    ZooKeeper zk = getClient(0);
    VoltZK.createPersistentZKNodes(zk);
    LeaderCache spwriter = new LeaderCache(zk, VoltZK.iv2masters);
    HostMessenger hm = mock(HostMessenger.class);
    when(hm.getZK()).thenReturn(m_messengers.get(0).getZK());
    Cartographer dut = new Cartographer(hm, 0, false);
    // Startup partitions
    spwriter.start(true);
    spwriter.put(0, 0l);
    verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
    reset(hm);
    spwriter.put(1, 1l);
    verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
    reset(hm);
    spwriter.put(2, 2l);
    verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
    reset(hm);
    // now change master for part 0
    spwriter.put(0, 3l);
    ArgumentCaptor<Long> hsIdCaptor = ArgumentCaptor.forClass(Long.class);
    ArgumentCaptor<BinaryPayloadMessage> bpmCaptor = ArgumentCaptor.forClass(BinaryPayloadMessage.class);
    verify(hm, timeout(10000)).send(hsIdCaptor.capture(), bpmCaptor.capture());
    JSONObject jsObj = new JSONObject(new String(bpmCaptor.getValue().m_payload, "UTF-8"));
    System.out.println("BPM: " + jsObj.toString());
    final int partitionId = jsObj.getInt(Cartographer.JSON_PARTITION_ID);
    final long initiatorHSId = jsObj.getLong(Cartographer.JSON_INITIATOR_HSID);
    assertEquals(0, partitionId);
    assertEquals(3, initiatorHSId);
    spwriter.shutdown();
}
Also used : BinaryPayloadMessage(org.voltcore.messaging.BinaryPayloadMessage) VoltMessage(org.voltcore.messaging.VoltMessage) ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) JSONObject(org.json_voltpatches.JSONObject) HostMessenger(org.voltcore.messaging.HostMessenger) Matchers.anyLong(org.mockito.Matchers.anyLong) Test(org.junit.Test)

Aggregations

JSONObject (org.json_voltpatches.JSONObject)123 JSONException (org.json_voltpatches.JSONException)45 JSONArray (org.json_voltpatches.JSONArray)30 IOException (java.io.IOException)20 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)18 HashMap (java.util.HashMap)17 File (java.io.File)14 Map (java.util.Map)14 ByteBuffer (java.nio.ByteBuffer)13 ExecutionException (java.util.concurrent.ExecutionException)12 ZooKeeper (org.apache.zookeeper_voltpatches.ZooKeeper)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)8 TreeSet (java.util.TreeSet)6 JSONStringer (org.json_voltpatches.JSONStringer)6 HashSet (java.util.HashSet)5 BinaryPayloadMessage (org.voltcore.messaging.BinaryPayloadMessage)5 ImmutableList (com.google_voltpatches.common.collect.ImmutableList)4 ImmutableMap (com.google_voltpatches.common.collect.ImmutableMap)4 InetAddress (java.net.InetAddress)4