Skip to content

Commit

Permalink
Fix Task List API to accept strings as TaskIds
Browse files Browse the repository at this point in the history
In the Java source, task identification is represented by the `TaskId`
class. That class contains a `nodeId` (a `string`) and a numerical `id`
(a `long`). In the Tasks API, these two get mixed. Sometimes the
`TaskId` is used, which is a concatenation of `nodeId`, a colon, and
the numerical `id`, which yields a `string`. Sometimes, though,
especially in the response structures, the `nodeId` and `id` get
returned individually. So we must look at each of the response
structures in the Java source to find out the correct type used in the
response.

This commit now changes the Task List API to accept task ids as a
string, because that's what the REST API spec defines. We also add the
`GroupBy` property, and some missing fields in the response structures.

We also update Travis configuration and `run-es.sh` to Elasticsearch 6.1.2.
  • Loading branch information
olivere committed Jan 18, 2018
1 parent e771061 commit 2a0f106
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ services:
- docker
before_install:
- sudo sysctl -w vm.max_map_count=262144
- docker run -d --rm -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "bootstrap.memory_lock=true" -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" docker.elastic.co/elasticsearch/elasticsearch:6.1.1 elasticsearch -Expack.security.enabled=false -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_
- docker run -d --rm -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "bootstrap.memory_lock=true" -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" docker.elastic.co/elasticsearch/elasticsearch:6.1.2 elasticsearch -Expack.security.enabled=false -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_
27 changes: 0 additions & 27 deletions docker-compose.yml

This file was deleted.

2 changes: 1 addition & 1 deletion run-es.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
VERSION=${VERSION:=6.1.1}
VERSION=${VERSION:=6.1.2}
docker run --rm -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "bootstrap.memory_lock=true" -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" docker.elastic.co/elasticsearch/elasticsearch:$VERSION elasticsearch -Expack.security.enabled=false -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_
46 changes: 26 additions & 20 deletions tasks_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,31 @@ import (

// TasksListService retrieves the list of currently executing tasks
// on one ore more nodes in the cluster. It is part of the Task Management API
// documented at http://www.elastic.co/guide/en/elasticsearch/reference/5.2/tasks-list.html.
// documented at https://www.elastic.co/guide/en/elasticsearch/reference/6.0/tasks.html.
//
// It is supported as of Elasticsearch 2.3.0.
type TasksListService struct {
client *Client
pretty bool
taskId []int64
taskId []string
actions []string
detailed *bool
nodeId []string
parentNode string
parentTask *int64
parentTaskId *string
waitForCompletion *bool
groupBy string
}

// NewTasksListService creates a new TasksListService.
func NewTasksListService(client *Client) *TasksListService {
return &TasksListService{
client: client,
taskId: make([]int64, 0),
actions: make([]string, 0),
nodeId: make([]string, 0),
client: client,
}
}

// TaskId indicates to returns the task(s) with specified id(s).
func (s *TasksListService) TaskId(taskId ...int64) *TasksListService {
func (s *TasksListService) TaskId(taskId ...string) *TasksListService {
s.taskId = append(s.taskId, taskId...)
return s
}
Expand Down Expand Up @@ -72,9 +70,9 @@ func (s *TasksListService) ParentNode(parentNode string) *TasksListService {
return s
}

// ParentTask returns tasks with specified parent task id. Set to -1 to return all.
func (s *TasksListService) ParentTask(parentTask int64) *TasksListService {
s.parentTask = &parentTask
// ParentTaskId returns tasks with specified parent task id (node_id:task_number). Set to -1 to return all.
func (s *TasksListService) ParentTaskId(parentTaskId string) *TasksListService {
s.parentTaskId = &parentTaskId
return s
}

Expand All @@ -85,6 +83,13 @@ func (s *TasksListService) WaitForCompletion(waitForCompletion bool) *TasksListS
return s
}

// GroupBy groups tasks by nodes or parent/child relationships.
// As of now, it can either be "nodes" (default) or "parents".
func (s *TasksListService) GroupBy(groupBy string) *TasksListService {
s.groupBy = groupBy
return s
}

// Pretty indicates that the JSON response be indented and human readable.
func (s *TasksListService) Pretty(pretty bool) *TasksListService {
s.pretty = pretty
Expand All @@ -97,12 +102,8 @@ func (s *TasksListService) buildURL() (string, url.Values, error) {
var err error
var path string
if len(s.taskId) > 0 {
var tasks []string
for _, taskId := range s.taskId {
tasks = append(tasks, fmt.Sprintf("%d", taskId))
}
path, err = uritemplates.Expand("/_tasks/{task_id}", map[string]string{
"task_id": strings.Join(tasks, ","),
"task_id": strings.Join(s.taskId, ","),
})
} else {
path = "/_tasks"
Expand All @@ -128,12 +129,15 @@ func (s *TasksListService) buildURL() (string, url.Values, error) {
if s.parentNode != "" {
params.Set("parent_node", s.parentNode)
}
if s.parentTask != nil {
params.Set("parent_task", fmt.Sprintf("%v", *s.parentTask))
if s.parentTaskId != nil {
params.Set("parent_task_id", *s.parentTaskId)
}
if s.waitForCompletion != nil {
params.Set("wait_for_completion", fmt.Sprintf("%v", *s.waitForCompletion))
}
if s.groupBy != "" {
params.Set("group_by", s.groupBy)
}
return path, params, nil
}

Expand Down Expand Up @@ -182,7 +186,7 @@ type TasksListResponse struct {
}

type TaskOperationFailure struct {
TaskId int64 `json:"task_id"`
TaskId int64 `json:"task_id"` // this is a long in the Java source
NodeId string `json:"node_id"`
Status string `json:"status"`
Reason *ErrorDetails `json:"reason"`
Expand All @@ -198,14 +202,16 @@ type DiscoveryNode struct {
TransportAddress string `json:"transport_address"`
Host string `json:"host"`
IP string `json:"ip"`
Roles []string `json:"roles"` // "master", "data", or "ingest"
Attributes map[string]interface{} `json:"attributes"`
// Tasks returns the tasks by its id (as a string).
Tasks map[string]*TaskInfo `json:"tasks"`
}

// TaskInfo represents information about a currently running task.
type TaskInfo struct {
Node string `json:"node"`
Id int64 `json:"id"` // the task id
Id int64 `json:"id"` // the task id (yes, this is a long in the Java source)
Type string `json:"type"`
Action string `json:"action"`
Status interface{} `json:"status"` // has separate implementations of Task.Status in Java for reindexing, replication, and "RawTaskStatus"
Expand Down
8 changes: 4 additions & 4 deletions tasks_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ func TestTasksListBuildURL(t *testing.T) {
client := setupTestClient(t)

tests := []struct {
TaskId []int64
TaskId []string
Expected string
}{
{
[]int64{},
[]string{},
"/_tasks",
},
{
[]int64{42},
[]string{"42"},
"/_tasks/42",
},
{
[]int64{42, 37},
[]string{"42", "37"},
"/_tasks/42%2C37",
},
}
Expand Down

0 comments on commit 2a0f106

Please sign in to comment.