Search in sources :

Example 1 with GetMetadataResponseSender

use of voldemort.rest.GetMetadataResponseSender in project voldemort by voldemort.

the class CoordinatorWorkerThread method run.

@Override
public // TODO: Add perf stats in the next iteration
void run() {
    Object message = messageEvent.getMessage();
    if (message instanceof CoordinatorStoreClientRequest) {
        CoordinatorStoreClientRequest storeClientRequestObject = (CoordinatorStoreClientRequest) message;
        this.requestObject = storeClientRequestObject.getRequestObject();
        this.storeClient = storeClientRequestObject.getStoreClient();
        // This shouldn't ideally happen.
        if (this.requestObject != null) {
            switch(requestObject.getOperationType()) {
                case VoldemortOpCode.GET_METADATA_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("GET Metadata request received.");
                    }
                    try {
                        String queryStoreName = ByteUtils.getString(this.requestObject.getKey().get(), "UTF-8");
                        StoreDefinition storeDef = StoreDefinitionUtils.getStoreDefinitionWithName(this.coordinatorMetadata.getStoresDefs(), queryStoreName);
                        String serializerInfoXml = RestUtils.constructSerializerInfoXml(storeDef);
                        GetMetadataResponseSender metadataResponseSender = new GetMetadataResponseSender(messageEvent, serializerInfoXml.getBytes());
                        metadataResponseSender.sendResponse(this.coordinatorPerfStats, true, this.requestObject.getRequestOriginTimeInMs());
                        if (logger.isDebugEnabled()) {
                            logger.debug("GET Metadata successful !");
                        }
                    } catch (Exception e) {
                        /*
                             * We might get InsufficientOperationalNodes
                             * exception due to a timeout, thus creating
                             * confusion in the root cause. Hence explicitly
                             * check for timeout.
                             */
                        if (System.currentTimeMillis() >= (this.requestObject.getRequestOriginTimeInMs() + this.requestObject.getRoutingTimeoutInMs())) {
                            RestErrorHandler.writeErrorResponse(this.messageEvent, REQUEST_TIMEOUT, "GET METADATA request timed out: " + e.getMessage());
                        } else {
                            getErrorHandler.handleExceptions(messageEvent, e);
                        }
                    }
                    break;
                case VoldemortOpCode.GET_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("GET request received.");
                    }
                    try {
                        boolean keyExists = false;
                        List<Versioned<byte[]>> versionedValues = this.storeClient.getWithCustomTimeout(this.requestObject);
                        if (versionedValues == null || versionedValues.size() == 0) {
                            if (this.requestObject.getValue() != null) {
                                if (versionedValues == null) {
                                    versionedValues = new ArrayList<Versioned<byte[]>>();
                                }
                                versionedValues.add(this.requestObject.getValue());
                                keyExists = true;
                            }
                        } else {
                            keyExists = true;
                        }
                        if (keyExists) {
                            GetResponseSender responseConstructor = new GetResponseSender(messageEvent, requestObject.getKey(), versionedValues, this.storeClient.getStoreName());
                            responseConstructor.sendResponse(this.coordinatorPerfStats, true, this.requestObject.getRequestOriginTimeInMs());
                            if (logger.isDebugEnabled()) {
                                logger.debug("GET successful !");
                            }
                        } else {
                            RestErrorHandler.writeErrorResponse(this.messageEvent, NOT_FOUND, "Requested Key does not exist");
                        }
                    } catch (Exception e) {
                        /*
                             * We might get InsufficientOperationalNodes
                             * exception due to a timeout, thus creating
                             * confusion in the root cause. Hence explicitly
                             * check for timeout.
                             */
                        if (System.currentTimeMillis() >= (this.requestObject.getRequestOriginTimeInMs() + this.requestObject.getRoutingTimeoutInMs())) {
                            RestErrorHandler.writeErrorResponse(this.messageEvent, REQUEST_TIMEOUT, "GET request timed out: " + e.getMessage());
                        } else {
                            getErrorHandler.handleExceptions(messageEvent, e);
                        }
                    }
                    break;
                case VoldemortOpCode.GET_ALL_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("GET ALL request received.");
                    }
                    try {
                        Map<ByteArray, List<Versioned<byte[]>>> versionedResponses = this.storeClient.getAllWithCustomTimeout(this.requestObject);
                        if (versionedResponses == null || versionedResponses.values().size() == 0) {
                            logger.error("Error when doing getall. Keys do not exist.");
                            RestErrorHandler.writeErrorResponse(this.messageEvent, NOT_FOUND, "Error when doing getall. Keys do not exist.");
                        } else {
                            GetAllResponseSender responseConstructor = new GetAllResponseSender(messageEvent, versionedResponses, this.storeClient.getStoreName());
                            responseConstructor.sendResponse(this.coordinatorPerfStats, true, this.requestObject.getRequestOriginTimeInMs());
                            if (logger.isDebugEnabled()) {
                                logger.debug("GET ALL successful !");
                            }
                        }
                    } catch (Exception e) {
                        /*
                             * We might get InsufficientOperationalNodes
                             * exception due to a timeout, thus creating
                             * confusion in the root cause. Hence explicitly
                             * check for timeout.
                             */
                        if (System.currentTimeMillis() >= (this.requestObject.getRequestOriginTimeInMs() + this.requestObject.getRoutingTimeoutInMs())) {
                            RestErrorHandler.writeErrorResponse(this.messageEvent, REQUEST_TIMEOUT, "GET ALL request timed out: " + e.getMessage());
                        } else {
                            getErrorHandler.handleExceptions(messageEvent, e);
                        }
                    }
                    break;
                // TODO: Implement this in the next pass
                case VoldemortOpCode.GET_VERSION_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("Incoming get version request");
                    }
                    try {
                        if (logger.isDebugEnabled()) {
                            logger.debug("GET versions request successful !");
                        }
                    } catch (Exception e) {
                        /*
                             * We might get InsufficientOperationalNodes
                             * exception due to a timeout, thus creating
                             * confusion in the root cause. Hence explicitly
                             * check for timeout.
                             */
                        if (System.currentTimeMillis() >= (this.requestObject.getRequestOriginTimeInMs() + this.requestObject.getRoutingTimeoutInMs())) {
                            RestErrorHandler.writeErrorResponse(this.messageEvent, REQUEST_TIMEOUT, "GET VERSION request timed out: " + e.getMessage());
                        } else {
                            getVersionErrorHandler.handleExceptions(messageEvent, e);
                        }
                    }
                    break;
                case VoldemortOpCode.PUT_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("PUT request received.");
                    }
                    try {
                        VectorClock successfulPutVC = null;
                        if (this.requestObject.getValue() != null) {
                            successfulPutVC = ((VectorClock) this.storeClient.putVersionedWithCustomTimeout(this.requestObject)).clone();
                        } else {
                            successfulPutVC = ((VectorClock) this.storeClient.putWithCustomTimeout(this.requestObject)).clone();
                        }
                        PutResponseSender responseConstructor = new PutResponseSender(messageEvent, successfulPutVC, this.storeClient.getStoreName(), this.requestObject.getKey());
                        responseConstructor.sendResponse(this.coordinatorPerfStats, true, this.requestObject.getRequestOriginTimeInMs());
                        if (logger.isDebugEnabled()) {
                            logger.debug("PUT successful !");
                        }
                    } catch (Exception e) {
                        /*
                             * We might get InsufficientOperationalNodes
                             * exception due to a timeout, thus creating
                             * confusion in the root cause. Hence explicitly
                             * check for timeout.
                             */
                        if (System.currentTimeMillis() >= (this.requestObject.getRequestOriginTimeInMs() + this.requestObject.getRoutingTimeoutInMs())) {
                            RestErrorHandler.writeErrorResponse(this.messageEvent, REQUEST_TIMEOUT, "PUT request timed out: " + e.getMessage());
                        } else {
                            putErrorHandler.handleExceptions(messageEvent, e);
                        }
                    }
                    break;
                case VoldemortOpCode.DELETE_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("Incoming delete request");
                    }
                    try {
                        boolean isDeleted = this.storeClient.deleteWithCustomTimeout(this.requestObject);
                        if (isDeleted) {
                            DeleteResponseSender responseConstructor = new DeleteResponseSender(messageEvent, this.storeClient.getStoreName(), this.requestObject.getKey());
                            responseConstructor.sendResponse(this.coordinatorPerfStats, true, this.requestObject.getRequestOriginTimeInMs());
                            if (logger.isDebugEnabled()) {
                                logger.debug("DELETE request successful !");
                            }
                        } else {
                            logger.error("Requested Key with the specified version does not exist");
                            RestErrorHandler.writeErrorResponse(this.messageEvent, NOT_FOUND, "Requested Key with the specified version does not exist");
                        }
                    } catch (Exception e) {
                        /*
                             * We might get InsufficientOperationalNodes
                             * exception due to a timeout, thus creating
                             * confusion in the root cause. Hence explicitly
                             * check for timeout.
                             */
                        if (System.currentTimeMillis() >= (this.requestObject.getRequestOriginTimeInMs() + this.requestObject.getRoutingTimeoutInMs())) {
                            RestErrorHandler.writeErrorResponse(this.messageEvent, REQUEST_TIMEOUT, "DELETE request timed out: " + e.getMessage());
                        } else {
                            deleteErrorHandler.handleExceptions(messageEvent, e);
                        }
                    }
                    break;
                default:
                    System.err.println("Illegal operation.");
                    return;
            }
        }
    }
}
Also used : PutResponseSender(voldemort.rest.PutResponseSender) GetResponseSender(voldemort.rest.GetResponseSender) Versioned(voldemort.versioning.Versioned) DeleteResponseSender(voldemort.rest.DeleteResponseSender) VectorClock(voldemort.versioning.VectorClock) GetAllResponseSender(voldemort.rest.GetAllResponseSender) StoreDefinition(voldemort.store.StoreDefinition) ByteArray(voldemort.utils.ByteArray) ArrayList(java.util.ArrayList) List(java.util.List) GetMetadataResponseSender(voldemort.rest.GetMetadataResponseSender)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 DeleteResponseSender (voldemort.rest.DeleteResponseSender)1 GetAllResponseSender (voldemort.rest.GetAllResponseSender)1 GetMetadataResponseSender (voldemort.rest.GetMetadataResponseSender)1 GetResponseSender (voldemort.rest.GetResponseSender)1 PutResponseSender (voldemort.rest.PutResponseSender)1 StoreDefinition (voldemort.store.StoreDefinition)1 ByteArray (voldemort.utils.ByteArray)1 VectorClock (voldemort.versioning.VectorClock)1 Versioned (voldemort.versioning.Versioned)1