|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.runners.core.metrics; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | +import org.apache.beam.model.pipeline.v1.MetricsApi; |
| 28 | +import org.apache.beam.sdk.values.KV; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +public class ShortIdMapTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testShortIdAssignment() throws Exception { |
| 35 | + ShortIdMap shortIdMap = new ShortIdMap(); |
| 36 | + List<KV<String, MetricsApi.MonitoringInfo>> testCases = new ArrayList<>(); |
| 37 | + |
| 38 | + SimpleMonitoringInfoBuilder builder = new SimpleMonitoringInfoBuilder(false); |
| 39 | + builder.setUrn(MonitoringInfoConstants.Urns.USER_DISTRIBUTION_INT64); |
| 40 | + testCases.add(KV.of("metric0", builder.build())); |
| 41 | + |
| 42 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 43 | + builder.setUrn(MonitoringInfoConstants.Urns.ELEMENT_COUNT); |
| 44 | + testCases.add(KV.of("metric1", builder.build())); |
| 45 | + |
| 46 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 47 | + builder.setUrn(MonitoringInfoConstants.Urns.USER_DISTRIBUTION_DOUBLE); |
| 48 | + testCases.add(KV.of("metric2", builder.build())); |
| 49 | + |
| 50 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 51 | + builder.setUrn("TestingSentinelUrn"); |
| 52 | + builder.setType("TestingSentinelType"); |
| 53 | + testCases.add(KV.of("metric3", builder.build())); |
| 54 | + |
| 55 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 56 | + builder.setUrn(MonitoringInfoConstants.Urns.FINISH_BUNDLE_MSECS); |
| 57 | + testCases.add(KV.of("metric4", builder.build())); |
| 58 | + |
| 59 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 60 | + builder.setUrn(MonitoringInfoConstants.Urns.USER_SUM_INT64); |
| 61 | + testCases.add(KV.of("metric5", builder.build())); |
| 62 | + |
| 63 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 64 | + builder.setUrn(MonitoringInfoConstants.Urns.USER_SUM_INT64); |
| 65 | + builder.setLabel(MonitoringInfoConstants.Labels.NAME, "metricNumber7"); |
| 66 | + builder.setLabel(MonitoringInfoConstants.Labels.NAMESPACE, "myNamespace"); |
| 67 | + builder.setLabel(MonitoringInfoConstants.Labels.PTRANSFORM, "myPtransform"); |
| 68 | + testCases.add(KV.of("metric6", builder.build())); |
| 69 | + |
| 70 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 71 | + builder.setUrn(MonitoringInfoConstants.Urns.USER_SUM_INT64); |
| 72 | + builder.setLabel(MonitoringInfoConstants.Labels.NAME, "metricNumber8"); |
| 73 | + builder.setLabel(MonitoringInfoConstants.Labels.NAMESPACE, "myNamespace"); |
| 74 | + builder.setLabel(MonitoringInfoConstants.Labels.PTRANSFORM, "myPtransform"); |
| 75 | + testCases.add(KV.of("metric7", builder.build())); |
| 76 | + |
| 77 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 78 | + builder.setUrn(MonitoringInfoConstants.Urns.API_REQUEST_COUNT); |
| 79 | + builder.setLabel(MonitoringInfoConstants.Labels.SERVICE, "BigQuery"); |
| 80 | + testCases.add(KV.of("metric8", builder.build())); |
| 81 | + |
| 82 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 83 | + builder.setUrn(MonitoringInfoConstants.Urns.API_REQUEST_COUNT); |
| 84 | + builder.setLabel(MonitoringInfoConstants.Labels.SERVICE, "Storage"); |
| 85 | + testCases.add(KV.of("metric9", builder.build())); |
| 86 | + |
| 87 | + // Validate that modifying the payload, but using the same URN/labels |
| 88 | + // does not change the shortId assignment. |
| 89 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 90 | + builder.setUrn(MonitoringInfoConstants.Urns.USER_DISTRIBUTION_DOUBLE); |
| 91 | + testCases.add(KV.of("metric2", builder.build())); |
| 92 | + |
| 93 | + builder = new SimpleMonitoringInfoBuilder(false); |
| 94 | + builder.setUrn(MonitoringInfoConstants.Urns.USER_SUM_INT64); |
| 95 | + builder.setLabel(MonitoringInfoConstants.Labels.NAME, "metricNumber7"); |
| 96 | + builder.setLabel(MonitoringInfoConstants.Labels.NAMESPACE, "myNamespace"); |
| 97 | + builder.setLabel(MonitoringInfoConstants.Labels.PTRANSFORM, "myPtransform"); |
| 98 | + testCases.add(KV.of("metric6", builder.build())); |
| 99 | + |
| 100 | + // Verify each short ID is assigned properly. |
| 101 | + Set<String> expectedShortIds = new HashSet<String>(); |
| 102 | + for (KV<String, MetricsApi.MonitoringInfo> entry : testCases) { |
| 103 | + assertEquals(entry.getKey(), shortIdMap.getOrCreateShortId(entry.getValue())); |
| 104 | + expectedShortIds.add(entry.getKey()); |
| 105 | + } |
| 106 | + |
| 107 | + HashMap<String, MetricsApi.MonitoringInfo> actualRecoveredInfos = new HashMap<>(); |
| 108 | + for (String expectedShortId : expectedShortIds) { |
| 109 | + actualRecoveredInfos.put(expectedShortId, shortIdMap.get(expectedShortId)); |
| 110 | + } |
| 111 | + // Retrieve all of the MonitoringInfos by short id, and verify that the |
| 112 | + // metadata (everything but the payload) matches the originals |
| 113 | + assertEquals(expectedShortIds, actualRecoveredInfos.keySet()); |
| 114 | + for (KV<String, MetricsApi.MonitoringInfo> entry : testCases) { |
| 115 | + // Clear payloads of both expected and actual before comparing |
| 116 | + MetricsApi.MonitoringInfo expectedMonitoringInfo = entry.getValue(); |
| 117 | + MetricsApi.MonitoringInfo.Builder expected = |
| 118 | + MetricsApi.MonitoringInfo.newBuilder(expectedMonitoringInfo); |
| 119 | + expected.clearPayload(); |
| 120 | + |
| 121 | + MetricsApi.MonitoringInfo.Builder actual = |
| 122 | + MetricsApi.MonitoringInfo.newBuilder(actualRecoveredInfos.get(entry.getKey())); |
| 123 | + actual.clearPayload(); |
| 124 | + assertEquals(expected.build(), actual.build()); |
| 125 | + } |
| 126 | + |
| 127 | + // Verify each short ID is assigned properly, in reverse. |
| 128 | + for (int i = testCases.size() - 1; i > 0; i--) { |
| 129 | + assertEquals( |
| 130 | + testCases.get(i).getKey(), shortIdMap.getOrCreateShortId(testCases.get(i).getValue())); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments