Search in sources :

Example 61 with JSONObject

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

the class SystemInformation method populatePartitionGroups.

private static void populatePartitionGroups(Integer hostId, VoltTable vt) {
    try {
        byte[] bytes = VoltDB.instance().getHostMessenger().getZK().getData(CoreZK.hosts_host + hostId, false, new Stat());
        String hostInfo = new String(bytes, StandardCharsets.UTF_8);
        JSONObject obj = new JSONObject(hostInfo);
        vt.addRow(hostId, "PLACEMENTGROUP", obj.getString("group"));
    } catch (KeeperException | InterruptedException | JSONException e) {
        vt.addRow(hostId, "PLACEMENTGROUP", "NULL");
    }
    Set<Integer> buddies = VoltDB.instance().getCartograhper().getHostIdsWithinPartitionGroup(hostId);
    String[] strIds = buddies.stream().sorted().map(i -> String.valueOf(i)).toArray(String[]::new);
    vt.addRow(hostId, "PARTITIONGROUP", String.join(",", strIds));
}
Also used : CoreZK(org.voltcore.zk.CoreZK) ParameterSet(org.voltdb.ParameterSet) ColumnInfo(org.voltdb.VoltTable.ColumnInfo) VoltType(org.voltdb.VoltType) ClusterSettings(org.voltdb.settings.ClusterSettings) HashMap(java.util.HashMap) InetAddress(java.net.InetAddress) Logger(org.apache.log4j.Logger) Systemsettings(org.voltdb.catalog.Systemsettings) DtxnConstants(org.voltdb.dtxn.DtxnConstants) CatalogUtil(org.voltdb.utils.CatalogUtil) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) ProcInfo(org.voltdb.ProcInfo) Deployment(org.voltdb.catalog.Deployment) Map(java.util.Map) JSONObject(org.json_voltpatches.JSONObject) CoreUtils(org.voltcore.utils.CoreUtils) Constants(org.voltcore.common.Constants) GroupRef(org.voltdb.catalog.GroupRef) User(org.voltdb.catalog.User) VoltLogger(org.voltcore.logging.VoltLogger) SnapshotSchedule(org.voltdb.catalog.SnapshotSchedule) SocketHubAppender(org.apache.log4j.net.SocketHubAppender) VoltTableUtil(org.voltdb.utils.VoltTableUtil) Connector(org.voltdb.catalog.Connector) VoltSystemProcedure(org.voltdb.VoltSystemProcedure) Set(java.util.Set) Cluster(org.voltdb.catalog.Cluster) DependencyPair(org.voltdb.DependencyPair) MiscUtils(org.voltdb.utils.MiscUtils) UnknownHostException(java.net.UnknownHostException) VoltDB(org.voltdb.VoltDB) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) JSONException(org.json_voltpatches.JSONException) SystemProcedureExecutionContext(org.voltdb.SystemProcedureExecutionContext) List(java.util.List) Stat(org.apache.zookeeper_voltpatches.data.Stat) NodeSettings(org.voltdb.settings.NodeSettings) Entry(java.util.Map.Entry) Database(org.voltdb.catalog.Database) JSONArray(org.json_voltpatches.JSONArray) CommandLog(org.voltdb.catalog.CommandLog) VoltTable(org.voltdb.VoltTable) Stat(org.apache.zookeeper_voltpatches.data.Stat) JSONObject(org.json_voltpatches.JSONObject) JSONException(org.json_voltpatches.JSONException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Example 62 with JSONObject

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

the class TestJSONInterface method testUsers.

public void testUsers() throws Exception {
    try {
        String simpleSchema = "CREATE TABLE foo (\n" + "    bar BIGINT NOT NULL,\n" + "    PRIMARY KEY (bar)\n" + ");";
        File schemaFile = VoltProjectBuilder.writeStringToTempFile(simpleSchema);
        String schemaPath = schemaFile.getPath();
        schemaPath = URLEncoder.encode(schemaPath, "UTF-8");
        VoltProjectBuilder builder = new VoltProjectBuilder();
        builder.addSchema(schemaPath);
        builder.addPartitionInfo("foo", "bar");
        builder.addProcedures(DelayProc.class);
        builder.setHTTPDPort(8095);
        builder.setUseDDLSchema(true);
        boolean success = builder.compile(Configuration.getPathToCatalogForTest("json.jar"));
        assertTrue(success);
        VoltDB.Configuration config = new VoltDB.Configuration();
        config.m_pathToCatalog = config.setPathToCatalogForTest("json.jar");
        config.m_pathToDeployment = builder.getPathToDeployment();
        server = new ServerThread(config);
        server.start();
        server.waitForInitialization();
        //Get users
        String json = getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/", null, null, null, 200, "application/json");
        assertEquals(json, "[]");
        getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/foo", null, null, null, 404, "application/json");
        //Put users
        ObjectMapper mapper = new ObjectMapper();
        UsersType.User user = new UsersType.User();
        user.setName("foo");
        user.setPassword("foo");
        String map = mapper.writeValueAsString(user);
        Map<String, String> params = new HashMap<>();
        params.put("user", map);
        putUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/foo/", null, null, null, 201, "application/json", params);
        //Get users
        json = getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/", null, null, null, 200, "application/json");
        JSONArray jarray = new JSONArray(json);
        assertEquals(jarray.length(), 1);
        JSONObject jobj = jarray.getJSONObject(0);
        assertTrue(jobj.getString("id").contains("/deployment/users/foo"));
        assertTrue(jobj.getString("roles").equalsIgnoreCase("null"));
        //Post users
        user.setRoles("foo");
        map = mapper.writeValueAsString(user);
        params.put("user", map);
        postUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/foo/", null, null, null, 200, "application/json", params);
        //Get users
        json = getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/", null, null, null, 200, "application/json");
        jarray = new JSONArray(json);
        assertEquals(jarray.length(), 1);
        jobj = jarray.getJSONObject(0);
        assertTrue(jobj.getString("roles").equals("foo"));
        //Delete users
        deleteUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/foo/", null, null, null, 204, "application/json");
        //Get users
        json = getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/", null, null, null, 200, "application/json");
        assertEquals(json, "[]");
    } finally {
        if (server != null) {
            server.shutdown();
            server.join();
        }
        server = null;
    }
}
Also used : Configuration(org.voltdb.VoltDB.Configuration) HashMap(java.util.HashMap) JSONArray(org.json_voltpatches.JSONArray) JSONObject(org.json_voltpatches.JSONObject) VoltProjectBuilder(org.voltdb.compiler.VoltProjectBuilder) Configuration(org.voltdb.VoltDB.Configuration) UsersType(org.voltdb.compiler.deploymentfile.UsersType) File(java.io.File) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 63 with JSONObject

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

the class TestJdbcDatabaseMetaDataGenerator method testGetTables.

public void testGetTables() throws Exception {
    String schema = "create table Table1 (Column1 varchar(10) not null, Column2 integer);" + "partition table Table1 on column Column1;" + "create table Table2 (Column1 integer);" + "create view View1 (Column1, num) as select Column1, count(*) from Table1 group by Column1;" + "create view View2 (Column2, num) as select Column2, count(*) from Table1 group by Column2;" + "create stream Export1 (Column1 integer);" + "create stream Export2 export to target foo (Column1 integer);" + "create procedure sample as select * from Table1;";
    VoltCompiler c = compileForDDLTest2(schema);
    System.out.println(c.getCatalog().serialize());
    JdbcDatabaseMetaDataGenerator dut = new JdbcDatabaseMetaDataGenerator(c.getCatalog(), null, new InMemoryJarfile(testout_jar));
    VoltTable tables = dut.getMetaData("tables");
    System.out.println(tables);
    assertEquals(10, tables.getColumnCount());
    assertEquals(6, tables.getRowCount());
    assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "Table1"));
    assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("TABLE"));
    assertTrue(tables.get("REMARKS", VoltType.STRING).equals("{\"partitionColumn\":\"COLUMN1\"}"));
    assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "Table2"));
    assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("TABLE"));
    assertEquals(null, tables.get("REMARKS", VoltType.STRING));
    assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "View1"));
    assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("VIEW"));
    assertTrue(tables.get("REMARKS", VoltType.STRING).equals(new JSONObject("{\"partitionColumn\":\"COLUMN1\",\"sourceTable\":\"TABLE1\"}").toString()));
    assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "View2"));
    assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("VIEW"));
    assertTrue(tables.get("REMARKS", VoltType.STRING).equals(new JSONObject("{\"partitionColumn\":\"COLUMN1\",\"sourceTable\":\"TABLE1\"}").toString()));
    assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "Export1"));
    assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("EXPORT"));
    assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "Export2"));
    assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("EXPORT"));
    assertFalse(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "NotATable"));
}
Also used : VoltCompiler(org.voltdb.compiler.VoltCompiler) JSONObject(org.json_voltpatches.JSONObject) InMemoryJarfile(org.voltdb.utils.InMemoryJarfile)

