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

Fix the issue that the order of subgraph does not idempotent #31

Merged
merged 1 commit into from
Sep 11, 2020

Conversation

goccy
Copy link
Owner

@goccy goccy commented Sep 11, 2020

Thank you for sharing the reproduction code and some information @k1LoW !
Maybe, I resolved this issue :)

Issue

The order of subgraph does not idempotent .
Because it use dttree for searching subgraph ( ordered set ) .
It depends on keys of dictionary but keys are not sorted .

Probably, Graphviz also have same problem ( FYI: https://gitlab.com/graphviz/graphviz/-/issues/1614 )

Reproduction code

package main

import (
	"bytes"
	"fmt"
	"io"

	"github.com/goccy/go-graphviz"
	"github.com/google/go-cmp/cmp"
)

const dot = `digraph test {
  graph [layout=dot];
  subgraph "cluster_a" {
    "node_1";
  }
  subgraph "cluster_b" {
    "node_2";
  }
}
`

func main() {
	diffc := 0
	nodiffc := 0
	for i := 0; i < 30; i++ {
		g := &Gviz{}
		buf := []byte(dot)
		a := &bytes.Buffer{}
		b := &bytes.Buffer{}
		if err := g.render(a, buf); err != nil {
			panic(err)
		}
		if err := g.render(b, buf); err != nil {
			panic(err)
		}

		if diff := cmp.Diff(a.Bytes(), b.Bytes(), nil); diff != "" {
			diffc++
			continue
		}
		nodiffc++
	}
	fmt.Printf("diff exist:%d / no diff:%d\n", diffc, nodiffc)
}

type Gviz struct {
}

func (g *Gviz) render(wr io.Writer, b []byte) (e error) {
	format := "svg"
	gviz := graphviz.New()
	graph, err := graphviz.ParseBytes(b)
	if err != nil {
		return err
	}
	defer func() {
		if err := gviz.Close(); err != nil {
			e = err
		}
		if err := graph.Close(); err != nil {
			e = err
		}
	}()
	if err := gviz.Render(graph, graphviz.Format(format), wr); err != nil {
		return err
	}
	return nil
}

Solution

Therefor, I fix this issue by using linked list for searching subgraph ( Dtlist )

@goccy goccy force-pushed the feature/fix-subgraph-not-idempotent branch from e04de67 to 84c4857 Compare September 11, 2020 07:18
@k1LoW
Copy link
Sponsor Contributor

k1LoW commented Sep 11, 2020

@goccy

GREAT!!!!!!!!!!! WONDERFULLLLLLLLL!!!! 🚀 🚀

I was able to get a deterministic output in my environment, too !!

Thank you!!!

@goccy goccy merged commit aabab27 into master Sep 11, 2020
@goccy goccy deleted the feature/fix-subgraph-not-idempotent branch September 11, 2020 09:04
@goccy
Copy link
Owner Author

goccy commented Sep 11, 2020

Thank you for confirmation! Merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants