Skip to content

Duplicate execution with always: true #42

Closed

Description

@dubo-dubon-duponey

Consider the following.

I have one "component", that is expected to produce a random string.

Then, I have two additional components ("test1" and "test2") that are copying and exporting this string.

package testing

component: #dagger: compute: [{
	do: "fetch-container"
	ref: "alpine"
}, {
	do: "exec"
	args: ["sh", "-c", """
	tr -dc A-Za-z0-9 </dev/urandom | head -c 13 > /id
	"""]
	dir: "/"
	always: true
}]

test1: {
	string

	#dagger: {
		compute: [
			{
				do: "fetch-container"
				ref: "busybox"
			},
			{
				do: "copy"
				from: component
				src: "/id"
				dest: "/"
			},
			{
				do: "export"
				source: "/id"
				format: "string"
			},
		]
	}
}

test2: {
	string

	#dagger: {
		compute: [
			{
				do: "fetch-container"
				ref: "busybox"
			},
			{
				do: "copy"
				from: component
				src: "/id"
				dest: "/"
			},
			{
				do: "export"
				source: "/id"
				format: "string"
			},
		]
	}
}

I was (naively?) expecting that "component" would run only once (and that "always: true" would ensure that it will re-run on subsequent executions), but in fact it's actually running twice (eg: it's acting like it has been inlined into test1 and test2).

Is this what you guys wanted/expected?

I guess it's somewhat confusing coming from a "usual" language.

Concretely, in a programing language I was expecting:


component=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13)

test1=$(echo $component)

test2=$(echo $component)

And that's really different from:

test1=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13)

test2=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13)

Activity

  1. dubo-dubon-duponey commented on Jan 15, 2021

    @dubo-dubon-duponey
    ContributorAuthor

    Conversely, consider the following:

    package testing
    
    component: #dagger: compute: [{
    	do: "fetch-container"
    	ref: "alpine"
    }, {
    	do: "exec"
    	args: ["sh", "-c", """
    	tr -dc A-Za-z0-9 </dev/urandom | head -c 13 > /id
    	"""]
    	dir: "/"
    }]
    
    test1: {
    	string
    
    	#dagger: {
    		compute: [
    			{
    				do: "fetch-container"
    				ref: "busybox"
    			},
    			{
    				do: "copy"
    				from: component
    				src: "/id"
    				dest: "/"
    			},
    			{
    				do: "export"
    				source: "/id"
    				format: "string"
    			},
    		]
    	}
    }
    
    test2: {
    	string
    
    	#dagger: {
    		compute: [
    			{
    				do: "fetch-container"
    				ref: "busybox"
    			},
    			{
    				do: "copy"
    				from: #dagger: compute: [{
    					do: "fetch-container"
    					ref: "alpine"
    				}, {
    					do: "exec"
    					args: ["sh", "-c", """
    					tr -dc A-Za-z0-9 </dev/urandom | head -c 13 > /id
    					"""]
    					dir: "/"
    				}]
    				src: "/id"
    				dest: "/"
    			},
    			{
    				do: "export"
    				source: "/id"
    				format: "string"
    			},
    		]
    	}
    }
    

    In that case, buildkit optimize out both random string generation (because they "look" the same to buildkit).
    This kinda makes sense from a caching perspective, but is also confusing, for similar reasons as above.

    As a developper the external "component" and the embedded one feel to me like they are really different "objects".

    eg, in bash lingo, that would "naively" express as:

    
    
    component=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13)
    
    test1=$(echo $component)
    
    test2=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13)
    

    Which is clearly not what we get here.

  2. aluzzardi commented on Feb 5, 2021

    @aluzzardi
    Contributor

    Good one. Indeed, dagger naively sends every #dagger: compute it finds to buildkit, letting it do deduplication.

    Which works out well most of the time. However, always: true forces buildkit not to cache and therefore, not to dedup.

    I agree this is very confusing, especially when doing a copy or a mount, you'd expect the component to be referenced, not embedded and re-executed.

    Bumping this to a bug.

    /cc @shykes

  3. added
    kind/bug
    and removed on Feb 5, 2021
  4. shykes commented on Feb 5, 2021

    @shykes
    Contributor

    Nice catch. I think we can implement always: true in such a way that the same random cache-buster is re-used across an entire compute job.

  5. changed the title [-]Question about "shared" component execution [/-] [+]Duplicate execution with `always: true`[/+] on Feb 5, 2021
  6. samalba commented on Mar 9, 2021

    @samalba
    Contributor

    I was able to repro. I tried to implement the fix @aluzzardi suggested: set the env var DAGGER_CACHEBUSTER only if it was not set before. However it requires to store the info in the task (when setting the env var for the first time), so we can then check if the env var was already set previously. I did not find a clean way to store the info in the cue tree (requires a fill() inside the pipeline array).

    A cleaner way would be to implement a task store for metadata that we could reuse for other things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions