forked from apache/paimon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support common jdbc catalog lock for filesystem catalog.
- Loading branch information
1 parent
48aa793
commit 3bb35f9
Showing
5 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
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
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
84 changes: 84 additions & 0 deletions
84
paimon-core/src/main/java/org/apache/paimon/catalog/LockContextUtils.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,84 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.paimon.catalog; | ||
|
||
import org.apache.paimon.jdbc.JdbcCatalogFactory; | ||
import org.apache.paimon.jdbc.JdbcCatalogLock; | ||
import org.apache.paimon.jdbc.JdbcClientPool; | ||
import org.apache.paimon.jdbc.JdbcUtils; | ||
import org.apache.paimon.options.CatalogOptions; | ||
import org.apache.paimon.options.Options; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Optional; | ||
|
||
/** Utils for {@link org.apache.paimon.catalog.CatalogLock.LockContext}. */ | ||
public class LockContextUtils { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(FileSystemCatalog.class); | ||
|
||
private static JdbcClientPool connections; | ||
|
||
public static Optional<CatalogLock.LockContext> lockContext( | ||
Options catalogOptions, String catalogKey) { | ||
String lockType = catalogOptions.get(CatalogOptions.LOCK_TYPE); | ||
if (lockType == null) { | ||
return Optional.of(new AbstractCatalog.OptionLockContext(catalogOptions)); | ||
} | ||
switch (lockType) { | ||
case JdbcCatalogFactory.IDENTIFIER: | ||
// Try init jdbc connections. | ||
tryInitializeJdbcConnections(catalogOptions); | ||
return Optional.of( | ||
new JdbcCatalogLock.JdbcLockContext( | ||
connections, catalogKey, catalogOptions)); | ||
default: | ||
LOG.warn("Unsupported lock type:" + lockType); | ||
return Optional.of(new AbstractCatalog.OptionLockContext(catalogOptions)); | ||
} | ||
} | ||
|
||
private static void tryInitializeJdbcConnections(Options catalogOptions) { | ||
if (connections == null) { | ||
connections = | ||
new JdbcClientPool( | ||
catalogOptions.get(CatalogOptions.CLIENT_POOL_SIZE), | ||
catalogOptions.get(CatalogOptions.URI.key()), | ||
catalogOptions.toMap()); | ||
// Check and create distributed lock table. | ||
try { | ||
JdbcUtils.createDistributedLockTable(connections, catalogOptions); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Cannot initialize JDBC distributed lock.", e); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException("Interrupted in call to initialize", e); | ||
} | ||
} | ||
} | ||
|
||
public static void close() { | ||
if (connections != null && !connections.isClosed()) { | ||
connections.close(); | ||
connections = null; | ||
} | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
paimon-core/src/test/java/org/apache/paimon/catalog/FileSystemCatalogTest.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,68 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.paimon.catalog; | ||
|
||
import org.apache.paimon.fs.Path; | ||
import org.apache.paimon.jdbc.JdbcCatalog; | ||
import org.apache.paimon.options.CatalogOptions; | ||
import org.apache.paimon.options.Options; | ||
|
||
import org.apache.paimon.shade.guava30.com.google.common.collect.Maps; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** Tests for {@link FileSystemCatalog}. */ | ||
public class FileSystemCatalogTest extends CatalogTestBase { | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
catalog = initCatalog(Maps.newHashMap()); | ||
} | ||
|
||
private FileSystemCatalog initCatalog(Map<String, String> props) { | ||
Map<String, String> properties = Maps.newHashMap(); | ||
properties.put( | ||
CatalogOptions.URI.key(), | ||
"jdbc:sqlite:file::memory:?ic" + UUID.randomUUID().toString().replace("-", "")); | ||
|
||
properties.put(JdbcCatalog.PROPERTY_PREFIX + "username", "user"); | ||
properties.put(JdbcCatalog.PROPERTY_PREFIX + "password", "password"); | ||
properties.put(CatalogOptions.WAREHOUSE.key(), warehouse); | ||
properties.put(CatalogOptions.LOCK_ENABLED.key(), "true"); | ||
properties.put(CatalogOptions.LOCK_TYPE.key(), "jdbc"); | ||
properties.putAll(props); | ||
FileSystemCatalog catalog = | ||
new FileSystemCatalog(fileIO, new Path(warehouse), Options.fromMap(properties)); | ||
return catalog; | ||
} | ||
|
||
@Override | ||
public void testListDatabasesWhenNoDatabases() { | ||
List<String> databases = catalog.listDatabases(); | ||
assertThat(databases).isEqualTo(new ArrayList<>()); | ||
} | ||
} |