From 56b4aba0005673ca9437646576f4112b54b0ff24 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Tue, 27 Aug 2019 11:46:26 +0300 Subject: [PATCH 01/18] IGNITE-12108 TCP Communication Metrics ported to a new framework. --- .../processors/resource/GridResourceIoc.java | 9 +- .../resource/GridResourceProcessor.java | 2 + .../GridResourceSupplierInjector.java | 64 ++++ .../MetricManagerResource.java} | 27 +- .../internal/util/ipc/IpcToNioAdapter.java | 33 +- .../nio/GridAbstractCommunicationClient.java | 27 +- .../internal/util/nio/GridNioServer.java | 116 ++++-- .../util/nio/GridSelectorNioSessionImpl.java | 33 +- .../nio/GridShmemCommunicationClient.java | 17 +- .../spi/communication/CommunicationSpi.java | 13 + .../tcp/TcpCommunicationMetricsListener.java | 341 ++++++++---------- .../tcp/TcpCommunicationSpi.java | 71 +++- .../junits/GridTestKernalContext.java | 9 + .../junits/IgniteTestResources.java | 28 +- 14 files changed, 495 insertions(+), 295 deletions(-) create mode 100644 modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceSupplierInjector.java rename modules/core/src/main/java/org/apache/ignite/internal/{util/nio/GridNioMetricsListener.java => resources/MetricManagerResource.java} (66%) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java index b0f78bd329e57..10af241fe1121 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java @@ -25,10 +25,12 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicReference; import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.internal.managers.deployment.GridDeployment; +import org.apache.ignite.internal.resources.MetricManagerResource; import org.apache.ignite.internal.util.GridLeanIdentitySet; import org.apache.ignite.internal.util.IgniteUtils; import org.apache.ignite.internal.util.typedef.F; @@ -48,7 +50,6 @@ import org.apache.ignite.resources.TaskContinuousMapperResource; import org.apache.ignite.resources.TaskSessionResource; import org.jetbrains.annotations.Nullable; -import java.util.concurrent.ConcurrentHashMap; /** * Resource container contains caches for classes used for injection. @@ -513,6 +514,9 @@ enum ResourceAnnotation { /** */ CACHE_STORE_SESSION(CacheStoreSessionResource.class), + /** */ + METRIC_MANAGER(MetricManagerResource.class), + /** */ FILESYSTEM_RESOURCE(FileSystemResource.class); @@ -537,7 +541,8 @@ public enum AnnotationSet { ResourceAnnotation.SPRING, ResourceAnnotation.IGNITE_INSTANCE, ResourceAnnotation.LOGGER, - ResourceAnnotation.SERVICE + ResourceAnnotation.SERVICE, + ResourceAnnotation.METRIC_MANAGER ), /** */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceProcessor.java index 6d508fe7830de..b47ff1b3c5e6e 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceProcessor.java @@ -74,6 +74,8 @@ public GridResourceProcessor(GridKernalContext ctx) { new GridResourceLoggerInjector(ctx.config().getGridLogger()); injectorByAnnotation[GridResourceIoc.ResourceAnnotation.IGNITE_INSTANCE.ordinal()] = new GridResourceBasicInjector<>(ctx.grid()); + injectorByAnnotation[GridResourceIoc.ResourceAnnotation.METRIC_MANAGER.ordinal()] = + new GridResourceSupplierInjector<>(ctx::metric); } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceSupplierInjector.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceSupplierInjector.java new file mode 100644 index 0000000000000..66e441f817242 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceSupplierInjector.java @@ -0,0 +1,64 @@ +/* + * 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.processors.resource; + +import java.util.function.Supplier; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.internal.managers.deployment.GridDeployment; +import org.apache.ignite.internal.util.typedef.internal.S; + +/** + * Simple injector which wraps resource object supplier. + * + * @param Type of injected resource. + */ +class GridResourceSupplierInjector implements GridResourceInjector { + /** Resource to inject. */ + private final Supplier supplier; + + /** + * Creates injector. + * + * @param supplier Resource supplier. + */ + GridResourceSupplierInjector(Supplier supplier) { + this.supplier = supplier; + } + + /** {@inheritDoc} */ + @Override public void inject(GridResourceField field, Object target, Class depCls, GridDeployment dep) + throws IgniteCheckedException { + GridResourceUtils.inject(field.getField(), target, supplier.get()); + } + + /** {@inheritDoc} */ + @Override public void inject(GridResourceMethod mtd, Object target, Class depCls, GridDeployment dep) + throws IgniteCheckedException { + GridResourceUtils.inject(mtd.getMethod(), target, supplier.get()); + } + + /** {@inheritDoc} */ + @Override public void undeploy(GridDeployment dep) { + /* No-op. There is no cache. */ + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(GridResourceSupplierInjector.class, this); + } +} \ No newline at end of file diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/internal/resources/MetricManagerResource.java similarity index 66% rename from modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioMetricsListener.java rename to modules/core/src/main/java/org/apache/ignite/internal/resources/MetricManagerResource.java index 1bdae3007eca4..21cddd4f73ef4 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/resources/MetricManagerResource.java @@ -15,21 +15,18 @@ * limitations under the License. */ -package org.apache.ignite.internal.util.nio; +package org.apache.ignite.internal.resources; -import java.util.EventListener; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; -/** - * Metrics listener for NIO communication. - */ -public interface GridNioMetricsListener extends EventListener { - /** - * @param bytesCnt Number of sent bytes. - */ - public void onBytesSent(int bytesCnt); - - /** - * @param bytesCnt Number of received bytes. - */ - public void onBytesReceived(int bytesCnt); +/** */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.FIELD}) +public @interface MetricManagerResource { + // No-op. } diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java index 7af6139893a8e..2abfea74bfd8b 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java @@ -26,13 +26,14 @@ import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteException; import org.apache.ignite.IgniteLogger; +import org.apache.ignite.internal.processors.metric.MetricRegistry; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.util.nio.GridNioFilter; import org.apache.ignite.internal.util.nio.GridNioFilterAdapter; import org.apache.ignite.internal.util.nio.GridNioFilterChain; import org.apache.ignite.internal.util.nio.GridNioFinishedFuture; import org.apache.ignite.internal.util.nio.GridNioFuture; import org.apache.ignite.internal.util.nio.GridNioMessageWriterFactory; -import org.apache.ignite.internal.util.nio.GridNioMetricsListener; import org.apache.ignite.internal.util.nio.GridNioServerListener; import org.apache.ignite.internal.util.nio.GridNioSession; import org.apache.ignite.internal.util.nio.GridNioSessionImpl; @@ -40,6 +41,11 @@ import org.apache.ignite.lang.IgniteInClosure; import org.apache.ignite.plugin.extensions.communication.Message; +import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_DESC; +import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_NAME; +import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_DESC; +import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_NAME; + /** * Allows to re-use existing {@link GridNioFilter}s on IPC (specifically shared memory IPC) * communications. @@ -63,25 +69,32 @@ public class IpcToNioAdapter { /** */ private final ByteBuffer writeBuf; - /** */ - private final GridNioMetricsListener metricsLsnr; + /** Received bytes count metric. */ + private final LongAdderMetric rcvdBytesCntMetric; + + /** Sent bytes count metric. */ + private final LongAdderMetric sentBytesCntMetric; /** */ private final GridNioMessageWriterFactory writerFactory; /** - * @param metricsLsnr Metrics listener. + * @param mreg Metrics registry. * @param log Log. * @param endp Endpoint. * @param lsnr Listener. * @param writerFactory Writer factory. * @param filters Filters. */ - public IpcToNioAdapter(GridNioMetricsListener metricsLsnr, IgniteLogger log, IpcEndpoint endp, - GridNioServerListener lsnr, GridNioMessageWriterFactory writerFactory, GridNioFilter... filters) { - assert metricsLsnr != null; + public IpcToNioAdapter(MetricRegistry mreg, IgniteLogger log, IpcEndpoint endp, + GridNioServerListener lsnr, GridNioMessageWriterFactory writerFactory, GridNioFilter... filters + ) { + assert mreg != null; + + rcvdBytesCntMetric = mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + + sentBytesCntMetric = mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); - this.metricsLsnr = metricsLsnr; this.endp = endp; this.writerFactory = writerFactory; @@ -116,7 +129,7 @@ public void serve() throws InterruptedException { int read = in.read(readBuf.array(), pos, readBuf.remaining()); if (read > 0) { - metricsLsnr.onBytesReceived(read); + rcvdBytesCntMetric.add(read); readBuf.position(0); readBuf.limit(pos + read); @@ -167,7 +180,7 @@ private GridNioFuture send(Message msg) { try { int cnt = U.writeMessageFully(msg, endp.outputStream(), writeBuf, writerFactory.writer(ses)); - metricsLsnr.onBytesSent(cnt); + sentBytesCntMetric.add(cnt); } catch (IOException | IgniteCheckedException e) { return new GridNioFinishedFuture(e); diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java index ed7e929145bd3..a3ccf0c6de209 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java @@ -18,10 +18,17 @@ package org.apache.ignite.internal.util.nio; import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.ignite.internal.processors.metric.MetricRegistry; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.util.typedef.internal.S; import org.apache.ignite.internal.util.typedef.internal.U; import org.jetbrains.annotations.Nullable; +import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_DESC; +import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_NAME; +import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_DESC; +import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_NAME; + /** * Implements basic lifecycle for communication clients. */ @@ -32,19 +39,27 @@ public abstract class GridAbstractCommunicationClient implements GridCommunicati /** Reservations. */ private final AtomicBoolean closed = new AtomicBoolean(); - /** Metrics listener. */ - protected final GridNioMetricsListener metricsLsnr; - /** */ private final int connIdx; + /** Received bytes count metric. */ + @Nullable protected final LongAdderMetric rcvdBytesCntMetric; + + /** Sent bytes count metric. */ + @Nullable protected final LongAdderMetric sentBytesCntMetric; + /** * @param connIdx Connection index. - * @param metricsLsnr Metrics listener. + * @param mreg Metrics registry. */ - protected GridAbstractCommunicationClient(int connIdx, @Nullable GridNioMetricsListener metricsLsnr) { + protected GridAbstractCommunicationClient(int connIdx, @Nullable MetricRegistry mreg) { this.connIdx = connIdx; - this.metricsLsnr = metricsLsnr; + + rcvdBytesCntMetric = mreg == null ? + null : mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + + sentBytesCntMetric = mreg == null ? + null : mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java index 4d6dfb84aaad5..347d6153c43fb 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java @@ -55,6 +55,8 @@ import org.apache.ignite.internal.IgniteInternalFuture; import org.apache.ignite.internal.IgniteInterruptedCheckedException; import org.apache.ignite.internal.managers.communication.GridIoMessage; +import org.apache.ignite.internal.processors.metric.MetricRegistry; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.util.GridConcurrentHashSet; import org.apache.ignite.internal.util.GridUnsafe; import org.apache.ignite.internal.util.future.GridCompoundFuture; @@ -129,6 +131,24 @@ public class GridNioServer { private static final boolean DISABLE_KEYSET_OPTIMIZATION = IgniteSystemProperties.getBoolean(IgniteSystemProperties.IGNITE_NO_SELECTOR_OPTS); + /** */ + public static final String OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME = "outboundMessagesQueueSize"; + + /** */ + public static final String OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC = "Number of messages waiting to be sent"; + + /** */ + public static final String RECEIVED_BYTES_METRIC_NAME = "receivedBytes"; + + /** */ + public static final String RECEIVED_BYTES_METRIC_DESC = "Total number of bytes received by current node"; + + /** */ + public static final String SENT_BYTES_METRIC_NAME = "sentBytes"; + + /** */ + public static final String SENT_BYTES_METRIC_DESC = "Total number of bytes sent by current node"; + /** * */ @@ -213,8 +233,15 @@ public class GridNioServer { /** Whether direct mode is used. */ private final boolean directMode; - /** Metrics listener. */ - private final GridNioMetricsListener metricsLsnr; + /** */ + @Nullable private final MetricRegistry mreg; + + /** Received bytes count metric. */ + @Nullable private final LongAdderMetric rcvdBytesCntMetric; + + /** Sent bytes count metric. */ + @Nullable private final LongAdderMetric sentBytesCntMetric; + /** Sessions. */ private final GridConcurrentHashSet sessions = new GridConcurrentHashSet<>(); @@ -267,12 +294,12 @@ public class GridNioServer { * @param sndQueueLimit Send queue limit. * @param directMode Whether direct mode is used. * @param daemon Daemon flag to create threads. - * @param metricsLsnr Metrics listener. * @param writerFactory Writer factory. * @param skipRecoveryPred Skip recovery predicate. * @param msgQueueLsnr Message queue size listener. * @param readWriteSelectorsAssign If {@code true} then in/out connections are assigned to even/odd workers. * @param workerLsnr Worker lifecycle listener. + * @param mreg Metrics registry. * @param filters Filters for this server. * @throws IgniteCheckedException If failed. */ @@ -293,12 +320,12 @@ private GridNioServer( int sndQueueLimit, boolean directMode, boolean daemon, - GridNioMetricsListener metricsLsnr, GridNioMessageWriterFactory writerFactory, IgnitePredicate skipRecoveryPred, IgniteBiInClosure msgQueueLsnr, boolean readWriteSelectorsAssign, @Nullable GridWorkerListener workerLsnr, + @Nullable MetricRegistry mreg, GridNioFilter... filters ) throws IgniteCheckedException { if (port != -1) @@ -382,7 +409,6 @@ private GridNioServer( } this.directMode = directMode; - this.metricsLsnr = metricsLsnr; this.writerFactory = writerFactory; this.skipRecoveryPred = skipRecoveryPred != null ? skipRecoveryPred : F.alwaysFalse(); @@ -403,7 +429,15 @@ private GridNioServer( } } - this.balancer = balancer0; + balancer = balancer0; + + this.mreg = mreg; + + rcvdBytesCntMetric = mreg == null ? + null : mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + + sentBytesCntMetric = mreg == null ? + null : mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); } /** @@ -1133,8 +1167,8 @@ else if (cnt == 0) if (log.isTraceEnabled()) log.trace("Bytes received [sockCh=" + sockCh + ", cnt=" + cnt + ']'); - if (metricsLsnr != null) - metricsLsnr.onBytesReceived(cnt); + if (rcvdBytesCntMetric != null) + rcvdBytesCntMetric.add(cnt); ses.bytesReceived(cnt); @@ -1195,8 +1229,8 @@ else if (cnt == 0) if (log.isTraceEnabled()) log.trace("Bytes sent [sockCh=" + sockCh + ", cnt=" + cnt + ']'); - if (metricsLsnr != null) - metricsLsnr.onBytesSent(cnt); + if (sentBytesCntMetric != null) + sentBytesCntMetric.add(cnt); ses.bytesSent(cnt); } @@ -1296,8 +1330,8 @@ protected DirectNioClientWorker( if (cnt == 0) return; - if (metricsLsnr != null) - metricsLsnr.onBytesReceived(cnt); + if (rcvdBytesCntMetric != null) + rcvdBytesCntMetric.add(cnt); ses.bytesReceived(cnt); onRead(cnt); @@ -1377,8 +1411,8 @@ private void processWriteSsl(SelectionKey key) throws IOException { if (sslNetBuf != null) { int cnt = sockCh.write(sslNetBuf); - if (metricsLsnr != null) - metricsLsnr.onBytesSent(cnt); + if (sentBytesCntMetric != null) + sentBytesCntMetric.add(cnt); ses.bytesSent(cnt); @@ -1494,8 +1528,8 @@ private void processWriteSsl(SelectionKey key) throws IOException { if (log.isTraceEnabled()) log.trace("Bytes sent [sockCh=" + sockCh + ", cnt=" + cnt + ']'); - if (metricsLsnr != null) - metricsLsnr.onBytesSent(cnt); + if (sentBytesCntMetric != null) + sentBytesCntMetric.add(cnt); ses.bytesSent(cnt); } @@ -1551,8 +1585,8 @@ private boolean writeSslSystem(GridSelectorNioSessionImpl ses, WritableByteChann while ((buf = queue.peek()) != null) { int cnt = sockCh.write(buf); - if (metricsLsnr != null) - metricsLsnr.onBytesSent(cnt); + if (sentBytesCntMetric != null) + sentBytesCntMetric.add(cnt); ses.bytesSent(cnt); @@ -1681,8 +1715,8 @@ private void processWrite0(SelectionKey key) throws IOException { if (log.isTraceEnabled()) log.trace("Bytes sent [sockCh=" + sockCh + ", cnt=" + cnt + ']'); - if (metricsLsnr != null) - metricsLsnr.onBytesSent(cnt); + if (sentBytesCntMetric != null) + sentBytesCntMetric.add(cnt); ses.bytesSent(cnt); onWrite(cnt); @@ -2583,6 +2617,7 @@ private void register(NioOperationFuture fut) { (InetSocketAddress)sockCh.getRemoteAddress(), fut.accepted(), sndQueueLimit, + mreg, writeBuf, readBuf); @@ -2860,14 +2895,17 @@ final void reset0() { * Gets outbound messages queue size. * * @return Write queue size. + * @deprecated Will be removed in the next major release and replaced with new metrics API. */ + @Deprecated public int outboundMessagesQueueSize() { - int res = 0; - - for (GridSelectorNioSessionImpl ses : sessions) - res += ses.writeQueueSize(); + if (mreg == null) + return -1; - return res; + return (int) mreg.longAdderMetric( + OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME, + OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC + ).value(); } /** @@ -3671,9 +3709,6 @@ public static class Builder { /** Whether direct mode is used. */ private boolean directMode; - /** Metrics listener. */ - private GridNioMetricsListener metricsLsnr; - /** NIO filters. */ private GridNioFilter[] filters; @@ -3707,6 +3742,9 @@ public static class Builder { /** Worker lifecycle listener to be used by server's worker threads. */ private GridWorkerListener workerLsnr; + /** Metrics registry. */ + private MetricRegistry mreg; + /** * Finishes building the instance. * @@ -3731,12 +3769,12 @@ public GridNioServer build() throws IgniteCheckedException { sndQueueLimit, directMode, daemon, - metricsLsnr, writerFactory, skipRecoveryPred, msgQueueLsnr, readWriteSelectorsAssign, workerLsnr, + mreg, filters != null ? Arrays.copyOf(filters, filters.length) : EMPTY_FILTERS ); @@ -3912,16 +3950,6 @@ public Builder directMode(boolean directMode) { return this; } - /** - * @param metricsLsnr Metrics listener. - * @return This for chaining. - */ - public Builder metricsListener(GridNioMetricsListener metricsLsnr) { - this.metricsLsnr = metricsLsnr; - - return this; - } - /** * @param filters NIO filters. * @return This for chaining. @@ -4001,6 +4029,16 @@ public Builder workerListener(GridWorkerListener workerLsnr) { return this; } + + /** + * @param mreg Metrics registry. + * @return This for chaining. + */ + public Builder metricRegistry(MetricRegistry mreg) { + this.mreg = mreg; + + return this; + } } /** diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java index ac2fc6133d54c..87ecb301a5780 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java @@ -28,12 +28,17 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteLogger; +import org.apache.ignite.internal.processors.metric.MetricRegistry; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.util.tostring.GridToStringExclude; import org.apache.ignite.internal.util.typedef.internal.LT; import org.apache.ignite.internal.util.typedef.internal.S; import org.apache.ignite.util.deque.FastSizeDeque; import org.jetbrains.annotations.Nullable; +import static org.apache.ignite.internal.util.nio.GridNioServer.OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC; +import static org.apache.ignite.internal.util.nio.GridNioServer.OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME; + /** * Session implementation bound to selector API and socket API. * Note that this implementation requires non-null values for local and remote @@ -81,6 +86,9 @@ public class GridSelectorNioSessionImpl extends GridNioSessionImpl implements Gr /** Close channel on session #close() called. */ private volatile boolean closeSocket = true; + /** Outbound messages queue size metric. */ + @Nullable private final LongAdderMetric outboundMessagesQueueSizeMetric; + /** * Creates session instance. * @@ -102,6 +110,7 @@ public class GridSelectorNioSessionImpl extends GridNioSessionImpl implements Gr InetSocketAddress rmtAddr, boolean accepted, int sndQueueLimit, + @Nullable MetricRegistry mreg, @Nullable ByteBuffer writeBuf, @Nullable ByteBuffer readBuf ) { @@ -132,6 +141,11 @@ public class GridSelectorNioSessionImpl extends GridNioSessionImpl implements Gr this.readBuf = readBuf; } + + outboundMessagesQueueSizeMetric = mreg == null ? null : mreg.longAdderMetric( + OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME, + OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC + ); } /** {@inheritDoc} */ @@ -295,6 +309,9 @@ int offerSystemFuture(SessionWriteRequest writeFut) { assert res : "Future was not added to queue"; + if (outboundMessagesQueueSizeMetric != null) + outboundMessagesQueueSizeMetric.increment(); + return queue.sizex(); } @@ -320,6 +337,9 @@ int offerFuture(SessionWriteRequest writeFut) { assert res : "Future was not added to queue"; + if (outboundMessagesQueueSizeMetric != null) + outboundMessagesQueueSizeMetric.increment(); + return queue.sizex(); } @@ -332,6 +352,9 @@ void resend(Collection futs) { boolean add = queue.addAll(futs); assert add; + + if (outboundMessagesQueueSizeMetric != null) + outboundMessagesQueueSizeMetric.add(futs.size()); } /** @@ -341,6 +364,9 @@ void resend(Collection futs) { SessionWriteRequest last = queue.poll(); if (last != null) { + if (outboundMessagesQueueSizeMetric != null) + outboundMessagesQueueSizeMetric.decrement(); + if (sem != null && !last.messageThread()) sem.release(); @@ -371,7 +397,12 @@ void resend(Collection futs) { boolean removeFuture(SessionWriteRequest fut) { assert closed(); - return queue.removeLastOccurrence(fut); + boolean rmv = queue.removeLastOccurrence(fut); + + if (rmv && outboundMessagesQueueSizeMetric != null) + outboundMessagesQueueSizeMetric.decrement(); + + return rmv; } /** diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java index f93738c18d4c9..7e0578efdfcc8 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java @@ -26,6 +26,7 @@ import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteException; import org.apache.ignite.IgniteLogger; +import org.apache.ignite.internal.processors.metric.MetricRegistry; import org.apache.ignite.internal.util.ipc.shmem.IpcSharedMemoryClientEndpoint; import org.apache.ignite.internal.util.lang.IgniteInClosure2X; import org.apache.ignite.internal.util.typedef.internal.S; @@ -49,7 +50,7 @@ public class GridShmemCommunicationClient extends GridAbstractCommunicationClien /** * @param connIdx Connection index. - * @param metricsLsnr Metrics listener. + * @param mreg Metrics registry. * @param port Shared memory IPC server port. * @param connTimeout Connection timeout. * @param log Logger. @@ -58,15 +59,15 @@ public class GridShmemCommunicationClient extends GridAbstractCommunicationClien */ public GridShmemCommunicationClient( int connIdx, - GridNioMetricsListener metricsLsnr, + MetricRegistry mreg, int port, long connTimeout, IgniteLogger log, - MessageFormatter formatter) - throws IgniteCheckedException { - super(connIdx, metricsLsnr); + MessageFormatter formatter + ) throws IgniteCheckedException { + super(connIdx, mreg); - assert metricsLsnr != null; + assert mreg != null; assert port > 0 && port < 0xffff; assert connTimeout >= 0; @@ -111,7 +112,7 @@ public GridShmemCommunicationClient( try { shmem.outputStream().write(data, 0, len); - metricsLsnr.onBytesSent(len); + sentBytesCntMetric.add(len); } catch (IOException e) { throw new IgniteCheckedException("Failed to send message to remote node: " + shmem, e); @@ -133,7 +134,7 @@ public GridShmemCommunicationClient( try { int cnt = U.writeMessageFully(msg, shmem.outputStream(), writeBuf, formatter.writer(nodeId)); - metricsLsnr.onBytesSent(cnt); + sentBytesCntMetric.add(cnt); } catch (IOException e) { throw new IgniteCheckedException("Failed to send message to remote node: " + shmem, e); diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java index ac544bf0e71e4..38cd1bcb437de 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java @@ -67,40 +67,53 @@ public interface CommunicationSpi extends IgniteSpi { * Gets sent messages count. * * @return Sent messages count. + * @deprecated Will be removed in the next major release and replaced with new metrics API. */ + @Deprecated public int getSentMessagesCount(); /** * Gets sent bytes count. * * @return Sent bytes count. + * @deprecated Will be removed in the next major release and replaced with new metrics API. */ + @Deprecated public long getSentBytesCount(); /** * Gets received messages count. * * @return Received messages count. + * @deprecated Will be removed in the next major release and replaced with new metrics API. */ + @Deprecated public int getReceivedMessagesCount(); /** * Gets received bytes count. * * @return Received bytes count. + * @deprecated Will be removed in the next major release and replaced with new metrics API. */ + @Deprecated public long getReceivedBytesCount(); /** * Gets outbound messages queue size. * * @return Outbound messages queue size. + * @deprecated Will be removed in the next major release and replaced with new metrics API. */ + @Deprecated public int getOutboundMessagesQueueSize(); /** * Resets metrics for this SPI instance. + * + * @deprecated Will be removed in the next major release and replaced with new metrics API. */ + @Deprecated public void resetMetrics(); /** diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index 1ca54517b65ee..2f2044503c5fe 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -17,50 +17,80 @@ package org.apache.ignite.spi.communication.tcp; -import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Set; import java.util.UUID; -import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.LongAdder; - +import java.util.function.Function; import org.apache.ignite.internal.managers.communication.GridIoMessage; -import org.apache.ignite.internal.util.nio.GridNioMetricsListener; -import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.processors.metric.GridMetricManager; +import org.apache.ignite.internal.processors.metric.MetricRegistry; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.plugin.extensions.communication.Message; +import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_DESC; +import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_NAME; +import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_DESC; +import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_NAME; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.COMMUNICATION_METRICS_GROUP_NAME; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_METRIC_DESC; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_METRIC_NAME; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_BY_NODE_ID_METRIC_DESC; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_BY_NODE_ID_METRIC_NAME; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_BY_TYPE_METRIC_DESC; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_METRIC_DESC; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_METRIC_NAME; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.receivedMessagesByTypeMetricName; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.sentMessagesByTypeMetricName; + /** * Statistics for {@link org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi}. */ -public class TcpCommunicationMetricsListener implements GridNioMetricsListener{ - /** Counter factory. */ - private static final Callable HOLDER_FACTORY = new Callable() { - @Override public LongHolder call() { - return new LongHolder(); - } - }; +class TcpCommunicationMetricsListener { + /** Metrics manager. */ + private final GridMetricManager mmgr; - /** Received bytes count. */ - private final LongAdder rcvdBytesCnt = new LongAdder(); + /** Metrics registry. */ + private final MetricRegistry mreg; - /** Sent bytes count.*/ - private final LongAdder sentBytesCnt = new LongAdder(); + /** */ + private final Function sentMsgsCntByTypeMetricFactory; - /** All registered metrics. */ - private final Set allMetrics = Collections.newSetFromMap(new ConcurrentHashMap<>()); + /** */ + private final Function rcvdMsgsCntByTypeMetricFactory; - /** Thread-local metrics. */ - private final ThreadLocal threadMetrics = new ThreadLocal() { - @Override protected ThreadMetrics initialValue() { - ThreadMetrics metrics = new ThreadMetrics(); + /** */ + private final Function sentMsgsCntByNodeIdMetricFactory; - allMetrics.add(metrics); + /** */ + private final Function rcvdMsgsCntByNodeIdMetricFactory; - return metrics; - } - }; + /** Sent bytes count metric.*/ + private final LongAdderMetric sentBytesMetric; + + /** Received bytes count metric. */ + private final LongAdderMetric rcvdBytesMetric; + + /** Sent messages count metric. */ + private final LongAdderMetric sentMsgsMetric; + + /** Received messages count metric. */ + private final LongAdderMetric rcvdMsgsMetric; + + /** Sent messages count metrics grouped by message type. */ + ConcurrentHashMap sentMsgsMetricsByType = new ConcurrentHashMap<>(); + + /** Received messages count metrics grouped by message type. */ + ConcurrentHashMap rcvdMsgsMetricsByType = new ConcurrentHashMap<>(); + + /** Sent messages count metrics grouped by message node id. */ + ConcurrentHashMap sentMsgsMetricsByNodeId = new ConcurrentHashMap<>(); + + /** Received messages metrics count grouped by message node id. */ + ConcurrentHashMap rcvdMsgsMetricsByNodeId = new ConcurrentHashMap<>(); /** Method to synchronize access to message type map. */ private final Object msgTypMapMux = new Object(); @@ -68,14 +98,44 @@ public class TcpCommunicationMetricsListener implements GridNioMetricsListener{ /** Message type map. */ private volatile Map msgTypMap; - /** {@inheritDoc} */ - @Override public void onBytesSent(int bytesCnt) { - sentBytesCnt.add(bytesCnt); + + /** */ + public TcpCommunicationMetricsListener(GridMetricManager mmgr) { + this.mmgr = mmgr; + + mreg = mmgr.registry(COMMUNICATION_METRICS_GROUP_NAME); + + sentMsgsCntByTypeMetricFactory = directType -> mreg.longAdderMetric( + sentMessagesByTypeMetricName(directType), + SENT_MESSAGES_BY_TYPE_METRIC_DESC + ); + rcvdMsgsCntByTypeMetricFactory = directType -> mreg.longAdderMetric( + receivedMessagesByTypeMetricName(directType), + RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC + ); + + sentMsgsCntByNodeIdMetricFactory = nodeId -> mmgr.registry(COMMUNICATION_METRICS_GROUP_NAME + "." + nodeId) + .longAdderMetric( + SENT_MESSAGES_BY_NODE_ID_METRIC_NAME, + SENT_MESSAGES_BY_NODE_ID_METRIC_DESC + ); + + rcvdMsgsCntByNodeIdMetricFactory = nodeId -> mmgr.registry(COMMUNICATION_METRICS_GROUP_NAME + "." + nodeId) + .longAdderMetric( + RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME, + RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC + ); + + sentBytesMetric = mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + rcvdBytesMetric = mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + + sentMsgsMetric = mreg.longAdderMetric(SENT_MESSAGES_METRIC_NAME, SENT_MESSAGES_METRIC_DESC); + rcvdMsgsMetric = mreg.longAdderMetric(RECEIVED_MESSAGES_METRIC_NAME, RECEIVED_MESSAGES_METRIC_DESC); } - /** {@inheritDoc} */ - @Override public void onBytesReceived(int bytesCnt) { - rcvdBytesCnt.add(bytesCnt); + /** Metrics registry. */ + public MetricRegistry metricRegistry() { + return mreg; } /** @@ -93,9 +153,11 @@ public void onMessageSent(Message msg, UUID nodeId) { updateMessageTypeMap(msg); - ThreadMetrics metrics = threadMetrics.get(); + sentMsgsMetric.increment(); + + sentMsgsMetricsByType.computeIfAbsent(msg.directType(), sentMsgsCntByTypeMetricFactory).increment(); - metrics.onMessageSent(msg, nodeId); + sentMsgsMetricsByNodeId.computeIfAbsent(nodeId, sentMsgsCntByNodeIdMetricFactory).increment(); } } @@ -114,9 +176,11 @@ public void onMessageReceived(Message msg, UUID nodeId) { updateMessageTypeMap(msg); - ThreadMetrics metrics = threadMetrics.get(); + rcvdMsgsMetric.increment(); - metrics.onMessageReceived(msg, nodeId); + rcvdMsgsMetricsByType.computeIfAbsent(msg.directType(), rcvdMsgsCntByTypeMetricFactory).increment(); + + rcvdMsgsMetricsByNodeId.computeIfAbsent(nodeId, rcvdMsgsCntByNodeIdMetricFactory).increment(); } } @@ -126,17 +190,9 @@ public void onMessageReceived(Message msg, UUID nodeId) { * @return Sent messages count. */ public int sentMessagesCount() { - long res = 0; - - for (ThreadMetrics metrics : allMetrics) - res += metrics.sentMsgsCnt; + int res0 = (int)sentMsgsMetric.value(); - int res0 = (int)res; - - if (res0 < 0) - res0 = Integer.MAX_VALUE; - - return res0; + return res0 < 0 ? Integer.MAX_VALUE : res0; } /** @@ -145,7 +201,7 @@ public int sentMessagesCount() { * @return Sent bytes count. */ public long sentBytesCount() { - return sentBytesCnt.longValue(); + return sentBytesMetric.value(); } /** @@ -154,17 +210,9 @@ public long sentBytesCount() { * @return Received messages count. */ public int receivedMessagesCount() { - long res = 0; - - for (ThreadMetrics metrics : allMetrics) - res += metrics.rcvdMsgsCnt; + int res0 = (int)rcvdMsgsMetric.value(); - int res0 = (int)res; - - if (res0 < 0) - res0 = Integer.MAX_VALUE; - - return res0; + return res0 < 0 ? Integer.MAX_VALUE : res0; } /** @@ -173,21 +221,7 @@ public int receivedMessagesCount() { * @return Received bytes count. */ public long receivedBytesCount() { - return rcvdBytesCnt.longValue(); - } - - /** - * Gets received messages counts (grouped by type). - * - * @return Map containing message types and respective counts. - */ - public Map receivedMessagesByType() { - Map res = new HashMap<>(); - - for (ThreadMetrics metrics : allMetrics) - addMetrics(res, metrics.rcvdMsgsCntByType); - - return convertMessageTypes(res); + return rcvdBytesMetric.value(); } /** @@ -196,23 +230,32 @@ public Map receivedMessagesByType() { * @param input Input map. * @return Result map. */ - private Map convertMessageTypes(Map input) { + private Map convertMessageTypes(Map input) { Map res = new HashMap<>(input.size()); Map msgTypMap0 = msgTypMap; if (msgTypMap0 != null) { - for (Map.Entry inputEntry : input.entrySet()) { + for (Map.Entry inputEntry : input.entrySet()) { String typeName = msgTypMap0.get(inputEntry.getKey()); if (typeName != null) - res.put(typeName, inputEntry.getValue()); + res.put(typeName, inputEntry.getValue().value()); } } return res; } + /** + * Gets received messages counts (grouped by type). + * + * @return Map containing message types and respective counts. + */ + public Map receivedMessagesByType() { + return convertMessageTypes(rcvdMsgsMetricsByType); + } + /** * Gets received messages counts (grouped by node). * @@ -221,8 +264,8 @@ private Map convertMessageTypes(Map input) { public Map receivedMessagesByNode() { Map res = new HashMap<>(); - for (ThreadMetrics metrics : allMetrics) - addMetrics(res, metrics.rcvdMsgsCntByNode); + for (Map.Entry entry : rcvdMsgsMetricsByNodeId.entrySet()) + res.put(entry.getKey(), entry.getValue().value()); return res; } @@ -233,12 +276,7 @@ public Map receivedMessagesByNode() { * @return Map containing message types and respective counts. */ public Map sentMessagesByType() { - Map res = new HashMap<>(); - - for (ThreadMetrics metrics : allMetrics) - addMetrics(res, metrics.sentMsgsCntByType); - - return convertMessageTypes(res); + return convertMessageTypes(sentMsgsMetricsByType); } /** @@ -249,8 +287,8 @@ public Map sentMessagesByType() { public Map sentMessagesByNode() { Map res = new HashMap<>(); - for (ThreadMetrics metrics : allMetrics) - addMetrics(res, metrics.sentMsgsCntByNode); + for (Map.Entry entry : sentMsgsMetricsByNodeId.entrySet()) + res.put(entry.getKey(), entry.getValue().value()); return res; } @@ -259,28 +297,31 @@ public Map sentMessagesByNode() { * Resets metrics for this instance. */ public void resetMetrics() { - for (ThreadMetrics metrics : allMetrics) - metrics.reset(); + rcvdMsgsMetric.reset(); + sentMsgsMetric.reset(); - sentBytesCnt.reset(); - rcvdBytesCnt.reset(); - } + sentBytesMetric.reset(); + rcvdBytesMetric.reset(); - /** - * Add single metrics to the total. - * - * @param total Total. - * @param current Current metrics. - */ - private void addMetrics(Map total, Map current) { - for (Map.Entry entry : current.entrySet()) { - T key = entry.getKey(); - long val = entry.getValue().val; + for (LongAdderMetric metric : sentMsgsMetricsByType.values()) + metric.reset(); - Long prevVal = total.get(key); + for (LongAdderMetric metric : rcvdMsgsMetricsByType.values()) + metric.reset(); - total.put(key, prevVal == null ? val : prevVal + val); - } + for (LongAdderMetric metric : sentMsgsMetricsByNodeId.values()) + metric.reset(); + + for (LongAdderMetric metric : rcvdMsgsMetricsByNodeId.values()) + metric.reset(); + } + + /** */ + public void onNodeLeft(UUID nodeId) { + sentMsgsMetricsByNodeId.remove(nodeId); + rcvdMsgsMetricsByNodeId.remove(nodeId); + + mmgr.remove(COMMUNICATION_METRICS_GROUP_NAME + "." + nodeId); } /** @@ -314,94 +355,4 @@ private void updateMessageTypeMap(Message msg) { } } } - - /** - * Long value holder. - */ - private static class LongHolder { - /** Value. */ - private long val; - - /** - * Increment value. - */ - private void increment() { - val++; - } - } - - /** - * Thread-local metrics. - */ - private static class ThreadMetrics { - /** Received messages count. */ - private long rcvdMsgsCnt; - - /** Sent messages count.*/ - private long sentMsgsCnt; - - /** Received messages count grouped by message type. */ - private final HashMap rcvdMsgsCntByType = new HashMap<>(); - - /** Received messages count grouped by sender. */ - private final HashMap rcvdMsgsCntByNode = new HashMap<>(); - - /** Sent messages count grouped by message type. */ - private final HashMap sentMsgsCntByType = new HashMap<>(); - - /** Sent messages count grouped by receiver. */ - private final HashMap sentMsgsCntByNode = new HashMap<>(); - - /** - * Collects statistics for message sent by SPI. - * - * @param msg Sent message. - * @param nodeId Receiver node id. - */ - private void onMessageSent(Message msg, UUID nodeId) { - sentMsgsCnt++; - - LongHolder cntByType = F.addIfAbsent(sentMsgsCntByType, msg.directType(), HOLDER_FACTORY); - LongHolder cntByNode = F.addIfAbsent(sentMsgsCntByNode, nodeId, HOLDER_FACTORY); - - assert cntByType != null; - assert cntByNode != null; - - cntByType.increment(); - cntByNode.increment(); - } - - /** - * Collects statistics for message received by SPI. - * - * @param msg Received message. - * @param nodeId Sender node id. - */ - private void onMessageReceived(Message msg, UUID nodeId) { - rcvdMsgsCnt++; - - LongHolder cntByType = F.addIfAbsent(rcvdMsgsCntByType, msg.directType(), HOLDER_FACTORY); - LongHolder cntByNode = F.addIfAbsent(rcvdMsgsCntByNode, nodeId, HOLDER_FACTORY); - - assert cntByType != null; - assert cntByNode != null; - - cntByType.increment(); - cntByNode.increment(); - } - - /** - * Reset metrics. - */ - private void reset() { - rcvdMsgsCnt = 0; - sentMsgsCnt = 0; - - sentMsgsCntByType.clear(); - sentMsgsCntByNode.clear(); - - rcvdMsgsCntByType.clear(); - rcvdMsgsCntByNode.clear(); - } - } } diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java index 569798eddf07b..cfd303e21c6f6 100755 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java @@ -75,6 +75,8 @@ import org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi; import org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener; import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener; +import org.apache.ignite.internal.processors.metric.GridMetricManager; +import org.apache.ignite.internal.resources.MetricManagerResource; import org.apache.ignite.internal.util.GridConcurrentFactory; import org.apache.ignite.internal.util.GridSpinReadWriteLock; import org.apache.ignite.internal.util.future.GridFinishedFuture; @@ -385,11 +387,7 @@ public class TcpCommunicationSpi extends IgniteSpiAdapter implements Communicati public static final int MAX_CONN_PER_NODE = 1024; /** No-op runnable. */ - private static final IgniteRunnable NOOP = new IgniteRunnable() { - @Override public void run() { - // No-op. - } - }; + private static final IgniteRunnable NOOP = () -> {}; /** Node ID message type. */ public static final short NODE_ID_MSG_TYPE = -1; @@ -403,6 +401,50 @@ public class TcpCommunicationSpi extends IgniteSpiAdapter implements Communicati /** Handshake wait message type. */ public static final short HANDSHAKE_WAIT_MSG_TYPE = -28; + /** Communication metrics group name. */ + public static final String COMMUNICATION_METRICS_GROUP_NAME = "communication.tcp"; + + /** */ + public static final String SENT_MESSAGES_METRIC_NAME = "sentMessagesCount"; + + /** */ + public static final String SENT_MESSAGES_METRIC_DESC = "Total number of messages sent by current node"; + + /** */ + public static final String RECEIVED_MESSAGES_METRIC_NAME = "receivedMessagesCount"; + + /** */ + public static final String RECEIVED_MESSAGES_METRIC_DESC = "Total number of messages received by current node"; + + + /** */ + public static String sentMessagesByTypeMetricName(Short directType) { + return "sentMessagesByType." + directType; + } + + /** */ + public static final String SENT_MESSAGES_BY_TYPE_METRIC_DESC = "Total number of messages with given type sent by current node"; + + /** */ + public static String receivedMessagesByTypeMetricName(Short directType) { + return "receivedMessagesByType." + directType; + } + + /** */ + public static final String RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC = "Total number of messages with given type received by current node"; + + /** */ + public static final String SENT_MESSAGES_BY_NODE_ID_METRIC_NAME = "sentMessagesToNode"; + + /** */ + public static final String SENT_MESSAGES_BY_NODE_ID_METRIC_DESC = "Total number of messages sent by current node to the given node"; + + /** */ + public static final String RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME = "receivedMessagesFromNode"; + + /** */ + public static final String RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC = "Total number of messages received by current node from the given node"; + /** */ private ConnectGateway connectGate; @@ -1283,7 +1325,7 @@ class ConnectClosure implements IgniteInClosure { private volatile boolean stopping; /** Statistics. */ - private final TcpCommunicationMetricsListener metricsLsnr = new TcpCommunicationMetricsListener(); + private TcpCommunicationMetricsListener metricsLsnr; /** Client connect futures. */ private final ConcurrentMap> clientFuts = @@ -1347,6 +1389,13 @@ public AddressResolver getAddressResolver() { } } + /** */ + @MetricManagerResource + private void injectMetricManager(GridMetricManager mmgr) { + if (mmgr != null) + metricsLsnr = new TcpCommunicationMetricsListener(mmgr); + } + /** * Sets local host address for socket binding. Note that one node could have * additional addresses beside the loopback one. This configuration @@ -2528,7 +2577,6 @@ private GridNioServer resetNioServer() throws IgniteCheckedException { .socketReceiveBufferSize(sockRcvBuf) .sendQueueLimit(msgQueueLimit) .directMode(true) - .metricsListener(metricsLsnr) .writeTimeout(sockWriteTimeout) .selectorSpins(selectorSpins) .filters(filters) @@ -2540,7 +2588,8 @@ private GridNioServer resetNioServer() throws IgniteCheckedException { if (ignite instanceof IgniteEx) { IgniteEx igniteEx = (IgniteEx)ignite; - builder.workerListener(igniteEx.context().workersRegistry()); + builder.workerListener(igniteEx.context().workersRegistry()) + .metricRegistry(igniteEx.context().metric().registry(COMMUNICATION_METRICS_GROUP_NAME)); } GridNioServer srvr = builder.build(); @@ -2723,6 +2772,8 @@ private GridNioServer resetNioServer() throws IgniteCheckedException { void onNodeLeft(UUID nodeId) { assert nodeId != null; + metricsLsnr.onNodeLeft(nodeId); + GridCommunicationClient[] clients0 = clients.remove(nodeId); if (clients0 != null) { @@ -3181,7 +3232,7 @@ private String clientString(GridCommunicationClient client, ClusterNode node) th try { client = new GridShmemCommunicationClient( connIdx, - metricsLsnr, + metricsLsnr.metricRegistry(), port, timeoutHelper.nextTimeoutChunk(connTimeout), log, @@ -4511,7 +4562,7 @@ private ShmemWorker(IpcEndpoint endpoint) { }; IpcToNioAdapter adapter = new IpcToNioAdapter<>( - metricsLsnr, + metricsLsnr.metricRegistry(), log, endpoint, srvLsnr, diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridTestKernalContext.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridTestKernalContext.java index 17e1fed6b1d4b..5ba9678952cff 100644 --- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridTestKernalContext.java +++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridTestKernalContext.java @@ -31,10 +31,13 @@ import org.apache.ignite.internal.GridLoggerProxy; import org.apache.ignite.internal.IgniteKernal; import org.apache.ignite.internal.LongJVMPauseDetector; +import org.apache.ignite.internal.processors.metric.GridMetricManager; import org.apache.ignite.internal.processors.plugin.IgnitePluginProcessor; +import org.apache.ignite.internal.processors.resource.GridResourceProcessor; import org.apache.ignite.internal.util.typedef.internal.S; import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.plugin.PluginProvider; +import org.apache.ignite.spi.metric.noop.NoopMetricExporterSpi; import org.apache.ignite.testframework.GridTestUtils; /** @@ -92,6 +95,12 @@ public GridTestKernalContext(IgniteLogger log, IgniteConfiguration cfg) { GridTestUtils.setFieldValue(grid(), "ctx", this); config().setGridLogger(log); + + if (cfg.getMetricExporterSpi() == null || cfg.getMetricExporterSpi().length == 0) + cfg.setMetricExporterSpi(new NoopMetricExporterSpi()); + + add(new GridMetricManager(this)); + add(new GridResourceProcessor(this)); } /** diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java index a424779cf9500..41be037796a3f 100644 --- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java +++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java @@ -32,6 +32,7 @@ import org.apache.ignite.internal.binary.BinaryContext; import org.apache.ignite.internal.binary.BinaryMarshaller; import org.apache.ignite.internal.processors.resource.GridResourceProcessor; +import org.apache.ignite.internal.resources.MetricManagerResource; import org.apache.ignite.internal.util.IgniteUtils; import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.logger.NullLogger; @@ -74,6 +75,9 @@ public class IgniteTestResources { /** */ private IgniteConfiguration cfg; + /** */ + private GridTestKernalContext ctx; + /** */ private GridResourceProcessor rsrcProc; @@ -93,9 +97,11 @@ public IgniteTestResources() throws IgniteCheckedException { else log = rootLog.getLogger(getClass()); - this.jmx = prepareMBeanServer(); + jmx = prepareMBeanServer(); + + ctx = new GridTestKernalContext(log); - this.rsrcProc = new GridResourceProcessor(new GridTestKernalContext(this.log)); + rsrcProc = new GridResourceProcessor(ctx); } /** @@ -103,9 +109,10 @@ public IgniteTestResources() throws IgniteCheckedException { */ public IgniteTestResources(IgniteConfiguration cfg) throws IgniteCheckedException { this.cfg = cfg; - this.log = rootLog.getLogger(getClass()); - this.jmx = prepareMBeanServer(); - this.rsrcProc = new GridResourceProcessor(new GridTestKernalContext(this.log, this.cfg)); + log = rootLog.getLogger(getClass()); + jmx = prepareMBeanServer(); + ctx = new GridTestKernalContext(log, this.cfg); + rsrcProc = new GridResourceProcessor(ctx); } /** @@ -115,8 +122,9 @@ public IgniteTestResources(MBeanServer jmx) throws IgniteCheckedException { assert jmx != null; this.jmx = jmx; - this.log = rootLog.getLogger(getClass()); - this.rsrcProc = new GridResourceProcessor(new GridTestKernalContext(this.log)); + log = rootLog.getLogger(getClass()); + ctx = new GridTestKernalContext(log); + rsrcProc = new GridResourceProcessor(ctx); } /** @@ -126,8 +134,9 @@ public IgniteTestResources(IgniteLogger log) throws IgniteCheckedException { assert log != null; this.log = log.getLogger(getClass()); - this.jmx = prepareMBeanServer(); - this.rsrcProc = new GridResourceProcessor(new GridTestKernalContext(this.log)); + jmx = prepareMBeanServer(); + ctx = new GridTestKernalContext(log); + rsrcProc = new GridResourceProcessor(ctx); } /** @@ -185,6 +194,7 @@ public void inject(Object target) throws IgniteCheckedException { rsrcProc.injectBasicResource(target, LoggerResource.class, getLogger().getLogger(target.getClass())); rsrcProc.injectBasicResource(target, IgniteInstanceResource.class, new IgniteMock(null, locHost, nodeId, getMarshaller(), jmx, home, cfg)); + rsrcProc.injectBasicResource(target, MetricManagerResource.class, ctx.metric()); } /** From 6bb92fad6113261fffba89d16d6f532acc78253d Mon Sep 17 00:00:00 2001 From: ibessonov Date: Wed, 28 Aug 2019 17:10:20 +0300 Subject: [PATCH 02/18] IGNITE-12108 Checkstyle fixed. --- .../processors/resource/GridResourceSupplierInjector.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceSupplierInjector.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceSupplierInjector.java index 66e441f817242..679303b80660d 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceSupplierInjector.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceSupplierInjector.java @@ -61,4 +61,4 @@ class GridResourceSupplierInjector implements GridResourceInjector { @Override public String toString() { return S.toString(GridResourceSupplierInjector.class, this); } -} \ No newline at end of file +} From cc1145f5fb2b34ccf8d851aca7559e369335cdcd Mon Sep 17 00:00:00 2001 From: ibessonov Date: Wed, 25 Sep 2019 17:07:19 +0300 Subject: [PATCH 03/18] IGNITE-12108 nodeId replaced with consistentId for usability purposes --- .../internal/util/ipc/IpcToNioAdapter.java | 10 +- .../nio/GridAbstractCommunicationClient.java | 10 +- .../internal/util/nio/GridNioServer.java | 12 +- .../util/nio/GridSelectorNioSessionImpl.java | 6 +- .../tcp/TcpCommunicationMetricsListener.java | 140 ++++++++++++------ .../tcp/TcpCommunicationSpi.java | 68 +++++---- .../tcp/internal/ConnectionKey.java | 22 ++- ...TcpCommunicationConnectionCheckFuture.java | 15 +- ...CommunicationSpiMultithreadedSelfTest.java | 2 +- .../tcp/TcpCommunicationStatisticsTest.java | 42 +++++- 10 files changed, 217 insertions(+), 110 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java index 2abfea74bfd8b..fe46de6dd2523 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java @@ -27,7 +27,7 @@ import org.apache.ignite.IgniteException; import org.apache.ignite.IgniteLogger; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; +import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; import org.apache.ignite.internal.util.nio.GridNioFilter; import org.apache.ignite.internal.util.nio.GridNioFilterAdapter; import org.apache.ignite.internal.util.nio.GridNioFilterChain; @@ -70,10 +70,10 @@ public class IpcToNioAdapter { private final ByteBuffer writeBuf; /** Received bytes count metric. */ - private final LongAdderMetric rcvdBytesCntMetric; + private final AtomicLongMetric rcvdBytesCntMetric; /** Sent bytes count metric. */ - private final LongAdderMetric sentBytesCntMetric; + private final AtomicLongMetric sentBytesCntMetric; /** */ private final GridNioMessageWriterFactory writerFactory; @@ -91,9 +91,9 @@ public IpcToNioAdapter(MetricRegistry mreg, IgniteLogger log, IpcEndpoint endp, ) { assert mreg != null; - rcvdBytesCntMetric = mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + rcvdBytesCntMetric = mreg.longMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); - sentBytesCntMetric = mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + sentBytesCntMetric = mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); this.endp = endp; this.writerFactory = writerFactory; diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java index a3ccf0c6de209..670ef152e6883 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java @@ -19,7 +19,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; +import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; import org.apache.ignite.internal.util.typedef.internal.S; import org.apache.ignite.internal.util.typedef.internal.U; import org.jetbrains.annotations.Nullable; @@ -43,10 +43,10 @@ public abstract class GridAbstractCommunicationClient implements GridCommunicati private final int connIdx; /** Received bytes count metric. */ - @Nullable protected final LongAdderMetric rcvdBytesCntMetric; + @Nullable protected final AtomicLongMetric rcvdBytesCntMetric; /** Sent bytes count metric. */ - @Nullable protected final LongAdderMetric sentBytesCntMetric; + @Nullable protected final AtomicLongMetric sentBytesCntMetric; /** * @param connIdx Connection index. @@ -56,10 +56,10 @@ protected GridAbstractCommunicationClient(int connIdx, @Nullable MetricRegistry this.connIdx = connIdx; rcvdBytesCntMetric = mreg == null ? - null : mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + null : mreg.longMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); sentBytesCntMetric = mreg == null ? - null : mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + null : mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java index 347d6153c43fb..a3c378fda29ba 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java @@ -56,7 +56,7 @@ import org.apache.ignite.internal.IgniteInterruptedCheckedException; import org.apache.ignite.internal.managers.communication.GridIoMessage; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; +import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; import org.apache.ignite.internal.util.GridConcurrentHashSet; import org.apache.ignite.internal.util.GridUnsafe; import org.apache.ignite.internal.util.future.GridCompoundFuture; @@ -237,10 +237,10 @@ public class GridNioServer { @Nullable private final MetricRegistry mreg; /** Received bytes count metric. */ - @Nullable private final LongAdderMetric rcvdBytesCntMetric; + @Nullable private final AtomicLongMetric rcvdBytesCntMetric; /** Sent bytes count metric. */ - @Nullable private final LongAdderMetric sentBytesCntMetric; + @Nullable private final AtomicLongMetric sentBytesCntMetric; /** Sessions. */ @@ -434,10 +434,10 @@ private GridNioServer( this.mreg = mreg; rcvdBytesCntMetric = mreg == null ? - null : mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + null : mreg.longMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); sentBytesCntMetric = mreg == null ? - null : mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + null : mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); } /** @@ -2902,7 +2902,7 @@ public int outboundMessagesQueueSize() { if (mreg == null) return -1; - return (int) mreg.longAdderMetric( + return (int) mreg.longMetric( OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME, OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC ).value(); diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java index 87ecb301a5780..f83b35a6c6e87 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java @@ -29,7 +29,7 @@ import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteLogger; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; +import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; import org.apache.ignite.internal.util.tostring.GridToStringExclude; import org.apache.ignite.internal.util.typedef.internal.LT; import org.apache.ignite.internal.util.typedef.internal.S; @@ -87,7 +87,7 @@ public class GridSelectorNioSessionImpl extends GridNioSessionImpl implements Gr private volatile boolean closeSocket = true; /** Outbound messages queue size metric. */ - @Nullable private final LongAdderMetric outboundMessagesQueueSizeMetric; + @Nullable private final AtomicLongMetric outboundMessagesQueueSizeMetric; /** * Creates session instance. @@ -142,7 +142,7 @@ public class GridSelectorNioSessionImpl extends GridNioSessionImpl implements Gr this.readBuf = readBuf; } - outboundMessagesQueueSizeMetric = mreg == null ? null : mreg.longAdderMetric( + outboundMessagesQueueSizeMetric = mreg == null ? null : mreg.longMetric( OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME, OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC ); diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index 2f2044503c5fe..6bc9422bebee2 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -21,11 +21,13 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; import org.apache.ignite.internal.managers.communication.GridIoMessage; import org.apache.ignite.internal.processors.metric.GridMetricManager; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; +import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; +import org.apache.ignite.internal.processors.metric.impl.MetricUtils; import org.apache.ignite.plugin.extensions.communication.Message; import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_DESC; @@ -57,40 +59,54 @@ class TcpCommunicationMetricsListener { private final MetricRegistry mreg; /** */ - private final Function sentMsgsCntByTypeMetricFactory; + private final Function sentMsgsCntByTypeMetricFactory; /** */ - private final Function rcvdMsgsCntByTypeMetricFactory; + private final Function rcvdMsgsCntByTypeMetricFactory; /** */ - private final Function sentMsgsCntByNodeIdMetricFactory; + private final Function sentMsgsCntByConsistentIdMetricFactory; /** */ - private final Function rcvdMsgsCntByNodeIdMetricFactory; + private final Function rcvdMsgsCntByConsistentIdMetricFactory; /** Sent bytes count metric.*/ - private final LongAdderMetric sentBytesMetric; + private final AtomicLongMetric sentBytesMetric; /** Received bytes count metric. */ - private final LongAdderMetric rcvdBytesMetric; + private final AtomicLongMetric rcvdBytesMetric; /** Sent messages count metric. */ - private final LongAdderMetric sentMsgsMetric; + private final AtomicLongMetric sentMsgsMetric; /** Received messages count metric. */ - private final LongAdderMetric rcvdMsgsMetric; + private final AtomicLongMetric rcvdMsgsMetric; /** Sent messages count metrics grouped by message type. */ - ConcurrentHashMap sentMsgsMetricsByType = new ConcurrentHashMap<>(); + ConcurrentHashMap sentMsgsMetricsByType = new ConcurrentHashMap<>(); /** Received messages count metrics grouped by message type. */ - ConcurrentHashMap rcvdMsgsMetricsByType = new ConcurrentHashMap<>(); + ConcurrentHashMap rcvdMsgsMetricsByType = new ConcurrentHashMap<>(); - /** Sent messages count metrics grouped by message node id. */ - ConcurrentHashMap sentMsgsMetricsByNodeId = new ConcurrentHashMap<>(); + /** + * Sent messages count metrics grouped by message node id. + * @deprecated for JMX support only. + */ + @Deprecated + ConcurrentHashMap sentMsgsMetricsByNodeId = new ConcurrentHashMap<>(); + + /** + * Received messages metrics count grouped by message node id. + * @deprecated for JMX support only. + */ + @Deprecated + ConcurrentHashMap rcvdMsgsMetricsByNodeId = new ConcurrentHashMap<>(); + + /** Sent messages count metrics grouped by message node consistent id. */ + ConcurrentHashMap sentMsgsMetricsByConsistentId = new ConcurrentHashMap<>(); - /** Received messages metrics count grouped by message node id. */ - ConcurrentHashMap rcvdMsgsMetricsByNodeId = new ConcurrentHashMap<>(); + /** Received messages metrics count grouped by message node consistent id. */ + ConcurrentHashMap rcvdMsgsMetricsByConsistentId = new ConcurrentHashMap<>(); /** Method to synchronize access to message type map. */ private final Object msgTypMapMux = new Object(); @@ -105,32 +121,45 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { mreg = mmgr.registry(COMMUNICATION_METRICS_GROUP_NAME); - sentMsgsCntByTypeMetricFactory = directType -> mreg.longAdderMetric( + sentMsgsCntByTypeMetricFactory = directType -> mreg.longMetric( sentMessagesByTypeMetricName(directType), SENT_MESSAGES_BY_TYPE_METRIC_DESC ); - rcvdMsgsCntByTypeMetricFactory = directType -> mreg.longAdderMetric( + rcvdMsgsCntByTypeMetricFactory = directType -> mreg.longMetric( receivedMessagesByTypeMetricName(directType), RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC ); - sentMsgsCntByNodeIdMetricFactory = nodeId -> mmgr.registry(COMMUNICATION_METRICS_GROUP_NAME + "." + nodeId) - .longAdderMetric( - SENT_MESSAGES_BY_NODE_ID_METRIC_NAME, - SENT_MESSAGES_BY_NODE_ID_METRIC_DESC - ); - rcvdMsgsCntByNodeIdMetricFactory = nodeId -> mmgr.registry(COMMUNICATION_METRICS_GROUP_NAME + "." + nodeId) - .longAdderMetric( - RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME, - RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC - ); + sentMsgsCntByConsistentIdMetricFactory = consistentId -> getOrCreateMetricRegistry(mmgr, consistentId) + .findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); + + + rcvdMsgsCntByConsistentIdMetricFactory = consistentId -> getOrCreateMetricRegistry(mmgr, consistentId) + .findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); + + sentBytesMetric = mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + rcvdBytesMetric = mreg.longMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + + sentMsgsMetric = mreg.longMetric(SENT_MESSAGES_METRIC_NAME, SENT_MESSAGES_METRIC_DESC); + rcvdMsgsMetric = mreg.longMetric(RECEIVED_MESSAGES_METRIC_NAME, RECEIVED_MESSAGES_METRIC_DESC); + } + + /** */ + private static synchronized MetricRegistry getOrCreateMetricRegistry(GridMetricManager mmgr, Object consistentId) { + String regName = MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, consistentId.toString()); + + for (MetricRegistry mreg : mmgr) { + if (mreg.name().equals(regName)) + return mreg; + } + + MetricRegistry mreg = mmgr.registry(regName); - sentBytesMetric = mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); - rcvdBytesMetric = mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + mreg.longMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME, SENT_MESSAGES_BY_NODE_ID_METRIC_DESC); + mreg.longMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME, RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC); - sentMsgsMetric = mreg.longAdderMetric(SENT_MESSAGES_METRIC_NAME, SENT_MESSAGES_METRIC_DESC); - rcvdMsgsMetric = mreg.longAdderMetric(RECEIVED_MESSAGES_METRIC_NAME, RECEIVED_MESSAGES_METRIC_DESC); + return mreg; } /** Metrics registry. */ @@ -142,9 +171,10 @@ public MetricRegistry metricRegistry() { * Collects statistics for message sent by SPI. * * @param msg Sent message. + * @param consistentId Receiver node consistent id. * @param nodeId Receiver node id. */ - public void onMessageSent(Message msg, UUID nodeId) { + public void onMessageSent(Message msg, Object consistentId, UUID nodeId) { assert msg != null; assert nodeId != null; @@ -157,7 +187,9 @@ public void onMessageSent(Message msg, UUID nodeId) { sentMsgsMetricsByType.computeIfAbsent(msg.directType(), sentMsgsCntByTypeMetricFactory).increment(); - sentMsgsMetricsByNodeId.computeIfAbsent(nodeId, sentMsgsCntByNodeIdMetricFactory).increment(); + sentMsgsMetricsByConsistentId.computeIfAbsent(consistentId, sentMsgsCntByConsistentIdMetricFactory).increment(); + + sentMsgsMetricsByNodeId.computeIfAbsent(nodeId, id -> new AtomicLong()).incrementAndGet(); } } @@ -165,9 +197,10 @@ public void onMessageSent(Message msg, UUID nodeId) { * Collects statistics for message received by SPI. * * @param msg Received message. + * @param consistentId Sender node consistent id. * @param nodeId Sender node id. */ - public void onMessageReceived(Message msg, UUID nodeId) { + public void onMessageReceived(Message msg, Object consistentId, UUID nodeId) { assert msg != null; assert nodeId != null; @@ -180,7 +213,9 @@ public void onMessageReceived(Message msg, UUID nodeId) { rcvdMsgsMetricsByType.computeIfAbsent(msg.directType(), rcvdMsgsCntByTypeMetricFactory).increment(); - rcvdMsgsMetricsByNodeId.computeIfAbsent(nodeId, rcvdMsgsCntByNodeIdMetricFactory).increment(); + rcvdMsgsMetricsByConsistentId.computeIfAbsent(consistentId, rcvdMsgsCntByConsistentIdMetricFactory).increment(); + + rcvdMsgsMetricsByNodeId.computeIfAbsent(nodeId, id -> new AtomicLong()).incrementAndGet(); } } @@ -230,13 +265,13 @@ public long receivedBytesCount() { * @param input Input map. * @return Result map. */ - private Map convertMessageTypes(Map input) { + private Map convertMessageTypes(Map input) { Map res = new HashMap<>(input.size()); Map msgTypMap0 = msgTypMap; if (msgTypMap0 != null) { - for (Map.Entry inputEntry : input.entrySet()) { + for (Map.Entry inputEntry : input.entrySet()) { String typeName = msgTypMap0.get(inputEntry.getKey()); if (typeName != null) @@ -264,8 +299,8 @@ public Map receivedMessagesByType() { public Map receivedMessagesByNode() { Map res = new HashMap<>(); - for (Map.Entry entry : rcvdMsgsMetricsByNodeId.entrySet()) - res.put(entry.getKey(), entry.getValue().value()); + for (Map.Entry entry : rcvdMsgsMetricsByNodeId.entrySet()) + res.put(entry.getKey(), entry.getValue().longValue()); return res; } @@ -287,8 +322,8 @@ public Map sentMessagesByType() { public Map sentMessagesByNode() { Map res = new HashMap<>(); - for (Map.Entry entry : sentMsgsMetricsByNodeId.entrySet()) - res.put(entry.getKey(), entry.getValue().value()); + for (Map.Entry entry : sentMsgsMetricsByNodeId.entrySet()) + res.put(entry.getKey(), entry.getValue().longValue()); return res; } @@ -303,25 +338,34 @@ public void resetMetrics() { sentBytesMetric.reset(); rcvdBytesMetric.reset(); - for (LongAdderMetric metric : sentMsgsMetricsByType.values()) + for (AtomicLongMetric metric : sentMsgsMetricsByType.values()) metric.reset(); - for (LongAdderMetric metric : rcvdMsgsMetricsByType.values()) + for (AtomicLongMetric metric : rcvdMsgsMetricsByType.values()) metric.reset(); - for (LongAdderMetric metric : sentMsgsMetricsByNodeId.values()) + for (AtomicLongMetric metric : sentMsgsMetricsByConsistentId.values()) metric.reset(); - for (LongAdderMetric metric : rcvdMsgsMetricsByNodeId.values()) + for (AtomicLongMetric metric : rcvdMsgsMetricsByConsistentId.values()) metric.reset(); + + sentMsgsMetricsByNodeId.clear(); + rcvdMsgsMetricsByNodeId.clear(); } - /** */ - public void onNodeLeft(UUID nodeId) { + /** + * @param consistentId Consistent id of the node. + * @param nodeId Left node id. + */ + public void onNodeLeft(Object consistentId, UUID nodeId) { sentMsgsMetricsByNodeId.remove(nodeId); rcvdMsgsMetricsByNodeId.remove(nodeId); - mmgr.remove(COMMUNICATION_METRICS_GROUP_NAME + "." + nodeId); + sentMsgsMetricsByConsistentId.remove(consistentId); + rcvdMsgsMetricsByConsistentId.remove(consistentId); + + mmgr.remove(MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, consistentId.toString())); } /** diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java index e9638c245f352..d450e1db778a3 100755 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java @@ -76,6 +76,7 @@ import org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener; import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener; import org.apache.ignite.internal.processors.metric.GridMetricManager; +import org.apache.ignite.internal.processors.metric.impl.MetricUtils; import org.apache.ignite.internal.resources.MetricManagerResource; import org.apache.ignite.internal.util.GridConcurrentFactory; import org.apache.ignite.internal.util.GridSpinReadWriteLock; @@ -416,20 +417,9 @@ public class TcpCommunicationSpi extends IgniteSpiAdapter implements Communicati /** */ public static final String RECEIVED_MESSAGES_METRIC_DESC = "Total number of messages received by current node"; - - /** */ - public static String sentMessagesByTypeMetricName(Short directType) { - return "sentMessagesByType." + directType; - } - /** */ public static final String SENT_MESSAGES_BY_TYPE_METRIC_DESC = "Total number of messages with given type sent by current node"; - /** */ - public static String receivedMessagesByTypeMetricName(Short directType) { - return "receivedMessagesByType." + directType; - } - /** */ public static final String RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC = "Total number of messages with given type received by current node"; @@ -445,6 +435,16 @@ public static String receivedMessagesByTypeMetricName(Short directType) { /** */ public static final String RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC = "Total number of messages received by current node from the given node"; + /** */ + public static String sentMessagesByTypeMetricName(Short directType) { + return MetricUtils.metricName("sentMessagesByType", directType.toString()); + } + + /** */ + public static String receivedMessagesByTypeMetricName(Short directType) { + return MetricUtils.metricName("receivedMessagesByType", directType.toString()); + } + /** */ private ConnectGateway connectGate; @@ -566,12 +566,13 @@ public static String receivedMessagesByTypeMetricName(Short directType) { */ private void onFirstMessage(final GridNioSession ses, Message msg) { UUID sndId; - - ConnectionKey connKey; + int connIdx; + long connCnt; if (msg instanceof NodeIdMessage) { sndId = U.bytesToUuid(((NodeIdMessage)msg).nodeIdBytes(), 0); - connKey = new ConnectionKey(sndId, 0, -1); + connIdx = 0; + connCnt = -1; } else { assert msg instanceof HandshakeMessage : msg; @@ -579,7 +580,8 @@ private void onFirstMessage(final GridNioSession ses, Message msg) { HandshakeMessage msg0 = (HandshakeMessage)msg; sndId = ((HandshakeMessage)msg).nodeId(); - connKey = new ConnectionKey(sndId, msg0.connectionIndex(), msg0.connectCount()); + connIdx = msg0.connectionIndex(); + connCnt = msg0.connectCount(); } if (log.isDebugEnabled()) @@ -627,6 +629,8 @@ else if (discoverySpi instanceof IgniteDiscoverySpi) return; } + ConnectionKey connKey = new ConnectionKey(rmtNode.consistentId(), sndId, connIdx, connCnt); + final ConnectionKey old = ses.addMeta(CONN_IDX_META, connKey); assert old == null; @@ -790,11 +794,8 @@ private void closeStaleConnections(ConnectionKey connKey) { @Override public void onMessageSent(GridNioSession ses, Message msg) { ConnectionKey connKey = ses.meta(CONN_IDX_META); - if (connKey != null) { - UUID nodeId = connKey.nodeId(); - - metricsLsnr.onMessageSent(msg, nodeId); - } + if (connKey != null) + metricsLsnr.onMessageSent(msg, connKey.consistentId(), connKey.nodeId()); } private void onChannelCreate( @@ -886,7 +887,7 @@ private void onChannelCreate( } if (msg instanceof RecoveryLastReceivedMessage) { - metricsLsnr.onMessageReceived(msg, connKey.nodeId()); + metricsLsnr.onMessageReceived(msg, connKey.consistentId(), connKey.nodeId()); GridNioRecoveryDescriptor recovery = ses.outRecoveryDescriptor(); @@ -937,7 +938,7 @@ else if (connKey.dummy()) { } } - metricsLsnr.onMessageReceived(msg, connKey.nodeId()); + metricsLsnr.onMessageReceived(msg, connKey.consistentId(), connKey.nodeId()); IgniteRunnable c; @@ -2769,12 +2770,13 @@ private GridNioServer resetNioServer() throws IgniteCheckedException { } /** + * @param consistentId Consistent id of the node. * @param nodeId Left node ID. */ - void onNodeLeft(UUID nodeId) { + void onNodeLeft(Object consistentId, UUID nodeId) { assert nodeId != null; - metricsLsnr.onNodeLeft(nodeId); + metricsLsnr.onNodeLeft(consistentId, nodeId); GridCommunicationClient[] clients0 = clients.remove(nodeId); @@ -3042,7 +3044,7 @@ private GridCommunicationClient reserveClient(ClusterNode node, int connIdx) thr // Do not allow concurrent connects. GridFutureAdapter fut = new ConnectFuture(); - ConnectionKey connKey = new ConnectionKey(nodeId, connIdx, -1); + ConnectionKey connKey = new ConnectionKey(node.consistentId(), nodeId, connIdx, -1); GridFutureAdapter oldFut = clientFuts.putIfAbsent(connKey, fut); @@ -3524,7 +3526,7 @@ private GridNioSession createNioSession(ClusterNode node, int connIdx) throws Ig if (sockSndBuf > 0) ch.socket().setSendBufferSize(sockSndBuf); - ConnectionKey connKey = new ConnectionKey(node.id(), connIdx, -1); + ConnectionKey connKey = new ConnectionKey(node.consistentId(), node.id(), connIdx, -1); GridNioRecoveryDescriptor recoveryDesc = outRecoveryDescriptor(node, connKey); @@ -4417,7 +4419,7 @@ public IgniteInternalFuture openChannel( assert nodeSupports(remote, CHANNEL_COMMUNICATION) : "Node doesn't support direct connection over socket channel " + "[nodeId=" + remote.id() + ']'; - ConnectionKey key = new ConnectionKey(remote.id(), chConnPlc.connectionIndex()); + ConnectionKey key = new ConnectionKey(remote.consistentId(), remote.id(), chConnPlc.connectionIndex()); GridFutureAdapter chFut = new GridFutureAdapter<>(); @@ -4494,7 +4496,9 @@ private class DiscoveryListener implements GridLocalEventListener, HighPriorityL assert evt instanceof DiscoveryEvent : evt; assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED; - onNodeLeft(((DiscoveryEvent)evt).eventNode().id()); + ClusterNode node = ((DiscoveryEvent)evt).eventNode(); + + onNodeLeft(node.consistentId(), node.id()); } /** {@inheritDoc} */ @@ -4700,7 +4704,9 @@ private void processIdle() { GridNioRecoveryDescriptor recovery = null; if (!usePairedConnections(node) && client instanceof GridTcpNioCommunicationClient) { - recovery = recoveryDescs.get(new ConnectionKey(node.id(), client.connectionIndex(), -1)); + recovery = recoveryDescs.get(new ConnectionKey( + node.consistentId(), node.id(), client.connectionIndex(), -1) + ); if (recovery != null && recovery.lastAcknowledged() != recovery.received()) { RecoveryLastReceivedMessage msg = new RecoveryLastReceivedMessage(recovery.received()); @@ -4726,7 +4732,9 @@ private void processIdle() { if (idleTime >= idleConnTimeout) { if (recovery == null && usePairedConnections(node)) - recovery = outRecDescs.get(new ConnectionKey(node.id(), client.connectionIndex(), -1)); + recovery = outRecDescs.get(new ConnectionKey( + node.consistentId(), node.id(), client.connectionIndex(), -1) + ); if (recovery != null && recovery.nodeAlive(getSpiContext().node(nodeId)) && diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/ConnectionKey.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/ConnectionKey.java index a107c876b26e8..8cece65e21797 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/ConnectionKey.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/ConnectionKey.java @@ -25,6 +25,9 @@ * Connection Key. */ public class ConnectionKey { + /** */ + private final Object consistentId; + /** */ private final UUID nodeId; @@ -38,32 +41,36 @@ public class ConnectionKey { private final boolean dummy; /** + * @param consistentId Consistent id of the node. * @param nodeId Node ID. Should be not null. * @param idx Connection index. */ - public ConnectionKey(@NotNull UUID nodeId, int idx) { - this(nodeId, idx, -1, false); + public ConnectionKey(@NotNull Object consistentId, @NotNull UUID nodeId, int idx) { + this(consistentId, nodeId, idx, -1, false); } /** * Creates ConnectionKey with false value of dummy flag. * + * @param consistentId Consistent id of the node. * @param nodeId Node ID. Should be not null. * @param idx Connection index. * @param connCnt Connection counter (set only for incoming connections). */ - public ConnectionKey(@NotNull UUID nodeId, int idx, long connCnt) { - this(nodeId, idx, connCnt, false); + public ConnectionKey(@NotNull Object consistentId, @NotNull UUID nodeId, int idx, long connCnt) { + this(consistentId, nodeId, idx, connCnt, false); } /** + * @param consistentId Consistent id of the node. * @param nodeId Node ID. Should be not null. * @param idx Connection index. * @param connCnt Connection counter (set only for incoming connections). * @param dummy Indicates that session with this ConnectionKey is temporary * (for now dummy sessions are used only for Communication Failure Resolving process). */ - public ConnectionKey(@NotNull UUID nodeId, int idx, long connCnt, boolean dummy) { + public ConnectionKey(@NotNull Object consistentId, @NotNull UUID nodeId, int idx, long connCnt, boolean dummy) { + this.consistentId = consistentId; this.nodeId = nodeId; this.idx = idx; this.connCnt = connCnt; @@ -84,6 +91,11 @@ public UUID nodeId() { return nodeId; } + /** */ + public Object consistentId() { + return consistentId; + } + /** * @return Connection index. */ diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/TcpCommunicationConnectionCheckFuture.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/TcpCommunicationConnectionCheckFuture.java index 46fdb0b155c48..7530d0dcc0e29 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/TcpCommunicationConnectionCheckFuture.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/TcpCommunicationConnectionCheckFuture.java @@ -142,14 +142,14 @@ public void init(long timeout) { if (addrs.size() == 1) { SingleAddressConnectFuture fut = new SingleAddressConnectFuture(i); - fut.init(addrs.iterator().next(), node.id()); + fut.init(addrs.iterator().next(), node.consistentId(), node.id()); futs[i] = fut; } else { MultipleAddressesConnectFuture fut = new MultipleAddressesConnectFuture(i); - fut.init(addrs, node.id()); + fut.init(addrs, node.consistentId(), node.id()); futs[i] = fut; } @@ -292,9 +292,10 @@ private class SingleAddressConnectFuture implements TcpCommunicationNodeConnecti /** * @param addr Node address. + * @param consistentId * @param rmtNodeId Id of node to open connection check session with. */ - public void init(InetSocketAddress addr, UUID rmtNodeId) { + public void init(InetSocketAddress addr, Object consistentId, UUID rmtNodeId) { boolean connect; try { @@ -317,7 +318,8 @@ public void init(InetSocketAddress addr, UUID rmtNodeId) { sesMeta = new GridLeanMap<>(3); // Set dummy key to identify connection-check outgoing connection. - sesMeta.put(TcpCommunicationSpi.CONN_IDX_META, new ConnectionKey(rmtNodeId, -1, -1, true)); + ConnectionKey connKey = new ConnectionKey(consistentId, rmtNodeId, -1, -1, true); + sesMeta.put(TcpCommunicationSpi.CONN_IDX_META, connKey); sesMeta.put(SES_FUT_META, this); nioSrvr.createSession(ch, sesMeta, true, new IgniteInClosure>() { @@ -422,9 +424,10 @@ private class MultipleAddressesConnectFuture implements ConnectFuture { /** * @param addrs Node addresses. + * @param consistentId Consistent if of the node. * @param rmtNodeId Id of node to open connection check session with. */ - void init(Collection addrs, UUID rmtNodeId) { + void init(Collection addrs, Object consistentId, UUID rmtNodeId) { SingleAddressConnectFuture[] futs = new SingleAddressConnectFuture[addrs.size()]; for (int i = 0; i < addrs.size(); i++) { @@ -442,7 +445,7 @@ void init(Collection addrs, UUID rmtNodeId) { int idx = 0; for (InetSocketAddress addr : addrs) { - futs[idx++].init(addr, rmtNodeId); + futs[idx++].init(addr, consistentId, rmtNodeId); if (resCnt == Integer.MAX_VALUE) return; diff --git a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiMultithreadedSelfTest.java b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiMultithreadedSelfTest.java index cfde86b318f74..a53b43b61efb7 100644 --- a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiMultithreadedSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiMultithreadedSelfTest.java @@ -349,7 +349,7 @@ public void testFlowSend() throws Exception { while (run.get() && !Thread.currentThread().isInterrupted()) { U.sleep(interval * 3 / 2); - ((TcpCommunicationSpi)spis.get(from.id())).onNodeLeft(to.id()); + ((TcpCommunicationSpi)spis.get(from.id())).onNodeLeft(to.consistentId(), to.id()); } } catch (IgniteInterruptedCheckedException ignored) { diff --git a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java index b84211dc73f2a..6cb59457731f1 100644 --- a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java +++ b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java @@ -34,6 +34,9 @@ import org.apache.ignite.internal.managers.communication.GridIoMessage; import org.apache.ignite.internal.managers.communication.GridIoMessageFactory; import org.apache.ignite.internal.managers.communication.GridIoPolicy; +import org.apache.ignite.internal.processors.metric.MetricRegistry; +import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; +import org.apache.ignite.internal.processors.metric.impl.MetricUtils; import org.apache.ignite.internal.util.typedef.CO; import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.lang.IgniteCallable; @@ -45,6 +48,10 @@ import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import org.junit.Test; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.COMMUNICATION_METRICS_GROUP_NAME; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_BY_NODE_ID_METRIC_NAME; + /** * Test for TcpCommunicationSpi statistics. */ @@ -53,7 +60,7 @@ public class TcpCommunicationStatisticsTest extends GridCommonAbstractTest { private final Object mux = new Object(); /** */ - private final CountDownLatch latch = new CountDownLatch(1); + private CountDownLatch latch = new CountDownLatch(1); static { GridIoMessageFactory.registerCustom(GridTestMessage.DIRECT_TYPE, new CO() { @@ -95,6 +102,8 @@ private class SynchronizedCommunicationSpi extends TcpCommunicationSpi { @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); + cfg.setConsistentId(igniteInstanceName); + TcpCommunicationSpi spi = new SynchronizedCommunicationSpi(); cfg.setCommunicationSpi(spi); @@ -132,6 +141,19 @@ public void testStatistics() throws Exception { startGrids(2); try { + Object node0consistentId = grid(0).localNode().consistentId(); + Object node1consistentId = grid(1).localNode().consistentId(); + + String node0regName = MetricUtils.metricName( + COMMUNICATION_METRICS_GROUP_NAME, + node0consistentId.toString() + ); + + String node1regName = MetricUtils.metricName( + COMMUNICATION_METRICS_GROUP_NAME, + node1consistentId.toString() + ); + // Send custom message from node0 to node1. grid(0).context().io().sendToGridTopic(grid(1).cluster().localNode(), GridTopic.TOPIC_IO_TEST, new GridTestMessage(), GridIoPolicy.PUBLIC_POOL); @@ -179,6 +201,24 @@ public void testStatistics() throws Exception { assertEquals(1, msgsSentByType0.get(GridTestMessage.class.getName()).longValue()); assertEquals(1, msgsReceivedByType1.get(GridTestMessage.class.getName()).longValue()); + + MetricRegistry mreg0 = grid(0).context().metric().registry(node1regName); + MetricRegistry mreg1 = grid(1).context().metric().registry(node0regName); + + AtomicLongMetric sentMetric = mreg0.findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); + assertNotNull(sentMetric); + assertEquals(mbean0.getSentMessagesCount(), sentMetric.value()); + + AtomicLongMetric rcvMetric = mreg1.findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); + assertNotNull(rcvMetric); + assertEquals(mbean1.getReceivedMessagesCount(), rcvMetric.value()); + + stopGrid(1); + + mreg0 = grid(0).context().metric().registry(node1regName); + + sentMetric = mreg0.findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); + assertNull(sentMetric); } } finally { From 967697d23c4d1b80bcdf7d914935a9cb65af876e Mon Sep 17 00:00:00 2001 From: ibessonov Date: Wed, 25 Sep 2019 17:44:04 +0300 Subject: [PATCH 04/18] IGNITE-12108 Last review remarks. --- .../ignite/spi/communication/tcp/TcpCommunicationSpi.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java index d450e1db778a3..e0814644551e4 100755 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java @@ -403,7 +403,7 @@ public class TcpCommunicationSpi extends IgniteSpiAdapter implements Communicati public static final short HANDSHAKE_WAIT_MSG_TYPE = -28; /** Communication metrics group name. */ - public static final String COMMUNICATION_METRICS_GROUP_NAME = "communication.tcp"; + public static final String COMMUNICATION_METRICS_GROUP_NAME = MetricUtils.metricName("communication", "tcp"); /** */ public static final String SENT_MESSAGES_METRIC_NAME = "sentMessagesCount"; From 3a467122ffc6e46985850b4c1c17bd763be0d585 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Wed, 25 Sep 2019 17:59:12 +0300 Subject: [PATCH 05/18] IGNITE-12108 checkstyle --- .../spi/communication/tcp/TcpCommunicationMetricsListener.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index 6bc9422bebee2..2009d9ca02f04 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -130,11 +130,9 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC ); - sentMsgsCntByConsistentIdMetricFactory = consistentId -> getOrCreateMetricRegistry(mmgr, consistentId) .findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); - rcvdMsgsCntByConsistentIdMetricFactory = consistentId -> getOrCreateMetricRegistry(mmgr, consistentId) .findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); From 688611400ccc853712ed9cb4968e9d0c2798810e Mon Sep 17 00:00:00 2001 From: ibessonov Date: Wed, 30 Oct 2019 12:11:27 +0300 Subject: [PATCH 06/18] IGNITE-12108 Review remarks addresses. --- .../internal/util/nio/GridNioServer.java | 15 +++++++---- .../junits/IgniteTestResources.java | 26 +++++++++---------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java index c1ffed914573d..20487cbc0c966 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java @@ -242,6 +242,9 @@ public class GridNioServer { /** Sent bytes count metric. */ @Nullable private final AtomicLongMetric sentBytesCntMetric; + /** Outbound messages queue size. */ + @Nullable private final AtomicLongMetric outboundMessagesQueueSizeMetric; + /** Sessions. */ private final GridConcurrentHashSet sessions = new GridConcurrentHashSet<>(); @@ -438,6 +441,11 @@ private GridNioServer( sentBytesCntMetric = mreg == null ? null : mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + + outboundMessagesQueueSizeMetric = mreg == null ? null : mreg.longMetric( + OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME, + OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC + ); } /** @@ -2900,13 +2908,10 @@ final void reset0() { */ @Deprecated public int outboundMessagesQueueSize() { - if (mreg == null) + if (outboundMessagesQueueSizeMetric == null) return -1; - return (int) mreg.longMetric( - OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME, - OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC - ).value(); + return (int) outboundMessagesQueueSizeMetric.value(); } /** diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java index 41be037796a3f..a154fe5443608 100644 --- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java +++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java @@ -97,11 +97,11 @@ public IgniteTestResources() throws IgniteCheckedException { else log = rootLog.getLogger(getClass()); - jmx = prepareMBeanServer(); + this.jmx = prepareMBeanServer(); - ctx = new GridTestKernalContext(log); + this.ctx = new GridTestKernalContext(log); - rsrcProc = new GridResourceProcessor(ctx); + this.rsrcProc = new GridResourceProcessor(ctx); } /** @@ -109,10 +109,10 @@ public IgniteTestResources() throws IgniteCheckedException { */ public IgniteTestResources(IgniteConfiguration cfg) throws IgniteCheckedException { this.cfg = cfg; - log = rootLog.getLogger(getClass()); - jmx = prepareMBeanServer(); - ctx = new GridTestKernalContext(log, this.cfg); - rsrcProc = new GridResourceProcessor(ctx); + this.log = rootLog.getLogger(getClass()); + this.jmx = prepareMBeanServer(); + this.ctx = new GridTestKernalContext(log, this.cfg); + this.rsrcProc = new GridResourceProcessor(ctx); } /** @@ -122,9 +122,9 @@ public IgniteTestResources(MBeanServer jmx) throws IgniteCheckedException { assert jmx != null; this.jmx = jmx; - log = rootLog.getLogger(getClass()); - ctx = new GridTestKernalContext(log); - rsrcProc = new GridResourceProcessor(ctx); + this.log = rootLog.getLogger(getClass()); + this.ctx = new GridTestKernalContext(log); + this.rsrcProc = new GridResourceProcessor(ctx); } /** @@ -134,9 +134,9 @@ public IgniteTestResources(IgniteLogger log) throws IgniteCheckedException { assert log != null; this.log = log.getLogger(getClass()); - jmx = prepareMBeanServer(); - ctx = new GridTestKernalContext(log); - rsrcProc = new GridResourceProcessor(ctx); + this.jmx = prepareMBeanServer(); + this.ctx = new GridTestKernalContext(log); + this.rsrcProc = new GridResourceProcessor(ctx); } /** From 946557d06d40392aebd4b9217becc38539e3b3d3 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Wed, 30 Oct 2019 12:14:11 +0300 Subject: [PATCH 07/18] IGNITE-12108 one more change. --- .../java/org/apache/ignite/internal/util/nio/GridNioServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java index 20487cbc0c966..012b1efea9172 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java @@ -432,7 +432,7 @@ private GridNioServer( } } - balancer = balancer0; + this.balancer = balancer0; this.mreg = mreg; From 76732cf0aef0604f0573c6c34188ac1333d93d30 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Thu, 14 Nov 2019 15:50:24 +0300 Subject: [PATCH 08/18] IGNITE-12108 Extra fields removed from GridAbstractCommunicationClient. --- .../nio/GridAbstractCommunicationClient.java | 23 +------------------ .../nio/GridShmemCommunicationClient.java | 13 ++++++++++- .../nio/GridTcpNioCommunicationClient.java | 2 +- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java index 670ef152e6883..79de224b7772a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridAbstractCommunicationClient.java @@ -18,16 +18,8 @@ package org.apache.ignite.internal.util.nio; import java.util.concurrent.atomic.AtomicBoolean; -import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; import org.apache.ignite.internal.util.typedef.internal.S; import org.apache.ignite.internal.util.typedef.internal.U; -import org.jetbrains.annotations.Nullable; - -import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_DESC; -import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_NAME; -import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_DESC; -import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_NAME; /** * Implements basic lifecycle for communication clients. @@ -42,24 +34,11 @@ public abstract class GridAbstractCommunicationClient implements GridCommunicati /** */ private final int connIdx; - /** Received bytes count metric. */ - @Nullable protected final AtomicLongMetric rcvdBytesCntMetric; - - /** Sent bytes count metric. */ - @Nullable protected final AtomicLongMetric sentBytesCntMetric; - /** * @param connIdx Connection index. - * @param mreg Metrics registry. */ - protected GridAbstractCommunicationClient(int connIdx, @Nullable MetricRegistry mreg) { + protected GridAbstractCommunicationClient(int connIdx) { this.connIdx = connIdx; - - rcvdBytesCntMetric = mreg == null ? - null : mreg.longMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); - - sentBytesCntMetric = mreg == null ? - null : mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java index 7e0578efdfcc8..59f7b3872a960 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java @@ -27,6 +27,7 @@ import org.apache.ignite.IgniteException; import org.apache.ignite.IgniteLogger; import org.apache.ignite.internal.processors.metric.MetricRegistry; +import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; import org.apache.ignite.internal.util.ipc.shmem.IpcSharedMemoryClientEndpoint; import org.apache.ignite.internal.util.lang.IgniteInClosure2X; import org.apache.ignite.internal.util.typedef.internal.S; @@ -34,6 +35,10 @@ import org.apache.ignite.lang.IgniteInClosure; import org.apache.ignite.plugin.extensions.communication.Message; import org.apache.ignite.plugin.extensions.communication.MessageFormatter; +import org.jetbrains.annotations.Nullable; + +import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_DESC; +import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_NAME; /** * @@ -48,6 +53,9 @@ public class GridShmemCommunicationClient extends GridAbstractCommunicationClien /** */ private final MessageFormatter formatter; + /** Sent bytes count metric. */ + @Nullable protected final AtomicLongMetric sentBytesCntMetric; + /** * @param connIdx Connection index. * @param mreg Metrics registry. @@ -65,7 +73,7 @@ public GridShmemCommunicationClient( IgniteLogger log, MessageFormatter formatter ) throws IgniteCheckedException { - super(connIdx, mreg); + super(connIdx); assert mreg != null; assert port > 0 && port < 0xffff; @@ -78,6 +86,9 @@ public GridShmemCommunicationClient( writeBuf.order(ByteOrder.nativeOrder()); this.formatter = formatter; + + sentBytesCntMetric = mreg == null ? + null : mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridTcpNioCommunicationClient.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridTcpNioCommunicationClient.java index 98f694162593a..768e62461a75a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridTcpNioCommunicationClient.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridTcpNioCommunicationClient.java @@ -52,7 +52,7 @@ public GridTcpNioCommunicationClient( GridNioSession ses, IgniteLogger log ) { - super(connIdx, null); + super(connIdx); assert ses != null; assert log != null; From 80c643dfcc81466254224abe535e90d51166c2e8 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Thu, 5 Dec 2019 12:59:02 +0300 Subject: [PATCH 09/18] IGNITE-12108 More review remarks fixed. --- .../internal/util/ipc/IpcToNioAdapter.java | 10 +-- .../internal/util/nio/GridNioServer.java | 16 ++-- .../util/nio/GridSelectorNioSessionImpl.java | 6 +- .../nio/GridShmemCommunicationClient.java | 3 +- .../spi/communication/CommunicationSpi.java | 12 --- .../tcp/TcpCommunicationMetricsListener.java | 83 ++++++++++--------- .../tcp/TcpCommunicationSpi.java | 10 --- .../tcp/TcpCommunicationStatisticsTest.java | 8 +- 8 files changed, 66 insertions(+), 82 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java index fe46de6dd2523..2abfea74bfd8b 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/IpcToNioAdapter.java @@ -27,7 +27,7 @@ import org.apache.ignite.IgniteException; import org.apache.ignite.IgniteLogger; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.util.nio.GridNioFilter; import org.apache.ignite.internal.util.nio.GridNioFilterAdapter; import org.apache.ignite.internal.util.nio.GridNioFilterChain; @@ -70,10 +70,10 @@ public class IpcToNioAdapter { private final ByteBuffer writeBuf; /** Received bytes count metric. */ - private final AtomicLongMetric rcvdBytesCntMetric; + private final LongAdderMetric rcvdBytesCntMetric; /** Sent bytes count metric. */ - private final AtomicLongMetric sentBytesCntMetric; + private final LongAdderMetric sentBytesCntMetric; /** */ private final GridNioMessageWriterFactory writerFactory; @@ -91,9 +91,9 @@ public IpcToNioAdapter(MetricRegistry mreg, IgniteLogger log, IpcEndpoint endp, ) { assert mreg != null; - rcvdBytesCntMetric = mreg.longMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + rcvdBytesCntMetric = mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); - sentBytesCntMetric = mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + sentBytesCntMetric = mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); this.endp = endp; this.writerFactory = writerFactory; diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java index 258ba690e0401..5578d3b92ffa1 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioServer.java @@ -56,7 +56,7 @@ import org.apache.ignite.internal.IgniteInterruptedCheckedException; import org.apache.ignite.internal.managers.communication.GridIoMessage; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.util.GridConcurrentHashSet; import org.apache.ignite.internal.util.GridUnsafe; import org.apache.ignite.internal.util.future.GridCompoundFuture; @@ -237,13 +237,13 @@ public class GridNioServer { @Nullable private final MetricRegistry mreg; /** Received bytes count metric. */ - @Nullable private final AtomicLongMetric rcvdBytesCntMetric; + @Nullable private final LongAdderMetric rcvdBytesCntMetric; /** Sent bytes count metric. */ - @Nullable private final AtomicLongMetric sentBytesCntMetric; + @Nullable private final LongAdderMetric sentBytesCntMetric; /** Outbound messages queue size. */ - @Nullable private final AtomicLongMetric outboundMessagesQueueSizeMetric; + @Nullable private final LongAdderMetric outboundMessagesQueueSizeMetric; /** Sessions. */ @@ -437,12 +437,12 @@ private GridNioServer( this.mreg = mreg; rcvdBytesCntMetric = mreg == null ? - null : mreg.longMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + null : mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); sentBytesCntMetric = mreg == null ? - null : mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + null : mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); - outboundMessagesQueueSizeMetric = mreg == null ? null : mreg.longMetric( + outboundMessagesQueueSizeMetric = mreg == null ? null : mreg.longAdderMetric( OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME, OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC ); @@ -2904,9 +2904,7 @@ final void reset0() { * Gets outbound messages queue size. * * @return Write queue size. - * @deprecated Will be removed in the next major release and replaced with new metrics API. */ - @Deprecated public int outboundMessagesQueueSize() { if (outboundMessagesQueueSizeMetric == null) return -1; diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java index f83b35a6c6e87..87ecb301a5780 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridSelectorNioSessionImpl.java @@ -29,7 +29,7 @@ import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteLogger; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.util.tostring.GridToStringExclude; import org.apache.ignite.internal.util.typedef.internal.LT; import org.apache.ignite.internal.util.typedef.internal.S; @@ -87,7 +87,7 @@ public class GridSelectorNioSessionImpl extends GridNioSessionImpl implements Gr private volatile boolean closeSocket = true; /** Outbound messages queue size metric. */ - @Nullable private final AtomicLongMetric outboundMessagesQueueSizeMetric; + @Nullable private final LongAdderMetric outboundMessagesQueueSizeMetric; /** * Creates session instance. @@ -142,7 +142,7 @@ public class GridSelectorNioSessionImpl extends GridNioSessionImpl implements Gr this.readBuf = readBuf; } - outboundMessagesQueueSizeMetric = mreg == null ? null : mreg.longMetric( + outboundMessagesQueueSizeMetric = mreg == null ? null : mreg.longAdderMetric( OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_NAME, OUTBOUND_MESSAGES_QUEUE_SIZE_METRIC_DESC ); diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java index 59f7b3872a960..db8638f2a4656 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java @@ -87,8 +87,7 @@ public GridShmemCommunicationClient( this.formatter = formatter; - sentBytesCntMetric = mreg == null ? - null : mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + sentBytesCntMetric = mreg.findMetric(SENT_BYTES_METRIC_NAME); } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java index 38cd1bcb437de..92e9daf29a6a5 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java @@ -67,53 +67,41 @@ public interface CommunicationSpi extends IgniteSpi { * Gets sent messages count. * * @return Sent messages count. - * @deprecated Will be removed in the next major release and replaced with new metrics API. */ - @Deprecated public int getSentMessagesCount(); /** * Gets sent bytes count. * * @return Sent bytes count. - * @deprecated Will be removed in the next major release and replaced with new metrics API. */ - @Deprecated public long getSentBytesCount(); /** * Gets received messages count. * * @return Received messages count. - * @deprecated Will be removed in the next major release and replaced with new metrics API. */ - @Deprecated public int getReceivedMessagesCount(); /** * Gets received bytes count. * * @return Received bytes count. - * @deprecated Will be removed in the next major release and replaced with new metrics API. */ - @Deprecated public long getReceivedBytesCount(); /** * Gets outbound messages queue size. * * @return Outbound messages queue size. - * @deprecated Will be removed in the next major release and replaced with new metrics API. */ - @Deprecated public int getOutboundMessagesQueueSize(); /** * Resets metrics for this SPI instance. * - * @deprecated Will be removed in the next major release and replaced with new metrics API. */ - @Deprecated public void resetMetrics(); /** diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index 2009d9ca02f04..6cc7217cab3cb 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -26,7 +26,7 @@ import org.apache.ignite.internal.managers.communication.GridIoMessage; import org.apache.ignite.internal.processors.metric.GridMetricManager; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.processors.metric.impl.MetricUtils; import org.apache.ignite.plugin.extensions.communication.Message; @@ -45,8 +45,6 @@ import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_BY_TYPE_METRIC_DESC; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_METRIC_DESC; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_METRIC_NAME; -import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.receivedMessagesByTypeMetricName; -import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.sentMessagesByTypeMetricName; /** * Statistics for {@link org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi}. @@ -58,55 +56,57 @@ class TcpCommunicationMetricsListener { /** Metrics registry. */ private final MetricRegistry mreg; - /** */ - private final Function sentMsgsCntByTypeMetricFactory; + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #sentMsgsMetricsByType}. */ + private final Function sentMsgsCntByTypeMetricFactory; - /** */ - private final Function rcvdMsgsCntByTypeMetricFactory; + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #rcvdMsgsMetricsByType}. */ + private final Function rcvdMsgsCntByTypeMetricFactory; - /** */ - private final Function sentMsgsCntByConsistentIdMetricFactory; + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #sentMsgsMetricsByConsistentId}. */ + private final Function sentMsgsCntByConsistentIdMetricFactory; - /** */ - private final Function rcvdMsgsCntByConsistentIdMetricFactory; + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #rcvdMsgsMetricsByConsistentId}. */ + private final Function rcvdMsgsCntByConsistentIdMetricFactory; /** Sent bytes count metric.*/ - private final AtomicLongMetric sentBytesMetric; + private final LongAdderMetric sentBytesMetric; /** Received bytes count metric. */ - private final AtomicLongMetric rcvdBytesMetric; + private final LongAdderMetric rcvdBytesMetric; /** Sent messages count metric. */ - private final AtomicLongMetric sentMsgsMetric; + private final LongAdderMetric sentMsgsMetric; /** Received messages count metric. */ - private final AtomicLongMetric rcvdMsgsMetric; + private final LongAdderMetric rcvdMsgsMetric; /** Sent messages count metrics grouped by message type. */ - ConcurrentHashMap sentMsgsMetricsByType = new ConcurrentHashMap<>(); + private ConcurrentHashMap sentMsgsMetricsByType = new ConcurrentHashMap<>(); /** Received messages count metrics grouped by message type. */ - ConcurrentHashMap rcvdMsgsMetricsByType = new ConcurrentHashMap<>(); + private ConcurrentHashMap rcvdMsgsMetricsByType = new ConcurrentHashMap<>(); /** * Sent messages count metrics grouped by message node id. - * @deprecated for JMX support only. + * + * @deprecated For JMX support only. Replaced with {@link #sentMsgsMetricsByConsistentId}. */ @Deprecated - ConcurrentHashMap sentMsgsMetricsByNodeId = new ConcurrentHashMap<>(); + private ConcurrentHashMap sentMsgsMetricsByNodeId = new ConcurrentHashMap<>(); /** * Received messages metrics count grouped by message node id. - * @deprecated for JMX support only. + * + * @deprecated For JMX support only. Replaced with {@link #rcvdMsgsMetricsByConsistentId}. */ @Deprecated - ConcurrentHashMap rcvdMsgsMetricsByNodeId = new ConcurrentHashMap<>(); + private ConcurrentHashMap rcvdMsgsMetricsByNodeId = new ConcurrentHashMap<>(); /** Sent messages count metrics grouped by message node consistent id. */ - ConcurrentHashMap sentMsgsMetricsByConsistentId = new ConcurrentHashMap<>(); + private ConcurrentHashMap sentMsgsMetricsByConsistentId = new ConcurrentHashMap<>(); /** Received messages metrics count grouped by message node consistent id. */ - ConcurrentHashMap rcvdMsgsMetricsByConsistentId = new ConcurrentHashMap<>(); + private ConcurrentHashMap rcvdMsgsMetricsByConsistentId = new ConcurrentHashMap<>(); /** Method to synchronize access to message type map. */ private final Object msgTypMapMux = new Object(); @@ -114,6 +114,15 @@ class TcpCommunicationMetricsListener { /** Message type map. */ private volatile Map msgTypMap; + /** Generate metric name by message direct type id. */ + public static String sentMessagesByTypeMetricName(Short directType) { + return MetricUtils.metricName("sentMessagesByType", directType.toString()); + } + + /** Generate metric name by message direct type id. */ + public static String receivedMessagesByTypeMetricName(Short directType) { + return MetricUtils.metricName("receivedMessagesByType", directType.toString()); + } /** */ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { @@ -121,11 +130,11 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { mreg = mmgr.registry(COMMUNICATION_METRICS_GROUP_NAME); - sentMsgsCntByTypeMetricFactory = directType -> mreg.longMetric( + sentMsgsCntByTypeMetricFactory = directType -> mreg.longAdderMetric( sentMessagesByTypeMetricName(directType), SENT_MESSAGES_BY_TYPE_METRIC_DESC ); - rcvdMsgsCntByTypeMetricFactory = directType -> mreg.longMetric( + rcvdMsgsCntByTypeMetricFactory = directType -> mreg.longAdderMetric( receivedMessagesByTypeMetricName(directType), RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC ); @@ -136,11 +145,11 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { rcvdMsgsCntByConsistentIdMetricFactory = consistentId -> getOrCreateMetricRegistry(mmgr, consistentId) .findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); - sentBytesMetric = mreg.longMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); - rcvdBytesMetric = mreg.longMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); + sentBytesMetric = mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); + rcvdBytesMetric = mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); - sentMsgsMetric = mreg.longMetric(SENT_MESSAGES_METRIC_NAME, SENT_MESSAGES_METRIC_DESC); - rcvdMsgsMetric = mreg.longMetric(RECEIVED_MESSAGES_METRIC_NAME, RECEIVED_MESSAGES_METRIC_DESC); + sentMsgsMetric = mreg.longAdderMetric(SENT_MESSAGES_METRIC_NAME, SENT_MESSAGES_METRIC_DESC); + rcvdMsgsMetric = mreg.longAdderMetric(RECEIVED_MESSAGES_METRIC_NAME, RECEIVED_MESSAGES_METRIC_DESC); } /** */ @@ -154,8 +163,8 @@ private static synchronized MetricRegistry getOrCreateMetricRegistry(GridMetricM MetricRegistry mreg = mmgr.registry(regName); - mreg.longMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME, SENT_MESSAGES_BY_NODE_ID_METRIC_DESC); - mreg.longMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME, RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC); + mreg.longAdderMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME, SENT_MESSAGES_BY_NODE_ID_METRIC_DESC); + mreg.longAdderMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME, RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC); return mreg; } @@ -263,13 +272,13 @@ public long receivedBytesCount() { * @param input Input map. * @return Result map. */ - private Map convertMessageTypes(Map input) { + private Map convertMessageTypes(Map input) { Map res = new HashMap<>(input.size()); Map msgTypMap0 = msgTypMap; if (msgTypMap0 != null) { - for (Map.Entry inputEntry : input.entrySet()) { + for (Map.Entry inputEntry : input.entrySet()) { String typeName = msgTypMap0.get(inputEntry.getKey()); if (typeName != null) @@ -336,16 +345,16 @@ public void resetMetrics() { sentBytesMetric.reset(); rcvdBytesMetric.reset(); - for (AtomicLongMetric metric : sentMsgsMetricsByType.values()) + for (LongAdderMetric metric : sentMsgsMetricsByType.values()) metric.reset(); - for (AtomicLongMetric metric : rcvdMsgsMetricsByType.values()) + for (LongAdderMetric metric : rcvdMsgsMetricsByType.values()) metric.reset(); - for (AtomicLongMetric metric : sentMsgsMetricsByConsistentId.values()) + for (LongAdderMetric metric : sentMsgsMetricsByConsistentId.values()) metric.reset(); - for (AtomicLongMetric metric : rcvdMsgsMetricsByConsistentId.values()) + for (LongAdderMetric metric : rcvdMsgsMetricsByConsistentId.values()) metric.reset(); sentMsgsMetricsByNodeId.clear(); diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java index 0c9265efedd61..b1a8463bc59d3 100755 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java @@ -435,16 +435,6 @@ public class TcpCommunicationSpi extends IgniteSpiAdapter implements Communicati /** */ public static final String RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC = "Total number of messages received by current node from the given node"; - /** */ - public static String sentMessagesByTypeMetricName(Short directType) { - return MetricUtils.metricName("sentMessagesByType", directType.toString()); - } - - /** */ - public static String receivedMessagesByTypeMetricName(Short directType) { - return MetricUtils.metricName("receivedMessagesByType", directType.toString()); - } - /** */ private ConnectGateway connectGate; diff --git a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java index 6cb59457731f1..f73e00fabf79e 100644 --- a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java +++ b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java @@ -35,7 +35,7 @@ import org.apache.ignite.internal.managers.communication.GridIoMessageFactory; import org.apache.ignite.internal.managers.communication.GridIoPolicy; import org.apache.ignite.internal.processors.metric.MetricRegistry; -import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric; +import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.processors.metric.impl.MetricUtils; import org.apache.ignite.internal.util.typedef.CO; import org.apache.ignite.internal.util.typedef.internal.U; @@ -60,7 +60,7 @@ public class TcpCommunicationStatisticsTest extends GridCommonAbstractTest { private final Object mux = new Object(); /** */ - private CountDownLatch latch = new CountDownLatch(1); + private final CountDownLatch latch = new CountDownLatch(1); static { GridIoMessageFactory.registerCustom(GridTestMessage.DIRECT_TYPE, new CO() { @@ -205,11 +205,11 @@ public void testStatistics() throws Exception { MetricRegistry mreg0 = grid(0).context().metric().registry(node1regName); MetricRegistry mreg1 = grid(1).context().metric().registry(node0regName); - AtomicLongMetric sentMetric = mreg0.findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); + LongAdderMetric sentMetric = mreg0.findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); assertNotNull(sentMetric); assertEquals(mbean0.getSentMessagesCount(), sentMetric.value()); - AtomicLongMetric rcvMetric = mreg1.findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); + LongAdderMetric rcvMetric = mreg1.findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); assertNotNull(rcvMetric); assertEquals(mbean1.getReceivedMessagesCount(), rcvMetric.value()); From bc35d376656dc0860c46c22dc059a03ca57d71d8 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Fri, 20 Dec 2019 15:20:40 +0300 Subject: [PATCH 10/18] IGNITE-12108 Minor code cleanup. --- .../ignite/internal/util/nio/GridShmemCommunicationClient.java | 1 - .../org/apache/ignite/spi/communication/CommunicationSpi.java | 1 - 2 files changed, 2 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java index db8638f2a4656..fce6ea2628d7d 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridShmemCommunicationClient.java @@ -37,7 +37,6 @@ import org.apache.ignite.plugin.extensions.communication.MessageFormatter; import org.jetbrains.annotations.Nullable; -import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_DESC; import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_NAME; /** diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java index 92e9daf29a6a5..ac544bf0e71e4 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/CommunicationSpi.java @@ -100,7 +100,6 @@ public interface CommunicationSpi extends IgniteSpi { /** * Resets metrics for this SPI instance. - * */ public void resetMetrics(); From ff8766905ec30797bde09eab33ed90eea4d670b0 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Fri, 20 Dec 2019 16:03:44 +0300 Subject: [PATCH 11/18] IGNITE-12108 REVERTED consistentId-based metrics. --- .../tcp/TcpCommunicationMetricsListener.java | 70 ++++++------------- .../tcp/TcpCommunicationSpi.java | 46 +++++------- .../tcp/internal/ConnectionKey.java | 22 ++---- ...TcpCommunicationConnectionCheckFuture.java | 15 ++-- ...CommunicationSpiMultithreadedSelfTest.java | 2 +- .../tcp/TcpCommunicationStatisticsTest.java | 8 +-- 6 files changed, 57 insertions(+), 106 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index 6cc7217cab3cb..29a9f6b0eb502 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -21,7 +21,6 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; import org.apache.ignite.internal.managers.communication.GridIoMessage; import org.apache.ignite.internal.processors.metric.GridMetricManager; @@ -62,11 +61,11 @@ class TcpCommunicationMetricsListener { /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #rcvdMsgsMetricsByType}. */ private final Function rcvdMsgsCntByTypeMetricFactory; - /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #sentMsgsMetricsByConsistentId}. */ - private final Function sentMsgsCntByConsistentIdMetricFactory; + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #sentMsgsMetricsByNodeId}. */ + private final Function sentMsgsCntByNodeIdMetricFactory; - /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #rcvdMsgsMetricsByConsistentId}. */ - private final Function rcvdMsgsCntByConsistentIdMetricFactory; + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #rcvdMsgsMetricsByNodeId}. */ + private final Function rcvdMsgsCntByNodeIdMetricFactory; /** Sent bytes count metric.*/ private final LongAdderMetric sentBytesMetric; @@ -88,25 +87,13 @@ class TcpCommunicationMetricsListener { /** * Sent messages count metrics grouped by message node id. - * - * @deprecated For JMX support only. Replaced with {@link #sentMsgsMetricsByConsistentId}. */ - @Deprecated - private ConcurrentHashMap sentMsgsMetricsByNodeId = new ConcurrentHashMap<>(); + private ConcurrentHashMap sentMsgsMetricsByNodeId = new ConcurrentHashMap<>(); /** * Received messages metrics count grouped by message node id. - * - * @deprecated For JMX support only. Replaced with {@link #rcvdMsgsMetricsByConsistentId}. */ - @Deprecated - private ConcurrentHashMap rcvdMsgsMetricsByNodeId = new ConcurrentHashMap<>(); - - /** Sent messages count metrics grouped by message node consistent id. */ - private ConcurrentHashMap sentMsgsMetricsByConsistentId = new ConcurrentHashMap<>(); - - /** Received messages metrics count grouped by message node consistent id. */ - private ConcurrentHashMap rcvdMsgsMetricsByConsistentId = new ConcurrentHashMap<>(); + private ConcurrentHashMap rcvdMsgsMetricsByNodeId = new ConcurrentHashMap<>(); /** Method to synchronize access to message type map. */ private final Object msgTypMapMux = new Object(); @@ -139,10 +126,10 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC ); - sentMsgsCntByConsistentIdMetricFactory = consistentId -> getOrCreateMetricRegistry(mmgr, consistentId) + sentMsgsCntByNodeIdMetricFactory = nodeId -> getOrCreateMetricRegistry(mmgr, nodeId) .findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); - rcvdMsgsCntByConsistentIdMetricFactory = consistentId -> getOrCreateMetricRegistry(mmgr, consistentId) + rcvdMsgsCntByNodeIdMetricFactory = nodeId -> getOrCreateMetricRegistry(mmgr, nodeId) .findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); sentBytesMetric = mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); @@ -153,8 +140,8 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { } /** */ - private static synchronized MetricRegistry getOrCreateMetricRegistry(GridMetricManager mmgr, Object consistentId) { - String regName = MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, consistentId.toString()); + private static synchronized MetricRegistry getOrCreateMetricRegistry(GridMetricManager mmgr, UUID nodeId) { + String regName = MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString()); for (MetricRegistry mreg : mmgr) { if (mreg.name().equals(regName)) @@ -178,10 +165,9 @@ public MetricRegistry metricRegistry() { * Collects statistics for message sent by SPI. * * @param msg Sent message. - * @param consistentId Receiver node consistent id. * @param nodeId Receiver node id. */ - public void onMessageSent(Message msg, Object consistentId, UUID nodeId) { + public void onMessageSent(Message msg, UUID nodeId) { assert msg != null; assert nodeId != null; @@ -194,9 +180,7 @@ public void onMessageSent(Message msg, Object consistentId, UUID nodeId) { sentMsgsMetricsByType.computeIfAbsent(msg.directType(), sentMsgsCntByTypeMetricFactory).increment(); - sentMsgsMetricsByConsistentId.computeIfAbsent(consistentId, sentMsgsCntByConsistentIdMetricFactory).increment(); - - sentMsgsMetricsByNodeId.computeIfAbsent(nodeId, id -> new AtomicLong()).incrementAndGet(); + sentMsgsMetricsByNodeId.computeIfAbsent(nodeId, sentMsgsCntByNodeIdMetricFactory).increment(); } } @@ -204,10 +188,9 @@ public void onMessageSent(Message msg, Object consistentId, UUID nodeId) { * Collects statistics for message received by SPI. * * @param msg Received message. - * @param consistentId Sender node consistent id. * @param nodeId Sender node id. */ - public void onMessageReceived(Message msg, Object consistentId, UUID nodeId) { + public void onMessageReceived(Message msg, UUID nodeId) { assert msg != null; assert nodeId != null; @@ -220,9 +203,7 @@ public void onMessageReceived(Message msg, Object consistentId, UUID nodeId) { rcvdMsgsMetricsByType.computeIfAbsent(msg.directType(), rcvdMsgsCntByTypeMetricFactory).increment(); - rcvdMsgsMetricsByConsistentId.computeIfAbsent(consistentId, rcvdMsgsCntByConsistentIdMetricFactory).increment(); - - rcvdMsgsMetricsByNodeId.computeIfAbsent(nodeId, id -> new AtomicLong()).incrementAndGet(); + rcvdMsgsMetricsByNodeId.computeIfAbsent(nodeId, rcvdMsgsCntByNodeIdMetricFactory).increment(); } } @@ -306,8 +287,8 @@ public Map receivedMessagesByType() { public Map receivedMessagesByNode() { Map res = new HashMap<>(); - for (Map.Entry entry : rcvdMsgsMetricsByNodeId.entrySet()) - res.put(entry.getKey(), entry.getValue().longValue()); + for (Map.Entry entry : rcvdMsgsMetricsByNodeId.entrySet()) + res.put(entry.getKey(), entry.getValue().value()); return res; } @@ -329,8 +310,8 @@ public Map sentMessagesByType() { public Map sentMessagesByNode() { Map res = new HashMap<>(); - for (Map.Entry entry : sentMsgsMetricsByNodeId.entrySet()) - res.put(entry.getKey(), entry.getValue().longValue()); + for (Map.Entry entry : sentMsgsMetricsByNodeId.entrySet()) + res.put(entry.getKey(), entry.getValue().value()); return res; } @@ -351,28 +332,21 @@ public void resetMetrics() { for (LongAdderMetric metric : rcvdMsgsMetricsByType.values()) metric.reset(); - for (LongAdderMetric metric : sentMsgsMetricsByConsistentId.values()) + for (LongAdderMetric metric : sentMsgsMetricsByNodeId.values()) metric.reset(); - for (LongAdderMetric metric : rcvdMsgsMetricsByConsistentId.values()) + for (LongAdderMetric metric : rcvdMsgsMetricsByNodeId.values()) metric.reset(); - - sentMsgsMetricsByNodeId.clear(); - rcvdMsgsMetricsByNodeId.clear(); } /** - * @param consistentId Consistent id of the node. * @param nodeId Left node id. */ - public void onNodeLeft(Object consistentId, UUID nodeId) { + public void onNodeLeft(UUID nodeId) { sentMsgsMetricsByNodeId.remove(nodeId); rcvdMsgsMetricsByNodeId.remove(nodeId); - sentMsgsMetricsByConsistentId.remove(consistentId); - rcvdMsgsMetricsByConsistentId.remove(consistentId); - - mmgr.remove(MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, consistentId.toString())); + mmgr.remove(MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())); } /** diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java index faae92798e3b4..acd82367a94cb 100755 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java @@ -556,13 +556,12 @@ public class TcpCommunicationSpi extends IgniteSpiAdapter implements Communicati */ private void onFirstMessage(final GridNioSession ses, Message msg) { UUID sndId; - int connIdx; - long connCnt; + + ConnectionKey connKey; if (msg instanceof NodeIdMessage) { sndId = U.bytesToUuid(((NodeIdMessage)msg).nodeIdBytes(), 0); - connIdx = 0; - connCnt = -1; + connKey = new ConnectionKey(sndId, 0, -1); } else { assert msg instanceof HandshakeMessage : msg; @@ -570,8 +569,7 @@ private void onFirstMessage(final GridNioSession ses, Message msg) { HandshakeMessage msg0 = (HandshakeMessage)msg; sndId = ((HandshakeMessage)msg).nodeId(); - connIdx = msg0.connectionIndex(); - connCnt = msg0.connectCount(); + connKey = new ConnectionKey(sndId, msg0.connectionIndex(), msg0.connectCount()); } if (log.isDebugEnabled()) @@ -619,8 +617,6 @@ else if (discoverySpi instanceof IgniteDiscoverySpi) return; } - ConnectionKey connKey = new ConnectionKey(rmtNode.consistentId(), sndId, connIdx, connCnt); - final ConnectionKey old = ses.addMeta(CONN_IDX_META, connKey); assert old == null; @@ -786,8 +782,11 @@ private void closeStaleConnections(ConnectionKey connKey) { @Override public void onMessageSent(GridNioSession ses, Message msg) { ConnectionKey connKey = ses.meta(CONN_IDX_META); - if (connKey != null) - metricsLsnr.onMessageSent(msg, connKey.consistentId(), connKey.nodeId()); + if (connKey != null) { + UUID nodeId = connKey.nodeId(); + + metricsLsnr.onMessageSent(msg, nodeId); + } } private void onChannelCreate( @@ -879,7 +878,7 @@ private void onChannelCreate( } if (msg instanceof RecoveryLastReceivedMessage) { - metricsLsnr.onMessageReceived(msg, connKey.consistentId(), connKey.nodeId()); + metricsLsnr.onMessageReceived(msg, connKey.nodeId()); GridNioRecoveryDescriptor recovery = ses.outRecoveryDescriptor(); @@ -930,7 +929,7 @@ else if (connKey.dummy()) { } } - metricsLsnr.onMessageReceived(msg, connKey.consistentId(), connKey.nodeId()); + metricsLsnr.onMessageReceived(msg, connKey.nodeId()); IgniteRunnable c; @@ -2762,13 +2761,12 @@ private GridNioServer resetNioServer() throws IgniteCheckedException { } /** - * @param consistentId Consistent id of the node. * @param nodeId Left node ID. */ - void onNodeLeft(Object consistentId, UUID nodeId) { + void onNodeLeft(UUID nodeId) { assert nodeId != null; - metricsLsnr.onNodeLeft(consistentId, nodeId); + metricsLsnr.onNodeLeft(nodeId); GridCommunicationClient[] clients0 = clients.remove(nodeId); @@ -3036,7 +3034,7 @@ private GridCommunicationClient reserveClient(ClusterNode node, int connIdx) thr // Do not allow concurrent connects. GridFutureAdapter fut = new ConnectFuture(); - ConnectionKey connKey = new ConnectionKey(node.consistentId(), nodeId, connIdx, -1); + ConnectionKey connKey = new ConnectionKey(nodeId, connIdx, -1); GridFutureAdapter oldFut = clientFuts.putIfAbsent(connKey, fut); @@ -3518,7 +3516,7 @@ private GridNioSession createNioSession(ClusterNode node, int connIdx) throws Ig if (sockSndBuf > 0) ch.socket().setSendBufferSize(sockSndBuf); - ConnectionKey connKey = new ConnectionKey(node.consistentId(), node.id(), connIdx, -1); + ConnectionKey connKey = new ConnectionKey(node.id(), connIdx, -1); GridNioRecoveryDescriptor recoveryDesc = outRecoveryDescriptor(node, connKey); @@ -4411,7 +4409,7 @@ public IgniteInternalFuture openChannel( assert nodeSupports(remote, CHANNEL_COMMUNICATION) : "Node doesn't support direct connection over socket channel " + "[nodeId=" + remote.id() + ']'; - ConnectionKey key = new ConnectionKey(remote.consistentId(), remote.id(), chConnPlc.connectionIndex()); + ConnectionKey key = new ConnectionKey(remote.id(), chConnPlc.connectionIndex()); GridFutureAdapter chFut = new GridFutureAdapter<>(); @@ -4488,9 +4486,7 @@ private class DiscoveryListener implements GridLocalEventListener, HighPriorityL assert evt instanceof DiscoveryEvent : evt; assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED; - ClusterNode node = ((DiscoveryEvent)evt).eventNode(); - - onNodeLeft(node.consistentId(), node.id()); + onNodeLeft(((DiscoveryEvent)evt).eventNode().id()); } /** {@inheritDoc} */ @@ -4696,9 +4692,7 @@ private void processIdle() { GridNioRecoveryDescriptor recovery = null; if (!usePairedConnections(node) && client instanceof GridTcpNioCommunicationClient) { - recovery = recoveryDescs.get(new ConnectionKey( - node.consistentId(), node.id(), client.connectionIndex(), -1) - ); + recovery = recoveryDescs.get(new ConnectionKey(node.id(), client.connectionIndex(), -1)); if (recovery != null && recovery.lastAcknowledged() != recovery.received()) { RecoveryLastReceivedMessage msg = new RecoveryLastReceivedMessage(recovery.received()); @@ -4724,9 +4718,7 @@ private void processIdle() { if (idleTime >= idleConnTimeout) { if (recovery == null && usePairedConnections(node)) - recovery = outRecDescs.get(new ConnectionKey( - node.consistentId(), node.id(), client.connectionIndex(), -1) - ); + recovery = outRecDescs.get(new ConnectionKey(node.id(), client.connectionIndex(), -1)); if (recovery != null && recovery.nodeAlive(getSpiContext().node(nodeId)) && diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/ConnectionKey.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/ConnectionKey.java index 8cece65e21797..a107c876b26e8 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/ConnectionKey.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/ConnectionKey.java @@ -25,9 +25,6 @@ * Connection Key. */ public class ConnectionKey { - /** */ - private final Object consistentId; - /** */ private final UUID nodeId; @@ -41,36 +38,32 @@ public class ConnectionKey { private final boolean dummy; /** - * @param consistentId Consistent id of the node. * @param nodeId Node ID. Should be not null. * @param idx Connection index. */ - public ConnectionKey(@NotNull Object consistentId, @NotNull UUID nodeId, int idx) { - this(consistentId, nodeId, idx, -1, false); + public ConnectionKey(@NotNull UUID nodeId, int idx) { + this(nodeId, idx, -1, false); } /** * Creates ConnectionKey with false value of dummy flag. * - * @param consistentId Consistent id of the node. * @param nodeId Node ID. Should be not null. * @param idx Connection index. * @param connCnt Connection counter (set only for incoming connections). */ - public ConnectionKey(@NotNull Object consistentId, @NotNull UUID nodeId, int idx, long connCnt) { - this(consistentId, nodeId, idx, connCnt, false); + public ConnectionKey(@NotNull UUID nodeId, int idx, long connCnt) { + this(nodeId, idx, connCnt, false); } /** - * @param consistentId Consistent id of the node. * @param nodeId Node ID. Should be not null. * @param idx Connection index. * @param connCnt Connection counter (set only for incoming connections). * @param dummy Indicates that session with this ConnectionKey is temporary * (for now dummy sessions are used only for Communication Failure Resolving process). */ - public ConnectionKey(@NotNull Object consistentId, @NotNull UUID nodeId, int idx, long connCnt, boolean dummy) { - this.consistentId = consistentId; + public ConnectionKey(@NotNull UUID nodeId, int idx, long connCnt, boolean dummy) { this.nodeId = nodeId; this.idx = idx; this.connCnt = connCnt; @@ -91,11 +84,6 @@ public UUID nodeId() { return nodeId; } - /** */ - public Object consistentId() { - return consistentId; - } - /** * @return Connection index. */ diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/TcpCommunicationConnectionCheckFuture.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/TcpCommunicationConnectionCheckFuture.java index 7530d0dcc0e29..46fdb0b155c48 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/TcpCommunicationConnectionCheckFuture.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/TcpCommunicationConnectionCheckFuture.java @@ -142,14 +142,14 @@ public void init(long timeout) { if (addrs.size() == 1) { SingleAddressConnectFuture fut = new SingleAddressConnectFuture(i); - fut.init(addrs.iterator().next(), node.consistentId(), node.id()); + fut.init(addrs.iterator().next(), node.id()); futs[i] = fut; } else { MultipleAddressesConnectFuture fut = new MultipleAddressesConnectFuture(i); - fut.init(addrs, node.consistentId(), node.id()); + fut.init(addrs, node.id()); futs[i] = fut; } @@ -292,10 +292,9 @@ private class SingleAddressConnectFuture implements TcpCommunicationNodeConnecti /** * @param addr Node address. - * @param consistentId * @param rmtNodeId Id of node to open connection check session with. */ - public void init(InetSocketAddress addr, Object consistentId, UUID rmtNodeId) { + public void init(InetSocketAddress addr, UUID rmtNodeId) { boolean connect; try { @@ -318,8 +317,7 @@ public void init(InetSocketAddress addr, Object consistentId, UUID rmtNodeId) { sesMeta = new GridLeanMap<>(3); // Set dummy key to identify connection-check outgoing connection. - ConnectionKey connKey = new ConnectionKey(consistentId, rmtNodeId, -1, -1, true); - sesMeta.put(TcpCommunicationSpi.CONN_IDX_META, connKey); + sesMeta.put(TcpCommunicationSpi.CONN_IDX_META, new ConnectionKey(rmtNodeId, -1, -1, true)); sesMeta.put(SES_FUT_META, this); nioSrvr.createSession(ch, sesMeta, true, new IgniteInClosure>() { @@ -424,10 +422,9 @@ private class MultipleAddressesConnectFuture implements ConnectFuture { /** * @param addrs Node addresses. - * @param consistentId Consistent if of the node. * @param rmtNodeId Id of node to open connection check session with. */ - void init(Collection addrs, Object consistentId, UUID rmtNodeId) { + void init(Collection addrs, UUID rmtNodeId) { SingleAddressConnectFuture[] futs = new SingleAddressConnectFuture[addrs.size()]; for (int i = 0; i < addrs.size(); i++) { @@ -445,7 +442,7 @@ void init(Collection addrs, Object consistentId, UUID rmtNode int idx = 0; for (InetSocketAddress addr : addrs) { - futs[idx++].init(addr, consistentId, rmtNodeId); + futs[idx++].init(addr, rmtNodeId); if (resCnt == Integer.MAX_VALUE) return; diff --git a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiMultithreadedSelfTest.java b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiMultithreadedSelfTest.java index a53b43b61efb7..cfde86b318f74 100644 --- a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiMultithreadedSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiMultithreadedSelfTest.java @@ -349,7 +349,7 @@ public void testFlowSend() throws Exception { while (run.get() && !Thread.currentThread().isInterrupted()) { U.sleep(interval * 3 / 2); - ((TcpCommunicationSpi)spis.get(from.id())).onNodeLeft(to.consistentId(), to.id()); + ((TcpCommunicationSpi)spis.get(from.id())).onNodeLeft(to.id()); } } catch (IgniteInterruptedCheckedException ignored) { diff --git a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java index f73e00fabf79e..90ee5729fadad 100644 --- a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java +++ b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java @@ -141,17 +141,17 @@ public void testStatistics() throws Exception { startGrids(2); try { - Object node0consistentId = grid(0).localNode().consistentId(); - Object node1consistentId = grid(1).localNode().consistentId(); + UUID node0Id = grid(0).localNode().id(); + UUID node1Id = grid(1).localNode().id(); String node0regName = MetricUtils.metricName( COMMUNICATION_METRICS_GROUP_NAME, - node0consistentId.toString() + node0Id.toString() ); String node1regName = MetricUtils.metricName( COMMUNICATION_METRICS_GROUP_NAME, - node1consistentId.toString() + node1Id.toString() ); // Send custom message from node0 to node1. From 7d24eb175fa87647b26090b22e2048fc7e5da516 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Fri, 20 Dec 2019 18:49:09 +0300 Subject: [PATCH 12/18] IGNITE-12108 assertion for tests. --- .../ignite/spi/communication/tcp/TcpCommunicationSpi.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java index acd82367a94cb..d750d86f13c93 100755 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java @@ -1386,6 +1386,8 @@ public AddressResolver getAddressResolver() { /** */ @MetricManagerResource private void injectMetricManager(GridMetricManager mmgr) { + assert mmgr != null : "Null metrics manager injected"; + if (mmgr != null) metricsLsnr = new TcpCommunicationMetricsListener(mmgr); } From d6fffb6a792735ae9fb3f5cca5c24d50d4401ea7 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Mon, 23 Dec 2019 14:36:17 +0300 Subject: [PATCH 13/18] IGNITE-12108 assertion removed. --- .../ignite/spi/communication/tcp/TcpCommunicationSpi.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java index d750d86f13c93..acd82367a94cb 100755 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java @@ -1386,8 +1386,6 @@ public AddressResolver getAddressResolver() { /** */ @MetricManagerResource private void injectMetricManager(GridMetricManager mmgr) { - assert mmgr != null : "Null metrics manager injected"; - if (mmgr != null) metricsLsnr = new TcpCommunicationMetricsListener(mmgr); } From 0aa0ea0cb4a08ca51eb12932a24972c4f82e0538 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Wed, 25 Dec 2019 18:53:06 +0300 Subject: [PATCH 14/18] IGNITE-12108 Avoided lookups in concurrent hash maps on every message. --- .../tcp/TcpCommunicationMetricsListener.java | 210 ++++++++++++------ .../tcp/TcpCommunicationSpi.java | 6 + 2 files changed, 144 insertions(+), 72 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index 29a9f6b0eb502..62e24d3e95bc6 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -17,8 +17,10 @@ package org.apache.ignite.spi.communication.tcp; +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; @@ -28,6 +30,8 @@ import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; import org.apache.ignite.internal.processors.metric.impl.MetricUtils; import org.apache.ignite.plugin.extensions.communication.Message; +import org.apache.ignite.spi.metric.LongMetric; +import org.apache.ignite.spi.metric.Metric; import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_DESC; import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_NAME; @@ -37,11 +41,13 @@ import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_METRIC_DESC; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.RECEIVED_MESSAGES_METRIC_NAME; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_BY_NODE_ID_METRIC_DESC; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_BY_NODE_ID_METRIC_NAME; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_BY_TYPE_METRIC_DESC; +import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_BY_TYPE_METRIC_NAME; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_METRIC_DESC; import static org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi.SENT_MESSAGES_METRIC_NAME; @@ -55,16 +61,28 @@ class TcpCommunicationMetricsListener { /** Metrics registry. */ private final MetricRegistry mreg; - /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #sentMsgsMetricsByType}. */ + /** All registered metrics. */ + private final Set allMetrics = Collections.newSetFromMap(new ConcurrentHashMap<>()); + + /** Thread-local metrics. */ + private final ThreadLocal threadMetrics = ThreadLocal.withInitial(() -> { + ThreadMetrics metrics = new ThreadMetrics(); + + allMetrics.add(metrics); + + return metrics; + }); + + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@code sentMsgsMetricsByType}. */ private final Function sentMsgsCntByTypeMetricFactory; - /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #rcvdMsgsMetricsByType}. */ + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@code rcvdMsgsMetricsByType}. */ private final Function rcvdMsgsCntByTypeMetricFactory; - /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #sentMsgsMetricsByNodeId}. */ + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@code sentMsgsMetricsByNodeId}. */ private final Function sentMsgsCntByNodeIdMetricFactory; - /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@link #rcvdMsgsMetricsByNodeId}. */ + /** Function to be used in {@link Map#computeIfAbsent(Object, Function)} of {@code rcvdMsgsMetricsByNodeId}. */ private final Function rcvdMsgsCntByNodeIdMetricFactory; /** Sent bytes count metric.*/ @@ -79,22 +97,6 @@ class TcpCommunicationMetricsListener { /** Received messages count metric. */ private final LongAdderMetric rcvdMsgsMetric; - /** Sent messages count metrics grouped by message type. */ - private ConcurrentHashMap sentMsgsMetricsByType = new ConcurrentHashMap<>(); - - /** Received messages count metrics grouped by message type. */ - private ConcurrentHashMap rcvdMsgsMetricsByType = new ConcurrentHashMap<>(); - - /** - * Sent messages count metrics grouped by message node id. - */ - private ConcurrentHashMap sentMsgsMetricsByNodeId = new ConcurrentHashMap<>(); - - /** - * Received messages metrics count grouped by message node id. - */ - private ConcurrentHashMap rcvdMsgsMetricsByNodeId = new ConcurrentHashMap<>(); - /** Method to synchronize access to message type map. */ private final Object msgTypMapMux = new Object(); @@ -103,12 +105,12 @@ class TcpCommunicationMetricsListener { /** Generate metric name by message direct type id. */ public static String sentMessagesByTypeMetricName(Short directType) { - return MetricUtils.metricName("sentMessagesByType", directType.toString()); + return MetricUtils.metricName(SENT_MESSAGES_BY_TYPE_METRIC_NAME, directType.toString()); } /** Generate metric name by message direct type id. */ public static String receivedMessagesByTypeMetricName(Short directType) { - return MetricUtils.metricName("receivedMessagesByType", directType.toString()); + return MetricUtils.metricName(RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME, directType.toString()); } /** */ @@ -178,9 +180,7 @@ public void onMessageSent(Message msg, UUID nodeId) { sentMsgsMetric.increment(); - sentMsgsMetricsByType.computeIfAbsent(msg.directType(), sentMsgsCntByTypeMetricFactory).increment(); - - sentMsgsMetricsByNodeId.computeIfAbsent(nodeId, sentMsgsCntByNodeIdMetricFactory).increment(); + threadMetrics.get().onMessageSent(msg, nodeId); } } @@ -201,9 +201,7 @@ public void onMessageReceived(Message msg, UUID nodeId) { rcvdMsgsMetric.increment(); - rcvdMsgsMetricsByType.computeIfAbsent(msg.directType(), rcvdMsgsCntByTypeMetricFactory).increment(); - - rcvdMsgsMetricsByNodeId.computeIfAbsent(nodeId, rcvdMsgsCntByNodeIdMetricFactory).increment(); + threadMetrics.get().onMessageReceived(msg, nodeId); } } @@ -247,36 +245,13 @@ public long receivedBytesCount() { return rcvdBytesMetric.value(); } - /** - * Convert message types. - * - * @param input Input map. - * @return Result map. - */ - private Map convertMessageTypes(Map input) { - Map res = new HashMap<>(input.size()); - - Map msgTypMap0 = msgTypMap; - - if (msgTypMap0 != null) { - for (Map.Entry inputEntry : input.entrySet()) { - String typeName = msgTypMap0.get(inputEntry.getKey()); - - if (typeName != null) - res.put(typeName, inputEntry.getValue().value()); - } - } - - return res; - } - /** * Gets received messages counts (grouped by type). * * @return Map containing message types and respective counts. */ public Map receivedMessagesByType() { - return convertMessageTypes(rcvdMsgsMetricsByType); + return collectMessagesCountByType(RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME + MetricUtils.SEPARATOR); } /** @@ -285,12 +260,7 @@ public Map receivedMessagesByType() { * @return Map containing sender nodes and respective counts. */ public Map receivedMessagesByNode() { - Map res = new HashMap<>(); - - for (Map.Entry entry : rcvdMsgsMetricsByNodeId.entrySet()) - res.put(entry.getKey(), entry.getValue().value()); - - return res; + return collectMessagesCountByNodeId(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); } /** @@ -299,7 +269,7 @@ public Map receivedMessagesByNode() { * @return Map containing message types and respective counts. */ public Map sentMessagesByType() { - return convertMessageTypes(sentMsgsMetricsByType); + return collectMessagesCountByType(SENT_MESSAGES_BY_TYPE_METRIC_NAME + MetricUtils.SEPARATOR); } /** @@ -308,10 +278,55 @@ public Map sentMessagesByType() { * @return Map containing receiver nodes and respective counts. */ public Map sentMessagesByNode() { + return collectMessagesCountByNodeId(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); + } + + /** */ + protected Map collectMessagesCountByType(String prefix) { + Map res = new HashMap<>(); + + prefix = MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, prefix); + + for (Metric metric : mreg) { + if (metric.name().startsWith(prefix)) { + try { + short directType = Short.parseShort(metric.name().substring(prefix.length())); + + Map msgTypMap0 = msgTypMap; + + if (msgTypMap0 != null) { + String typeName = msgTypMap0.get(directType); + + if (typeName != null) + res.put(typeName, ((LongMetric)metric).value()); + } + } + catch (NumberFormatException ignore) { + } + } + } + + return res; + } + + /** */ + protected Map collectMessagesCountByNodeId(String metricName) { Map res = new HashMap<>(); - for (Map.Entry entry : sentMsgsMetricsByNodeId.entrySet()) - res.put(entry.getKey(), entry.getValue().value()); + String mregPrefix = COMMUNICATION_METRICS_GROUP_NAME + MetricUtils.SEPARATOR; + for (MetricRegistry mreg : mmgr) { + if (mreg.name().startsWith(mregPrefix)) { + String nodeIdStr = mreg.name().substring(mregPrefix.length()); + + try { + UUID nodeId = UUID.fromString(nodeIdStr); + + res.put(nodeId, mreg.findMetric(metricName).value()); + } + catch (IllegalArgumentException ignore) { + } + } + } return res; } @@ -326,25 +341,30 @@ public void resetMetrics() { sentBytesMetric.reset(); rcvdBytesMetric.reset(); - for (LongAdderMetric metric : sentMsgsMetricsByType.values()) - metric.reset(); - - for (LongAdderMetric metric : rcvdMsgsMetricsByType.values()) - metric.reset(); + for (Metric metric : mreg) { + if (metric.name().startsWith(SENT_MESSAGES_BY_TYPE_METRIC_NAME)) + metric.reset(); + else if (metric.name().startsWith(RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME)) + metric.reset(); + } - for (LongAdderMetric metric : sentMsgsMetricsByNodeId.values()) - metric.reset(); + for (MetricRegistry mreg : mmgr) { + if (mreg.name().startsWith(COMMUNICATION_METRICS_GROUP_NAME + MetricUtils.SEPARATOR)) { + mreg.findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME).reset(); - for (LongAdderMetric metric : rcvdMsgsMetricsByNodeId.values()) - metric.reset(); + mreg.findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME).reset(); + } + } } /** * @param nodeId Left node id. */ public void onNodeLeft(UUID nodeId) { - sentMsgsMetricsByNodeId.remove(nodeId); - rcvdMsgsMetricsByNodeId.remove(nodeId); + for (ThreadMetrics threadMetrics : allMetrics) { + threadMetrics.rcvdMsgsMetricsByNodeId = new HashMap<>(); + threadMetrics.sentMsgsMetricsByNodeId = new HashMap<>(); + } mmgr.remove(MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())); } @@ -380,4 +400,50 @@ private void updateMessageTypeMap(Message msg) { } } } + + /** + * Thread-local metrics. + */ + private class ThreadMetrics { + /** Sent messages count metrics grouped by message type. */ + private final HashMap sentMsgsMetricsByType = new HashMap<>(); + + /** Received messages count metrics grouped by message type. */ + private final HashMap rcvdMsgsMetricsByType = new HashMap<>(); + + /** + * Sent messages count metrics grouped by message node id. + */ + public volatile HashMap sentMsgsMetricsByNodeId = new HashMap<>(); + + /** + * Received messages metrics count grouped by message node id. + */ + public volatile HashMap rcvdMsgsMetricsByNodeId = new HashMap<>(); + + + /** + * Collects statistics for message sent by SPI. + * + * @param msg Sent message. + * @param nodeId Receiver node id. + */ + private void onMessageSent(Message msg, UUID nodeId) { + sentMsgsMetricsByType.computeIfAbsent(msg.directType(), sentMsgsCntByTypeMetricFactory).increment(); + + sentMsgsMetricsByNodeId.computeIfAbsent(nodeId, sentMsgsCntByNodeIdMetricFactory).increment(); + } + + /** + * Collects statistics for message received by SPI. + * + * @param msg Received message. + * @param nodeId Sender node id. + */ + private void onMessageReceived(Message msg, UUID nodeId) { + rcvdMsgsMetricsByType.computeIfAbsent(msg.directType(), rcvdMsgsCntByTypeMetricFactory).increment(); + + rcvdMsgsMetricsByNodeId.computeIfAbsent(nodeId, rcvdMsgsCntByNodeIdMetricFactory).increment(); + } + } } diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java index acd82367a94cb..0a21d5d72b5ab 100755 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java @@ -417,9 +417,15 @@ public class TcpCommunicationSpi extends IgniteSpiAdapter implements Communicati /** */ public static final String RECEIVED_MESSAGES_METRIC_DESC = "Total number of messages received by current node"; + /** */ + public static final String SENT_MESSAGES_BY_TYPE_METRIC_NAME = "sentMessagesByType"; + /** */ public static final String SENT_MESSAGES_BY_TYPE_METRIC_DESC = "Total number of messages with given type sent by current node"; + /** */ + public static final String RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME = "receivedMessagesByType"; + /** */ public static final String RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC = "Total number of messages with given type received by current node"; From 002bd64bde378d8b5fd4c67ba34d2b10f12ce15c Mon Sep 17 00:00:00 2001 From: ibessonov Date: Thu, 26 Dec 2019 12:51:10 +0300 Subject: [PATCH 15/18] IGNITE-12108 HashMap replaced with Map in ThreadMetrics --- .../tcp/TcpCommunicationMetricsListener.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index 62e24d3e95bc6..e664bee3a40cd 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -406,20 +406,20 @@ private void updateMessageTypeMap(Message msg) { */ private class ThreadMetrics { /** Sent messages count metrics grouped by message type. */ - private final HashMap sentMsgsMetricsByType = new HashMap<>(); + private final Map sentMsgsMetricsByType = new HashMap<>(); /** Received messages count metrics grouped by message type. */ - private final HashMap rcvdMsgsMetricsByType = new HashMap<>(); + private final Map rcvdMsgsMetricsByType = new HashMap<>(); /** * Sent messages count metrics grouped by message node id. */ - public volatile HashMap sentMsgsMetricsByNodeId = new HashMap<>(); + public volatile Map sentMsgsMetricsByNodeId = new HashMap<>(); /** * Received messages metrics count grouped by message node id. */ - public volatile HashMap rcvdMsgsMetricsByNodeId = new HashMap<>(); + public volatile Map rcvdMsgsMetricsByNodeId = new HashMap<>(); /** From d93e7e5dad4a4a971ec2be005a9cac69580b64b0 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Fri, 10 Jan 2020 11:24:53 +0300 Subject: [PATCH 16/18] IGNITE-12108 Latest review remarks addressed: removed empty catch blocks; metrics registration moved to listeners. --- .../tcp/TcpCommunicationMetricsListener.java | 47 ++++++++----------- .../tcp/TcpCommunicationStatisticsTest.java | 3 +- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index e664bee3a40cd..712a092e9fd41 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -139,23 +139,23 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { sentMsgsMetric = mreg.longAdderMetric(SENT_MESSAGES_METRIC_NAME, SENT_MESSAGES_METRIC_DESC); rcvdMsgsMetric = mreg.longAdderMetric(RECEIVED_MESSAGES_METRIC_NAME, RECEIVED_MESSAGES_METRIC_DESC); + + mmgr.addMetricRegistryCreationListener(mreg -> { + // Metrics for the specific nodes. + if (!mreg.name().startsWith(COMMUNICATION_METRICS_GROUP_NAME + MetricUtils.SEPARATOR)) + return; + + mreg.longAdderMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME, SENT_MESSAGES_BY_NODE_ID_METRIC_DESC); + + mreg.longAdderMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME, RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC); + }); } /** */ private static synchronized MetricRegistry getOrCreateMetricRegistry(GridMetricManager mmgr, UUID nodeId) { String regName = MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString()); - for (MetricRegistry mreg : mmgr) { - if (mreg.name().equals(regName)) - return mreg; - } - - MetricRegistry mreg = mmgr.registry(regName); - - mreg.longAdderMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME, SENT_MESSAGES_BY_NODE_ID_METRIC_DESC); - mreg.longAdderMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME, RECEIVED_MESSAGES_BY_NODE_ID_METRIC_DESC); - - return mreg; + return mmgr.registry(regName); } /** Metrics registry. */ @@ -289,19 +289,15 @@ protected Map collectMessagesCountByType(String prefix) { for (Metric metric : mreg) { if (metric.name().startsWith(prefix)) { - try { - short directType = Short.parseShort(metric.name().substring(prefix.length())); + short directType = Short.parseShort(metric.name().substring(prefix.length())); - Map msgTypMap0 = msgTypMap; + Map msgTypMap0 = msgTypMap; - if (msgTypMap0 != null) { - String typeName = msgTypMap0.get(directType); + if (msgTypMap0 != null) { + String typeName = msgTypMap0.get(directType); - if (typeName != null) - res.put(typeName, ((LongMetric)metric).value()); - } - } - catch (NumberFormatException ignore) { + if (typeName != null) + res.put(typeName, ((LongMetric)metric).value()); } } } @@ -314,17 +310,14 @@ protected Map collectMessagesCountByNodeId(String metricName) { Map res = new HashMap<>(); String mregPrefix = COMMUNICATION_METRICS_GROUP_NAME + MetricUtils.SEPARATOR; + for (MetricRegistry mreg : mmgr) { if (mreg.name().startsWith(mregPrefix)) { String nodeIdStr = mreg.name().substring(mregPrefix.length()); - try { - UUID nodeId = UUID.fromString(nodeIdStr); + UUID nodeId = UUID.fromString(nodeIdStr); - res.put(nodeId, mreg.findMetric(metricName).value()); - } - catch (IllegalArgumentException ignore) { - } + res.put(nodeId, mreg.findMetric(metricName).value()); } } diff --git a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java index 90ee5729fadad..399b8a36361ac 100644 --- a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java +++ b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationStatisticsTest.java @@ -218,7 +218,8 @@ public void testStatistics() throws Exception { mreg0 = grid(0).context().metric().registry(node1regName); sentMetric = mreg0.findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); - assertNull(sentMetric); + assertNotNull(sentMetric); // Automatically generated by MetricRegistryCreationListener. + assertEquals(0, sentMetric.value()); } } finally { From 284bc638acc8118f271d3ef1f7121296d81818bd Mon Sep 17 00:00:00 2001 From: ibessonov Date: Fri, 10 Jan 2020 11:39:21 +0300 Subject: [PATCH 17/18] IGNITE-12108 Method inlined. --- .../tcp/TcpCommunicationMetricsListener.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index 712a092e9fd41..ebaff6d5ef89c 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -128,11 +128,13 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { RECEIVED_MESSAGES_BY_TYPE_METRIC_DESC ); - sentMsgsCntByNodeIdMetricFactory = nodeId -> getOrCreateMetricRegistry(mmgr, nodeId) - .findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); + sentMsgsCntByNodeIdMetricFactory = nodeId -> + mmgr.registry(MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())) + .findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); - rcvdMsgsCntByNodeIdMetricFactory = nodeId -> getOrCreateMetricRegistry(mmgr, nodeId) - .findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); + rcvdMsgsCntByNodeIdMetricFactory = nodeId -> + mmgr.registry(MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())) + .findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); sentBytesMetric = mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); rcvdBytesMetric = mreg.longAdderMetric(RECEIVED_BYTES_METRIC_NAME, RECEIVED_BYTES_METRIC_DESC); @@ -151,13 +153,6 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { }); } - /** */ - private static synchronized MetricRegistry getOrCreateMetricRegistry(GridMetricManager mmgr, UUID nodeId) { - String regName = MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString()); - - return mmgr.registry(regName); - } - /** Metrics registry. */ public MetricRegistry metricRegistry() { return mreg; From f07eb731b40697bad427e4c7822f220f2ac2c172 Mon Sep 17 00:00:00 2001 From: ibessonov Date: Fri, 10 Jan 2020 12:10:40 +0300 Subject: [PATCH 18/18] IGNITE-12108 Minor refactoring. --- .../tcp/TcpCommunicationMetricsListener.java | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java index ebaff6d5ef89c..4fdfa1370d4a9 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationMetricsListener.java @@ -28,11 +28,12 @@ import org.apache.ignite.internal.processors.metric.GridMetricManager; import org.apache.ignite.internal.processors.metric.MetricRegistry; import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric; -import org.apache.ignite.internal.processors.metric.impl.MetricUtils; import org.apache.ignite.plugin.extensions.communication.Message; import org.apache.ignite.spi.metric.LongMetric; import org.apache.ignite.spi.metric.Metric; +import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.SEPARATOR; +import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.metricName; import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_DESC; import static org.apache.ignite.internal.util.nio.GridNioServer.RECEIVED_BYTES_METRIC_NAME; import static org.apache.ignite.internal.util.nio.GridNioServer.SENT_BYTES_METRIC_DESC; @@ -103,16 +104,6 @@ class TcpCommunicationMetricsListener { /** Message type map. */ private volatile Map msgTypMap; - /** Generate metric name by message direct type id. */ - public static String sentMessagesByTypeMetricName(Short directType) { - return MetricUtils.metricName(SENT_MESSAGES_BY_TYPE_METRIC_NAME, directType.toString()); - } - - /** Generate metric name by message direct type id. */ - public static String receivedMessagesByTypeMetricName(Short directType) { - return MetricUtils.metricName(RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME, directType.toString()); - } - /** */ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { this.mmgr = mmgr; @@ -129,11 +120,11 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { ); sentMsgsCntByNodeIdMetricFactory = nodeId -> - mmgr.registry(MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())) + mmgr.registry(metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())) .findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME); rcvdMsgsCntByNodeIdMetricFactory = nodeId -> - mmgr.registry(MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())) + mmgr.registry(metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())) .findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME); sentBytesMetric = mreg.longAdderMetric(SENT_BYTES_METRIC_NAME, SENT_BYTES_METRIC_DESC); @@ -144,7 +135,7 @@ public TcpCommunicationMetricsListener(GridMetricManager mmgr) { mmgr.addMetricRegistryCreationListener(mreg -> { // Metrics for the specific nodes. - if (!mreg.name().startsWith(COMMUNICATION_METRICS_GROUP_NAME + MetricUtils.SEPARATOR)) + if (!mreg.name().startsWith(COMMUNICATION_METRICS_GROUP_NAME + SEPARATOR)) return; mreg.longAdderMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME, SENT_MESSAGES_BY_NODE_ID_METRIC_DESC); @@ -246,7 +237,7 @@ public long receivedBytesCount() { * @return Map containing message types and respective counts. */ public Map receivedMessagesByType() { - return collectMessagesCountByType(RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME + MetricUtils.SEPARATOR); + return collectMessagesCountByType(RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME + SEPARATOR); } /** @@ -264,7 +255,7 @@ public Map receivedMessagesByNode() { * @return Map containing message types and respective counts. */ public Map sentMessagesByType() { - return collectMessagesCountByType(SENT_MESSAGES_BY_TYPE_METRIC_NAME + MetricUtils.SEPARATOR); + return collectMessagesCountByType(SENT_MESSAGES_BY_TYPE_METRIC_NAME + SEPARATOR); } /** @@ -280,7 +271,7 @@ public Map sentMessagesByNode() { protected Map collectMessagesCountByType(String prefix) { Map res = new HashMap<>(); - prefix = MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, prefix); + prefix = metricName(COMMUNICATION_METRICS_GROUP_NAME, prefix); for (Metric metric : mreg) { if (metric.name().startsWith(prefix)) { @@ -304,7 +295,7 @@ protected Map collectMessagesCountByType(String prefix) { protected Map collectMessagesCountByNodeId(String metricName) { Map res = new HashMap<>(); - String mregPrefix = COMMUNICATION_METRICS_GROUP_NAME + MetricUtils.SEPARATOR; + String mregPrefix = COMMUNICATION_METRICS_GROUP_NAME + SEPARATOR; for (MetricRegistry mreg : mmgr) { if (mreg.name().startsWith(mregPrefix)) { @@ -337,7 +328,7 @@ else if (metric.name().startsWith(RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME)) } for (MetricRegistry mreg : mmgr) { - if (mreg.name().startsWith(COMMUNICATION_METRICS_GROUP_NAME + MetricUtils.SEPARATOR)) { + if (mreg.name().startsWith(COMMUNICATION_METRICS_GROUP_NAME + SEPARATOR)) { mreg.findMetric(SENT_MESSAGES_BY_NODE_ID_METRIC_NAME).reset(); mreg.findMetric(RECEIVED_MESSAGES_BY_NODE_ID_METRIC_NAME).reset(); @@ -354,7 +345,7 @@ public void onNodeLeft(UUID nodeId) { threadMetrics.sentMsgsMetricsByNodeId = new HashMap<>(); } - mmgr.remove(MetricUtils.metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())); + mmgr.remove(metricName(COMMUNICATION_METRICS_GROUP_NAME, nodeId.toString())); } /** @@ -389,6 +380,16 @@ private void updateMessageTypeMap(Message msg) { } } + /** Generate metric name by message direct type id. */ + public static String sentMessagesByTypeMetricName(Short directType) { + return metricName(SENT_MESSAGES_BY_TYPE_METRIC_NAME, directType.toString()); + } + + /** Generate metric name by message direct type id. */ + public static String receivedMessagesByTypeMetricName(Short directType) { + return metricName(RECEIVED_MESSAGES_BY_TYPE_METRIC_NAME, directType.toString()); + } + /** * Thread-local metrics. */