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  
19  package org.apache.hadoop.hbase.procedure2.store.wal;
20  
21  import java.io.IOException;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.HashMap;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.classification.InterfaceStability;
30  import org.apache.hadoop.fs.FSDataInputStream;
31  import org.apache.hadoop.hbase.procedure2.Procedure;
32  import org.apache.hadoop.hbase.procedure2.store.ProcedureStoreTracker;
33  import org.apache.hadoop.hbase.protobuf.generated.ProcedureProtos;
34  import org.apache.hadoop.hbase.protobuf.generated.ProcedureProtos.ProcedureWALEntry;
35  
36  /**
37   * Helper class that loads the procedures stored in a WAL
38   */
39  @InterfaceAudience.Private
40  @InterfaceStability.Evolving
41  public class ProcedureWALFormatReader {
42    private static final Log LOG = LogFactory.getLog(ProcedureWALFormatReader.class);
43  
44    private final ProcedureStoreTracker tracker;
45    //private final long compactionLogId;
46  
47    private final Map<Long, Procedure> procedures = new HashMap<Long, Procedure>();
48    private final Map<Long, ProcedureProtos.Procedure> localProcedures =
49      new HashMap<Long, ProcedureProtos.Procedure>();
50  
51    private long maxProcId = 0;
52  
53    public ProcedureWALFormatReader(final ProcedureStoreTracker tracker) {
54      this.tracker = tracker;
55    }
56  
57    public void read(ProcedureWALFile log, ProcedureWALFormat.Loader loader) throws IOException {
58      FSDataInputStream stream = log.getStream();
59      try {
60        boolean hasMore = true;
61        while (hasMore) {
62          ProcedureWALEntry entry = ProcedureWALFormat.readEntry(stream);
63          if (entry == null) {
64            LOG.warn("nothing left to decode. exiting with missing EOF");
65            hasMore = false;
66            break;
67          }
68          switch (entry.getType()) {
69            case INIT:
70              readInitEntry(entry);
71              break;
72            case INSERT:
73              readInsertEntry(entry);
74              break;
75            case UPDATE:
76            case COMPACT:
77              readUpdateEntry(entry);
78              break;
79            case DELETE:
80              readDeleteEntry(entry);
81              break;
82            case EOF:
83              hasMore = false;
84              break;
85            default:
86              throw new CorruptedWALProcedureStoreException("Invalid entry: " + entry);
87          }
88        }
89      } catch (IOException e) {
90        LOG.error("got an exception while reading the procedure WAL: " + log, e);
91        loader.markCorruptedWAL(log, e);
92      }
93  
94      if (localProcedures.isEmpty()) {
95        LOG.info("No active entry found in state log " + log + ". removing it");
96        loader.removeLog(log);
97      } else {
98        Iterator<Map.Entry<Long, ProcedureProtos.Procedure>> itd =
99          localProcedures.entrySet().iterator();
100       while (itd.hasNext()) {
101         Map.Entry<Long, ProcedureProtos.Procedure> entry = itd.next();
102         itd.remove();
103 
104         // Deserialize the procedure
105         Procedure proc = Procedure.convert(entry.getValue());
106         procedures.put(entry.getKey(), proc);
107       }
108 
109       // TODO: Some procedure may be already runnables (see readInitEntry())
110       //       (we can also check the "update map" in the log trackers)
111     }
112   }
113 
114   public Iterator<Procedure> getProcedures() {
115     return procedures.values().iterator();
116   }
117 
118   private void loadEntries(final ProcedureWALEntry entry) {
119     for (ProcedureProtos.Procedure proc: entry.getProcedureList()) {
120       maxProcId = Math.max(maxProcId, proc.getProcId());
121       if (isRequired(proc.getProcId())) {
122         if (LOG.isTraceEnabled()) {
123           LOG.trace("read " + entry.getType() + " entry " + proc.getProcId());
124         }
125         localProcedures.put(proc.getProcId(), proc);
126         tracker.setDeleted(proc.getProcId(), false);
127       }
128     }
129   }
130 
131   private void readInitEntry(final ProcedureWALEntry entry)
132       throws IOException {
133     assert entry.getProcedureCount() == 1 : "Expected only one procedure";
134     // TODO: Make it runnable, before reading other files
135     loadEntries(entry);
136   }
137 
138   private void readInsertEntry(final ProcedureWALEntry entry) throws IOException {
139     assert entry.getProcedureCount() >= 1 : "Expected one or more procedures";
140     loadEntries(entry);
141   }
142 
143   private void readUpdateEntry(final ProcedureWALEntry entry) throws IOException {
144     assert entry.getProcedureCount() == 1 : "Expected only one procedure";
145     loadEntries(entry);
146   }
147 
148   private void readDeleteEntry(final ProcedureWALEntry entry) throws IOException {
149     assert entry.getProcedureCount() == 0 : "Expected no procedures";
150     assert entry.hasProcId() : "expected ProcID";
151     if (LOG.isTraceEnabled()) {
152       LOG.trace("read delete entry " + entry.getProcId());
153     }
154     maxProcId = Math.max(maxProcId, entry.getProcId());
155     localProcedures.remove(entry.getProcId());
156     tracker.setDeleted(entry.getProcId(), true);
157   }
158 
159   private boolean isDeleted(final long procId) {
160     return tracker.isDeleted(procId) == ProcedureStoreTracker.DeleteState.YES;
161   }
162 
163   private boolean isRequired(final long procId) {
164     return !isDeleted(procId) && !procedures.containsKey(procId);
165   }
166 }