Skip to content

Commit 6af1b83

Browse files
Qi Xiaoauxten
authored andcommitted
Add more metric vars of miner node (#326)
* Generate wallet address in loadConfig * Add miner node metric * Fix mis-used create metric call * Update chain feature metrics * Add miner disk usage metrics * Golint issues * Avoid metric nil panic on cleared block node
1 parent 3d0f67b commit 6af1b83

File tree

8 files changed

+226
-32
lines changed

8 files changed

+226
-32
lines changed

cmd/cql-minerd/disk_usage.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2019 The CovenantSQL Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"expvar"
21+
"io/ioutil"
22+
"os/exec"
23+
"runtime"
24+
"strconv"
25+
"strings"
26+
27+
"github.com/CovenantSQL/CovenantSQL/conf"
28+
"github.com/CovenantSQL/CovenantSQL/utils/log"
29+
)
30+
31+
var (
32+
diskUsageMetric = expvar.NewInt("service:miner:disk:usage")
33+
)
34+
35+
func collectDiskUsage() (err error) {
36+
// run du on linux and mac
37+
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
38+
return
39+
}
40+
41+
if conf.GConf == nil || conf.GConf.Miner == nil || conf.GConf.Miner.RootDir == "" {
42+
log.Error("miner config is empty, disk usage report is disabled")
43+
return
44+
}
45+
46+
duBin, err := exec.LookPath("du")
47+
if err != nil {
48+
log.WithError(err).Error("could not found du command")
49+
return
50+
}
51+
52+
cmd := exec.Command(duBin, "-sk", conf.GConf.Miner.RootDir)
53+
duOutput, err := cmd.StdoutPipe()
54+
if err != nil {
55+
log.WithError(err).Error("could not get result of disk usage")
56+
return
57+
}
58+
59+
err = cmd.Start()
60+
if err != nil {
61+
log.WithError(err).Error("could not start disk usage command")
62+
return
63+
}
64+
65+
err = cmd.Wait()
66+
if err != nil {
67+
log.WithError(err).Error("run disk usage command failed")
68+
return
69+
}
70+
71+
duResult, err := ioutil.ReadAll(duOutput)
72+
if err != nil {
73+
log.WithError(err).Error("get disk usage result failed")
74+
return
75+
}
76+
77+
splitResult := strings.SplitN(string(duResult), "\t", 2)
78+
if len(splitResult) == 0 || len(strings.TrimSpace(splitResult[0])) == 0 {
79+
log.Error("could not get disk usage result")
80+
return
81+
}
82+
83+
usedKiloBytes, err := strconv.ParseInt(strings.TrimSpace(splitResult[0]), 10, 64)
84+
if err != nil {
85+
log.WithError(err).Error("could not parse usage bytes result")
86+
return
87+
}
88+
89+
diskUsageMetric.Set(usedKiloBytes)
90+
91+
return
92+
}

