Search in sources :

Example 1 with GetVersionResponseSender

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

Aggregations

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