alievmirza commented on a change in pull request #114:
URL: https://github.com/apache/ignite-3/pull/114#discussion_r628361446



##########
File path: 
modules/runner/src/main/java/org/apache/ignite/internal/storage/DistributedConfigurationStorage.java
##########
@@ -49,52 +72,154 @@ public DistributedConfigurationStorage(MetaStorageManager 
metaStorageMgr) {
         this.metaStorageMgr = metaStorageMgr;
     }
 
-    /** Map to store values. */
-    private Map<String, Serializable> map = new ConcurrentHashMap<>();
-
     /** Change listeners. */
     private List<ConfigurationStorageListener> listeners = new 
CopyOnWriteArrayList<>();
 
-    /** Storage version. */
-    private AtomicLong version = new AtomicLong(0);
+    /** Storage version. It stores actual metastorage revision, that is 
applied to configuration manager. */
+    private AtomicLong version = new AtomicLong(0L);
 
     /** {@inheritDoc} */
     @Override public synchronized Data readAll() throws StorageException {
-        return new Data(new HashMap<>(map), version.get(), 0);
+
+        Cursor<Entry> cur = metaStorageMgr.rangeWithAppliedRevision(new 
Key(DISTRIBUTED_PREFIX + "."),

Review comment:
       added todo with ticket

##########
File path: 
modules/runner/src/main/java/org/apache/ignite/internal/storage/DistributedConfigurationStorage.java
##########
@@ -49,52 +72,154 @@ public DistributedConfigurationStorage(MetaStorageManager 
metaStorageMgr) {
         this.metaStorageMgr = metaStorageMgr;
     }
 
-    /** Map to store values. */
-    private Map<String, Serializable> map = new ConcurrentHashMap<>();
-
     /** Change listeners. */
     private List<ConfigurationStorageListener> listeners = new 
CopyOnWriteArrayList<>();
 
-    /** Storage version. */
-    private AtomicLong version = new AtomicLong(0);
+    /** Storage version. It stores actual metastorage revision, that is 
applied to configuration manager. */
+    private AtomicLong version = new AtomicLong(0L);
 
     /** {@inheritDoc} */
     @Override public synchronized Data readAll() throws StorageException {
-        return new Data(new HashMap<>(map), version.get(), 0);
+
+        Cursor<Entry> cur = metaStorageMgr.rangeWithAppliedRevision(new 
Key(DISTRIBUTED_PREFIX + "."),
+            new Key(DISTRIBUTED_PREFIX + (char)('.' + 1)));
+
+        HashMap<String, Serializable> data = new HashMap<>();
+
+        long maxRevision = 0L;
+
+        Entry entryForMasterKey = null;
+
+        for (Entry entry : cur) {
+            if (!entry.key().equals(masterKey)) {
+                
data.put(entry.key().toString().replaceFirst(DISTRIBUTED_PREFIX + ".", ""),
+                    (Serializable)ByteUtils.fromBytes(entry.value()));
+
+                // Move to stream
+                if (maxRevision < entry.revision())
+                    maxRevision = entry.revision();
+            } else
+                entryForMasterKey = entry;
+        }
+
+        if (!data.isEmpty()) {
+            assert entryForMasterKey != null;
+
+            assert maxRevision == entryForMasterKey.revision();
+
+            assert maxRevision >= version.get();
+
+            return new Data(data, maxRevision);
+        }
+
+        return new Data(data, version.get());
     }
 
     /** {@inheritDoc} */
-    @Override public synchronized CompletableFuture<Boolean> write(Map<String, 
Serializable> newValues, long version) {
-        if (version != this.version.get())
+    @Override public synchronized CompletableFuture<Boolean> write(Map<String, 
Serializable> newValues, long sentVersion) {
+        assert sentVersion <= version.get();
+
+        if (sentVersion != version.get())
+            // This means that sentVersion is less than version and other node 
has already updated configuration and
+            // write should be retried. Actual version will be set when watch 
and corresponding configuration listener
+            // updates configuration and notifyApplied is triggered afterwards.
             return CompletableFuture.completedFuture(false);
 
+        HashSet<Operation> operations = new HashSet<>();
+
+        HashSet<Operation> failures = new HashSet<>();
+
         for (Map.Entry<String, Serializable> entry : newValues.entrySet()) {
+            Key key = new Key(DISTRIBUTED_PREFIX + "." + entry.getKey());
+
             if (entry.getValue() != null)
-                map.put(entry.getKey(), entry.getValue());
+                // TODO: investigate overhead when deserialize int, long, 
double, boolean, string, arrays of above
+                operations.add(Operations.put(key, 
ByteUtils.toBytes(entry.getValue())));
             else
-                map.remove(entry.getKey());
-        }
+                operations.add(Operations.remove(key));
 
-        this.version.incrementAndGet();
+            failures.add(Operations.noop());
+        }
 
-        listeners.forEach(listener -> listener.onEntriesChanged(new 
Data(newValues, this.version.get(), 0)));
+        operations.add(Operations.put(masterKey, 
ByteUtils.longToBytes(sentVersion)));
 
-        return CompletableFuture.completedFuture(true);
+        return 
metaStorageMgr.invoke(Conditions.key(masterKey).revision().eq(version.get()), 
operations, failures);
     }
 
     /** {@inheritDoc} */
-    @Override public void addListener(ConfigurationStorageListener listener) {
+    @Override public synchronized void 
addListener(ConfigurationStorageListener listener) {
         listeners.add(listener);
+
+        if (watchId == null) {
+            watchId = metaStorageMgr.registerWatchByPrefix(masterKey, new 
WatchListener() {

Review comment:
       added todo with ticket

##########
File path: 
modules/runner/src/main/java/org/apache/ignite/internal/storage/DistributedConfigurationStorage.java
##########
@@ -49,52 +72,154 @@ public DistributedConfigurationStorage(MetaStorageManager 
metaStorageMgr) {
         this.metaStorageMgr = metaStorageMgr;
     }
 
-    /** Map to store values. */
-    private Map<String, Serializable> map = new ConcurrentHashMap<>();
-
     /** Change listeners. */
     private List<ConfigurationStorageListener> listeners = new 
CopyOnWriteArrayList<>();
 
-    /** Storage version. */
-    private AtomicLong version = new AtomicLong(0);
+    /** Storage version. It stores actual metastorage revision, that is 
applied to configuration manager. */
+    private AtomicLong version = new AtomicLong(0L);
 
     /** {@inheritDoc} */
     @Override public synchronized Data readAll() throws StorageException {
-        return new Data(new HashMap<>(map), version.get(), 0);
+
+        Cursor<Entry> cur = metaStorageMgr.rangeWithAppliedRevision(new 
Key(DISTRIBUTED_PREFIX + "."),
+            new Key(DISTRIBUTED_PREFIX + (char)('.' + 1)));
+
+        HashMap<String, Serializable> data = new HashMap<>();
+
+        long maxRevision = 0L;
+
+        Entry entryForMasterKey = null;
+
+        for (Entry entry : cur) {
+            if (!entry.key().equals(masterKey)) {
+                
data.put(entry.key().toString().replaceFirst(DISTRIBUTED_PREFIX + ".", ""),
+                    (Serializable)ByteUtils.fromBytes(entry.value()));
+
+                // Move to stream
+                if (maxRevision < entry.revision())
+                    maxRevision = entry.revision();
+            } else
+                entryForMasterKey = entry;
+        }
+
+        if (!data.isEmpty()) {
+            assert entryForMasterKey != null;
+
+            assert maxRevision == entryForMasterKey.revision();
+
+            assert maxRevision >= version.get();
+
+            return new Data(data, maxRevision);
+        }
+
+        return new Data(data, version.get());
     }
 
     /** {@inheritDoc} */
-    @Override public synchronized CompletableFuture<Boolean> write(Map<String, 
Serializable> newValues, long version) {
-        if (version != this.version.get())
+    @Override public synchronized CompletableFuture<Boolean> write(Map<String, 
Serializable> newValues, long sentVersion) {
+        assert sentVersion <= version.get();
+
+        if (sentVersion != version.get())
+            // This means that sentVersion is less than version and other node 
has already updated configuration and
+            // write should be retried. Actual version will be set when watch 
and corresponding configuration listener
+            // updates configuration and notifyApplied is triggered afterwards.
             return CompletableFuture.completedFuture(false);
 
+        HashSet<Operation> operations = new HashSet<>();
+
+        HashSet<Operation> failures = new HashSet<>();
+
         for (Map.Entry<String, Serializable> entry : newValues.entrySet()) {
+            Key key = new Key(DISTRIBUTED_PREFIX + "." + entry.getKey());
+
             if (entry.getValue() != null)
-                map.put(entry.getKey(), entry.getValue());
+                // TODO: investigate overhead when deserialize int, long, 
double, boolean, string, arrays of above
+                operations.add(Operations.put(key, 
ByteUtils.toBytes(entry.getValue())));
             else
-                map.remove(entry.getKey());
-        }
+                operations.add(Operations.remove(key));
 
-        this.version.incrementAndGet();
+            failures.add(Operations.noop());
+        }
 
-        listeners.forEach(listener -> listener.onEntriesChanged(new 
Data(newValues, this.version.get(), 0)));
+        operations.add(Operations.put(masterKey, 
ByteUtils.longToBytes(sentVersion)));
 
-        return CompletableFuture.completedFuture(true);
+        return 
metaStorageMgr.invoke(Conditions.key(masterKey).revision().eq(version.get()), 
operations, failures);
     }
 
     /** {@inheritDoc} */
-    @Override public void addListener(ConfigurationStorageListener listener) {
+    @Override public synchronized void 
addListener(ConfigurationStorageListener listener) {
         listeners.add(listener);
+
+        if (watchId == null) {
+            watchId = metaStorageMgr.registerWatchByPrefix(masterKey, new 
WatchListener() {
+                @Override public boolean onUpdate(@NotNull 
Iterable<WatchEvent> events) {
+                    HashMap<String, Serializable> data = new HashMap<>();
+
+                    long maxRevision = 0L;
+
+                    Entry entryForMasterKey = null;
+
+                    for (WatchEvent event : events) {
+                        Entry e = event.newEntry();
+
+                        if (!e.key().equals(masterKey)) {
+                            
data.put(e.key().toString().replaceFirst(DISTRIBUTED_PREFIX + ".", ""),
+                                (Serializable)ByteUtils.fromBytes(e.value()));
+
+                            if (maxRevision < e.revision())
+                                maxRevision = e.revision();
+                        } else
+                            entryForMasterKey = e;
+                    }
+
+                    // Contract of metastorage ensures that all updates of one 
revision will come in one batch.
+                    // Also masterKey should be updated every time when we 
update cfg.
+                    // That means that masterKey update must be included in 
the batch.
+                    assert entryForMasterKey != null;
+
+                    assert maxRevision == entryForMasterKey.revision();
+
+                    assert maxRevision >= version.get();
+
+                    long finalMaxRevision = maxRevision;
+
+                    listeners.forEach(listener -> 
listener.onEntriesChanged(new Data(data, finalMaxRevision)));
+
+                    return true;
+                }
+
+                @Override public void onError(@NotNull Throwable e) {
+                    LOG.error("Metastorage listener issue", e);

Review comment:
       added todo with ticket 

##########
File path: 
modules/runner/src/main/java/org/apache/ignite/internal/storage/LocalConfigurationStorage.java
##########
@@ -49,47 +70,122 @@ public LocalConfigurationStorage(VaultManager vaultMgr) {
         this.vaultMgr = vaultMgr;
     }
 
-    /** Map to store values. */
-    private Map<String, Serializable> map = new ConcurrentHashMap<>();
-
     /** Change listeners. */
     private List<ConfigurationStorageListener> listeners = new 
CopyOnWriteArrayList<>();
 
     /** Storage version. */
-    private AtomicLong version = new AtomicLong(0);
+    private AtomicLong ver = new AtomicLong(0);
+
+    /** Start key in range for searching local configuration keys. */
+    private static final ByteArray locKeysStartRange = 
ByteArray.fromString(LOC_PREFIX + ".");

Review comment:
       done

##########
File path: 
modules/runner/src/main/java/org/apache/ignite/internal/storage/LocalConfigurationStorage.java
##########
@@ -49,47 +70,122 @@ public LocalConfigurationStorage(VaultManager vaultMgr) {
         this.vaultMgr = vaultMgr;
     }
 
-    /** Map to store values. */
-    private Map<String, Serializable> map = new ConcurrentHashMap<>();
-
     /** Change listeners. */
     private List<ConfigurationStorageListener> listeners = new 
CopyOnWriteArrayList<>();
 
     /** Storage version. */
-    private AtomicLong version = new AtomicLong(0);
+    private AtomicLong ver = new AtomicLong(0);
+
+    /** Start key in range for searching local configuration keys. */
+    private static final ByteArray locKeysStartRange = 
ByteArray.fromString(LOC_PREFIX + ".");
+
+    /** End key in range for searching local configuration keys. */
+    private static final ByteArray locKeysEndRange = 
ByteArray.fromString(LOC_PREFIX + (char)('.' + 1));
 
     /** {@inheritDoc} */
     @Override public synchronized Data readAll() throws StorageException {
-        return new Data(new HashMap<>(map), version.get(), 0);
+        Iterator<Entry> iter =
+            vaultMgr.range(locKeysStartRange, locKeysEndRange);
+
+        HashMap<String, Serializable> data = new HashMap<>();
+
+        while (iter.hasNext()) {
+            Entry val = iter.next();
+
+            data.put(val.key().toString().replaceFirst(LOC_PREFIX + ".", ""),

Review comment:
       fixed

##########
File path: 
modules/runner/src/main/java/org/apache/ignite/internal/storage/LocalConfigurationStorage.java
##########
@@ -49,47 +70,122 @@ public LocalConfigurationStorage(VaultManager vaultMgr) {
         this.vaultMgr = vaultMgr;
     }
 
-    /** Map to store values. */
-    private Map<String, Serializable> map = new ConcurrentHashMap<>();
-
     /** Change listeners. */
     private List<ConfigurationStorageListener> listeners = new 
CopyOnWriteArrayList<>();
 
     /** Storage version. */
-    private AtomicLong version = new AtomicLong(0);
+    private AtomicLong ver = new AtomicLong(0);
+
+    /** Start key in range for searching local configuration keys. */
+    private static final ByteArray locKeysStartRange = 
ByteArray.fromString(LOC_PREFIX + ".");
+
+    /** End key in range for searching local configuration keys. */
+    private static final ByteArray locKeysEndRange = 
ByteArray.fromString(LOC_PREFIX + (char)('.' + 1));
 
     /** {@inheritDoc} */
     @Override public synchronized Data readAll() throws StorageException {
-        return new Data(new HashMap<>(map), version.get(), 0);
+        Iterator<Entry> iter =
+            vaultMgr.range(locKeysStartRange, locKeysEndRange);
+
+        HashMap<String, Serializable> data = new HashMap<>();
+
+        while (iter.hasNext()) {
+            Entry val = iter.next();
+
+            data.put(val.key().toString().replaceFirst(LOC_PREFIX + ".", ""),
+                (Serializable)ByteUtils.fromBytes(val.value()));
+        }
+
+        // TODO: Need to restore version from pds when restart will be 
developed
+        return new Data(data, ver.get());
     }
 
     /** {@inheritDoc} */
-    @Override public synchronized CompletableFuture<Boolean> write(Map<String, 
Serializable> newValues, long version) {
-        if (version != this.version.get())
+    @Override public synchronized CompletableFuture<Boolean> write(Map<String, 
Serializable> newValues, long sentVersion) {
+        if (sentVersion != ver.get())
             return CompletableFuture.completedFuture(false);
 
+        CompletableFuture[] futs = new CompletableFuture[newValues.size()];
+
+        int i = 0;
+
+        latch = new CountDownLatch(newValues.size());
+
         for (Map.Entry<String, Serializable> entry : newValues.entrySet()) {
+            ByteArray key = ByteArray.fromString(LOC_PREFIX + "." + 
entry.getKey());
+
             if (entry.getValue() != null)
-                map.put(entry.getKey(), entry.getValue());
+                futs[i++] = vaultMgr.put(key, 
ByteUtils.toBytes(entry.getValue()));
             else
-                map.remove(entry.getKey());
+                futs[i++] = vaultMgr.remove(key);
         }
 
-        this.version.incrementAndGet();
+        try {
+            CompletableFuture.allOf(futs).get();
+
+            latch.await();
+
+            for (Map.Entry<String, Serializable> entry : newValues.entrySet()) 
{
+                ByteArray key = ByteArray.fromString(LOC_PREFIX + "." + 
entry.getKey());
+
+                Entry e = vaultMgr.get(key).get();

Review comment:
       this code was removed at all

##########
File path: 
modules/runner/src/main/java/org/apache/ignite/internal/storage/LocalConfigurationStorage.java
##########
@@ -49,47 +70,122 @@ public LocalConfigurationStorage(VaultManager vaultMgr) {
         this.vaultMgr = vaultMgr;
     }
 
-    /** Map to store values. */
-    private Map<String, Serializable> map = new ConcurrentHashMap<>();
-
     /** Change listeners. */
     private List<ConfigurationStorageListener> listeners = new 
CopyOnWriteArrayList<>();
 
     /** Storage version. */
-    private AtomicLong version = new AtomicLong(0);
+    private AtomicLong ver = new AtomicLong(0);
+
+    /** Start key in range for searching local configuration keys. */
+    private static final ByteArray locKeysStartRange = 
ByteArray.fromString(LOC_PREFIX + ".");
+
+    /** End key in range for searching local configuration keys. */
+    private static final ByteArray locKeysEndRange = 
ByteArray.fromString(LOC_PREFIX + (char)('.' + 1));
 
     /** {@inheritDoc} */
     @Override public synchronized Data readAll() throws StorageException {
-        return new Data(new HashMap<>(map), version.get(), 0);
+        Iterator<Entry> iter =
+            vaultMgr.range(locKeysStartRange, locKeysEndRange);
+
+        HashMap<String, Serializable> data = new HashMap<>();
+
+        while (iter.hasNext()) {
+            Entry val = iter.next();
+
+            data.put(val.key().toString().replaceFirst(LOC_PREFIX + ".", ""),
+                (Serializable)ByteUtils.fromBytes(val.value()));
+        }
+
+        // TODO: Need to restore version from pds when restart will be 
developed
+        return new Data(data, ver.get());
     }
 
     /** {@inheritDoc} */
-    @Override public synchronized CompletableFuture<Boolean> write(Map<String, 
Serializable> newValues, long version) {
-        if (version != this.version.get())
+    @Override public synchronized CompletableFuture<Boolean> write(Map<String, 
Serializable> newValues, long sentVersion) {
+        if (sentVersion != ver.get())
             return CompletableFuture.completedFuture(false);
 
+        CompletableFuture[] futs = new CompletableFuture[newValues.size()];
+
+        int i = 0;
+
+        latch = new CountDownLatch(newValues.size());
+
         for (Map.Entry<String, Serializable> entry : newValues.entrySet()) {
+            ByteArray key = ByteArray.fromString(LOC_PREFIX + "." + 
entry.getKey());
+
             if (entry.getValue() != null)
-                map.put(entry.getKey(), entry.getValue());
+                futs[i++] = vaultMgr.put(key, 
ByteUtils.toBytes(entry.getValue()));
             else
-                map.remove(entry.getKey());
+                futs[i++] = vaultMgr.remove(key);
         }
 
-        this.version.incrementAndGet();
+        try {
+            CompletableFuture.allOf(futs).get();
+
+            latch.await();
+
+            for (Map.Entry<String, Serializable> entry : newValues.entrySet()) 
{
+                ByteArray key = ByteArray.fromString(LOC_PREFIX + "." + 
entry.getKey());
+
+                Entry e = vaultMgr.get(key).get();
 
-        listeners.forEach(listener -> listener.onEntriesChanged(new 
Data(newValues, this.version.get(), 0)));
+                if (Arrays.equals(e.value(), 
ByteUtils.toBytes(entry.getValue())))
+                    // value by some key was overwritten, that means that 
changes not
+                    // from LocalConfigurationStorage.write overlapped with 
current changes, so write should be retried.
+                    return CompletableFuture.completedFuture(false);
+            }
+        }
+        catch (InterruptedException | ExecutionException e) {
+            return CompletableFuture.failedFuture(e);
+        }
 
         return CompletableFuture.completedFuture(true);
     }
 
     /** {@inheritDoc} */
-    @Override public void addListener(ConfigurationStorageListener listener) {
-        listeners.add(listener);
+    @Override public void addListener(ConfigurationStorageListener lsnr) {
+        listeners.add(lsnr);
+
+        if (watchId == 0) {
+            try {
+                watchId = vaultMgr.watch(new VaultWatch(locKeysStartRange, 
locKeysEndRange, new VaultListener() {
+                    // In the current implementation entries always contains 
only one entry
+                    @Override public boolean onUpdate(@NotNull Iterable<Entry> 
entries) {
+                        HashMap<String, Serializable> data = new HashMap<>();
+
+                        for (Entry e : entries) {
+                            
data.put(e.key().toString().substring(locKeysEndRange.toString().length()),
+                                (Serializable)ByteUtils.fromBytes(e.value()));
+                        }
+
+                        listeners.forEach(listener -> 
listener.onEntriesChanged(new Data(data, ver.incrementAndGet())));
+
+                        latch.countDown();
+
+                        return true;
+                    }
+
+                    @Override public void onError(@NotNull Throwable e) {
+                        LOG.error("Vault listener issue", e);

Review comment:
       this code was removed at all




-- 
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:
[email protected]


Reply via email to