-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
db config persistence builder suggestion #4772
Merged
tuliren
merged 1 commit into
liren/db-config-persistence-v2
from
cgardens/db-config-persistence-builder
Jul 15, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
...fig/persistence/src/main/java/io/airbyte/config/persistence/ConfigPersistenceBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2020 Airbyte | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package io.airbyte.config.persistence; | ||
|
||
import static io.airbyte.config.persistence.AirbyteConfigsTable.AIRBYTE_CONFIGS_TABLE_SCHEMA; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import io.airbyte.commons.resources.MoreResources; | ||
import io.airbyte.config.Configs; | ||
import io.airbyte.db.Database; | ||
import io.airbyte.db.Databases; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.function.Function; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* By default, this factory returns a database config persistence. it can still return a file system | ||
* config persistence for testing purpose. This legacy feature should be removed after the file to | ||
* database migration is completely done. | ||
*/ | ||
public class ConfigPersistenceBuilder { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigPersistenceBuilder.class); | ||
|
||
@VisibleForTesting | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These annotations seem redundant. It is pretty obvious that they are visible for testing purpose. I'd like to remove all of them. |
||
static class ConfigPersistenceFactory { | ||
|
||
private final Configs configs; | ||
private final boolean setupDatabase; | ||
private final boolean useConfigDatabase; | ||
|
||
/** | ||
* @param setupDatabase initialize the database and load data; this is necessary because this method | ||
* has multiple callers, and we want to setup the database only once to prevent race | ||
* conditions. | ||
*/ | ||
@VisibleForTesting | ||
ConfigPersistenceFactory(Configs configs, boolean setupDatabase, boolean useConfigDatabase) { | ||
this.configs = configs; | ||
this.setupDatabase = setupDatabase; | ||
this.useConfigDatabase = useConfigDatabase; | ||
} | ||
|
||
/** | ||
* Create a config persistence based on the configs. | ||
* <p/> | ||
* If config root is defined, create a database config persistence and copy the configs from the | ||
* file-based config persistence. Otherwise, seed the database from the yaml files. | ||
*/ | ||
public ConfigPersistence create() throws IOException { | ||
if (!useConfigDatabase) { | ||
Path configRoot = configs.getConfigRoot(); | ||
LOGGER.info("Use file system config persistence (root: {})", configRoot); | ||
return FileSystemConfigPersistence.createWithValidation(configRoot); | ||
} | ||
|
||
if (configs.getConfigRoot() == null) { | ||
// This branch will only be true in a future Airbyte version, in which | ||
// the config root is no longer required and everything lives in the database. | ||
return createDbPersistenceWithYamlSeed(); | ||
} | ||
|
||
return createDbPersistenceWithFileSeed(); | ||
} | ||
|
||
@VisibleForTesting | ||
ConfigPersistence createDbPersistenceWithYamlSeed() throws IOException { | ||
ConfigPersistence seedConfigPersistence = new YamlSeedConfigPersistence(); | ||
return createDbPersistence(seedConfigPersistence); | ||
} | ||
|
||
@VisibleForTesting | ||
ConfigPersistence createDbPersistenceWithFileSeed() throws IOException { | ||
Path configRoot = configs.getConfigRoot(); | ||
ConfigPersistence fsConfigPersistence = FileSystemConfigPersistence.createWithValidation(configRoot); | ||
return createDbPersistence(fsConfigPersistence); | ||
} | ||
|
||
@VisibleForTesting | ||
ConfigPersistence createDbPersistence(ConfigPersistence seedConfigPersistence) throws IOException { | ||
LOGGER.info("Use database config persistence."); | ||
|
||
// When setupDatabase is true, it means the database will be initialized after we | ||
// connect to the database. So the database itself is considered ready as long as | ||
// the connection is alive. Otherwise, the database is expected to have full data. | ||
Function<Database, Boolean> isReady = setupDatabase | ||
? Databases.IS_CONFIG_DATABASE_CONNECTED | ||
: Databases.IS_CONFIG_DATABASE_LOADED_WITH_DATA; | ||
|
||
Database database = Databases.createPostgresDatabaseWithRetry( | ||
configs.getConfigDatabaseUser(), | ||
configs.getConfigDatabasePassword(), | ||
configs.getConfigDatabaseUrl(), | ||
isReady); | ||
|
||
DatabaseConfigPersistence dbConfigPersistence = new DatabaseConfigPersistence(database); | ||
if (setupDatabase) { | ||
dbConfigPersistence.initialize(MoreResources.readResource(AIRBYTE_CONFIGS_TABLE_SCHEMA)); | ||
dbConfigPersistence.loadData(seedConfigPersistence); | ||
} | ||
|
||
return new ValidatingConfigPersistence(dbConfigPersistence); | ||
} | ||
|
||
} | ||
|
||
public static Builder builder(Configs configs) { | ||
return new Builder(configs); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private final Configs configs; | ||
private boolean setupDatabase = false; | ||
private boolean useConfigDatabase = true; | ||
|
||
private Builder(Configs configs) { | ||
this.configs = configs; | ||
} | ||
|
||
public Builder setupDatabase(boolean setupDatabase) { | ||
this.setupDatabase = setupDatabase; | ||
return this; | ||
} | ||
|
||
public Builder useConfigDatabase(boolean useConfigDatabase) { | ||
this.useConfigDatabase = useConfigDatabase; | ||
return this; | ||
} | ||
|
||
public ConfigPersistence build() throws IOException { | ||
return new ConfigPersistenceFactory(configs, setupDatabase, useConfigDatabase).create(); | ||
} | ||
|
||
} | ||
|
||
} |
151 changes: 0 additions & 151 deletions
151
...fig/persistence/src/main/java/io/airbyte/config/persistence/ConfigPersistenceFactory.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this diff it a bit annoying but really
ConfigPersistenceBuilder.java
is a rename ofConfigPersistenceFactory.java