Skip to content

Commit

Permalink
updated to use array length
Browse files Browse the repository at this point in the history
  • Loading branch information
DNVindhya committed Apr 26, 2024
1 parent 677f82c commit 2dbf553
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions api/src/main/java/io/grpc/MetricInstrumentRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@ public final class MetricInstrumentRegistry {
static final int INITIAL_INSTRUMENT_CAPACITY = 5;
private static MetricInstrumentRegistry instance;
private final Object lock = new Object();
private final Set<String> registeredMetricNames;
private volatile MetricInstrument[] metricInstruments;
private volatile int instrumentListCapacity;
private final Set<String> registeredMetricNames = new HashSet<>();
private volatile MetricInstrument[] metricInstruments =
new MetricInstrument[INITIAL_INSTRUMENT_CAPACITY];
@GuardedBy("lock")
private int nextAvailableMetricIndex;

@VisibleForTesting
MetricInstrumentRegistry() {
this.metricInstruments = new MetricInstrument[INITIAL_INSTRUMENT_CAPACITY];
this.registeredMetricNames = new HashSet<>();
this.instrumentListCapacity = metricInstruments.length;
}
MetricInstrumentRegistry() {}

/**
* Returns the default metric instrument registry.
Expand Down Expand Up @@ -94,7 +90,7 @@ public DoubleCounterMetricInstrument registerDoubleCounter(String name,
throw new IllegalStateException("Metric with name " + name + " already exists");
}
int index = nextAvailableMetricIndex;
if (index + 1 == instrumentListCapacity) {
if (index + 1 == metricInstruments.length) {
resizeMetricInstruments();
}
DoubleCounterMetricInstrument instrument = new DoubleCounterMetricInstrument(
Expand Down Expand Up @@ -132,7 +128,7 @@ public LongCounterMetricInstrument registerLongCounter(String name,
throw new IllegalStateException("Metric with name " + name + " already exists");
}
int index = nextAvailableMetricIndex;
if (index + 1 == instrumentListCapacity) {
if (index + 1 == metricInstruments.length) {
resizeMetricInstruments();
}
LongCounterMetricInstrument instrument = new LongCounterMetricInstrument(
Expand Down Expand Up @@ -172,7 +168,7 @@ public DoubleHistogramMetricInstrument registerDoubleHistogram(String name,
throw new IllegalStateException("Metric with name " + name + " already exists");
}
int index = nextAvailableMetricIndex;
if (index + 1 == instrumentListCapacity) {
if (index + 1 == metricInstruments.length) {
resizeMetricInstruments();
}
DoubleHistogramMetricInstrument instrument = new DoubleHistogramMetricInstrument(
Expand Down Expand Up @@ -213,7 +209,7 @@ public LongHistogramMetricInstrument registerLongHistogram(String name,
throw new IllegalStateException("Metric with name " + name + " already exists");
}
int index = nextAvailableMetricIndex;
if (index + 1 == instrumentListCapacity) {
if (index + 1 == metricInstruments.length) {
resizeMetricInstruments();
}
LongHistogramMetricInstrument instrument = new LongHistogramMetricInstrument(
Expand Down Expand Up @@ -253,7 +249,7 @@ public LongGaugeMetricInstrument registerLongGauge(String name, String descripti
throw new IllegalStateException("Metric with name " + name + " already exists");
}
int index = nextAvailableMetricIndex;
if (index + 1 == instrumentListCapacity) {
if (index + 1 == metricInstruments.length) {
resizeMetricInstruments();
}
LongGaugeMetricInstrument instrument = new LongGaugeMetricInstrument(
Expand All @@ -268,9 +264,9 @@ public LongGaugeMetricInstrument registerLongGauge(String name, String descripti

private synchronized void resizeMetricInstruments() {
// Increase the capacity of the metricInstruments array by INITIAL_INSTRUMENT_CAPACITY
instrumentListCapacity += INITIAL_INSTRUMENT_CAPACITY;
int newInstrumentsCapacity = metricInstruments.length + INITIAL_INSTRUMENT_CAPACITY;
MetricInstrument[] resizedMetricInstruments = Arrays.copyOf(metricInstruments,
instrumentListCapacity);
newInstrumentsCapacity);
metricInstruments = resizedMetricInstruments;
}
}

0 comments on commit 2dbf553

Please sign in to comment.