Search in sources :

Example 21 with ClientConfig

use of org.voltdb.client.ClientConfig in project voltdb by VoltDB.

the class TestLiveTableSchemaMigration method migrateSchema.

/**
     * Assuming given tables have schema metadata, fill them with random data
     * and compare a pure-java schema migration with an EE schema migration.
     */
void migrateSchema(VoltTable t1, VoltTable t2, boolean withData) throws Exception {
    ServerThread server = null;
    Client client = null;
    TableHelper helper = new TableHelper();
    try {
        if (withData) {
            helper.randomFill(t1, 1000, 1024);
        }
        String catPath1 = catalogPathForTable(t1, "t1.jar");
        String catPath2 = catalogPathForTable(t2, "t2.jar");
        byte[] catBytes2 = MiscUtils.fileToBytes(new File(catPath2));
        DeploymentBuilder depBuilder = new DeploymentBuilder(1, 1, 0);
        depBuilder.setVoltRoot("/tmp/rootbar");
        // disable logging
        depBuilder.configureLogging("/tmp/foobar", "/tmp/goobar", false, false, 1, 1, 3);
        String deployment = depBuilder.getXML();
        File deploymentFile = VoltProjectBuilder.writeStringToTempFile(deployment);
        VoltDB.Configuration config = new VoltDB.Configuration();
        config.m_pathToDeployment = deploymentFile.getAbsolutePath();
        config.m_pathToCatalog = catPath1;
        config.m_ipcPort = 10000;
        //config.m_backend = BackendTarget.NATIVE_EE_IPC;
        server = new ServerThread(config);
        server.start();
        server.waitForInitialization();
        System.out.printf("PRE:  %s\n", TableHelper.ddlForTable(t1, false));
        System.out.printf("POST: %s\n", TableHelper.ddlForTable(t2, false));
        ClientConfig clientConfig = new ClientConfig();
        client = ClientFactory.createClient(clientConfig);
        client.createConnection("localhost");
        TableHelper.loadTable(client, t1);
        ClientResponseImpl response = (ClientResponseImpl) client.callProcedure("@UpdateApplicationCatalog", catBytes2, null);
        System.out.println(response.toJSONString());
        VoltTable t3 = client.callProcedure("@AdHoc", "select * from FOO").getResults()[0];
        t3 = TableHelper.sortTable(t3);
        // compute the migrated table entirely in Java for comparison purposes
        TableHelper.migrateTable(t1, t2);
        t2 = TableHelper.sortTable(t2);
        // compare the tables
        StringBuilder sb = new StringBuilder();
        if (!TableHelper.deepEqualsWithErrorMsg(t2, t3, sb)) {
            System.out.println("Table Mismatch");
            //System.out.printf("PRE:  %s\n", t2.toFormattedString());
            //System.out.printf("POST: %s\n", t3.toFormattedString());
            System.out.println(sb.toString());
            fail();
        }
    } finally {
        if (client != null) {
            client.close();
        }
        if (server != null) {
            server.shutdown();
        }
    }
}
Also used : Configuration(org.voltdb.VoltDB.Configuration) VoltTable(org.voltdb.VoltTable) VoltDB(org.voltdb.VoltDB) ServerThread(org.voltdb.ServerThread) TableHelper(org.voltdb.TableHelper) Configuration(org.voltdb.VoltDB.Configuration) ClientResponseImpl(org.voltdb.ClientResponseImpl) Client(org.voltdb.client.Client) ClientConfig(org.voltdb.client.ClientConfig) File(java.io.File) DeploymentBuilder(org.voltdb.compiler.DeploymentBuilder)

Example 22 with ClientConfig

use of org.voltdb.client.ClientConfig in project voltdb by VoltDB.

the class SQLCommand method mainWithReturnCode.

/**
     * Wraps the main routine. Is callable from other code without fear of it
     * calling System.exit(..).
     */
