You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by lz...@apache.org on 2022/07/27 01:35:15 UTC
[flink-table-store] branch release-0.2 updated: [FLINK-28692] Check warehouse path in CatalogFactory
This is an automated email from the ASF dual-hosted git repository.
lzljs3620320 pushed a commit to branch release-0.2
in repository https://gitbox.apache.org/repos/asf/flink-table-store.git
The following commit(s) were added to refs/heads/release-0.2 by this push:
new f149a4f3 [FLINK-28692] Check warehouse path in CatalogFactory
f149a4f3 is described below
commit f149a4f349fa2ced986bec42b3d1f0cd27004343
Author: Jingsong Lee <ji...@gmail.com>
AuthorDate: Wed Jul 27 09:33:40 2022 +0800
[FLINK-28692] Check warehouse path in CatalogFactory
This closes #243
---
.../table/store/file/catalog/CatalogFactory.java | 21 +++++++++
.../store/file/catalog/CatalogFactoryTest.java | 55 ++++++++++++++++++++++
2 files changed, 76 insertions(+)
diff --git a/flink-table-store-core/src/main/java/org/apache/flink/table/store/file/catalog/CatalogFactory.java b/flink-table-store-core/src/main/java/org/apache/flink/table/store/file/catalog/CatalogFactory.java
index c140587b..de6c97ab 100644
--- a/flink-table-store-core/src/main/java/org/apache/flink/table/store/file/catalog/CatalogFactory.java
+++ b/flink-table-store-core/src/main/java/org/apache/flink/table/store/file/catalog/CatalogFactory.java
@@ -19,8 +19,12 @@
package org.apache.flink.table.store.file.catalog;
import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
import org.apache.flink.util.Preconditions;
+import java.io.IOException;
+import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
@@ -28,6 +32,7 @@ import java.util.stream.Collectors;
import static org.apache.flink.table.store.CatalogOptions.METASTORE;
import static org.apache.flink.table.store.CatalogOptions.WAREHOUSE;
+import static org.apache.flink.util.Preconditions.checkArgument;
/** Factory to create {@link Catalog}. Each factory should have a unique identifier. */
public interface CatalogFactory {
@@ -69,6 +74,22 @@ public interface CatalogFactory {
.collect(Collectors.joining("\n")));
}
+ try {
+ Path warehousePath = new Path(warehouse);
+ FileSystem fs = warehousePath.getFileSystem();
+ if (fs.exists(warehousePath)) {
+ checkArgument(
+ fs.getFileStatus(warehousePath).isDir(),
+ "The %s path '%s' should be a directory.",
+ WAREHOUSE.key(),
+ warehouse);
+ } else {
+ fs.mkdirs(warehousePath);
+ }
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+
return factories.get(0).create(warehouse, options);
}
}
diff --git a/flink-table-store-core/src/test/java/org/apache/flink/table/store/file/catalog/CatalogFactoryTest.java b/flink-table-store-core/src/test/java/org/apache/flink/table/store/file/catalog/CatalogFactoryTest.java
new file mode 100644
index 00000000..75eaf6bc
--- /dev/null
+++ b/flink-table-store-core/src/test/java/org/apache/flink/table/store/file/catalog/CatalogFactoryTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.flink.table.store.file.catalog;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.table.store.file.utils.FileUtils;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.IOException;
+
+import static org.apache.flink.table.store.CatalogOptions.WAREHOUSE;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Test for {@link CatalogFactory}. */
+public class CatalogFactoryTest {
+
+ @Test
+ public void testAutomaticCreatePath(@TempDir java.nio.file.Path path) {
+ Path root = new Path(path.toUri().toString());
+ Configuration options = new Configuration();
+ options.set(WAREHOUSE, new Path(root, "warehouse").toString());
+ assertThat(CatalogFactory.createCatalog(options).listDatabases()).isEmpty();
+ }
+
+ @Test
+ public void testNotDirectory(@TempDir java.nio.file.Path path) throws IOException {
+ Path root = new Path(path.toUri().toString());
+ Path warehouse = new Path(root, "warehouse");
+ FileUtils.writeFileUtf8(warehouse, "");
+ Configuration options = new Configuration();
+ options.set(WAREHOUSE, warehouse.toString());
+ assertThatThrownBy(() -> CatalogFactory.createCatalog(options))
+ .hasMessageContaining("should be a directory");
+ }
+}