Skip to content
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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
Copy link
Contributor Author

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 of ConfigPersistenceFactory.java

* 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}

}

}

This file was deleted.

Loading