PakhomovAlexander commented on code in PR #2359: URL: https://github.com/apache/ignite-3/pull/2359#discussion_r1285688630
########## modules/cli/src/main/java/org/apache/ignite/internal/cli/core/repl/EventListeningActivationPoint.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.cli.core.repl; + +import jakarta.inject.Inject; +import jakarta.inject.Singleton; +import org.apache.ignite.internal.cli.core.repl.prompt.ReplPromptProvider; +import org.apache.ignite.internal.cli.core.repl.registry.impl.ClusterConfigRegistryImpl; +import org.apache.ignite.internal.cli.core.repl.registry.impl.MetricRegistryImpl; +import org.apache.ignite.internal.cli.core.repl.registry.impl.NodeConfigRegistryImpl; +import org.apache.ignite.internal.cli.core.repl.registry.impl.UnitsRegistryImpl; +import org.apache.ignite.internal.cli.event.EventSubscriber; +import org.apache.ignite.internal.cli.event.EventType; + +/** + * Subscribes beans on appropriate events. + */ +@Singleton +public class EventListeningActivationPoint { + + @Inject + private EventSubscriber eventSubscriber; Review Comment: Can you please rename `EventSubscriber` to something like `EventSubscribtionManager`? Because I'm confused a little. When I read "Event Subscriber" I think of it like this object is subscribed to event but not subscribes others. ########## modules/cli/src/integrationTest/java/org/apache/ignite/internal/cli/commands/CliCommandTestNotInitializedIntegrationBase.java: ########## @@ -89,13 +94,14 @@ public void setUp(TestInfo testInfo) throws Exception { cmd = new CommandLine(getCommandClass(), new MicronautFactory(context)) .registerConverter(NodeNameOrUrl.class, new NodeNameOrUrlConverter(nodeNameRegistry)); cmd.setDefaultValueProvider(configDefaultValueProvider); + eventListeningActivationPoint.subscribe(); resetOutput(); CommandLineContextProvider.setCmd(cmd); } @AfterEach public void tearDown() { - session.disconnect(); + eventPublisher.fireEvent(Events.disconnect()); Review Comment: `Publisher` is supposed to `publish`. Could you rename the method? ########## modules/cli/src/main/java/org/apache/ignite/internal/cli/event/Event.java: ########## @@ -0,0 +1,25 @@ +/* + * 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.cli.event; + +/** + * The event cas which is produced by event producer component. + */ +public interface Event { + EventType eventType(); +} Review Comment: Missed eof. It is better to configure your IDEA to do it automatically. ########## modules/cli/src/main/java/org/apache/ignite/internal/cli/core/repl/ConnectionHeartBeat.java: ########## @@ -0,0 +1,130 @@ +/* + * 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.cli.core.repl; + +import io.micronaut.context.annotation.Value; +import jakarta.inject.Singleton; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.annotation.Nullable; +import org.apache.ignite.internal.cli.core.rest.ApiClientFactory; +import org.apache.ignite.internal.cli.event.Event; +import org.apache.ignite.internal.cli.event.EventListener; +import org.apache.ignite.internal.cli.event.EventPublisher; +import org.apache.ignite.internal.cli.event.EventType; +import org.apache.ignite.internal.cli.event.Events; +import org.apache.ignite.internal.cli.logger.CliLoggers; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.thread.NamedThreadFactory; +import org.apache.ignite.rest.client.api.NodeManagementApi; +import org.apache.ignite.rest.client.invoker.ApiException; + +/** + * Connection to node heart beat. + */ +@Singleton +public class ConnectionHeartBeat implements EventListener { + + private static final IgniteLogger log = CliLoggers.forClass(ConnectionHeartBeat.class); + + /** CLI check connection period period. */ + private final long cliCheckConnectionPeriodSecond; + + /** Scheduled executor for connection heartbeat. */ + @Nullable + private ScheduledExecutorService scheduledConnectionHeartbeatExecutor; + + private final ApiClientFactory clientFactory; + + private final EventPublisher eventPublisher; + + private final AtomicBoolean connected = new AtomicBoolean(false); + + /** + * Creates the instance of connection heartbeat. + * + * @param clientFactory api client factory. + * @param eventPublisher event publisher. + */ + public ConnectionHeartBeat(@Value("${cli.check.connection.period.second:5}") long cliCheckConnectionPeriodSecond, + ApiClientFactory clientFactory, + EventPublisher eventPublisher) { + this.clientFactory = clientFactory; + this.eventPublisher = eventPublisher; + this.cliCheckConnectionPeriodSecond = cliCheckConnectionPeriodSecond; + } + + /** + * Starts connection heartbeat. By default connection will be checked every 5 sec. + * + * @param sessionInfo session info with node url + */ + private void onConnect(SessionInfo sessionInfo) { + if (connected.compareAndSet(false, true)) { + eventPublisher.fireEvent(Events.connectionRestored()); + } + + if (scheduledConnectionHeartbeatExecutor == null) { Review Comment: I think a null check on `scheduledConnectionHeartbeatExecutor ` should be done under the lock in `onConnect` and in `onDisconnect` methods. ########## modules/cli/src/integrationTest/java/org/apache/ignite/internal/cli/commands/ItConnectionHeartbeatTest.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.internal.cli.commands; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import io.micronaut.context.annotation.Property; +import io.micronaut.context.annotation.Value; +import jakarta.inject.Inject; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.ignite.internal.cli.core.repl.Session; +import org.apache.ignite.internal.cli.event.EventFactory; +import org.apache.ignite.internal.cli.event.EventListener; +import org.apache.ignite.internal.cli.event.EventType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@Property(name="cli.check.connection.period.second", value="1") +class ItConnectionHeartbeatTest extends CliCommandTestInitializedIntegrationBase { + + @Inject + Session session; + + @Inject + EventFactory eventFactory; + + @Value("${cli.check.connection.period.second}") + private long CLI_CHECK_CONNECTION_PERIOD_SECONDS; + + private final AtomicInteger connectionLost = new AtomicInteger(0); + private final AtomicInteger connectionRestored = new AtomicInteger(0); + + @BeforeEach + void setUp() { + //ToDo: Set connection check timeout to 1 sec to make test fast Review Comment: Still here ########## modules/cli/src/main/java/org/apache/ignite/internal/cli/event/EventFactory.java: ########## @@ -0,0 +1,87 @@ +/* + * 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.cli.event; + +import jakarta.inject.Singleton; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +import org.apache.ignite.internal.cli.logger.CliLoggers; +import org.apache.ignite.internal.logger.IgniteLogger; + +/** + * Register listeners and produces events. + */ +@Singleton +public class EventFactory implements EventPublisher, EventSubscriber { + + private static final IgniteLogger log = CliLoggers.forClass(EventFactory.class); + + /** All listeners. */ + private final ConcurrentHashMap<EventType, List<EventListener>> listeners = new ConcurrentHashMap<>(); + + public EventFactory() { + } + + /** + * Registers an event listener. + * + * @param eventType type of event to listen. + * @param eventListener event listener. + */ + @Override + public void listen(EventType eventType, EventListener eventListener) { Review Comment: I think it is better to use "subscribe" everywhere. Sometimes you mix "subscribe" and "listen" but in fact, there is no difference. -- 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]
