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