Search in sources :

Example 11 with GNSClient

use of edu.umass.cs.gnsclient.client.GNSClient in project GNS by MobilityFirst.

the class ServerConnectTest method setUpBeforeClass.

/**
   *
   * @throws Exception
   */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    if (System.getProperty("startServer") != null && System.getProperty("startServer").equals("true")) {
        // clear explicitly if gigapaxos
        if (useGPScript()) {
            RunCommand.command("kill -s TERM `ps -ef | grep GNS.jar | grep -v grep | " + "grep -v ServerConnectTest  | grep -v \"context\" | awk '{print $2}'`", ".");
            System.out.println(System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + getGigaPaxosOptions() + " forceclear all");
            RunCommand.command(System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + getGigaPaxosOptions() + " forceclear all", ".");
            /* We need to do this to limit the number of files used by mongo.
				 * Otherwise failed runs quickly lead to more failed runs because
				 * index files created in previous runs are not removed.
         */
            dropAllDatabases();
            options = getGigaPaxosOptions() + " restart all";
        } else {
            options = SCRIPTS_OPTIONS;
        }
        System.out.println(System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + options);
        ArrayList<String> output = RunCommand.command(System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + options, ".");
        if (output != null) {
            for (String line : output) {
                System.out.println(line);
            }
        } else {
            failWithStackTrace("Server command failure: ; aborting all tests.");
        }
    }
    String gpConfFile = System.getProperty(DefaultProps.GIGAPAXOS_CONFIG.key);
    String logFile = System.getProperty(DefaultProps.LOGGING_PROPERTIES.key);
    ArrayList<String> output = RunCommand.command("cat " + logFile + " | grep \"java.util.logging.FileHandler.pattern\" | sed 's/java.util.logging.FileHandler.pattern = //g'", ".", false);
    String logFiles = output.get(0) + "*";
    System.out.println("Waiting for servers to be ready...");
    output = RunCommand.command("cat " + gpConfFile + " | grep \"reconfigurator\\.\" | wc -l ", ".", false);
    int numRC = Integer.parseInt(output.get(0));
    output = RunCommand.command("cat " + gpConfFile + " | grep \"active\\.\" | wc -l ", ".", false);
    int numAR = Integer.parseInt(output.get(0));
    int numServers = numRC + numAR;
    output = RunCommand.command("cat " + logFiles + " | grep \"server ready\" | wc -l ", ".", false);
    int numServersUp = Integer.parseInt(output.get(0));
    while (numServersUp < numServers) {
        Thread.sleep(5000);
        output = RunCommand.command("cat " + logFiles + " | grep \"server ready\" | wc -l ", ".", false);
        numServersUp = Integer.parseInt(output.get(0));
        System.out.println(Integer.toString(numServersUp) + " out of " + Integer.toString(numServers) + " servers are ready.");
    }
    System.out.println("Starting client");
    client = new GNSClientCommands();
    // Make all the reads be coordinated
    client.setForceCoordinatedReads(true);
    // arun: connectivity check embedded in GNSClient constructor
    boolean connected = client instanceof GNSClient;
    if (connected) {
        System.out.println("Client created and connected to server.");
    }
    //
    int tries = 5;
    boolean accountCreated = false;
    do {
        try {
            System.out.println("Creating account guid: " + (tries - 1) + " attempt remaining.");
            masterGuid = GuidUtils.lookupOrCreateAccountGuid(client, accountAlias, PASSWORD, true);
            accountCreated = true;
        } catch (Exception e) {
            e.printStackTrace();
            ThreadUtils.sleep((5 - tries) * 5000);
        }
    } while (!accountCreated && --tries > 0);
    if (accountCreated == false) {
        failWithStackTrace("Failure setting up account guid; aborting all tests.");
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) GNSClient(edu.umass.cs.gnsclient.client.GNSClient) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 12 with GNSClient

use of edu.umass.cs.gnsclient.client.GNSClient in project GNS by MobilityFirst.

the class AdminBadAuthTest method setupBeforeClass.

/**
   *
   * @throws IOException
   */
