|
| 1 | +package org.tron.common.prometheus; |
| 2 | + |
| 3 | +import com.google.protobuf.ByteString; |
| 4 | +import io.prometheus.client.CollectorRegistry; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.concurrent.atomic.AtomicInteger; |
| 8 | +import javax.annotation.Resource; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Assert; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.tron.common.BaseTest; |
| 15 | +import org.tron.common.TestConstants; |
| 16 | +import org.tron.common.utils.StringUtil; |
| 17 | +import org.tron.consensus.dpos.MaintenanceManager; |
| 18 | +import org.tron.core.capsule.AccountCapsule; |
| 19 | +import org.tron.core.capsule.VotesCapsule; |
| 20 | +import org.tron.core.capsule.WitnessCapsule; |
| 21 | +import org.tron.core.config.args.Args; |
| 22 | +import org.tron.core.consensus.ConsensusService; |
| 23 | +import org.tron.protos.Protocol; |
| 24 | +import org.tron.protos.Protocol.Vote; |
| 25 | + |
| 26 | +@Slf4j(topic = "metric") |
| 27 | +public class SRMetricsTest extends BaseTest { |
| 28 | + |
| 29 | + private static final AtomicInteger PORT = new AtomicInteger(0); |
| 30 | + private static final AtomicInteger UNIQUE = new AtomicInteger(0); |
| 31 | + |
| 32 | + @Resource |
| 33 | + private MaintenanceManager maintenanceManager; |
| 34 | + @Resource |
| 35 | + private ConsensusService consensusService; |
| 36 | + |
| 37 | + static { |
| 38 | + Args.setParam(new String[]{"-d", dbPath()}, TestConstants.TEST_CONF); |
| 39 | + Args.getInstance().setNodeListenPort(20000 + PORT.incrementAndGet()); |
| 40 | + Args.getInstance().setMetricsPrometheusEnable(true); |
| 41 | + Metrics.init(); |
| 42 | + } |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setUp() { |
| 46 | + Args.getInstance().setMetricsPrometheusEnable(true); |
| 47 | + consensusService.start(); |
| 48 | + } |
| 49 | + |
| 50 | + @After |
| 51 | + public void tearDown() { |
| 52 | + Args.getInstance().setMetricsPrometheusEnable(true); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Drive the full maintenance flow: starting with a single active witness while WitnessStore |
| 57 | + * contains additional ones, doMaintenance() should expand active witnesses to the full set and |
| 58 | + * emit SR_ADD for each newly active witness. |
| 59 | + */ |
| 60 | + @Test |
| 61 | + public void testSrAddViaMaintenance() { |
| 62 | + ByteString stableWit = registerWitness(); |
| 63 | + ByteString newWit1 = registerWitness(); |
| 64 | + ByteString newWit2 = registerWitness(); |
| 65 | + |
| 66 | + chainBaseManager.getWitnessScheduleStore() |
| 67 | + .saveActiveWitnesses(Collections.singletonList(stableWit)); |
| 68 | + |
| 69 | + seedVote(stableWit); |
| 70 | + |
| 71 | + maintenanceManager.doMaintenance(); |
| 72 | + |
| 73 | + Assert.assertEquals(1, sample(MetricLabels.Counter.SR_ADD, newWit1).intValue()); |
| 74 | + Assert.assertEquals(1, sample(MetricLabels.Counter.SR_ADD, newWit2).intValue()); |
| 75 | + Assert.assertNull(sample(MetricLabels.Counter.SR_ADD, stableWit)); |
| 76 | + Assert.assertNull(sample(MetricLabels.Counter.SR_REMOVE, stableWit)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Active witness set already matches WitnessStore → no metric emitted. |
| 81 | + */ |
| 82 | + @Test |
| 83 | + public void testNoMetricWhenSetUnchanged() { |
| 84 | + ByteString witA = registerWitness(); |
| 85 | + ByteString witB = registerWitness(); |
| 86 | + |
| 87 | + chainBaseManager.getWitnessScheduleStore() |
| 88 | + .saveActiveWitnesses(Arrays.asList(witA, witB)); |
| 89 | + |
| 90 | + seedVote(witA); |
| 91 | + |
| 92 | + maintenanceManager.doMaintenance(); |
| 93 | + |
| 94 | + Assert.assertNull(sample(MetricLabels.Counter.SR_ADD, witA)); |
| 95 | + Assert.assertNull(sample(MetricLabels.Counter.SR_ADD, witB)); |
| 96 | + Assert.assertNull(sample(MetricLabels.Counter.SR_REMOVE, witA)); |
| 97 | + Assert.assertNull(sample(MetricLabels.Counter.SR_REMOVE, witB)); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Empty VotesStore → countVote() is empty → SR change check is skipped, even when the active |
| 102 | + * set differs from the full witness store. |
| 103 | + */ |
| 104 | + @Test |
| 105 | + public void testNoMetricWhenNoVotes() { |
| 106 | + ByteString stableWit = registerWitness(); |
| 107 | + ByteString newWit = registerWitness(); |
| 108 | + |
| 109 | + chainBaseManager.getWitnessScheduleStore() |
| 110 | + .saveActiveWitnesses(Collections.singletonList(stableWit)); |
| 111 | + |
| 112 | + maintenanceManager.doMaintenance(); |
| 113 | + |
| 114 | + Assert.assertNull(sample(MetricLabels.Counter.SR_ADD, newWit)); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Metrics disabled → record() short-circuits even though the active set changes. |
| 119 | + */ |
| 120 | + @Test |
| 121 | + public void testNoMetricWhenMetricsDisabled() { |
| 122 | + Args.getInstance().setMetricsPrometheusEnable(false); |
| 123 | + try { |
| 124 | + ByteString stableWit = registerWitness(); |
| 125 | + ByteString newWit = registerWitness(); |
| 126 | + |
| 127 | + chainBaseManager.getWitnessScheduleStore() |
| 128 | + .saveActiveWitnesses(Collections.singletonList(stableWit)); |
| 129 | + |
| 130 | + seedVote(stableWit); |
| 131 | + |
| 132 | + maintenanceManager.doMaintenance(); |
| 133 | + |
| 134 | + Assert.assertNull(sample(MetricLabels.Counter.SR_ADD, newWit)); |
| 135 | + } finally { |
| 136 | + Args.getInstance().setMetricsPrometheusEnable(true); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * SR_REMOVE is verified by directly calling record() instead of going through doMaintenance(), |
| 142 | + * because driving a removal through the real flow is impractical here: |
| 143 | + * |
| 144 | + * <p>Inside doMaintenance(), the block before SRMetrics.recordSrSetChange() iterates currentWits |
| 145 | + * and calls setIsJobs(false) on each WitnessCapsule fetched from WitnessStore. If currentWits |
| 146 | + * contains any address that is not present in WitnessStore, getWitness() returns null and the |
| 147 | + * code NPEs — so SR_REMOVE cannot be triggered by simply pointing the active set at an |
| 148 | + * "obsolete" address. |
| 149 | + * |
| 150 | + * <p>The only other path to SR_REMOVE is rank-based eviction: with more than |
| 151 | + * MAX_ACTIVE_WITNESS_NUM (27) witnesses, sorting drops the lowest-ranked one. Building that |
| 152 | + * setup just to exercise this branch is heavy and adds little value, since SR_ADD and |
| 153 | + * SR_REMOVE share the exact same emit logic in record() — verifying SR_ADD via doMaintenance |
| 154 | + * already proves the wiring is correct, and this direct call covers the symmetric branch. |
| 155 | + */ |
| 156 | + @Test |
| 157 | + public void testSrRemoveDirect() { |
| 158 | + ByteString stableWit = uniqueAddress(); |
| 159 | + ByteString removedWit = uniqueAddress(); |
| 160 | + |
| 161 | + SRMetrics.recordSrSetChange( |
| 162 | + Arrays.asList(stableWit, removedWit), |
| 163 | + Collections.singletonList(stableWit)); |
| 164 | + |
| 165 | + Assert.assertEquals(1, sample(MetricLabels.Counter.SR_REMOVE, removedWit).intValue()); |
| 166 | + Assert.assertNull(sample(MetricLabels.Counter.SR_ADD, removedWit)); |
| 167 | + Assert.assertNull(sample(MetricLabels.Counter.SR_REMOVE, stableWit)); |
| 168 | + } |
| 169 | + |
| 170 | + private ByteString registerWitness() { |
| 171 | + ByteString address = uniqueAddress(); |
| 172 | + chainBaseManager.getWitnessStore().put(address.toByteArray(), new WitnessCapsule(address)); |
| 173 | + chainBaseManager.addWitness(address); |
| 174 | + chainBaseManager.getAccountStore().put(address.toByteArray(), |
| 175 | + new AccountCapsule(Protocol.Account.newBuilder().setAddress(address).build())); |
| 176 | + return address; |
| 177 | + } |
| 178 | + |
| 179 | + private void seedVote(ByteString voteFor) { |
| 180 | + ByteString voter = uniqueAddress(); |
| 181 | + VotesCapsule votes = new VotesCapsule(voter, Collections.emptyList(), |
| 182 | + Collections.singletonList(Vote.newBuilder() |
| 183 | + .setVoteAddress(voteFor) |
| 184 | + .setVoteCount(1L) |
| 185 | + .build())); |
| 186 | + chainBaseManager.getVotesStore().put(voter.toByteArray(), votes); |
| 187 | + } |
| 188 | + |
| 189 | + private ByteString uniqueAddress() { |
| 190 | + int n = UNIQUE.incrementAndGet(); |
| 191 | + byte[] bytes = new byte[21]; |
| 192 | + bytes[0] = 0x41; |
| 193 | + bytes[17] = (byte) ((n >> 16) & 0xFF); |
| 194 | + bytes[18] = (byte) ((n >> 8) & 0xFF); |
| 195 | + bytes[19] = (byte) (n & 0xFF); |
| 196 | + bytes[20] = 0x01; |
| 197 | + return ByteString.copyFrom(bytes); |
| 198 | + } |
| 199 | + |
| 200 | + private Double sample(String action, ByteString witness) { |
| 201 | + return CollectorRegistry.defaultRegistry.getSampleValue( |
| 202 | + MetricKeys.Counter.SR_SET_CHANGE + "_total", |
| 203 | + new String[]{"action", "witness"}, |
| 204 | + new String[]{action, StringUtil.encode58Check(witness.toByteArray())}); |
| 205 | + } |
| 206 | +} |
0 commit comments