|
3 | 3 | import com.codahale.metrics.Counter; |
4 | 4 | import com.google.protobuf.ByteString; |
5 | 5 | import java.util.ArrayList; |
| 6 | +import java.util.HashSet; |
6 | 7 | import java.util.List; |
7 | 8 | import java.util.Map; |
| 9 | +import java.util.Set; |
8 | 10 | import java.util.SortedMap; |
9 | 11 | import java.util.concurrent.ConcurrentHashMap; |
| 12 | +import java.util.stream.Collectors; |
10 | 13 | import lombok.Getter; |
11 | 14 | import lombok.Setter; |
12 | 15 | import org.bouncycastle.util.encoders.Hex; |
@@ -42,6 +45,7 @@ public class BlockChainMetricManager { |
42 | 45 | private long failProcessBlockNum = 0; |
43 | 46 | @Setter |
44 | 47 | private String failProcessBlockReason = ""; |
| 48 | + private final Set<String> lastActiveWitnesses = ConcurrentHashMap.newKeySet(); |
45 | 49 |
|
46 | 50 | public BlockChainInfo getBlockChainInfo() { |
47 | 51 | BlockChainInfo blockChainInfo = new BlockChainInfo(); |
@@ -168,6 +172,45 @@ public void applyBlock(BlockCapsule block) { |
168 | 172 | MetricsUtil.meterMark(MetricsKey.BLOCKCHAIN_TPS, block.getTransactions().size()); |
169 | 173 | Metrics.counterInc(MetricKeys.Counter.TXS, block.getTransactions().size(), |
170 | 174 | MetricLabels.Counter.TXS_SUCCESS, MetricLabels.Counter.TXS_SUCCESS); |
| 175 | + } else { |
| 176 | + // Empty block |
| 177 | + Metrics.counterInc(MetricKeys.Counter.BLOCK_EMPTY, 1, |
| 178 | + StringUtil.encode58Check(address)); |
| 179 | + } |
| 180 | + |
| 181 | + // SR set change detection |
| 182 | + Set<String> currentWitnesses = chainBaseManager.getWitnessScheduleStore().getActiveWitnesses() |
| 183 | + .stream() |
| 184 | + .map(w -> Hex.toHexString(w.toByteArray())) |
| 185 | + .collect(Collectors.toSet()); |
| 186 | + recordSrSetChange(currentWitnesses); |
| 187 | + } |
| 188 | + |
| 189 | + private void recordSrSetChange(Set<String> currentWitnesses) { |
| 190 | + if (currentWitnesses.isEmpty()) { |
| 191 | + return; |
| 192 | + } |
| 193 | + if (lastActiveWitnesses.isEmpty()) { |
| 194 | + lastActiveWitnesses.addAll(currentWitnesses); |
| 195 | + return; |
| 196 | + } |
| 197 | + Set<String> added = new HashSet<>(currentWitnesses); |
| 198 | + added.removeAll(lastActiveWitnesses); |
| 199 | + |
| 200 | + Set<String> removed = new HashSet<>(lastActiveWitnesses); |
| 201 | + removed.removeAll(currentWitnesses); |
| 202 | + |
| 203 | + for (String address : added) { |
| 204 | + Metrics.counterInc(MetricKeys.Counter.SR_SET_CHANGE, 1, |
| 205 | + MetricLabels.Counter.SR_ADD, StringUtil.encode58Check(Hex.decode(address))); |
| 206 | + } |
| 207 | + for (String address : removed) { |
| 208 | + Metrics.counterInc(MetricKeys.Counter.SR_SET_CHANGE, 1, |
| 209 | + MetricLabels.Counter.SR_REMOVE, StringUtil.encode58Check(Hex.decode(address))); |
| 210 | + } |
| 211 | + if (!added.isEmpty() || !removed.isEmpty()) { |
| 212 | + lastActiveWitnesses.clear(); |
| 213 | + lastActiveWitnesses.addAll(currentWitnesses); |
171 | 214 | } |
172 | 215 | } |
173 | 216 |
|
|
0 commit comments