diem_config/config/
storage_config.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::utils;
9use serde::{Deserialize, Serialize};
10use std::{
11    net::{IpAddr, Ipv4Addr, SocketAddr},
12    path::PathBuf,
13};
14
15/// Port selected RocksDB options for tuning underlying rocksdb instance of
16/// DiemDB. see <https://github.com/facebook/rocksdb/blob/master/include/rocksdb/options.h>
17/// for detailed explanations.
18#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
19#[serde(default, deny_unknown_fields)]
20pub struct RocksdbConfig {
21    pub max_open_files: i32,
22    pub max_total_wal_size: u64,
23}
24
25impl Default for RocksdbConfig {
26    fn default() -> Self {
27        Self {
28            // Set max_open_files to 10k instead of -1 to avoid keep-growing memory in accordance
29            // with the number of files.
30            max_open_files: 10_000,
31            // For now we set the max total WAL size to be 1G. This config can be useful when column
32            // families are updated at non-uniform frequencies.
33            #[allow(clippy::integer_arithmetic)] // TODO: remove once clippy lint fixed
34            max_total_wal_size: 1u64 << 30,
35        }
36    }
37}
38
39#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
40#[serde(default, deny_unknown_fields)]
41pub struct StorageConfig {
42    pub address: SocketAddr,
43    pub backup_service_address: SocketAddr,
44    pub dir: PathBuf,
45    pub grpc_max_receive_len: Option<i32>,
46    /// None disables pruning. The windows is in number of versions, consider
47    /// system tps (transaction per second) when calculating proper window.
48    pub prune_window: Option<u64>,
49    #[serde(skip)]
50    data_dir: PathBuf,
51    /// Read, Write, Connect timeout for network operations in milliseconds
52    pub timeout_ms: u64,
53    /// Rocksdb-specific configurations
54    pub rocksdb_config: RocksdbConfig,
55}
56
57impl Default for StorageConfig {
58    fn default() -> StorageConfig {
59        StorageConfig {
60            address: SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 6666),
61            backup_service_address: SocketAddr::new(
62                IpAddr::V4(Ipv4Addr::LOCALHOST),
63                6186,
64            ),
65            dir: PathBuf::from("db"),
66            grpc_max_receive_len: Some(100_000_000),
67            // The prune window must at least out live a RPC request because its
68            // sub requests are to return a consistent view of the
69            // DB at exactly same version. Considering a few
70            // thousand TPS we are potentially going to achieve, and a few
71            // minutes a consistent view of the DB might require,
72            // 10k (TPS)  * 100 (seconds)  =  1 Million might be a
73            // conservatively safe minimal prune window. It'll take a few
74            // Gigabytes of disk space depending on the size of an
75            // average account blob.
76            prune_window: None,
77            data_dir: PathBuf::from("./pos_db"),
78            // Default read/write/connection timeout, in milliseconds
79            timeout_ms: 30_000,
80            rocksdb_config: RocksdbConfig::default(),
81        }
82    }
83}
84
85impl StorageConfig {
86    pub fn dir(&self) -> PathBuf {
87        if self.dir.is_relative() {
88            self.data_dir.join(&self.dir)
89        } else {
90            self.dir.clone()
91        }
92    }
93
94    pub fn set_data_dir(&mut self, data_dir: PathBuf) {
95        self.data_dir = data_dir;
96    }
97
98    pub fn randomize_ports(&mut self) {
99        self.address.set_port(utils::get_available_port());
100        self.backup_service_address
101            .set_port(utils::get_available_port());
102    }
103}