Exemplo n.º 1
0
  public GetQueryResultResponse getResultResponse(QueryId queryId) throws ServiceException {
    if (queryId.equals(QueryIdFactory.NULL_QUERY_ID)) {
      return null;
    }

    NettyClientBase client = null;
    try {
      InetSocketAddress queryMasterAddr = queryMasterMap.get(queryId);
      if (queryMasterAddr == null) {
        LOG.warn("No Connection to QueryMaster for " + queryId);
        return null;
      }
      client = connPool.getConnection(queryMasterAddr, QueryMasterClientProtocol.class, false);
      QueryMasterClientProtocolService.BlockingInterface queryMasterService = client.getStub();
      GetQueryResultRequest.Builder builder = GetQueryResultRequest.newBuilder();
      builder.setQueryId(queryId.getProto());
      GetQueryResultResponse response = queryMasterService.getQueryResult(null, builder.build());

      return response;
    } catch (Exception e) {
      throw new ServiceException(e.getMessage(), e);
    } finally {
      connPool.releaseConnection(client);
    }
  }
Exemplo n.º 2
0
    @Override
    public Response run(JerseyResourceDelegateContext context) {
      JerseyResourceDelegateContextKey<String> sessionIdKey =
          JerseyResourceDelegateContextKey.valueOf(sessionIdKeyName, String.class);
      String sessionId = context.get(sessionIdKey);
      JerseyResourceDelegateContextKey<SubmitQueryRequest> submitQueryRequestKey =
          JerseyResourceDelegateContextKey.valueOf(
              submitQueryRequestKeyName, SubmitQueryRequest.class);
      SubmitQueryRequest request = context.get(submitQueryRequestKey);
      JerseyResourceDelegateContextKey<MasterContext> masterContextKey =
          JerseyResourceDelegateContextKey.valueOf(
              JerseyResourceDelegateUtil.MasterContextKey, MasterContext.class);
      MasterContext masterContext = context.get(masterContextKey);

      if (sessionId == null || sessionId.isEmpty()) {
        return ResourcesUtil.createBadRequestResponse(LOG, "Session Id is null or empty string.");
      }
      if (request == null || request.getQuery() == null || request.getQuery().isEmpty()) {
        return ResourcesUtil.createBadRequestResponse(LOG, "query is null or emptry string.");
      }

      Session session;
      try {
        session = masterContext.getSessionManager().getSession(sessionId);
      } catch (InvalidSessionException e) {
        return ResourcesUtil.createBadRequestResponse(
            LOG, "Provided session id (" + sessionId + ") is invalid.");
      }

      SubmitQueryResponse response =
          masterContext.getGlobalEngine().executeQuery(session, request.getQuery(), false);
      if (ReturnStateUtil.isError(response.getState())) {
        return ResourcesUtil.createExceptionResponse(LOG, response.getState().getMessage());
      } else {
        JerseyResourceDelegateContextKey<UriInfo> uriInfoKey =
            JerseyResourceDelegateContextKey.valueOf(
                JerseyResourceDelegateUtil.UriInfoKey, UriInfo.class);
        UriInfo uriInfo = context.get(uriInfoKey);

        QueryId queryId = new QueryId(response.getQueryId());
        URI queryURI =
            uriInfo
                .getBaseUriBuilder()
                .path(QueryResource.class)
                .path(QueryResource.class, "getQuery")
                .build(queryId.toString());

        GetSubmitQueryResponse queryResponse = new GetSubmitQueryResponse();
        if (queryId.isNull() == false) {
          queryResponse.setUri(queryURI);
        }

        queryResponse.setResultCode(response.getState().getReturnCode());
        queryResponse.setQuery(request.getQuery());
        return Response.status(Status.OK).entity(queryResponse).build();
      }
    }
