Browse Source

Merge pull request #79 from tendermint/update-monitor-and-bench

Update monitor and bench
pull/1943/head
Anton Kaliaev 7 years ago
committed by GitHub
parent
commit
62965e68f1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 583 additions and 363 deletions
  1. +2
    -74
      tm-bench/README.rst
  2. +1
    -0
      tm-monitor/.dockerignore
  3. +5
    -2
      tm-monitor/Dockerfile.dev
  4. +296
    -0
      tm-monitor/Gopkg.lock
  5. +54
    -0
      tm-monitor/Gopkg.toml
  6. +78
    -10
      tm-monitor/Makefile
  7. +74
    -1
      tm-monitor/README.rst
  8. +0
    -176
      tm-monitor/glide.lock
  9. +0
    -25
      tm-monitor/glide.yaml
  10. +21
    -6
      tm-monitor/monitor/monitor.go
  11. +6
    -9
      tm-monitor/monitor/monitor_test.go
  12. +11
    -0
      tm-monitor/monitor/network.go
  13. +21
    -37
      tm-monitor/monitor/network_test.go
  14. +3
    -2
      tm-monitor/monitor/node.go
  15. +11
    -21
      tm-monitor/monitor/node_test.go

+ 2
- 74
tm-bench/README.rst View File

@ -1,5 +1,5 @@
Benchmarking and Monitoring
===========================
Benchmarking
============
tm-bench
--------
@ -85,75 +85,3 @@ Development
make get_vendor_deps
make test
tm-monitor
----------
Tendermint blockchain monitoring tool; watches over one or more nodes, collecting and providing various statistics to the user: https://github.com/tendermint/tools/tree/master/tm-monitor
Quick Start
^^^^^^^^^^^
Docker
~~~~~~
::
docker run -it --rm -v "/tmp:/tendermint" tendermint/tendermint init
docker run -it --rm -v "/tmp:/tendermint" -p "46657:46657" --name=tm tendermint/tendermint
docker run -it --rm --link=tm tendermint/monitor tm:46657
Binaries
~~~~~~~~
This will be the same as you did for ``tm-bench`` above, except for the last line which should be:
::
tm-monitor localhost:46657
Usage
^^^^^
::
tm-monitor [-v] [-no-ton] [-listen-addr="tcp://0.0.0.0:46670"] [endpoints]
Examples:
# monitor single instance
tm-monitor localhost:46657
# monitor a few instances by providing comma-separated list of RPC endpoints
tm-monitor host1:46657,host2:46657
Flags:
-listen-addr string
HTTP and Websocket server listen address (default "tcp://0.0.0.0:46670")
-no-ton
Do not show ton (table of nodes)
-v verbose logging
RPC UI
^^^^^^
Run ``tm-monitor`` and visit http://localhost:46670
You should see the list of the available RPC endpoints:
::
http://localhost:46670/status
http://localhost:46670/status/network
http://localhost:46670/monitor?endpoint=_
http://localhost:46670/status/node?name=_
http://localhost:46670/unmonitor?endpoint=_
The API is available as GET requests with URI encoded parameters, or as JSONRPC
POST requests. The JSONRPC methods are also exposed over websocket.
Development
^^^^^^^^^^^
::
make get_vendor_deps
make test

+ 1
- 0
tm-monitor/.dockerignore View File

@ -0,0 +1 @@
vendor

+ 5
- 2
tm-monitor/Dockerfile.dev View File

@ -4,8 +4,11 @@ RUN mkdir -p /go/src/github.com/tendermint/tools/tm-monitor
WORKDIR /go/src/github.com/tendermint/tools/tm-monitor
COPY Makefile /go/src/github.com/tendermint/tools/tm-monitor/
COPY glide.yaml /go/src/github.com/tendermint/tools/tm-monitor/
COPY glide.lock /go/src/github.com/tendermint/tools/tm-monitor/
RUN make get_tools
COPY Gopkg.toml /go/src/github.com/tendermint/tools/tm-monitor/
COPY Gopkg.lock /go/src/github.com/tendermint/tools/tm-monitor/
RUN make get_vendor_deps


+ 296
- 0
tm-monitor/Gopkg.lock View File

