;; -*- Mode: lisp; fill-column: 120 -*-

(supervisor 'ns_server_cluster_sup :mode :one_for_one
            (supervisor 'cb_couch_sup :mode :one_for_one :since "2.0"
                        (supervisor 'couch_app))
            (tmp 'cb_init_loggers)
            (tmp 'log_os_info)
            (gen-server 'timeout_diag_logger
                        "keeps track of blocks of code that are observed for timeouts. If anything takes
                        longer then expected it'll log lots of details so that we can diagnose reason of
                        timeout")
            (gen-server 'dist_manager
                        "manages changing of node's address. Persists node's address when it's changed")
            (gen-server 'ns_cookie_manager
                        "saves node's name and cookie. So that service shutdown can reach us. Guards node's
                        cookie changes as well")
            (gen-server 'ns_cluster
                        "performs node join/leave requests")
            (supervisor 'mb_mnesia_sup :mode :one_for_one
                        (gen-event 'mb_mnesia_events)
                        (gen-server 'mb_mnesia))
            (supervisor 'ns_config_sup :mode :rest_for_one
                        (gen-event 'ns_config_events
                                   "fired when any ns_config variable is changed")
                        (gen-event 'ns_config_events_local :since "1.8.1"
                                   "fired when any ns_config variable is changed locally (i.e. not replicated from other
                                   node, but genuinely changed on this node")
                        (gen-server 'ns_config
                                    "maintains local ns_config. Manages loading and (async) saving. Fires
                                    ns_config_events when any config variable is changed")
                        (gen-server 'ns_config_remote
                                    "proxy for ns_config. Why? Because we've found that replying to remote
                                    ns_config:get() calls may suspend replying process. Thus we have that
                                    proxy for 'spoonfeeding' of remote callers")
                        (gen-server 'ns_config_isasl_sync
                                    "saves bucket passwords to isasl.pw"
                                    (pubsub-link nil :to 'ns_config_events))
                        (gen-server 'ns_config_log
                                    "logs config changes"
                                    (pubsub-link nil :to 'ns_config_events))
                        (gen-server 'cb_config_couch_sync :since "2.0"
                                    "syncs important ns_config variables into couch config"
                                    (pubsub-link nil :to 'ns_config_events)))
            (worker 'vbucket_filter_changes_registry)
            (supervisor 'ns_server_sup :mode :one_for_one
                        (gen-server 'ns_log)
                        (event-handler 'ns_config_ets_dup :to 'ns_config_events :since "1.8.1"
                                       "This guy merely maintains public ets table with copy of ns_config. For real
                                        quick but not necessarily reliable access. This is used to get timeout values in
                                        few places.")
                        (gen-event 'ns_log_events
                                   "ns_log logged events are broadcasted here. ns_mail_log uses it")
                        (supervisor 'ns_node_disco_sup :mode :rest_for_one
                                    (gen-event 'ns_node_disco_events
                                               "fired when nodes() or nodes_wanted() changes")
                                    (gen-server 'ns_node_disco
                                                "fires ns_node_disco_events and guards (do we still need that ?)
                                                access to nodes_wanted")
                                    (event-handler 'ns_node_disco_log :to 'ns_node_disco_events
                                                   "logs nodes_wanted and actual nodes() changes")
                                    (event-handler 'ns_node_disco_conf_events :to 'ns_config_events
                                                   "passes config changes to ns_config_rep for replication")
                                    (worker 'ns_config_rep_merger
                                            "worker process that does actual config merging and updates config via
                                            ns_config:cas_config/2. See ns_config_rep.erl")
                                    (gen-server 'ns_config_rep
                                                "replicates entire & parts of config to/from other nodes"
                                                (event-handler 'ns_node_disco_rep_events :to 'ns_node_disco_events
                                                               "requests complete config replication to/from
                                                               newly discovered nodes")))
                        (work_queue 'vbucket_map_mirror :since "2.0"
                                    "maintains ets cache of vbucket map for faster view merger params generation. Also
                                    caches capi_url(Node)")
                        (gen-event 'ns_tick_event
                                   "local tick event. Singleton ns_tick on master node will fire tick events
                                   on all nodes")
                        (gen-event 'mb_master_events
                                   "used (incorrecly!) for waking up of pool details sleepers when master node
                                   is changed")
                        (gen-event 'buckets_events
                                   "fired when bucket is started/warmed-up/shutdown. Also fired by ns_doctor
                                   when remote bucket startup/warmup/shutdown discovered")
                        (supervisor 'ns_mail_sup :mode :one_for_all
                                    "sends out mails for 'alertful' ns_log entries"
                                    (gen-server 'ns_mail)
                                    (event-handler 'ns_mail_log :to 'ns_log_events))
                        (gen-event 'ns_stats_event
                                   "various stats collectors (for all buckets) fire newly gathered stats
                                   samples via this guy") ; NOTE: 1.8.1 spawns this a bit later
                        (gen-server 'ns_heart
                                    "gathers local stats & other info and broadcasts that to all node's ns_doctor")
                        (gen-server 'ns_doctor
                                    "keeps track of latest heartbeats from other nodes")
                        (gen-server 'remote_clusters_info
                                    "service to query and cache remote clusters information (cluster nodes and vbucket
                                    maps in particular")
                        (gen-fsm 'mb_master
                                 ;; NOTE: the following is just spawn_link relation
                                 (supervisor 'mb_master_sup :mode :one_for_one
                                             (gen-fsm (:global 'ns_orchestrator)
                                                      ;; failover is done on orchestrator process itself
                                                      (notable-call 'ns_rebalancer 'failover/1)
                                                      ;; but rebalance is separate process
                                                      (spawns 'ns_rebalancer 'rebalance/3)
                                                      ;; and janitor is separate as well
                                                      (spawns 'ns_janitor 'cleanup/2))
                                             (gen-server (:global 'ns_tick))
                                             (gen-server (:global 'auto_failover))))
                        (gen-event 'master_activity_events
                                   "Timestamped master activity events. Real data only on master node.")
                        (gen-event 'master_activity_events_ingress
                                   "Raw master activity events. Remote nodes send their stuff to master's ingress events process")
                        (pubsub-link 'master_activity_events_timestamper :to 'master_activity_events_ingress
                                     "adds timestamps to ingress events also serves queued note_xxx requests
                                     by sending them to ingress process on master")
                        (gen-server 'master_activity_events_pids_watcher)
                        (gen-server 'master_activity_events_keeper
                                    "local 'archive' of recent master events. Keeps 8k of recent events. Only
                                    relevant on master node.")
                        (supervisor 'menelaus_sup :mode :one_for_one
                                    (worker 'menelaus_web)
                                    (gen-server 'menelaus_web_alerts_srv)
                                    (event-handler 'menelaus_event
                                                   :to ns_config_events
                                                   :to ns_node_disco_events
                                                   :to mb_master_events))
                        (supervisor 'mc_sup :mode :rest_for_one :since "2.0"
                                    "remains of mccouch"
                                    (gen-event 'mc_couch_events
                                               "used to signal (synchronously!) vbucket state changes")
                                    (supervisor 'mc_conn_sup
                                                "individual mccouch connections live here. See mc_connection")
                                    (worker 'mc_tcp_listener
                                            "accept loop for mccouch connections"))
                        (supervisor 'ns_port_sup :mode :one_for_one
                                    (event-handler 'ns_port_init :to 'ns_config_events)
                                    ;; dynamic, but typically memcached & moxi ports. Those are in fact
                                    ;; started through supervisor_cushion
                                    (gen_server 'ns_port_server "memcached")
                                    (gen_server 'ns_port_server "moxi"))
                        (event-handler 'ns_port_memcached_killer :to ns_config_events
                                       "implements memcached die! signalling after failover")
                        (gen-server 'ns_memcached_log_rotator
                                    "periodically sweeps too old memcached.log.XXX-s")
                        (gen-server 'ns_bucket_worker
                                    "'work' of starting and shutting down ns_bucket_sup childs is done by this
                                    guy. I.e. because supervisor:xxx API is synchronous")
                        (supervisor 'xdc_replication_sup :mode :one_for_one
                                    "owns individual XDCR replications")
                        (gen-server 'xdc_rep_manager
                                    "monitors _replicator db for replications. Babysits couch replications. Interacts
                                    with couch replicator in really subtle ways")
                        (supervisor 'ns_bucket_sup :mode :one_for_one
                                    (pubsub-link 'buckets_observing_subscription :to 'ns_config_events
                                                 "observes config for buckets that should be started/stopped
                                                 on this node and submits work for doing that to ns_bucket_worker")
                                    ;; dynamic, but lets assume we have bucket "default"
                                    (worker '(per_bucket_sup "default")
                                            (supervisor 'single_bucket_sup :mode :rest_for_one
                                                        (gen-server '(capi_set_view_manager "default") :since "2.0"
                                                                    "changes state of set indexes so that indexing
                                                                     covers active and only active vbuckets. Plus keeps
                                                                     track of all ddocs and replicates them across the
                                                                     cluster."
                                                                    (worker :name 'capi_ddoc_replication_srv-default
                                                                            "In order to avoid breaking backwards compat
                                                                             with C-release all ddoc replication casts
                                                                             are actually sent to this guy, which merely
                                                                             proxies them to capi_set_view_manager"))
                                                        (gen-server '(ns_memcached "default") :name 'ns_memcached-default
                                                                    "our communication channel with bucket. Creates
                                                                     bucket on startup. Monitors it for warmup. Monitors
                                                                     bucket parameters versus config (i.e. if bucket
                                                                     quota needs to be adjusted). Performs requests to
                                                                     grab stats and change vbucket states. Since 1.8.1
                                                                     we maintain pool of workers and each worker has 1
                                                                     connection to memcached. Main process is passing
                                                                     requests to workers in pool trying to be smart
                                                                     about queuing.")
                                                        (supervisor '(ns_vbm_sup "default") :name 'ns_vbm_sup-default
                                                                    "hosts replication ebucketmigrators")
                                                        (gen_server '(tap_replication_manager "default") :name 'tap_replication_manager-default
                                                                    "keeps track of desired replications. Serializes
                                                                    replication changes w.r.t. not-ready-vbuckets
                                                                    restarts.")
                                                        (supervisor '(ns_vbm_new_sup "default") :name 'ns_vbm_sup-default :since "2.0"
                                                                    "hosts replication ebucketmigrators for 2.0
                                                                     \"replicators on destination\"")
                                                        (gen-server '(janitor_agent "default") :name 'janitor_agent-default :since "2.0"
                                                                    "applies replication and vbucket state changes
                                                                     decided by janitor and vbuckets mover")
                                                        (gen-server '(couch_stats_reader "default") :name 'couch_stats_reader-default :since "2.0"
                                                                    "samples & keeps some couch stats so that
                                                                    stats_collector can get them quickly")
                                                        (gen-server '(stats_collector "default")
                                                                    "on each tick grabs memcached stats parses/massages
                                                                     them a bit and sends them out to ns_stats_event"
                                                                    (pubsub-link nil :to 'ns_tick_event))
                                                        (gen-server '(stats_archiver "default")
                                                                    "saves stats for this bucket that it sees on
                                                                     ns_stats_event"
                                                                    (pubsub-link nil :to 'ns_stats_event))
                                                        (gen-server '(stats_reader "default")
                                                                    "used to read mnesia stats archive")
                                                        (event-handler '(failover_safeness_level "default") :to 'ns_stats_event
                                                                       "estimates bucket's replication up-to-date-ness
                                                                        'from this node' based on stats it sees"))))
                        (gen-server 'system_stats_collector)
                        (gen-server '(stats_archiver "@system"))
                        (gen-server '(stats_reader "@system"))
                        (work_queue 'ns_moxi_sup_work_queue)
                        (supervisor 'ns_moxi_sup :mode :one_for_one
                                    "hosts per-port moxis")
                        (gen-server 'compaction_daemon :since "2.0"
                                    "triggers and monitors bucket's db and views compactions")
                        (cb_generic_replication_srv 'xdc_rdoc_replication_srv :since "2.0"
                                                    "replicates _replicator docs across cluster")
                        (gen-server 'set_view_update_daemon :since "2.0"
                                    "periodically triggers set view indexes update")
                        (gen-server 'samples_loader_tasks :since "2.0"
                                    "babysits sample loading tasks and exposes them via tasks API")))
