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;
20
21 import java.io.IOException;
22
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.classification.InterfaceStability;
25 import org.apache.hadoop.hbase.ProcedureInfo;
26 import org.apache.hadoop.hbase.procedure2.Procedure;
27
28 /**
29 * The ProcedureStore is used by the executor to persist the state of each procedure execution.
30 * This allows to resume the execution of pending/in-progress procedures in case
31 * of machine failure or service shutdown.
32 */
33 @InterfaceAudience.Private
34 @InterfaceStability.Evolving
35 public interface ProcedureStore {
36 /**
37 * Store listener interface.
38 * The main process should register a listener and respond to the store events.
39 */
40 public interface ProcedureStoreListener {
41 /**
42 * triggered when the store sync is completed.
43 */
44 void postSync();
45
46 /**
47 * triggered when the store is not able to write out data.
48 * the main process should abort.
49 */
50 void abortProcess();
51 }
52
53 /**
54 * An Iterator over a collection of Procedure
55 */
56 public interface ProcedureIterator {
57 /**
58 * Reset the Iterator by seeking to the beginning of the list.
59 */
60 void reset();
61
62 /**
63 * Returns true if the iterator has more elements.
64 * (In other words, returns true if next() would return a Procedure
65 * rather than throwing an exception.)
66 * @return true if the iterator has more procedures
67 */
68 boolean hasNext();
69
70 /**
71 * @return true if the iterator next element is a completed procedure.
72 */
73 boolean isNextCompleted();
74
75 /**
76 * Skip the next procedure
77 */
78 void skipNext();
79
80 /**
81 * Returns the next procedure in the iteration.
82 * @throws IOException if there was an error fetching/deserializing the procedure
83 * @return the next procedure in the iteration.
84 */
85 Procedure nextAsProcedure() throws IOException;
86
87 /**
88 * @return the next procedure in the iteration as ProcedureInfo.
89 */
90 ProcedureInfo nextAsProcedureInfo();
91 }
92
93 /**
94 * Interface passed to the ProcedureStore.load() method to handle the store-load events.
95 */
96 public interface ProcedureLoader {
97 /**
98 * Called by ProcedureStore.load() to notify about the maximum proc-id in the store.
99 * @param maxProcId the highest proc-id in the store
100 */
101 void setMaxProcId(long maxProcId);
102
103 /**
104 * Called by the ProcedureStore.load() every time a set of procedures are ready to be executed.
105 * The ProcedureIterator passed to the method, has the procedure sorted in replay-order.
106 * @param procIter iterator over the procedures ready to be added to the executor.
107 */
108 void load(ProcedureIterator procIter) throws IOException;
109
110 /**
111 * Called by the ProcedureStore.load() in case we have procedures not-ready to be added to
112 * the executor, which probably means they are corrupted since some information/link is missing.
113 * @param procIter iterator over the procedures not ready to be added to the executor, corrupted
114 */
115 void handleCorrupted(ProcedureIterator procIter) throws IOException;
116 }
117
118 /**
119 * Add the listener to the notification list.
120 * @param listener The AssignmentListener to register
121 */
122 void registerListener(ProcedureStoreListener listener);
123
124 /**
125 * Remove the listener from the notification list.
126 * @param listener The AssignmentListener to unregister
127 * @return true if the listner was in the list and it was removed, otherwise false.
128 */
129 boolean unregisterListener(ProcedureStoreListener listener);
130
131 /**
132 * Start/Open the procedure store
133 * @param numThreads
134 */
135 void start(int numThreads) throws IOException;
136
137 /**
138 * Stop/Close the procedure store
139 * @param abort true if the stop is an abort
140 */
141 void stop(boolean abort);
142
143 /**
144 * @return true if the store is running, otherwise false.
145 */
146 boolean isRunning();
147
148 /**
149 * @return the number of threads/slots passed to start()
150 */
151 int getNumThreads();
152
153 /**
154 * Acquire the lease for the procedure store.
155 */
156 void recoverLease() throws IOException;
157
158 /**
159 * Load the Procedures in the store.
160 * @param loader the ProcedureLoader that will handle the store-load events
161 */
162 void load(ProcedureLoader loader) throws IOException;
163
164 /**
165 * When a procedure is submitted to the executor insert(proc, null) will be called.
166 * 'proc' has a 'RUNNABLE' state and the initial information required to start up.
167 *
168 * When a procedure is executed and it returns children insert(proc, subprocs) will be called.
169 * 'proc' has a 'WAITING' state and an update state.
170 * 'subprocs' are the children in 'RUNNABLE' state with the initial information.
171 *
172 * @param proc the procedure to serialize and write to the store.
173 * @param subprocs the newly created child of the proc.
174 */
175 void insert(Procedure proc, Procedure[] subprocs);
176
177 /**
178 * The specified procedure was executed,
179 * and the new state should be written to the store.
180 * @param proc the procedure to serialize and write to the store.
181 */
182 void update(Procedure proc);
183
184 /**
185 * The specified procId was removed from the executor,
186 * due to completion, abort or failure.
187 * The store implementor should remove all the information about the specified procId.
188 * @param procId the ID of the procedure to remove.
189 */
190 void delete(long procId);
191 }