sashapolo commented on code in PR #3068:
URL: https://github.com/apache/ignite-3/pull/3068#discussion_r1461740828


##########
modules/network/src/main/java/org/apache/ignite/network/NettyWorkersRegistrar.java:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.network;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static 
org.apache.ignite.internal.util.CompletableFutures.nullCompletedFuture;
+
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.util.concurrent.EventExecutor;
+import io.netty.util.concurrent.SingleThreadEventExecutor;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Future;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import org.apache.ignite.internal.manager.IgniteComponent;
+import org.apache.ignite.internal.worker.CriticalWorker;
+import org.apache.ignite.internal.worker.CriticalWorkerRegistry;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * This component is responsible for registering Netty workers with the {@link 
CriticalWorkerRegistry} and for updating their heartbeats.
+ */
+public class NettyWorkersRegistrar implements IgniteComponent {
+    /*
+     * It seems impossible to instrument tasks executed by Netty event loops, 
so the strategy we use is to
+     * send 'heartbeat' tasks to each of the event loops periodically; this 
tasks just update the heartbeat timestamp
+     * of the worker corresponding to an event loop. If an event loop's thread 
is hung, the hearbeat will not
+     * be updated, and the worker watchdog will treat the event loop worker as 
blocked.
+     */
+
+    private final CriticalWorkerRegistry criticalWorkerRegistry;
+
+    private final ScheduledExecutorService scheduler;
+
+    private final NettyBootstrapFactory bootstrapFactory;
+
+    private final List<NettyWorker> workers = new CopyOnWriteArrayList<>();
+
+    @Nullable
+    private volatile ScheduledFuture<?> sendHearbeatsTaskFuture;
+
+    /**
+     * Constructor.
+     *
+     * @param criticalWorkerRegistry Used to register workers representing 
Netty event loops to detect when their threads are
+     *         blocked.
+     * @param scheduler Used to schedule periodic tasks.
+     * @param bootstrapFactory Used to obtain Netty workers.
+     */
+    public NettyWorkersRegistrar(
+            CriticalWorkerRegistry criticalWorkerRegistry,
+            ScheduledExecutorService scheduler,
+            NettyBootstrapFactory bootstrapFactory
+    ) {
+        this.criticalWorkerRegistry = criticalWorkerRegistry;
+        this.scheduler = scheduler;
+        this.bootstrapFactory = bootstrapFactory;
+    }
+
+    @Override
+    public CompletableFuture<Void> start() {
+        for (NioEventLoopGroup group : bootstrapFactory.eventLoopGroups()) {
+            registerWorkersFor(group);
+        }
+
+        sendHearbeatsTaskFuture = 
scheduler.scheduleAtFixedRate(this::sendHearbeats, 100, 100, MILLISECONDS);
+
+        return nullCompletedFuture();
+    }
+
+    private void registerWorkersFor(NioEventLoopGroup group) {
+        List<NettyWorker> groupWorkers = new 
ArrayList<>(group.executorCount());
+
+        for (EventExecutor eventExecutor : group) {
+            SingleThreadEventExecutor executor = (SingleThreadEventExecutor) 
eventExecutor;
+            groupWorkers.add(new NettyWorker(executor));
+        }
+
+        for (NettyWorker worker : groupWorkers) {
+            criticalWorkerRegistry.register(worker);
+        }
+
+        workers.addAll(groupWorkers);
+    }
+
+    private void sendHearbeats() {
+        for (NettyWorker worker : workers) {
+            worker.sendHeartbeat();
+        }
+    }
+
+    @Override
+    public void stop() throws Exception {
+        Future<?> heartBeatsTaskFuture = sendHearbeatsTaskFuture;
+        if (heartBeatsTaskFuture != null) {
+            heartBeatsTaskFuture.cancel(false);
+        }
+
+        for (NettyWorker worker : workers) {
+            criticalWorkerRegistry.unregister(worker);
+        }
+    }
+
+    private static class NettyWorker implements CriticalWorker {
+        private final SingleThreadEventExecutor eventExecutor;
+
+        private volatile long heartbeatNanos = System.nanoTime();
+
+        private final Runnable sendHeartbeatTask = () -> heartbeatNanos = 
System.nanoTime();

Review Comment:
   Is there a difference if you allocate it here or once inside `sendHeartbeat`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to