TAJO-553: Add a method to the TajoClient to get finished query lists. (Ilhyun Suh via jihoon)
diff --git a/CHANGES.txt b/CHANGES.txt
index 1e5f747..6b397c2 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -130,6 +130,9 @@
 
   IMPROVEMENTS
 
+    TAJO-553: Add a method to the TajoClient to get finished query lists. 
+    (Ilhyun Suh via jihoon)
+
     TAJO-670: Change daemon's hostname to canonical hostname 
     (hyoungjunkim via hyunsik)
 
diff --git a/tajo-client/src/main/java/org/apache/tajo/client/TajoAdmin.java b/tajo-client/src/main/java/org/apache/tajo/client/TajoAdmin.java
index e6fe88f..0cc8110 100644
--- a/tajo-client/src/main/java/org/apache/tajo/client/TajoAdmin.java
+++ b/tajo-client/src/main/java/org/apache/tajo/client/TajoAdmin.java
@@ -172,7 +172,7 @@
 
   public static void processDesc(Writer writer, TajoClient client) throws ParseException, IOException,
       ServiceException, SQLException {
-    List<BriefQueryInfo> queryList = client.getQueryList();
+    List<BriefQueryInfo> queryList = client.getRunningQueryList();
     SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
     int id = 1;
     for (BriefQueryInfo queryInfo : queryList) {
@@ -375,7 +375,7 @@
 
   public static void processList(Writer writer, TajoClient client) throws ParseException, IOException,
       ServiceException, SQLException {
-    List<BriefQueryInfo> queryList = client.getQueryList();
+    List<BriefQueryInfo> queryList = client.getRunningQueryList();
     SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
     String fmt = "%1$-20s %2$-7s %3$-20s %4$-30s%n";
     String line = String.format(fmt, "QueryId", "State", 
diff --git a/tajo-client/src/main/java/org/apache/tajo/client/TajoClient.java b/tajo-client/src/main/java/org/apache/tajo/client/TajoClient.java
index 7ee4694..7fc6780 100644
--- a/tajo-client/src/main/java/org/apache/tajo/client/TajoClient.java
+++ b/tajo-client/src/main/java/org/apache/tajo/client/TajoClient.java
@@ -400,14 +400,27 @@
 
   }
 
-  public List<BriefQueryInfo> getQueryList() throws ServiceException {
+  public List<BriefQueryInfo> getRunningQueryList() throws ServiceException {
     return new ServerCallable<List<BriefQueryInfo>>(connPool, tajoMasterAddr,
         TajoMasterClientProtocol.class, false, true) {
       public List<BriefQueryInfo> call(NettyClientBase client) throws ServiceException {
         TajoMasterClientProtocolService.BlockingInterface tajoMasterService = client.getStub();
 
         GetQueryListRequest.Builder builder = GetQueryListRequest.newBuilder();
-        GetQueryListResponse res = tajoMasterService.getQueryList(null, builder.build());
+        GetQueryListResponse res = tajoMasterService.getRunningQueryList(null, builder.build());
+        return res.getQueryListList();
+      }
+    }.withRetries();
+  }
+
+  public List<BriefQueryInfo> getFinishedQueryList() throws ServiceException {
+    return new ServerCallable<List<BriefQueryInfo>>(connPool, tajoMasterAddr,
+        TajoMasterClientProtocol.class, false, true) {
+      public List<BriefQueryInfo> call(NettyClientBase client) throws ServiceException {
+        TajoMasterClientProtocolService.BlockingInterface tajoMasterService = client.getStub();
+
+        GetQueryListRequest.Builder builder = GetQueryListRequest.newBuilder();
+        GetQueryListResponse res = tajoMasterService.getFinishedQueryList(null, builder.build());
         return res.getQueryListList();
       }
     }.withRetries();
diff --git a/tajo-client/src/main/proto/TajoMasterClientProtocol.proto b/tajo-client/src/main/proto/TajoMasterClientProtocol.proto
index 9feecdb..93e5af9 100644
--- a/tajo-client/src/main/proto/TajoMasterClientProtocol.proto
+++ b/tajo-client/src/main/proto/TajoMasterClientProtocol.proto
@@ -34,7 +34,8 @@
   rpc updateQuery(QueryRequest) returns (UpdateQueryResponse);
   rpc explainQuery(ExplainQueryRequest) returns (ExplainQueryResponse);
   rpc getQueryResult(GetQueryResultRequest) returns (GetQueryResultResponse);
-  rpc getQueryList(GetQueryListRequest) returns (GetQueryListResponse);
+  rpc getRunningQueryList(GetQueryListRequest) returns (GetQueryListResponse);
+  rpc getFinishedQueryList(GetQueryListRequest) returns (GetQueryListResponse);
   rpc getQueryStatus(GetQueryStatusRequest) returns (GetQueryStatusResponse);
   rpc killQuery(QueryIdProto) returns (BoolProto);
   rpc getClusterInfo(GetClusterInfoRequest) returns (GetClusterInfoResponse);
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/TajoMasterClientService.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/TajoMasterClientService.java
index 60f705f..dc70f23 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/TajoMasterClientService.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/TajoMasterClientService.java
@@ -224,7 +224,7 @@
     }
 
     @Override
-    public GetQueryListResponse getQueryList(RpcController controller,
+    public GetQueryListResponse getRunningQueryList(RpcController controller,
                                              GetQueryListRequest request)
         throws ServiceException {
       GetQueryListResponse.Builder builder
@@ -257,6 +257,39 @@
     }
 
     @Override
+    public GetQueryListResponse getFinishedQueryList(RpcController controller,
+                                             GetQueryListRequest request)
+        throws ServiceException {
+      GetQueryListResponse.Builder builder
+          = GetQueryListResponse.newBuilder();
+
+      Collection<QueryInProgress> queries
+          = context.getQueryJobManager().getFinishedQueries();
+
+      BriefQueryInfo.Builder infoBuilder = BriefQueryInfo.newBuilder();
+
+      for (QueryInProgress queryInProgress : queries) {
+        QueryInfo queryInfo = queryInProgress.getQueryInfo();
+
+        infoBuilder.setQueryId(queryInfo.getQueryId().getProto());
+        infoBuilder.setState(queryInfo.getQueryState());
+        infoBuilder.setQuery(queryInfo.getSql());
+        infoBuilder.setStartTime(queryInfo.getStartTime());
+        long endTime = (queryInfo.getFinishTime() == 0) ?
+            System.currentTimeMillis() : queryInfo.getFinishTime();
+        infoBuilder.setFinishTime(endTime);
+        infoBuilder.setProgress(queryInfo.getProgress());
+        infoBuilder.setQueryMasterPort(queryInfo.getQueryMasterPort());
+        infoBuilder.setQueryMasterHost(queryInfo.getQueryMasterHost());
+
+        builder.addQueryList(infoBuilder.build());
+      }
+
+      GetQueryListResponse result = builder.build();
+      return result;
+    }
+
+    @Override
     public GetQueryStatusResponse getQueryStatus(RpcController controller,
                                                  GetQueryStatusRequest request)
         throws ServiceException {
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/client/TestTajoClient.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/client/TestTajoClient.java
index ee03bd6..3dac2f1 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/client/TestTajoClient.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/client/TestTajoClient.java
@@ -37,6 +37,7 @@
 import org.junit.experimental.categories.Category;
 
 import java.io.IOException;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.List;
@@ -458,4 +459,24 @@
     functions = client.getFunctions(null);
     assertEquals(catalogFunctions.size(), functions.size());
   }
+
+  @Test
+  public final void testGetFinishedQueryList() throws IOException,
+      ServiceException, SQLException {
+    final String tableName = "testGetFinishedQueryList";
+    String sql = "create table " + tableName + " (deptname text, score int4)";
+
+    client.updateQuery(sql);
+    assertTrue(client.existTable(tableName));
+
+    int numFinishedQueries = client.getFinishedQueryList().size();
+    ResultSet resultSet = client.executeQueryAndGetResult("select * from " + tableName);
+    assertNotNull(resultSet);
+
+    resultSet = client.executeQueryAndGetResult("select * from " + tableName);
+    assertNotNull(resultSet);
+    assertEquals(numFinishedQueries + 2, client.getFinishedQueryList().size());
+
+    resultSet.close();
+  }
 }