private void getQueryResult()

in tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/webapp/QueryExecutorServlet.java [277:374]


    private void getQueryResult(QueryId tajoQueryId) {
      // query execute
      try {
        QueryStatus status = null;

        while (!stop.get()) {
          try {
            Thread.sleep(1000);
          } catch(InterruptedException e) {
            break;
          }
          status = tajoClient.getQueryStatus(tajoQueryId);
          if (status.getState() == TajoProtos.QueryState.QUERY_MASTER_INIT
              || status.getState() == TajoProtos.QueryState.QUERY_MASTER_LAUNCHED) {
            continue;
          }

          if (status.getState() == TajoProtos.QueryState.QUERY_RUNNING
              || status.getState() == TajoProtos.QueryState.QUERY_SUCCEEDED) {
            int progressValue = (int) (status.getProgress() * 100.0f);
            if(progressValue == 100)  {
              progressValue = 99;
            }
            progress.set(progressValue);
          }
          if (status.getState() != TajoProtos.QueryState.QUERY_RUNNING
              && status.getState() != TajoProtos.QueryState.QUERY_NOT_ASSIGNED) {
            break;
          }
        }

        if(status == null) {
          LOG.error("Query Status is null");
          error = new Exception("Query Status is null");
          return;
        }
        if (status.getState() == TajoProtos.QueryState.QUERY_ERROR ||
            status.getState() == TajoProtos.QueryState.QUERY_FAILED) {
          error = new Exception(status.getErrorMessage());
        } else if (status.getState() == TajoProtos.QueryState.QUERY_KILLED) {
          LOG.info(queryId + " is killed.");
          error = new Exception(queryId + " is killed.");
        } else {
          if (status.getState() == TajoProtos.QueryState.QUERY_SUCCEEDED) {
            if (status.hasResult()) {
              ResultSet res = null;
              try {
                ClientProtos.GetQueryResultResponse response = tajoClient.getResultResponse(tajoQueryId);
                TableDesc desc = CatalogUtil.newTableDesc(response.getTableDesc());
                tajoClient.getConf().setVar(TajoConf.ConfVars.USERNAME, response.getTajoUserName());
                res = new TajoResultSet(tajoClient, queryId, tajoClient.getConf(), desc);

                ResultSetMetaData rsmd = res.getMetaData();
                resultSize = desc.getStats().getNumBytes();
                LOG.info("Tajo Query Result: " + desc.getPath() + "\n");

                int numOfColumns = rsmd.getColumnCount();
                for(int i = 0; i < numOfColumns; i++) {
                  columnNames.add(rsmd.getColumnName(i + 1));
                }
                queryResult = new ArrayList<List<Object>>();

                if(sizeLimit < resultSize) {
                    numOfRows = (long)((float)(desc.getStats().getNumRows()) * ((float)sizeLimit / (float)resultSize));
                } else {
                    numOfRows = desc.getStats().getNumRows();
                }
                int rowCount = 0;
                boolean hasMoreData = false;
                while (res.next()) {
                  if(rowCount > numOfRows) {
                    hasMoreData = true;
                    break;
                  }
                  List<Object> row = new ArrayList<Object>();
                  for(int i = 0; i < numOfColumns; i++) {
                    row.add(res.getObject(i + 1).toString());
                  }
                  queryResult.add(row);
                  rowCount++;

                }
              } finally {
                if (res != null) {
                  res.close();
                }
                progress.set(100);
              }
            } else {
              error = new Exception(queryId + " no result");
            }
          }
        }
      } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        error = e;
      }
    }