valepakh commented on code in PR #3522:
URL: https://github.com/apache/ignite-3/pull/3522#discussion_r1546159440


##########
modules/eventlog/src/main/java/org/apache/ignite/internal/eventlog/config/schema/SinkConfigurationSchema.java:
##########
@@ -20,16 +20,20 @@
 import org.apache.ignite.configuration.annotation.InjectedName;
 import org.apache.ignite.configuration.annotation.PolymorphicConfig;
 import org.apache.ignite.configuration.annotation.PolymorphicId;
+import org.apache.ignite.configuration.annotation.Value;
 
 
 /** Configuration schema for sink. */
 @PolymorphicConfig
 public class SinkConfigurationSchema {
     /** The id of the sink that is used to identify the type: log, webhook, 
kafka. */

Review Comment:
   Could you rephrase this to reflect that this is not an id, but the type now?



##########
modules/eventlog/src/main/java/org/apache/ignite/internal/eventlog/impl/ChannelRegistry.java:
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.eventlog.impl;
+
+import java.util.Set;
+import org.apache.ignite.internal.eventlog.api.EventChannel;
+
+interface ChannelRegistry {

Review Comment:
   Even if it's an internal interface and more or less self-explanatory, I'd 
like to see a javadoc here.



##########
modules/eventlog/src/main/java/org/apache/ignite/internal/eventlog/impl/ConfigurationBasedChannelRegistry.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.eventlog.impl;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.stream.Collectors;
+import org.apache.ignite.configuration.NamedListView;
+import org.apache.ignite.configuration.notifications.ConfigurationListener;
+import 
org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent;
+import org.apache.ignite.internal.eventlog.api.EventChannel;
+import org.apache.ignite.internal.eventlog.config.schema.ChannelView;
+import org.apache.ignite.internal.eventlog.config.schema.EventLogConfiguration;
+
+class ConfigurationBasedChannelRegistry implements ChannelRegistry {
+    private final ReadWriteLock guard;
+
+    private final Map<String, EventChannel> cache;
+
+    private final Map<String, Set<EventChannel>> typeCache;
+
+    private final SinkRegistry sinkRegistry;
+
+    ConfigurationBasedChannelRegistry(EventLogConfiguration cfg, SinkRegistry 
sinkRegistry) {
+        this.guard = new ReentrantReadWriteLock();
+        this.cache = new HashMap<>();
+        this.typeCache = new HashMap<>();
+        this.sinkRegistry = sinkRegistry;
+
+        cfg.channels().listen(new CacheUpdater());
+    }
+
+    @Override
+    public EventChannel getByName(String name) {
+        guard.readLock().lock();
+        try {
+            return cache.get(name);
+        } finally {
+            guard.readLock().unlock();
+        }
+    }
+
+    @Override
+    public Set<EventChannel> findAllChannelsByEventType(String 
igniteEventType) {
+        guard.readLock().lock();
+        try {
+            Set<EventChannel> result = typeCache.get(igniteEventType);
+            return result == null ? Set.of() : result;
+        } finally {
+            guard.readLock().unlock();
+        }
+    }
+
+    private class CacheUpdater implements 
ConfigurationListener<NamedListView<ChannelView>> {
+        @Override
+        public CompletableFuture<?> 
onUpdate(ConfigurationNotificationEvent<NamedListView<ChannelView>> ctx) {
+            NamedListView<ChannelView> newListValue = ctx.newValue();
+
+            guard.writeLock().lock();
+
+            try {
+                cache.clear();
+                typeCache.clear();
+
+                newListValue.forEach(view -> {
+                    if (view.enabled()) {
+                        cache.put(view.name(), createChannel(view));
+                        for (String eventType : view.events()) {
+                            typeCache.computeIfAbsent(
+                                    eventType.trim(),
+                                    t -> ConcurrentHashMap.newKeySet()

Review Comment:
   Is the thread-safe set really needed here?



##########
modules/eventlog/src/test/java/org/apache/ignite/internal/eventlog/impl/ConfigurationBasedChannelRegistryTest.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.eventlog.impl;
+
+import static org.awaitility.Awaitility.await;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import java.util.Objects;
+import 
org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import 
org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.eventlog.api.EventChannel;
+import org.apache.ignite.internal.eventlog.api.IgniteEventType;
+import org.apache.ignite.internal.eventlog.config.schema.EventLogConfiguration;
+import org.apache.ignite.internal.testframework.BaseIgniteAbstractTest;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@ExtendWith(ConfigurationExtension.class)
+class ConfigurationBasedChannelRegistryTest extends BaseIgniteAbstractTest {
+    private static final String TEST_CHANNEL = "testChannel";
+
+    @InjectConfiguration
+    private EventLogConfiguration cfg;
+
+    private ConfigurationBasedChannelRegistry registry;
+
+    @BeforeEach
+    void setUp() {
+        registry = new ConfigurationBasedChannelRegistry(cfg, new 
ConfigurationBasedSinkRegistry(cfg));
+    }
+
+    @Test
+    void noSuchChannel() {
+        assertNull(registry.getByName("noSuchChannel"));
+    }
+
+    @Test
+    void addNewConfigurationEntry() {
+        // Given configuration with a channel.
+        cfg.channels().change(c -> c.create(TEST_CHANNEL, s -> {
+            s.changeEnabled(true);
+            s.changeEvents(IgniteEventType.USER_AUTHENTICATED.name());
+        }));
+
+        // When get channel from registry.
+        EventChannel channel = await().until(

Review Comment:
   Shouldn't the registry be updated after the change configuration future is 
complete?



##########
modules/eventlog/src/main/java/org/apache/ignite/internal/eventlog/impl/ConfigurationBasedSinkRegistry.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.eventlog.impl;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.ignite.configuration.NamedListView;
+import org.apache.ignite.configuration.notifications.ConfigurationListener;
+import 
org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent;
+import org.apache.ignite.internal.eventlog.api.Sink;
+import org.apache.ignite.internal.eventlog.config.schema.EventLogConfiguration;
+import org.apache.ignite.internal.eventlog.config.schema.SinkView;
+
+class ConfigurationBasedSinkRegistry implements SinkRegistry {
+    private final ReadWriteLock guard;
+
+    private final Map<String, Sink> cache;
+
+    private final Map<String, Set<Sink>> cacheByChannel;
+
+    private final SinkFactory sinkFactory;
+
+    ConfigurationBasedSinkRegistry(EventLogConfiguration cfg) {
+        this.guard = new ReentrantReadWriteLock();
+        this.cache = new HashMap<>();
+        this.cacheByChannel = new HashMap<>();
+        this.sinkFactory = new SinkFactory();
+
+        cfg.sinks().listen(new CacheUpdater());
+    }
+
+    @Override
+    public Sink getByName(String name) {
+        guard.readLock().lock();
+        try {
+            return cache.get(name);
+        } finally {
+            guard.readLock().unlock();
+        }
+    }
+
+    @Override
+    public Set<Sink> findAllByChannel(String channel) {
+        guard.readLock().lock();
+        try {
+            Set<Sink> sinks = cacheByChannel.get(channel);
+            return new HashSet<>(sinks != null ? sinks : Set.of());
+        } finally {
+            guard.readLock().unlock();
+        }
+    }
+
+    private class CacheUpdater implements 
ConfigurationListener<NamedListView<SinkView>> {
+        @Override
+        public CompletableFuture<?> 
onUpdate(ConfigurationNotificationEvent<NamedListView<SinkView>> ctx) {
+            NamedListView<SinkView> newListValue = ctx.newValue();
+
+            guard.writeLock().lock();
+            try {
+                cache.clear();
+                cacheByChannel.clear();
+                for (SinkView sinkView : newListValue) {
+                    cache.put(sinkView.name(), 
sinkFactory.createSink(sinkView));
+                    cacheByChannel.computeIfAbsent(sinkView.channel(), k -> 
new HashSet<>()).add(cache.get(sinkView.name()));
+                }
+                return CompletableFuture.completedFuture(null);

Review Comment:
   Same comments here.



##########
modules/eventlog/src/main/java/org/apache/ignite/internal/eventlog/config/schema/SinkConfigurationSchema.java:
##########
@@ -20,16 +20,20 @@
 import org.apache.ignite.configuration.annotation.InjectedName;
 import org.apache.ignite.configuration.annotation.PolymorphicConfig;
 import org.apache.ignite.configuration.annotation.PolymorphicId;
+import org.apache.ignite.configuration.annotation.Value;
 
 
 /** Configuration schema for sink. */
 @PolymorphicConfig
 public class SinkConfigurationSchema {
     /** The id of the sink that is used to identify the type: log, webhook, 
kafka. */
     @PolymorphicId(hasDefault = true)
-    public String id = LogSinkConfigurationSchema.POLYMORPHIC_ID;
+    public String type = LogSinkConfigurationSchema.POLYMORPHIC_ID;
 
     /** The name of the sink. */
     @InjectedName
     public String name;
+

Review Comment:
   Please add a comment here.



##########
modules/eventlog/src/main/java/org/apache/ignite/internal/eventlog/impl/ConfigurationBasedChannelRegistry.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.eventlog.impl;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.stream.Collectors;
+import org.apache.ignite.configuration.NamedListView;
+import org.apache.ignite.configuration.notifications.ConfigurationListener;
+import 
org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent;
+import org.apache.ignite.internal.eventlog.api.EventChannel;
+import org.apache.ignite.internal.eventlog.config.schema.ChannelView;
+import org.apache.ignite.internal.eventlog.config.schema.EventLogConfiguration;
+
+class ConfigurationBasedChannelRegistry implements ChannelRegistry {
+    private final ReadWriteLock guard;
+
+    private final Map<String, EventChannel> cache;
+
+    private final Map<String, Set<EventChannel>> typeCache;
+
+    private final SinkRegistry sinkRegistry;
+
+    ConfigurationBasedChannelRegistry(EventLogConfiguration cfg, SinkRegistry 
sinkRegistry) {
+        this.guard = new ReentrantReadWriteLock();
+        this.cache = new HashMap<>();
+        this.typeCache = new HashMap<>();
+        this.sinkRegistry = sinkRegistry;
+
+        cfg.channels().listen(new CacheUpdater());
+    }
+
+    @Override
+    public EventChannel getByName(String name) {
+        guard.readLock().lock();
+        try {
+            return cache.get(name);
+        } finally {
+            guard.readLock().unlock();
+        }
+    }
+
+    @Override
+    public Set<EventChannel> findAllChannelsByEventType(String 
igniteEventType) {
+        guard.readLock().lock();
+        try {
+            Set<EventChannel> result = typeCache.get(igniteEventType);
+            return result == null ? Set.of() : result;
+        } finally {
+            guard.readLock().unlock();
+        }
+    }
+
+    private class CacheUpdater implements 
ConfigurationListener<NamedListView<ChannelView>> {
+        @Override
+        public CompletableFuture<?> 
onUpdate(ConfigurationNotificationEvent<NamedListView<ChannelView>> ctx) {
+            NamedListView<ChannelView> newListValue = ctx.newValue();
+
+            guard.writeLock().lock();
+
+            try {
+                cache.clear();
+                typeCache.clear();
+
+                newListValue.forEach(view -> {
+                    if (view.enabled()) {
+                        cache.put(view.name(), createChannel(view));
+                        for (String eventType : view.events()) {
+                            typeCache.computeIfAbsent(
+                                    eventType.trim(),
+                                    t -> ConcurrentHashMap.newKeySet()
+                            ).add(cache.get(view.name()));
+                        }
+                    }
+                });
+
+                return CompletableFuture.completedFuture(null);

Review Comment:
   Could you please use statically imported 
`CompletableFutures.nullCompletedFuture()` here?



##########
modules/eventlog/src/main/java/org/apache/ignite/internal/eventlog/impl/ConfigurationBasedChannelRegistry.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.eventlog.impl;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.stream.Collectors;
+import org.apache.ignite.configuration.NamedListView;
+import org.apache.ignite.configuration.notifications.ConfigurationListener;
+import 
org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent;
+import org.apache.ignite.internal.eventlog.api.EventChannel;
+import org.apache.ignite.internal.eventlog.config.schema.ChannelView;
+import org.apache.ignite.internal.eventlog.config.schema.EventLogConfiguration;
+
+class ConfigurationBasedChannelRegistry implements ChannelRegistry {
+    private final ReadWriteLock guard;
+
+    private final Map<String, EventChannel> cache;
+
+    private final Map<String, Set<EventChannel>> typeCache;
+
+    private final SinkRegistry sinkRegistry;
+
+    ConfigurationBasedChannelRegistry(EventLogConfiguration cfg, SinkRegistry 
sinkRegistry) {
+        this.guard = new ReentrantReadWriteLock();
+        this.cache = new HashMap<>();
+        this.typeCache = new HashMap<>();
+        this.sinkRegistry = sinkRegistry;
+
+        cfg.channels().listen(new CacheUpdater());
+    }
+
+    @Override
+    public EventChannel getByName(String name) {
+        guard.readLock().lock();
+        try {
+            return cache.get(name);
+        } finally {
+            guard.readLock().unlock();
+        }
+    }
+
+    @Override
+    public Set<EventChannel> findAllChannelsByEventType(String 
igniteEventType) {
+        guard.readLock().lock();
+        try {
+            Set<EventChannel> result = typeCache.get(igniteEventType);
+            return result == null ? Set.of() : result;
+        } finally {
+            guard.readLock().unlock();
+        }
+    }
+
+    private class CacheUpdater implements 
ConfigurationListener<NamedListView<ChannelView>> {
+        @Override
+        public CompletableFuture<?> 
onUpdate(ConfigurationNotificationEvent<NamedListView<ChannelView>> ctx) {
+            NamedListView<ChannelView> newListValue = ctx.newValue();
+
+            guard.writeLock().lock();
+
+            try {
+                cache.clear();
+                typeCache.clear();
+
+                newListValue.forEach(view -> {
+                    if (view.enabled()) {
+                        cache.put(view.name(), createChannel(view));
+                        for (String eventType : view.events()) {
+                            typeCache.computeIfAbsent(
+                                    eventType.trim(),
+                                    t -> ConcurrentHashMap.newKeySet()
+                            ).add(cache.get(view.name()));

Review Comment:
   Can you reuse the result of `createChannel` here?



-- 
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