Search in sources :

Example 6 with HttpMethod

use of org.jboss.netty.handler.codec.http.HttpMethod in project opentsdb by OpenTSDB.

the class UniqueIdRpc method handleTSMeta.

/**
 * Handles CRUD calls to individual TSMeta data entries
 * @param tsdb The TSDB from the RPC router
 * @param query The query for this request
 */
private void handleTSMeta(final TSDB tsdb, final HttpQuery query) {
    final HttpMethod method = query.getAPIMethod();
    // GET
    if (method == HttpMethod.GET) {
        String tsuid = null;
        if (query.hasQueryStringParam("tsuid")) {
            tsuid = query.getQueryStringParam("tsuid");
            try {
                final TSMeta meta = TSMeta.getTSMeta(tsdb, tsuid).joinUninterruptibly();
                if (meta != null) {
                    query.sendReply(query.serializer().formatTSMetaV1(meta));
                } else {
                    throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Could not find Timeseries meta data");
                }
            } catch (NoSuchUniqueName e) {
                // the timeseries meta data
                throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to find one of the UIDs", e);
            } catch (BadRequestException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            String mquery = query.getRequiredQueryStringParam("m");
            // m is of the following forms:
            // metric[{tag=value,...}]
            // where the parts in square brackets `[' .. `]' are optional.
            final HashMap<String, String> tags = new HashMap<String, String>();
            String metric = null;
            try {
                metric = Tags.parseWithMetric(mquery, tags);
            } catch (IllegalArgumentException e) {
                throw new BadRequestException(e);
            }
            final TSUIDQuery tsuid_query = new TSUIDQuery(tsdb, metric, tags);
            try {
                final List<TSMeta> tsmetas = tsuid_query.getTSMetas().joinUninterruptibly();
                query.sendReply(query.serializer().formatTSMetaListV1(tsmetas));
            } catch (NoSuchUniqueName e) {
                throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to find one of the UIDs", e);
            } catch (BadRequestException e) {
                throw e;
            } catch (RuntimeException e) {
                throw new BadRequestException(e);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    // POST / PUT
    } else if (method == HttpMethod.POST || method == HttpMethod.PUT) {
        final TSMeta meta;
        if (query.hasContent()) {
            meta = query.serializer().parseTSMetaV1();
        } else {
            meta = this.parseTSMetaQS(query);
        }
        /**
         * Storage callback used to determine if the storage call was successful
         * or not. Also returns the updated object from storage.
         */
        class SyncCB implements Callback<Deferred<TSMeta>, Boolean> {

            @Override
            public Deferred<TSMeta> call(Boolean success) throws Exception {
                if (!success) {
                    throw new BadRequestException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Failed to save the TSMeta to storage", "This may be caused by another process modifying storage data");
                }
                return TSMeta.getTSMeta(tsdb, meta.getTSUID());
            }
        }
        if (meta.getTSUID() == null || meta.getTSUID().isEmpty()) {
            // we got a JSON object without TSUID. Try to find a timeseries spec of
            // the form "m": "metric{tagk=tagv,...}"
            final String metric = query.getRequiredQueryStringParam("m");
            final boolean create = query.getQueryStringParam("create") != null && query.getQueryStringParam("create").equals("true");
            final String tsuid = getTSUIDForMetric(metric, tsdb);
            class WriteCounterIfNotPresentCB implements Callback<Boolean, Boolean> {

                @Override
                public Boolean call(Boolean exists) throws Exception {
                    if (!exists && create) {
                        final PutRequest put = new PutRequest(tsdb.metaTable(), UniqueId.stringToUid(tsuid), TSMeta.FAMILY(), TSMeta.COUNTER_QUALIFIER(), Bytes.fromLong(0));
                        tsdb.getClient().put(put);
                    }
                    return exists;
                }
            }
            try {
                // Check whether we have a TSMeta stored already
                final boolean exists = TSMeta.metaExistsInStorage(tsdb, tsuid).joinUninterruptibly();
                // set TSUID
                meta.setTSUID(tsuid);
                if (!exists && create) {
                    // Write 0 to counter column if not present
                    TSMeta.counterExistsInStorage(tsdb, UniqueId.stringToUid(tsuid)).addCallback(new WriteCounterIfNotPresentCB()).joinUninterruptibly();
                    // set TSUID
                    final Deferred<TSMeta> process_meta = meta.storeNew(tsdb).addCallbackDeferring(new SyncCB());
                    final TSMeta updated_meta = process_meta.joinUninterruptibly();
                    tsdb.indexTSMeta(updated_meta);
                    tsdb.processTSMetaThroughTrees(updated_meta);
                    query.sendReply(query.serializer().formatTSMetaV1(updated_meta));
                } else if (exists) {
                    final Deferred<TSMeta> process_meta = meta.syncToStorage(tsdb, method == HttpMethod.PUT).addCallbackDeferring(new SyncCB());
                    final TSMeta updated_meta = process_meta.joinUninterruptibly();
                    tsdb.indexTSMeta(updated_meta);
                    query.sendReply(query.serializer().formatTSMetaV1(updated_meta));
                } else {
                    throw new BadRequestException("Could not find TSMeta, specify \"create=true\" to create a new one.");
                }
            } catch (IllegalStateException e) {
                query.sendStatusOnly(HttpResponseStatus.NOT_MODIFIED);
            } catch (IllegalArgumentException e) {
                throw new BadRequestException(e);
            } catch (BadRequestException e) {
                throw e;
            } catch (NoSuchUniqueName e) {
                // the timeseries meta data
                throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to find one or more UIDs", e);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            try {
                final Deferred<TSMeta> process_meta = meta.syncToStorage(tsdb, method == HttpMethod.PUT).addCallbackDeferring(new SyncCB());
                final TSMeta updated_meta = process_meta.joinUninterruptibly();
                tsdb.indexTSMeta(updated_meta);
                query.sendReply(query.serializer().formatTSMetaV1(updated_meta));
            } catch (IllegalStateException e) {
                query.sendStatusOnly(HttpResponseStatus.NOT_MODIFIED);
            } catch (IllegalArgumentException e) {
                throw new BadRequestException(e);
            } catch (NoSuchUniqueName e) {
                // the timeseries meta data
                throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to find one or more UIDs", e);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    // DELETE
    } else if (method == HttpMethod.DELETE) {
        final TSMeta meta;
        if (query.hasContent()) {
            meta = query.serializer().parseTSMetaV1();
        } else {
            meta = this.parseTSMetaQS(query);
        }
        try {
            meta.delete(tsdb);
            tsdb.deleteTSMeta(meta.getTSUID());
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("Unable to delete TSMeta information", e);
        }
        query.sendStatusOnly(HttpResponseStatus.NO_CONTENT);
    } else {
        throw new BadRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED, "Method not allowed", "The HTTP method [" + method.getName() + "] is not permitted for this endpoint");
    }
}
Also used : HashMap(java.util.HashMap) Deferred(com.stumbleupon.async.Deferred) TSMeta(net.opentsdb.meta.TSMeta) PutRequest(org.hbase.async.PutRequest) IOException(java.io.IOException) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod) TSUIDQuery(net.opentsdb.meta.TSUIDQuery) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Example 7 with HttpMethod

use of org.jboss.netty.handler.codec.http.HttpMethod in project opentsdb by OpenTSDB.

the class UniqueIdRpc method handleUIDMeta.

/**
 * Handles CRUD calls to individual UIDMeta data entries
 * @param tsdb The TSDB from the RPC router
 * @param query The query for this request
 */
private void handleUIDMeta(final TSDB tsdb, final HttpQuery query) {
    final HttpMethod method = query.getAPIMethod();
    // GET
    if (method == HttpMethod.GET) {
        final String uid = query.getRequiredQueryStringParam("uid");
        final UniqueIdType type = UniqueId.stringToUniqueIdType(query.getRequiredQueryStringParam("type"));
        try {
            final UIDMeta meta = UIDMeta.getUIDMeta(tsdb, type, uid).joinUninterruptibly();
            query.sendReply(query.serializer().formatUidMetaV1(meta));
        } catch (NoSuchUniqueId e) {
            throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Could not find the requested UID", e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    // POST
    } else if (method == HttpMethod.POST || method == HttpMethod.PUT) {
        final UIDMeta meta;
        if (query.hasContent()) {
            meta = query.serializer().parseUidMetaV1();
        } else {
            meta = this.parseUIDMetaQS(query);
        }
        /**
         * Storage callback used to determine if the storage call was successful
         * or not. Also returns the updated object from storage.
         */
        class SyncCB implements Callback<Deferred<UIDMeta>, Boolean> {

            @Override
            public Deferred<UIDMeta> call(Boolean success) throws Exception {
                if (!success) {
                    throw new BadRequestException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Failed to save the UIDMeta to storage", "This may be caused by another process modifying storage data");
                }
                return UIDMeta.getUIDMeta(tsdb, meta.getType(), meta.getUID());
            }
        }
        try {
            final Deferred<UIDMeta> process_meta = meta.syncToStorage(tsdb, method == HttpMethod.PUT).addCallbackDeferring(new SyncCB());
            final UIDMeta updated_meta = process_meta.joinUninterruptibly();
            tsdb.indexUIDMeta(updated_meta);
            query.sendReply(query.serializer().formatUidMetaV1(updated_meta));
        } catch (IllegalStateException e) {
            query.sendStatusOnly(HttpResponseStatus.NOT_MODIFIED);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException(e);
        } catch (NoSuchUniqueId e) {
            throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Could not find the requested UID", e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    // DELETE
    } else if (method == HttpMethod.DELETE) {
        final UIDMeta meta;
        if (query.hasContent()) {
            meta = query.serializer().parseUidMetaV1();
        } else {
            meta = this.parseUIDMetaQS(query);
        }
        try {
            meta.delete(tsdb).joinUninterruptibly();
            tsdb.deleteUIDMeta(meta);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("Unable to delete UIDMeta information", e);
        } catch (NoSuchUniqueId e) {
            throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Could not find the requested UID", e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        query.sendStatusOnly(HttpResponseStatus.NO_CONTENT);
    } else {
        throw new BadRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED, "Method not allowed", "The HTTP method [" + method.getName() + "] is not permitted for this endpoint");
    }
}
Also used : Deferred(com.stumbleupon.async.Deferred) UniqueIdType(net.opentsdb.uid.UniqueId.UniqueIdType) IOException(java.io.IOException) UIDMeta(net.opentsdb.meta.UIDMeta) NoSuchUniqueId(net.opentsdb.uid.NoSuchUniqueId) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod)

Example 8 with HttpMethod

use of org.jboss.netty.handler.codec.http.HttpMethod in project opentsdb by OpenTSDB.

the class SearchRpc method execute.

/**
 * Handles the /api/search/&lt;type&gt; endpoint
 * @param tsdb The TSDB to which we belong
 * @param query The HTTP query to work with
 */
@Override
public void execute(TSDB tsdb, HttpQuery query) {
    final HttpMethod method = query.getAPIMethod();
    if (method != HttpMethod.GET && method != HttpMethod.POST) {
        throw new BadRequestException("Unsupported method: " + method.getName());
    }
    // the uri will be /api/vX/search/<type> or /api/search/<type>
    final String[] uri = query.explodeAPIPath();
    final String endpoint = uri.length > 1 ? uri[1] : "";
    final SearchType type;
    final SearchQuery search_query;
    try {
        type = SearchQuery.parseSearchType(endpoint);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Invalid search query type supplied", e);
    }
    if (query.hasContent()) {
        search_query = query.serializer().parseSearchQueryV1();
    } else {
        search_query = parseQueryString(query, type);
    }
    search_query.setType(type);
    if (type == SearchType.LOOKUP) {
        processLookup(tsdb, query, search_query);
        return;
    }
    try {
        final SearchQuery results = tsdb.executeSearch(search_query).joinUninterruptibly();
        query.sendReply(query.serializer().formatSearchResultsV1(results));
    } catch (IllegalStateException e) {
        throw new BadRequestException("Searching is not enabled", e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SearchQuery(net.opentsdb.search.SearchQuery) SearchType(net.opentsdb.search.SearchQuery.SearchType) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod) DeferredGroupException(com.stumbleupon.async.DeferredGroupException)

Example 9 with HttpMethod

use of org.jboss.netty.handler.codec.http.HttpMethod in project opentsdb by OpenTSDB.

the class AnnotationRpc method execute.

/**
 * Performs CRUD methods on individual annotation objects.
 * @param tsdb The TSD to which we belong
 * @param query The query to parse and respond to
 */
public void execute(final TSDB tsdb, HttpQuery query) throws IOException {
    final HttpMethod method = query.getAPIMethod();
    final String[] uri = query.explodeAPIPath();
    final String endpoint = uri.length > 1 ? uri[1] : "";
    if (endpoint != null && endpoint.toLowerCase().endsWith("bulk")) {
        executeBulk(tsdb, method, query);
        return;
    }
    final Annotation note;
    if (query.hasContent()) {
        note = query.serializer().parseAnnotationV1();
    } else {
        note = parseQS(query);
    }
    // GET
    if (method == HttpMethod.GET) {
        try {
            if ("annotations".toLowerCase().equals(uri[0])) {
                fetchMultipleAnnotations(tsdb, note, query);
            } else {
                fetchSingleAnnotation(tsdb, note, query);
            }
        } catch (BadRequestException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    // POST
    } else if (method == HttpMethod.POST || method == HttpMethod.PUT) {
        /**
         * Storage callback used to determine if the storage call was successful
         * or not. Also returns the updated object from storage.
         */
        class SyncCB implements Callback<Deferred<Annotation>, Boolean> {

            @Override
            public Deferred<Annotation> call(Boolean success) throws Exception {
                if (!success) {
                    throw new BadRequestException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Failed to save the Annotation to storage", "This may be caused by another process modifying storage data");
                }
                return Annotation.getAnnotation(tsdb, note.getTSUID(), note.getStartTime());
            }
        }
        try {
            final Deferred<Annotation> process_meta = note.syncToStorage(tsdb, method == HttpMethod.PUT).addCallbackDeferring(new SyncCB());
            final Annotation updated_meta = process_meta.joinUninterruptibly();
            tsdb.indexAnnotation(note);
            query.sendReply(query.serializer().formatAnnotationV1(updated_meta));
        } catch (IllegalStateException e) {
            query.sendStatusOnly(HttpResponseStatus.NOT_MODIFIED);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    // DELETE
    } else if (method == HttpMethod.DELETE) {
        try {
            note.delete(tsdb).joinUninterruptibly();
            tsdb.deleteAnnotation(note);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("Unable to delete Annotation information", e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        query.sendStatusOnly(HttpResponseStatus.NO_CONTENT);
    } else {
        throw new BadRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED, "Method not allowed", "The HTTP method [" + method.getName() + "] is not permitted for this endpoint");
    }
}
Also used : Deferred(com.stumbleupon.async.Deferred) Annotation(net.opentsdb.meta.Annotation) IOException(java.io.IOException) JSONException(net.opentsdb.utils.JSONException) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod)

Example 10 with HttpMethod

use of org.jboss.netty.handler.codec.http.HttpMethod in project voldemort by voldemort.

the class CoordinatorAdminRequestHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
    if (!readingChunks) {
        HttpRequest request = this.request = (HttpRequest) messageEvent.getMessage();
        String requestURI = this.request.getUri();
        if (logger.isDebugEnabled()) {
            logger.debug("Admin Request URI: " + requestURI);
        }
        if (request.isChunked()) {
            readingChunks = true;
        } else {
            String[] requestUriSegments = requestURI.split("/");
            HttpResponse response;
            if (requestUriSegments.length > 0 && requestUriSegments[1].equals(STORE_OPS_NAMESPACE)) {
                List<String> storeList = Lists.newArrayList();
                if (requestUriSegments.length > 2) {
                    String csvStoreList = requestUriSegments[2];
                    String[] storeArray = csvStoreList.split(",");
                    storeList = Lists.newArrayList(storeArray);
                }
                HttpMethod httpMethod = request.getMethod();
                if (httpMethod.equals(HttpMethod.GET)) {
                    response = handleGet(storeList);
                } else if (httpMethod.equals(HttpMethod.POST)) {
                    Map<String, Properties> configsToPut = Maps.newHashMap();
                    ChannelBuffer content = this.request.getContent();
                    if (content != null) {
                        byte[] postBody = new byte[content.capacity()];
                        content.readBytes(postBody);
                        configsToPut = ClientConfigUtil.readMultipleClientConfigAvro(new String(postBody));
                    }
                    response = handlePut(configsToPut);
                } else if (httpMethod.equals(HttpMethod.DELETE)) {
                    if (storeList.size() == 0) {
                        response = handleBadRequest("Cannot delete config for all stores. Please specify at least one store name.");
                    } else {
                        response = handleDelete(storeList);
                    }
                } else {
                    // Bad HTTP method
                    response = handleBadRequest("Unsupported HTTP method. Only GET, POST and DELETE are supported.");
                }
            } else {
                // Bad namespace
                response = handleBadRequest("Unsupported namespace. Only /" + STORE_OPS_NAMESPACE + "/ is supported.");
            }
            messageEvent.getChannel().write(response);
        }
    } else {
        HttpChunk chunk = (HttpChunk) messageEvent.getMessage();
        if (chunk.isLast()) {
            readingChunks = false;
        }
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Map(java.util.Map) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Aggregations

HttpMethod (org.jboss.netty.handler.codec.http.HttpMethod)11 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)4 Deferred (com.stumbleupon.async.Deferred)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)3 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)3 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)3 ChannelFuture (org.jboss.netty.channel.ChannelFuture)2 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)2 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)2 AuditLogContent (co.cask.cdap.common.logging.AuditLogContent)1 AccessTokenTransformer (co.cask.cdap.security.auth.AccessTokenTransformer)1 TokenState (co.cask.cdap.security.auth.TokenState)1 IRequestHandler (com.feeyo.net.http.handler.IRequestHandler)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1