Search in sources :

Example 1 with GetResponseSender

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

the class StorageWorkerThread method run.

@Override
public void run() {
    Object message = messageEvent.getMessage();
    boolean fromLocalZone = false;
    if (message instanceof VoldemortStoreRequest) {
        VoldemortStoreRequest storeRequestObject = (VoldemortStoreRequest) message;
        this.requestObject = storeRequestObject.getRequestObject();
        long now = System.currentTimeMillis();
        if (requestObject.getRequestOriginTimeInMs() + requestObject.getRoutingTimeoutInMs() <= now) {
            RestErrorHandler.writeErrorResponse(messageEvent, HttpResponseStatus.REQUEST_TIMEOUT, "current time: " + now + "\torigin time: " + requestObject.getRequestOriginTimeInMs() + "\ttimeout in ms: " + requestObject.getRoutingTimeoutInMs());
            return;
        } else {
            fromLocalZone = isLocalZoneId(storeRequestObject.getZoneId());
            this.store = storeRequestObject.getStore();
            String storeName = store.getName();
            performanceStats = storeStatsMap.get(storeName);
            if (performanceStats == null) {
                // Add to concurrentHashMap
                storeStatsMap.putIfAbsent(storeName, new StoreStats(storeName, aggregatedStoreStats));
                performanceStats = storeStatsMap.get(storeName);
                // Register MBeans for new store performance stats
                if (isJmxEnabled) {
                    JmxUtils.registerMbean(new StoreStatsJmx(performanceStats), JmxUtils.createObjectName(JmxUtils.getPackageName(this.getClass()) + ".store.stats", storeName));
                }
            }
            switch(requestObject.getOperationType()) {
                case VoldemortOpCode.GET_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("Incoming get request");
                    }
                    try {
                        List<Versioned<byte[]>> versionedValues = store.get(requestObject.getKey(), null);
                        // handle non existing key
                        if (versionedValues.size() > 0) {
                            GetResponseSender responseConstructor = new GetResponseSender(messageEvent, requestObject.getKey(), versionedValues, storeName);
                            responseConstructor.sendResponse(performanceStats, fromLocalZone, requestObject.getRequestOriginTimeInMs());
                        } else {
                            logger.error("Error when doing get. Key does not exist");
                            RestErrorHandler.writeErrorResponse(messageEvent, NOT_FOUND, "Key does not exist");
                        }
                    } catch (Exception e) {
                        getErrorHandler.handleExceptions(messageEvent, e);
                    }
                    break;
                case VoldemortOpCode.GET_ALL_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("Incoming get all request");
                    }
                    try {
                        Map<ByteArray, List<Versioned<byte[]>>> keyValuesMap = store.getAll(requestObject.getIterableKeys(), null);
                        // check if there is at least one valid key
                        // before sending response
                        boolean hasAtleastOneValidKey = false;
                        for (List<Versioned<byte[]>> values : keyValuesMap.values()) {
                            if (values.size() > 0) {
                                hasAtleastOneValidKey = true;
                                break;
                            }
                        }
                        if (hasAtleastOneValidKey) {
                            GetAllResponseSender responseConstructor = new GetAllResponseSender(messageEvent, keyValuesMap, storeName);
                            responseConstructor.sendResponse(performanceStats, fromLocalZone, requestObject.getRequestOriginTimeInMs());
                        } else {
                            logger.error("Error when doing getall. Key does not exist or key is null");
                            RestErrorHandler.writeErrorResponse(messageEvent, NOT_FOUND, "Key does not exist or key is null");
                        }
                    } catch (Exception e) {
                        getErrorHandler.handleExceptions(messageEvent, e);
                    }
                    break;
                case VoldemortOpCode.PUT_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("Incoming put request");
                    }
                    try {
                        store.put(requestObject.getKey(), requestObject.getValue(), null);
                        PutResponseSender responseConstructor = new PutResponseSender(messageEvent);
                        responseConstructor.sendResponse(performanceStats, fromLocalZone, requestObject.getRequestOriginTimeInMs());
                    } catch (Exception e) {
                        putErrorHandler.handleExceptions(messageEvent, e);
                    }
                    break;
                case VoldemortOpCode.DELETE_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("Incoming delete request");
                    }
                    try {
                        boolean result = store.delete(requestObject.getKey(), requestObject.getVersion());
                        if (!result) {
                            logger.error("Error when doing delete. Non Existing key/version. Nothing to delete");
                            RestErrorHandler.writeErrorResponse(messageEvent, NOT_FOUND, "Non Existing key/version. Nothing to delete");
                            break;
                        }
                        DeleteResponseSender responseConstructor = new DeleteResponseSender(messageEvent);
                        responseConstructor.sendResponse(performanceStats, fromLocalZone, requestObject.getRequestOriginTimeInMs());
                    } catch (Exception e) {
                        deleteErrorHandler.handleExceptions(messageEvent, e);
                    }
                    break;
                case VoldemortOpCode.GET_VERSION_OP_CODE:
                    if (logger.isDebugEnabled()) {
                        logger.debug("Incoming get version request");
                    }
                    try {
                        List<Version> versions = store.getVersions(requestObject.getKey());
                        // handle non existing key
                        if (versions.size() > 0) {
                            GetVersionResponseSender responseConstructor = new GetVersionResponseSender(messageEvent, requestObject.getKey(), versions, storeName);
                            responseConstructor.sendResponse(performanceStats, fromLocalZone, requestObject.getRequestOriginTimeInMs());
                        } else {
                            logger.error("Error when doing getversion. Key does not exist or key is null");
                            RestErrorHandler.writeErrorResponse(messageEvent, NOT_FOUND, "Key does not exist or key is null");
                        }
                    } catch (Exception e) {
                        getVersionErrorHandler.handleExceptions(messageEvent, e);
                    }
                    break;
                default:
                    // above, the code stops here.
                    return;
            }
        }
    }
}
Also used : PutResponseSender(voldemort.rest.PutResponseSender) StoreStats(voldemort.store.stats.StoreStats) GetResponseSender(voldemort.rest.GetResponseSender) Versioned(voldemort.versioning.Versioned) DeleteResponseSender(voldemort.rest.DeleteResponseSender) GetAllResponseSender(voldemort.rest.GetAllResponseSender) GetVersionResponseSender(voldemort.rest.GetVersionResponseSender) Version(voldemort.versioning.Version) ByteArray(voldemort.utils.ByteArray) List(java.util.List) StoreStatsJmx(voldemort.store.stats.StoreStatsJmx)

Example 2 with GetResponseSender

use of voldemort.rest.GetResponseSender 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

List (java.util.List)2 DeleteResponseSender (voldemort.rest.DeleteResponseSender)2 GetAllResponseSender (voldemort.rest.GetAllResponseSender)2 GetResponseSender (voldemort.rest.GetResponseSender)2 PutResponseSender (voldemort.rest.PutResponseSender)2 ByteArray (voldemort.utils.ByteArray)2 Versioned (voldemort.versioning.Versioned)2 ArrayList (java.util.ArrayList)1 GetMetadataResponseSender (voldemort.rest.GetMetadataResponseSender)1 GetVersionResponseSender (voldemort.rest.GetVersionResponseSender)1 StoreDefinition (voldemort.store.StoreDefinition)1 StoreStats (voldemort.store.stats.StoreStats)1 StoreStatsJmx (voldemort.store.stats.StoreStatsJmx)1 VectorClock (voldemort.versioning.VectorClock)1 Version (voldemort.versioning.Version)1