001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.replication.regionserver;
019
020import java.io.File;
021import java.io.IOException;
022import java.net.URL;
023import java.util.HashMap;
024import java.util.Map;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.fs.FileUtil;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseConfiguration;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034/**
035 * This will load all the xml configuration files for the source cluster replication ID from user
036 * configured replication configuration directory.
037 */
038@InterfaceAudience.Private
039public class DefaultSourceFSConfigurationProvider implements SourceFSConfigurationProvider {
040  private static final Logger LOG =
041    LoggerFactory.getLogger(DefaultSourceFSConfigurationProvider.class);
042
043  // Map containing all the source clusters configurations against their replication cluster id
044  private final Map<String, Configuration> sourceClustersConfs = new HashMap<>();
045  private static final String XML = ".xml";
046
047  @Override
048  public Configuration getConf(Configuration sinkConf, String replicationClusterId)
049    throws IOException {
050    if (sourceClustersConfs.get(replicationClusterId) == null) {
051      synchronized (this.sourceClustersConfs) {
052        if (sourceClustersConfs.get(replicationClusterId) == null) {
053          LOG.info("Loading source cluster FS client conf for cluster " + replicationClusterId);
054          // Load only user provided client configurations.
055          Configuration sourceClusterConf = new Configuration(false);
056
057          String replicationConfDir = sinkConf.get(HConstants.REPLICATION_CONF_DIR);
058          if (replicationConfDir == null) {
059            LOG.debug(HConstants.REPLICATION_CONF_DIR + " is not configured.");
060            URL resource = HBaseConfiguration.class.getClassLoader().getResource("hbase-site.xml");
061            if (resource != null) {
062              String path = resource.getPath();
063              replicationConfDir = path.substring(0, path.lastIndexOf("/"));
064            } else {
065              replicationConfDir = System.getenv("HBASE_CONF_DIR");
066            }
067          }
068
069          File confDir = new File(replicationConfDir, replicationClusterId);
070          LOG.info("Loading source cluster " + replicationClusterId
071            + " file system configurations from xml " + "files under directory " + confDir);
072          String[] listofConfFiles = FileUtil.list(confDir);
073          for (String confFile : listofConfFiles) {
074            if (new File(confDir, confFile).isFile() && confFile.endsWith(XML)) {
075              // Add all the user provided client conf files
076              sourceClusterConf.addResource(new Path(confDir.getPath(), confFile));
077            }
078          }
079          this.sourceClustersConfs.put(replicationClusterId, sourceClusterConf);
080        }
081      }
082    }
083    return this.sourceClustersConfs.get(replicationClusterId);
084  }
085
086}