---
# tasks file for mongodb_linux

- name: Include OS-specific vars
  include_vars:
    file: "{{ lookup('first_found', params) }}"
  vars:
    params:
      paths:
        - "vars"
      files:
        - "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_version }}.yml"
        - "{{ ansible_facts.os_family }}-{{ ansible_facts.distribution_major_version }}.yml"
        - "{{ ansible_facts.distribution }}.yml"
        - "{{ ansible_facts.os_family }}.yml"
        - default.yml

- name: Check if we are in docker
  stat:
    path: is_docker.txt
  register: is_docker

# Tasks based on: https://docs.mongodb.com/manual/administration/production-notes/

- name: Set swappiness
  sysctl:
    name: vm.swappiness
    value: "{{ swappiness }}"
    state: present

- name: Ensure ntp service is installed
  package:
    name: "{{ ntp_package }}"
    state: present
  register: _pkg
  until: _pkg is succeeded
  retries: 5

- name: Update perms on chrony pid dir on RedHat 8
  file:
    path: /run/chrony/
    owner: root
    group: root
    state: directory
  when:
    - ansible_facts.os_family == "RedHat"
    - ansible_facts.distribution_major_version|int >= 8

- name: Ensure ntp service is configured
  service:
    name: "{{ ntp_service }}"
    state: started
    enabled: yes

- name: Ensure GNU C Library is the latest
  package:
    name: "{{ gnu_c_lib }}"
    state: latest
  register: _pkg
  until: _pkg is succeeded
  retries: 5

- name: Ensure NUMA zone reclaim is disabled
  sysctl:
    name: vm.zone_reclaim_mode
    value: 0
    state: present
    reload: yes
  when: not is_docker.stat.exists

# https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/
- name: Ensure thp-disable service exists
  copy:
    src: thp-disable.service
    dest: /etc/systemd/system/disable-transparent-huge-pages.service
    owner: root
    group: root
  register: thp

- name: Reload systemd
  systemd:
    daemon_reexec: yes
  when: thp is changed

- name: Check if disable-transparent-huge-pages service is already run
  shell: cat /sys/kernel/mm/transparent_hugepage/enabled | grep -o "never"
  register: _huge_page_status
  ignore_errors: yes
  changed_when: _huge_page_status.stdout == ""

- name: Enable disable-transparent-huge-pages service
  service:
    name: disable-transparent-huge-pages
    state: started
    enabled: yes
  when: (not is_docker.stat.exists) and (_huge_page_status.stdout == "")

# Tasks based on: https://docs.mongodb.com/manual/reference/ulimit/

- name: Set pam limits (nproc and nofile)
  pam_limits:
    domain: "{{ item[0] }}"
    limit_type: "{{ item[1] }}"
    limit_item: "{{ item[2] }}"
    value: "{{ item[3] }}"
  with_nested:
    - ["root", "mongodb"]
    - ["hard", "soft"]
    - ["nproc", "nofile"]
    - ["{{ nproc_and_nofile_limit }}"]

- name: Set pam limits (memlock)
  pam_limits:
    domain: "{{ item[0] }}"
    limit_type: "{{ item[1] }}"
    limit_item: "{{ item[2] }}"
    value: "{{ item[3] }}"
  with_nested:
    - ["root", "mongodb"]
    - ["hard", "soft"]
    - ["memlock"]
    - ["{{ memlock_limit }}"]

# Other tuning settings

- name: Set sysctl values
  sysctl:
    name: "{{ item['name'] }}"
    value: "{{ item['value'] }}"
    state: present
  loop:
    # TODO: These may need to be configurable for different usage patterns.
    - { "name": "vm.dirty_ratio", "value": "15" }
    - { "name": "vm.dirty_background_ratio", "value": "5" }
    - { "name": "net.core.somaxconn ", "value": "4096" }
    - { "name": "net.ipv4.tcp_fin_timeout", "value": "30" }
    - { "name": "net.ipv4.tcp_keepalive_intvl", "value": "30" }
    - { "name": "net.ipv4.tcp_keepalive_time", "value": "120" }
    - { "name": "net.ipv4.tcp_max_syn_backlog ", "value": "4096" }
