Skip to content

Commit

Permalink
fix: avoid to re-index new cluster when swapping the cluster in views.
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Feb 2, 2023
1 parent 3743c34 commit 2b9e783
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public boolean registerLiveUpdateFor(ODatabaseSession db, String viewName) {
OView view = db.getMetadata().getSchema().getView(viewName);
viewsExist = true;
boolean registered = false;
if (view.getUpdateStrategy() != null
if (view != null
&& view.getUpdateStrategy() != null
&& view.getUpdateStrategy().equalsIgnoreCase(OViewConfig.UPDATE_STRATEGY_LIVE)) {
db.live(view.getQuery(), new ViewUpdateListener(view.getName()));
registered = true;
Expand Down Expand Up @@ -199,7 +200,6 @@ public void cleanUnusedViewClusters(ODatabaseDocument db) {
}
}
for (Integer cluster : clusters) {
System.out.println("dropping cluster " + cluster);
db.dropCluster(cluster);
viewCluserVisitors.remove(cluster);
oldClustersPerViews.remove(cluster);
Expand Down Expand Up @@ -329,23 +329,22 @@ public void updateViewInternal(OView view, ODatabaseDocumentInternal db) {
// backup is running handle rebuild the next run
return;
}
String viewName = view.getName();
try {
synchronized (this) {
if (refreshing.contains(view.getName())) {
if (refreshing.contains(viewName)) {
return;
}
refreshing.add(view.getName());
refreshing.add(viewName);
}

OLogManager.instance().info(this, "Starting refresh of view '%s'", view.getName());
lastUpdateTimestampForView.put(view.getName(), System.currentTimeMillis());
OLogManager.instance().info(this, "Starting refresh of view '%s'", viewName);
lastUpdateTimestampForView.put(viewName, System.currentTimeMillis());
String clusterName = createNextClusterNameFor(view, db);
int cluster = db.getClusterIdByName(clusterName);

String viewName = view.getName();
String query = view.getQuery();
String originRidField = view.getOriginRidField();

List<OIndex> indexes = createNewIndexesForView(view, cluster, db);

try {
Expand All @@ -355,9 +354,10 @@ public void updateViewInternal(OView view, ODatabaseDocumentInternal db) {
db.getMetadata().getIndexManagerInternal().dropIndex(db, index.getName());
}
db.dropCluster(cluster);
throw e;
}

view = db.getMetadata().getSchema().getView(view.getName());
view = db.getMetadata().getSchema().getView(viewName);
if (view == null) {
// the view was dropped in the meantime
clustersToDrop.add(cluster);
Expand All @@ -370,15 +370,15 @@ public void updateViewInternal(OView view, ODatabaseDocumentInternal db) {
.warn(
this,
"Replacing for view '%s' clusters '%s' with '%s'",
view.getName(),
viewName,
Arrays.stream(oldMetadata.getClusters())
.mapToObj((i) -> i + " => " + db.getClusterNameById(i))
.collect(Collectors.toList())
.toString(),
cluster + " => " + db.getClusterNameById(cluster));
for (int i : oldMetadata.getClusters()) {
clustersToDrop.add(i);
oldClustersPerViews.put(i, view.getName());
oldClustersPerViews.put(i, viewName);
}

oldMetadata
Expand All @@ -389,9 +389,9 @@ public void updateViewInternal(OView view, ODatabaseDocumentInternal db) {
});
cleanUnusedViewIndexes(db);
cleanUnusedViewClusters(db);
OLogManager.instance().info(this, "Finished refresh of view '%s'", view.getName());
OLogManager.instance().info(this, "Finished refresh of view '%s'", viewName);
} finally {
refreshing.remove(view.getName());
refreshing.remove(viewName);
}
}

Expand All @@ -410,6 +410,7 @@ private void fillView(
addItemToView(item, db, originRidField, viewName, clusterName, indexes);
if (iterationCount % 100 == 0) {
db.commit();
db.begin();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
*/
package com.orientechnologies.orient.core.exception;

public class OTransactionException extends OCoreException {
import com.orientechnologies.common.exception.OHighLevelException;

public class OTransactionException extends OCoreException implements OHighLevelException {

private static final long serialVersionUID = 2347493191705052402L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,18 @@ protected void addPolymorphicClusterId(int clusterId) {
}
}

protected void onlyAddPolymorphicClusterId(int clusterId) {
if (Arrays.binarySearch(polymorphicClusterIds, clusterId) >= 0) return;

polymorphicClusterIds = OArrays.copyOf(polymorphicClusterIds, polymorphicClusterIds.length + 1);
polymorphicClusterIds[polymorphicClusterIds.length - 1] = clusterId;
Arrays.sort(polymorphicClusterIds);

for (OClassImpl superClass : superClasses) {
superClass.onlyAddPolymorphicClusterId(clusterId);
}
}

protected abstract OProperty addProperty(
final String propertyName,
final OType type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected OClass addClusterIdInternal(ODatabaseDocumentInternal database, final
clusterIds[clusterIds.length - 1] = clusterId;
Arrays.sort(clusterIds);

addPolymorphicClusterId(clusterId);
onlyAddPolymorphicClusterId(clusterId);

if (defaultClusterId == NOT_EXISTENT_CLUSTER_ID) defaultClusterId = clusterId;

Expand Down

0 comments on commit 2b9e783

Please sign in to comment.