wilfred-s commented on a change in pull request #186:
URL: 
https://github.com/apache/incubator-yunikorn-core/pull/186#discussion_r462017189



##########
File path: pkg/webservice/handlers.go
##########
@@ -82,14 +82,31 @@ func getClusterInfo(w http.ResponseWriter, r *http.Request) 
{
 func getApplicationsInfo(w http.ResponseWriter, r *http.Request) {
        writeHeaders(w)
 
+       queueName := r.URL.Query().Get("queue")
+       if queueName != "" {
+               queueNameArr := strings.Split(queueName, ".")
+               for _, name := range queueNameArr {
+                       if !configs.QueueNameRegExp.MatchString(name) {
+                               http.Error(w, "Invalid queue name "+name+", a 
name must only have alphanumeric characters,"+
+                                       " - or _, and be no longer than 64 
characters", http.StatusBadRequest)
+                       }
+               }

Review comment:
       Couple of comments:
   1. factor out the check, this is reusable to later also allow queue lists to 
be filtered: `validateQueueQuery(string) error`
   1. when an error is generated we need to return immediately we cannot go on 
processing, the filter will cause all results to be ignored any way
   1. echo the full parameter first, then the part that is incorrect within 
that parameter as part of the error. Also be clear about the fact that the 
_query_ parameter parsing failed

##########
File path: pkg/cache/app_test.go
##########
@@ -0,0 +1,34 @@
+package cache
+
+import (
+       "gotest.tools/assert"
+       "testing"
+)
+
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+func TestKilledStateTransition1(t *testing.T) {

Review comment:
       Not sure why this is added as this is already covered in 
`applicaton_state_test.TestKilledStateTransition()`

##########
File path: pkg/webservice/handlers_test.go
##########
@@ -175,3 +177,87 @@ func TestContainerHistory(t *testing.T) {
        assert.Equal(t, contHist[0].TotalContainers, "2", "metric 1 should be 1 
apps and was not")
        assert.Equal(t, contHist[4].TotalContainers, "300", "metric 5 should be 
300 apps and was not")
 }
+
+func TestQueryParamInAppsHandler(t *testing.T) {
+       const configDefault = `
+partitions:
+  - name: default
+    queues:
+      - name: root
+        queues:
+        - name: default
+`
+       rmID := "rm-123"
+       policyGroup := "default-policy-group"
+       clusterInfo := cache.NewClusterInfo()
+
+       configs.MockSchedulerConfigByData([]byte(configDefault))
+       if _, err := cache.SetClusterInfoFromConfigFile(clusterInfo, rmID, 
policyGroup); err != nil {
+               t.Errorf("Error when load clusterInfo from config %v", err)
+               return
+       }
+       assert.Equal(t, 1, len(clusterInfo.ListPartitions()))
+
+       // Check default partition
+       partitionName := "[" + rmID + "]default"
+       defaultPartition := clusterInfo.GetPartition(partitionName)
+       assert.Equal(t, 0, len(defaultPartition.GetApplications()))
+
+       // add a new app
+       appInfo := cache.CreateNewApplicationInfo("app-1", partitionName, 
"root.default")
+       err := cache.AddAppToPartition(appInfo, defaultPartition)
+       if err != nil {

Review comment:
       use `assert.NilError`

##########
File path: pkg/webservice/handlers_test.go
##########
@@ -175,3 +177,87 @@ func TestContainerHistory(t *testing.T) {
        assert.Equal(t, contHist[0].TotalContainers, "2", "metric 1 should be 1 
apps and was not")
        assert.Equal(t, contHist[4].TotalContainers, "300", "metric 5 should be 
300 apps and was not")
 }
+
+func TestQueryParamInAppsHandler(t *testing.T) {
+       const configDefault = `
+partitions:
+  - name: default
+    queues:
+      - name: root
+        queues:
+        - name: default
+`
+       rmID := "rm-123"
+       policyGroup := "default-policy-group"
+       clusterInfo := cache.NewClusterInfo()
+
+       configs.MockSchedulerConfigByData([]byte(configDefault))
+       if _, err := cache.SetClusterInfoFromConfigFile(clusterInfo, rmID, 
policyGroup); err != nil {
+               t.Errorf("Error when load clusterInfo from config %v", err)
+               return
+       }
+       assert.Equal(t, 1, len(clusterInfo.ListPartitions()))
+
+       // Check default partition
+       partitionName := "[" + rmID + "]default"
+       defaultPartition := clusterInfo.GetPartition(partitionName)
+       assert.Equal(t, 0, len(defaultPartition.GetApplications()))
+
+       // add a new app
+       appInfo := cache.CreateNewApplicationInfo("app-1", partitionName, 
"root.default")
+       err := cache.AddAppToPartition(appInfo, defaultPartition)
+       if err != nil {
+               t.Errorf("add application to partition should not have failed: 
%v", err)
+       }
+       assert.Equal(t, appInfo.GetApplicationState(), "New")
+       assert.Equal(t, 1, len(defaultPartition.GetApplications()))
+
+       NewWebApp(clusterInfo, nil)
+
+       // Passing "root.default" as filter return 1 application
+       req, errReq := http.NewRequest("GET", "/ws/v1/apps?queue=root.default", 
strings.NewReader(""))
+       assert.NilError(t, errReq, "App Handler request failed")
+       resp := &MockResponseWriter{}
+       var appsDao []*dao.ApplicationDAOInfo
+       getApplicationsInfo(resp, req)
+       err = json.Unmarshal(resp.outputBytes, &appsDao)
+       assert.NilError(t, err, "failed to unmarshal applications dao response 
from response body: %s", string(resp.outputBytes))
+       assert.Equal(t, len(appsDao), 1)
+
+       // Passing "root.q1" as filter return 0 application as there is no app 
running in "root.q1" queue
+       req1, err1 := http.NewRequest("GET", "/ws/v1/apps?queue=root.q1", 
strings.NewReader(""))
+       assert.NilError(t, err1, "App Handler request failed")
+       resp1 := &MockResponseWriter{}
+       var appsDao1 []*dao.ApplicationDAOInfo

Review comment:
       you can reuse `appsDao`, if the conversion fails an error is returned 
otherwise the content is updated by the Unmarshal

##########
File path: pkg/webservice/handlers.go
##########
@@ -82,14 +82,31 @@ func getClusterInfo(w http.ResponseWriter, r *http.Request) 
{
 func getApplicationsInfo(w http.ResponseWriter, r *http.Request) {
        writeHeaders(w)
 
+       queueName := r.URL.Query().Get("queue")
+       if queueName != "" {
+               queueNameArr := strings.Split(queueName, ".")
+               for _, name := range queueNameArr {
+                       if !configs.QueueNameRegExp.MatchString(name) {
+                               http.Error(w, "Invalid queue name "+name+", a 
name must only have alphanumeric characters,"+
+                                       " - or _, and be no longer than 64 
characters", http.StatusBadRequest)
+                       }
+               }
+       }
        var appsDao []*dao.ApplicationDAOInfo
        lists := gClusterInfo.ListPartitions()
        for _, k := range lists {
                partition := gClusterInfo.GetPartition(k)
                appList := partition.GetApplications()
                for _, app := range appList {
-                       appDao := getApplicationJSON(app)
-                       appsDao = append(appsDao, appDao)
+                       if len(queueName) > 0 {
+                               if strings.EqualFold(queueName, app.QueueName) {
+                                       appDao := getApplicationJSON(app)
+                                       appsDao = append(appsDao, appDao)
+                               }
+                       } else {
+                               appDao := getApplicationJSON(app)
+                               appsDao = append(appsDao, appDao)
+                       }

Review comment:
       Simplify this to:
   ```
   if len(queueName) == 0 || strings.EqualFold(queueName, app.QueueName) {
        appsDao = append(appsDao, getApplicationJSON(app))
   }
   ```

##########
File path: pkg/webservice/handlers_test.go
##########
@@ -175,3 +177,87 @@ func TestContainerHistory(t *testing.T) {
        assert.Equal(t, contHist[0].TotalContainers, "2", "metric 1 should be 1 
apps and was not")
        assert.Equal(t, contHist[4].TotalContainers, "300", "metric 5 should be 
300 apps and was not")
 }
+
+func TestQueryParamInAppsHandler(t *testing.T) {
+       const configDefault = `
+partitions:
+  - name: default
+    queues:
+      - name: root
+        queues:
+        - name: default
+`
+       rmID := "rm-123"
+       policyGroup := "default-policy-group"
+       clusterInfo := cache.NewClusterInfo()
+
+       configs.MockSchedulerConfigByData([]byte(configDefault))
+       if _, err := cache.SetClusterInfoFromConfigFile(clusterInfo, rmID, 
policyGroup); err != nil {
+               t.Errorf("Error when load clusterInfo from config %v", err)
+               return
+       }
+       assert.Equal(t, 1, len(clusterInfo.ListPartitions()))
+
+       // Check default partition
+       partitionName := "[" + rmID + "]default"
+       defaultPartition := clusterInfo.GetPartition(partitionName)
+       assert.Equal(t, 0, len(defaultPartition.GetApplications()))
+
+       // add a new app
+       appInfo := cache.CreateNewApplicationInfo("app-1", partitionName, 
"root.default")
+       err := cache.AddAppToPartition(appInfo, defaultPartition)
+       if err != nil {
+               t.Errorf("add application to partition should not have failed: 
%v", err)
+       }
+       assert.Equal(t, appInfo.GetApplicationState(), "New")
+       assert.Equal(t, 1, len(defaultPartition.GetApplications()))
+
+       NewWebApp(clusterInfo, nil)
+
+       // Passing "root.default" as filter return 1 application
+       req, errReq := http.NewRequest("GET", "/ws/v1/apps?queue=root.default", 
strings.NewReader(""))
+       assert.NilError(t, errReq, "App Handler request failed")
+       resp := &MockResponseWriter{}
+       var appsDao []*dao.ApplicationDAOInfo
+       getApplicationsInfo(resp, req)
+       err = json.Unmarshal(resp.outputBytes, &appsDao)
+       assert.NilError(t, err, "failed to unmarshal applications dao response 
from response body: %s", string(resp.outputBytes))
+       assert.Equal(t, len(appsDao), 1)
+
+       // Passing "root.q1" as filter return 0 application as there is no app 
running in "root.q1" queue
+       req1, err1 := http.NewRequest("GET", "/ws/v1/apps?queue=root.q1", 
strings.NewReader(""))

Review comment:
       You can reuse variables: `resp`, `req` and `errReq`, no need to create 
req1..req4
   You can even reuse `err` instead of `errReq` by declaring `var req 
*http.Request` before line 218




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to