@BeforeClass
public static void setupBeforeClass() throws IOException {
    System.out.println("Starting client");
    clientCommands = new GNSClientCommands();
    // Make all the reads be coordinated
    clientCommands.setForceCoordinatedReads(true);
    // arun: connectivity check embedded in GNSClient constructor
    boolean connected = clientCommands instanceof GNSClient;
    if (connected) {
        System.out.println("Client created and connected to server.");
    }
    badClient = new BadClient(ReconfigurationConfig.getReconfiguratorAddresses());
    badClient.setForceCoordinatedReads(true);
    connected = badClient instanceof GNSClient;
    if (connected) {
        System.out.println("BadClient created and connected to server.");
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) GNSClient(edu.umass.cs.gnsclient.client.GNSClient) BeforeClass(org.junit.BeforeClass)

Example 13 with GNSClient

use of edu.umass.cs.gnsclient.client.GNSClient in project GNS by MobilityFirst.

the class ClientExample method main.

/**
	 * @param args
	 * @throws IOException
	 * @throws InvalidKeySpecException
	 * @throws NoSuchAlgorithmException
	 * @throws ClientException
	 * @throws InvalidKeyException
	 * @throws SignatureException
	 * @throws Exception
	 */
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
    /* Create the client that connects to a default reconfigurator as
		 * specified in gigapaxos properties file. */
    client = new GNSClient();
    System.out.println("[Client connected to GNS]\n");
    try {
        /**
			 * Create an account GUID if one doesn't already exists.
			 */
        System.out.println("// account GUID creation\n" + "client.execute(" + ACCOUNT_NAME + ")");
        client.execute(GNSCommand.createAccount(ACCOUNT_NAME));
        GUID = GuidUtils.getGUIDKeys(ACCOUNT_NAME);
    } catch (Exception | Error e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    // Create a JSON Object to initialize our guid record
    JSONObject json = new JSONObject("{\"occupation\":\"busboy\"," + "\"friends\":[\"Joe\",\"Sam\",\"Billy\"]," + "\"gibberish\":{\"meiny\":\"bloop\",\"einy\":\"floop\"}," + "\"location\":\"work\",\"name\":\"frank\"}");
    // Write out the JSON Object
    client.execute(GNSCommand.update(GUID, json));
    System.out.println("\n// record update\n" + "client.update(GUID, record) // record=" + json);
    // and read the entire object back in
    JSONObject result = client.execute(GNSCommand.read(GUID)).getResultJSONObject();
    System.out.println("client.read(GUID) -> " + result.toString());
    // Change a field
    client.execute(GNSCommand.update(GUID, new JSONObject("{\"occupation\":\"rocket scientist\"}")));
    System.out.println("\n// field update\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue={\"occupation\":\"rocket scientist\"}");
    // and read the entire object back in
    result = client.execute(GNSCommand.read(GUID)).getResultJSONObject();
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add a field
    client.execute(GNSCommand.update(GUID, new JSONObject("{\"ip address\":\"127.0.0.1\"}")));
    System.out.println("\n// field add\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue= {\"ip address\":\"127.0.0.1\"}");
    // and read the entire object back in
    result = client.execute(GNSCommand.read(GUID)).getResultJSONObject();
    System.out.println("client.read(GUID) -> " + result.toString());
    // Remove a field
    client.execute(GNSCommand.fieldRemove(GUID.getGuid(), "gibberish", GUID));
    System.out.println("\n// field remove\n" + "client.fieldRemove(GUID, \"gibberish\")");
    // and read the entire object back in
    result = client.execute(GNSCommand.read(GUID)).getResultJSONObject();
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add some more stuff to read back
    JSONObject newJson = new JSONObject();
    JSONObject subJson = new JSONObject();
    subJson.put("sally", "red");
    subJson.put("sammy", "green");
    JSONObject subsubJson = new JSONObject();
    subsubJson.put("right", "seven");
    subsubJson.put("left", "eight");
    subJson.put("sally", subsubJson);
    newJson.put("flapjack", subJson);
    client.execute(GNSCommand.update(GUID, newJson));
    System.out.println("\n// field add with JSON value\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue=" + newJson);
    // Read a single field at the top level
    String resultString = client.execute(GNSCommand.fieldRead(GUID, "flapjack")).getResultString();
    System.out.println("client.fieldRead(\"flapjack\") -> " + resultString);
    // Read a single field using dot notation
    resultString = client.execute(GNSCommand.fieldRead(GUID, "flapjack.sally.right")).getResultString();
    System.out.println("\n// dotted field read\n" + "client.fieldRead(GUID, \"flapjack.sally.right\") -> " + resultString);
    // Update a field using dot notation
    JSONArray newValue = new JSONArray(Arrays.asList("One", "Ready", "Frap"));
    client.execute(GNSCommand.fieldUpdate(GUID, "flapjack.sammy", newValue));
    System.out.println("\n// dotted field update\n" + "client.fieldUpdate(GUID, \"flapjack.sammy\", " + newValue);
    // Read the same field using dot notation
    result = client.execute(GNSCommand.fieldRead(GUID, "flapjack.sammy")).getResultJSONObject();
    System.out.println("client.fieldRead(GUID, \"flapjack.sammy\") -> " + result);
    // Read two fields at a time
    result = client.execute(GNSCommand.fieldRead(GUID, new ArrayList<String>(Arrays.asList("name", "occupation")))).getResultJSONObject();
    System.out.println("\n// multi-field read\n" + "client.fieldRead(GUID, [\"name\",\"occupation\"]) -> " + result);
    // Read the entire object back in
    result = client.execute(GNSCommand.read(GUID)).getResultJSONObject();
    System.out.println("\nclient.read(GUID) -> " + result.toString());
    // Delete created GUID
    // client.execute(GNSCommand.accountGuidRemove(guid));
    System.out.println("\n// GUID delete\n" + "client.accountGuidRemove(GUID) // GUID=" + GUID);
    // Try read the entire record after deleting (expecting to fail)
    try {
        result = client.execute(GNSCommand.read(GUID)).getResultJSONObject();
    } catch (Exception e) {
        System.out.println("\n// non-existent GUID error (expected)\n" + "client.read(GUID) // GUID= " + GUID + "\n  " + e.getMessage());
    }
    client.close();
    System.out.println("\nclient.close() // test successful");
}
Also used : GNSClient(edu.umass.cs.gnsclient.client.GNSClient) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

GNSClient (edu.umass.cs.gnsclient.client.GNSClient)13 IOException (java.io.IOException)11 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)7 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)6 BeforeClass (org.junit.BeforeClass)6 File (java.io.File)4 JSONException (org.json.JSONException)4 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)3 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)3 RandomString (edu.umass.cs.gnscommon.utils.RandomString)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)1 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)1