Skip to content

Commit 8232ddd

Browse files
committed
Merge pull request #534 from sahilTakiar/getOrDefaultBug
Fixing bug with GobblinMetricsRegistry.getOrDefault
2 parents d519e78 + 21448c6 commit 8232ddd

File tree

7 files changed

+77
-50
lines changed

7 files changed

+77
-50
lines changed

gobblin-core/src/main/java/gobblin/instrumented/Instrumented.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,18 @@ public static MetricContext getMetricContext(State state, Class<?> klazz, List<T
126126

127127
List<Tag<?>> generatedTags = Lists.newArrayList();
128128
if (construct != null) {
129-
generatedTags.add(new Tag<String>("construct", construct.toString()));
129+
generatedTags.add(new Tag<>("construct", construct.toString()));
130130
}
131131
if (!klazz.isAnonymousClass()) {
132-
generatedTags.add(new Tag<String>("class", klazz.getCanonicalName()));
132+
generatedTags.add(new Tag<>("class", klazz.getCanonicalName()));
133133
}
134134

135-
GobblinMetrics gobblinMetrics;
136-
MetricContext.Builder builder = state.contains(METRIC_CONTEXT_NAME_KEY) &&
137-
(gobblinMetrics = GobblinMetricsRegistry.getInstance().get(state.getProp(METRIC_CONTEXT_NAME_KEY))) != null ?
138-
gobblinMetrics.getMetricContext().childBuilder(klazz.getCanonicalName() + "." + randomId) :
135+
Optional<GobblinMetrics> gobblinMetrics = state.contains(METRIC_CONTEXT_NAME_KEY) ?
136+
GobblinMetricsRegistry.getInstance().get(state.getProp(METRIC_CONTEXT_NAME_KEY)) :
137+
Optional.<GobblinMetrics>absent();
138+
139+
MetricContext.Builder builder = gobblinMetrics.isPresent() ?
140+
gobblinMetrics.get().getMetricContext().childBuilder(klazz.getCanonicalName() + "." + randomId) :
139141
MetricContext.builder(klazz.getCanonicalName() + "." + randomId);
140142
return builder.
141143
addTags(generatedTags).

gobblin-core/src/main/java/gobblin/metrics/GobblinMetrics.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919
import java.util.Map;
2020
import java.util.Properties;
21+
import java.util.concurrent.Callable;
2122
import java.util.concurrent.TimeUnit;
2223

2324
import org.apache.hadoop.conf.Configuration;
@@ -120,8 +121,12 @@ public static GobblinMetrics get(String id, MetricContext parentContext) {
120121
* @param tags the given list of {@link Tag}s
121122
* @return a {@link GobblinMetrics} instance
122123
*/
123-
public static GobblinMetrics get(String id, MetricContext parentContext, List<Tag<?>> tags) {
124-
return GOBBLIN_METRICS_REGISTRY.getOrDefault(id, new GobblinMetrics(id, parentContext, tags));
124+
public static GobblinMetrics get(final String id, final MetricContext parentContext, final List<Tag<?>> tags) {
125+
return GOBBLIN_METRICS_REGISTRY.getOrDefault(id, new Callable<GobblinMetrics>() {
126+
@Override public GobblinMetrics call() throws Exception {
127+
return new GobblinMetrics(id, parentContext, tags);
128+
}
129+
});
125130
}
126131

127132
/**
@@ -436,10 +441,10 @@ private void buildFileMetricReporter(Properties properties) {
436441
}
437442

438443
OutputStream output = append ? fs.append(metricLogFile) : fs.create(metricLogFile, true);
439-
this.scheduledReporters.add(this.closer.register(OutputStreamReporter.forContext(this.metricContext)
440-
.outputTo(output).build()));
441-
this.scheduledReporters.add(this.closer.register(OutputStreamEventReporter.forContext(this.metricContext)
442-
.outputTo(output).build()));
444+
this.scheduledReporters
445+
.add(this.closer.register(OutputStreamReporter.forContext(this.metricContext).outputTo(output).build()));
446+
this.scheduledReporters
447+
.add(this.closer.register(OutputStreamEventReporter.forContext(this.metricContext).outputTo(output).build()));
443448

444449
LOGGER.info("Will start reporting metrics to directory " + metricsLogDir);
445450
} catch (IOException ioe) {

gobblin-core/src/main/java/gobblin/metrics/GobblinMetricsRegistry.java

+26-28
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,30 @@
1515
import java.util.concurrent.Callable;
1616
import java.util.concurrent.ExecutionException;
1717

18+
import com.google.common.base.Optional;
1819
import com.google.common.base.Throwables;
1920
import com.google.common.cache.Cache;
2021
import com.google.common.cache.CacheBuilder;
2122

2223

2324
/**
24-
* Registry that stores instances of {@link GobblinMetrics} identified by an arbitrary string id.
25-
* The static method getInstance() provides a static instance of this this class that should be considered
26-
* the global registry of metrics.
27-
* An application could also instantiate one or more registries to for example separate instances of
28-
* {@link GobblinMetrics} into different scopes.
25+
* Registry that stores instances of {@link GobblinMetrics} identified by an arbitrary string id. The static method
26+
* {@link #getInstance()} provides a static instance of this this class that should be considered the global registry of
27+
* metrics.
28+
*
29+
* <p>
30+
* An application could also instantiate one or more registries to, for example, separate instances of
31+
* {@link GobblinMetrics} into different scopes.
32+
* </p>
2933
*/
3034
public class GobblinMetricsRegistry {
3135

3236
private static final GobblinMetricsRegistry GLOBAL_INSTANCE = new GobblinMetricsRegistry();
3337

34-
private final Cache<String, GobblinMetrics> metricsMap = CacheBuilder.newBuilder().softValues().build();
38+
private final Cache<String, GobblinMetrics> metricsCache = CacheBuilder.newBuilder().softValues().build();
3539

3640
private GobblinMetricsRegistry() {
37-
41+
// Do nothing
3842
}
3943

4044
/**
@@ -47,39 +51,34 @@ private GobblinMetricsRegistry() {
4751
* if there's no previous {@link GobblinMetrics} instance associated with the ID
4852
*/
4953
public GobblinMetrics putIfAbsent(String id, GobblinMetrics gobblinMetrics) {
50-
return this.metricsMap.asMap().putIfAbsent(id, gobblinMetrics);
54+
return this.metricsCache.asMap().putIfAbsent(id, gobblinMetrics);
5155
}
5256

5357
/**
5458
* Get the {@link GobblinMetrics} instance associated with a given ID.
5559
*
5660
* @param id the given {@link GobblinMetrics} ID
57-
* @return the {@link GobblinMetrics} instance associated with the ID or {@code null}
58-
* if no {@link GobblinMetrics} instance for the given ID is found
61+
* @return the {@link GobblinMetrics} instance associated with the ID, wrapped in an {@link Optional} or
62+
* {@link Optional#absent()} if no {@link GobblinMetrics} instance for the given ID is found
5963
*/
60-
public GobblinMetrics get(String id) {
61-
return this.metricsMap.getIfPresent(id);
64+
public Optional<GobblinMetrics> get(String id) {
65+
return Optional.fromNullable(this.metricsCache.getIfPresent(id));
6266
}
6367

6468
/**
65-
* Get the {@link GobblinMetrics} instance associated with a given ID or the given default
66-
* {@link GobblinMetrics} instance if no {@link GobblinMetrics} instance for the given ID
67-
* is found.
69+
* Get the {@link GobblinMetrics} instance associated with a given ID. If the ID is not found this method returns the
70+
* {@link GobblinMetrics} returned by the given {@link Callable}, and creates a mapping between the specified ID
71+
* and the {@link GobblinMetrics} instance returned by the {@link Callable}.
6872
*
6973
* @param id the given {@link GobblinMetrics} ID
70-
* @param defaultValue the default {@link GobblinMetrics} instance
71-
* @return the {@link GobblinMetrics} instance associated with a given ID or the given default
72-
* {@link GobblinMetrics} instance if no {@link GobblinMetrics} instance for the given ID
73-
* is found
74+
* @param valueLoader a {@link Callable} that returns a {@link GobblinMetrics}, the {@link Callable} is only invoked
75+
* if the given id is not found
76+
*
77+
* @return a {@link GobblinMetrics} instance associated with the id
7478
*/
75-
public GobblinMetrics getOrDefault(String id, final GobblinMetrics defaultValue) {
79+
public GobblinMetrics getOrDefault(String id, Callable<? extends GobblinMetrics> valueLoader) {
7680
try {
77-
return this.metricsMap.get(id, new Callable<GobblinMetrics>() {
78-
@Override
79-
public GobblinMetrics call() throws Exception {
80-
return defaultValue;
81-
}
82-
});
81+
return this.metricsCache.get(id, valueLoader);
8382
} catch (ExecutionException ee) {
8483
throw Throwables.propagate(ee);
8584
}
@@ -93,7 +92,7 @@ public GobblinMetrics call() throws Exception {
9392
* {@link GobblinMetrics} instance for the given ID is found
9493
*/
9594
public GobblinMetrics remove(String id) {
96-
return this.metricsMap.asMap().remove(id);
95+
return this.metricsCache.asMap().remove(id);
9796
}
9897

9998
/**
@@ -104,5 +103,4 @@ public GobblinMetrics remove(String id) {
104103
public static GobblinMetricsRegistry getInstance() {
105104
return GLOBAL_INSTANCE;
106105
}
107-
108106
}

gobblin-metrics/src/main/java/gobblin/metrics/MetricContext.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,10 @@ public MetricContext build() {
692692
try {
693693
return buildStrict();
694694
} catch (NameConflictException nce) {
695-
this.name = this.name + "_" + UUID.randomUUID().toString();
695+
String uuid = UUID.randomUUID().toString();
696+
LOG.warn("MetricContext with specified name already exists, appending UUID to the given name: " + uuid);
697+
698+
this.name = this.name + "_" + uuid;
696699
try {
697700
return buildStrict();
698701
} catch (NameConflictException nce2) {

gobblin-runtime/src/main/java/gobblin/runtime/util/JobMetrics.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package gobblin.runtime.util;
1414

1515
import java.util.List;
16+
import java.util.concurrent.Callable;
1617

1718
import com.google.common.collect.Lists;
1819

@@ -70,8 +71,12 @@ public static JobMetrics get(String jobId) {
7071
* @param parentContext is the parent {@link MetricContext}
7172
* @return a {@link JobMetrics} instance
7273
*/
73-
public static JobMetrics get(JobState jobState, MetricContext parentContext) {
74-
return (JobMetrics) GOBBLIN_METRICS_REGISTRY.getOrDefault(name(jobState), new JobMetrics(jobState, parentContext));
74+
public static JobMetrics get(final JobState jobState, final MetricContext parentContext) {
75+
return (JobMetrics) GOBBLIN_METRICS_REGISTRY.getOrDefault(name(jobState), new Callable<GobblinMetrics>() {
76+
@Override public GobblinMetrics call() throws Exception {
77+
return new JobMetrics(jobState, parentContext);
78+
}
79+
});
7580
}
7681

7782
/**
@@ -80,8 +85,12 @@ public static JobMetrics get(JobState jobState, MetricContext parentContext) {
8085
* @param jobState the given {@link JobState} instance
8186
* @return a {@link JobMetrics} instance
8287
*/
83-
public static JobMetrics get(JobState jobState) {
84-
return (JobMetrics) GOBBLIN_METRICS_REGISTRY.getOrDefault(name(jobState), new JobMetrics(jobState));
88+
public static JobMetrics get(final JobState jobState) {
89+
return (JobMetrics) GOBBLIN_METRICS_REGISTRY.getOrDefault(name(jobState), new Callable<GobblinMetrics>() {
90+
@Override public GobblinMetrics call() throws Exception {
91+
return new JobMetrics(jobState);
92+
}
93+
});
8594
}
8695

8796
/**

gobblin-runtime/src/main/java/gobblin/runtime/util/TaskMetrics.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package gobblin.runtime.util;
1414

1515
import java.util.List;
16+
import java.util.concurrent.Callable;
1617

1718
import com.google.common.collect.Lists;
1819

@@ -43,8 +44,12 @@ protected TaskMetrics(TaskState taskState) {
4344
* @param taskState the given {@link TaskState} instance
4445
* @return a {@link TaskMetrics} instance
4546
*/
46-
public static TaskMetrics get(TaskState taskState) {
47-
return (TaskMetrics) GOBBLIN_METRICS_REGISTRY.getOrDefault(name(taskState), new TaskMetrics(taskState));
47+
public static TaskMetrics get(final TaskState taskState) {
48+
return (TaskMetrics) GOBBLIN_METRICS_REGISTRY.getOrDefault(name(taskState), new Callable<GobblinMetrics>() {
49+
@Override public GobblinMetrics call() throws Exception {
50+
return new TaskMetrics(taskState);
51+
}
52+
});
4853
}
4954

5055
/**

gobblin-yarn/src/main/java/gobblin/yarn/ContainerMetrics.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package gobblin.yarn;
1414

1515
import java.util.List;
16+
import java.util.concurrent.Callable;
1617

1718
import org.apache.hadoop.yarn.api.records.ContainerId;
1819

@@ -46,9 +47,13 @@ protected ContainerMetrics(State containerState, String applicationName, Contain
4647
* @param containerId the {@link ContainerId} of the container
4748
* @return a {@link ContainerMetrics} instance
4849
*/
49-
public static ContainerMetrics get(State containerState, String applicationName, ContainerId containerId) {
50-
return (ContainerMetrics) GOBBLIN_METRICS_REGISTRY
51-
.getOrDefault(name(containerId), new ContainerMetrics(containerState, applicationName, containerId));
50+
public static ContainerMetrics get(final State containerState, final String applicationName,
51+
final ContainerId containerId) {
52+
return (ContainerMetrics) GOBBLIN_METRICS_REGISTRY.getOrDefault(name(containerId), new Callable<GobblinMetrics>() {
53+
@Override public GobblinMetrics call() throws Exception {
54+
return new ContainerMetrics(containerState, applicationName, containerId);
55+
}
56+
});
5257
}
5358

5459
private static String name(ContainerId containerId) {

0 commit comments

Comments
 (0)