|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.stats; |
| 20 | + |
| 21 | +import com.google.common.collect.Multimap; |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.UUID; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import lombok.Cleanup; |
| 27 | +import org.apache.pulsar.broker.authentication.AuthenticationProviderToken; |
| 28 | +import org.apache.pulsar.broker.service.BrokerTestBase; |
| 29 | +import org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsGenerator; |
| 30 | +import org.apache.pulsar.client.api.Consumer; |
| 31 | +import org.apache.pulsar.client.api.Message; |
| 32 | +import org.apache.pulsar.client.api.Producer; |
| 33 | +import org.apache.pulsar.client.api.Schema; |
| 34 | +import org.apache.pulsar.metadata.api.MetadataStoreConfig; |
| 35 | +import org.testng.Assert; |
| 36 | +import org.testng.annotations.AfterMethod; |
| 37 | +import org.testng.annotations.BeforeMethod; |
| 38 | +import org.testng.annotations.Test; |
| 39 | + |
| 40 | + |
| 41 | +@Test(groups = "broker") |
| 42 | +public class MetadataStoreStatsTest extends BrokerTestBase { |
| 43 | + |
| 44 | + @BeforeMethod(alwaysRun = true) |
| 45 | + @Override |
| 46 | + protected void setup() throws Exception { |
| 47 | + conf.setTopicLevelPoliciesEnabled(false); |
| 48 | + conf.setSystemTopicEnabled(false); |
| 49 | + super.baseSetup(); |
| 50 | + AuthenticationProviderToken.resetMetrics(); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterMethod(alwaysRun = true) |
| 54 | + @Override |
| 55 | + protected void cleanup() throws Exception { |
| 56 | + super.internalCleanup(); |
| 57 | + resetConfig(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testMetadataStoreStats() throws Exception { |
| 62 | + String ns = "prop/ns-abc1"; |
| 63 | + admin.namespaces().createNamespace(ns); |
| 64 | + |
| 65 | + String topic = "persistent://prop/ns-abc1/metadata-store-" + UUID.randomUUID(); |
| 66 | + String subName = "my-sub1"; |
| 67 | + |
| 68 | + @Cleanup |
| 69 | + Producer<String> producer = pulsarClient.newProducer(Schema.STRING) |
| 70 | + .topic(topic).create(); |
| 71 | + @Cleanup |
| 72 | + Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING) |
| 73 | + .topic(topic).subscriptionName(subName).subscribe(); |
| 74 | + |
| 75 | + for (int i = 0; i < 100; i++) { |
| 76 | + producer.newMessage().value(UUID.randomUUID().toString()).send(); |
| 77 | + } |
| 78 | + |
| 79 | + for (;;) { |
| 80 | + Message<String> message = consumer.receive(10, TimeUnit.SECONDS); |
| 81 | + if (message == null) { |
| 82 | + break; |
| 83 | + } |
| 84 | + consumer.acknowledge(message); |
| 85 | + } |
| 86 | + |
| 87 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 88 | + PrometheusMetricsGenerator.generate(pulsar, false, false, false, false, output); |
| 89 | + String metricsStr = output.toString(); |
| 90 | + Multimap<String, PrometheusMetricsTest.Metric> metricsMap = PrometheusMetricsTest.parseMetrics(metricsStr); |
| 91 | + |
| 92 | + Collection<PrometheusMetricsTest.Metric> opsLatency = metricsMap.get("pulsar_metadata_store_ops_latency_ms" + "_sum"); |
| 93 | + Collection<PrometheusMetricsTest.Metric> putBytes = metricsMap.get("pulsar_metadata_store_put_bytes" + "_total"); |
| 94 | + |
| 95 | + Assert.assertTrue(opsLatency.size() > 1); |
| 96 | + Assert.assertTrue(putBytes.size() > 1); |
| 97 | + |
| 98 | + for (PrometheusMetricsTest.Metric m : opsLatency) { |
| 99 | + Assert.assertEquals(m.tags.get("cluster"), "test"); |
| 100 | + String metadataStoreName = m.tags.get("name"); |
| 101 | + Assert.assertNotNull(metadataStoreName); |
| 102 | + Assert.assertTrue(metadataStoreName.equals(MetadataStoreConfig.METADATA_STORE) |
| 103 | + || metadataStoreName.equals(MetadataStoreConfig.CONFIGURATION_METADATA_STORE) |
| 104 | + || metadataStoreName.equals(MetadataStoreConfig.STATE_METADATA_STORE)); |
| 105 | + Assert.assertNotNull(m.tags.get("status")); |
| 106 | + |
| 107 | + if (m.tags.get("status").equals("success")) { |
| 108 | + if (m.tags.get("type").equals("get")) { |
| 109 | + Assert.assertTrue(m.value >= 0); |
| 110 | + } else if (m.tags.get("type").equals("del")) { |
| 111 | + Assert.assertTrue(m.value >= 0); |
| 112 | + } else if (m.tags.get("type").equals("put")) { |
| 113 | + Assert.assertTrue(m.value >= 0); |
| 114 | + } else { |
| 115 | + Assert.fail(); |
| 116 | + } |
| 117 | + } else { |
| 118 | + if (m.tags.get("type").equals("get")) { |
| 119 | + Assert.assertTrue(m.value >= 0); |
| 120 | + } else if (m.tags.get("type").equals("del")) { |
| 121 | + Assert.assertTrue(m.value >= 0); |
| 122 | + } else if (m.tags.get("type").equals("put")) { |
| 123 | + Assert.assertTrue(m.value >= 0); |
| 124 | + } else { |
| 125 | + Assert.fail(); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + for (PrometheusMetricsTest.Metric m : putBytes) { |
| 130 | + Assert.assertEquals(m.tags.get("cluster"), "test"); |
| 131 | + String metadataStoreName = m.tags.get("name"); |
| 132 | + Assert.assertNotNull(metadataStoreName); |
| 133 | + Assert.assertTrue(metadataStoreName.equals(MetadataStoreConfig.METADATA_STORE) |
| 134 | + || metadataStoreName.equals(MetadataStoreConfig.CONFIGURATION_METADATA_STORE) |
| 135 | + || metadataStoreName.equals(MetadataStoreConfig.STATE_METADATA_STORE)); |
| 136 | + Assert.assertTrue(m.value > 0); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments