|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.jdbc; |
| 20 | + |
| 21 | +import org.apache.paimon.catalog.Catalog; |
| 22 | +import org.apache.paimon.catalog.CatalogContext; |
| 23 | +import org.apache.paimon.fs.FileIO; |
| 24 | +import org.apache.paimon.fs.Path; |
| 25 | +import org.apache.paimon.options.CatalogOptions; |
| 26 | +import org.apache.paimon.options.Options; |
| 27 | + |
| 28 | +import org.apache.paimon.shade.guava30.com.google.common.collect.Maps; |
| 29 | + |
| 30 | +import org.junit.jupiter.api.AfterAll; |
| 31 | +import org.junit.jupiter.api.BeforeAll; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.io.TempDir; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
| 37 | +import org.testcontainers.containers.PostgreSQLContainer; |
| 38 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 39 | + |
| 40 | +import java.sql.SQLException; |
| 41 | +import java.util.Map; |
| 42 | + |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | + |
| 45 | +/* |
| 46 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 47 | + * or more contributor license agreements. See the NOTICE file |
| 48 | + * distributed with this work for additional information |
| 49 | + * regarding copyright ownership. The ASF licenses this file |
| 50 | + * to you under the Apache License, Version 2.0 (the |
| 51 | + * "License"); you may not use this file except in compliance |
| 52 | + * with the License. You may obtain a copy of the License at |
| 53 | + * |
| 54 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 55 | + * |
| 56 | + * Unless required by applicable law or agreed to in writing, software |
| 57 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 58 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 59 | + * See the License for the specific language governing permissions and |
| 60 | + * limitations under the License. |
| 61 | + */ |
| 62 | + |
| 63 | +/** Tests for {@link JdbcCatalog} with Postgres. */ |
| 64 | +public class PostgresqlCatalogTest { |
| 65 | + private static final Logger LOG = LoggerFactory.getLogger(PostgresqlCatalogTest.class); |
| 66 | + |
| 67 | + public static final String DEFAULT_DB = "postgres"; |
| 68 | + |
| 69 | + private static final String USER = "paimonuser"; |
| 70 | + private static final String PASSWORD = "paimonpw"; |
| 71 | + |
| 72 | + @TempDir java.nio.file.Path tempFile; |
| 73 | + protected String warehouse; |
| 74 | + protected FileIO fileIO; |
| 75 | + protected Catalog catalog; |
| 76 | + |
| 77 | + protected static final PostgreSQLContainer<?> POSTGRES_CONTAINER = |
| 78 | + new PostgreSQLContainer<>("postgres:13-alpine") |
| 79 | + .withDatabaseName(DEFAULT_DB) |
| 80 | + .withUsername(USER) |
| 81 | + .withPassword(PASSWORD) |
| 82 | + .withLogConsumer(new Slf4jLogConsumer(LOG)); |
| 83 | + |
| 84 | + @BeforeAll |
| 85 | + protected static void start() { |
| 86 | + LOG.info("Starting containers..."); |
| 87 | + POSTGRES_CONTAINER.start(); |
| 88 | + LOG.info("Containers are started."); |
| 89 | + } |
| 90 | + |
| 91 | + @AfterAll |
| 92 | + public static void stopContainers() { |
| 93 | + LOG.info("Stopping containers..."); |
| 94 | + POSTGRES_CONTAINER.stop(); |
| 95 | + LOG.info("Containers are stopped."); |
| 96 | + } |
| 97 | + |
| 98 | + @BeforeEach |
| 99 | + public void setUp() throws Exception { |
| 100 | + warehouse = tempFile.toUri().toString(); |
| 101 | + Options catalogOptions = new Options(); |
| 102 | + catalogOptions.set(CatalogOptions.WAREHOUSE, warehouse); |
| 103 | + CatalogContext catalogContext = CatalogContext.create(catalogOptions); |
| 104 | + fileIO = FileIO.get(new Path(warehouse), catalogContext); |
| 105 | + catalog = initCatalog(Maps.newHashMap()); |
| 106 | + } |
| 107 | + |
| 108 | + private JdbcCatalog initCatalog(Map<String, String> props) { |
| 109 | + LOG.info("Init catalog {}", POSTGRES_CONTAINER.getJdbcUrl()); |
| 110 | + |
| 111 | + Map<String, String> properties = Maps.newHashMap(); |
| 112 | + properties.put(CatalogOptions.URI.key(), POSTGRES_CONTAINER.getJdbcUrl()); |
| 113 | + |
| 114 | + properties.put(JdbcCatalog.PROPERTY_PREFIX + "user", USER); |
| 115 | + properties.put(JdbcCatalog.PROPERTY_PREFIX + "password", PASSWORD); |
| 116 | + properties.put(CatalogOptions.WAREHOUSE.key(), warehouse); |
| 117 | + properties.put(CatalogOptions.LOCK_ENABLED.key(), "true"); |
| 118 | + properties.put(CatalogOptions.LOCK_TYPE.key(), "jdbc"); |
| 119 | + properties.putAll(props); |
| 120 | + JdbcCatalog catalog = |
| 121 | + new JdbcCatalog( |
| 122 | + fileIO, |
| 123 | + "test-jdbc-postgres-catalog", |
| 124 | + Options.fromMap(properties), |
| 125 | + warehouse); |
| 126 | + assertThat(catalog.warehouse()).isEqualTo(warehouse); |
| 127 | + return catalog; |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testAcquireLockFail() throws SQLException, InterruptedException { |
| 132 | + String lockId = "jdbc.testDb.testTable"; |
| 133 | + assertThat(JdbcUtils.acquire(((JdbcCatalog) catalog).getConnections(), lockId, 3000)) |
| 134 | + .isTrue(); |
| 135 | + assertThat(JdbcUtils.acquire(((JdbcCatalog) catalog).getConnections(), lockId, 3000)) |
| 136 | + .isFalse(); |
| 137 | + JdbcUtils.release(((JdbcCatalog) catalog).getConnections(), lockId); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testCleanTimeoutLockAndAcquireLock() throws SQLException, InterruptedException { |
| 142 | + String lockId = "jdbc.testDb.testTable"; |
| 143 | + assertThat(JdbcUtils.acquire(((JdbcCatalog) catalog).getConnections(), lockId, 1000)) |
| 144 | + .isTrue(); |
| 145 | + Thread.sleep(2000); |
| 146 | + assertThat(JdbcUtils.acquire(((JdbcCatalog) catalog).getConnections(), lockId, 1000)) |
| 147 | + .isTrue(); |
| 148 | + JdbcUtils.release(((JdbcCatalog) catalog).getConnections(), lockId); |
| 149 | + } |
| 150 | +} |
0 commit comments