001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to you under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.quotas;
018
019import java.util.Objects;
020import java.util.concurrent.ConcurrentHashMap;
021import java.util.concurrent.ConcurrentMap;
022
023import org.apache.commons.lang3.builder.HashCodeBuilder;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Connection;
028import org.apache.yetus.audience.InterfaceAudience;
029
030import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
031
032/**
033 * A factory for getting instances of {@link FileArchiverNotifier}.
034 */
035@InterfaceAudience.Private
036public final class FileArchiverNotifierFactoryImpl implements FileArchiverNotifierFactory {
037  private static final FileArchiverNotifierFactoryImpl DEFAULT_INSTANCE =
038      new FileArchiverNotifierFactoryImpl();
039  private static volatile FileArchiverNotifierFactory CURRENT_INSTANCE = DEFAULT_INSTANCE;
040  private final ConcurrentMap<TableName,FileArchiverNotifier> CACHE;
041
042  private FileArchiverNotifierFactoryImpl() {
043    CACHE = new ConcurrentHashMap<>();
044  }
045
046  public static FileArchiverNotifierFactory getInstance() {
047    return CURRENT_INSTANCE;
048  }
049
050  @VisibleForTesting
051  static void setInstance(FileArchiverNotifierFactory inst) {
052    CURRENT_INSTANCE = Objects.requireNonNull(inst);
053  }
054
055  @VisibleForTesting
056  static void reset() {
057    CURRENT_INSTANCE = DEFAULT_INSTANCE;
058  }
059
060  /**
061   * Returns the {@link FileArchiverNotifier} instance for the given {@link TableName}.
062   *
063   * @param tn The table to obtain a notifier for
064   * @return The notifier for the given {@code tablename}.
065   */
066  public FileArchiverNotifier get(Connection conn, Configuration conf, FileSystem fs,
067      TableName tn) {
068    // Ensure that only one instance is exposed to callers
069    return CACHE.computeIfAbsent(tn, key -> new FileArchiverNotifierImpl(conn, conf, fs, key));
070  }
071
072  public int getCacheSize() {
073    return CACHE.size();
074  }
075
076  static class CacheKey {
077    final Connection conn;
078    final Configuration conf;
079    final FileSystem fs;
080    final TableName tn;
081
082    CacheKey(Connection conn, Configuration conf, FileSystem fs, TableName tn) {
083      this.conn = conn;
084      this.conf = conf;
085      this.fs = fs;
086      this.tn = tn;
087    }
088
089    @Override
090    public boolean equals(Object o) {
091      if (!(o instanceof CacheKey)) {
092        return false;
093      }
094      CacheKey other = (CacheKey) o;
095      // TableName should be the only thing differing..
096      return tn.equals(other.tn) && conn.equals(other.conn) && conf.equals(other.conf)
097          && fs.equals(other.fs);
098    }
099
100    @Override
101    public int hashCode() {
102      return new HashCodeBuilder().append(conn).append(conf).append(fs).append(tn).toHashCode();
103    }
104
105    @Override
106    public String toString() {
107      return "CacheKey[TableName=" + tn + "]";
108    }
109  }
110}