Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Pascal Engélibert
duniter
Commits
63a7f4b7
Commit
63a7f4b7
authored
Jan 11, 2018
by
Vincent Rousseau
Browse files
Add and remove workers during eco mode
parent
53fdad34
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/modules/prover/lib/blockProver.ts
View file @
63a7f4b7
...
...
@@ -48,6 +48,14 @@ export class WorkerFarm {
return
this
.
theEngine
.
getNbWorkers
()
}
reduceNbCores
()
{
return
this
.
theEngine
.
reduceNbCores
()
}
boostCPU
(){
return
this
.
theEngine
.
boostCPU
()
}
changeCPU
(
cpu
:
any
)
{
return
this
.
theEngine
.
setConf
({
cpu
})
}
...
...
@@ -194,13 +202,12 @@ export class BlockProver {
});
if
(
!
result
)
{
this
.
logger
.
info
(
'
GIVEN proof-of-work for block#%s with %s leading zeros followed by [0-
'
+
highMark
+
'
]! stop PoW for %s
'
,
block
.
number
,
nbZeros
,
this
.
pair
&&
this
.
pair
.
pub
.
slice
(
0
,
6
));
let
selfNbBlockInFrame
=
await
this
.
server
.
getBcContext
().
getIssuerNbBlockInFrame
(
this
.
server
.
PeeringService
.
selfPubkey
)
if
(
this
.
conf
.
ecoMode
===
true
)
{
let
selfNbBlockInFrame
=
await
this
.
server
.
getBcContext
().
getIssuerNbBlockInFrame
(
this
.
server
.
PeeringService
.
selfPubkey
)
if
(
selfNbBlockInFrame
<
2
)
{
this
.
change
CPU
(
1
)
this
.
conf
.
nbCores
=
this
.
server
.
conf
.
nb
C
or
e
s
this
.
logger
.
info
(
"
Boost number of CPU cores
"
+
this
.
conf
.
nb
C
or
e
s
+
"
with only
"
+
selfNbBlockInFrame
+
"
block member in frame
"
)
this
.
boost
CPU
()
this
.
conf
.
nbCores
=
powFarm
.
nb
W
or
ker
s
this
.
logger
.
info
(
"
Boost number of CPU cores
"
+
powFarm
.
nb
W
or
ker
s
+
"
with only
"
+
selfNbBlockInFrame
+
"
block member in frame
"
)
}
}
throw
'
Proof-of-work computation canceled because block received
'
;
...
...
@@ -214,14 +221,15 @@ export class BlockProver {
if
(
this
.
conf
.
ecoMode
===
true
)
{
let
selfNbBlockInFrame
=
await
this
.
server
.
getBcContext
().
getIssuerNbBlockInFrame
(
this
.
server
.
PeeringService
.
selfPubkey
)
if
(
selfNbBlockInFrame
<
2
)
{
this
.
change
CPU
(
1
)
this
.
conf
.
nbCores
=
this
.
server
.
conf
.
nb
C
or
e
s
this
.
logger
.
info
(
"
Boost number of CPU cores
"
+
this
.
conf
.
nb
C
or
e
s
+
"
with only
"
+
selfNbBlockInFrame
+
"
block member in frame
"
)
this
.
boost
CPU
()
this
.
conf
.
nbCores
=
powFarm
.
nb
W
or
ker
s
this
.
logger
.
info
(
"
Boost number of CPU cores
"
+
powFarm
.
nb
W
or
ker
s
+
"
with only
"
+
selfNbBlockInFrame
+
"
block member in frame
"
)
}
else
if
(
this
.
conf
.
nbCores
*
testsPerSecond
>
ProverConstants
.
ECO_MODE_MINIMAL_TESTS_PER_SECONDS
)
{
if
(
this
.
conf
.
nbCores
>
1
)
{
this
.
logger
.
info
(
"
Reducing number of CPU cores
"
+
this
.
conf
.
nbCores
)
this
.
conf
.
nbCores
=
this
.
conf
.
nbCores
-
1
else
if
(
testsPerSecond
>
ProverConstants
.
ECO_MODE_MINIMAL_TESTS_PER_SECONDS
)
{
if
(
powFarm
.
nbWorkers
>
1
)
{
this
.
logger
.
info
(
"
Reducing number of CPU cores
"
+
powFarm
.
nbWorkers
)
this
.
reduceNbCores
()
this
.
conf
.
nbCores
=
powFarm
.
nbWorkers
}
else
if
(
this
.
conf
.
cpu
>
ProverConstants
.
ECO_MODE_MINIMAL_CPU
){
let
cpu
:
number
=
this
.
conf
.
cpu
-
0.1
...
...
@@ -235,6 +243,17 @@ export class BlockProver {
})()
};
async
reduceNbCores
()
{
const
farm
=
await
this
.
getWorker
()
return
farm
.
reduceNbCores
()
}
async
boostCPU
()
{
this
.
conf
.
cpu
=
1.0
const
farm
=
await
this
.
getWorker
()
return
farm
.
boostCPU
()
}
async
changeCPU
(
cpu
:
number
)
{
this
.
conf
.
cpu
=
Math
.
max
(
0.01
,
Math
.
min
(
1.0
,
cpu
));
const
farm
=
await
this
.
getWorker
()
...
...
app/modules/prover/lib/engine.ts
View file @
63a7f4b7
...
...
@@ -12,15 +12,12 @@ if(debug) {
export
class
PowEngine
{
private
nbWorkers
:
number
private
cluster
:
PowCluster
readonly
id
:
number
constructor
(
private
conf
:
ConfDTO
,
logger
:
any
)
{
// We use as much cores as available, but not more than CORES_MAXIMUM_USE_IN_PARALLEL
this
.
nbWorkers
=
conf
.
nbCores
this
.
cluster
=
new
PowCluster
(
this
.
nbWorkers
,
logger
)
this
.
cluster
=
new
PowCluster
(
conf
.
nbCores
,
logger
)
this
.
id
=
this
.
cluster
.
clusterId
}
...
...
@@ -41,6 +38,14 @@ export class PowEngine {
return
this
.
cluster
.
cancelWork
()
}
reduceNbCores
()
{
return
this
.
cluster
.
removeSlave
()
}
boostCPU
()
{
return
this
.
cluster
.
boostCPU
()
}
setConf
(
value
:
any
)
{
return
this
.
cluster
.
changeConf
(
value
)
}
...
...
app/modules/prover/lib/powCluster.ts
View file @
63a7f4b7
...
...
@@ -35,6 +35,7 @@ export class Master {
logger
:
any
onInfoCallback
:
any
workersOnline
:
Promise
<
any
>
[]
maxNbCores
:
number
=
Math
.
min
(
ProverConstants
.
CORES_MAXIMUM_USE_IN_PARALLEL
,
require
(
'
os
'
).
cpus
().
length
)
constructor
(
private
nbCores
:
number
,
logger
:
any
)
{
this
.
clusterId
=
clusterId
++
...
...
@@ -65,6 +66,56 @@ export class Master {
// this.logger.debug(`ENGINE c#${this.clusterId}#${this.slavesMap[worker.id].index}:`, message)
}
createSlave
(
index
:
number
)
{
const
nodejsWorker
=
cluster
.
fork
()
const
worker
=
new
PowWorker
(
nodejsWorker
,
message
=>
{
this
.
onWorkerMessage
(
index
,
message
)
},
()
=>
{
this
.
logger
.
info
(
`[online] worker c#
${
this
.
clusterId
}
#w#
${
index
}
`
)
worker
.
sendConf
({
command
:
'
conf
'
,
value
:
this
.
conf
})
},
(
code
:
any
,
signal
:
any
)
=>
{
this
.
logger
.
info
(
`worker
${
worker
.
pid
}
died with code
${
code
}
and signal
${
signal
}
`
)
})
this
.
logger
.
info
(
`Creating worker c#
${
this
.
clusterId
}
#w#
${
nodejsWorker
.
id
}
`
)
const
slave
=
{
// The Node.js worker
worker
,
// Inner identifier
index
,
// Worker ready
online
:
worker
.
online
,
// Each worker has his own chunk of possible nonces
nonceBeginning
:
this
.
nbCores
===
1
?
0
:
(
index
+
1
)
*
ProverConstants
.
NONCE_RANGE
}
this
.
slavesMap
[
nodejsWorker
.
id
]
=
slave
return
slave
}
boostCPU
()
{
if
(
this
.
nbWorkers
<
this
.
maxNbCores
)
{
while
(
this
.
nbWorkers
<
this
.
maxNbCores
)
{
this
.
slaves
.
push
(
this
.
createSlave
(
this
.
nbWorkers
))
}
}
let
conf
:
any
=
{
cpu
:
1
}
this
.
changeConf
(
conf
)
}
async
removeSlave
()
{
let
nb_workers
=
this
.
nbWorkers
await
this
.
slaves
[
nb_workers
-
1
].
worker
.
kill
()
this
.
slaves
.
pop
()
this
.
logger
.
info
(
'
Remove slave number
'
+
(
nb_workers
-
1
))
}
/*****************
* CLUSTER METHODS
****************/
...
...
@@ -77,36 +128,7 @@ export class Master {
})
this
.
slaves
=
Array
.
from
({
length
:
this
.
nbCores
}).
map
((
value
,
index
)
=>
{
const
nodejsWorker
=
cluster
.
fork
()
const
worker
=
new
PowWorker
(
nodejsWorker
,
message
=>
{
this
.
onWorkerMessage
(
index
,
message
)
},
()
=>
{
this
.
logger
.
info
(
`[online] worker c#
${
this
.
clusterId
}
#w#
${
index
}
`
)
worker
.
sendConf
({
command
:
'
conf
'
,
value
:
this
.
conf
})
},
(
code
:
any
,
signal
:
any
)
=>
{
this
.
logger
.
info
(
`worker
${
worker
.
pid
}
died with code
${
code
}
and signal
${
signal
}
`
)
})
this
.
logger
.
info
(
`Creating worker c#
${
this
.
clusterId
}
#w#
${
nodejsWorker
.
id
}
`
)
const
slave
=
{
// The Node.js worker
worker
,
// Inner identifier
index
,
// Worker ready
online
:
worker
.
online
,
// Each worker has his own chunk of possible nonces
nonceBeginning
:
this
.
nbCores
===
1
?
0
:
(
index
+
1
)
*
ProverConstants
.
NONCE_RANGE
}
this
.
slavesMap
[
nodejsWorker
.
id
]
=
slave
return
slave
return
this
.
createSlave
(
index
)
})
this
.
workersOnline
=
this
.
slaves
.
map
((
s
)
=>
s
.
online
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment