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.mapreduce.v2.hs;
20 
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Set;
26 
27 import org.apache.hadoop.classification.InterfaceAudience.Private;
28 import org.apache.hadoop.classification.InterfaceStability.Unstable;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.mapreduce.v2.api.MRDelegationTokenIdentifier;
31 import org.apache.hadoop.security.token.delegation.DelegationKey;
32 import org.apache.hadoop.service.AbstractService;
33 
34 @Private
35 @Unstable
36 /**
37  * Base class for history server state storage.
38  * Storage implementations need to implement blocking store and load methods
39  * to actually store and load the state.
40  */
41 public abstract class HistoryServerStateStoreService extends AbstractService {
42 
43   public static class HistoryServerState {
44     Map<MRDelegationTokenIdentifier, Long> tokenState =
45         new HashMap<MRDelegationTokenIdentifier, Long>();
46     Set<DelegationKey> tokenMasterKeyState = new HashSet<DelegationKey>();
47 
getTokenState()48     public Map<MRDelegationTokenIdentifier, Long> getTokenState() {
49       return tokenState;
50     }
51 
getTokenMasterKeyState()52     public Set<DelegationKey> getTokenMasterKeyState() {
53       return tokenMasterKeyState;
54     }
55   }
56 
HistoryServerStateStoreService()57   public HistoryServerStateStoreService() {
58     super(HistoryServerStateStoreService.class.getName());
59   }
60 
61   /**
62    * Initialize the state storage
63    *
64    * @param conf the configuration
65    * @throws IOException
66    */
67   @Override
serviceInit(Configuration conf)68   public void serviceInit(Configuration conf) throws IOException {
69     initStorage(conf);
70   }
71 
72   /**
73    * Start the state storage for use
74    *
75    * @throws IOException
76    */
77   @Override
serviceStart()78   public void serviceStart() throws IOException {
79     startStorage();
80   }
81 
82   /**
83    * Shutdown the state storage.
84    *
85    * @throws IOException
86    */
87   @Override
serviceStop()88   public void serviceStop() throws IOException {
89     closeStorage();
90   }
91 
92   /**
93    * Implementation-specific initialization.
94    *
95    * @param conf the configuration
96    * @throws IOException
97    */
initStorage(Configuration conf)98   protected abstract void initStorage(Configuration conf) throws IOException;
99 
100   /**
101    * Implementation-specific startup.
102    *
103    * @throws IOException
104    */
startStorage()105   protected abstract void startStorage() throws IOException;
106 
107   /**
108    * Implementation-specific shutdown.
109    *
110    * @throws IOException
111    */
closeStorage()112   protected abstract void closeStorage() throws IOException;
113 
114   /**
115    * Load the history server state from the state storage.
116    *
117    * @throws IOException
118    */
loadState()119   public abstract HistoryServerState loadState() throws IOException;
120 
121   /**
122    * Blocking method to store a delegation token along with the current token
123    * sequence number to the state storage.
124    *
125    * Implementations must not return from this method until the token has been
126    * committed to the state store.
127    *
128    * @param tokenId the token to store
129    * @param renewDate the token renewal deadline
130    * @throws IOException
131    */
storeToken(MRDelegationTokenIdentifier tokenId, Long renewDate)132   public abstract void storeToken(MRDelegationTokenIdentifier tokenId,
133       Long renewDate) throws IOException;
134 
135   /**
136    * Blocking method to update the expiration of a delegation token
137    * in the state storage.
138    *
139    * Implementations must not return from this method until the expiration
140    * date of the token has been updated in the state store.
141    *
142    * @param tokenId the token to update
143    * @param renewDate the new token renewal deadline
144    * @throws IOException
145    */
updateToken(MRDelegationTokenIdentifier tokenId, Long renewDate)146   public abstract void updateToken(MRDelegationTokenIdentifier tokenId,
147       Long renewDate) throws IOException;
148 
149   /**
150    * Blocking method to remove a delegation token from the state storage.
151    *
152    * Implementations must not return from this method until the token has been
153    * removed from the state store.
154    *
155    * @param tokenId the token to remove
156    * @throws IOException
157    */
removeToken(MRDelegationTokenIdentifier tokenId)158   public abstract void removeToken(MRDelegationTokenIdentifier tokenId)
159       throws IOException;
160 
161   /**
162    * Blocking method to store a delegation token master key.
163    *
164    * Implementations must not return from this method until the key has been
165    * committed to the state store.
166    *
167    * @param key the master key to store
168    * @throws IOException
169    */
storeTokenMasterKey( DelegationKey key)170   public abstract void storeTokenMasterKey(
171       DelegationKey key) throws IOException;
172 
173   /**
174    * Blocking method to remove a delegation token master key.
175    *
176    * Implementations must not return from this method until the key has been
177    * removed from the state store.
178    *
179    * @param key the master key to remove
180    * @throws IOException
181    */
removeTokenMasterKey(DelegationKey key)182   public abstract void removeTokenMasterKey(DelegationKey key)
183       throws IOException;
184 }
185