use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class SocketJoiner method requestForConnection.
public SocketChannel requestForConnection(InetSocketAddress hostAddr) throws IOException, JSONException {
SocketChannel socket = connectToHost(hostAddr);
/*
* Get the clock skew value
*/
ByteBuffer currentTimeBuf = ByteBuffer.allocate(8);
while (currentTimeBuf.hasRemaining()) {
socket.read(currentTimeBuf);
}
assert currentTimeBuf.position() == 8 : "time buffer is at an unexpected position";
JSONObject jsObj = new JSONObject();
jsObj.put(TYPE, ConnectionType.REQUEST_CONNECTION.name());
jsObj.put(VERSION_STRING, m_acceptor.getVersionChecker().getVersionString());
jsObj.put(HOST_ID, m_localHostId);
jsObj.put(PORT, m_internalPort);
jsObj.put(ADDRESS, m_internalInterface.isEmpty() ? m_reportedInternalInterface : m_internalInterface);
byte[] jsBytes = jsObj.toString(4).getBytes(StandardCharsets.UTF_8);
ByteBuffer addConnection = ByteBuffer.allocate(4 + jsBytes.length);
addConnection.putInt(jsBytes.length);
addConnection.put(jsBytes).flip();
while (addConnection.hasRemaining()) {
socket.write(addConnection);
}
// read the json response from socketjoiner with version info and validate it
final String remoteAddress = socket.socket().getRemoteSocketAddress().toString();
processJSONResponse(socket, remoteAddress, null, false);
return socket;
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class InvocationDispatcher method useSnapshotCatalogToRestoreSnapshotSchema.
private final ClientResponseImpl useSnapshotCatalogToRestoreSnapshotSchema(final StoredProcedureInvocation task, final InvocationClientHandler handler, final Connection ccxn, final AuthUser user, OverrideCheck bypass) {
CatalogContext catalogContext = m_catalogContext.get();
if (!catalogContext.cluster.getUseddlschema()) {
return gracefulFailureResponse("Cannot restore catalog from snapshot when schema is set to catalog in the deployment.", task.clientHandle);
}
log.info("No schema found. Restoring schema and procedures from snapshot.");
try {
JSONObject jsObj = new JSONObject(task.getParams().getParam(0).toString());
final File catalogFH = getSnapshotCatalogFile(jsObj);
final byte[] catalog;
try {
catalog = MiscUtils.fileToBytes(catalogFH);
} catch (IOException e) {
log.warn("Unable to access catalog file " + catalogFH, e);
return unexpectedFailureResponse("Unable to access catalog file " + catalogFH, task.clientHandle);
}
final String dep = new String(catalogContext.getDeploymentBytes(), StandardCharsets.UTF_8);
final StoredProcedureInvocation catalogUpdateTask = new StoredProcedureInvocation();
catalogUpdateTask.setProcName("@UpdateApplicationCatalog");
catalogUpdateTask.setParams(catalog, dep);
//A connection with positive id will be thrown into live client statistics. The connection does not support stats.
//Thus make the connection id as a negative constant to skip the stats collection.
final SimpleClientResponseAdapter alternateAdapter = new SimpleClientResponseAdapter(ClientInterface.RESTORE_SCHEMAS_CID, "Empty database snapshot restore catalog update");
final InvocationClientHandler alternateHandler = new InvocationClientHandler() {
@Override
public boolean isAdmin() {
return handler.isAdmin();
}
@Override
public long connectionId() {
return ClientInterface.RESTORE_SCHEMAS_CID;
}
};
final long sourceHandle = task.clientHandle;
SimpleClientResponseAdapter.SyncCallback restoreCallback = new SimpleClientResponseAdapter.SyncCallback();
final ListenableFuture<ClientResponse> onRestoreComplete = restoreCallback.getResponseFuture();
onRestoreComplete.addListener(new Runnable() {
@Override
public void run() {
ClientResponse r;
try {
r = onRestoreComplete.get();
} catch (ExecutionException | InterruptedException e) {
VoltDB.crashLocalVoltDB("Should never happen", true, e);
return;
}
transmitResponseMessage(r, ccxn, sourceHandle);
}
}, CoreUtils.SAMETHREADEXECUTOR);
task.setClientHandle(alternateAdapter.registerCallback(restoreCallback));
SimpleClientResponseAdapter.SyncCallback catalogUpdateCallback = new SimpleClientResponseAdapter.SyncCallback();
final ListenableFuture<ClientResponse> onCatalogUpdateComplete = catalogUpdateCallback.getResponseFuture();
onCatalogUpdateComplete.addListener(new Runnable() {
@Override
public void run() {
ClientResponse r;
try {
r = onCatalogUpdateComplete.get();
} catch (ExecutionException | InterruptedException e) {
VoltDB.crashLocalVoltDB("Should never happen", true, e);
return;
}
if (r.getStatus() != ClientResponse.SUCCESS) {
transmitResponseMessage(r, ccxn, sourceHandle);
log.error("Received error response for updating catalog " + r.getStatusString());
return;
}
m_catalogContext.set(VoltDB.instance().getCatalogContext());
dispatch(task, alternateHandler, alternateAdapter, user, bypass, false);
}
}, CoreUtils.SAMETHREADEXECUTOR);
catalogUpdateTask.setClientHandle(alternateAdapter.registerCallback(catalogUpdateCallback));
VoltDB.instance().getClientInterface().bindAdapter(alternateAdapter, null);
// dispatch the catalog update
dispatchNTProcedure(alternateHandler, catalogUpdateTask, user, alternateAdapter, System.nanoTime(), false);
} catch (JSONException e) {
return unexpectedFailureResponse("Unable to parse parameters.", task.clientHandle);
}
return null;
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class ExtensibleSnapshotDigestData method mergeConsumerDrIdTrackerToZK.
/*
* When recording snapshot completion we also record DR remote DC unique ids
* as JSON. Need to merge our unique ids with existing numbers
* since multiple replicas will submit the unique ids
*/
private void mergeConsumerDrIdTrackerToZK(JSONObject jsonObj) throws JSONException {
//DR ids/unique ids for remote partitions indexed by remote datacenter id,
//each DC has a full partition set
JSONObject dcIdMap;
if (jsonObj.has("drMixedClusterSizeConsumerState")) {
dcIdMap = jsonObj.getJSONObject("drMixedClusterSizeConsumerState");
} else {
dcIdMap = new JSONObject();
jsonObj.put("drMixedClusterSizeConsumerState", dcIdMap);
}
for (Map.Entry<Integer, JSONObject> dcEntry : m_drMixedClusterSizeConsumerState.entrySet()) {
//Last seen ids for a specific data center
final String consumerPartitionString = dcEntry.getKey().toString();
if (!dcIdMap.has(consumerPartitionString)) {
dcIdMap.put(consumerPartitionString, dcEntry.getValue());
}
}
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class ExtensibleSnapshotDigestData method serializeSiteConsumerDrIdTrackersToJSON.
public static JSONObject serializeSiteConsumerDrIdTrackersToJSON(Map<Integer, Map<Integer, DRConsumerDrIdTracker>> drMixedClusterSizeConsumerState) throws JSONException {
JSONObject clusters = new JSONObject();
if (drMixedClusterSizeConsumerState == null) {
return clusters;
}
for (Map.Entry<Integer, Map<Integer, DRConsumerDrIdTracker>> e : drMixedClusterSizeConsumerState.entrySet()) {
// The key is the remote Data Center's partitionId. HeteroTopology implies a different partition count
// from the local cluster's partition count (which is not tracked here)
JSONObject partitions = new JSONObject();
for (Map.Entry<Integer, DRConsumerDrIdTracker> e2 : e.getValue().entrySet()) {
partitions.put(e2.getKey().toString(), e2.getValue().toJSON());
}
clusters.put(e.getKey().toString(), partitions);
}
return clusters;
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class TraceAgent method parseParamsForSystemInformation.
// Parse the provided parameter set object and fill in subselector and interval into
// the provided JSONObject. If there's an error, return that in the String, otherwise
// return null. Yes, ugly. Bang it out, then refactor later.
private String parseParamsForSystemInformation(ParameterSet params, JSONObject obj) throws Exception {
// Default with no args is OVERVIEW
String subselector = "status";
if (params.toArray().length < 1) {
return "Incorrect number of arguments to @Trace (expects as least 1, received " + params.toArray().length + ")";
}
if (params.toArray().length >= 1) {
Object first = params.toArray()[0];
if (!(first instanceof String)) {
return "First argument to @Trace must be a valid STRING selector, instead was " + first;
}
subselector = (String) first;
if (!(subselector.equalsIgnoreCase("enable") || subselector.equalsIgnoreCase("disable") || subselector.equalsIgnoreCase("status") || subselector.equalsIgnoreCase("dump"))) {
return "Invalid @Trace selector " + subselector;
}
}
// Would be nice to have subselector validation here, maybe. Maybe later.
obj.put("subselector", subselector);
if (params.toArray().length >= 2) {
obj.put("categories", params.toArray()[1]);
}
obj.put("interval", false);
return null;
}
Aggregations