Skip to content

Commit

Permalink
IGNITE-20201 Fixed availability to set configuration for unregistered…
Browse files Browse the repository at this point in the history
… metrics (#10903)
  • Loading branch information
petrov-mg authored Aug 21, 2023
1 parent 8f2bbed commit afd19ba
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ public void configureHitRate(String name, long rateTimeInterval) throws IgniteCh
if (ctx.isStopping())
throw new NodeStoppingException("Operation has been cancelled (node is stopping)");

ensureMetricRegistered(name, HitRateMetric.class);

metastorage.write(metricName(HITRATE_CFG_PREFIX, name), rateTimeInterval);
}

Expand All @@ -443,6 +445,8 @@ public void configureHistogram(String name, long[] bounds) throws IgniteCheckedE
if (ctx.isStopping())
throw new NodeStoppingException("Operation has been cancelled (node is stopping)");

ensureMetricRegistered(name, HistogramMetric.class);

metastorage.write(metricName(HISTOGRAM_CFG_PREFIX, name), bounds);
}

Expand Down Expand Up @@ -565,6 +569,12 @@ public MemoryUsage heapMemoryUsage() {
}
}

/** */
private <T extends Metric> void ensureMetricRegistered(String name, Class<T> cls) {
if (find(name, cls) == null)
throw new IgniteException("Failed to find registered metric with specified name [metricName=" + name + ']');
}