Example 64 with JSONObject

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

the class PlannerTestAideDeCamp method compile.

/**
     * Compile and cache the statement and plan and return the final plan graph.
     */
private List<AbstractPlanNode> compile(String sql, int paramCount, String joinOrder, boolean inferPartitioning, boolean forceSingle, DeterminismMode detMode) {
    String stmtLabel = "stmt-" + String.valueOf(compileCounter++);
    Statement catalogStmt = proc.getStatements().add(stmtLabel);
    catalogStmt.setSqltext(sql);
    catalogStmt.setSinglepartition(forceSingle);
    // determine the type of the query
    QueryType qtype = QueryType.SELECT;
    catalogStmt.setReadonly(true);
    if (sql.toLowerCase().startsWith("insert")) {
        qtype = QueryType.INSERT;
        catalogStmt.setReadonly(false);
    }
    if (sql.toLowerCase().startsWith("update")) {
        qtype = QueryType.UPDATE;
        catalogStmt.setReadonly(false);
    }
    if (sql.toLowerCase().startsWith("delete")) {
        qtype = QueryType.DELETE;
        catalogStmt.setReadonly(false);
    }
    catalogStmt.setQuerytype(qtype.getValue());
    // name will look like "basename-stmt-#"
    String name = catalogStmt.getParent().getTypeName() + "-" + catalogStmt.getTypeName();
    DatabaseEstimates estimates = new DatabaseEstimates();
    TrivialCostModel costModel = new TrivialCostModel();
    StatementPartitioning partitioning;
    if (inferPartitioning) {
        partitioning = StatementPartitioning.inferPartitioning();
    } else if (forceSingle) {
        partitioning = StatementPartitioning.forceSP();
    } else {
        partitioning = StatementPartitioning.forceMP();
    }
    String procName = catalogStmt.getParent().getTypeName();
    QueryPlanner planner = new QueryPlanner(sql, stmtLabel, procName, db, partitioning, hsql, estimates, false, StatementCompiler.DEFAULT_MAX_JOIN_TABLES, costModel, null, joinOrder, detMode);
    CompiledPlan plan = null;
    planner.parse();
    plan = planner.plan();
    assert (plan != null);
    // Partitioning optionally inferred from the planning process.
    if (partitioning.isInferred()) {
        catalogStmt.setSinglepartition(partitioning.isInferredSingle());
    }
    // We will need to update the system catalogs with this new information
    for (int i = 0; i < plan.parameters.length; ++i) {
        StmtParameter catalogParam = catalogStmt.getParameters().add(String.valueOf(i));
        ParameterValueExpression pve = plan.parameters[i];
        catalogParam.setJavatype(pve.getValueType().getValue());
        catalogParam.setIsarray(pve.getParamIsVector());
        catalogParam.setIndex(i);
    }
    List<PlanNodeList> nodeLists = new ArrayList<>();
    nodeLists.add(new PlanNodeList(plan.rootPlanGraph));
    if (plan.subPlanGraph != null) {
        nodeLists.add(new PlanNodeList(plan.subPlanGraph));
    }
    // Now update our catalog information
    // HACK: We're using the node_tree's hashCode() as it's name. It would be really
    //     nice if the Catalog code give us an guid without needing a name first...
    String json = null;
    try {
        JSONObject jobj = new JSONObject(nodeLists.get(0).toJSONString());
        json = jobj.toString(4);
    } catch (JSONException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
        System.exit(-1);
        return null;
    }
    //
    try {
        BuildDirectoryUtils.writeFile("statement-plans", name + "_json.txt", json, true);
        BuildDirectoryUtils.writeFile("statement-plans", name + ".dot", nodeLists.get(0).toDOTString("name"), true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    List<AbstractPlanNode> plannodes = new ArrayList<>();
    for (PlanNodeList nodeList : nodeLists) {
        plannodes.add(nodeList.getRootPlanNode());
    }
    m_currentPlan = plan;
    return plannodes;
}
Also used : AbstractPlanNode(org.voltdb.plannodes.AbstractPlanNode) Statement(org.voltdb.catalog.Statement) ArrayList(java.util.ArrayList) JSONException(org.json_voltpatches.JSONException) PlanNodeList(org.voltdb.plannodes.PlanNodeList) JSONException(org.json_voltpatches.JSONException) StmtParameter(org.voltdb.catalog.StmtParameter) JSONObject(org.json_voltpatches.JSONObject) ParameterValueExpression(org.voltdb.expressions.ParameterValueExpression) QueryType(org.voltdb.types.QueryType) DatabaseEstimates(org.voltdb.compiler.DatabaseEstimates)

Example 65 with JSONObject

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

the class PlanNodeTree method loadPlanNodesFromJSONArrays.

/**
     *  Load plan nodes from the "PLAN_NODE" array. All the nodes are from
     *  a substatement with the id = stmtId
     * @param stmtId
     * @param jArray - PLAN_NODES
     * @param db
     * @throws JSONException
     */
private void loadPlanNodesFromJSONArrays(int stmtId, JSONArray jArray, Database db) {
    List<AbstractPlanNode> planNodes = new ArrayList<AbstractPlanNode>();
    int size = jArray.length();
    try {
        for (int i = 0; i < size; i++) {
            JSONObject jobj = jArray.getJSONObject(i);
            String nodeTypeStr = jobj.getString("PLAN_NODE_TYPE");
            PlanNodeType nodeType = PlanNodeType.get(nodeTypeStr);
            AbstractPlanNode apn = nodeType.getPlanNodeClass().newInstance();
            apn.loadFromJSONObject(jobj, db);
            planNodes.add(apn);
        }
        //link children and parents
        for (int i = 0; i < size; i++) {
            JSONObject jobj = jArray.getJSONObject(i);
            if (jobj.has("CHILDREN_IDS")) {
                AbstractPlanNode parent = planNodes.get(i);
                JSONArray children = jobj.getJSONArray("CHILDREN_IDS");
                for (int j = 0; j < children.length(); j++) {
                    AbstractPlanNode child = getNodeofId(children.getInt(j), planNodes);
                    parent.addAndLinkChild(child);
                }
            }
        }
        m_planNodesListMap.put(stmtId, planNodes);
    } catch (JSONException | InstantiationException | IllegalAccessException e) {
        System.err.println(e);
        e.printStackTrace();
    }
}
Also used : PlanNodeType(org.voltdb.types.PlanNodeType) JSONObject(org.json_voltpatches.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json_voltpatches.JSONArray) JSONException(org.json_voltpatches.JSONException) JSONString(org.json_voltpatches.JSONString)

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