refactor:make sure to run views update in parallel
@@ -37,8 +37,10 @@ import com.orientechnologies.orient.core.sql.parser.OStatementCache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Locale;
+import java.util.Set;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.Callable;
@@ -79,6 +81,7 @@ public class ViewManager {
private final ConcurrentMap<String, Long> lastUpdateTimestampForView = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Long> lastChangePerClass = new ConcurrentHashMap<>();
+ private final Set<String> refreshing = Collections.synchronizedSet(new HashSet<>());
private volatile TimerTask timerTask;
private volatile Future<?> lastTask;
@@ -147,7 +150,7 @@ public class ViewManager {
});
}
};
- this.orientDB.scheduleOnce(timerTask, 1000);
+ this.orientDB.schedule(timerTask, 1000, 1000);
}
private void updateViews(ODatabaseDocumentInternal db) {
@@ -158,8 +161,7 @@ public class ViewManager {
if (view != null) {
updateView(view, db);
}
- // When the run is finished schedule the next run.
- schedule();
+
} catch (Exception e) {
OLogManager.instance().warn(this, "Failed to update views", e);
}
@@ -254,12 +256,19 @@ public class ViewManager {
if (!needsUpdateBasedOnWatchRules(view, db)) {
continue;
}
+ if (isViewRefreshing(view)) {
+ continue;
+ }
return db.getMetadata().getSchema().getView(view.getName());
}
return null;
}
+ private boolean isViewRefreshing(OView view) {
+ return this.refreshing.contains(view.getName());
+ }
+
private boolean isLiveUpdate(ODatabaseDocumentInternal db, OView view) {
return OViewConfig.UPDATE_STRATEGY_LIVE.equalsIgnoreCase(view.getUpdateStrategy());
}
@@ -318,6 +327,9 @@ public class ViewManager {
// backup is running handle rebuild the next run
return;
}
+ try {
+ refreshing.add(view.getName());
+
OLogManager.instance().info(this, "Starting refresh of view '%s'", view.getName());
lastUpdateTimestampForView.put(view.getName(), System.currentTimeMillis());
int cluster = db.addCluster(getNextClusterNameFor(view, db));
@@ -375,6 +387,9 @@ public class ViewManager {
cleanUnusedViewIndexes(db);
cleanUnusedViewClusters(db);
OLogManager.instance().info(this, "Finished refresh of view '%s'", view.getName());
+ } finally {
+ refreshing.remove(view.getName());
+ }
}
private void addItemToView(
