ton-blockchain / ton

Main TON monorepo
Other
3.09k stars 979 forks source link

Run `storage-daemon` as a daemon via systemd #675

Open air3ijai opened 1 year ago

air3ijai commented 1 year ago

Describe the issue

How to run storage-daemon as a daemon via systemd?

What behavior did you expect?

When we run storage-daemon with -d key it should be run as a daemon in background

storage-daemon -C global.config.json -I 0.0.0.0:3333 -p 5555 -D storage-db -d
storage-daemon --help
Server for seeding and downloading bags of files (torrents)
. Options:
  -v, --verbosity<arg>     set verbosity level
  -V, --version            shows storage-daemon build information
  -h, --help               prints a help message
  -I, --ip<arg>            set <ip>:<port> for adnl. :<port> for client mode
  -p, --control-port<arg>  port for control interface
  -C, --global-config<arg> global TON configuration file
  -D, --db<arg>            db root
  -d, --daemonize          set SIGHUP
  -l, --logname<arg>       log to file
  -P, --storage-provider   run storage provider

What was the actual behavior?

Application is running in a foreground

storage-daemon -v 3 -C global.config.json -I 0.0.0.0:3333 -p 5555 -D storage-db -d
[ 3][t 1][2023-04-22 19:38:36.042555279][storage-daemon.cpp:183][!storage-daemon]       Storage daemon ADNL id is gs6ziNuWQZUBLieC48NGEaXw1R/3MfrTmXbickMHM6M=
[ 3][t 1][2023-04-22 19:38:36.043221737][storage-daemon.cpp:184][!storage-daemon]       DHT id is 35h1TxI12cWNKjd7DGVD1I68XZ+OsJyMSn0k7+3REak=
[ 3][t 1][2023-04-22 19:38:36.095549265][StorageManager.cpp:49][!storage]       Starting Storage manager. DB = /opt/ton/storage-db/torrent

How reliably can you reproduce the issue, what are the steps to do so?

  1. Download storage-daemon binary
  2. Download global.config.json config
  3. Run storage-daemon -C global.config.json -I 0.0.0.0:3333 -p 5555 -D storage-db -d
  4. Application remains in a foreground

What version of TON are you using, where did you get it (github actions, self-compiled, etc)?

https://github.com/ton-blockchain/ton/releases/tag/v2023.04

What type of machine are you observing the error on (OS/CPU and disk type)?

# hostnamectl
Virtualization: amazon
Operating System: Ubuntu 22.04.2 LTS
Kernel: Linux 5.15.0-1031-aws
Architecture: x86-64
Hardware Vendor: Amazon EC2
Hardware Model: t3a.nano

Thanks!

air3ijai commented 1 year ago

Performed steps

  1. Create unit file

    file=/lib/systemd/system/ton-storage.service
    public_ip=1.1.1.1
    
    cat << EOF > $file
    [Unit]
    Description=TON Storage daemon
    Documentation=https://docs.ton.org
    After=network.target
    
    [Service]
    LimitNOFILE=10000
    MemorySwapMax=0
    TimeoutStartSec=infinity
    Type=notify
    User=root
    Group=root
    StateDirectory=ton-storage
    ExecStart=/usr/local/bin/storage-daemon -v 5 --global-config /opt/ton/global.config.json --ip $public_ip:3333 -p 5555 --db /opt/ton/storage-db --logname /opt/ton/storage-daemon.log --daemonize
    Restart=always
    RestartSec=2
    TimeoutStopSec=5
    KillSignal=SIGINT
    
    [Install]
    WantedBy=default.target
    EOF
    1. Enable service

      systemctl enable ton-storage
    2. Start serivce

      systemctl start ton-storage
    3. Check status

      
      systemctl status ton-storage
      ● ton-storage.service - TON Storage daemon
      Loaded: loaded (/lib/systemd/system/ton-storage.service; enabled; vendor preset: enabled)
      Active: activating (start) since Fri 2023-04-28 17:12:30 UTC; 3min 46s ago
        Docs: https://docs.ton.org
      Main PID: 37976 (storage-daemon)
        Tasks: 15 (limit: 1116)
      Memory: 26.1M (swap max: 0B)
          CPU: 23.046s
      CGroup: /system.slice/ton-storage.service
              └─37976 /usr/local/bin/storage-daemon -v 5 --global-config /opt/ton/global.config.json --ip 1.1.1.1:3333 -p 5555 --db /opt/ton/storage-db ->

    Apr 28 17:12:30 ip-172-31-47-142 systemd[1]: Starting TON Storage daemon...

We see that

  1. Active: activating
  2. Starting TON Storage daemon...

It looks like it not started but starting and can't be restarted seamlessly.

air3ijai commented 1 year ago

Played with the IPFS in daemon mode

  1. Install
    curl -OL https://github.com/ipfs/kubo/releases/download/v0.19.1/kubo_v0.19.1_linux-amd64.tar.gz
    tar -zxvf kubo_v0.19.1_linux-amd64.tar.gz
    bash kubo/install.sh
  2. Init
    ipfs init
  3. Run in daemon mode

    ipfs daemon
    
    Initializing daemon...
    Kubo version: 0.19.1
    Repo version: 13
    System version: amd64/linux
    Golang version: go1.19.8
    
    Computed default go-libp2p Resource Manager limits based on:
        - 'Swarm.ResourceMgr.MaxMemory': "496 MB"
        - 'Swarm.ResourceMgr.MaxFileDescriptors': 524288
    
    Theses can be inspected with 'ipfs swarm resources'.
    
    2023/04/30 14:00:49 failed to sufficiently increase receive buffer size (was: 208 kiB, wanted: 2048 kiB, got: 416 kiB). See https://github.com/quic-go/quic-go/wiki/UDP-Receive-Buffer-Size for details.
    Swarm listening on /ip4/127.0.0.1/tcp/4001
    Swarm listening on /ip4/127.0.0.1/udp/4001/quic
    Swarm listening on /ip4/127.0.0.1/udp/4001/quic-v1
    Swarm listening on /ip4/127.0.0.1/udp/4001/quic-v1/webtransport/certhash/uEiCvH6BHMTkQzJhxsPDgQ3mokrGFpaPAanfiTIn44LY-uw/certhash/uEiArS423IOw-SiFiQnEIpQ2RDZquEQgotEe_z8-gu4CcTw
    Swarm listening on /ip4/172.31.40.254/tcp/4001
    ...

    Conclusion is that in daemon mode application is not not necessary to work in a foreground.

At the same time, systemd service

vi /lib/systemd/system/ipfs.service
[Unit]
Description=InterPlanetary File System (IPFS) daemon
Documentation=https://docs.ipfs.tech
After=network.target

[Service]
LimitNOFILE=10000
MemorySwapMax=0
TimeoutStartSec=infinity
Type=notify
User=root
Group=root
StateDirectory=ipfs
ExecStart=ipfs daemon
Restart=always
RestartSec=2
TimeoutStopSec=5
KillSignal=SIGINT

[Install]
WantedBy=default.target
systemctl enable ipfs
systemctl start ipfs
systemctl status ipfs
Screenshot 2023-04-30 at 17 08 18

We see that service is active (running) and we can manage it via systemctl (stop|start|restart).