Search in sources :

Example 96 with GuidEntry

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

the class SomeReadsEncryptionFails method test_01_SingleWrite.

/**
   * Verifies a single write is successful.
   */
@Test
public void test_01_SingleWrite() {
    GuidEntry guid = guidEntries[0];
    try {
        clients[0].fieldUpdate(guid, someField, someValue);
        // verify written value
        Assert.assertEquals(clients[0].fieldRead(guid, someField), (someValue));
        Assert.assertEquals(clients[numClients > 1 ? 1 : 0].fieldRead(guid, someField), (someValue));
    } catch (IOException | ClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) Test(org.junit.Test) DefaultTest(edu.umass.cs.utils.DefaultTest)

Example 97 with GuidEntry

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

the class ContextAwareGroupGuidExample 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 {
    // BOILER PLATE FOR RUNNING AN EXAMPLE
    // Start the client
    client = new GNSClientCommands(null);
    try {
        // Create the account guid using your email address and password = "password"
        masterGuid = lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password");
    } catch (Exception e) {
        System.out.println("Exception during accountGuid creation: " + e);
        System.exit(1);
    }
    System.out.println("Client connected to GNS.");
    // Create 5 guids each of which have the field using our fieldname with a value of 25
    for (int cnt = 0; cnt < 5; cnt++) {
        GuidEntry guidEntry = client.guidCreate(masterGuid, "valueField-" + cnt);
        client.fieldUpdate(guidEntry, fieldName, 25);
    }
    //FIXME: THIS CODE IS OBSOLETE. THE INTERFACE HAS CHANGED.
    // Create a guid for the first group
    groupOneGuidEntry = client.guidCreate(masterGuid, "contextAware > 20 Group");
    // Set up the group context with a query
    String query = "~" + fieldName + " : {$gt: 20}";
    // Note that the zero for the interval means that we will never get stale results (not a 
    // good idea to do in production code! The default is 60 (seconds))
    JSONArray result = client.selectSetupGroupQuery(masterGuid, groupOneGuidEntry.getGuid(), query, 0);
    // Show the values from the guids that we got back
    System.out.println("Members of " + groupOneGuidEntry.getEntityName() + ":");
    showFieldValuesInGuids(result, fieldName);
    //FIXME: THIS CODE IS OBSOLETE. THE INTERFACE HAS CHANGED.
    // Create a second group guid
    groupTwoGuidEntry = client.guidCreate(masterGuid, "contextAware = 0 Group");
    // Set up a second group with a different query
    query = "~" + fieldName + " : 0";
    // Note that the zero for the interval means that we will never get stale results (not a 
    // good idea to do in production code!  The default is 60 (seconds))
    JSONArray resultTwo = client.selectSetupGroupQuery(masterGuid, groupTwoGuidEntry.getGuid(), query, 0);
    // Show the values from the guids that we got back
    System.out.println("Members of " + groupTwoGuidEntry.getEntityName() + ": (should be empty)");
    showFieldValuesInGuids(resultTwo, fieldName);
    // Now we lookup the values of the first group again
    JSONArray resultThree = client.selectLookupGroupQuery(groupOneGuidEntry.getGuid());
    System.out.println("Members of " + groupOneGuidEntry.getEntityName() + ": (should still be 5 of them)");
    showFieldValuesInGuids(resultThree, fieldName);
    // THIS IS WHERE IT GETS INTERESTING
    // And change the values of all but one of them
    System.out.println("Changing 4 of the 5 guids to have a their " + fieldName + " field's value be 0");
    for (int i = 0; i < result.length() - 1; i++) {
        BasicGuidEntry guidInfo = new BasicGuidEntry(client.lookupGuidRecord(result.getString(i)));
        GuidEntry entry = GuidUtils.lookupGuidEntryFromDatabase(client, guidInfo.getEntityName());
        System.out.println("Changing value of " + fieldName + " field in " + entry.getEntityName() + " to 0");
        client.fieldUpdate(entry, fieldName, 0);
    }
    // Now we lookup the values of the first group again - showing that there is only one guid in the group
    JSONArray resultFour = client.selectLookupGroupQuery(groupOneGuidEntry.getGuid());
    System.out.println("Members of " + groupOneGuidEntry.getEntityName() + ": (should only be one of them)");
    showFieldValuesInGuids(resultFour, fieldName);
    // Now we lookup the values of the second group again - this one now has 4 guids
    JSONArray resultFive = client.selectLookupGroupQuery(groupTwoGuidEntry.getGuid());
    System.out.println("Members of " + groupTwoGuidEntry.getEntityName() + ": (should be 4 of them)");
    showFieldValuesInGuids(resultFive, fieldName);
    // So we can run this again without duplicate name/guid errors.
    cleanupAllGuids();
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) BasicGuidEntry(edu.umass.cs.gnsclient.client.util.BasicGuidEntry) JSONArray(org.json.JSONArray) 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) BasicGuidEntry(edu.umass.cs.gnsclient.client.util.BasicGuidEntry) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 98 with GuidEntry

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