public static int mainWithReturnCode(String[] args) {
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+0"));
    // Initialize parameter defaults
    String serverList = "localhost";
    int port = 21212;
    String user = "";
    String password = "";
    String kerberos = "";
    List<String> queries = null;
    String ddlFileText = "";
    String sslConfigFile = null;
    boolean enableSSL = false;
    // Parse out parameters
    for (int i = 0; i < args.length; i++) {
        String arg = args[i];
        if (arg.startsWith("--servers=")) {
            serverList = extractArgInput(arg);
            if (serverList == null)
                return -1;
        } else if (arg.startsWith("--port=")) {
            String portStr = extractArgInput(arg);
            if (portStr == null)
                return -1;
            port = Integer.valueOf(portStr);
        } else if (arg.startsWith("--user=")) {
            user = extractArgInput(arg);
            if (user == null)
                return -1;
        } else if (arg.startsWith("--password=")) {
            password = extractArgInput(arg);
            if (password == null)
                return -1;
        } else if (arg.startsWith("--kerberos=")) {
            kerberos = extractArgInput(arg);
            if (kerberos == null)
                return -1;
        } else if (arg.startsWith("--kerberos")) {
            kerberos = "VoltDBClient";
        } else if (arg.startsWith("--query=")) {
            List<String> argQueries = SQLParser.parseQuery(arg.substring(8));
            if (!argQueries.isEmpty()) {
                if (queries == null) {
                    queries = argQueries;
                } else {
                    queries.addAll(argQueries);
                }
            }
        } else if (arg.startsWith("--output-format=")) {
            String formatName = extractArgInput(arg);
            if (formatName == null)
                return -1;
            formatName = formatName.toLowerCase();
            if (formatName.equals("fixed")) {
                m_outputFormatter = new SQLCommandOutputFormatterDefault();
            } else if (formatName.equals("csv")) {
                m_outputFormatter = new SQLCommandOutputFormatterCSV();
            } else if (formatName.equals("tab")) {
                m_outputFormatter = new SQLCommandOutputFormatterTabDelimited();
            } else {
                printUsage("Invalid value for --output-format");
                return -1;
            }
        } else if (arg.startsWith("--stop-on-error=")) {
            String optionName = extractArgInput(arg);
            if (optionName == null)
                return -1;
            optionName = optionName.toLowerCase();
            if (optionName.equals("true")) {
                m_stopOnError = true;
            } else if (optionName.equals("false")) {
                m_stopOnError = false;
            } else {
                printUsage("Invalid value for --stop-on-error");
                return -1;
            }
        } else if (arg.startsWith("--ddl-file=")) {
            String ddlFilePath = extractArgInput(arg);
            if (ddlFilePath == null)
                return -1;
            try {
                File ddlJavaFile = new File(ddlFilePath);
                Scanner scanner = new Scanner(ddlJavaFile);
                ddlFileText = scanner.useDelimiter("\\Z").next();
                scanner.close();
            } catch (FileNotFoundException e) {
                printUsage("DDL file not found at path:" + ddlFilePath);
                return -1;
            }
        } else if (arg.startsWith("--query-timeout=")) {
            m_hasBatchTimeout = true;
            String batchTimeoutStr = extractArgInput(arg);
            if (batchTimeoutStr == null)
                return -1;
            m_batchTimeout = Integer.valueOf(batchTimeoutStr);
        } else // equals check starting here
        if (arg.equals("--output-skip-metadata")) {
            m_outputShowMetadata = false;
        } else if (arg.startsWith("--ssl=")) {
            enableSSL = true;
            sslConfigFile = extractArgInput(arg);
            if (sslConfigFile == null)
                return -1;
        } else if (arg.startsWith("--ssl")) {
            enableSSL = true;
            sslConfigFile = null;
        } else if (arg.equals("--debug")) {
            m_debug = true;
        } else if (arg.equals("--help")) {
            // Print readme to the screen
            printHelp(System.out);
            System.out.println("\n\n");
            printUsage();
            return -1;
        } else if (arg.equals("--no-version-check")) {
            // Disable new version phone home check
            m_versionCheck = false;
        } else if ((arg.equals("--usage")) || (arg.equals("-?"))) {
            printUsage();
            return -1;
        } else {
            printUsage("Invalid Parameter: " + arg);
            return -1;
        }
    }
    // Split server list
    String[] servers = serverList.split(",");
    // Phone home to see if there is a newer version of VoltDB
    if (m_versionCheck) {
        openURLAsync();
    }
    try {
        // If we need to prompt the user for a password, do so.
        password = CLIConfig.readPasswordIfNeeded(user, password, "Enter password: ");
    } catch (IOException ex) {
        printUsage("Unable to read password: " + ex);
    }
    // Create connection
    ClientConfig config = new ClientConfig(user, password, null);
    if (enableSSL && sslConfigFile != null && !sslConfigFile.trim().isEmpty()) {
        config.setTrustStoreConfigFromPropertyFile(sslConfigFile);
        config.enableSSL();
    }
    // Set procedure all to infinite timeout, see ENG-2670
    config.setProcedureCallTimeout(0);
    try {
        // if specified enable kerberos
        if (!kerberos.isEmpty()) {
            config.enableKerberosAuthentication(kerberos);
        }
        m_client = getClient(config, servers, port);
    } catch (Exception exc) {
        System.err.println(exc.getMessage());
        return -1;
    }
    try {
        if (!ddlFileText.equals("")) {
            // fast DDL Loader mode
            // System.out.println("fast DDL Loader mode with DDL input:\n" + ddlFile);
            m_client.callProcedure("@AdHoc", ddlFileText);
            return m_exitCode;
        }
        // Load system procedures
        loadSystemProcedures();
        // Load user stored procs
        loadStoredProcedures(Procedures, Classlist);
        // Removed code to prevent Ctrl-C from exiting. The original code is visible
        // in Git history hash 837df236c059b5b4362ffca7e7a5426fba1b7f20.
        m_interactive = true;
        if (queries != null && !queries.isEmpty()) {
            // If queries are provided via command line options run them in
            // non-interactive mode.
            //TODO: Someday we should honor batching.
            m_interactive = false;
            for (String query : queries) {
                executeStatement(query, null, 0);
            }
        }
        // before quitting.
        if (System.console() == null && m_interactive) {
            m_interactive = false;
            executeNoninteractive();
        }
        if (m_interactive) {
            // Print out welcome message
            System.out.printf("SQL Command :: %s%s:%d\n", (user == "" ? "" : user + "@"), serverList, port);
            interactWithTheUser();
        }
    } catch (SQLCmdEarlyExitException e) {
        return m_exitCode;
    } catch (Exception x) {
        try {
            stopOrContinue(x);
        } catch (SQLCmdEarlyExitException e) {
            return m_exitCode;
        }
    } finally {
        try {
            m_client.close();
        } catch (Exception x) {
        }
    }
    //* enable to debug */ System.err.println("Exiting with code " + m_exitCode);
    return m_exitCode;
}
Also used : Scanner(java.util.Scanner) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ProcCallException(org.voltdb.client.ProcCallException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NoConnectionsException(org.voltdb.client.NoConnectionsException) ClientConfig(org.voltdb.client.ClientConfig) File(java.io.File)

Example 23 with ClientConfig

use of org.voltdb.client.ClientConfig in project voltdb by VoltDB.

the class TestJSONInterface method testAJAXAndClientTogether.

public void testAJAXAndClientTogether() throws Exception {
    try {
        String simpleSchema = "CREATE TABLE foo (\n" + "    bar BIGINT NOT NULL,\n" + "    PRIMARY KEY (bar)\n" + ");";
        VoltProjectBuilder builder = new VoltProjectBuilder();
        builder.addLiteralSchema(simpleSchema);
        builder.setHTTPDPort(8095);
        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();
        client = ClientFactory.createClient(new ClientConfig());
        client.createConnection("localhost");
        final AtomicLong fcnt = new AtomicLong(0);
        final AtomicLong scnt = new AtomicLong(0);
        final AtomicLong cfcnt = new AtomicLong(0);
        final AtomicLong cscnt = new AtomicLong(0);
        final int jsonRunnerCount = 50;
        final int clientRunnerCount = 50;
        final ParameterSet pset = ParameterSet.fromArrayNoCopy("select count(*) from foo");
        String responseJSON = callProcOverJSON("@AdHoc", pset, null, null, false);
        Response r = responseFromJSON(responseJSON);
        assertEquals(ClientResponse.SUCCESS, r.status);
        //Do replicated table read.
        class JSONRunner implements Runnable {

            @Override
            public void run() {
                try {
                    String rresponseJSON = callProcOverJSON("@AdHoc", pset, null, null, false);
                    System.out.println("Response: " + rresponseJSON);
                    Response rr = responseFromJSON(rresponseJSON);
                    assertEquals(ClientResponse.SUCCESS, rr.status);
                    scnt.incrementAndGet();
                } catch (Exception ex) {
                    fcnt.incrementAndGet();
                    ex.printStackTrace();
                }
            }
        }
        //Do replicated table read.
        class ClientRunner implements Runnable {

            class Callback implements ProcedureCallback {

                @Override
                public void clientCallback(ClientResponse clientResponse) throws Exception {
                    if (clientResponse.getStatus() == ClientResponse.SUCCESS) {
                        cscnt.incrementAndGet();
                    } else {
                        System.out.println("Client failed: " + clientResponse.getStatusString());
                        cfcnt.incrementAndGet();
                    }
                }
            }

            @Override
            public void run() {
                try {
                    if (!client.callProcedure(new Callback(), "@AdHoc", "SELECT count(*) from foo")) {
                        cfcnt.decrementAndGet();
                    }
                } catch (Exception ex) {
                    fcnt.incrementAndGet();
                    ex.printStackTrace();
                }
            }
        }
        //Start runners
        ExecutorService es = CoreUtils.getBoundedSingleThreadExecutor("runners", jsonRunnerCount);
        for (int i = 0; i < jsonRunnerCount; i++) {
            es.submit(new JSONRunner());
        }
        ExecutorService ces = CoreUtils.getBoundedSingleThreadExecutor("crunners", clientRunnerCount);
        for (int i = 0; i < clientRunnerCount; i++) {
            ces.submit(new ClientRunner());
        }
        es.shutdown();
        es.awaitTermination(1, TimeUnit.DAYS);
        assertEquals(jsonRunnerCount, scnt.get());
        ces.shutdown();
        ces.awaitTermination(1, TimeUnit.DAYS);
        client.drain();
        assertEquals(clientRunnerCount, cscnt.get());
        responseJSON = callProcOverJSON("@AdHoc", pset, null, null, false);
        r = responseFromJSON(responseJSON);
        assertEquals(ClientResponse.SUCCESS, r.status);
        //Make sure we are still good.
        ClientResponse resp = client.callProcedure("@AdHoc", "SELECT count(*) from foo");
        assertEquals(ClientResponse.SUCCESS, resp.getStatus());
    } finally {
        if (server != null) {
            server.shutdown();
            server.join();
        }
        server = null;
        if (client != null) {
            client.close();
        }
    }
}
Also used : ClientResponse(org.voltdb.client.ClientResponse) Configuration(org.voltdb.VoltDB.Configuration) JSONException(org.json_voltpatches.JSONException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) ClientResponse(org.voltdb.client.ClientResponse) HttpResponse(org.apache.http.HttpResponse) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProcedureCallback(org.voltdb.client.ProcedureCallback) VoltProjectBuilder(org.voltdb.compiler.VoltProjectBuilder) Configuration(org.voltdb.VoltDB.Configuration) ExecutorService(java.util.concurrent.ExecutorService) ClientConfig(org.voltdb.client.ClientConfig)

Example 24 with ClientConfig

use of org.voltdb.client.ClientConfig in project voltdb by VoltDB.

the class TestJDBCLoader method startDatabase.

@BeforeClass
public static void startDatabase() throws Exception {
    prepare();
    String pathToCatalog = Configuration.getPathToCatalogForTest("csv.jar");
    String pathToDeployment = Configuration.getPathToCatalogForTest("csv.xml");
    VoltProjectBuilder builder = new VoltProjectBuilder();
    builder.addLiteralSchema("create table BLAH (" + "clm_integer integer not null, " + "clm_tinyint tinyint default 0, " + "clm_smallint smallint default 0, " + "clm_bigint bigint default 0, " + "clm_string varchar(20) default null, " + "clm_decimal decimal default null, " + "clm_float float default null, " + "clm_timestamp timestamp default null, " + "PRIMARY KEY(clm_integer) " + ");\n" + "create table JBLAH (" + "clm_integer integer not null, " + "clm_tinyint tinyint default 0, " + "clm_smallint smallint default 0, " + "clm_bigint bigint default 0, " + "clm_string varchar(16) default null, " + "clm_decimal decimal default null, " + "clm_float float default null, " + "clm_timestamp timestamp default null, " + "PRIMARY KEY(clm_integer) " + ");");
    builder.addPartitionInfo("BLAH", "clm_integer");
    builder.addPartitionInfo("JBLAH", "clm_integer");
    boolean success = builder.compile(pathToCatalog, 2, 1, 0);
    assertTrue(success);
    MiscUtils.copyFile(builder.getPathToDeployment(), pathToDeployment);
    Configuration config = new Configuration();
    config.m_pathToCatalog = pathToCatalog;
    config.m_pathToDeployment = pathToDeployment;
    localServer = new ServerThread(config);
    client = null;
    localServer.start();
    localServer.waitForInitialization();
    client = ClientFactory.createClient(new ClientConfig());
    client.createConnection("localhost");
}
Also used : Configuration(org.voltdb.VoltDB.Configuration) VoltProjectBuilder(org.voltdb.compiler.VoltProjectBuilder) ServerThread(org.voltdb.ServerThread) ClientConfig(org.voltdb.client.ClientConfig) BeforeClass(org.junit.BeforeClass)

Example 25 with ClientConfig

use of org.voltdb.client.ClientConfig in project voltdb by VoltDB.

the class RegressionSuite method getClient.

public Client getClient(long timeout, ClientAuthScheme scheme, boolean useAdmin) throws IOException {
    final Random r = new Random();
    String listener = null;
    if (useAdmin) {
        listener = m_config.getAdminAddress(r.nextInt(m_config.getListenerCount()));
    } else {
        listener = m_config.getListenerAddress(r.nextInt(m_config.getListenerCount()));
    }
    ClientConfig config = new ClientConfigForTest(m_username, m_password, scheme);
    config.setConnectionResponseTimeout(timeout);
    config.setProcedureCallTimeout(timeout);
    final Client client = ClientFactory.createClient(config);
    // Use the port generated by LocalCluster if applicable
    try {
        client.createConnection(listener);
    }// retry once
     catch (ConnectException e) {
        if (useAdmin) {
            listener = m_config.getAdminAddress(r.nextInt(m_config.getListenerCount()));
        } else {
            listener = m_config.getListenerAddress(r.nextInt(m_config.getListenerCount()));
        }
        client.createConnection(listener);
    }
    m_clients.add(client);
    return client;
}
Also used : Random(java.util.Random) ClientConfig(org.voltdb.client.ClientConfig) ClientConfigForTest(org.voltdb.client.ClientConfigForTest) Client(org.voltdb.client.Client) ConnectException(java.net.ConnectException)

Aggregations

ClientConfig (org.voltdb.client.ClientConfig)38 Client (org.voltdb.client.Client)25 IOException (java.io.IOException)11 VoltTable (org.voltdb.VoltTable)10 Configuration (org.voltdb.VoltDB.Configuration)9 File (java.io.File)7 VoltProjectBuilder (org.voltdb.compiler.VoltProjectBuilder)7 ClientResponse (org.voltdb.client.ClientResponse)6 ServerThread (org.voltdb.ServerThread)5 ConnectException (java.net.ConnectException)4 UnknownHostException (java.net.UnknownHostException)4 Random (java.util.Random)4 Test (org.junit.Test)4 ClientConfigForTest (org.voltdb.client.ClientConfigForTest)4 ClientImpl (org.voltdb.client.ClientImpl)4 ProcCallException (org.voltdb.client.ProcCallException)4 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 VoltDB (org.voltdb.VoltDB)3