-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce FileIOManager and FileIO implementations for HDFS and Local…
… Storage (#96) Laying foundations for storage part 4: `FileIOManager` and FileIO implementations for `HDFS` and `Local`
- Loading branch information
Showing
12 changed files
with
219 additions
and
3 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
65 changes: 65 additions & 0 deletions
65
...nalcatalog/src/main/java/com/linkedin/openhouse/internal/catalog/fileio/FileIOConfig.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,65 @@ | ||
package com.linkedin.openhouse.internal.catalog.fileio; | ||
|
||
import com.linkedin.openhouse.cluster.storage.StorageManager; | ||
import com.linkedin.openhouse.cluster.storage.StorageType; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.iceberg.hadoop.HadoopFileIO; | ||
import org.apache.iceberg.io.FileIO; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Configures the FileIO beans for storages configured in {@link StorageManager} | ||
* | ||
* <p>Each storage type should have a corresponding FileIO bean defined in this class. The return | ||
* value of the bean is null if the storage type is not configured. The return class of the bean is | ||
* the FileIO implementation for the respective storage type. If conflicting class could be returned | ||
* for the same storage type, the bean name should be annotated with Qualifier to distinguish | ||
* between them. | ||
*/ | ||
@Slf4j | ||
@Configuration | ||
public class FileIOConfig { | ||
|
||
@Autowired StorageManager storageManager; | ||
|
||
/** | ||
* Provides the HdfsFileIO bean for HDFS storage type | ||
* | ||
* @return HdfsFileIO bean for HDFS storage type, or null if HDFS storage type is not configured | ||
*/ | ||
@Bean("HdfsFileIO") | ||
HadoopFileIO provideHdfsFileIO() { | ||
try { | ||
FileSystem fs = | ||
(FileSystem) storageManager.getStorage(StorageType.HDFS).getClient().getNativeClient(); | ||
return new HadoopFileIO(fs.getConf()); | ||
} catch (IllegalArgumentException e) { | ||
// If the HDFS storage type is not configured, return null | ||
// Spring doesn't define the bean if the return value is null | ||
log.debug("HDFS storage type is not configured", e); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Provides the HdfsFileIO bean for Local storage type | ||
* | ||
* @return HdfsFileIO bean for Local storage type, or null if Local storage type is not configured | ||
*/ | ||
@Bean("LocalFileIO") | ||
FileIO provideLocalFileIO() { | ||
try { | ||
FileSystem fs = | ||
(FileSystem) storageManager.getStorage(StorageType.LOCAL).getClient().getNativeClient(); | ||
return new HadoopFileIO(fs.getConf()); | ||
} catch (IllegalArgumentException e) { | ||
// If the Local storage type is not configured, return null | ||
// Spring doesn't define the bean if the return value is null | ||
log.debug("Local storage type is not configured", e); | ||
return null; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...alcatalog/src/main/java/com/linkedin/openhouse/internal/catalog/fileio/FileIOManager.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,51 @@ | ||
package com.linkedin.openhouse.internal.catalog.fileio; | ||
|
||
import static com.linkedin.openhouse.cluster.storage.StorageType.HDFS; | ||
import static com.linkedin.openhouse.cluster.storage.StorageType.LOCAL; | ||
|
||
import com.linkedin.openhouse.cluster.storage.StorageType; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
import org.apache.iceberg.hadoop.HadoopFileIO; | ||
import org.apache.iceberg.io.FileIO; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* This is the main class that provides the FileIO implementation based on the storage type. Each | ||
* storage type should have a corresponding FileIO bean field defined in this class and the | ||
* corresponding FileIO bean should be returned for appropriate storage type in the method {@link | ||
* #getFileIO(StorageType.Type)}. If the storage type is not configured, the method should throw an | ||
* IllegalArgumentException. | ||
*/ | ||
@Component | ||
public class FileIOManager { | ||
|
||
@Autowired(required = false) | ||
@Qualifier("HdfsFileIO") | ||
HadoopFileIO hdfsFileIO; | ||
|
||
@Autowired(required = false) | ||
@Qualifier("LocalFileIO") | ||
FileIO localFileIO; | ||
|
||
/** | ||
* Returns the FileIO implementation for the given storage type. | ||
* | ||
* @param storageType, the storage type for which the FileIO implementation is required | ||
* @return FileIO implementation for the given storage type | ||
* @throws IllegalArgumentException if the storage type is not configured | ||
*/ | ||
public FileIO getFileIO(StorageType.Type storageType) throws IllegalArgumentException { | ||
Supplier<? extends RuntimeException> exceptionSupplier = | ||
() -> new IllegalArgumentException(storageType.getValue() + " is not configured"); | ||
if (HDFS.equals(storageType)) { | ||
return Optional.ofNullable(hdfsFileIO).orElseThrow(exceptionSupplier); | ||
} else if (LOCAL.equals(storageType)) { | ||
return Optional.ofNullable(localFileIO).orElseThrow(exceptionSupplier); | ||
} else { | ||
throw new IllegalArgumentException("FileIO not supported for storage type: " + storageType); | ||
} | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
...talog/src/test/java/com/linkedin/openhouse/internal/catalog/fileio/FileIOManagerTest.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,37 @@ | ||
package com.linkedin.openhouse.internal.catalog.fileio; | ||
|
||
import com.linkedin.openhouse.cluster.storage.StorageType; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@SpringBootTest(classes = FileIOManagerTest.FileIOManagerTestConfig.class) | ||
public class FileIOManagerTest { | ||
|
||
@Autowired FileIOManager fileIOManager; | ||
|
||
@Test | ||
public void testGetLocalFileIO() { | ||
// local storage is configured | ||
Assertions.assertNotNull(fileIOManager.getFileIO(StorageType.LOCAL)); | ||
} | ||
|
||
@Test | ||
public void testGetUndefinedFileIOThrowsException() { | ||
// hdfs storage is not configured | ||
Assertions.assertThrows( | ||
IllegalArgumentException.class, () -> fileIOManager.getFileIO(StorageType.HDFS)); | ||
} | ||
|
||
@Configuration | ||
@ComponentScan( | ||
basePackages = { | ||
"com.linkedin.openhouse.internal.catalog.fileio", | ||
"com.linkedin.openhouse.cluster.storage", | ||
"com.linkedin.openhouse.cluster.configs" | ||
}) | ||
public static class FileIOManagerTestConfig {} | ||
} |
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
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
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
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