use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class SnapshotUtil method transformRestoreParamsToJSON.
/*
* Do parameter checking for the pre-JSON version of @SnapshotRestore old version
*/
public static ClientResponseImpl transformRestoreParamsToJSON(StoredProcedureInvocation task) {
Object[] params = task.getParams().toArray();
if (params.length == 1) {
try {
JSONObject jsObj = new JSONObject((String) params[0]);
String path = jsObj.optString(JSON_PATH);
String dupPath = jsObj.optString(JSON_DUPLICATES_PATH);
if (!path.isEmpty() && dupPath.isEmpty()) {
jsObj.put(JSON_DUPLICATES_PATH, path);
}
task.setParams(jsObj.toString());
} catch (JSONException e) {
Throwables.propagate(e);
}
return null;
} else if (params.length == 2) {
if (params[0] == null) {
return new ClientResponseImpl(ClientResponseImpl.GRACEFUL_FAILURE, new VoltTable[0], "@SnapshotRestore parameter 0 was null", task.getClientHandle());
}
if (params[1] == null) {
return new ClientResponseImpl(ClientResponseImpl.GRACEFUL_FAILURE, new VoltTable[0], "@SnapshotRestore parameter 1 was null", task.getClientHandle());
}
if (!(params[0] instanceof String)) {
return new ClientResponseImpl(ClientResponseImpl.GRACEFUL_FAILURE, new VoltTable[0], "@SnapshotRestore param 0 (path) needs to be a string, but was type " + params[0].getClass().getSimpleName(), task.getClientHandle());
}
if (!(params[1] instanceof String)) {
return new ClientResponseImpl(ClientResponseImpl.GRACEFUL_FAILURE, new VoltTable[0], "@SnapshotRestore param 1 (nonce) needs to be a string, but was type " + params[1].getClass().getSimpleName(), task.getClientHandle());
}
JSONObject jsObj = new JSONObject();
try {
jsObj.put(SnapshotUtil.JSON_PATH, params[0]);
if (VoltDB.instance().isRunningWithOldVerbs()) {
jsObj.put(SnapshotUtil.JSON_PATH_TYPE, SnapshotPathType.SNAP_PATH);
}
jsObj.put(SnapshotUtil.JSON_NONCE, params[1]);
jsObj.put(SnapshotUtil.JSON_DUPLICATES_PATH, params[0]);
} catch (JSONException e) {
Throwables.propagate(e);
}
task.setParams(jsObj.toString());
return null;
} else {
return new ClientResponseImpl(ClientResponseImpl.GRACEFUL_FAILURE, new VoltTable[0], "@SnapshotRestore supports a single json document parameter or two parameters (path, nonce), " + params.length + " parameters provided", task.getClientHandle());
}
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class SnapshotUtil method retrieveSnapshotFilesInternal.
private static void retrieveSnapshotFilesInternal(File directory, NamedSnapshots namedSnapshots, FileFilter filter, boolean validate, SnapshotPathType stype, VoltLogger logger, int recursion) {
if (recursion == 32) {
return;
}
if (!directory.exists()) {
System.err.println("Error: Directory " + directory.getPath() + " doesn't exist");
return;
}
if (!directory.canRead()) {
System.err.println("Error: Directory " + directory.getPath() + " is not readable");
return;
}
if (!directory.canExecute()) {
System.err.println("Error: Directory " + directory.getPath() + " is not executable");
return;
}
for (File f : directory.listFiles(filter)) {
if (f.isDirectory()) {
if (!f.canRead() || !f.canExecute()) {
System.err.println("Warning: Skipping directory " + f.getPath() + " due to lack of read permission");
} else {
retrieveSnapshotFilesInternal(f, namedSnapshots, filter, validate, stype, logger, recursion++);
}
continue;
}
if (!f.canRead()) {
System.err.println("Warning: " + f.getPath() + " is not readable");
continue;
}
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
} catch (FileNotFoundException e1) {
System.err.println(e1.getMessage());
continue;
}
try {
if (f.getName().endsWith(".digest")) {
JSONObject digest = CRCCheck(f, logger);
if (digest == null)
continue;
Long snapshotTxnId = digest.getLong("txnId");
String nonce = parseNonceFromSnapshotFilename(f.getName());
Snapshot named_s = namedSnapshots.get(nonce);
named_s.setTxnId(snapshotTxnId);
InstanceId iid = new InstanceId(0, 0);
if (digest.has("instanceId")) {
iid = new InstanceId(digest.getJSONObject("instanceId"));
}
named_s.setInstanceId(iid);
TreeSet<String> tableSet = new TreeSet<String>();
JSONArray tables = digest.getJSONArray("tables");
for (int ii = 0; ii < tables.length(); ii++) {
tableSet.add(tables.getString(ii));
}
named_s.m_digestTables.add(tableSet);
named_s.m_digests.add(f);
} else if (f.getName().endsWith(".jar")) {
String nonce = parseNonceFromSnapshotFilename(f.getName());
Snapshot named_s = namedSnapshots.get(nonce);
named_s.m_catalogFile = f;
} else if (f.getName().endsWith(HASH_EXTENSION)) {
String nonce = parseNonceFromSnapshotFilename(f.getName());
Snapshot named_s = namedSnapshots.get(nonce);
if (validate) {
try {
// Retrieve hashinator config data for validation only.
// Throws IOException when the CRC check fails.
HashinatorSnapshotData hashData = new HashinatorSnapshotData();
hashData.restoreFromFile(f);
named_s.m_hashConfig = f;
} catch (IOException e) {
logger.warn(String.format("Skipping bad hashinator snapshot file '%s'", f.getPath()));
// Skip bad hashinator files.
continue;
}
}
} else {
HashSet<Integer> partitionIds = new HashSet<Integer>();
TableSaveFile saveFile = new TableSaveFile(fis, 1, null, true);
try {
for (Integer partitionId : saveFile.getPartitionIds()) {
partitionIds.add(partitionId);
}
if (validate && saveFile.getCompleted()) {
while (saveFile.hasMoreChunks()) {
BBContainer cont = saveFile.getNextChunk();
if (cont != null) {
cont.discard();
}
}
}
partitionIds.removeAll(saveFile.getCorruptedPartitionIds());
String nonce = parseNonceFromSnapshotFilename(f.getName());
Snapshot named_s = namedSnapshots.get(nonce);
named_s.setTxnId(saveFile.getTxnId());
TableFiles namedTableFiles = named_s.m_tableFiles.get(saveFile.getTableName());
if (namedTableFiles == null) {
namedTableFiles = new TableFiles(saveFile.isReplicated());
named_s.m_tableFiles.put(saveFile.getTableName(), namedTableFiles);
}
namedTableFiles.m_files.add(f);
namedTableFiles.m_completed.add(saveFile.getCompleted());
namedTableFiles.m_validPartitionIds.add(partitionIds);
namedTableFiles.m_corruptParititionIds.add(saveFile.getCorruptedPartitionIds());
namedTableFiles.m_totalPartitionCounts.add(saveFile.getTotalPartitions());
} finally {
saveFile.close();
}
}
} catch (IOException e) {
System.err.println(e.getMessage());
System.err.println("Error: Unable to process " + f.getPath());
} catch (JSONException e) {
System.err.println(e.getMessage());
System.err.println("Error: Unable to process " + f.getPath());
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
}
}
}
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class StreamSnapshotRequestConfig method parseStreamPairs.
private static Multimap<Long, Long> parseStreamPairs(JSONObject jsData) {
ArrayListMultimap<Long, Long> streamPairs = ArrayListMultimap.create();
if (jsData != null) {
try {
JSONObject sp = jsData.getJSONObject("streamPairs");
Iterator<String> it = sp.keys();
while (it.hasNext()) {
String key = it.next();
long sourceHSId = Long.valueOf(key);
JSONArray destJSONArray = sp.getJSONArray(key);
for (int i = 0; i < destJSONArray.length(); i++) {
long destHSId = destJSONArray.getLong(i);
streamPairs.put(sourceHSId, destHSId);
}
}
} catch (JSONException e) {
SNAP_LOG.warn("Failed to parse stream pair information", e);
}
}
return streamPairs;
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class TestHostMessenger method testProbedJoinerAcceptorMismatchCrash.
@Test
public void testProbedJoinerAcceptorMismatchCrash() throws Exception {
VoltDB.ignoreCrash = true;
MeshProber.Builder jcb = MeshProber.builder().coordinators(coordinators(2)).hostCount(3).startAction(StartAction.PROBE).nodeState(NodeState.INITIALIZING).kfactor(1).paused(false).bare(true);
MeshProber jc1 = jcb.build();
JoinAcceptor jc2 = new JoinAcceptor() {
@Override
public void detract(Set<Integer> hostIds) {
}
@Override
public void detract(int hostId) {
}
@Override
public void accrue(Map<Integer, JSONObject> jos) {
}
@Override
public void accrue(int hostId, JSONObject jo) {
}
};
HostMessenger hm1 = createHostMessenger(0, jcb.prober(jc1).build(), false);
HostMessenger hm2 = createHostMessenger(1, jcb.prober(jc1).build(), false);
HostMessenger hm3 = createHostMessenger(2, jc2, false);
final AtomicReference<Exception> exception = new AtomicReference<Exception>();
HostMessengerThread hm1Start = new HostMessengerThread(hm1, exception);
HostMessengerThread hm2Start = new HostMessengerThread(hm2, exception);
hm1Start.start();
hm2Start.start();
hm1Start.join();
hm2Start.join();
if (exception.get() != null) {
fail(exception.get().toString());
}
try {
hm3.start();
fail("did not crash on whole cluster rejoin attempt");
} catch (AssertionError pass) {
assertTrue(VoltDB.wasCrashCalled);
assertTrue(VoltDB.crashMessage.contains("is incompatible with this node verion"));
}
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class TestDRConsumerDrIdTracker method testJsonSerialization.
@Test
public void testJsonSerialization() throws Exception {
tracker.append(5L, 5L, 0L, 0L);
tracker.append(15L, 20L, 0L, 0L);
DRConsumerDrIdTracker tracker2 = DRConsumerDrIdTracker.createPartitionTracker(17L, 0L, 0L, 0);
tracker2.append(20L, 25L, 0L, 0L);
tracker2.append(28L, 28L, 0L, 0L);
Map<Integer, DRConsumerDrIdTracker> perProducerPartitionTrackers = new HashMap<Integer, DRConsumerDrIdTracker>();
perProducerPartitionTrackers.put(0, tracker);
perProducerPartitionTrackers.put(1, tracker2);
Map<Integer, Map<Integer, DRConsumerDrIdTracker>> perSiteTrackers = new HashMap<Integer, Map<Integer, DRConsumerDrIdTracker>>();
// Insert trackers from cluster 20
perSiteTrackers.put(20, perProducerPartitionTrackers);
JSONObject trackersInJSON = ExtensibleSnapshotDigestData.serializeSiteConsumerDrIdTrackersToJSON(perSiteTrackers);
JSONStringer stringer = new JSONStringer();
stringer.object();
// ConsumerPartitionId
stringer.key("5");
stringer.value(trackersInJSON);
stringer.endObject();
String output = stringer.toString();
JSONObject allsiteInfo = new JSONObject(output);
JSONObject siteInfo = allsiteInfo.getJSONObject("5");
final Map<Integer, Map<Integer, DRConsumerDrIdTracker>> siteTrackers = ExtensibleSnapshotDigestData.buildConsumerSiteDrIdTrackersFromJSON(siteInfo);
DRConsumerDrIdTracker tracker3 = siteTrackers.get(20).get(0);
DRConsumerDrIdTracker tracker4 = siteTrackers.get(20).get(1);
assertTrue(tracker.getSafePointDrId() == tracker3.getSafePointDrId());
assertTrue(tracker.getLastSpUniqueId() == tracker3.getLastSpUniqueId());
assertTrue(tracker.getLastMpUniqueId() == tracker3.getLastMpUniqueId());
assertTrue(tracker.getDrIdRanges().equals(tracker3.getDrIdRanges()));
assertTrue(tracker2.getSafePointDrId() == tracker4.getSafePointDrId());
assertTrue(tracker2.getLastSpUniqueId() == tracker4.getLastSpUniqueId());
assertTrue(tracker2.getLastMpUniqueId() == tracker4.getLastMpUniqueId());
assertTrue(tracker2.getDrIdRanges().equals(tracker4.getDrIdRanges()));
}
Aggregations