Search in sources :

Example 46 with JSONException

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

the class TopologyZKUtils method readTopologyFromZK.

public static AbstractTopology readTopologyFromZK(ZooKeeper zk) {
    AbstractTopology topology = null;
    try {
        byte[] data = zk.getData(VoltZK.topology, false, null);
        String jsonTopology = new String(data, Charsets.UTF_8);
        topology = AbstractTopology.topologyFromJSON(jsonTopology);
    } catch (KeeperException | InterruptedException | JSONException e) {
        VoltDB.crashLocalVoltDB("Unable to read topology from ZK, dying", true, e);
    }
    return topology;
}
Also used : AbstractTopology(org.voltdb.AbstractTopology) JSONException(org.json_voltpatches.JSONException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Example 47 with JSONException

use of org.json_voltpatches.JSONException 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 48 with JSONException

use of org.json_voltpatches.JSONException 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 49 with JSONException

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

the class ExportBenchmark method getStatsMessage.

/**
     * Parses a received statistics message & logs the information
     * @param channel   The channel with the incoming packet
     */
private void getStatsMessage(DatagramChannel channel) {
    String message = null;
    // Read the data
    try {
        buffer.clear();
        channel.receive(buffer);
        buffer.flip();
        int messageLength = buffer.get();
        if (messageLength > buffer.capacity()) {
            System.out.println("WARN: packet exceeds allocate size; message truncated");
        }
        byte[] localBuf = new byte[messageLength];
        buffer.get(localBuf, 0, messageLength);
        message = new String(localBuf);
    } catch (IOException e) {
        exitWithException("Couldn't read from socket", e);
    }
    // Parse the stats message
    JSONObject json;
    try {
        json = new JSONObject(message);
    } catch (JSONException e) {
        System.err.println("Received invalid JSON: " + e.getLocalizedMessage());
        return;
    }
    final Integer partitionId;
    final Long transactions;
    final Long decode;
    final Long startTime;
    final Long endTime;
    try {
        partitionId = new Integer(json.getInt("partitionId"));
        transactions = new Long(json.getLong("transactions"));
        decode = new Long(json.getLong("decodeTime"));
        startTime = new Long(json.getLong("startTime"));
        endTime = new Long(json.getLong("endTime"));
    } catch (JSONException e) {
        System.err.println("Unable to parse JSON " + e.getLocalizedMessage());
        return;
    }
    // This should always be true
    if (transactions > 0 && decode > 0 && startTime > 0 && endTime > startTime) {
        serverStats.add(new StatClass(partitionId, transactions, decode, startTime, endTime));
        if (startTime < serverStartTS || serverStartTS == 0) {
            serverStartTS = startTime;
        }
        if (endTime > serverEndTS) {
            serverEndTS = endTime;
        }
        if (partitionId > partCount) {
            partCount = partitionId;
        }
        decodeTime += decode;
    } else // If the else is called it means we received invalid data from the export client
    {
        System.out.println("WARN: invalid data received - partitionId: " + partitionId + " | transactions: " + transactions + " | decode: " + decode + " | startTime: " + startTime + " | endTime: " + endTime);
    }
}
Also used : JSONObject(org.json_voltpatches.JSONObject) AtomicLong(java.util.concurrent.atomic.AtomicLong) JSONException(org.json_voltpatches.JSONException) IOException(java.io.IOException)

Example 50 with JSONException

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

the class CatalogContext method getDebuggingInfoFromCatalog.

// Generate helpful status messages based on configuration present in the
// catalog.  Used to generated these messages at startup and after an
// @UpdateApplicationCatalog
SortedMap<String, String> getDebuggingInfoFromCatalog(boolean verbose) {
    SortedMap<String, String> logLines = new TreeMap<>();
    // topology
    Deployment deployment = cluster.getDeployment().iterator().next();
    int hostCount = m_dbSettings.getCluster().hostcount();
    if (verbose) {
        Map<Integer, Integer> sphMap;
        try {
            sphMap = m_messenger.getSitesPerHostMapFromZK();
        } catch (KeeperException | InterruptedException | JSONException e) {
            hostLog.warn("Failed to get sitesperhost information from Zookeeper", e);
            sphMap = null;
        }
        int kFactor = deployment.getKfactor();
        if (sphMap == null) {
            logLines.put("deployment1", String.format("Cluster has %d hosts with leader hostname: \"%s\". [unknown] local sites count. K = %d.", hostCount, VoltDB.instance().getConfig().m_leader, kFactor));
            logLines.put("deployment2", "Unable to retrieve partition information from the cluster.");
        } else {
            int localSitesCount = sphMap.get(m_messenger.getHostId());
            logLines.put("deployment1", String.format("Cluster has %d hosts with leader hostname: \"%s\". %d local sites count. K = %d.", hostCount, VoltDB.instance().getConfig().m_leader, localSitesCount, kFactor));
            int totalSitesCount = 0;
            for (Map.Entry<Integer, Integer> e : sphMap.entrySet()) {
                totalSitesCount += e.getValue();
            }
            int replicas = kFactor + 1;
            int partitionCount = totalSitesCount / replicas;
            logLines.put("deployment2", String.format("The entire cluster has %d %s of%s %d logical partition%s.", replicas, replicas > 1 ? "copies" : "copy", partitionCount > 1 ? " each of the" : "", partitionCount, partitionCount > 1 ? "s" : ""));
        }
    }
    // voltdb root
    logLines.put("voltdbroot", "Using \"" + VoltDB.instance().getVoltDBRootPath() + "\" for voltdbroot directory.");
    // partition detection
    if (cluster.getNetworkpartition()) {
        logLines.put("partition-detection", "Detection of network partitions in the cluster is enabled.");
    } else {
        logLines.put("partition-detection", "Detection of network partitions in the cluster is not enabled.");
    }
    // security info
    if (cluster.getSecurityenabled()) {
        logLines.put("sec-enabled", "Client authentication is enabled.");
    } else {
        logLines.put("sec-enabled", "Client authentication is not enabled. Anonymous clients accepted.");
    }
    // auto snapshot info
    SnapshotSchedule ssched = database.getSnapshotschedule().get("default");
    if (ssched == null || !ssched.getEnabled()) {
        logLines.put("snapshot-schedule1", "No schedule set for automated snapshots.");
    } else {
        final String frequencyUnitString = ssched.getFrequencyunit().toLowerCase();
        final char frequencyUnit = frequencyUnitString.charAt(0);
        String msg = "[unknown frequency]";
        switch(frequencyUnit) {
            case 's':
                msg = String.valueOf(ssched.getFrequencyvalue()) + " seconds";
                break;
            case 'm':
                msg = String.valueOf(ssched.getFrequencyvalue()) + " minutes";
                break;
            case 'h':
                msg = String.valueOf(ssched.getFrequencyvalue()) + " hours";
                break;
        }
        logLines.put("snapshot-schedule1", "Automatic snapshots enabled, saved to " + VoltDB.instance().getSnapshotPath() + " and named with prefix '" + ssched.getPrefix() + "'.");
        logLines.put("snapshot-schedule2", "Database will retain a history of " + ssched.getRetain() + " snapshots, generated every " + msg + ".");
    }
    return logLines;
}
Also used : Deployment(org.voltdb.catalog.Deployment) JSONException(org.json_voltpatches.JSONException) TreeMap(java.util.TreeMap) SnapshotSchedule(org.voltdb.catalog.SnapshotSchedule) Map(java.util.Map) TreeMap(java.util.TreeMap) CatalogMap(org.voltdb.catalog.CatalogMap) SortedMap(java.util.SortedMap) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Aggregations

JSONException (org.json_voltpatches.JSONException)76 JSONObject (org.json_voltpatches.JSONObject)36 AbstractExpression (org.voltdb.expressions.AbstractExpression)17 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)13 ColumnRef (org.voltdb.catalog.ColumnRef)13 JSONArray (org.json_voltpatches.JSONArray)12 JSONStringer (org.json_voltpatches.JSONStringer)12 Column (org.voltdb.catalog.Column)12 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)11 HashMap (java.util.HashMap)9 Map (java.util.Map)9 File (java.io.File)8 Constraint (org.voltdb.catalog.Constraint)8 TupleValueExpression (org.voltdb.expressions.TupleValueExpression)8 HashSet (java.util.HashSet)7 Table (org.voltdb.catalog.Table)7 Index (org.voltdb.catalog.Index)6 TreeMap (java.util.TreeMap)5 ExecutionException (java.util.concurrent.ExecutionException)5