Search in sources :

Example 6 with DuplicateNameException

use of edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException in project GNS by MobilityFirst.

the class GNSClientCapacityTest 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[] { accountGUIDPrefix + i });
        try {
            accountGuidEntries[i] = GuidUtils.lookupOrCreateAccountGuid(clients[0], accountGUIDPrefix + i, PASSWORD);
            log.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i).getGuid()));
        } catch (DuplicateNameException e) {
            numPreExisting++;
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i);
            log.log(Level.INFO, "Found that account {0} already exists", new Object[] { accountGuidEntries[i] });
        }
    // any other exceptions should be thrown 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 = GuidUtils.lookupOrCreateGuid(clients[0], accountGuidEntries[i / numGuidsPerAccount], subGuid);
                    assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
                } catch (DuplicateNameException de) {
                // ignore, will retrieve it locally below
                }
            // any other exceptions should be thrown 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));
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) 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) HashSet(java.util.HashSet)

Example 7 with DuplicateNameException

use of edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException in project GNS by MobilityFirst.

the class BatchCreateOpsFail method setupClientsAndGuids.

private void setupClientsAndGuids() throws Exception {
    clients = new GNSClientCommands[numClients];
    for (int i = 0; i < numClients; i++) {
        clients[i] = new GNSClientCommands();
    }
    String gnsInstance = clients[0].getGNSProvider();
    accountGuidEntries = new GuidEntry[numAccountGuids];
    for (int i = 0; i < numAccountGuids; i++) {
        log.log(Level.FINE, "{0} creating account GUID {1}", new Object[] { this, ACCOUNT_GUID_PREFIX + i });
        try {
            accountGuidEntries[i] = clients[0].accountGuidCreate(ACCOUNT_GUID_PREFIX + i, PASSWORD);
            log.log(Level.FINE, "{0} created account ", new Object[] { this, accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i).getGuid()));
        } catch (DuplicateNameException e) {
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i);
            log.log(Level.INFO, "{0} found that account {1} already exists", new Object[] { this, accountGuidEntries[i] });
        }
    // any other exception should be throw up
    }
    System.out.println("Created or found pre-existing " + 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, "{0} created sub-guid(s) {1}", new Object[] { this, 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));
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) IOException(java.io.IOException) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) HashSet(java.util.HashSet)

Example 8 with DuplicateNameException

use of edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException in project GNS by MobilityFirst.

the class CommandUtils method checkResponse.

/**
   *
   * @param command
   *
   * @param responsePacket
   * @return Response as a string.
   * @throws ClientException
   */
public static ResponsePacket checkResponse(ResponsePacket responsePacket, CommandPacket command) throws ClientException {
    ResponseCode code = responsePacket.getErrorCode();
    String returnValue = responsePacket.getReturnValue();
    // wants to return a null value.
    if (code.isOKResult()) {
        return (returnValue.startsWith(GNSProtocol.NULL_RESPONSE.toString())) ? null : //returnValue;
        responsePacket;
    }
    // else error
    String errorSummary = code + ": " + returnValue + //+ ": " + responsePacket.getSummary()
    (command != null ? " for command " + command.getSummary() : "");
    switch(code) {
        case SIGNATURE_ERROR:
            throw new EncryptionException(code, errorSummary);
        case BAD_GUID_ERROR:
        case BAD_ACCESSOR_ERROR:
        case BAD_ACCOUNT_ERROR:
            throw new InvalidGuidException(code, errorSummary);
        case FIELD_NOT_FOUND_ERROR:
            throw new FieldNotFoundException(code, errorSummary);
        case ACCESS_ERROR:
            throw new AclException(code, errorSummary);
        case VERIFICATION_ERROR:
            throw new VerificationException(code, errorSummary);
        case ALREADY_VERIFIED_EXCEPTION:
            throw new VerificationException(code, errorSummary);
        case DUPLICATE_ID_EXCEPTION:
            //case DUPLICATE_NAME_EXCEPTION:
            throw new DuplicateNameException(code, errorSummary);
        case DUPLICATE_FIELD_EXCEPTION:
            throw new InvalidFieldException(code, errorSummary);
        case ACTIVE_REPLICA_EXCEPTION:
            throw new InvalidGuidException(code, errorSummary);
        case NONEXISTENT_NAME_EXCEPTION:
            throw new InvalidGuidException(code, errorSummary);
        case TIMEOUT:
        case RECONFIGURATION_EXCEPTION:
            throw new ClientException(code, errorSummary);
        default:
            throw new ClientException(code, "Error received with an unknown response code: " + errorSummary);
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) InvalidGuidException(edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException) VerificationException(edu.umass.cs.gnscommon.exceptions.client.VerificationException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) AclException(edu.umass.cs.gnscommon.exceptions.client.AclException) InvalidFieldException(edu.umass.cs.gnscommon.exceptions.client.InvalidFieldException)

Aggregations

DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)8 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)7 IOException (java.io.IOException)7 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)6 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)6 HashSet (java.util.HashSet)6 JSONException (org.json.JSONException)3 HttpClient (edu.umass.cs.gnsclient.client.http.HttpClient)1 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)1 AclException (edu.umass.cs.gnscommon.exceptions.client.AclException)1 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)1 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)1 InvalidFieldException (edu.umass.cs.gnscommon.exceptions.client.InvalidFieldException)1 InvalidGuidException (edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException)1 VerificationException (edu.umass.cs.gnscommon.exceptions.client.VerificationException)1 RandomString (edu.umass.cs.gnscommon.utils.RandomString)1 JSONObject (org.json.JSONObject)1