View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.util.concurrent.ConcurrentMap;
22  import java.util.concurrent.ConcurrentSkipListMap;
23  import java.util.concurrent.atomic.AtomicLong;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.util.Bytes;
27  
28  /**
29   * RegionServerAccounting keeps record of some basic real time information about
30   * the Region Server. Currently, it only keeps record the global memstore size. 
31   */
32  @InterfaceAudience.Private
33  public class RegionServerAccounting {
34  
35    private final AtomicLong atomicGlobalMemstoreSize = new AtomicLong(0);
36    
37    // Store the edits size during replaying WAL. Use this to roll back the  
38    // global memstore size once a region opening failed.
39    private final ConcurrentMap<byte[], AtomicLong> replayEditsPerRegion = 
40      new ConcurrentSkipListMap<byte[], AtomicLong>(Bytes.BYTES_COMPARATOR);
41  
42    /**
43     * @return the global Memstore size in the RegionServer
44     */
45    public long getGlobalMemstoreSize() {
46      return atomicGlobalMemstoreSize.get();
47    }
48    
49    /**
50     * @param memStoreSize the Memstore size will be added to 
51     *        the global Memstore size 
52     * @return the global Memstore size in the RegionServer 
53     */
54    public long addAndGetGlobalMemstoreSize(long memStoreSize) {
55      return atomicGlobalMemstoreSize.addAndGet(memStoreSize);
56    }
57    
58    /***
59     * Add memStoreSize to replayEditsPerRegion.
60     * 
61     * @param regionName region name.
62     * @param memStoreSize the Memstore size will be added to replayEditsPerRegion.
63     * @return the replay edits size for the region.
64     */
65    public long addAndGetRegionReplayEditsSize(byte[] regionName, long memStoreSize) {
66      AtomicLong replayEdistsSize = replayEditsPerRegion.get(regionName);
67      if (replayEdistsSize == null) {
68        replayEdistsSize = new AtomicLong(0);
69        replayEditsPerRegion.put(regionName, replayEdistsSize);
70      }
71      return replayEdistsSize.addAndGet(memStoreSize);
72    }
73  
74    /**
75     * Roll back the global MemStore size for a specified region when this region
76     * can't be opened.
77     * 
78     * @param regionName the region which could not open.
79     * @return the global Memstore size in the RegionServer
80     */
81    public long rollbackRegionReplayEditsSize(byte[] regionName) {
82      AtomicLong replayEditsSize = replayEditsPerRegion.get(regionName);
83      long editsSizeLong = 0L;
84      if (replayEditsSize != null) {
85        editsSizeLong = -replayEditsSize.get();
86        clearRegionReplayEditsSize(regionName);
87      }
88      return addAndGetGlobalMemstoreSize(editsSizeLong);
89    }
90  
91    /**
92     * Clear a region from replayEditsPerRegion.
93     * 
94     * @param regionName region name.
95     */
96    public void clearRegionReplayEditsSize(byte[] regionName) {
97      replayEditsPerRegion.remove(regionName);
98    }
99    
100 }