@ -0,0 +1,296 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "github.com/btcsuite/btcd"
packages = ["btcec"]
revision = "2be2f12b358dc57d70b8f501b00be450192efbc3"
[[projects]]
name = "github.com/davecgh/go-spew"
packages = ["spew"]
revision = "346938d642f2ec3594ed81d874461961cd0faa76"
version = "v1.1.0"
[[projects]]
branch = "master"
name = "github.com/ebuchman/fail-test"
packages = ["."]
revision = "95f809107225be108efcf10a3509e4ea6ceef3c4"
[[projects]]
name = "github.com/go-kit/kit"
packages = [
"log",
"log/level",
"log/term"
]
revision = "4dc7be5d2d12881735283bcab7352178e190fc71"
version = "v0.6.0"
[[projects]]
name = "github.com/go-logfmt/logfmt"
packages = ["."]
revision = "390ab7935ee28ec6b286364bba9b4dd6410cb3d5"
version = "v0.3.0"
[[projects]]
name = "github.com/go-stack/stack"
packages = ["."]
revision = "259ab82a6cad3992b4e21ff5cac294ccb06474bc"
version = "v1.7.0"
[[projects]]
name = "github.com/gogo/protobuf"
packages = [
"gogoproto",
"jsonpb",
"proto",
"protoc-gen-gogo/descriptor",
"sortkeys",
"types"
]
revision = "1adfc126b41513cc696b209667c8656ea7aac67c"
version = "v1.0.0"
[[projects]]
name = "github.com/golang/protobuf"
packages = [
"proto",
"ptypes",
"ptypes/any",
"ptypes/duration",
"ptypes/timestamp"
]
revision = "925541529c1fa6821df4e44ce2723319eb2be768"
version = "v1.0.0"
[[projects]]
branch = "master"
name = "github.com/golang/snappy"
packages = ["."]
revision = "553a641470496b2327abcac10b36396bd98e45c9"
[[projects]]
name = "github.com/gorilla/websocket"
packages = ["."]
revision = "ea4d1f681babbce9545c9c5f3d5194a789c89f5b"
version = "v1.2.0"
[[projects]]
branch = "master"
name = "github.com/jmhodges/levigo"
packages = ["."]
revision = "c42d9e0ca023e2198120196f842701bb4c55d7b9"
[[projects]]
branch = "master"
name = "github.com/kr/logfmt"
packages = ["."]
revision = "b84e30acd515aadc4b783ad4ff83aff3299bdfe0"
[[projects]]
name = "github.com/pkg/errors"
packages = ["."]
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
version = "v0.8.0"
[[projects]]
name = "github.com/pmezard/go-difflib"
packages = ["difflib"]
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
version = "v1.0.0"
[[projects]]
branch = "master"
name = "github.com/rcrowley/go-metrics"
packages = ["."]
revision = "8732c616f52954686704c8645fe1a9d59e9df7c1"
[[projects]]
name = "github.com/stretchr/testify"
packages = [
"assert",
"require"
]
revision = "12b6f73e6084dad08a7c6e575284b177ecafbc71"
version = "v1.2.1"
[[projects]]
branch = "master"
name = "github.com/syndtr/goleveldb"
packages = [
"leveldb",
"leveldb/cache",
"leveldb/comparer",
"leveldb/errors",
"leveldb/filter",
"leveldb/iterator",
"leveldb/journal",
"leveldb/memdb",
"leveldb/opt",
"leveldb/storage",
"leveldb/table",
"leveldb/util"
]
revision = "714f901b98fdb3aa954b4193d8cbd64a28d80cad"
[[projects]]
name = "github.com/tendermint/abci"
packages = [
"client",
"example/code",
"example/kvstore",
"types"
]
revision = "46686763ba8ea595ede16530ed4a40fb38f49f94"
version = "v0.10.2"
[[projects]]
branch = "master"
name = "github.com/tendermint/ed25519"
packages = [
".",
"edwards25519",
"extra25519"
]
revision = "d8387025d2b9d158cf4efb07e7ebf814bcce2057"
[[projects]]
name = "github.com/tendermint/go-crypto"
packages = ["."]
revision = "c3e19f3ea26f5c3357e0bcbb799b0761ef923755"
version = "v0.5.0"
[[projects]]
name = "github.com/tendermint/go-wire"
packages = [
".",
"data"
]
revision = "fa721242b042ecd4c6ed1a934ee740db4f74e45c"
source = "github.com/tendermint/go-amino"
version = "v0.7.3"
[[projects]]
name = "github.com/tendermint/tendermint"
packages = [
"config",
"consensus/types",
"p2p",
"p2p/conn",
"p2p/upnp",
"proxy",
"rpc/core/types",
"rpc/lib/client",
"rpc/lib/server",
"rpc/lib/types",
"state",
"types",
"wire"
]
revision = "6f9956990c444d53f62f2a3905ed410cfe9afe77"
version = "v0.17.1"
[[projects]]
name = "github.com/tendermint/tmlibs"
packages = [
"common",
"db",
"events",
"flowrate",
"log",
"merkle",
"pubsub",
"pubsub/query"
]
revision = "24da7009c3d8c019b40ba4287495749e3160caca"
version = "v0.7.1"
[[projects]]
branch = "master"
name = "golang.org/x/crypto"
packages = [
"curve25519",
"nacl/box",
"nacl/secretbox",
"openpgp/armor",
"openpgp/errors",
"poly1305",
"ripemd160",
"salsa20/salsa"
]
revision = "12892e8c234f4fe6f6803f052061de9057903bb2"
[[projects]]
branch = "master"
name = "golang.org/x/net"
packages = [
"context",
"http2",
"http2/hpack",
"idna",
"internal/timeseries",
"lex/httplex",
"trace"
]
revision = "b68f30494add4df6bd8ef5e82803f308e7f7c59c"
[[projects]]
name = "golang.org/x/text"
packages = [
"collate",
"collate/build",
"internal/colltab",
"internal/gen",
"internal/tag",
"internal/triegen",
"internal/ucd",
"language",
"secure/bidirule",
"transform",
"unicode/bidi",
"unicode/cldr",
"unicode/norm",
"unicode/rangetable"
]
revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0"
version = "v0.3.0"
[[projects]]
branch = "master"
name = "google.golang.org/genproto"
packages = ["googleapis/rpc/status"]
revision = "ab0870e398d5dd054b868c0db1481ab029b9a9f2"
[[projects]]
name = "google.golang.org/grpc"
packages = [
".",
"balancer",
"codes",
"connectivity",
"credentials",
"grpclb/grpc_lb_v1/messages",
"grpclog",
"internal",
"keepalive",
"metadata",
"naming",
"peer",
"resolver",
"stats",
"status",
"tap",
"transport"
]
revision = "5b3c4e850e90a4cf6a20ebd46c8b32a0a3afcb9e"
version = "v1.7.5"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "55767d07c0e1fddf98d5fa04eb583bb6e32d48cd5582f8c141ed79602a5bc75a"
solver-name = "gps-cdcl"
solver-version = 1

+ 54
- 0
tm-monitor/Gopkg.toml View File

@ -0,0 +1,54 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
[[constraint]]
branch = "master"
name = "github.com/rcrowley/go-metrics"
[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.1"
[[constraint]]
name = "github.com/tendermint/go-crypto"
version = "0.5.0"
[[constraint]]
name = "github.com/tendermint/tendermint"
version = "0.17.1"
[[constraint]]
name = "github.com/tendermint/tmlibs"
version = "0.7.1"
[prune]
go-tests = true
unused-packages = true

+ 78
- 10
tm-monitor/Makefile View File

@ -1,26 +1,50 @@
DIST_DIRS := find * -type d -exec
VERSION := $(shell perl -ne '/^var version.*"([^"]+)".*$$/ && print "v$$1\n"' main.go)
GOTOOLS = \
github.com/mitchellh/gox
PACKAGES=$(shell go list ./... | grep -v '/vendor')
github.com/mitchellh/gox \
github.com/golang/dep/cmd/dep \
gopkg.in/alecthomas/gometalinter.v2
PACKAGES=$(shell go list ./... | grep -v '/vendor/')
tools:
go get $(GOTOOLS)
all: check get_vendor_deps build test install metalinter
check: check_tools
########################################
### Tools & dependencies
check_tools:
@# https://stackoverflow.com/a/25668869
@echo "Found tools: $(foreach tool,$(GOTOOLS_CHECK),\
$(if $(shell which $(tool)),$(tool),$(error "No $(tool) in PATH")))"
get_tools:
@echo "--> Installing tools"
go get -u -v $(GOTOOLS)
@gometalinter.v2 --install
update_tools:
@echo "--> Updating tools"
@go get -u $(GOTOOLS)
get_vendor_deps:
@hash glide 2>/dev/null || go get github.com/Masterminds/glide
glide install
@rm -rf vendor/
@echo "--> Running dep ensure"
@dep ensure
########################################
### Build
build:
go build
@go build
install:
go install
@go install
test:
@go test -race $(PACKAGES)
build-all: tools
build-all: check_tools
rm -rf ./dist
gox -verbose \
-ldflags "-s -w" \
@ -36,6 +60,9 @@ dist: build-all
shasum -a256 ./*.tar.gz > "./tm-monitor_${VERSION}_SHA256SUMS" && \
cd ..
########################################
### Docker
build-docker:
rm -f ./tm-monitor
docker run -it --rm -v "$(PWD):/go/src/github.com/tendermint/tools/tm-monitor" -w "/go/src/github.com/tendermint/tools/tm-monitor" -e "CGO_ENABLED=0" golang:alpine go build -ldflags "-s -w" -o tm-monitor
@ -45,4 +72,45 @@ clean:
rm -f ./tm-monitor
rm -rf ./dist
.PHONY: tools get_vendor_deps build install test build-all dist clean build-docker
########################################
### Formatting, linting, and vetting
fmt:
@go fmt ./...
metalinter:
@echo "==> Running linter"
gometalinter.v2 --vendor --deadline=600s --disable-all \
--enable=maligned \
--enable=deadcode \
--enable=goconst \
--enable=goimports \
--enable=gosimple \
--enable=ineffassign \
--enable=megacheck \
--enable=misspell \
--enable=staticcheck \
--enable=safesql \
--enable=structcheck \
--enable=unconvert \
--enable=unused \
--enable=varcheck \
--enable=vetshadow \
./...
#--enable=gas \
#--enable=dupl \
#--enable=errcheck \
#--enable=gocyclo \
#--enable=golint \ <== comments on anything exported
#--enable=gotype \
#--enable=interfacer \
#--enable=unparam \
#--enable=vet \
metalinter_all:
gometalinter.v2 --vendor --deadline=600s --enable-all --disable=lll ./...
# To avoid unintended conflicts with file names, always add to .PHONY
# unless there is a reason not to.
# https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
.PHONY: check check_tools get_tools update_tools get_vendor_deps build install test build-all dist fmt metalinter metalinter_all build-docker clean

+ 74
- 1
tm-monitor/README.rst View File

@ -1 +1,74 @@
NOTE: Please see the ``tm-bench`` directory for the README about tm-monitor. You can also find the documentation at: http://tendermint.readthedocs.io
Monitoring
==========
tm-monitor
----------
Tendermint blockchain monitoring tool; watches over one or more nodes, collecting and providing various statistics to the user: https://github.com/tendermint/tools/tree/master/tm-monitor
Quick Start
^^^^^^^^^^^
Docker
~~~~~~
::
docker run -it --rm -v "/tmp:/tendermint" tendermint/tendermint init
docker run -it --rm -v "/tmp:/tendermint" -p "46657:46657" --name=tm tendermint/tendermint
docker run -it --rm --link=tm tendermint/monitor tm:46657
Binaries
~~~~~~~~
This will be the same as you did for ``tm-bench`` above, except for the last line which should be:
::
tm-monitor localhost:46657
Usage
^^^^^
::
tm-monitor [-v] [-no-ton] [-listen-addr="tcp://0.0.0.0:46670"] [endpoints]
Examples:
# monitor single instance
tm-monitor localhost:46657
# monitor a few instances by providing comma-separated list of RPC endpoints
tm-monitor host1:46657,host2:46657
Flags:
-listen-addr string
HTTP and Websocket server listen address (default "tcp://0.0.0.0:46670")
-no-ton
Do not show ton (table of nodes)
-v verbose logging
RPC UI
^^^^^^
Run ``tm-monitor`` and visit http://localhost:46670
You should see the list of the available RPC endpoints:
::
http://localhost:46670/status
http://localhost:46670/status/network
http://localhost:46670/monitor?endpoint=_
http://localhost:46670/status/node?name=_
http://localhost:46670/unmonitor?endpoint=_
The API is available as GET requests with URI encoded parameters, or as JSONRPC
POST requests. The JSONRPC methods are also exposed over websocket.
Development
^^^^^^^^^^^
::
make get_vendor_deps
make test

+ 0
- 176
tm-monitor/glide.lock View File

@ -1,176 +0,0 @@
hash: defdaf7b594915e7916df4f96d43363c2976ffcb2c35f0dd50b23ec1dd2551fb
updated: 2018-03-27T16:08:55.112172+08:00
imports:
- name: github.com/btcsuite/btcd
version: 2e60448ffcc6bf78332d1fe590260095f554dd78
subpackages:
- btcec
- name: github.com/ebuchman/fail-test
version: 95f809107225be108efcf10a3509e4ea6ceef3c4
- name: github.com/go-kit/kit
version: ca4112baa34cb55091301bdc13b1420a122b1b9e
subpackages:
- log
- log/level
- log/term
- name: github.com/go-logfmt/logfmt
version: 390ab7935ee28ec6b286364bba9b4dd6410cb3d5
- name: github.com/go-stack/stack
version: 259ab82a6cad3992b4e21ff5cac294ccb06474bc
- name: github.com/gogo/protobuf
version: 1adfc126b41513cc696b209667c8656ea7aac67c
subpackages:
- gogoproto
- jsonpb
- proto
- protoc-gen-gogo/descriptor
- sortkeys
- types
- name: github.com/golang/protobuf
version: 925541529c1fa6821df4e44ce2723319eb2be768
subpackages:
- proto
- ptypes
- ptypes/any
- ptypes/duration
- ptypes/timestamp
- name: github.com/golang/snappy
version: 553a641470496b2327abcac10b36396bd98e45c9
- name: github.com/gorilla/websocket
version: ea4d1f681babbce9545c9c5f3d5194a789c89f5b
- name: github.com/jmhodges/levigo
version: c42d9e0ca023e2198120196f842701bb4c55d7b9
- name: github.com/kr/logfmt
version: b84e30acd515aadc4b783ad4ff83aff3299bdfe0
- name: github.com/pkg/errors
version: 645ef00459ed84a119197bfb8d8205042c6df63d
- name: github.com/rcrowley/go-metrics
version: e181e095bae94582363434144c61a9653aff6e50
- name: github.com/syndtr/goleveldb
version: adf24ef3f94bd13ec4163060b21a5678f22b429b
subpackages:
- leveldb
- leveldb/cache
- leveldb/comparer
- leveldb/errors
- leveldb/filter
- leveldb/iterator
- leveldb/journal
- leveldb/memdb
- leveldb/opt
- leveldb/storage
- leveldb/table
- leveldb/util
- name: github.com/tendermint/abci
version: 68592f4d8ee34e97db94b7a7976b1309efdb7eb9
subpackages:
- client
- example/code
- example/dummy
- types
- name: github.com/tendermint/ed25519
version: d8387025d2b9d158cf4efb07e7ebf814bcce2057
subpackages:
- edwards25519
- extra25519
- name: github.com/tendermint/go-crypto
version: dd20358a264c772b4a83e477b0cfce4c88a7001d
- name: github.com/tendermint/go-wire
version: b6fc872b42d41158a60307db4da051dd6f179415
subpackages:
- data
- name: github.com/tendermint/tendermint
version: c8a2bdf78ba7aaaf4284fa78c1b9b05c5e7342bc
subpackages:
- config
- consensus/types
- p2p
- p2p/conn
- p2p/upnp
- proxy
- rpc/core/types
- rpc/lib/client
- rpc/lib/server
- rpc/lib/types
- state
- types
- name: github.com/tendermint/tmlibs
version: 1b9b5652a199ab0be2e781393fb275b66377309d
subpackages:
- common
- db
- events
- flowrate
- log
- merkle
- pubsub
- pubsub/query
- name: golang.org/x/crypto
version: 94eea52f7b742c7cbe0b03b22f0c4c8631ece122
subpackages:
- curve25519
- nacl/box
- nacl/secretbox
- openpgp/armor
- openpgp/errors
- poly1305
- ripemd160
- salsa20/salsa
- name: golang.org/x/net
version: 5ccada7d0a7ba9aeb5d3aca8d3501b4c2a509fec
subpackages:
- context
- http2
- http2/hpack
- idna
- internal/timeseries
- lex/httplex
- trace
- name: golang.org/x/text
version: 57961680700a5336d15015c8c50686ca5ba362a4
subpackages:
- secure/bidirule
- transform
- unicode/bidi
- unicode/norm
- name: google.golang.org/genproto
version: a8101f21cf983e773d0c1133ebc5424792003214
repo: https://github.com/google/go-genproto
vcs: git
subpackages:
- googleapis/rpc/status
- name: google.golang.org/grpc
version: 401e0e00e4bb830a10496d64cd95e068c5bf50de
repo: https://github.com/grpc/grpc-go
vcs: git
subpackages:
- balancer
- codes
- connectivity
- credentials
- grpclb/grpc_lb_v1/messages
- grpclog
- internal
- keepalive
- metadata
- naming
- peer
- resolver
- stats
- status
- tap
- transport
testImports:
- name: github.com/davecgh/go-spew
version: 04cdfd42973bb9c8589fd6a731800cf222fde1a9
subpackages:
- spew
- name: github.com/pmezard/go-difflib
version: d8ed2627bdf02c080bf22230dbb337003b7aba2d
subpackages:
- difflib
- name: github.com/stretchr/testify
version: 2aa2c176b9dab406a6970f6a55f513e8a8c8b18f
subpackages:
- assert
- require

+ 0
- 25
tm-monitor/glide.yaml View File

@ -1,25 +0,0 @@
package: github.com/tendermint/tools/tm-monitor
import:
- package: github.com/gorilla/websocket
- package: github.com/pkg/errors
- package: github.com/rcrowley/go-metrics
- package: github.com/tendermint/go-crypto
- package: github.com/tendermint/tendermint
version: v0.16.0
- package: github.com/tendermint/tmlibs
version: v0.7.0
subpackages:
- common
- events
- log
- package: google.golang.org/grpc
repo: https://github.com/grpc/grpc-go
vcs: git
- package: google.golang.org/genproto
repo: https://github.com/google/go-genproto
vcs: git
testImport:
- package: github.com/stretchr/testify
subpackages:
- assert
- require

+ 21
- 6
tm-monitor/monitor/monitor.go View File

@ -3,6 +3,7 @@ package monitor
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/pkg/errors"
@ -18,7 +19,9 @@ const nodeLivenessTimeout = 5 * time.Second
//
// Common statistics is stored in Network struct.
type Monitor struct {
Nodes []*Node
mtx sync.Mutex
Nodes []*Node
Network *Network
monitorQuit chan struct{} // monitor exitting
@ -75,7 +78,9 @@ func (m *Monitor) SetLogger(l log.Logger) {
// Monitor begins to monitor the node `n`. The node will be started and added
// to the monitor.
func (m *Monitor) Monitor(n *Node) error {
m.mtx.Lock()
m.Nodes = append(m.Nodes, n)
m.mtx.Unlock()
blockCh := make(chan tmtypes.Header, 10)
n.SendBlocksTo(blockCh)
@ -105,13 +110,19 @@ func (m *Monitor) Unmonitor(n *Node) {
close(m.nodeQuit[n.Name])
delete(m.nodeQuit, n.Name)
i, _ := m.NodeByName(n.Name)
m.mtx.Lock()
m.Nodes[i] = m.Nodes[len(m.Nodes)-1]
m.Nodes = m.Nodes[:len(m.Nodes)-1]
m.mtx.Unlock()
}
// NodeByName returns the node and its index if such node exists within the
// monitor. Otherwise, -1 and nil are returned.
func (m *Monitor) NodeByName(name string) (index int, node *Node) {
m.mtx.Lock()
defer m.mtx.Unlock()
for i, n := range m.Nodes {
if name == n.Name {
return i, n
@ -187,18 +198,23 @@ func (m *Monitor) updateNumValidatorLoop() {
var err error
for {
if 0 == len(m.Nodes) {
m.mtx.Lock()
nodesCount := len(m.Nodes)
m.mtx.Unlock()
if 0 == nodesCount {
time.Sleep(m.numValidatorsUpdateInterval)
continue
}
randomNodeIndex := rand.Intn(len(m.Nodes))
randomNodeIndex := rand.Intn(nodesCount)
select {
case <-m.monitorQuit:
return
case <-time.After(m.numValidatorsUpdateInterval):
i := 0
m.mtx.Lock()
for _, n := range m.Nodes {
if i == randomNodeIndex {
height, num, err = n.NumValidators()
@ -209,10 +225,9 @@ func (m *Monitor) updateNumValidatorLoop() {
}
i++
}
m.mtx.Unlock()
if m.Network.Height <= height {
m.Network.NumValidators = num
}
m.Network.UpdateNumValidatorsForHeight(num, height)
}
}
}

+ 6
- 9
tm-monitor/monitor/monitor_test.go View File

@ -15,27 +15,24 @@ import (
)
func TestMonitorUpdatesNumberOfValidators(t *testing.T) {
assert := assert.New(t)
m := startMonitor(t)
defer m.Stop()
n, _ := createValidatorNode(t)
m.Monitor(n)
assert.Equal(1, m.Network.NumNodesMonitored)
assert.Equal(1, m.Network.NumNodesMonitoredOnline)
assert.Equal(t, 1, m.Network.NumNodesMonitored)
assert.Equal(t, 1, m.Network.NumNodesMonitoredOnline)
time.Sleep(1 * time.Second)
assert.Equal(1, m.Network.NumValidators)
// DATA RACE
// assert.Equal(t, 1, m.Network.NumValidators())
}
func TestMonitorRecalculatesNetworkUptime(t *testing.T) {
assert := assert.New(t)
m := startMonitor(t)
defer m.Stop()
assert.Equal(100.0, m.Network.Uptime())
assert.Equal(t, 100.0, m.Network.Uptime())
n, _ := createValidatorNode(t)
m.Monitor(n)
@ -45,7 +42,7 @@ func TestMonitorRecalculatesNetworkUptime(t *testing.T) {
m.Network.NodeIsOnline(n.Name)
time.Sleep(1 * time.Second)
assert.True(m.Network.Uptime() < 100.0, "Uptime should be less than 100%")
assert.True(t, m.Network.Uptime() < 100.0, "Uptime should be less than 100%")
}
func startMonitor(t *testing.T) *monitor.Monitor {


+ 11
- 0
tm-monitor/monitor/network.go View File

@ -164,6 +164,15 @@ func (n *Network) updateHealth() {
}
}
func (n *Network) UpdateNumValidatorsForHeight(num int, height uint64) {
n.mu.Lock()
defer n.mu.Unlock()
if n.Height <= height {
n.NumValidators = num
}
}
func (n *Network) GetHealthString() string {
switch n.Health {
case FullHealth:
@ -179,6 +188,8 @@ func (n *Network) GetHealthString() string {
// Uptime returns network's uptime in percentages.
func (n *Network) Uptime() float64 {
n.mu.Lock()
defer n.mu.Unlock()
return n.UptimeData.Uptime
}


+ 21
- 37
tm-monitor/monitor/network_test.go View File

@ -10,86 +10,70 @@ import (
)
func TestNetworkNewBlock(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
n.NewBlock(tmtypes.Header{Height: 5, NumTxs: 100})
assert.Equal(uint64(5), n.Height)
assert.Equal(0.0, n.AvgBlockTime)
assert.Equal(0.0, n.AvgTxThroughput)
assert.Equal(t, uint64(5), n.Height)
assert.Equal(t, 0.0, n.AvgBlockTime)
assert.Equal(t, 0.0, n.AvgTxThroughput)
}
func TestNetworkNewBlockLatency(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
n.NewBlockLatency(9000000.0) // nanoseconds
assert.Equal(0.0, n.AvgBlockLatency)
assert.Equal(t, 0.0, n.AvgBlockLatency)
}
func TestNetworkNodeIsDownThenOnline(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
n.NewNode("test")
n.NodeIsDown("test")
assert.Equal(0, n.NumNodesMonitoredOnline)
assert.Equal(monitor.Dead, n.Health)
assert.Equal(t, 0, n.NumNodesMonitoredOnline)
assert.Equal(t, monitor.Dead, n.Health)
n.NodeIsDown("test")
assert.Equal(0, n.NumNodesMonitoredOnline)
assert.Equal(t, 0, n.NumNodesMonitoredOnline)
n.NodeIsOnline("test")
assert.Equal(1, n.NumNodesMonitoredOnline)
assert.Equal(monitor.ModerateHealth, n.Health)
assert.Equal(t, 1, n.NumNodesMonitoredOnline)
assert.Equal(t, monitor.ModerateHealth, n.Health)
n.NodeIsOnline("test")
assert.Equal(1, n.NumNodesMonitoredOnline)
assert.Equal(t, 1, n.NumNodesMonitoredOnline)
}
func TestNetworkNewNode(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
assert.Equal(0, n.NumNodesMonitored)
assert.Equal(0, n.NumNodesMonitoredOnline)
assert.Equal(t, 0, n.NumNodesMonitored)
assert.Equal(t, 0, n.NumNodesMonitoredOnline)
n.NewNode("test")
assert.Equal(1, n.NumNodesMonitored)
assert.Equal(1, n.NumNodesMonitoredOnline)
assert.Equal(t, 1, n.NumNodesMonitored)
assert.Equal(t, 1, n.NumNodesMonitoredOnline)
}
func TestNetworkNodeDeleted(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
n.NewNode("test")
n.NodeDeleted("test")
assert.Equal(0, n.NumNodesMonitored)
assert.Equal(0, n.NumNodesMonitoredOnline)
assert.Equal(t, 0, n.NumNodesMonitored)
assert.Equal(t, 0, n.NumNodesMonitoredOnline)
}
func TestNetworkGetHealthString(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
assert.Equal("full", n.GetHealthString())
assert.Equal(t, "full", n.GetHealthString())
n.Health = monitor.ModerateHealth
assert.Equal("moderate", n.GetHealthString())
assert.Equal(t, "moderate", n.GetHealthString())
n.Health = monitor.Dead
assert.Equal("dead", n.GetHealthString())
assert.Equal(t, "dead", n.GetHealthString())
}
func TestNetworkUptime(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
assert.Equal(100.0, n.Uptime())
assert.Equal(t, 100.0, n.Uptime())
}
func TestNetworkStartTime(t *testing.T) {
assert := assert.New(t)
n := monitor.NewNetwork()
assert.True(n.StartTime().Before(time.Now()))
assert.True(t, n.StartTime().Before(time.Now()))
}

+ 3
- 2
tm-monitor/monitor/node.go View File

@ -214,8 +214,9 @@ func (n *Node) checkIsValidator() {
_, validators, err := n.validators()
if err == nil {
for _, v := range validators {
key, err := n.getPubKey()
if err == nil && v.PubKey == key {
key, err1 := n.getPubKey()
// TODO: use bytes.Equal
if err1 == nil && v.PubKey == key {
n.IsValidator = true
}
}


+ 11
- 21
tm-monitor/monitor/node_test.go View File

@ -19,18 +19,14 @@ const (
)
func TestNodeStartStop(t *testing.T) {
assert := assert.New(t)
n, _ := startValidatorNode(t)
defer n.Stop()
assert.Equal(true, n.Online)
assert.Equal(true, n.IsValidator)
assert.Equal(t, true, n.Online)
assert.Equal(t, true, n.IsValidator)
}
func TestNodeNewBlockReceived(t *testing.T) {
assert := assert.New(t)
blockCh := make(chan tmtypes.Header, 100)
n, emMock := startValidatorNode(t)
defer n.Stop()
@ -39,13 +35,11 @@ func TestNodeNewBlockReceived(t *testing.T) {
blockHeader := &tmtypes.Header{Height: 5}
emMock.Call("eventCallback", &em.EventMetric{}, tmtypes.TMEventData{tmtypes.EventDataNewBlockHeader{blockHeader}})
assert.Equal(uint64(5), n.Height)
assert.Equal(*blockHeader, <-blockCh)
assert.Equal(t, uint64(5), n.Height)
assert.Equal(t, *blockHeader, <-blockCh)
}
func TestNodeNewBlockLatencyReceived(t *testing.T) {
assert := assert.New(t)
blockLatencyCh := make(chan float64, 100)
n, emMock := startValidatorNode(t)
defer n.Stop()
@ -53,13 +47,11 @@ func TestNodeNewBlockLatencyReceived(t *testing.T) {
emMock.Call("latencyCallback", 1000000.0)
assert.Equal(1.0, n.BlockLatency)
assert.Equal(1000000.0, <-blockLatencyCh)
assert.Equal(t, 1.0, n.BlockLatency)
assert.Equal(t, 1000000.0, <-blockLatencyCh)
}
func TestNodeConnectionLost(t *testing.T) {
assert := assert.New(t)
disconnectCh := make(chan bool, 100)
n, emMock := startValidatorNode(t)
defer n.Stop()
@ -67,20 +59,18 @@ func TestNodeConnectionLost(t *testing.T) {
emMock.Call("disconnectCallback")
assert.Equal(true, <-disconnectCh)
assert.Equal(false, n.Online)
assert.Equal(t, true, <-disconnectCh)
assert.Equal(t, false, n.Online)
}
func TestNumValidators(t *testing.T) {
assert := assert.New(t)
n, _ := startValidatorNode(t)
defer n.Stop()
height, num, err := n.NumValidators()
assert.Nil(err)
assert.Equal(uint64(blockHeight), height)
assert.Equal(1, num)
assert.Nil(t, err)
assert.Equal(t, uint64(blockHeight), height)
assert.Equal(t, 1, num)
}
func startValidatorNode(t *testing.T) (n *monitor.Node, emMock *mock.EventMeter) {


Loading…
Cancel
Save