View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver.wal;
19  
20  
21  import java.util.List;
22  import java.util.concurrent.atomic.AtomicLong;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.HTableDescriptor;
27  import org.apache.hadoop.hbase.KeyValue;
28  
29  /**
30   * A WAL Entry for {@link FSHLog} implementation.  Immutable.
31   * A subclass of {@link HLog.Entry} that carries extra info across the ring buffer such as
32   * region sequence id (we want to use this later, just before we write the WAL to ensure region
33   * edits maintain order).  The extra info added here is not 'serialized' as part of the WALEdit
34   * hence marked 'transient' to underline this fact.  It also adds mechanism so we can wait on
35   * the assign of the region sequence id.  See {@link #stampRegionSequenceId()}.
36   */
37  @InterfaceAudience.Private
38  class FSWALEntry extends HLog.Entry {
39    // The below data members are denoted 'transient' just to highlight these are not persisted;
40    // they are only in memory and held here while passing over the ring buffer.
41    private final transient long sequence;
42    private final transient AtomicLong regionSequenceIdReference;
43    private final transient boolean inMemstore;
44    private final transient HTableDescriptor htd;
45    private final transient HRegionInfo hri;
46    private final transient List<KeyValue> memstoreKVs;
47  
48    FSWALEntry(final long sequence, final HLogKey key, final WALEdit edit,
49        final AtomicLong referenceToRegionSequenceId, final boolean inMemstore,
50        final HTableDescriptor htd, final HRegionInfo hri, List<KeyValue> memstoreKVs) {
51      super(key, edit);
52      this.regionSequenceIdReference = referenceToRegionSequenceId;
53      this.inMemstore = inMemstore;
54      this.htd = htd;
55      this.hri = hri;
56      this.sequence = sequence;
57      this.memstoreKVs = memstoreKVs;
58    }
59  
60    public String toString() {
61      return "sequence=" + this.sequence + ", " + super.toString();
62    };
63  
64    boolean isInMemstore() {
65      return this.inMemstore;
66    }
67  
68    HTableDescriptor getHTableDescriptor() {
69      return this.htd;
70    }
71  
72    HRegionInfo getHRegionInfo() {
73      return this.hri;
74    }
75  
76    /**
77     * @return The sequence on the ring buffer when this edit was added.
78     */
79    long getSequence() {
80      return this.sequence;
81    }
82  
83    /**
84     * Stamp this edit with a region edit/sequence id.
85     * Call when safe to do so: i.e. the context is such that the increment on the passed in
86     * {@link #regionSequenceIdReference} is guaranteed aligned w/ how appends are going into the
87     * WAL.  This method works with {@link #getRegionSequenceId()}.  It will block waiting on this
88     * method to be called.
89     * @return The region edit/sequence id we set for this edit.
90     * @see #getRegionSequenceId()
91     */
92    long stampRegionSequenceId() {
93      long regionSequenceId = this.regionSequenceIdReference.incrementAndGet();
94      if (!this.getEdit().isReplay() && memstoreKVs != null && !memstoreKVs.isEmpty()) {
95        for(KeyValue kv : this.memstoreKVs){
96          kv.setSequenceId(regionSequenceId);
97        }
98      }
99      HLogKey key = getKey();
100     key.setLogSeqNum(regionSequenceId);
101     return regionSequenceId;
102   }
103 }