cmd/cql-minerd/main.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func main() {
151151
if conf.GConf.Miner.MaxReqTimeGap.Seconds() <= 0 {
152152
log.Fatal("miner request time gap is invalid")
153153
}
154+
if conf.GConf.Miner.DiskUsageInterval.Seconds() <= 0 {
155+
// set to default disk usage interval
156+
log.Warning("miner disk usage interval not provided, set to default 10 minutes")
157+
conf.GConf.Miner.DiskUsageInterval = time.Minute * 10
158+
}
154159

155160
log.Debugf("config:\n%#v", conf.GConf)
156161

@@ -176,6 +181,8 @@ func main() {
176181
log.WithError(err).Fatal("init node failed")
177182
}
178183

184+
initMetrics()
185+
179186
// stop channel for all daemon routines
180187
stopCh := make(chan struct{})
181188
defer close(stopCh)
@@ -197,7 +204,7 @@ func main() {
197204
// start prometheus collector
198205
reg := metric.StartMetricCollector()
199206

200-
// start period provide service transaction generator
207+
// start periodic provide service transaction generator
201208
go func() {
202209
tick := time.NewTicker(conf.GConf.Miner.ProvideServiceInterval)
203210
defer tick.Stop()
@@ -213,6 +220,22 @@ func main() {
213220
}
214221
}()
215222

223+
// start periodic disk usage metric update
224+
go func() {
225+
for {
226+
err := collectDiskUsage()
227+
if err != nil {
228+
log.WithError(err).Error("collect disk usage failed")
229+
}
230+
231+
select {
232+
case <-stopCh:
233+
return
234+
case <-time.After(conf.GConf.Miner.DiskUsageInterval):
235+
}
236+
}
237+
}()
238+
216239
// start dbms
217240
var dbms *worker.DBMS
218241
if dbms, err = startDBMS(server, direct, func() {

cmd/cql-minerd/node.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"expvar"
2021
"fmt"
2122
"syscall"
2223
"time"
@@ -32,6 +33,13 @@ import (
3233
"github.com/CovenantSQL/CovenantSQL/utils/log"
3334
)
3435

36+
const (
37+
mwMinerAddr = "service:miner:addr"
38+
mwMinerNodeID = "service:miner:node"
39+
mwMinerWallet = "service:miner:wallet"
40+
mwMinerDiskRoot = "service:miner:disk:root"
41+
)
42+
3543
func initNode() (server *mux.Server, direct *rpc.Server, err error) {
3644
var masterKey []byte
3745
if !conf.GConf.UseTestMasterKey {
@@ -78,9 +86,6 @@ func initNode() (server *mux.Server, direct *rpc.Server, err error) {
7886

7987
func createServer(privateKeyPath string, masterKey []byte, listenAddr string) (server *mux.Server, err error) {
8088
server = mux.NewServer()
81-
if err != nil {
82-
return
83-
}
8489
err = server.InitRPCServer(listenAddr, privateKeyPath, masterKey)
8590
return
8691
}
@@ -90,9 +95,18 @@ func createDirectServer(privateKeyPath string, masterKey []byte, listenAddr stri
9095
return nil, nil
9196
}
9297
server = rpc.NewServer()
93-
if err != nil {
94-
return
95-
}
9698
err = server.InitRPCServer(listenAddr, privateKeyPath, masterKey)
9799
return
98100
}
101+
102+
func initMetrics() {
103+
if conf.GConf != nil {
104+
expvar.NewString(mwMinerAddr).Set(conf.GConf.ListenAddr)
105+
expvar.NewString(mwMinerNodeID).Set(string(conf.GConf.ThisNodeID))
106+
expvar.NewString(mwMinerWallet).Set(conf.GConf.WalletAddress)
107+
108+
if conf.GConf.Miner != nil {
109+
expvar.NewString(mwMinerDiskRoot).Set(conf.GConf.Miner.RootDir)
110+
}
111+
}
112+
}

cmd/cql/internal/wallet.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/CovenantSQL/CovenantSQL/client"
2424
"github.com/CovenantSQL/CovenantSQL/conf"
25-
"github.com/CovenantSQL/CovenantSQL/crypto"
2625
"github.com/CovenantSQL/CovenantSQL/types"
2726
)
2827

@@ -51,32 +50,11 @@ func init() {
5150
CmdWallet.Flag.StringVar(&tokenName, "token", "", "Get specific token's balance of current account, e.g. Particle, Wave, All")
5251
}
5352

54-
func walletGen() string {
55-
//TODO if config has wallet, print and return
56-
57-
publicKey := getPublicFromConfig()
58-
59-
keyHash, err := crypto.PubKeyHash(publicKey)
60-
if err != nil {
61-
ConsoleLog.WithError(err).Error("unexpected error")
62-
SetExitStatus(1)
63-
return ""
64-
}
65-
66-
return keyHash.String()
67-
68-
//TODO store in config.yaml
69-
}
70-
7153
func runWallet(cmd *Command, args []string) {
7254
commonFlagsInit(cmd)
7355
configInit()
7456

75-
if conf.GConf.WalletAddress != "" {
76-
fmt.Printf("\n\nwallet address: %s\n", conf.GConf.WalletAddress)
77-
} else {
78-
fmt.Printf("\n\nwallet address: %s\n", walletGen())
79-
}
57+
fmt.Printf("\n\nwallet address: %s\n", conf.GConf.WalletAddress)
8058

8159
var err error
8260
if strings.ToLower(tokenName) == "" {

conf/config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
yaml "gopkg.in/yaml.v2"
2525

26+
"github.com/CovenantSQL/CovenantSQL/crypto"
2627
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
2728
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
2829
"github.com/CovenantSQL/CovenantSQL/pow/cpuminer"
@@ -93,6 +94,7 @@ type MinerInfo struct {
9394
RootDir string `yaml:"RootDir"`
9495
MaxReqTimeGap time.Duration `yaml:"MaxReqTimeGap,omitempty"`
9596
ProvideServiceInterval time.Duration `yaml:"ProvideServiceInterval,omitempty"`
97+
DiskUsageInterval time.Duration `yaml:"DiskUsageInterval,omitempty"`
9698
TargetUsers []proto.AccountAddress `yaml:"TargetUsers,omitempty"`
9799
}
98100

@@ -203,5 +205,22 @@ func LoadConfig(configPath string) (config *Config, err error) {
203205
if config.Miner != nil && !path.IsAbs(config.Miner.RootDir) {
204206
config.Miner.RootDir = path.Join(configDir, config.Miner.RootDir)
205207
}
208+
209+
if config.WalletAddress != "" {
210+
for _, node := range config.KnownNodes {
211+
if node.ID == config.ThisNodeID {
212+
if node.PublicKey != nil {
213+
var walletHash proto.AccountAddress
214+
215+
if walletHash, err = crypto.PubKeyHash(node.PublicKey); err != nil {
216+
return
217+
}
218+
219+
config.WalletAddress = walletHash.String()
220+
}
221+
}
222+
}
223+
}
224+
206225
return
207226
}

0 commit comments

Comments
 (0)