From aab2b39d1c2c862a92b9f327b125c36e13df68e3 Mon Sep 17 00:00:00 2001 From: Christophe Duong Date: Tue, 21 Sep 2021 11:32:59 +0200 Subject: [PATCH] Fix exception on API calls --- .../io/airbyte/server/ConfigDumpImporter.java | 26 ++++++++++++------- .../java/io/airbyte/server/ServerApp.java | 3 +++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/airbyte-server/src/main/java/io/airbyte/server/ConfigDumpImporter.java b/airbyte-server/src/main/java/io/airbyte/server/ConfigDumpImporter.java index 3039b399d55e..bb483d303678 100644 --- a/airbyte-server/src/main/java/io/airbyte/server/ConfigDumpImporter.java +++ b/airbyte-server/src/main/java/io/airbyte/server/ConfigDumpImporter.java @@ -89,14 +89,13 @@ public class ConfigDumpImporter { private static final String CONFIG_FOLDER_NAME = "airbyte_config"; private static final String DB_FOLDER_NAME = "airbyte_db"; private static final String VERSION_FILE_NAME = "VERSION"; - private static final String TMP_AIRBYTE_STAGED_RESOURCES = "/tmp/airbyte_staged_resources"; + private static final Path TMP_AIRBYTE_STAGED_RESOURCES = Path.of("/tmp/airbyte_staged_resources"); private final ConfigRepository configRepository; private final WorkspaceHelper workspaceHelper; private final SpecFetcher specFetcher; private final JsonSchemaValidator jsonSchemaValidator; private final JobPersistence jobPersistence; - private final Path stagedResourceRoot; public ConfigDumpImporter(ConfigRepository configRepository, JobPersistence jobPersistence, @@ -116,13 +115,22 @@ public ConfigDumpImporter(ConfigRepository configRepository, this.configRepository = configRepository; this.workspaceHelper = workspaceHelper; this.specFetcher = specFetcher; + } + + /** + * Re-initialize the staged resource folder that contains uploaded artifacts when importing + * workspaces. This is because they need to be done in two steps (two API endpoints), upload + * resource first then import. When server starts, we flush the content of this folder, deleting + * previously staged resources that were not imported yet. + */ + public static void initStagedResourceFolder() { try { - this.stagedResourceRoot = Path.of(TMP_AIRBYTE_STAGED_RESOURCES); - if (stagedResourceRoot.toFile().exists()) { - FileUtils.forceDelete(stagedResourceRoot.toFile()); + File stagedResourceRoot = TMP_AIRBYTE_STAGED_RESOURCES.toFile(); + if (stagedResourceRoot.exists()) { + FileUtils.forceDelete(stagedResourceRoot); } - FileUtils.forceMkdir(stagedResourceRoot.toFile()); - FileUtils.forceDeleteOnExit(stagedResourceRoot.toFile()); + FileUtils.forceMkdir(stagedResourceRoot); + FileUtils.forceDeleteOnExit(stagedResourceRoot); } catch (IOException e) { throw new RuntimeException("Failed to create staging resource folder", e); } @@ -325,7 +333,7 @@ private void checkDBVersion(final String airbyteVersion) throws IOException { public UploadRead uploadArchiveResource(File archive) { try { final UUID resourceId = UUID.randomUUID(); - FileUtils.moveFile(archive, stagedResourceRoot.resolve(resourceId.toString()).toFile()); + FileUtils.moveFile(archive, TMP_AIRBYTE_STAGED_RESOURCES.resolve(resourceId.toString()).toFile()); return new UploadRead() .status(UploadRead.StatusEnum.SUCCEEDED) .resourceId(resourceId); @@ -336,7 +344,7 @@ public UploadRead uploadArchiveResource(File archive) { } public File getArchiveResource(UUID resourceId) { - final File archive = stagedResourceRoot.resolve(resourceId.toString()).toFile(); + final File archive = TMP_AIRBYTE_STAGED_RESOURCES.resolve(resourceId.toString()).toFile(); if (!archive.exists()) { throw new IdNotFoundKnownException("Archive Resource not found", resourceId.toString()); } diff --git a/airbyte-server/src/main/java/io/airbyte/server/ServerApp.java b/airbyte-server/src/main/java/io/airbyte/server/ServerApp.java index 516b6bbcf757..0e9d52917d84 100644 --- a/airbyte-server/src/main/java/io/airbyte/server/ServerApp.java +++ b/airbyte-server/src/main/java/io/airbyte/server/ServerApp.java @@ -174,6 +174,9 @@ public static ServerRunnable getServer(final ServerFactory apiFactory) throws Ex LogClientSingleton.setWorkspaceMdc(LogClientSingleton.getServerLogsRoot(configs)); + LOGGER.info("Creating Staged Resource folder..."); + ConfigDumpImporter.initStagedResourceFolder(); + LOGGER.info("Creating config repository..."); final Database configDatabase = new ConfigsDatabaseInstance( configs.getConfigDatabaseUser(),