View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * 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, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.List;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.Server;
29  
30  /**
31   * Executes region merge as a "transaction". It is similar with
32   * SplitTransaction. Call {@link #prepare(RegionServerServices)} to setup the
33   * transaction, {@link #execute(Server, RegionServerServices)} to run the
34   * transaction and {@link #rollback(Server, RegionServerServices)} to cleanup if
35   * execute fails.
36   * 
37   * <p>Here is an example of how you would use this interface:
38   * <pre>
39   *  RegionMergeTransactionFactory factory = new RegionMergeTransactionFactory(conf);
40   *  RegionMergeTransaction mt = factory.create(parent, midKey)
41   *    .registerTransactionListener(new TransactionListener() {
42   *       public void transition(RegionMergeTransaction transaction,
43   *         RegionMergeTransactionPhase from, RegionMergeTransactionPhase to) throws IOException {
44   *         // ...
45   *       }
46   *       public void rollback(RegionMergeTransaction transaction,
47   *         RegionMergeTransactionPhase from, RegionMergeTransactionPhase to) {
48   *         // ...
49   *       }
50   *    });
51   *  if (!mt.prepare()) return;
52   *  try {
53   *    mt.execute(server, services);
54   *  } catch (IOException ioe) {
55   *    try {
56   *      mt.rollback(server, services);
57   *      return;
58   *    } catch (RuntimeException e) {
59   *      // abort the server
60   *    }
61   *  }
62   * </Pre>
63   * <p>A merge transaction is not thread safe.  Callers must ensure a split is run by
64   * one thread only.
65   */
66  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
67  @InterfaceStability.Evolving
68  public interface RegionMergeTransaction {
69    /**
70     * Each enum is a step in the merge transaction.
71     */
72    enum RegionMergeTransactionPhase {
73      STARTED,
74      /**
75       * Prepared
76       */
77      PREPARED,
78      /**
79       * Set region as in transition, set it into MERGING state.
80       */
81      SET_MERGING,
82      /**
83       * We created the temporary merge data directory.
84       */
85      CREATED_MERGE_DIR,
86      /**
87       * Closed the merging region A.
88       */
89      CLOSED_REGION_A,
90      /**
91       * The merging region A has been taken out of the server's online regions list.
92       */
93      OFFLINED_REGION_A,
94      /**
95       * Closed the merging region B.
96       */
97      CLOSED_REGION_B,
98      /**
99       * The merging region B has been taken out of the server's online regions list.
100      */
101     OFFLINED_REGION_B,
102     /**
103      * Started in on creation of the merged region.
104      */
105     STARTED_MERGED_REGION_CREATION,
106     /**
107      * Point of no return. If we got here, then transaction is not recoverable
108      * other than by crashing out the regionserver.
109      */
110     PONR,
111     /**
112      * Completed
113      */
114     COMPLETED
115   }
116 
117   /**
118    * Split transaction journal entry
119    */
120   public interface JournalEntry {
121 
122     /** @return the completed phase marked by this journal entry */
123     RegionMergeTransactionPhase getPhase();
124 
125     /** @return the time of phase completion */
126     long getTimeStamp();
127   }
128 
129   /**
130    * Split transaction listener
131    */
132   public interface TransactionListener {
133 
134     /**
135      * Invoked when transitioning forward from one transaction phase to another
136      * @param transaction the transaction
137      * @param from the current phase
138      * @param to the next phase
139      * @throws IOException listener can throw this to abort
140      */
141     void transition(RegionMergeTransaction transaction, RegionMergeTransactionPhase from,
142         RegionMergeTransactionPhase to) throws IOException;
143 
144     /**
145      * Invoked when rolling back a transaction from one transaction phase to the
146      * previous
147      * @param transaction the transaction
148      * @param from the current phase
149      * @param to the previous phase
150      */
151     void rollback(RegionMergeTransaction transaction, RegionMergeTransactionPhase from,
152         RegionMergeTransactionPhase to);
153   }
154 
155   /**
156    * Check merge inputs and prepare the transaction.
157    * @param services
158    * @return <code>true</code> if the regions are mergeable else
159    *         <code>false</code> if they are not (e.g. its already closed, etc.).
160    * @throws IOException 
161    */
162   boolean prepare(RegionServerServices services) throws IOException;
163 
164   /**
165    * Run the transaction.
166    * @param server Hosting server instance. Can be null when testing
167    * @param services Used to online/offline regions.
168    * @throws IOException If thrown, transaction failed. Call
169    *           {@link #rollback(Server, RegionServerServices)}
170    * @return merged region
171    * @throws IOException
172    * @see #rollback(Server, RegionServerServices)
173    */
174   Region execute(Server server, RegionServerServices services) throws IOException;
175 
176   /**
177    * Roll back a failed transaction
178    * @param server Hosting server instance (May be null when testing).
179    * @param services Services of regionserver, used to online regions.
180    * @throws IOException If thrown, rollback failed. Take drastic action.
181    * @return True if we successfully rolled back, false if we got to the point
182    *         of no return and so now need to abort the server to minimize
183    *         damage.
184    */
185   boolean rollback(Server server, RegionServerServices services) throws IOException;
186 
187   /**
188    * Register a listener for transaction preparation, execution, and possibly
189    * rollback phases.
190    * <p>A listener can abort a transaction by throwing an exception. 
191    * @param listener the listener
192    * @return 'this' for chaining
193    */
194   RegionMergeTransaction registerTransactionListener(TransactionListener listener);
195 
196   /** @return merged region info */
197   HRegionInfo getMergedRegionInfo();
198 
199   /**
200    * Get the journal for the transaction.
201    * <p>Journal entries are an opaque type represented as JournalEntry. They can
202    * also provide useful debugging information via their toString method.
203    * @return the transaction journal
204    */
205   List<JournalEntry> getJournal();
206 
207   /**
208    * Get the Server running the transaction or rollback
209    * @return server instance
210    */
211   Server getServer();
212 
213   /**
214    * Get the RegonServerServices of the server running the transaction or rollback
215    * @return region server services
216    */
217   RegionServerServices getRegionServerServices();
218 }