the class ServerFailureTests method test_010_Throughput.

/**
   *
   * @throws Exception
   */
@Test
public void test_010_Throughput() throws Exception {
    String threadString = System.getProperty("failureTest.threads");
    int numThreads = 20;
    if (threadString != null) {
        numThreads = Integer.parseInt(threadString);
    }
    System.out.println("Creating guid for the test...");
    String testGuidName = "testGUID" + RandomString.randomString(6);
    final GuidEntry testGuid;
    try {
        testGuid = client.guidCreate(masterGuid, testGuidName);
        System.out.println("Guid created.");
    } catch (Exception e) {
        System.out.println("Exception while creating testGuid: " + e);
        throw e;
    }
    startClock = System.currentTimeMillis();
    numRequestsSuccessful = 0;
    Thread[] threads = new Thread[numThreads];
    System.out.println("Spawning client threads...");
    for (int i = 0; i < numThreads; i++) {
        threads[i] = new Thread() {

            public void run() {
                try {
                    //Reads the guidEntry continuously until the thread is interrupted.
                    while (true) {
                        if (Thread.interrupted()) {
                            return;
                        }
                        try {
                            final int roundNumber = suite.roundNumber;
                            assertEquals(masterGuid.getGuid(), client.lookupPrimaryGuid(testGuid.getGuid()));
                            //We spawn a new thread that will sequentially increment the number of requests that have been successful so we don't have to block this thread.
                            Thread increment = new Thread() {

                                public void run() {
                                    synchronized (suite) {
                                        if (suite.roundNumber == roundNumber) {
                                            //If the testing has moved on to a new failure phase, then don't count this request since it was started before.
                                            suite.numRequestsSuccessful++;
                                        }
                                    }
                                }
                            };
                            increment.run();
                        } catch (IOException | ClientException e) {
                        //Ignore failures to read, they just won't increment the successful request count.
                        }
                    }
                } catch (Exception e) {
                    //Since this is threaded we need to handle any exceptions.  In this case by failing the test.
                    StringWriter printException = new StringWriter();
                    e.printStackTrace(new PrintWriter(printException));
                    fail("A testing thread threw an exception during the test:\n" + printException.toString());
                }
            }
        };
        threads[i].start();
    }
    System.out.println("Client threads all spawned.");
    //Wait TEST_TIME ms before ending the test phase.
    int requestCount = 0;
    //Cause a majority of servers to fail.  So if there are 5 servers it will fail 0, 1, 2, 3 servers and expect throughput to hit 0 on the 4th round of testing.
    int NUM_FAILURES = (numServers / 2) + 1;
    endClock = System.currentTimeMillis();
    double throughput = 0;
    for (int i = 0; i <= NUM_FAILURES; i++) {
        synchronized (suite) {
            //This is used so we don't end up counting throughput from requests of the previous phase.
            suite.roundNumber = i;
        }
        //Bring all servers back up
        recoverAllServers();
        //Start with 0 failures, then 1, then 2, etc.
        System.out.println("Causing " + i + " server failures.");
        for (int j = 0; j < i; j++) {
            causeRandomServerFailure();
        }
        synchronized (suite) {
            startClock = System.currentTimeMillis();
            suite.numRequestsSuccessful = 0;
        }
        while (endClock - startClock < TEST_TIME) {
            Thread.sleep(1000);
            endClock = System.currentTimeMillis();
            requestCount = suite.numRequestsSuccessful;
            throughput = 1000 * requestCount / (endClock - startClock);
            System.out.println("Average read throughput: " + Double.toString(throughput));
        }
        if (i < NUM_FAILURES) {
            if (throughput < EXPECTED_MIN_THROUGHPUT) {
                fail("The measured throughput of " + Double.toString(throughput) + " requests/second was below the expected minimum throughput of " + Integer.toString(EXPECTED_MIN_THROUGHPUT) + " requests/second.");
            }
        } else //This checks that the throughput was 0 on the last round, which is when a majority of replicas were down.
        if (throughput > 0) {
            fail("When a majority of " + Integer.toString(NUM_FAILURES) + " out of " + Integer.toString(numServers) + " active replicas were down some requests were still handled!");
        }
    }
    //Stop all the threads.
    for (int i = 0; i < numThreads; i++) {
        threads[i].interrupt();
    }
/*for (int i = 0; i < numRuns; i++){
			if (i % 1000 == 0){
				System.out.println("Running test for the " + Integer.toString(i) + "th time.");
				endClock = System.currentTimeMillis();
				double deltaSeconds = (endClock - startClock) / 1000;
				System.out.println("Throughput: " + Double.toString(numRequestsSuccessful / deltaSeconds) + " requests/second.");
				startClock = System.currentTimeMillis();
				numRequestsSuccessful = 0;
			}
		    try {
			      assertEquals(masterGuid.getGuid(), client.lookupPrimaryGuid(testGuid.getGuid()));
			      numRequestsSuccessful++;
			    } catch (IOException | ClientException e) {
			      //System.out.println("Exception while looking up primary guid on " + Integer.toString(i)+ "th run : " + e);
			      //throw e;
			    	//Ignore failures to read, they just won't increment the successful request count.
			    }*/
}
Also used : StringWriter(java.io.StringWriter) RandomString(edu.umass.cs.gnscommon.utils.RandomString) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 99 with GuidEntry

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

