|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020 Airbyte |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package io.airbyte.config.persistence; |
| 26 | + |
| 27 | +import static io.airbyte.config.persistence.AirbyteConfigsTable.AIRBYTE_CONFIGS_TABLE_SCHEMA; |
| 28 | + |
| 29 | +import com.google.common.annotations.VisibleForTesting; |
| 30 | +import io.airbyte.commons.resources.MoreResources; |
| 31 | +import io.airbyte.config.Configs; |
| 32 | +import io.airbyte.db.Database; |
| 33 | +import io.airbyte.db.Databases; |
| 34 | +import java.io.IOException; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.util.function.Function; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | +/** |
| 41 | + * By default, this factory returns a database config persistence. it can still return a file system |
| 42 | + * config persistence for testing purpose. This legacy feature should be removed after the file to |
| 43 | + * database migration is completely done. |
| 44 | + */ |
| 45 | +public class ConfigPersistenceBuilder { |
| 46 | + |
| 47 | + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigPersistenceBuilder.class); |
| 48 | + |
| 49 | + @VisibleForTesting |
| 50 | + static class ConfigPersistenceFactory { |
| 51 | + |
| 52 | + private final Configs configs; |
| 53 | + private final boolean setupDatabase; |
| 54 | + private final boolean useConfigDatabase; |
| 55 | + |
| 56 | + /** |
| 57 | + * @param setupDatabase initialize the database and load data; this is necessary because this method |
| 58 | + * has multiple callers, and we want to setup the database only once to prevent race |
| 59 | + * conditions. |
| 60 | + */ |
| 61 | + @VisibleForTesting |
| 62 | + ConfigPersistenceFactory(Configs configs, boolean setupDatabase, boolean useConfigDatabase) { |
| 63 | + this.configs = configs; |
| 64 | + this.setupDatabase = setupDatabase; |
| 65 | + this.useConfigDatabase = useConfigDatabase; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Create a config persistence based on the configs. |
| 70 | + * <p/> |
| 71 | + * If config root is defined, create a database config persistence and copy the configs from the |
| 72 | + * file-based config persistence. Otherwise, seed the database from the yaml files. |
| 73 | + */ |
| 74 | + public ConfigPersistence create() throws IOException { |
| 75 | + if (!useConfigDatabase) { |
| 76 | + Path configRoot = configs.getConfigRoot(); |
| 77 | + LOGGER.info("Use file system config persistence (root: {})", configRoot); |
| 78 | + return FileSystemConfigPersistence.createWithValidation(configRoot); |
| 79 | + } |
| 80 | + |
| 81 | + if (configs.getConfigRoot() == null) { |
| 82 | + // This branch will only be true in a future Airbyte version, in which |
| 83 | + // the config root is no longer required and everything lives in the database. |
| 84 | + return createDbPersistenceWithYamlSeed(); |
| 85 | + } |
| 86 | + |
| 87 | + return createDbPersistenceWithFileSeed(); |
| 88 | + } |
| 89 | + |
| 90 | + @VisibleForTesting |
| 91 | + ConfigPersistence createDbPersistenceWithYamlSeed() throws IOException { |
| 92 | + ConfigPersistence seedConfigPersistence = new YamlSeedConfigPersistence(); |
| 93 | + return createDbPersistence(seedConfigPersistence); |
| 94 | + } |
| 95 | + |
| 96 | + @VisibleForTesting |
| 97 | + ConfigPersistence createDbPersistenceWithFileSeed() throws IOException { |
| 98 | + Path configRoot = configs.getConfigRoot(); |
| 99 | + ConfigPersistence fsConfigPersistence = FileSystemConfigPersistence.createWithValidation(configRoot); |
| 100 | + return createDbPersistence(fsConfigPersistence); |
| 101 | + } |
| 102 | + |
| 103 | + @VisibleForTesting |
| 104 | + ConfigPersistence createDbPersistence(ConfigPersistence seedConfigPersistence) throws IOException { |
| 105 | + LOGGER.info("Use database config persistence."); |
| 106 | + |
| 107 | + // When setupDatabase is true, it means the database will be initialized after we |
| 108 | + // connect to the database. So the database itself is considered ready as long as |
| 109 | + // the connection is alive. Otherwise, the database is expected to have full data. |
| 110 | + Function<Database, Boolean> isReady = setupDatabase |
| 111 | + ? Databases.IS_CONFIG_DATABASE_CONNECTED |
| 112 | + : Databases.IS_CONFIG_DATABASE_LOADED_WITH_DATA; |
| 113 | + |
| 114 | + Database database = Databases.createPostgresDatabaseWithRetry( |
| 115 | + configs.getConfigDatabaseUser(), |
| 116 | + configs.getConfigDatabasePassword(), |
| 117 | + configs.getConfigDatabaseUrl(), |
| 118 | + isReady); |
| 119 | + |
| 120 | + DatabaseConfigPersistence dbConfigPersistence = new DatabaseConfigPersistence(database); |
| 121 | + if (setupDatabase) { |
| 122 | + dbConfigPersistence.initialize(MoreResources.readResource(AIRBYTE_CONFIGS_TABLE_SCHEMA)); |
| 123 | + dbConfigPersistence.loadData(seedConfigPersistence); |
| 124 | + } |
| 125 | + |
| 126 | + return new ValidatingConfigPersistence(dbConfigPersistence); |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + public static Builder builder(Configs configs) { |
| 132 | + return new Builder(configs); |
| 133 | + } |
| 134 | + |
| 135 | + public static class Builder { |
| 136 | + |
| 137 | + private final Configs configs; |
| 138 | + private boolean setupDatabase = false; |
| 139 | + private boolean useConfigDatabase = true; |
| 140 | + |
| 141 | + private Builder(Configs configs) { |
| 142 | + this.configs = configs; |
| 143 | + } |
| 144 | + |
| 145 | + public Builder setupDatabase(boolean setupDatabase) { |
| 146 | + this.setupDatabase = setupDatabase; |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + public Builder useConfigDatabase(boolean useConfigDatabase) { |
| 151 | + this.useConfigDatabase = useConfigDatabase; |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + public ConfigPersistence build() throws IOException { |
| 156 | + return new ConfigPersistenceFactory(configs, setupDatabase, useConfigDatabase).create(); |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments