Search in sources :

Example 6 with JSONStringer

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

the class RealVoltDB method collectLocalNetworkMetadata.

void collectLocalNetworkMetadata() {
    boolean threw = false;
    JSONStringer stringer = new JSONStringer();
    try {
        stringer.object();
        stringer.key("interfaces").array();
        if (m_config.m_externalInterface.equals("")) {
            LinkedList<NetworkInterface> interfaces = new LinkedList<>();
            try {
                Enumeration<NetworkInterface> intfEnum = NetworkInterface.getNetworkInterfaces();
                while (intfEnum.hasMoreElements()) {
                    NetworkInterface intf = intfEnum.nextElement();
                    if (intf.isLoopback() || !intf.isUp()) {
                        continue;
                    }
                    interfaces.offer(intf);
                }
            } catch (SocketException e) {
                throw new RuntimeException(e);
            }
            if (interfaces.isEmpty()) {
                stringer.value("localhost");
            } else {
                boolean addedIp = false;
                while (!interfaces.isEmpty()) {
                    NetworkInterface intf = interfaces.poll();
                    Enumeration<InetAddress> inetAddrs = intf.getInetAddresses();
                    Inet6Address inet6addr = null;
                    Inet4Address inet4addr = null;
                    while (inetAddrs.hasMoreElements()) {
                        InetAddress addr = inetAddrs.nextElement();
                        if (addr instanceof Inet6Address) {
                            inet6addr = (Inet6Address) addr;
                            if (inet6addr.isLinkLocalAddress()) {
                                inet6addr = null;
                            }
                        } else if (addr instanceof Inet4Address) {
                            inet4addr = (Inet4Address) addr;
                        }
                    }
                    if (inet4addr != null) {
                        stringer.value(inet4addr.getHostAddress());
                        addedIp = true;
                    }
                    if (inet6addr != null) {
                        stringer.value(inet6addr.getHostAddress());
                        addedIp = true;
                    }
                }
                if (!addedIp) {
                    stringer.value("localhost");
                }
            }
        } else {
            stringer.value(m_config.m_externalInterface);
        }
    } catch (Exception e) {
        threw = true;
        hostLog.warn("Error while collecting data about local network interfaces", e);
    }
    try {
        if (threw) {
            stringer = new JSONStringer();
            stringer.object();
            stringer.key("interfaces").array();
            stringer.value("localhost");
            stringer.endArray();
        } else {
            stringer.endArray();
        }
        stringer.keySymbolValuePair("clientPort", m_config.m_port);
        stringer.keySymbolValuePair("clientInterface", m_config.m_clientInterface);
        stringer.keySymbolValuePair("adminPort", m_config.m_adminPort);
        stringer.keySymbolValuePair("adminInterface", m_config.m_adminInterface);
        stringer.keySymbolValuePair("httpPort", m_config.m_httpPort);
        stringer.keySymbolValuePair("httpInterface", m_config.m_httpPortInterface);
        stringer.keySymbolValuePair("internalPort", m_config.m_internalPort);
        stringer.keySymbolValuePair("internalInterface", m_config.m_internalInterface);
        String[] zkInterface = m_config.m_zkInterface.split(":");
        stringer.keySymbolValuePair("zkPort", zkInterface[1]);
        stringer.keySymbolValuePair("zkInterface", zkInterface[0]);
        stringer.keySymbolValuePair("drPort", VoltDB.getReplicationPort(m_catalogContext.cluster.getDrproducerport()));
        stringer.keySymbolValuePair("drInterface", VoltDB.getDefaultReplicationInterface());
        stringer.keySymbolValuePair("publicInterface", m_config.m_publicInterface);
        stringer.endObject();
        JSONObject obj = new JSONObject(stringer.toString());
        // possibly atomic swap from null to realz
        m_localMetadata = obj.toString(4);
        hostLog.debug("System Metadata is: " + m_localMetadata);
    } catch (Exception e) {
        hostLog.warn("Failed to collect data about lcoal network interfaces", e);
    }
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) LinkedList(java.util.LinkedList) SocketException(java.net.SocketException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JSONException(org.json_voltpatches.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) SettingsException(org.voltdb.settings.SettingsException) JSONObject(org.json_voltpatches.JSONObject) JSONStringer(org.json_voltpatches.JSONStringer) InetAddress(java.net.InetAddress)

Example 7 with JSONStringer

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

the class JoinCoordinator method makeSnapshotRequest.

public static String makeSnapshotRequest(SnapshotRequestConfig config) {
    try {
        JSONStringer jsStringer = new JSONStringer();
        jsStringer.object();
        config.toJSONString(jsStringer);
        jsStringer.endObject();
        return jsStringer.toString();
    } catch (Exception e) {
        VoltDB.crashLocalVoltDB("Failed to serialize to JSON", true, e);
    }
    // unreachable;
    return null;
}
Also used : JSONStringer(org.json_voltpatches.JSONStringer) JSONException(org.json_voltpatches.JSONException) ExecutionException(java.util.concurrent.ExecutionException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Example 8 with JSONStringer

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

the class ExecuteTask_SP method jsonifyTrackedDRData.

public static String jsonifyTrackedDRData(Pair<Long, Long> lastConsumerUniqueIds, Map<Integer, Map<Integer, DRConsumerDrIdTracker>> allProducerTrackers) throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.object();
    stringer.keySymbolValuePair("lastConsumerSpUniqueId", lastConsumerUniqueIds.getFirst());
    stringer.keySymbolValuePair("lastConsumerMpUniqueId", lastConsumerUniqueIds.getSecond());
    stringer.key("trackers").object();
    if (allProducerTrackers != null) {
        for (Map.Entry<Integer, Map<Integer, DRConsumerDrIdTracker>> clusterTrackers : allProducerTrackers.entrySet()) {
            stringer.key(Integer.toString(clusterTrackers.getKey())).object();
            for (Map.Entry<Integer, DRConsumerDrIdTracker> e : clusterTrackers.getValue().entrySet()) {
                stringer.key(e.getKey().toString());
                stringer.value(e.getValue().toJSON());
            }
            stringer.endObject();
        }
    }
    stringer.endObject();
    stringer.endObject();
    return stringer.toString();
}
Also used : DRConsumerDrIdTracker(org.voltdb.DRConsumerDrIdTracker) JSONStringer(org.json_voltpatches.JSONStringer) Map(java.util.Map)

Example 9 with JSONStringer

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

the class SnapshotUtil method writeSnapshotDigest.

/**
     * Create a digest for a snapshot
     * @param txnId   transaction ID when snapshot was initiated
     * @param path    path to which snapshot files will be written
     * @param nonce   nonce used to distinguish this snapshot
     * @param tables   List of tables present in this snapshot
     * @param hostId   Host ID where this is happening
     * @param extraSnapshotData persisted export, DR, etc state
     * @throws IOException
     */
public static Runnable writeSnapshotDigest(long txnId, long catalogCRC, String path, String pathType, String nonce, List<Table> tables, int hostId, Map<Integer, Long> partitionTransactionIds, ExtensibleSnapshotDigestData extraSnapshotData, InstanceId instanceId, long timestamp, int newPartitionCount, int clusterId) throws IOException {
    final File f = new VoltFile(path, constructDigestFilenameForNonce(nonce, hostId));
    if (f.exists()) {
        if (!f.delete()) {
            throw new IOException("Unable to write table list file " + f);
        }
    }
    boolean success = false;
    try {
        final FileOutputStream fos = new FileOutputStream(f);
        StringWriter sw = new StringWriter();
        JSONStringer stringer = new JSONStringer();
        try {
            stringer.object();
            stringer.keySymbolValuePair("version", 1);
            stringer.keySymbolValuePair("clusterid", clusterId);
            stringer.keySymbolValuePair("txnId", txnId);
            stringer.keySymbolValuePair("timestamp", timestamp);
            stringer.keySymbolValuePair("timestampString", SnapshotUtil.formatHumanReadableDate(timestamp));
            stringer.keySymbolValuePair("newPartitionCount", newPartitionCount);
            stringer.key("tables").array();
            for (int ii = 0; ii < tables.size(); ii++) {
                stringer.value(tables.get(ii).getTypeName());
            }
            stringer.endArray();
            stringer.key("partitionTransactionIds").object();
            for (Map.Entry<Integer, Long> entry : partitionTransactionIds.entrySet()) {
                stringer.key(entry.getKey().toString()).value(entry.getValue());
            }
            stringer.endObject();
            stringer.keySymbolValuePair("catalogCRC", catalogCRC);
            stringer.key("instanceId").value(instanceId.serializeToJSONObject());
            extraSnapshotData.writeToSnapshotDigest(stringer);
            stringer.endObject();
        } catch (JSONException e) {
            throw new IOException(e);
        }
        sw.append(stringer.toString());
        final byte[] tableListBytes = sw.getBuffer().toString().getBytes(StandardCharsets.UTF_8);
        final PureJavaCrc32 crc = new PureJavaCrc32();
        crc.update(tableListBytes);
        ByteBuffer fileBuffer = ByteBuffer.allocate(tableListBytes.length + 4);
        fileBuffer.putInt((int) crc.getValue());
        fileBuffer.put(tableListBytes);
        fileBuffer.flip();
        fos.getChannel().write(fileBuffer);
        success = true;
        return new Runnable() {

            @Override
            public void run() {
                try {
                    fos.getChannel().force(true);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } finally {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
    } finally {
        if (!success) {
            f.delete();
        }
    }
}
Also used : PureJavaCrc32(org.apache.hadoop_voltpatches.util.PureJavaCrc32) JSONException(org.json_voltpatches.JSONException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) StringWriter(java.io.StringWriter) VoltFile(org.voltdb.utils.VoltFile) FileOutputStream(java.io.FileOutputStream) VoltFile(org.voltdb.utils.VoltFile) File(java.io.File) JSONStringer(org.json_voltpatches.JSONStringer) Map(java.util.Map) CatalogMap(org.voltdb.catalog.CatalogMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 10 with JSONStringer

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

the class SnapshotPredicates method toBytes.

public byte[] toBytes() {
    byte[][] predicates = new byte[m_predicates.size()][];
    int i = 0;
    int size = 0;
    try {
        for (Pair<AbstractExpression, Boolean> p : m_predicates) {
            final AbstractExpression predicate = p.getFirst();
            JSONStringer stringer = new JSONStringer();
            stringer.object();
            stringer.keySymbolValuePair("triggersDelete", p.getSecond());
            // overhead when there is only one data target that wants all the rows.
            if (predicate != null) {
                stringer.key("predicateExpression").object();
                predicate.toJSONString(stringer);
                stringer.endObject();
            }
            stringer.endObject();
            predicates[i] = stringer.toString().getBytes(Charsets.UTF_8);
            size += predicates[i].length;
            i++;
        }
    } catch (Exception e) {
        VoltDB.crashLocalVoltDB("Failed to serialize snapshot predicates", true, e);
    }
    ByteBuffer buf = ByteBuffer.allocate(// predicate count
    4 + // predicate byte lengths
    4 * predicates.length + // predicate bytes
    size);
    buf.putInt(m_predicates.size());
    for (byte[] predicate : predicates) {
        buf.putInt(predicate.length);
        buf.put(predicate);
    }
    return buf.array();
}
Also used : AbstractExpression(org.voltdb.expressions.AbstractExpression) JSONStringer(org.json_voltpatches.JSONStringer) ByteBuffer(java.nio.ByteBuffer)

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