You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/04/28 13:57:51 UTC

[GitHub] [ignite-3] ascherbakoff commented on a change in pull request #111: IGNITE-14446 Added support of watch and put/get/scan operations to MetaStorageManager

ascherbakoff commented on a change in pull request #111:
URL: https://github.com/apache/ignite-3/pull/111#discussion_r622130698



##########
File path: modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/watch/WatchAggregator.java
##########
@@ -0,0 +1,243 @@
+/*
+ * 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.metastorage.watch;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.BiConsumer;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.metastorage.common.Key;
+import org.apache.ignite.metastorage.common.WatchEvent;
+import org.apache.ignite.metastorage.common.WatchListener;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Needed to aggregate multiple watches to one aggregated watch.
+ * This approach needed to provide the following additional guarantees to watching mechanism:
+ * - watch events will be processed sequentially
+ * - watch events will be resolved in the order of watch registration
+ */
+public class WatchAggregator {
+    /**
+     * Watches' map must be synchronized because of changes from WatchListener in separate thread.
+     */
+    private final Map<Long, Watch> watches = Collections.synchronizedMap(new LinkedHashMap<>());
+
+    /** Simple auto increment id for internal watches. */
+    private long idCntr;

Review comment:
       Should it be AtomicLong ?

##########
File path: modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/watch/WatchAggregator.java
##########
@@ -0,0 +1,243 @@
+/*
+ * 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.metastorage.watch;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.BiConsumer;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.metastorage.common.Key;
+import org.apache.ignite.metastorage.common.WatchEvent;
+import org.apache.ignite.metastorage.common.WatchListener;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Needed to aggregate multiple watches to one aggregated watch.
+ * This approach needed to provide the following additional guarantees to watching mechanism:
+ * - watch events will be processed sequentially
+ * - watch events will be resolved in the order of watch registration
+ */
+public class WatchAggregator {
+    /**
+     * Watches' map must be synchronized because of changes from WatchListener in separate thread.
+     */
+    private final Map<Long, Watch> watches = Collections.synchronizedMap(new LinkedHashMap<>());
+
+    /** Simple auto increment id for internal watches. */
+    private long idCntr;
+
+    /**
+     * Adds new watch with simple exact criterion.
+     *
+     * @param key Key for watching.
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long add(Key key, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.ExactCriterion(key), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Adds new watch with filter by key prefix.
+     *
+     * @param key Prefix for key.
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long addPrefix(Key key, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.PrefixCriterion(key), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Adds new watch with filter by collection of keys.
+     *
+     * @param keys Collection of keys to listen.
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long add(Collection<Key> keys, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.CollectionCriterion(keys), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Adds new watch with filter by collection of keys.
+     *
+     * @param from Start key of range to listen.
+     * @param to End key of range (exclusively)..
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long add(Key from, Key to, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.RangeCriterion(from, to), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Cancel watch by id.
+     *
+     * @param id of watch to cancel.
+     */
+    public void cancel(long id) {
+        watches.remove(id);
+    }
+
+    /**
+     * Cancel multiple watches by ids.
+     *
+     * @param ids of watches to cancel.
+     */
+    public void cancelAll(Collection<Long> ids) {
+        watches.keySet().removeAll(ids);
+    }
+
+    /**
+     * Produce watch with aggregated key criterion and general watch listener dispatcher.
+     *
+     * @param revision start revision to listen event.
+     * @param saveRevisionAct action to commit keys-revision pair to persistent store for processed keys.
+     * @return result aggregated watch.
+     */
+    public Optional<AggregatedWatch> watch(long revision, BiConsumer<Collection<IgniteBiTuple<Key, byte[]>>, Long> saveRevisionAct) {
+        synchronized (watches) {
+            if (watches.isEmpty())
+                return Optional.empty();
+            else
+                return Optional.of(new AggregatedWatch(inferGeneralCriteria(), revision, watchListener(saveRevisionAct)));
+        }
+    }
+
+    /**
+     * Returns general criterion, which overlays all aggregated criteria.
+     *
+     * @return aggregated criterion.
+     */
+    // TODO: We can do it better than infer range always
+    private KeyCriterion inferGeneralCriteria() {
+        return new KeyCriterion.RangeCriterion(
+            watches.values().stream().map(w -> w.keyCriterion().toRange().getKey()).min(Key::compareTo).get(),
+            watches.values().stream().map(w -> w.keyCriterion().toRange().getValue()).max(Key::compareTo).get()
+        );
+    }
+
+    /**
+     * Produces the watch listener, which will dispatch events to appropriate watches.
+     *
+     * @param storeRevision action to commit keys-revision pair to persistent store for processed keys.
+     * @return watch listener, which will dispatch events to appropriate watches.
+     */
+    private WatchListener watchListener(BiConsumer<Collection<IgniteBiTuple<Key, byte[]>>, Long> storeRevision) {
+        // Copy watches to separate collection, because all changes on the WatchAggregator watches
+        // shouldn't be propagated to listener watches immediately.
+        // WatchAggregator will be redeployed with new watches if needed instead.
+        final LinkedHashMap<Long, Watch> cpWatches = new LinkedHashMap<>(watches);
+
+        return new WatchListener() {
+
+            @Override public boolean onUpdate(@NotNull Iterable<WatchEvent> evts) {
+                var watchIt = cpWatches.entrySet().iterator();
+                Collection<Long> toCancel = new ArrayList<>();
+
+                while (watchIt.hasNext()) {
+                    Map.Entry<Long, WatchAggregator.Watch> entry = watchIt.next();
+                    WatchAggregator.Watch watch = entry.getValue();
+                    var filteredEvts = new ArrayList<WatchEvent>();
+
+                    for (WatchEvent evt : evts) {
+                        if (watch.keyCriterion().contains(evt.oldEntry().key()))
+                            filteredEvts.add(evt);
+                    }
+
+                    if (!filteredEvts.isEmpty()) {
+                        if (!watch.lsnr().onUpdate(filteredEvts)) {
+                            watchIt.remove();
+
+                            toCancel.add(entry.getKey());
+                        }
+                    }
+                }
+
+                // Cancel finished watches from the global watch map
+                // to prevent finished watches from redeploy.
+                if (!toCancel.isEmpty())
+                    cancelAll(toCancel);
+
+                var revision = 0L;
+                var entries = new ArrayList<IgniteBiTuple<Key, byte[]>>();
+                for (WatchEvent evt: evts) {
+                    revision = evt.newEntry().revision();
+
+                    entries.add(new IgniteBiTuple<>(evt.newEntry().key(), evt.newEntry().value()));
+                }
+
+                storeRevision.accept(entries, revision);
+
+                return true;
+            }
+
+            @Override public void onError(@NotNull Throwable e) {
+                watches.values().forEach(w -> w.lsnr().onError(e));
+            }
+        };
+    }
+
+    /**
+     * (key criterion, watch listener) container.
+     */
+    private static class Watch {
+        /** Key criterion. */
+        private final KeyCriterion keyCriterion;
+
+        /** Watch listener. */
+        private final WatchListener lsnr;
+
+        /** Creates the watch. */
+        private Watch(KeyCriterion keyCriterion, WatchListener lsnr) {
+            this.keyCriterion = keyCriterion;
+            this.lsnr = lsnr;
+        }
+
+        /**
+         * @return key criterion.
+         */
+        public KeyCriterion keyCriterion() {
+            return keyCriterion;
+        }
+
+        /**
+         * @return watch listener.
+         */
+        public WatchListener lsnr() {

Review comment:
       Method name abbreviations are forbidden

##########
File path: modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageManager.java
##########
@@ -62,11 +95,15 @@
     public MetaStorageManager(
         VaultManager vaultMgr,
         ClusterService clusterNetSvc,
-        Loza raftMgr
+        Loza raftMgr,
+        MetaStorageService metaStorageSvc
     ) {
         this.vaultMgr = vaultMgr;
         this.clusterNetSvc = clusterNetSvc;
         this.raftMgr = raftMgr;
+        this.metaStorageSvc = metaStorageSvc;
+        watchAggregator = new WatchAggregator();
+        igniteUuidFut = new CompletableFuture<>();

Review comment:
       ```suggestion
           deployFut = new CompletableFuture<>();
   ```

##########
File path: modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/watch/WatchAggregator.java
##########
@@ -0,0 +1,243 @@
+/*
+ * 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.metastorage.watch;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.BiConsumer;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.metastorage.common.Key;
+import org.apache.ignite.metastorage.common.WatchEvent;
+import org.apache.ignite.metastorage.common.WatchListener;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Needed to aggregate multiple watches to one aggregated watch.
+ * This approach needed to provide the following additional guarantees to watching mechanism:
+ * - watch events will be processed sequentially
+ * - watch events will be resolved in the order of watch registration
+ */
+public class WatchAggregator {
+    /**
+     * Watches' map must be synchronized because of changes from WatchListener in separate thread.
+     */
+    private final Map<Long, Watch> watches = Collections.synchronizedMap(new LinkedHashMap<>());
+
+    /** Simple auto increment id for internal watches. */
+    private long idCntr;
+
+    /**
+     * Adds new watch with simple exact criterion.
+     *
+     * @param key Key for watching.
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long add(Key key, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.ExactCriterion(key), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Adds new watch with filter by key prefix.
+     *
+     * @param key Prefix for key.
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long addPrefix(Key key, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.PrefixCriterion(key), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Adds new watch with filter by collection of keys.
+     *
+     * @param keys Collection of keys to listen.
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long add(Collection<Key> keys, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.CollectionCriterion(keys), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Adds new watch with filter by collection of keys.
+     *
+     * @param from Start key of range to listen.
+     * @param to End key of range (exclusively)..
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long add(Key from, Key to, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.RangeCriterion(from, to), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Cancel watch by id.
+     *
+     * @param id of watch to cancel.
+     */
+    public void cancel(long id) {
+        watches.remove(id);
+    }
+
+    /**
+     * Cancel multiple watches by ids.
+     *
+     * @param ids of watches to cancel.
+     */
+    public void cancelAll(Collection<Long> ids) {
+        watches.keySet().removeAll(ids);
+    }
+
+    /**
+     * Produce watch with aggregated key criterion and general watch listener dispatcher.
+     *
+     * @param revision start revision to listen event.
+     * @param saveRevisionAct action to commit keys-revision pair to persistent store for processed keys.
+     * @return result aggregated watch.
+     */
+    public Optional<AggregatedWatch> watch(long revision, BiConsumer<Collection<IgniteBiTuple<Key, byte[]>>, Long> saveRevisionAct) {
+        synchronized (watches) {
+            if (watches.isEmpty())
+                return Optional.empty();
+            else
+                return Optional.of(new AggregatedWatch(inferGeneralCriteria(), revision, watchListener(saveRevisionAct)));
+        }
+    }
+
+    /**
+     * Returns general criterion, which overlays all aggregated criteria.
+     *
+     * @return aggregated criterion.
+     */
+    // TODO: We can do it better than infer range always
+    private KeyCriterion inferGeneralCriteria() {
+        return new KeyCriterion.RangeCriterion(
+            watches.values().stream().map(w -> w.keyCriterion().toRange().getKey()).min(Key::compareTo).get(),
+            watches.values().stream().map(w -> w.keyCriterion().toRange().getValue()).max(Key::compareTo).get()
+        );
+    }
+
+    /**
+     * Produces the watch listener, which will dispatch events to appropriate watches.
+     *
+     * @param storeRevision action to commit keys-revision pair to persistent store for processed keys.
+     * @return watch listener, which will dispatch events to appropriate watches.
+     */
+    private WatchListener watchListener(BiConsumer<Collection<IgniteBiTuple<Key, byte[]>>, Long> storeRevision) {
+        // Copy watches to separate collection, because all changes on the WatchAggregator watches
+        // shouldn't be propagated to listener watches immediately.
+        // WatchAggregator will be redeployed with new watches if needed instead.
+        final LinkedHashMap<Long, Watch> cpWatches = new LinkedHashMap<>(watches);
+
+        return new WatchListener() {
+
+            @Override public boolean onUpdate(@NotNull Iterable<WatchEvent> evts) {
+                var watchIt = cpWatches.entrySet().iterator();
+                Collection<Long> toCancel = new ArrayList<>();
+
+                while (watchIt.hasNext()) {
+                    Map.Entry<Long, WatchAggregator.Watch> entry = watchIt.next();
+                    WatchAggregator.Watch watch = entry.getValue();
+                    var filteredEvts = new ArrayList<WatchEvent>();
+
+                    for (WatchEvent evt : evts) {
+                        if (watch.keyCriterion().contains(evt.oldEntry().key()))
+                            filteredEvts.add(evt);
+                    }
+
+                    if (!filteredEvts.isEmpty()) {
+                        if (!watch.lsnr().onUpdate(filteredEvts)) {
+                            watchIt.remove();
+
+                            toCancel.add(entry.getKey());
+                        }
+                    }
+                }
+
+                // Cancel finished watches from the global watch map
+                // to prevent finished watches from redeploy.
+                if (!toCancel.isEmpty())
+                    cancelAll(toCancel);

Review comment:
       How the aggregated watch is redelpoyed on some sub watches cancellation ?

##########
File path: modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/watch/AggregatedWatch.java
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.metastorage.watch;
+
+import org.apache.ignite.metastorage.common.WatchListener;
+
+/**
+ * Watch implementation with associated revision.
+ * Instance of this watch produced by {@link WatchAggregator}.
+ */
+public class AggregatedWatch {
+    /** Watch key criterion. */
+    private final KeyCriterion keyCriterion;
+
+    /** Aggregated watch listener. */
+    private final WatchListener lsnr;
+
+    /** Watch revision. */
+    private final long revision;
+
+    /**
+     * Creates the instance of aggregated watch.
+     *
+     * @param keyCriterion Aggregated key criterion.
+     * @param revision Aggregated revision.
+     * @param lsnr Aggregated listener.
+     */
+    public AggregatedWatch(KeyCriterion keyCriterion, long revision, WatchListener lsnr) {
+        this.keyCriterion = keyCriterion;
+        this.revision = revision;
+        this.lsnr = lsnr;
+    }
+
+    /**
+     * @return Key criterion.
+     */
+    public KeyCriterion keyCriterion() {
+        return keyCriterion;
+    }
+
+    /**
+     * @return Watch listener.
+     */
+    public WatchListener lsnr() {

Review comment:
       Method name abbreviations are forbidden

##########
File path: modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/watch/KeyCriterion.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.metastorage.watch;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.metastorage.common.Key;
+
+/**
+ * Filter for listen key's changes on metastore.
+ */
+public interface KeyCriterion {
+    /**
+     * Translates any type of key criterion to range of keys.
+     *
+     * @return Ignite tuple with first key as start of range and second as the end.
+     */
+    public IgniteBiTuple<Key, Key> toRange();

Review comment:
       This method produces unnecessary calculations on call. Better to have instead
   Key lower();
   Key upper();

##########
File path: modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/watch/WatchAggregator.java
##########
@@ -0,0 +1,243 @@
+/*
+ * 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.metastorage.watch;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.BiConsumer;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.metastorage.common.Key;
+import org.apache.ignite.metastorage.common.WatchEvent;
+import org.apache.ignite.metastorage.common.WatchListener;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Needed to aggregate multiple watches to one aggregated watch.
+ * This approach needed to provide the following additional guarantees to watching mechanism:
+ * - watch events will be processed sequentially
+ * - watch events will be resolved in the order of watch registration
+ */
+public class WatchAggregator {
+    /**
+     * Watches' map must be synchronized because of changes from WatchListener in separate thread.
+     */
+    private final Map<Long, Watch> watches = Collections.synchronizedMap(new LinkedHashMap<>());
+
+    /** Simple auto increment id for internal watches. */
+    private long idCntr;
+
+    /**
+     * Adds new watch with simple exact criterion.
+     *
+     * @param key Key for watching.
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long add(Key key, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.ExactCriterion(key), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Adds new watch with filter by key prefix.
+     *
+     * @param key Prefix for key.
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long addPrefix(Key key, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.PrefixCriterion(key), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Adds new watch with filter by collection of keys.
+     *
+     * @param keys Collection of keys to listen.
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long add(Collection<Key> keys, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.CollectionCriterion(keys), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Adds new watch with filter by collection of keys.
+     *
+     * @param from Start key of range to listen.
+     * @param to End key of range (exclusively)..
+     * @param lsnr Listener which will be executed on watch event.
+     * @return id of registered watch. Can be used for remove watch from later.
+     */
+    public long add(Key from, Key to, WatchListener lsnr) {
+        var watch = new Watch(new KeyCriterion.RangeCriterion(from, to), lsnr);
+        var id = idCntr++;
+        watches.put(id, watch);
+        return id;
+    }
+
+    /**
+     * Cancel watch by id.
+     *
+     * @param id of watch to cancel.
+     */
+    public void cancel(long id) {
+        watches.remove(id);
+    }
+
+    /**
+     * Cancel multiple watches by ids.
+     *
+     * @param ids of watches to cancel.
+     */
+    public void cancelAll(Collection<Long> ids) {
+        watches.keySet().removeAll(ids);
+    }
+
+    /**
+     * Produce watch with aggregated key criterion and general watch listener dispatcher.
+     *
+     * @param revision start revision to listen event.
+     * @param saveRevisionAct action to commit keys-revision pair to persistent store for processed keys.
+     * @return result aggregated watch.
+     */
+    public Optional<AggregatedWatch> watch(long revision, BiConsumer<Collection<IgniteBiTuple<Key, byte[]>>, Long> saveRevisionAct) {
+        synchronized (watches) {
+            if (watches.isEmpty())
+                return Optional.empty();
+            else
+                return Optional.of(new AggregatedWatch(inferGeneralCriteria(), revision, watchListener(saveRevisionAct)));
+        }
+    }
+
+    /**
+     * Returns general criterion, which overlays all aggregated criteria.
+     *
+     * @return aggregated criterion.
+     */
+    // TODO: We can do it better than infer range always

Review comment:
       A ticket is absent in TODO




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org