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.regionserver;
019
020import org.apache.hadoop.hbase.io.MetricsIOSource;
021import org.apache.hadoop.hbase.io.MetricsIOSourceImpl;
022import org.apache.hadoop.hbase.io.MetricsIOWrapper;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * Factory to create MetricsRegionServerSource when given a MetricsRegionServerWrapper
027 */
028@InterfaceAudience.Private
029public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServerSourceFactory {
030
031  @SuppressWarnings("ImmutableEnumChecker")
032  public static enum FactoryStorage {
033    INSTANCE;
034
035    private final Object aggLock = new Object();
036    private MetricsRegionAggregateSourceImpl regionAggImpl;
037    private MetricsUserAggregateSourceImpl userAggImpl;
038    private MetricsTableAggregateSourceImpl tblAggImpl;
039    private MetricsHeapMemoryManagerSourceImpl heapMemMngImpl;
040  }
041
042  private synchronized MetricsRegionAggregateSourceImpl getRegionAggregate() {
043    synchronized (FactoryStorage.INSTANCE.aggLock) {
044      if (FactoryStorage.INSTANCE.regionAggImpl == null) {
045        FactoryStorage.INSTANCE.regionAggImpl = new MetricsRegionAggregateSourceImpl();
046      }
047      return FactoryStorage.INSTANCE.regionAggImpl;
048    }
049  }
050
051  @Override
052  public synchronized MetricsUserAggregateSourceImpl getUserAggregate() {
053    synchronized (FactoryStorage.INSTANCE.aggLock) {
054      if (FactoryStorage.INSTANCE.userAggImpl == null) {
055        FactoryStorage.INSTANCE.userAggImpl = new MetricsUserAggregateSourceImpl();
056      }
057      return FactoryStorage.INSTANCE.userAggImpl;
058    }
059  }
060
061  @Override
062  public synchronized MetricsTableAggregateSourceImpl getTableAggregate() {
063    synchronized (FactoryStorage.INSTANCE.aggLock) {
064      if (FactoryStorage.INSTANCE.tblAggImpl == null) {
065        FactoryStorage.INSTANCE.tblAggImpl = new MetricsTableAggregateSourceImpl();
066      }
067      return FactoryStorage.INSTANCE.tblAggImpl;
068    }
069  }
070
071  @Override
072  public synchronized MetricsHeapMemoryManagerSource getHeapMemoryManager() {
073    synchronized (FactoryStorage.INSTANCE.aggLock) {
074      if (FactoryStorage.INSTANCE.heapMemMngImpl == null) {
075        FactoryStorage.INSTANCE.heapMemMngImpl = new MetricsHeapMemoryManagerSourceImpl();
076      }
077      return FactoryStorage.INSTANCE.heapMemMngImpl;
078    }
079  }
080
081  @Override
082  public synchronized MetricsRegionServerSource
083    createServer(MetricsRegionServerWrapper regionServerWrapper) {
084    return new MetricsRegionServerSourceImpl(regionServerWrapper);
085  }
086
087  @Override
088  public MetricsRegionSource createRegion(MetricsRegionWrapper wrapper) {
089    return new MetricsRegionSourceImpl(wrapper, getRegionAggregate());
090  }
091
092  @Override
093  public MetricsTableSource createTable(String table, MetricsTableWrapperAggregate wrapper) {
094    return new MetricsTableSourceImpl(table, getTableAggregate(), wrapper);
095  }
096
097  @Override
098  public MetricsIOSource createIO(MetricsIOWrapper wrapper) {
099    return new MetricsIOSourceImpl(wrapper);
100  }
101
102  @Override
103  public org.apache.hadoop.hbase.regionserver.MetricsUserSource createUser(String shortUserName) {
104    return new org.apache.hadoop.hbase.regionserver.MetricsUserSourceImpl(shortUserName,
105      getUserAggregate());
106  }
107}