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();
}
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);
}
}
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());
}
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());
}
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());
}
}
Aggregations