@Override public DatasetStateStore<JobState.DatasetState> createStateStore(Config config) { String stateStoreTableName = config.hasPath(ConfigurationKeys.STATE_STORE_DB_TABLE_KEY) ? config.getString(ConfigurationKeys.STATE_STORE_DB_TABLE_KEY) : ConfigurationKeys.DEFAULT_STATE_STORE_DB_TABLE; boolean compressedValues = config.hasPath(ConfigurationKeys.STATE_STORE_COMPRESSED_VALUES_KEY) ? config.getBoolean(ConfigurationKeys.STATE_STORE_COMPRESSED_VALUES_KEY) : ConfigurationKeys.DEFAULT_STATE_STORE_COMPRESSED_VALUES; try { BasicDataSource basicDataSource = MysqlDataSourceFactory.get(config, SharedResourcesBrokerFactory.getImplicitBroker()); return new MysqlDatasetStateStore(basicDataSource, stateStoreTableName, compressedValues); } catch (Exception e) { throw new RuntimeException("Failed to create MysqlDatasetStateStore with factory", e); } } }
/** * Get a {@link BasicDataSource} based on the config * @param config configuration * @param broker broker * @return a {@link BasicDataSource} * @throws IOException */ public static <S extends ScopeType<S>> BasicDataSource get(Config config, SharedResourcesBroker<S> broker) throws IOException { try { return broker.getSharedResource(new MysqlDataSourceFactory<S>(), new MysqlDataSourceKey(MysqlStateStore.getDataSourceId(config), config)); } catch (NotConfiguredException nce) { throw new IOException(nce); } }
@Test public void testSameKey() throws IOException { Config config = ConfigFactory.parseMap(ImmutableMap.of(ConfigurationKeys.STATE_STORE_DB_URL_KEY, "url", ConfigurationKeys.STATE_STORE_DB_USER_KEY, "user", ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY, "dummypwd")); BasicDataSource basicDataSource1 = MysqlDataSourceFactory.get(config, SharedResourcesBrokerFactory.getImplicitBroker()); BasicDataSource basicDataSource2 = MysqlDataSourceFactory.get(config, SharedResourcesBrokerFactory.getImplicitBroker()); Assert.assertEquals(basicDataSource1, basicDataSource2); }
/** * Get a {@link BasicDataSource} based on the config * @param config configuration * @param broker broker * @return a {@link BasicDataSource} * @throws IOException */ public static <S extends ScopeType<S>> BasicDataSource get(Config config, SharedResourcesBroker<S> broker) throws IOException { try { return broker.getSharedResource(new MysqlDataSourceFactory<S>(), new MysqlDataSourceKey(MysqlStateStore.getDataSourceId(config), config)); } catch (NotConfiguredException nce) { throw new IOException(nce); } }
@Override public <T extends State> StateStore<T> createStateStore(Config config, Class<T> stateClass) { String stateStoreTableName = ConfigUtils.getString(config, ConfigurationKeys.STATE_STORE_DB_TABLE_KEY, ConfigurationKeys.DEFAULT_STATE_STORE_DB_TABLE); boolean compressedValues = ConfigUtils.getBoolean(config, ConfigurationKeys.STATE_STORE_COMPRESSED_VALUES_KEY, ConfigurationKeys.DEFAULT_STATE_STORE_COMPRESSED_VALUES); try { BasicDataSource basicDataSource = MysqlDataSourceFactory.get(config, SharedResourcesBrokerFactory.getImplicitBroker()); return new MysqlStateStore(basicDataSource, stateStoreTableName, compressedValues, stateClass); } catch (Exception e) { throw new RuntimeException("Failed to create MysqlStateStore with factory", e); } } }
@Test public void testDifferentKey() throws IOException { Config config1 = ConfigFactory.parseMap(ImmutableMap.of(ConfigurationKeys.STATE_STORE_DB_URL_KEY, "url1", ConfigurationKeys.STATE_STORE_DB_USER_KEY, "user", ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY, "dummypwd")); Config config2 = ConfigFactory.parseMap(ImmutableMap.of(ConfigurationKeys.STATE_STORE_DB_URL_KEY, "url2", ConfigurationKeys.STATE_STORE_DB_USER_KEY, "user", ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY, "dummypwd")); BasicDataSource basicDataSource1 = MysqlDataSourceFactory.get(config1, SharedResourcesBrokerFactory.getImplicitBroker()); BasicDataSource basicDataSource2 = MysqlDataSourceFactory.get(config2, SharedResourcesBrokerFactory.getImplicitBroker()); Assert.assertNotEquals(basicDataSource1, basicDataSource2); }
@Test public void testSameDbDifferentUser() throws IOException { Config config1 = ConfigFactory.parseMap(ImmutableMap.of(ConfigurationKeys.STATE_STORE_DB_URL_KEY, "url1", ConfigurationKeys.STATE_STORE_DB_USER_KEY, "user1", ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY, "dummypwd")); Config config2 = ConfigFactory.parseMap(ImmutableMap.of(ConfigurationKeys.STATE_STORE_DB_URL_KEY, "url1", ConfigurationKeys.STATE_STORE_DB_USER_KEY, "user2", ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY, "dummypwd")); BasicDataSource basicDataSource1 = MysqlDataSourceFactory.get(config1, SharedResourcesBrokerFactory.getImplicitBroker()); BasicDataSource basicDataSource2 = MysqlDataSourceFactory.get(config2, SharedResourcesBrokerFactory.getImplicitBroker()); Assert.assertNotEquals(basicDataSource1, basicDataSource2); } }
@Override public DatasetStateStore<JobState.DatasetState> createStateStore(Config config) { String stateStoreTableName = config.hasPath(ConfigurationKeys.STATE_STORE_DB_TABLE_KEY) ? config.getString(ConfigurationKeys.STATE_STORE_DB_TABLE_KEY) : ConfigurationKeys.DEFAULT_STATE_STORE_DB_TABLE; boolean compressedValues = config.hasPath(ConfigurationKeys.STATE_STORE_COMPRESSED_VALUES_KEY) ? config.getBoolean(ConfigurationKeys.STATE_STORE_COMPRESSED_VALUES_KEY) : ConfigurationKeys.DEFAULT_STATE_STORE_COMPRESSED_VALUES; try { BasicDataSource basicDataSource = MysqlDataSourceFactory.get(config, SharedResourcesBrokerFactory.getImplicitBroker()); return new MysqlDatasetStateStore(basicDataSource, stateStoreTableName, compressedValues); } catch (Exception e) { throw new RuntimeException("Failed to create MysqlDatasetStateStore with factory", e); } } }
@Override public <T extends State> StateStore<T> createStateStore(Config config, Class<T> stateClass) { String stateStoreTableName = ConfigUtils.getString(config, ConfigurationKeys.STATE_STORE_DB_TABLE_KEY, ConfigurationKeys.DEFAULT_STATE_STORE_DB_TABLE); boolean compressedValues = ConfigUtils.getBoolean(config, ConfigurationKeys.STATE_STORE_COMPRESSED_VALUES_KEY, ConfigurationKeys.DEFAULT_STATE_STORE_COMPRESSED_VALUES); try { BasicDataSource basicDataSource = MysqlDataSourceFactory.get(config, SharedResourcesBrokerFactory.getImplicitBroker()); return new MysqlStateStore(basicDataSource, stateStoreTableName, compressedValues, stateClass); } catch (Exception e) { throw new RuntimeException("Failed to create MysqlStateStore with factory", e); } } }