Search in sources :

Example 6 with Response

use of com.xiaomi.linden.thrift.common.Response in project linden by XiaoMi.

the class CoreLindenServiceImpl method delete.

@Override
public Future<Response> delete(final LindenDeleteRequest request) {
    final Stopwatch sw = Stopwatch.createStarted();
    return instanceExecutorPool.apply(new Function0<Response>() {

        @Override
        public Response apply() {
            Response response = null;
            try {
                long eps = sw.elapsed(TimeUnit.MILLISECONDS);
                if (eps > 10) {
                    LOGGER.warn("Warning: instanceExecutorPool took " + eps + "ms to start delete.");
                    if (eps > instanceFuturePoolWaitTimeout) {
                        response = ResponseUtils.buildFailedResponse("Waiting time is too long, " + eps + "ms in instance future pool");
                        return response;
                    }
                }
                response = lindenCore.delete(request);
            } catch (Exception e) {
                String errorStackInfo = Throwables.getStackTraceAsString(e);
                response = ResponseUtils.buildFailedResponse(errorStackInfo);
            } finally {
                if (response.isSuccess()) {
                    LOGGER.info("Instance delete request succeeded, request: {}, cost: {} ms.", request, sw.elapsed(TimeUnit.MILLISECONDS));
                } else {
                    LOGGER.error("Instance delete request failed, request: {}, cost: {} ms.", request, sw.elapsed(TimeUnit.MILLISECONDS));
                }
                return response;
            }
        }
    });
}
Also used : Response(com.xiaomi.linden.thrift.common.Response) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException)

Example 7 with Response

use of com.xiaomi.linden.thrift.common.Response in project linden by XiaoMi.

the class LindenController method delete.

@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public String delete(@RequestParam("bql") String bql) {
    Response response;
    try {
        Future<Response> future = LindenAdmin.getService().handleClusterDeleteRequest(bql);
        response = Await.result(future, Duration.apply(30000, TimeUnit.MILLISECONDS));
    } catch (Exception e) {
        response = new Response();
        response.setSuccess(false).setError(Throwables.getStackTraceAsString(e));
    }
    return ThriftToJSON(response);
}
Also used : Response(com.xiaomi.linden.thrift.common.Response) TException(org.apache.thrift.TException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with Response

use of com.xiaomi.linden.thrift.common.Response in project linden by XiaoMi.

the class CoreLindenServiceImpl method index.

@Override
public Future<Response> index(final String content) {
    final Stopwatch sw = Stopwatch.createStarted();
    return instanceExecutorPool.apply(new Function0<Response>() {

        @Override
        public Response apply() {
            Response response = null;
            try {
                long eps = sw.elapsed(TimeUnit.MILLISECONDS);
                if (eps > 10) {
                    LOGGER.warn("Warning: instanceExecutorPool took " + eps + "ms to start index.");
                    if (eps > instanceFuturePoolWaitTimeout) {
                        response = ResponseUtils.buildFailedResponse("Waiting time is too long, " + eps + "ms in instance future pool");
                        return response;
                    }
                }
                LindenIndexRequest indexRequest = LindenIndexRequestParser.parse(config.getSchema(), content);
                response = lindenCore.index(indexRequest);
            } catch (Exception e) {
                String errorStackInfo = Throwables.getStackTraceAsString(e);
                response = ResponseUtils.buildFailedResponse(errorStackInfo);
            } finally {
                if (response.isSuccess()) {
                    LOGGER.info("Instance index request succeeded, content: {}, cost: {} ms.", content, sw.elapsed(TimeUnit.MILLISECONDS));
                } else {
                    LOGGER.error("Instance index request failed, content: {}, cost: {} ms.", content, sw.elapsed(TimeUnit.MILLISECONDS));
                }
                return response;
            }
        }
    });
}
Also used : Response(com.xiaomi.linden.thrift.common.Response) LindenIndexRequest(com.xiaomi.linden.thrift.common.LindenIndexRequest) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException)

Example 9 with Response

use of com.xiaomi.linden.thrift.common.Response in project linden by XiaoMi.

the class MultiLindenCoreImpl method delete.

@Override
public Response delete(LindenDeleteRequest request) throws IOException {
    List<String> indexNames;
    // Only INDEX_NAME division type supports specified index name request
    if (multiIndexStrategy instanceof TimeLimitMultiIndexStrategy || multiIndexStrategy instanceof DocNumLimitMultiIndexStrategy) {
        indexNames = new ArrayList<>(lindenCoreMap.keySet());
    } else {
        if (request.getIndexNames() == null || (request.getIndexNamesSize() == 1 && request.getIndexNames().get(0).equals(LINDEN))) {
            indexNames = new ArrayList<>(lindenCoreMap.keySet());
        } else {
            indexNames = request.getIndexNames();
            for (int i = 0; i < indexNames.size(); ++i) {
                indexNames.set(i, MultiIndexStrategy.MULTI_INDEX_PREFIX_NAME + indexNames.get(i));
            }
        }
    }
    StringBuilder errorInfo = new StringBuilder();
    for (final String indexName : indexNames) {
        final LindenCore core = lindenCoreMap.get(indexName);
        if (core != null) {
            try {
                Response response = core.delete(request);
                if (!response.isSuccess()) {
                    errorInfo.append(indexName + ":" + response.getError() + ";");
                    LOGGER.error("Multi-index {} delete error: {}", indexName, response.error);
                }
            } catch (Exception e) {
                errorInfo.append(indexName + ":" + Throwables.getStackTraceAsString(e) + ";");
                LOGGER.error("Multi-index {} delete error: {}", indexName, Throwables.getStackTraceAsString(e));
            }
        } else {
            errorInfo.append(indexName + " doesn't exist");
            LOGGER.error("Multi-index {} delete error: " + indexName + " doesn't exist");
        }
    }
    if (errorInfo.length() > 0) {
        return ResponseUtils.buildFailedResponse("Multi-index delete error: " + errorInfo.toString());
    } else {
        return ResponseUtils.SUCCESS;
    }
}
Also used : Response(com.xiaomi.linden.thrift.common.Response) DocNumLimitMultiIndexStrategy(com.xiaomi.linden.core.indexing.DocNumLimitMultiIndexStrategy) TimeLimitMultiIndexStrategy(com.xiaomi.linden.core.indexing.TimeLimitMultiIndexStrategy) IOException(java.io.IOException)

Example 10 with Response

use of com.xiaomi.linden.thrift.common.Response in project linden by XiaoMi.

the class MultiLindenCoreImpl method index.

@Override
public Response index(LindenIndexRequest request) throws IOException {
    if (request.getType().equals(IndexRequestType.DELETE_INDEX)) {
        return deleteIndex(request);
    }
    LindenCore core = multiIndexStrategy.getCurrentLindenCore(request.getIndexName());
    if (core != null) {
        switch(request.getType()) {
            case INDEX:
                return core.index(request);
            case DELETE:
                return delete(request);
            case REPLACE:
                request.setType(IndexRequestType.DELETE);
                Response response = delete(request);
                if (response.isSuccess()) {
                    request.setType(IndexRequestType.INDEX);
                    response = core.index(request);
                }
                return response;
            default:
                return ResponseUtils.buildFailedResponse("IndexRequestType " + request.getType() + " is not supported.");
        }
    } else {
        return ResponseUtils.buildFailedResponse("No corresponding linden core is found.");
    }
}
Also used : Response(com.xiaomi.linden.thrift.common.Response)

Aggregations

Response (com.xiaomi.linden.thrift.common.Response)18 IOException (java.io.IOException)14 Stopwatch (com.google.common.base.Stopwatch)8 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Future (com.twitter.util.Future)3 FutureTransformer (com.twitter.util.FutureTransformer)3 LindenRequest (com.xiaomi.linden.thrift.common.LindenRequest)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 LindenIndexRequest (com.xiaomi.linden.thrift.common.LindenIndexRequest)2 LindenResult (com.xiaomi.linden.thrift.common.LindenResult)2 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)2 TException (org.apache.thrift.TException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 JSONObject (com.alibaba.fastjson.JSONObject)1 DocNumLimitMultiIndexStrategy (com.xiaomi.linden.core.indexing.DocNumLimitMultiIndexStrategy)1 TimeLimitMultiIndexStrategy (com.xiaomi.linden.core.indexing.TimeLimitMultiIndexStrategy)1