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

Ordering of patches with cycles during upload #2524

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

aditya-07
Copy link
Collaborator

IMPORTANT: All PRs must be linked to an issue (except for extremely trivial and straightforward changes).

Fixes #2500

Description

  1. Find subgraphs (weakly connected) of Resources with cycles in them .
  2. Pack the subgraphs in Bundles such that a single subgraph is not split between two Bundles (as it may cause server error).
  3. Order the remaining resources and add them to the Bundle (in remaining space of or new Bundles) .

Alternative(s) considered
Have you considered any alternatives? And if so, why have you chosen the approach in this PR?

Type
Choose one: Bug fix

Screenshots (if applicable)

Checklist

  • I have read and acknowledged the Code of conduct.
  • I have read the Contributing page.
  • I have signed the Google Individual CLA, or I am covered by my company's Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked issue(s) and we have agreed upon the general approach.
  • I have run ./gradlew spotlessApply and ./gradlew spotlessCheck to check my code follows the style guide of this project.
  • I have run ./gradlew check and ./gradlew connectedCheck to test my changes locally.
  • I have built and run the demo app(s) to verify my change fixes the issue and/or does not break the demo app(s).

@aditya-07 aditya-07 requested review from santosh-pingle and a team as code owners April 23, 2024 06:56
@aditya-07 aditya-07 changed the title Ordering of patches with cyclic during upload Ordering of patches with cycles during upload Apr 23, 2024
@MJ1998
Copy link
Collaborator

MJ1998 commented May 1, 2024

Is this ready for review ?

@vorburger vorburger removed the request for review from a team May 16, 2024 15:05
Copy link
Collaborator

@MJ1998 MJ1998 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we use Strongly Connected Components (SCC) ?

  1. Identify the SCCs
  2. Consolidate all nodes in each SCC into a supernode.
  3. Then apply the same topological sorting

So for following graph:-
1 -> 2, 3
2 -> 4, 5
4 -> 1
5 -> 6
7 -> 8, 9
8 -> 7

SCC = [{1,2,4}, {7, 8}]
We can have the topological sort as - [{3}, {6}, {5}, {1, 2, 4}, {9}, {7, 8}]

Current solution is also right but the above solution has more improvements:-

  1. Time complexity is one-fourth (from first look).
  2. In current approach the weakly-connected-components will go in one bundle if there is a cycle. Whereas in the above approach only the SCC will go in one bundle -> Reduces the size of the bundle a lot.

@MJ1998
Copy link
Collaborator

MJ1998 commented May 17, 2024

@@ -67,3 +67,20 @@ internal data class PatchMapping(
val localChanges: List<LocalChange>,
val generatedPatch: Patch,
)

/** Structure to help describe the cyclic nature of ordered [PatchMapping]. */
internal sealed interface OrderedMapping {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think there is no order in this structure itself - so maybe patch mapping group is a better name - or something similar?


import kotlin.math.min

internal class StronglyConnectedPatches(private val diGraph: Graph, private val nodesCount: Int) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment pls


import kotlin.math.min

internal class StronglyConnectedPatches(private val diGraph: Graph, private val nodesCount: Int) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal class StronglyConnectedPatches(private val diGraph: Graph, private val nodesCount: Int) {
internal class StronglyConnectedPatches(private val directedGraph: Graph, private val nodeCount: Int) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're having these as data members of the class - calling sscOrdered twice would not have any side effects? Just making sure... because you probably will need to make sure that nothing in your algorithm is modifying the graph and the node count.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

say: the node count is the number of nodes in the directedGraph, inconsistency between the graph and the node count might generate incorrect results.

Comment on lines +132 to +142
/**
* Converts a graph that has ssc to a graph with super nodes as per the provided [nodeToSuperNode]
* mapping.
*
* **Input**
* * oldGraph `[A : B, C], [B : A, C], [C : A, B, F], [D : E], [E : D], [F : G]`
* * nodeToSuperNode `[A:1, B:1, C:1, D:2, E:2, F:3, G:4]`
*
* **Output** `[1 : 3], [2], [3: 4]`
*/
private fun transformGraphWithSSCtoGraphWithSuperNodes(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what this function actually does is: generating edges in the graph G' of super nodes from the edges in the original graph G.

so if there's an edge a->b in G, then there is an edge from S(a)->S(b) in G' where S(x) is the super node of x and S(a)!=S(b).

maybe you can add this to the description as at the moment it's not as detailed.

return createTopologicalOrderedList(adjacencyList).mapNotNull { resourceIdToPatchMapping[it] }

return StronglyConnectedPatches(adjacencyList, resourceIdToPatchMapping.size)
.sscOrdered { createTopologicalOrderedList(it) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you can do

.sscOrdered(::createTopologicalOrderedList)


import kotlin.math.min

internal class StronglyConnectedPatches(private val diGraph: Graph, private val nodesCount: Int) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs some tests. especially simple cases e.g. graph of size 1, graph of size 0. graph of size 2 with or without a single link.

@@ -67,3 +67,20 @@ internal data class PatchMapping(
val localChanges: List<LocalChange>,
val generatedPatch: Patch,
)

/** Structure to help describe the cyclic nature of ordered [PatchMapping]. */
internal sealed interface PatchMappingGroup {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we can define a single structure which is a collection of patch mappings that should be uploaded together. in (possibly most) some cases, they should just be individual patch mappings, but in the case of cycles, they would be bundled together as a result of your SCC calculation.

private typealias Node = String
typealias Node = String

typealias Graph = Map<Node, List<Node>>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think about this alternative definition:

data class Node(private id: String, private out: List<Node>)
typealias Graph = List<Node>

and potentially this may simplify your code a bit. because you can define things like

data class SuperNode(val out: List<SuperNode>, val nodes: List<Nodes>)
data class Node(val id: String, val out: List<Node>, val superNode: SuperNode)

and another thing is that - if we use this, we might be able to do away with copying the strings.

Comment on lines +39 to +40
val intToNode = mutableMapOf<Int, String>()
val nodeToInt = mutableMapOf<String, Int>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comments here.

val sccs = IntArray(graph.size)
val stack = ArrayDeque<Int>()

fun dfs(at: Int) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment here

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

Successfully merging this pull request may close these issues.

Sync upload local changes reordering patch issue
3 participants