forked from graphprotocol/graph-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_builder.rs
More file actions
194 lines (175 loc) · 6.09 KB
/
Copy pathstore_builder.rs
File metadata and controls
194 lines (175 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use std::iter::FromIterator;
use std::{collections::HashMap, sync::Arc};
use graph::prelude::{o, MetricsRegistry};
use graph::{
prelude::{info, CheapClone, EthereumNetworkIdentifier, Logger},
util::security::SafeDisplay,
};
use graph_store_postgres::connection_pool::ConnectionPool;
use graph_store_postgres::{
ChainHeadUpdateListener as PostgresChainHeadUpdateListener, ChainStore as DieselChainStore,
NetworkStore as DieselNetworkStore, Shard as ShardName, ShardedStore, Store as DieselStore,
SubscriptionManager, PRIMARY_SHARD,
};
use crate::config::{Config, Shard};
pub struct StoreBuilder {
store: Arc<ShardedStore>,
primary_pool: ConnectionPool,
chain_head_update_listener: Arc<PostgresChainHeadUpdateListener>,
}
impl StoreBuilder {
pub fn new(logger: &Logger, config: &Config, registry: Arc<dyn MetricsRegistry>) -> Self {
let primary = config.primary_store();
let subscriptions = Arc::new(SubscriptionManager::new(
logger.cheap_clone(),
primary.connection.to_owned(),
));
let shards: Vec<_> = config
.stores
.iter()
.map(|(name, shard)| {
Self::make_shard(
logger,
name,
shard,
subscriptions.cheap_clone(),
registry.cheap_clone(),
)
})
.collect();
let primary_pool = shards
.iter()
.find(|(name, _, _)| name.as_str() == PRIMARY_SHARD.as_str())
.unwrap()
.2
.clone();
let shard_map =
HashMap::from_iter(shards.into_iter().map(|(name, shard, _)| (name, shard)));
let store = Arc::new(ShardedStore::new(
shard_map,
Arc::new(config.deployment.clone()),
));
let chain_head_update_listener = Arc::new(PostgresChainHeadUpdateListener::new(
&logger,
registry.cheap_clone(),
primary.connection.to_owned(),
));
Self {
store,
primary_pool,
chain_head_update_listener,
}
}
fn make_shard(
logger: &Logger,
name: &str,
shard: &Shard,
subscriptions: Arc<SubscriptionManager>,
registry: Arc<dyn MetricsRegistry>,
) -> (ShardName, Arc<DieselStore>, ConnectionPool) {
let logger = logger.new(o!("shard" => name.to_string()));
let conn_pool = Self::main_pool(&logger, name, shard, registry.cheap_clone());
let (read_only_conn_pools, weights) =
Self::replica_pools(&logger, name, shard, registry.cheap_clone());
let shard = Arc::new(DieselStore::new(
&logger,
subscriptions.cheap_clone(),
conn_pool.clone(),
read_only_conn_pools.clone(),
weights,
registry.cheap_clone(),
));
let name = ShardName::new(name.to_string()).expect("shard names have been validated");
(name, shard, conn_pool)
}
/// Create a connection pool for the main database of hte primary shard
/// without connecting to all the other configured databases
pub fn main_pool(
logger: &Logger,
name: &str,
shard: &Shard,
registry: Arc<dyn MetricsRegistry>,
) -> ConnectionPool {
let logger = logger.new(o!("pool" => "main"));
info!(
logger,
"Connecting to Postgres";
"url" => SafeDisplay(shard.connection.as_str()),
"conn_pool_size" => shard.pool_size,
"weight" => shard.weight
);
ConnectionPool::create(
name,
"main",
shard.connection.to_owned(),
shard.pool_size,
&logger,
registry.cheap_clone(),
)
}
/// Create connection pools for each of the replicas
fn replica_pools(
logger: &Logger,
name: &str,
shard: &Shard,
registry: Arc<dyn MetricsRegistry>,
) -> (Vec<ConnectionPool>, Vec<usize>) {
let mut weights: Vec<_> = vec![shard.weight];
(
shard
.replicas
.values()
.enumerate()
.map(|(i, replica)| {
let pool = &format!("replica{}", i + 1);
let logger = logger.new(o!("pool" => pool.clone()));
info!(
&logger,
"Connecting to Postgres (read replica {})", i+1;
"url" => SafeDisplay(replica.connection.as_str()),
"weight" => replica.weight
);
weights.push(replica.weight);
ConnectionPool::create(
name,
pool,
replica.connection.clone(),
replica.pool_size,
&logger,
registry.cheap_clone(),
)
})
.collect(),
weights,
)
}
/// Return a store that includes a `ChainStore` for the given network
pub fn network_store(
&self,
network_name: String,
network_identifier: EthereumNetworkIdentifier,
) -> Arc<DieselNetworkStore> {
let chain_store = DieselChainStore::new(
network_name,
network_identifier,
self.chain_head_update_listener.cheap_clone(),
self.primary_pool.clone(),
);
Arc::new(DieselNetworkStore::new(
self.store.cheap_clone(),
Arc::new(chain_store),
))
}
/// Return the store for subgraph and other storage; this store can
/// handle everything besides being a `ChainStore`
pub fn store(&self) -> Arc<ShardedStore> {
self.store.cheap_clone()
}
// This is used in the test-store, but rustc keeps complaining that it
// is not used
#[cfg(debug_assertions)]
#[allow(dead_code)]
pub fn primary_pool(&self) -> ConnectionPool {
self.primary_pool.clone()
}
}