use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class SomeReadsEncryptionFails method setupClientsAndGuids.
private static void setupClientsAndGuids() throws Exception {
clients = new GNSClientCommands[numClients];
executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(numClients);
for (int i = 0; i < numClients; i++) {
clients[i] = new GNSClientCommands();
}
String gnsInstance = clients[0].getGNSProvider();
accountGuidEntries = new GuidEntry[numAccountGuids];
int numPreExisting = 0;
for (int i = 0; i < numAccountGuids; i++) {
log.log(Level.FINE, "Creating account GUID {0}", new Object[] { ACCOUNT_GUID_PREFIX + i });
try {
accountGuidEntries[i] = clients[0].accountGuidCreate(ACCOUNT_GUID_PREFIX + i, PASSWORD);
log.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i).getGuid()));
} catch (DuplicateNameException e) {
numPreExisting++;
accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i);
log.log(Level.INFO, "Found that account {0} already exists", new Object[] { accountGuidEntries[i] });
}
// any other exception should be throw up
}
System.out.println("Created (" + (numAccountGuids - numPreExisting) + ") or found pre-existing (" + numPreExisting + ") a total of " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
if (accountGuidsOnly) {
for (int i = 0; i < accountGuidEntries.length; i++) {
guidEntries[i] = accountGuidEntries[i];
}
return;
}
guidEntries = new GuidEntry[numGuids];
Set<String> subGuids = new HashSet<String>();
for (int i = 0, j = 0; i < numGuids; i++) {
subGuids.add(Config.getGlobalString(TC.TEST_GUID_PREFIX) + i);
if (subGuids.size() == numGuidsPerAccount || i == numGuids - 1) {
// because batch creation seems buggy
if (subGuids.size() == 1) {
String subGuid = subGuids.iterator().next();
try {
GuidEntry created = clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
} catch (DuplicateNameException de) {
// ignore, will retrieve it locally below
}
// any other exception should be throw up
} else {
try {
// batch create
clients[0].guidBatchCreate(accountGuidEntries[i / numGuidsPerAccount], subGuids);
} catch (Exception e) {
for (String subGuid : subGuids) {
try {
clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
} catch (DuplicateNameException de) {
// ignore, will retrieve it locally below
}
// any other exception should be throw up
}
}
}
for (String subGuid : subGuids) {
guidEntries[j++] = KeyPairUtils.getGuidEntry(gnsInstance, subGuid);
}
log.log(Level.FINE, "Created sub-guid(s) {0}", new Object[] { subGuids });
subGuids.clear();
}
}
for (GuidEntry guidEntry : accountGuidEntries) {
assert (guidEntry != null);
}
for (GuidEntry guidEntry : guidEntries) {
assert (guidEntry != null);
}
System.out.println("Created or found " + guidEntries.length + " pre-existing sub-guids " + Arrays.asList(guidEntries));
}
use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class ServerIntegrationTest method setUpBeforeClassOld.
/**
* @throws FileNotFoundException
* @throws IOException
* @throws InterruptedException
*/
@Deprecated
public static void setUpBeforeClassOld() throws FileNotFoundException, IOException, InterruptedException {
/* The waitTillAllServersReady parameter is not needed for
* single-machine tests as we check the logs explicitly below. It is
* still useful for distributed tests as there is no intentionally
* support in gigapaxos' async client to detect if all servrs are up. */
String waitString = System.getProperty("waitTillAllServersReady");
if (waitString != null) {
WAIT_TILL_ALL_SERVERS_READY = Integer.parseInt(waitString);
}
// get pattern for log files
Properties logProps = new Properties();
logProps.load(new FileInputStream(System.getProperty(DefaultProps.LOGGING_PROPERTIES.key)));
String logFiles = logProps.getProperty("java.util.logging.FileHandler.pattern");
if (logFiles != null) {
logFiles = logFiles.replaceAll("%.*", "").trim() + "*";
}
new File(logFiles.replaceFirst("/[^/]*$", "")).mkdirs();
if (logFiles != null) {
System.out.print("Deleting log files " + logFiles);
RunCommand.command("rm -f " + logFiles, ".", false);
System.out.print(" ...done" + logFiles);
}
// start server
if (System.getProperty("startServer") != null && System.getProperty("startServer").equals("true")) {
// clear explicitly if gigapaxos
if (useGPScript()) {
// forceclear
String forceClearCmd = System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + getGigaPaxosOptions() + " forceclear all";
System.out.println(forceClearCmd);
RunCommand.command(forceClearCmd, ".");
/* 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;
}
String startServerCmd = System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + options;
System.out.println(startServerCmd);
// servers are being started here
if (singleJVM()) {
startServersSingleJVM();
} else {
ArrayList<String> output = RunCommand.command(startServerCmd, ".");
if (output != null) {
for (String line : output) {
System.out.println(line);
}
} else {
failWithStackTrace("Server command failure: ; aborting all tests.");
}
}
}
int numServers = PaxosConfig.getActives().size() + ReconfigurationConfig.getReconfigurators().size();
ArrayList<String> output;
int numServersUp = 0;
// a little sleep ensures that there is time for at least one log file to get created
Thread.sleep(500);
if (!singleJVM()) {
do {
output = RunCommand.command("cat " + logFiles + " | grep -a \"server ready\" | wc -l ", ".", false);
String temp = output.get(0);
temp = temp.replaceAll("\\s", "");
try {
numServersUp = Integer.parseInt(temp);
} catch (NumberFormatException e) {
// can happen if no files have yet gotten created
System.out.println(e);
}
System.out.println(Integer.toString(numServersUp) + " out of " + Integer.toString(numServers) + " servers are ready.");
Thread.sleep(1000);
} while (numServersUp < numServers);
}
System.out.println("Starting client");
int numRetries = 2;
boolean forceCoordinated = true;
clientCommands = (GNSClientCommands) new GNSClientCommands().setNumRetriesUponTimeout(numRetries).setForceCoordinatedReads(forceCoordinated);
client = new GNSClient().setNumRetriesUponTimeout(numRetries).setForceCoordinatedReads(forceCoordinated).setForcedTimeout(8000);
System.out.println("Client created and connected to server.");
//
int tries = 5;
boolean accountCreated = false;
Thread.sleep(WAIT_TILL_ALL_SERVERS_READY);
do {
try {
System.out.println("Creating account guid: " + (tries - 1) + " attempt remaining.");
String createdGUID = client.execute(GNSCommand.createAccount(accountAlias)).getResultString();
Assert.assertEquals(createdGUID, GuidUtils.getGUIDKeys(accountAlias).guid);
// older code; okay to leave it hanging or to remove
masterGuid = GuidUtils.lookupOrCreateAccountGuid(clientCommands, accountAlias, PASSWORD, true);
accountCreated = true;
} catch (Exception e) {
e.printStackTrace();
ThreadUtils.sleep((5 - tries) * 4000);
}
} while (!accountCreated && --tries > 0);
if (accountCreated == false) {
failWithStackTrace("Failure setting up account guid; aborting all tests.");
}
}
use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class HighConcurrencyReadsTest method cleanup.
/**
* Removes all account and sub-guids created during the test.
*
* @throws Exception
*/
@AfterClass
public static void cleanup() throws Exception {
Thread.sleep(2000);
assert (clients != null && clients[0] != null);
if (!accountGuidsOnly) {
System.out.println("About to delete " + guidEntries.length + " sub-guids: " + Arrays.asList(guidEntries));
for (GuidEntry guidEntry : guidEntries) {
try {
LOGGER.log(Level.FINE, "About to delete sub-guid {0}", new Object[] { guidEntry });
clients[0].guidRemove(guidEntry);
LOGGER.log(Level.FINE, "Deleted sub-guid {0}", new Object[] { guidEntry });
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to delete sub-guid {0}", new Object[] { guidEntry });
e.printStackTrace();
// continue with rest
}
}
}
System.out.println("About to delete " + accountGuidEntries.length + " account guids: " + Arrays.asList(accountGuidEntries));
for (GuidEntry accGuidEntry : accountGuidEntries) {
try {
LOGGER.log(Level.FINE, "About to delete account guid {0}", new Object[] { accGuidEntry });
clients[0].accountGuidRemove(accGuidEntry);
LOGGER.log(Level.FINE, "Deleted account guid {0}", new Object[] { accGuidEntry });
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to delete account guid {0}", new Object[] { accGuidEntry });
e.printStackTrace();
// continue with rest
}
}
for (GNSClientCommands client : clients) {
client.close();
}
executor.shutdown();
System.out.println(DelayProfiler.getStats());
}
use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class GNSClientCommandsExample 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 GNSClientCommands();
System.out.println("[Client connected to GNS]\n");
try {
/**
* Create an account GUID if one doesn't already exists. The true
* flag makes it verbosely print out what it is doing. The password
* is for future use and is needed mainly if the keypair is
* generated on the server in order to retrieve the private key.
* lookupOrCreateAccountGuid "cheats" by bypassing email-based or
* other verification mechanisms using a shared secret between the
* server and the client.
*
*/
System.out.println("// account GUID creation\n" + "GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS," + " \"password\", true)");
guid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
} 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.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.read(guid);
System.out.println("client.read(GUID) -> " + result.toString());
// Change a field
client.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.read(guid);
System.out.println("client.read(GUID) -> " + result.toString());
// Add a field
client.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.read(guid);
System.out.println("client.read(GUID) -> " + result.toString());
// Remove a field
client.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.read(guid);
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.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.fieldRead(guid, "flapjack");
System.out.println("client.fieldRead(\"flapjack\") -> " + resultString);
// Read a single field using dot notation
resultString = client.fieldRead(guid, "flapjack.sally.right");
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.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
resultString = client.fieldRead(guid, "flapjack.sammy");
System.out.println("client.fieldRead(GUID, \"flapjack.sammy\") -> " + resultString);
// Read two fields at a time
resultString = client.fieldRead(guid, new ArrayList<String>(Arrays.asList("name", "occupation")));
System.out.println("\n// multi-field read\n" + "client.fieldRead(GUID, [\"name\",\"occupation\"] -> " + resultString);
// Read the entire object back in
result = client.read(guid);
System.out.println("\nclient.read(GUID) -> " + result.toString());
// Delete created GUID
client.accountGuidRemove(guid);
System.out.println("\n// GUID delete\n" + "client.accountGuidRemove(GUID) // GUID=" + guid);
// Try read the entire record
try {
result = client.read(guid);
} 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");
}
use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class Select method parse.
@Override
public void parse(String commandText) throws Exception {
try {
GNSClientCommands gnsClient = module.getGnsClient();
StringTokenizer st = new StringTokenizer(commandText.trim());
GuidEntry guidEntry;
switch(st.countTokens()) {
case 2:
guidEntry = null;
break;
case 3:
String alias = st.nextToken();
guidEntry = KeyPairUtils.getGuidEntry(module.getGnsInstance(), alias);
if (guidEntry == null) {
console.printString("Unknown alias " + alias);
console.printNewline();
return;
}
break;
default:
wrongArguments();
return;
}
String field = st.nextToken();
String value = st.nextToken();
JSONArray result;
if (guidEntry != null) {
result = gnsClient.select(guidEntry, field, value);
} else {
result = gnsClient.select(field, value);
}
console.printString(result.toString());
console.printNewline();
} catch (IOException | ClientException e) {
console.printString("Failed to access GNS ( " + e + ")\n");
}
}
Aggregations