Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented msec counters support in FnApi world.
Minor refactoring to generalize validation and counter transformation.
- Loading branch information
Mikhail Gryzykhin
committed
Dec 19, 2018
1 parent
bd68ef6
commit 92b3642
Showing
15 changed files
with
940 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...-java/src/main/java/org/apache/beam/runners/core/metrics/SpecMonitoringInfoValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.beam.runners.core.metrics; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import org.apache.beam.model.fnexecution.v1.BeamFnApi; | ||
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfo; | ||
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfoSpec; | ||
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfoSpecs; | ||
|
||
/** Class implements validation of MonitoringInfos against MonitoringInfoSpecs. */ | ||
public class SpecMonitoringInfoValidator { | ||
protected final MonitoringInfoSpec[] specs; | ||
|
||
public SpecMonitoringInfoValidator() { | ||
specs = | ||
Arrays.stream(MonitoringInfoSpecs.Enum.values()) | ||
.filter(x -> !(x).name().equals("UNRECOGNIZED")) | ||
.map( | ||
x -> x.getValueDescriptor().getOptions().getExtension(BeamFnApi.monitoringInfoSpec)) | ||
.toArray(size -> new MonitoringInfoSpec[size]); | ||
} | ||
|
||
/** | ||
* Validates provided {link MonitoringInfo} against relevant {link MonitoringInfoSpecs} if | ||
* present. | ||
* | ||
* @return error string if validation fails. | ||
*/ | ||
public Optional<String> validate(MonitoringInfo monitoringInfo) { | ||
MonitoringInfoSpec spec = null; | ||
|
||
for (MonitoringInfoSpec specIterator : specs) { | ||
if (monitoringInfo.getUrn().startsWith(specIterator.getUrn())) { | ||
spec = specIterator; | ||
break; | ||
} | ||
} | ||
|
||
// Skip checking unknown MonitoringInfos | ||
if (spec == null) { | ||
return Optional.empty(); | ||
} | ||
|
||
if (!monitoringInfo.getType().equals(spec.getTypeUrn())) { | ||
return Optional.of( | ||
String.format( | ||
"Monitoring info with urn: %s should have type: %s, received %s", | ||
monitoringInfo.getUrn(), spec.getTypeUrn(), monitoringInfo.getType())); | ||
} | ||
|
||
Set<String> requiredLabels = new HashSet<>(spec.getRequiredLabelsList()); | ||
if (!monitoringInfo.getLabelsMap().keySet().containsAll(requiredLabels)) { | ||
return Optional.of( | ||
String.format( | ||
"MonitoringInfo with urn: %s should have labels: %s, actual: %s", | ||
monitoringInfo.getUrn(), requiredLabels, monitoringInfo.getLabelsMap())); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...a/src/test/java/org/apache/beam/runners/core/metrics/SpecMonitoringInfoValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.beam.runners.core.metrics; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfo; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** Relevant tests. */ | ||
public class SpecMonitoringInfoValidatorTest { | ||
|
||
SpecMonitoringInfoValidator testObject = null; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
testObject = new SpecMonitoringInfoValidator(); | ||
} | ||
|
||
@Test | ||
public void validateReturnsErrorOnInvalidMonitoringInfoType() { | ||
MonitoringInfo testInput = | ||
MonitoringInfo.newBuilder() | ||
.setUrn("beam:metric:user:someCounter") | ||
.setType("beam:metrics:bad_value") | ||
.build(); | ||
assertTrue(testObject.validate(testInput).isPresent()); | ||
} | ||
|
||
@Test | ||
public void validateReturnsNoErrorOnValidMonitoringInfo() { | ||
MonitoringInfo testInput = | ||
MonitoringInfo.newBuilder() | ||
.setUrn("beam:metric:user:someCounter") | ||
.setType("beam:metrics:sum_int_64") | ||
.putLabels("dummy", "value") | ||
.build(); | ||
assertFalse(testObject.validate(testInput).isPresent()); | ||
|
||
testInput = | ||
MonitoringInfo.newBuilder() | ||
.setUrn("beam:metric:element_count:v1") | ||
.setType("beam:metrics:sum_int_64") | ||
.putLabels("PTRANSFORM", "value") | ||
.putLabels("PCOLLECTION", "anotherValue") | ||
.build(); | ||
assertFalse(testObject.validate(testInput).isPresent()); | ||
} | ||
|
||
@Test | ||
public void validateReturnsErrorOnInvalidMonitoringInfoLabels() { | ||
MonitoringInfo testInput = | ||
MonitoringInfo.newBuilder() | ||
.setUrn("beam:metric:element_count:v1") | ||
.setType("beam:metrics:sum_int_64") | ||
.putLabels("PCOLLECTION", "anotherValue") | ||
.build(); | ||
assertTrue(testObject.validate(testInput).isPresent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.