CalvinKirs commented on code in PR #2485: URL: https://github.com/apache/incubator-seatunnel/pull/2485#discussion_r953321127
########## seatunnel-engine/seatunnel-engine-storage/checkpoint-storage-plugins/checkpoint-storage-hdfs/src/main/java/org/apache/seatunnel/engine/checkpoint/storage/hdfs/HdfsStorage.java: ########## @@ -0,0 +1,262 @@ +/* + * 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.seatunnel.engine.checkpoint.storage.hdfs; + +import static org.apache.seatunnel.engine.checkpoint.storage.constants.StorageConstants.STORAGE_NAME_SPACE; + +import org.apache.seatunnel.engine.checkpoint.storage.PipelineState; +import org.apache.seatunnel.engine.checkpoint.storage.api.AbstractCheckpointStorage; +import org.apache.seatunnel.engine.checkpoint.storage.exception.CheckpointStorageException; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.LocatedFileStatus; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.RemoteIterator; +import org.apache.hadoop.mapred.JobConf; +import org.apache.hadoop.security.UserGroupInformation; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +@Slf4j +public class HdfsStorage extends AbstractCheckpointStorage { + + public FileSystem fs; + private static final String HADOOP_SECURITY_AUTHENTICATION_KEY = "hadoop.security.authentication"; + + private static final String KERBEROS_KEY = "kerberos"; + + private static final String STORAGE_TMP_SUFFIX = "tmp"; + + public HdfsStorage(Map<String, String> configuration) throws CheckpointStorageException { + this.initStorage(configuration); + } + + @Override + public void initStorage(Map<String, String> configuration) throws CheckpointStorageException { + Configuration hadoopConf = new Configuration(); + if (configuration.containsKey(HdfsConstants.HDFS_DEF_FS_NAME)) { + hadoopConf.set(HdfsConstants.HDFS_DEF_FS_NAME, configuration.get(HdfsConstants.HDFS_DEF_FS_NAME)); + } + if (StringUtils.isNotBlank(configuration.get(STORAGE_NAME_SPACE))) { + setStorageNameSpace(configuration.get(STORAGE_NAME_SPACE)); + } + // todo support other config configurations + if (configuration.containsKey(HdfsConstants.KERBEROS_PRINCIPAL) && configuration.containsKey(HdfsConstants.KERBEROS_KEYTAB_FILE_PATH)) { + String kerberosPrincipal = configuration.get(HdfsConstants.KERBEROS_PRINCIPAL); + String kerberosKeytabFilePath = configuration.get(HdfsConstants.KERBEROS_KEYTAB_FILE_PATH); + if (StringUtils.isNotBlank(kerberosPrincipal) && StringUtils.isNotBlank(kerberosKeytabFilePath)) { + hadoopConf.set(HADOOP_SECURITY_AUTHENTICATION_KEY, KERBEROS_KEY); + authenticateKerberos(kerberosPrincipal, kerberosKeytabFilePath, hadoopConf); + } + } + JobConf jobConf = new JobConf(hadoopConf); + try { + fs = FileSystem.get(jobConf); + } catch (IOException e) { + throw new CheckpointStorageException("Failed to get file system", e); + } + + } + + @Override + public String storeCheckPoint(PipelineState state) throws CheckpointStorageException { + byte[] datas; + try { + datas = serializeCheckPointData(state); + } catch (IOException e) { + throw new CheckpointStorageException("Failed to serialize checkpoint data,state is :" + state, e); + } + Path filePath = new Path(getStorageParentDirectory() + state.getJobId() + "/" + getCheckPointName(state)); + + Path tmpFilePath = new Path(getStorageParentDirectory() + state.getJobId() + "/" + getCheckPointName(state) + STORAGE_TMP_SUFFIX); + try (FSDataOutputStream out = fs.create(tmpFilePath)) { + out.write(datas); + out.hsync(); + out.hflush(); Review Comment: thx~ you are right -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
