From e5256f1f2022b0a256af6f28a85d74f54a02d8e0 Mon Sep 17 00:00:00 2001 From: Rajan Dhabalia Date: Thu, 7 Jun 2018 00:17:54 -0700 Subject: [PATCH] fix pkg location init (#1927) * fix pkg location init * test download-dir with permission --- .../functions/worker/FunctionActioner.java | 19 ++++++++++--------- .../worker/FunctionActionerTest.java | 2 ++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/FunctionActioner.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/FunctionActioner.java index 714cb50aa6265..70a1ce4859080 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/FunctionActioner.java +++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/FunctionActioner.java @@ -118,6 +118,14 @@ protected void startFunction(FunctionRuntimeInfo functionRuntimeInfo) throws Exc if(isPkgUrlProvided && pkgLocation.startsWith(Utils.FILE)) { pkgFile = new File(pkgLocation); } else { + File pkgDir = new File( + workerConfig.getDownloadDirectory(), + getDownloadPackagePath(functionMetaData, instanceId)); + pkgDir.mkdirs(); + + pkgFile = new File( + pkgDir, + new File(FunctionDetailsUtils.getDownloadFileName(functionMetaData.getFunctionDetails())).getName()); downloadFile(pkgFile, isPkgUrlProvided, functionMetaData, instanceId); } @@ -138,15 +146,8 @@ protected void startFunction(FunctionRuntimeInfo functionRuntimeInfo) throws Exc private void downloadFile(File pkgFile, boolean isPkgUrlProvided, FunctionMetaData functionMetaData, int instanceId) throws FileNotFoundException, IOException { - File pkgDir = new File( - workerConfig.getDownloadDirectory(), - getDownloadPackagePath(functionMetaData, instanceId)); - pkgDir.mkdirs(); - - pkgFile = new File( - pkgDir, - new File(FunctionDetailsUtils.getDownloadFileName(functionMetaData.getFunctionDetails())).getName()); - + File pkgDir = pkgFile.getParentFile(); + if (pkgFile.exists()) { log.warn("Function package exists already {} deleting it", pkgFile); diff --git a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/FunctionActionerTest.java b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/FunctionActionerTest.java index 9243a13c343b5..1dfe0fdd9a40c 100644 --- a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/FunctionActionerTest.java +++ b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/FunctionActionerTest.java @@ -96,6 +96,8 @@ public void testStartFunctionWithPkgUrl() throws Exception { workerConfig.setPulsarServiceUrl("pulsar://localhost:6650"); workerConfig.setStateStorageServiceUrl("foo"); workerConfig.setFunctionAssignmentTopicName("assignments"); + String downloadDir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); + workerConfig.setDownloadDirectory(downloadDir); RuntimeFactory factory = mock(RuntimeFactory.class); Runtime runtime = mock(Runtime.class);