|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/Fantom-foundation/Norma/driver" |
| 6 | + "github.com/Fantom-foundation/Norma/driver/monitoring" |
| 7 | + netmon "github.com/Fantom-foundation/Norma/driver/monitoring/network" |
| 8 | + nodemon "github.com/Fantom-foundation/Norma/driver/monitoring/node" |
| 9 | + "github.com/Fantom-foundation/Norma/driver/network/local" |
| 10 | + "golang.org/x/exp/constraints" |
| 11 | + "log" |
| 12 | + "sort" |
| 13 | + "sync" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +// progressLogger is a helper struct that logs the progress of the network. |
| 18 | +// It lists nodes and logs the progress of the network periodically. |
| 19 | +type progressLogger struct { |
| 20 | + monitor *monitoring.Monitor |
| 21 | + stop chan<- bool |
| 22 | + done <-chan bool |
| 23 | +} |
| 24 | + |
| 25 | +// startProgressLogger starts a progress logger that logs the progress of the network. |
| 26 | +func startProgressLogger(monitor *monitoring.Monitor, net *local.LocalNetwork) *progressLogger { |
| 27 | + stop := make(chan bool) |
| 28 | + done := make(chan bool) |
| 29 | + |
| 30 | + activeNodes := &activeNodes{ |
| 31 | + data: make(map[driver.NodeID]struct{}), |
| 32 | + } |
| 33 | + net.RegisterListener(activeNodes) |
| 34 | + for _, node := range net.GetActiveNodes() { |
| 35 | + activeNodes.AfterNodeCreation(node) |
| 36 | + } |
| 37 | + |
| 38 | + go func() { |
| 39 | + defer close(done) |
| 40 | + ticker := time.NewTicker(time.Second) |
| 41 | + for { |
| 42 | + select { |
| 43 | + case <-stop: |
| 44 | + return |
| 45 | + case <-ticker.C: |
| 46 | + logState(monitor, activeNodes) |
| 47 | + } |
| 48 | + } |
| 49 | + }() |
| 50 | + |
| 51 | + return &progressLogger{ |
| 52 | + monitor, |
| 53 | + stop, |
| 54 | + done, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +type activeNodes struct { |
| 59 | + data map[driver.NodeID]struct{} |
| 60 | + mutex sync.Mutex |
| 61 | +} |
| 62 | + |
| 63 | +func (l *activeNodes) AfterNodeCreation(node driver.Node) { |
| 64 | + l.mutex.Lock() |
| 65 | + defer l.mutex.Unlock() |
| 66 | + l.data[driver.NodeID(node.GetLabel())] = struct{}{} |
| 67 | +} |
| 68 | + |
| 69 | +func (l *activeNodes) AfterNodeRemoval(node driver.Node) { |
| 70 | + l.mutex.Lock() |
| 71 | + defer l.mutex.Unlock() |
| 72 | + delete(l.data, driver.NodeID(node.GetLabel())) |
| 73 | +} |
| 74 | + |
| 75 | +func (l *activeNodes) AfterApplicationCreation(app driver.Application) { |
| 76 | + // noop |
| 77 | +} |
| 78 | + |
| 79 | +func (l *activeNodes) containsId(id driver.NodeID) bool { |
| 80 | + l.mutex.Lock() |
| 81 | + defer l.mutex.Unlock() |
| 82 | + |
| 83 | + _, exists := l.data[id] |
| 84 | + return exists |
| 85 | +} |
| 86 | + |
| 87 | +func (l *progressLogger) shutdown() { |
| 88 | + close(l.stop) |
| 89 | + <-l.done |
| 90 | +} |
| 91 | + |
| 92 | +func logState(monitor *monitoring.Monitor, nodes *activeNodes) { |
| 93 | + numNodes := getNumNodes(monitor) |
| 94 | + blockStatuses := getBlockStatuses(monitor, nodes) |
| 95 | + txPers := getTxPerSec(monitor, nodes) |
| 96 | + txs := getNumTxs(monitor) |
| 97 | + gas := getGasUsed(monitor) |
| 98 | + processingTimes := getBlockProcessingTimes(monitor, nodes) |
| 99 | + |
| 100 | + log.Printf("Nodes: %s, block heights: %v, tx/s: %v, txs: %v, gas: %s, block processing: %v", numNodes, blockStatuses, txPers, txs, gas, processingTimes) |
| 101 | +} |
| 102 | + |
| 103 | +func getNumNodes(monitor *monitoring.Monitor) string { |
| 104 | + data, exists := monitoring.GetData(monitor, monitoring.Network{}, netmon.NumberOfNodes) |
| 105 | + return getLastValAsString[monitoring.Time, int](exists, data) |
| 106 | +} |
| 107 | + |
| 108 | +func getNumTxs(monitor *monitoring.Monitor) string { |
| 109 | + data, exists := monitoring.GetData(monitor, monitoring.Network{}, netmon.BlockNumberOfTransactions) |
| 110 | + return getLastValAsString[monitoring.BlockNumber, int](exists, data) |
| 111 | +} |
| 112 | + |
| 113 | +func getTxPerSec(monitor *monitoring.Monitor, nodes *activeNodes) []string { |
| 114 | + metric := nodemon.TransactionsThroughput |
| 115 | + return getLastValAllSubjects[monitoring.BlockNumber, float32](monitor, metric, nodes) |
| 116 | +} |
| 117 | + |
| 118 | +func getGasUsed(monitor *monitoring.Monitor) string { |
| 119 | + data, exists := monitoring.GetData(monitor, monitoring.Network{}, netmon.BlockGasUsed) |
| 120 | + return getLastValAsString[monitoring.BlockNumber, int](exists, data) |
| 121 | +} |
| 122 | + |
| 123 | +func getBlockStatuses(monitor *monitoring.Monitor, nodes *activeNodes) []string { |
| 124 | + metric := nodemon.NodeBlockStatus |
| 125 | + return getLastValAllSubjects[ |
| 126 | + monitoring.Time, |
| 127 | + monitoring.BlockStatus, |
| 128 | + monitoring.Series[monitoring.Time, monitoring.BlockStatus]]( |
| 129 | + monitor, metric, nodes) |
| 130 | +} |
| 131 | + |
| 132 | +func getBlockProcessingTimes(monitor *monitoring.Monitor, nodes *activeNodes) []string { |
| 133 | + metric := nodemon.BlockEventAndTxsProcessingTime |
| 134 | + return getLastValAllSubjects[ |
| 135 | + monitoring.BlockNumber, |
| 136 | + time.Duration, |
| 137 | + monitoring.Series[monitoring.BlockNumber, time.Duration]]( |
| 138 | + monitor, metric, nodes) |
| 139 | +} |
| 140 | + |
| 141 | +func getLastValAllSubjects[K constraints.Ordered, T any, X monitoring.Series[K, T]]( |
| 142 | + monitor *monitoring.Monitor, |
| 143 | + metric monitoring.Metric[monitoring.Node, X], |
| 144 | + activeNodes *activeNodes) []string { |
| 145 | + |
| 146 | + nodes := monitoring.GetSubjects(monitor, metric) |
| 147 | + sort.Slice(nodes, func(i, j int) bool { return nodes[i] < nodes[j] }) |
| 148 | + |
| 149 | + res := make([]string, 0, len(nodes)) |
| 150 | + for _, node := range nodes { |
| 151 | + if exists := activeNodes.containsId(driver.NodeID(node)); exists { |
| 152 | + data, exists := monitoring.GetData(monitor, node, metric) |
| 153 | + d := getLastValAsString[K, T](exists, data) |
| 154 | + res = append(res, d) |
| 155 | + } |
| 156 | + } |
| 157 | + return res |
| 158 | +} |
| 159 | + |
| 160 | +func getLastValAsString[K constraints.Ordered, T any](exists bool, series monitoring.Series[K, T]) string { |
| 161 | + if !exists || series == nil { |
| 162 | + return "N/A" |
| 163 | + } |
| 164 | + point := series.GetLatest() |
| 165 | + if point == nil { |
| 166 | + return "N/A" |
| 167 | + } |
| 168 | + return fmt.Sprintf("%v", point.Value) |
| 169 | +} |
0 commit comments