Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce ecs api call in running_essential_task? method #37

Conversation

woshidan
Copy link
Contributor

@woshidan woshidan commented Aug 9, 2018

Modify instance deregister logic to reduce ECS API calls.

task_groups = service_config.client.describe_tasks(cluster: service_config.cluster, tasks: task_arns).tasks.map(&:group)
task_groups.include?("service:#{service_config.name}")
container_arns = service_config.client.list_container_instances(cluster: service_config.cluster, filter: "task:group == service:#{service_config.name}")[0]
container_arns.include?(instance.container_instance_arn)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that this diff return the same results.

The test environment like this:

  • 1 cluster
    • 1 autoscaling group for the cluster
  • 2 services in the cluster
  • 1 service is specified with services:name key in YAML config file for autoscaler
    • then I stop and run this specified service's task and check result

detail for test environment: https://gist.github.com/woshidan/5463dfeba59cb506c3ca2cc4d2106e4a

task_arns = service_config.client.list_tasks(cluster: service_config.cluster, container_instance: instance.container_instance_arn).task_arns
task_groups = service_config.client.describe_tasks(cluster: service_config.cluster, tasks: task_arns).tasks.map(&:group)
task_groups.include?("service:#{service_config.name}")
container_arns = service_config.client.list_container_instances(cluster: service_config.cluster, filter: "task:group == service:#{service_config.name}")[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このAPI呼び出しをこのメソッドから分離できる。
これを分離して339行目のループの外側で、serviceに関連するコンテナインスタンスを事前に取得して、このメソッドでは引数として存在するinstanceのarnと比較するだけにすれば、API呼び出し回数は50分の1ぐらいになる。

多分メソッドの置き場所としては242行目にあるServiceConfig#fetch_container_instancesですね。
まず、このメソッドの名前をfetch_all_container_instancesに変える。
ServiceConfig#fetch_container_instancesでサービスが起動したタスクをホストしているcontainer_instancesを取れる様にする。
そうすれば割と自然になりますね。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8ecb03ae6ccd81 のコミットで対応しました。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

検証に利用したecs_autoscalerの設定

polling_interval: 30

auto_scaling_groups:
  - name: ag_woshidan_test
    region: ap-northeast-1
    buffer: 1 # タスク数に対する余剰のインスタンス数

services:
  - name: woshidan-test-service
    cluster: woshidan-test-cluster
    region: ap-northeast-1
    auto_scaling_group_name: ag_woshidan_test
    step: 3
    idle_time: 120
    max_task_count: [15]
    cooldown_time_for_reach_max: 600
    min_task_count: 3
    upscale_triggers:
      - alarm_name: "TEST ALARM TO TRIGGER UPSCALE"
        state: ALARM
    downscale_triggers:
      - alarm_name: "TEST ALARM TO TRIGGER DOWNSCALE"
        state: ALARM
        step: 6

検証内容・結果

アラートを

時刻 アラートの状態変化
16:39 TEST ALARM TO TRIGGER UPSCALE: OK => ARARM
16:51 TEST ALARM TO TRIGGER UPSCALE: ARARM => OK
16:51 TEST ALARM TO TRIGGER DOWNSCALE: OK => ARARM

のように変化させて、

  • ecs_autoscalerで管理しているサービス( woshidan-test-service )のタスクの数
  • 同じクラスタのコンテナインスタンスに乗っているecs_autoscalerで管理していないサービス( woshidan-test-service-2 )のタスクの数
  • AutoScalingグループのインスタンスのデタッチ、アタッチの履歴

は以下なのでよさそうです(環境構築の詳細は https://gist.github.com/woshidan/2235b3e6a194bd37d795379855d3f2be)。

image

image

image

@woshidan woshidan force-pushed the reduce-ecs-api-call-in-running-essential-task-method branch from 0d2ca6e to 1da8ccc Compare August 14, 2018 03:52
… and fetch the list out of the check if conatiner instance in cluster is deregisterable
@woshidan woshidan force-pushed the reduce-ecs-api-call-in-running-essential-task-method branch from 1da8ccc to e6ccd81 Compare August 14, 2018 05:35
break unless resp.next_token
end

chunk_size = 50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、arnしか使ってないので、ここから下必要無くて、arnだけ返せば良いと思う。
他の部分は良さそうですね!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます!

そこはたしかにARNしかつかってないんですが、fetch_container_instances_in_servicefetch_container_instances_in_cluster で対照的なメソッドがあるとき、その両方で扱ってるオブジェクトのクラスが同じ方がわかりやすいかもなーってちょっと迷ってます。

直しておいたほうがいいでしょうか。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そもそもAPIリクエスト回数を減らすのが目的だし、そこが対照的であったとしても、そんなに分かり易さに寄与しないと私は思います。
まあ、これで減るのはserviceの数 * せいぜい1回か2回分ぐらいですが。

後、実質的に使ってないコードがあるより、無い方が良い。
使ってなさそうなコードが残ってると後になって読んだ時にそうである意図に悩むことになる。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

では、直しちゃいます 💪

…ist because container instances in service is not used in running_essential_task?
Copy link
Collaborator

@joker1007 joker1007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM。
動作確認取れたらマージして良いです。

@woshidan
Copy link
Contributor Author

アラートを

時刻 アラートの状態変化
15:37 TEST ALARM TO TRIGGER UPSCALE: OK => ARARM
15:53 TEST ALARM TO TRIGGER UPSCALE: ARARM => OK
15:53 TEST ALARM TO TRIGGER DOWNSCALE: OK => ARARM

のように変化させて、 #37 (comment) と同様の検証を行ったところ、動作確認の結果はよさそうです 🙆

2018-08-16 16 05 17

2018-08-16 16 02 42
2018-08-16 16 02 54
2018-08-16 16 03 04

@woshidan
Copy link
Contributor Author

ということでマージします!

@woshidan woshidan merged commit 6ae37b7 into reproio:master Aug 16, 2018
@woshidan woshidan deleted the reduce-ecs-api-call-in-running-essential-task-method branch August 16, 2018 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants