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 temp #190

Merged
merged 2 commits into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scio-core/src/main/scala/com/spotify/scio/ScioContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.spotify.scio
import java.beans.Introspector
import java.io.File
import java.net.{URI, URLClassLoader}
import java.nio.file.Files
import java.util.concurrent.TimeUnit

import com.google.api.services.bigquery.model.TableReference
Expand Down Expand Up @@ -157,6 +158,13 @@ class ScioContext private[scio] (val options: PipelineOptions,
Try(optionsAs[DataflowPipelineWorkerPoolOptions])
.foreach(_.setFilesToStage(getFilesToStage(artifacts).asJava))
_pipeline = if (testId.isEmpty) {
// if in local runner, temp location may be needed, but is not currently required by
// the runner, which may end up with NPE. If not set but user generate new temp dir
if (ScioUtil.isLocalRunner(options) && options.getTempLocation == null) {
val tmpDir = Files.createTempDirectory("scio-temp-")
logger.debug(s"New temp directory at $tmpDir")
options.setTempLocation(tmpDir.toString)
}
Pipeline.create(options)
} else {
val tp = TestPipeline.create()
Expand Down
12 changes: 12 additions & 0 deletions scio-core/src/main/scala/com/spotify/scio/util/ScioUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ package com.spotify.scio.util
import java.net.URI

import com.google.cloud.dataflow.sdk.coders.{Coder, CoderRegistry}
import com.google.cloud.dataflow.sdk.options.PipelineOptions
import com.google.cloud.dataflow.sdk.runners.DirectPipelineRunner
import com.google.cloud.dataflow.sdk.runners.inprocess.InProcessPipelineRunner

import scala.reflect.ClassTag

Expand All @@ -40,4 +43,13 @@ private[scio] object ScioUtil {

coderRegistry.getScalaCoder[T]
}

def isLocalRunner(options: PipelineOptions): Boolean = {
val runner = options.getRunner

require(runner != null, "Pipeline runner not set!")

runner.isAssignableFrom(classOf[DirectPipelineRunner]) ||
runner.isAssignableFrom(classOf[InProcessPipelineRunner])
}
}
42 changes: 42 additions & 0 deletions scio-test/src/test/scala/com/spotify/scio/ScioContextTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package com.spotify.scio

import com.google.cloud.dataflow.sdk.options.{DataflowPipelineOptions, PipelineOptionsFactory}
import com.google.cloud.dataflow.sdk.runners.{DataflowPipelineRunner, DirectPipelineRunner}
import com.google.cloud.dataflow.sdk.runners.inprocess.InProcessPipelineRunner
import com.google.cloud.dataflow.sdk.testing.DataflowAssert
import com.google.cloud.dataflow.sdk.transforms.Create
import com.google.common.collect.Lists
import com.spotify.scio.testing.PipelineSpec
import org.apache.commons.lang.exception.ExceptionUtils

import scala.collection.JavaConverters._

Expand All @@ -39,4 +43,42 @@ class ScioContextTest extends PipelineSpec {
pipeline.run()
}

it should "have temp directory for default runner" in {
val pipeline = ScioContext().pipeline
pipeline.getOptions.getTempLocation should not be null
}

it should "have temp directory for default InProcessPipelineRunner" in {
val opts = PipelineOptionsFactory.create()
opts.setRunner(classOf[InProcessPipelineRunner])
val pipeline = ScioContext(opts).pipeline
pipeline.getOptions.getTempLocation should not be null
}

it should "have temp directory for default DirectPipelineRunner" in {
val opts = PipelineOptionsFactory.create()
opts.setRunner(classOf[DirectPipelineRunner])
val pipeline = ScioContext(opts).pipeline
pipeline.getOptions.getTempLocation should not be null
}

it should "use user specified temp directory" in {
val expected = "/expected"
val opts = PipelineOptionsFactory.create()
opts.setTempLocation(expected)
val pipeline = ScioContext(opts).pipeline
pipeline.getOptions.getTempLocation shouldEqual expected
}

it should "fail without temp/staging dir for DataflowPipelineRunner " in {
val opts = PipelineOptionsFactory.create()
opts.setRunner(classOf[DataflowPipelineRunner])
val dfOpts = opts.as(classOf[DataflowPipelineOptions])
dfOpts.setProject("foobar")
val sc = ScioContext(dfOpts)
val e = intercept[RuntimeException] { sc.pipeline }
ExceptionUtils.getFullStackTrace(e) should
include ("at least one of tempLocation or stagingLocation must be set.")
}

}