Exemplo n.º 3
0
  public void reportQueryStatusToQueryMaster(QueryId queryId, TajoProtos.QueryState state) {
    LOG.info("Send QueryMaster Ready to QueryJobManager:" + queryId);
    NettyClientBase tmClient = null;
    try {
      tmClient =
          connPool.getConnection(
              queryMasterContext.getWorkerContext().getTajoMasterAddress(),
              TajoMasterProtocol.class,
              true);
      TajoMasterProtocol.TajoMasterProtocolService masterClientService = tmClient.getStub();

      TajoHeartbeat.Builder queryHeartbeatBuilder =
          TajoHeartbeat.newBuilder()
              .setTajoWorkerHost(
                  workerContext.getQueryMasterManagerService().getBindAddr().getHostName())
              .setTajoQueryMasterPort(
                  workerContext.getQueryMasterManagerService().getBindAddr().getPort())
              .setTajoWorkerClientPort(
                  workerContext.getTajoWorkerClientService().getBindAddr().getPort())
              .setState(state)
              .setQueryId(queryId.getProto());

      CallFuture<TajoHeartbeatResponse> callBack = new CallFuture<TajoHeartbeatResponse>();

      masterClientService.heartbeat(
          callBack.getController(), queryHeartbeatBuilder.build(), callBack);
    } catch (Exception e) {
      LOG.error(e.getMessage(), e);
    } finally {
      connPool.releaseConnection(tmClient);
    }
  }
Exemplo n.º 4
0
  public boolean killQuery(final QueryId queryId) throws ServiceException, IOException {

    QueryStatus status = getQueryStatus(queryId);

    NettyClientBase tmClient = null;
    try {
      /* send a kill to the TM */
      tmClient = connPool.getConnection(tajoMasterAddr, TajoMasterClientProtocol.class, false);
      TajoMasterClientProtocolService.BlockingInterface tajoMasterService = tmClient.getStub();
      tajoMasterService.killQuery(null, queryId.getProto());

      long currentTimeMillis = System.currentTimeMillis();
      long timeKillIssued = currentTimeMillis;
      while ((currentTimeMillis < timeKillIssued + 10000L)
          && (status.getState() != QueryState.QUERY_KILLED)) {
        try {
          Thread.sleep(100L);
        } catch (InterruptedException ie) {
          break;
        }
        currentTimeMillis = System.currentTimeMillis();
        status = getQueryStatus(queryId);
      }
      return status.getState() == QueryState.QUERY_KILLED;
    } catch (Exception e) {
      LOG.debug("Error when checking for application status", e);
      return false;
    } finally {
      connPool.releaseConnection(tmClient);
    }
  }
Exemplo n.º 5
0
  public ResultSet getQueryResultAndWait(QueryId queryId) throws ServiceException, IOException {
    if (queryId.equals(QueryIdFactory.NULL_QUERY_ID)) {
      return createNullResultSet(queryId);
    }
    QueryStatus status = getQueryStatus(queryId);

    while (status != null && isQueryRunnning(status.getState())) {
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      status = getQueryStatus(queryId);
    }

    if (status.getState() == QueryState.QUERY_SUCCEEDED) {
      if (status.hasResult()) {
        return getQueryResult(queryId);
      } else {
        return createNullResultSet(queryId);
      }

    } else {
      LOG.warn("Query (" + status.getQueryId() + ") failed: " + status.getState());

      // TODO throw SQLException(?)
      return createNullResultSet(queryId);
    }
  }
Exemplo n.º 6
0
 public ResultSet getQueryResult(QueryId queryId) throws ServiceException, IOException {
   if (queryId.equals(QueryIdFactory.NULL_QUERY_ID)) {
     return createNullResultSet(queryId);
   }
   GetQueryResultResponse response = getResultResponse(queryId);
   TableDesc tableDesc = CatalogUtil.newTableDesc(response.getTableDesc());
   conf.setVar(ConfVars.USERNAME, response.getTajoUserName());
   return new TajoResultSet(this, queryId, conf, tableDesc);
 }
Exemplo n.º 7
0
  /**
   * It submits a query statement and get a response. The main difference from {@link
   * #executeQuery(String)} is a blocking method. So, this method is wait for the finish of the
   * submitted query.
   *
   * @return If failed, return null.
   */
  public ResultSet executeQueryAndGetResult(final String sql) throws ServiceException, IOException {
    GetQueryStatusResponse response =
        new ServerCallable<GetQueryStatusResponse>(
            connPool, tajoMasterAddr, TajoMasterClientProtocol.class, false, true) {
          public GetQueryStatusResponse call(NettyClientBase client) throws ServiceException {
            final QueryRequest.Builder builder = QueryRequest.newBuilder();
            builder.setQuery(sql);
            TajoMasterClientProtocolService.BlockingInterface tajoMasterService = client.getStub();
            return tajoMasterService.submitQuery(null, builder.build());
          }
        }.withRetries();

    QueryId queryId = new QueryId(response.getQueryId());
    if (queryId.equals(QueryIdFactory.NULL_QUERY_ID)) {
      return this.createNullResultSet(queryId);
    } else {
      return this.getQueryResultAndWait(queryId);
    }
  }
Exemplo n.º 8
0
  public QueryStatus getQueryStatus(QueryId queryId) throws ServiceException {
    GetQueryStatusRequest.Builder builder = GetQueryStatusRequest.newBuilder();
    builder.setQueryId(queryId.getProto());

    GetQueryStatusResponse res = null;
    if (queryMasterMap.containsKey(queryId)) {
      NettyClientBase qmClient = null;
      try {
        qmClient =
            connPool.getConnection(
                queryMasterMap.get(queryId), QueryMasterClientProtocol.class, false);
        QueryMasterClientProtocolService.BlockingInterface queryMasterService = qmClient.getStub();
        res = queryMasterService.getQueryStatus(null, builder.build());
      } catch (Exception e) {
        throw new ServiceException(e.getMessage(), e);
      } finally {
        connPool.releaseConnection(qmClient);
      }
    } else {
      NettyClientBase tmClient = null;
      try {
        tmClient = connPool.getConnection(tajoMasterAddr, TajoMasterClientProtocol.class, false);
        TajoMasterClientProtocolService.BlockingInterface tajoMasterService = tmClient.getStub();
        res = tajoMasterService.getQueryStatus(null, builder.build());

        String queryMasterHost = res.getQueryMasterHost();
        if (queryMasterHost != null && !queryMasterHost.isEmpty()) {
          NettyClientBase qmClient = null;
          try {
            InetSocketAddress qmAddr =
                NetUtils.createSocketAddr(queryMasterHost, res.getQueryMasterPort());
            qmClient = connPool.getConnection(qmAddr, QueryMasterClientProtocol.class, false);
            QueryMasterClientProtocolService.BlockingInterface queryMasterService =
                qmClient.getStub();
            res = queryMasterService.getQueryStatus(null, builder.build());

            queryMasterMap.put(queryId, qmAddr);
          } catch (Exception e) {
            throw new ServiceException(e.getMessage(), e);
          } finally {
            connPool.releaseConnection(qmClient);
          }
        }
      } catch (Exception e) {
        throw new ServiceException(e.getMessage(), e);
      } finally {
        connPool.releaseConnection(tmClient);
      }
    }
    return new QueryStatus(res);
  }
Exemplo n.º 9
0
 /**
  * Call to QueryMaster closing query resources
  *
  * @param queryId
  */
 public void closeQuery(final QueryId queryId) {
   if (queryMasterMap.containsKey(queryId)) {
     NettyClientBase qmClient = null;
     try {
       qmClient =
           connPool.getConnection(
               queryMasterMap.get(queryId), QueryMasterClientProtocol.class, false);
       QueryMasterClientProtocolService.BlockingInterface queryMasterService = qmClient.getStub();
       queryMasterService.closeQuery(null, queryId.getProto());
     } catch (Exception e) {
       LOG.warn(
           "Fail to close a QueryMaster connection (qid="
               + queryId
               + ", msg="
               + e.getMessage()
               + ")",
           e);
     } finally {
       connPool.closeConnection(qmClient);
       queryMasterMap.remove(queryId);
     }
   }
 }
Exemplo n.º 10
0
  private void cleanup(QueryId queryId) {
    LOG.info("cleanup query resources : " + queryId);
    NettyClientBase rpc = null;
    List<TajoMasterProtocol.WorkerResourceProto> workers = getAllWorker();

    for (TajoMasterProtocol.WorkerResourceProto worker : workers) {
      try {
        if (worker.getPeerRpcPort() == 0) continue;

        rpc =
            connPool.getConnection(
                NetUtils.createSocketAddr(worker.getHost(), worker.getPeerRpcPort()),
                TajoWorkerProtocol.class,
                true);
        TajoWorkerProtocol.TajoWorkerProtocolService tajoWorkerProtocolService = rpc.getStub();

        tajoWorkerProtocolService.cleanup(null, queryId.getProto(), NullCallback.get());
      } catch (Exception e) {
        LOG.error(e.getMessage());
      } finally {
        connPool.releaseConnection(rpc);
      }
    }
  }