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.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Set;
25  
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.CellUtil;
28  import org.apache.hadoop.hbase.HRegionInfo;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.hadoop.hbase.util.CollectionUtils;
34  
35  import com.google.common.collect.Sets;
36  
37  import org.apache.hadoop.hbase.wal.WAL.Entry;
38  import org.apache.hadoop.hbase.wal.WALKey;
39  
40  /**
41   * A WAL Entry for {@link FSHLog} implementation.  Immutable.
42   * A subclass of {@link Entry} that carries extra info across the ring buffer such as
43   * region sequence id (we want to use this later, just before we write the WAL to ensure region
44   * edits maintain order).  The extra info added here is not 'serialized' as part of the WALEdit
45   * hence marked 'transient' to underline this fact.  It also adds mechanism so we can wait on
46   * the assign of the region sequence id.  See #stampRegionSequenceId().
47   */
48  @InterfaceAudience.Private
49  class FSWALEntry extends Entry {
50    // The below data members are denoted 'transient' just to highlight these are not persisted;
51    // they are only in memory and held here while passing over the ring buffer.
52    private final transient long sequence;
53    private final transient boolean inMemstore;
54    private final transient HTableDescriptor htd;
55    private final transient HRegionInfo hri;
56    private final Set<byte[]> familyNames;
57  
58    FSWALEntry(final long sequence, final WALKey key, final WALEdit edit,
59        final HTableDescriptor htd, final HRegionInfo hri, final boolean inMemstore) {
60      super(key, edit);
61      this.inMemstore = inMemstore;
62      this.htd = htd;
63      this.hri = hri;
64      this.sequence = sequence;
65      if (inMemstore) {
66        // construct familyNames here to reduce the work of log sinker.
67        ArrayList<Cell> cells = this.getEdit().getCells();
68        if (CollectionUtils.isEmpty(cells)) {
69          this.familyNames = Collections.<byte[]> emptySet();
70        } else {
71          Set<byte[]> familySet = Sets.newTreeSet(Bytes.BYTES_COMPARATOR);
72          for (Cell cell : cells) {
73            if (!CellUtil.matchingFamily(cell, WALEdit.METAFAMILY)) {
74              familySet.add(CellUtil.cloneFamily(cell));
75            }
76          }
77          this.familyNames = Collections.unmodifiableSet(familySet);
78        }
79      } else {
80        this.familyNames = Collections.<byte[]> emptySet();
81      }
82    }
83  
84    @Override
85    public String toString() {
86      return "sequence=" + this.sequence + ", " + super.toString();
87    };
88  
89    boolean isInMemstore() {
90      return this.inMemstore;
91    }
92  
93    HTableDescriptor getHTableDescriptor() {
94      return this.htd;
95    }
96  
97    HRegionInfo getHRegionInfo() {
98      return this.hri;
99    }
100 
101   /**
102    * @return The sequence on the ring buffer when this edit was added.
103    */
104   long getSequence() {
105     return this.sequence;
106   }
107 
108   /**
109    * Here is where a WAL edit gets its sequenceid.
110    * @param we after HBASE-17471 we already get the mvcc number
111    * in WriteEntry, just stamp the writenumber to cells and walkey
112    * @return The sequenceid we stamped on this edit.
113    * @throws IOException
114    */
115   long stampRegionSequenceId(MultiVersionConcurrencyControl.WriteEntry we) throws IOException {
116     long regionSequenceId = we.getWriteNumber();
117     if (!this.getEdit().isReplay() && inMemstore) {
118       for (Cell c : getEdit().getCells()) {
119         CellUtil.setSequenceId(c, regionSequenceId);
120       }
121     }
122     getKey().setWriteEntry(we);
123     return regionSequenceId;
124   }
125 
126   /**
127    * @return the family names which are effected by this edit.
128    */
129   Set<byte[]> getFamilyNames() {
130     return familyNames;
131   }
132 }