Skip to content

Commit 50e0134

Browse files
author
pmmiranda
committed
- Added beacon node db file locks
- minor fixes
1 parent c850a49 commit 50e0134

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

nimbus/consensus/wrapper_consensus.nim

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{.push raises: [].}
99

1010
import
11-
std/[os, random, terminal, times, exitprocs],
11+
std/[os, random, terminal, times, exitprocs, atomics],
1212
chronos,
1313
chronicles,
1414
metrics,
@@ -2295,12 +2295,17 @@ proc run(node: BeaconNode) {.raises: [CatchableError].} =
22952295
# time to say goodbye
22962296
node.stop()
22972297

2298+
# db lock
2299+
var shouldCreatePid*: Atomic[bool]
2300+
shouldCreatePid.store(true)
2301+
22982302
var gPidFile: string
22992303
proc createPidFile(filename: string) {.raises: [IOError].} =
2300-
writeFile filename, $os.getCurrentProcessId()
2301-
gPidFile = filename
2302-
addExitProc proc() {.noconv.} =
2303-
discard io2.removeFile(gPidFile)
2304+
if shouldCreatePid.load():
2305+
writeFile filename, $os.getCurrentProcessId()
2306+
gPidFile = filename
2307+
addExitProc proc() {.noconv.} =
2308+
discard io2.removeFile(gPidFile)
23042309

23052310
proc initializeNetworking(node: BeaconNode) {.async.} =
23062311
node.installMessageValidators()

nimbus/execution/execution_layer.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import
2020
logScope:
2121
topics = "Execution layer"
2222

23-
var nimbusHandler = NimbusNode()
23+
var nimbusHandler: NimbusNode
2424

2525
proc shutdownExecution*() =
2626
nimbusHandler.state = NimbusState.Stopping

nimbus/nimbus.nim

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,37 @@
55
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
66
# at your option. This file may not be copied, modified, or distributed except according to those terms.
77

8+
{.push raises: [].}
9+
810
import
9-
std/[concurrency/atomics, os],
11+
std/[concurrency/atomics, os, exitprocs],
1012
chronicles,
1113
execution/execution_layer,
12-
consensus/consensus_layer,
14+
consensus/[consensus_layer, wrapper_consensus],
1315
common/utils,
1416
conf,
1517
confutils/cli_parser,
18+
stew/io2,
1619
beacon_chain/conf,
1720
../execution_chain/config
1821

1922
# ------------------------------------------------------------------------------
2023
# Private
2124
# ------------------------------------------------------------------------------
2225

26+
#beacon node db lock
27+
var beaconNodeLock {.global.}: string
28+
29+
proc createBeaconNodeFileLock(filename: string) {.raises: [IOError].} =
30+
shouldCreatePid.store(false)
31+
32+
writeFile filename, $os.getCurrentProcessId()
33+
beaconNodeLock = filename
34+
35+
addExitProc proc() {.noconv.} =
36+
if beaconNodeLock.len > 0:
37+
discard io2.removeFile(beaconNodeLock)
38+
2339
## create and configure service
2440
proc startService(nimbus: var Nimbus, service: var NimbusService) =
2541
#channel creation (shared memory)
@@ -32,7 +48,10 @@ proc startService(nimbus: var Nimbus, service: var NimbusService) =
3248
isConfigRead.store(false)
3349

3450
#start thread
35-
createThread(service.serviceHandler, service.serviceFunc, serviceChannel)
51+
try:
52+
createThread(service.serviceHandler, service.serviceFunc, serviceChannel)
53+
except Exception as e:
54+
fatal "error creating thread", err = e.msg
3655

3756
let optionsTable = block:
3857
case service.layerConfig.kind
@@ -64,7 +83,10 @@ proc startService(nimbus: var Nimbus, service: var NimbusService) =
6483
writeConfigString(writeOffset, opt)
6584
writeConfigString(writeOffset, arg)
6685

67-
serviceChannel[].send(byteArray)
86+
try:
87+
serviceChannel[].send(byteArray)
88+
except Exception as e:
89+
fatal "channel error: ", err = e.msg
6890

6991
#wait for service read ack
7092
while not isConfigRead.load():
@@ -115,19 +137,15 @@ proc controlCHandler() {.noconv.} =
115137

116138
notice "\tCtrl+C pressed. Shutting down services ..."
117139

118-
# WA to shutdown client(exceptions thrown)
119-
# issues related with nat.nim shutdown procedure (nim-eth
120-
quit 0
121-
122140
shutdownExecution()
123141
shutdownConsensus()
124142

125143
# ------------------------------------------------------------------------------
126144
# Public
127145
# ------------------------------------------------------------------------------
128146

129-
# Setup services
130-
proc setup*(nimbus: var Nimbus) =
147+
# Setup nimbus and services
148+
proc setup(nimbus: var Nimbus) {.raises: [CatchableError].} =
131149
let
132150
executionConfigNames = extractFieldNames(NimbusConf)
133151
consensusConfigNames = extractFieldNames(BeaconNodeConf)
@@ -164,6 +182,9 @@ proc setup*(nimbus: var Nimbus) =
164182
nimbus.serviceList.add(executionService)
165183
nimbus.serviceList.add(consensusService)
166184

185+
# todo: replace path with config,datadir when creating Nimbus config
186+
createBeaconNodeFileLock(".beacon_node.pid")
187+
167188
## start nimbus client
168189
proc run*(nimbus: var Nimbus) =
169190
try:
@@ -181,12 +202,17 @@ proc run*(nimbus: var Nimbus) =
181202
# wait for shutdown
182203
nimbus.monitorServices()
183204

184-
# ------
205+
{.pop.}
206+
# -----
207+
185208
when isMainModule:
186209
notice "Starting Nimbus"
187210

188211
setupFileLimits()
189212

213+
# todo: replace path with config after creating Nimbus config
214+
# setupLogging(config.logLevel, config.logStdout, config.logFile)
215+
190216
var nimbus = Nimbus()
191217
nimbus.setup()
192218
nimbus.run()

0 commit comments

Comments
 (0)