the class ServerConnectTest method test_020_RemoveGuid.

/**
   * Removes a guid.
   */
@Test
public void test_020_RemoveGuid() {
    String testGuidName = "testGUID" + RandomString.randomString(12);
    GuidEntry testGuid = null;
    try {
        testGuid = client.guidCreate(masterGuid, testGuidName);
    } catch (Exception e) {
        failWithStackTrace("Exception while creating testGuid: ", e);
    }
    waitSettle();
    try {
        client.guidRemove(masterGuid, testGuid.getGuid());
    } catch (Exception e) {
        failWithStackTrace("Exception while removing testGuid: ", e);
    }
    waitSettle();
    try {
        client.lookupGuidRecord(testGuid.getGuid());
        failWithStackTrace("Lookup testGuid should have throw an exception.");
    } catch (ClientException e) {
    } catch (IOException e) {
        failWithStackTrace("Exception while doing Lookup testGuid: ", e);
    }
}
Also used : RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) Test(org.junit.Test) DefaultTest(edu.umass.cs.utils.DefaultTest)

Example 100 with GuidEntry

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

the class ServerIntegrationTest method test_331_QuerySelectWorldReadable.

/**
   * Tests that selectQuery without a reader will return results from world readable fields.
   */
@Test
public void test_331_QuerySelectWorldReadable() {
    String fieldName = "testQueryWorldReadable";
    try {
        for (int cnt = 0; cnt < 5; cnt++) {
            GuidEntry testEntry = clientCommands.guidCreate(masterGuid, "queryTest-" + RandomString.randomString(12));
            // save them so we can delete them later
            CREATED_GUIDS.add(testEntry);
            JSONArray array = new JSONArray(Arrays.asList(25));
            clientCommands.fieldReplaceOrCreateList(testEntry.getGuid(), fieldName, array, testEntry);
        }
    } catch (ClientException | IOException e) {
        failWithStackTrace("Exception while trying to create the guids: ", e);
    }
    try {
        //See comment under the method header for test_320_GeoSpatialSelect
        waitSettle(SELECT_WAIT);
        String query = "~" + fieldName + " : ($gt: 0)";
        JSONArray result = clientCommands.selectQuery(query);
        //      for (int i = 0; i < result.length(); i++) {
        //        System.out.print("guid: " + result.get(i).toString() + "  ");
        //      }
        // best we can do should be at least 5, but possibly more objects in
        // results
        Assert.assertThat(result.length(), Matchers.greaterThanOrEqualTo(5));
    } catch (ClientException | IOException e) {
        failWithStackTrace("Exception executing selectQuery: ", e);
    }
    try {
        for (GuidEntry guid : CREATED_GUIDS) {
            clientCommands.guidRemove(masterGuid, guid.getGuid());
        }
        CREATED_GUIDS.clear();
    } catch (ClientException | IOException e) {
        failWithStackTrace("Exception during cleanup: " + e);
    }
}
Also used : JSONArray(org.json.JSONArray) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) BasicGuidEntry(edu.umass.cs.gnsclient.client.util.BasicGuidEntry) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest) Test(org.junit.Test)

Aggregations

GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)134 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)87 IOException (java.io.IOException)85 Test (org.junit.Test)69 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)61 RandomString (edu.umass.cs.gnscommon.utils.RandomString)54 BasicGuidEntry (edu.umass.cs.gnsclient.client.util.BasicGuidEntry)46 JSONException (org.json.JSONException)45 JSONArray (org.json.JSONArray)36 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)28 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)15 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)14 StringTokenizer (java.util.StringTokenizer)13 JSONObject (org.json.JSONObject)12 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)11 FileNotFoundException (java.io.FileNotFoundException)11 HashSet (java.util.HashSet)10 FileInputStream (java.io.FileInputStream)8 Repeat (edu.umass.cs.utils.Repeat)7