@@ -33,20 +33,20 @@ package com.salesforce.op
3333
3434import java .io .File
3535
36- import com .salesforce .op .OpWorkflowModelReadWriteShared .{FieldNames => FN }
37- import com .salesforce .op .OpWorkflowModelReadWriteShared .FieldNames ._
3836import com .salesforce .op .OpWorkflowModelReadWriteShared .DeprecatedFieldNames ._
37+ import com .salesforce .op .OpWorkflowModelReadWriteShared .FieldNames ._
38+ import com .salesforce .op .OpWorkflowModelReadWriteShared .{FieldNames => FN }
3939import com .salesforce .op .features .{FeatureJsonHelper , OPFeature , TransientFeature }
4040import com .salesforce .op .filters .{FeatureDistribution , RawFeatureFilterResults }
4141import com .salesforce .op .stages .OpPipelineStageReaderWriter ._
4242import com .salesforce .op .stages ._
43- import org .zeroturnaround .zip .ZipUtil
4443import org .apache .commons .io .IOUtils
4544import org .apache .hadoop .conf .Configuration
4645import org .apache .hadoop .fs .{FileSystem , Path }
4746import org .apache .hadoop .io .compress .CompressionCodecFactory
4847import org .json4s .JsonAST .{JArray , JNothing , JValue }
4948import org .json4s .jackson .JsonMethods .parse
49+ import org .zeroturnaround .zip .ZipUtil
5050
5151import scala .collection .mutable .ArrayBuffer
5252import scala .io .Source
@@ -71,34 +71,39 @@ class OpWorkflowModelReader(val workflowOpt: Option[OpWorkflow], val asSpark: Bo
7171 */
7272 final def load (path : String , modelStagingDir : String = WorkflowFileReader .modelStagingDir): OpWorkflowModel = {
7373 implicit val conf = new Configuration ()
74- val localPath = new Path (modelStagingDir)
7574 val localFileSystem = FileSystem .getLocal(conf)
75+ val localPath = localFileSystem.makeQualified(new Path (modelStagingDir))
7676 localFileSystem.delete(localPath, true )
7777
7878 val savePath = new Path (path)
7979 val remoteFileSystem = savePath.getFileSystem(conf)
80-
81- val zipPath = new Path (localPath, WorkflowFileReader .zipModel)
82-
83- remoteFileSystem.copyToLocalFile(savePath, zipPath)
84-
80+ val zipDir = new Path (localPath, WorkflowFileReader .zipModel)
81+ remoteFileSystem.copyToLocalFile(savePath, zipDir)
82+
83+ // New serialization:
84+ // remote: savePath (dir) -> Model.zip (file)
85+ // local: Model.zip (dir) -> Model.zip (file)
86+ // Old serialization:
87+ // remote: savePath (dir)
88+ // local: Model.zip (dir)
8589 val modelDir = new Path (localPath, WorkflowFileReader .rawModel)
86- val fileToLoad = Try {
87- val zipFile = new File (zipPath.toString )
88- val subZip = // TODO figure out why it puts the files like this
89- if (zipFile.isDirectory) new File (zipFile, WorkflowFileReader .zipModel)
90- else zipFile
91- ZipUtil .unpack(subZip, new File ( modelDir.toString))
92- } match { // For backwards compatibility since old models will not be zipped
93- case Success (_) => modelDir .toString
94- case Failure (_) => zipPath.toString
95- }
96-
97- val model = Try ( WorkflowFileReader .loadFile( OpWorkflowModelReadWriteShared .jsonPath(fileToLoad)))
98- .flatMap(loadJson(_, path = fileToLoad)) match {
99- case Failure (error) => throw new RuntimeException (s " Failed to load Workflow from path ' $path' " , error)
90+ val modelPath = Try {
91+ localFileSystem.open( new Path (zipDir, WorkflowFileReader .zipModel) )
92+ }.map { inputStream =>
93+ try {
94+ ZipUtil .unpack(inputStream, new File (modelDir.toUri.getPath))
95+ modelDir.toString
96+ } finally inputStream.close()
97+ }.getOrElse(zipDir .toString)
98+
99+ val model = Try (
100+ WorkflowFileReader .loadFile( OpWorkflowModelReadWriteShared .jsonPath(modelPath))
101+ ).flatMap(loadJson(_, path = modelPath)) match {
102+ case Failure (error) =>
103+ throw new RuntimeException (s " Failed to load Workflow from path ' $path' " , error)
100104 case Success (wf) => wf
101105 }
106+
102107 localFileSystem.delete(localPath, true )
103108 model
104109 }
@@ -260,7 +265,6 @@ class OpWorkflowModelReader(val workflowOpt: Option[OpWorkflow], val asSpark: Bo
260265}
261266
262267private object WorkflowFileReader {
263-
264268 val rawModel = " rawModel"
265269 val zipModel = " Model.zip"
266270 def modelStagingDir : String = s " modelStagingDir/model- ${System .currentTimeMillis}"
@@ -286,14 +290,16 @@ private object WorkflowFileReader {
286290 }
287291
288292 private def readAsString (path : Path )(implicit conf : Configuration ): String = {
289- val fs = path.getFileSystem(conf)
290293 val codecFactory = new CompressionCodecFactory (conf)
291294 val codec = Option (codecFactory.getCodec(path))
292- val in = fs.open(path)
293- val read = codec.map( c => Source .fromInputStream(c.createInputStream(in)).mkString )
294- .getOrElse( IOUtils .toString(in, " UTF-8" ) )
295- in.close()
296- read
295+ val in = FileSystem .getLocal(conf).open(path)
296+ try {
297+ val read = codec.map(c => Source .fromInputStream(c.createInputStream(in)).mkString)
298+ .getOrElse(IOUtils .toString(in, " UTF-8" ))
299+ read
300+ } finally {
301+ in.close()
302+ }
297303 }
298304}
299305
0 commit comments