View Javadoc

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