/**
* @return Total system memory.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.ignite.internal.processors.metric.impl.LongGauge;
import org.apache.ignite.internal.processors.metric.impl.ObjectGauge;
import org.apache.ignite.internal.processors.metric.impl.ObjectMetricImpl;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.spi.metric.BooleanMetric;
import org.apache.ignite.spi.metric.IntMetric;
import org.apache.ignite.spi.metric.Metric;
Expand Down Expand Up @@ -322,6 +323,8 @@ public HistogramMetricImpl histogram(String name, long[] bounds, @Nullable Strin
* @return Registered metric.
*/
private <T extends Metric> T addMetric(String name, T metric) {
assert !F.isEmpty(name);

T old = (T)metrics.putIfAbsent(name, metric);

if (old != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,21 @@ public static String metricName(String... names) {
* @return Array consist of registry name and metric name.
*/
public static T2<String, String> fromFullName(String name) {
return new T2<>(
name.substring(0, name.lastIndexOf(SEPARATOR)),
name.substring(name.lastIndexOf(SEPARATOR) + 1)
);
int metricNamePos = name.lastIndexOf(SEPARATOR);

String regName;
String metricName;

if (metricNamePos == -1) {
regName = name;
metricName = "";
}
else {
regName = name.substring(0, metricNamePos);
metricName = name.substring(metricNamePos + 1);
}

return new T2<>(regName, metricName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.ignite.internal.metric;

import org.apache.ignite.IgniteException;
import org.apache.ignite.configuration.DataRegionConfiguration;
import org.apache.ignite.configuration.DataStorageConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.junit.Test;

import static org.apache.ignite.cluster.ClusterState.ACTIVE;
import static org.apache.ignite.cluster.ClusterState.INACTIVE;
import static org.apache.ignite.internal.processors.cache.persistence.DataStorageMetricsImpl.DATASTORAGE_METRIC_PREFIX;

/** */
public class MetricConfigurationTest extends GridCommonAbstractTest {
/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
return super.getConfiguration(igniteInstanceName)
.setDataStorageConfiguration(new DataStorageConfiguration()
.setDefaultDataRegionConfiguration(new DataRegionConfiguration()
.setPersistenceEnabled(true)));
}

/** {@inheritDoc} */
@Override protected void beforeTest() throws Exception {
super.beforeTest();

cleanPersistenceDir();
}

/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
super.afterTest();

stopAllGrids();
cleanPersistenceDir();
}

/** */
@Test
public void testInvalidMetricConfigurationName() throws Exception {
startGrid(0);

grid(0).cluster().state(ACTIVE);

checkMetricConfigurationUpdateFailed("invalid");
checkMetricConfigurationUpdateFailed(".");
checkMetricConfigurationUpdateFailed(".invalid");
checkMetricConfigurationUpdateFailed("invalid.metric.name");
checkMetricConfigurationUpdateFailed(DATASTORAGE_METRIC_PREFIX);
checkMetricConfigurationUpdateFailed(DATASTORAGE_METRIC_PREFIX + '.');

grid(0).cluster().state(INACTIVE);

stopAllGrids();

startGrid(0);

grid(0).cluster().state(ACTIVE);
}

/** */
private void checkMetricConfigurationUpdateFailed(String name) {
GridTestUtils.assertThrowsAnyCause(
log,
() -> {
grid(0).context().metric().configureHistogram(name, new long[] {1, 2, 3});

return null;
},
IgniteException.class,
"Failed to find registered metric with specified name [metricName=" + name + ']'
);

GridTestUtils.assertThrowsAnyCause(
log,
() -> {
grid(0).context().metric().configureHitRate(name, 1000);

return null;
},
IgniteException.class,
"Failed to find registered metric with specified name [metricName=" + name + ']'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void testRegister() throws Exception {
assertEquals(0, l.value());

assertThrowsWithCause(() -> mreg.register(new AtomicLongMetric(mName, "")),
StringIndexOutOfBoundsException.class);
AssertionError.class);

assertThrowsWithCause(() -> mreg.register(new AtomicLongMetric(metricName("mreg", mName), "")),
AssertionError.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
import static org.apache.ignite.internal.processors.cache.GridCacheUtils.cacheGroupId;
import static org.apache.ignite.internal.processors.cache.GridCacheUtils.cacheId;
import static org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl.BINARY_METADATA_VIEW;
import static org.apache.ignite.internal.processors.cache.persistence.DataStorageMetricsImpl.DATASTORAGE_METRIC_PREFIX;
import static org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager.METASTORE_VIEW;
import static org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager.DATA_REGION_PAGE_LIST_VIEW;
import static org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager.PAGE_TS_HISTOGRAM_VIEW;
Expand All @@ -178,6 +179,7 @@
import static org.apache.ignite.internal.processors.datastructures.DataStructuresProcessor.STAMPED_VIEW;
import static org.apache.ignite.internal.processors.datastructures.DataStructuresProcessor.VOLATILE_DATA_REGION_NAME;
import static org.apache.ignite.internal.processors.metastorage.persistence.DistributedMetaStorageImpl.DISTRIBUTED_METASTORE_VIEW;
import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.metricName;
import static org.apache.ignite.internal.processors.odbc.ClientListenerProcessor.CLI_CONN_ATTR_VIEW;
import static org.apache.ignite.internal.processors.odbc.ClientListenerProcessor.CLI_CONN_VIEW;
import static org.apache.ignite.internal.processors.pool.PoolProcessor.STREAM_POOL_QUEUE_VIEW;
Expand Down Expand Up @@ -2055,9 +2057,9 @@ public void testDistributedMetastorage() throws Exception {
)))) {
ignite.cluster().state(ClusterState.ACTIVE);

String histogramName = "my-histogram";
String histogramName = "CheckpointBeforeLockHistogram";

ignite.context().metric().configureHistogram(histogramName, new long[] { 1, 2, 3});
ignite.context().metric().configureHistogram(metricName(DATASTORAGE_METRIC_PREFIX, histogramName), new long[] { 1, 2, 3});

DistributedMetaStorage dms = ignite.context().distributedMetastorage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.ignite.internal.managers.IgniteDiagnosticPartitionReleaseFutureLimitTest;
import org.apache.ignite.internal.managers.communication.GridIoManagerFileTransmissionSelfTest;
import org.apache.ignite.internal.managers.discovery.IncompleteDeserializationExceptionTest;
import org.apache.ignite.internal.metric.MetricConfigurationTest;
import org.apache.ignite.internal.metric.MetricsClusterActivationTest;
import org.apache.ignite.internal.metric.PeriodicHistogramMetricImplTest;
import org.apache.ignite.internal.mxbean.IgniteStandardMXBeanTest;
Expand Down Expand Up @@ -135,6 +136,7 @@
CacheFreeListSelfTest.class,
DataRegionMetricsSelfTest.class,
MetricsClusterActivationTest.class,
MetricConfigurationTest.class,
SwapPathConstructionSelfTest.class,
BitSetIntSetTest.class,
ImmutableIntSetTest.class,
Expand Down

0 comments on commit afd19ba

Please sign in to comment.