AMashenkov commented on code in PR #6649:
URL: https://github.com/apache/ignite-3/pull/6649#discussion_r2464999793


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/statistic/SqlStatisticManagerImpl.java:
##########
@@ -171,6 +135,72 @@ public void start() {
                 tableSizeMap.putIfAbsent(table.id(), DEFAULT_VALUE);
             }
         }
+
+        scheduler.scheduleAtFixedRate(this::update, INITIAL_DELAY, 
REFRESH_PERIOD, TimeUnit.MILLISECONDS);
+    }
+
+    private void update() {
+        if (!latestUpdateFut.get().isDone()) {
+            return;
+        }
+
+        Collection<InternalTable> tables = new 
ArrayList<>(tableSizeMap.size());
+
+        for (Map.Entry<Integer, ActualSize> ent : tableSizeMap.entrySet()) {
+            Integer tableId = ent.getKey();
+
+            if (droppedTables.contains(tableId)) {
+                continue;
+            }
+
+            TableViewInternal tableView = tableManager.cachedTable(tableId);
+
+            if (tableView == null) {
+                LOG.debug("No table found to update statistics [id={}].", 
ent.getKey());
+            } else {
+                tables.add(tableView.internalTable());
+            }
+        }
+
+        CompletableFuture<Void> updateResult = 
statSupplier.estimatedSizeWithLastUpdate(tables)
+                .handle((infos, err) -> {
+                    for (Map.Entry<Integer, PartitionModificationInfo> ent : 
infos.entrySet()) {
+                        int tableId = ent.getKey();
+                        PartitionModificationInfo info = ent.getValue();
+
+                        if (err != null) {
+                            LOG.debug("Failed to update table statistics for 
[tableId={}].", err, tableId);
+                        } else {
+                            ActualSize estimatedTableSize = new 
ActualSize(Math.max(info.getEstimatedSize(), DEFAULT_TABLE_SIZE),
+                                    info.lastModificationCounter());
+                            ActualSize prevSize = tableSizeMap.get(tableId);
+                            // the table can be concurrently dropped and we 
shouldn't put new value in this case.
+                            tableSizeMap.compute(tableId, (k, v) -> {
+                                if (v == null) {
+                                    return estimatedTableSize;
+                                }
+
+                                // check for stale update
+                                if (v.modificationCounter() > 
info.lastModificationCounter()) {
+                                    return v;
+                                }
+
+                                return estimatedTableSize;
+                            });
+
+                            if (!estimatedTableSize.equals(prevSize)) {
+                                StatisticUpdatesSupplier supplier = 
changesSupplier.get();
+                                if (supplier != null) {
+                                    supplier.accept(tableId);
+                                }

Review Comment:
   Still not clear what's happening here.
   
   Maybe, StatisticUpdatesNotifier can be EventProducer...
   and PrepareService can subscribe to the event.
   With this approach, the methods `get()` and `accept()` would become 
`listeners.notify()`.



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