Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not all edges are deleted when their connected vertices are deleted #1451

Closed
countfloyd opened this issue Feb 2, 2024 · 4 comments
Closed
Assignees
Labels
bug Something isn't working fixed
Milestone

Comments

@countfloyd
Copy link

ArcadeDB Version:

23.12.2

OS and JDK Version:

macos 14.3, zulu 21.0.2 JDK
<OS and JDK brand and version here. You can find it in the 2nd line of the server output>

Expected behavior

Running the enclosed Java file should result in '# of edges: 0' message

Actual behavior

The number of edges changes over different runs, varying from 0 to sometimes 2 or 3

Steps to reproduce

Run the following Java test class. Look at the # of edges output. Run repeatedly to see the number change.

package arcadedb.test;

import com.arcadedb.database.Database;
import com.arcadedb.database.DatabaseFactory;
import com.arcadedb.graph.Vertex;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestVertexDelete {
    public static class DeleteOnClose implements AutoCloseable {
        public DeleteOnClose(Path p) {
            System.out.println("Creating temporary directory " + p);
            this.p = p;
        }

        public Path getPath() {
            return p;
        }

        public void close() {
            System.out.println("Deleting temporary directory " + p);
            deleteDirectory(p.toFile());
        }

        private void deleteDirectory(File directory) {
            if (directory.exists()) {
                File[] files = directory.listFiles();
                if (files != null) {
                    for (File file : files) {
                        if (file.isDirectory()) {
                            deleteDirectory(file);
                        } else {
                            if (!file.delete()) System.out.println("Could not delete file " + file);
                        }
                    }
                }
                if (!directory.delete()) System.out.println("Could not delete directory " + directory);
            }
        }

        private final Path p;
    }

    public static void main(String[] args) throws IOException {
        try (var td = createTemporaryDirectory();
             var df = new DatabaseFactory(td.getPath().toString());
             var db = df.create()
        ) {
            createSchema(db);
            db.transaction(() -> {
                var vlist = createTree(db);
                deleteTree(vlist);
            });
            db.transaction(() -> {
                var v1c = db.countType("v1", false);
                var pc = db.countType("hasParent", false);
                // This always prints 0
                System.out.println("# of vertices: " + v1c);
                // This prints a different number each time you run it (from 0 to 3)
                // Should be 0
                System.out.println("# of edges: " + pc);
            });
        }
    }

    private static DeleteOnClose createTemporaryDirectory() throws IOException {
        return new DeleteOnClose(Files.createTempDirectory("arcadedb"));
    }

    private static void createSchema(Database db) {
        db.transaction(() -> {
            db.getSchema().createVertexType("v1");
            db.getSchema().createEdgeType("hasParent");
        });
    }

    // create tree of vertices all connected by edges
    private static List<Vertex> createTree(Database db) {
        var p1 = db.newVertex("v1").save();
        var p11 = db.newVertex("v1").save();
        p11.newEdge("hasParent", p1, true).save();
        var p12 = db.newVertex("v1").save();
        p12.newEdge("hasParent", p1, true).save();
        var n1 = db.newVertex("v1").save();
        n1.newEdge("hasParent", p1, true).save();
        var n2 = db.newVertex("v1").save();
        n2.newEdge("hasParent", p11, true).save();
        var n3 = db.newVertex("v1").save();
        n3.newEdge("hasParent", p11, true).save();
        var n4 = db.newVertex("v1").save();
        n4.newEdge("hasParent", p12, true).save();

        return List.of(p1, p11, p12, n1, n2, n3, n4);
    }

    private static void deleteTree(List<Vertex> vs) {
        var mvs = new ArrayList<>(vs);
        // change order of vertices before deleting
        Collections.shuffle(mvs);
        mvs.forEach((v) -> {
            System.out.println("deleting " + v);
            v.delete();
        });
    }
}
@lvca
Copy link
Contributor

lvca commented Feb 3, 2024

Reproduced, thanks!

I noticed if I separate the creation of the tree and the deletion in 2 separate transactions, always works.

@lvca lvca self-assigned this Feb 3, 2024
@lvca lvca added this to the 24.1.1 milestone Feb 3, 2024
@lvca lvca added the in progress The team is actively working on this issue label Feb 3, 2024
@countfloyd
Copy link
Author

Excellent! Thanks for your help on this.

@lvca
Copy link
Contributor

lvca commented Feb 4, 2024

Still digging into this issue, hard to reproduce in debug. It's fundamental to fix this ASAP because it could highlight an internal problem we need to fix. Thanks for helping @countfloyd with the test case.

@lvca lvca modified the milestones: 24.1.1, 24.2.1 Feb 5, 2024
lvca added a commit that referenced this issue Feb 8, 2024
@lvca lvca added bug Something isn't working fixed and removed in progress The team is actively working on this issue labels Feb 8, 2024
@lvca
Copy link
Contributor

lvca commented Feb 8, 2024

I was able to find the bug. I haven't noticed the random shuffle on delete at first. Once I identified the sequence(s) that generated the issue I was able to find the problem. The deletion from vertices created a cascading effect to the edges, but the internal iterators skipped some of the edges because moved forward by the cascading effect.

As a result of the fix, the delete of graph elements now it's also faster.

@lvca lvca closed this as completed Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants