Search in sources :

Example 71 with JSONObject

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

the class TestCartographer method testMPIChange.

@Test
public void testMPIChange() throws Exception {
    ZooKeeper zk = getClient(0);
    VoltZK.createPersistentZKNodes(zk);
    LeaderCache mpwriter = new LeaderCache(zk, VoltZK.iv2mpi);
    HostMessenger hm = mock(HostMessenger.class);
    when(hm.getZK()).thenReturn(m_messengers.get(0).getZK());
    Cartographer dut = new Cartographer(hm, 0, false);
    mpwriter.start(true);
    // initial master
    mpwriter.put(MpInitiator.MP_INIT_PID, 0l);
    verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
    reset(hm);
    // Now change the master
    mpwriter.put(MpInitiator.MP_INIT_PID, 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"));
    final int partitionId = jsObj.getInt(Cartographer.JSON_PARTITION_ID);
    final long initiatorHSId = jsObj.getLong(Cartographer.JSON_INITIATOR_HSID);
    assertEquals(MpInitiator.MP_INIT_PID, partitionId);
    assertEquals(3, initiatorHSId);
    mpwriter.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)

Example 72 with JSONObject

use of org.json_voltpatches.JSONObject 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 73 with JSONObject

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

the class InstanceId method serializeToJSONObject.

public JSONObject serializeToJSONObject() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.object();
    stringer.keySymbolValuePair("coord", m_coord);
    stringer.keySymbolValuePair("timestamp", m_timestamp);
    stringer.endObject();
    return new JSONObject(stringer.toString());
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONStringer(org.json_voltpatches.JSONStringer)

Example 74 with JSONObject

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

the class AbstractTopology method topologyToJSON.

/////////////////////////////////////
//
// SERIALIZATION API
//
/////////////////////////////////////
public JSONObject topologyToJSON() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.object();
    stringer.keySymbolValuePair(TOPO_VERSION, version);
    stringer.key(TOPO_HAGROUPS).array();
    List<HAGroup> haGroups = hostsById.values().stream().map(h -> h.haGroup).distinct().collect(Collectors.toList());
    for (HAGroup haGroup : haGroups) {
        haGroup.toJSON(stringer);
    }
    stringer.endArray();
    stringer.key(TOPO_PARTITIONS).array();
    for (Partition partition : partitionsById.values()) {
        partition.toJSON(stringer);
    }
    stringer.endArray();
    stringer.key(TOPO_HOSTS).array();
    for (Host host : hostsById.values()) {
        host.toJSON(stringer);
    }
    stringer.endArray();
    stringer.endObject();
    return new JSONObject(stringer.toString());
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONStringer(org.json_voltpatches.JSONStringer)

Example 75 with JSONObject

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

the class MapCache method processChildEvent.

/**
     * Update a modified child and republish a new snapshot. This may indicate
     * a deleted child or a child with modified data.
     */
private void processChildEvent(WatchedEvent event) throws Exception {
    HashMap<String, JSONObject> cacheCopy = new HashMap<String, JSONObject>(m_publicCache.get());
    ByteArrayCallback cb = new ByteArrayCallback();
    m_zk.getData(event.getPath(), m_childWatch, cb, null);
    try {
        byte[] payload = cb.getData();
        JSONObject jsObj = new JSONObject(new String(payload, "UTF-8"));
        cacheCopy.put(cb.getPath(), jsObj);
    } catch (KeeperException.NoNodeException e) {
        cacheCopy.remove(event.getPath());
    }
    m_publicCache.set(ImmutableMap.copyOf(cacheCopy));
    if (m_cb != null) {
        m_cb.run(m_publicCache.get());
    }
}
Also used : ByteArrayCallback(org.voltcore.zk.ZKUtil.ByteArrayCallback) JSONObject(org.json_voltpatches.JSONObject) HashMap(java.util.HashMap) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

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