001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.replication;
019
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023import java.util.SortedSet;
024
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.util.Pair;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Perform read/write to the replication queue storage.
032 */
033@InterfaceAudience.Private
034public interface ReplicationQueueStorage {
035
036  /**
037   * Remove a replication queue for a given regionserver.
038   * @param serverName the name of the regionserver
039   * @param queueId a String that identifies the queue.
040   */
041  void removeQueue(ServerName serverName, String queueId) throws ReplicationException;
042
043  /**
044   * Add a new WAL file to the given queue for a given regionserver. If the queue does not exist it
045   * is created.
046   * @param serverName the name of the regionserver
047   * @param queueId a String that identifies the queue.
048   * @param fileName name of the WAL
049   */
050  void addWAL(ServerName serverName, String queueId, String fileName) throws ReplicationException;
051
052  /**
053   * Remove an WAL file from the given queue for a given regionserver.
054   * @param serverName the name of the regionserver
055   * @param queueId a String that identifies the queue.
056   * @param fileName name of the WAL
057   */
058  void removeWAL(ServerName serverName, String queueId, String fileName)
059      throws ReplicationException;
060
061  /**
062   * Set the current position for a specific WAL in a given queue for a given regionserver.
063   * @param serverName the name of the regionserver
064   * @param queueId a String that identifies the queue
065   * @param fileName name of the WAL
066   * @param position the current position in the file. Will ignore if less than or equal to 0.
067   * @param lastSeqIds map with {encodedRegionName, sequenceId} pairs for serial replication.
068   */
069  void setWALPosition(ServerName serverName, String queueId, String fileName, long position,
070      Map<String, Long> lastSeqIds) throws ReplicationException;
071
072  /**
073   * Read the max sequence id of the specific region for a given peer. For serial replication, we
074   * need the max sequenced id to decide whether we can push the next entries.
075   * @param encodedRegionName the encoded region name
076   * @param peerId peer id
077   * @return the max sequence id of the specific region for a given peer.
078   */
079  long getLastSequenceId(String encodedRegionName, String peerId) throws ReplicationException;
080
081  /**
082   * Set the max sequence id of a bunch of regions for a given peer. Will be called when setting up
083   * a serial replication peer.
084   * @param peerId peer id
085   * @param lastSeqIds map with {encodedRegionName, sequenceId} pairs for serial replication.
086   */
087  void setLastSequenceIds(String peerId, Map<String, Long> lastSeqIds) throws ReplicationException;
088
089  /**
090   * Remove all the max sequence id record for the given peer.
091   * @param peerId peer id
092   */
093  void removeLastSequenceIds(String peerId) throws ReplicationException;
094
095  /**
096   * Remove the max sequence id record for the given peer and regions.
097   * @param peerId peer id
098   * @param encodedRegionNames the encoded region names
099   */
100  void removeLastSequenceIds(String peerId, List<String> encodedRegionNames)
101      throws ReplicationException;
102
103  /**
104   * Get the current position for a specific WAL in a given queue for a given regionserver.
105   * @param serverName the name of the regionserver
106   * @param queueId a String that identifies the queue
107   * @param fileName name of the WAL
108   * @return the current position in the file
109   */
110  long getWALPosition(ServerName serverName, String queueId, String fileName)
111      throws ReplicationException;
112
113  /**
114   * Get a list of all WALs in the given queue on the given region server.
115   * @param serverName the server name of the region server that owns the queue
116   * @param queueId a String that identifies the queue
117   * @return a list of WALs
118   */
119  List<String> getWALsInQueue(ServerName serverName, String queueId) throws ReplicationException;
120
121  /**
122   * Get a list of all queues for the specified region server.
123   * @param serverName the server name of the region server that owns the set of queues
124   * @return a list of queueIds
125   */
126  List<String> getAllQueues(ServerName serverName) throws ReplicationException;
127
128  /**
129   * Change ownership for the queue identified by queueId and belongs to a dead region server.
130   * @param sourceServerName the name of the dead region server
131   * @param destServerName the name of the target region server
132   * @param queueId the id of the queue
133   * @return the new PeerId and A SortedSet of WALs in its queue
134   */
135  Pair<String, SortedSet<String>> claimQueue(ServerName sourceServerName, String queueId,
136      ServerName destServerName) throws ReplicationException;
137
138  /**
139   * Remove the record of region server if the queue is empty.
140   */
141  void removeReplicatorIfQueueIsEmpty(ServerName serverName) throws ReplicationException;
142
143  /**
144   * Get a list of all region servers that have outstanding replication queues. These servers could
145   * be alive, dead or from a previous run of the cluster.
146   * @return a list of server names
147   */
148  List<ServerName> getListOfReplicators() throws ReplicationException;
149
150  /**
151   * Load all wals in all replication queues. This method guarantees to return a snapshot which
152   * contains all WALs at the start of this call even there is concurrent queue failover. However,
153   * some newly created WALs during the call may not be included.
154   */
155  Set<String> getAllWALs() throws ReplicationException;
156
157  /**
158   * Add a peer to hfile reference queue if peer does not exist.
159   * @param peerId peer cluster id to be added
160   * @throws ReplicationException if fails to add a peer id to hfile reference queue
161   */
162  void addPeerToHFileRefs(String peerId) throws ReplicationException;
163
164  /**
165   * Remove a peer from hfile reference queue.
166   * @param peerId peer cluster id to be removed
167   */
168  void removePeerFromHFileRefs(String peerId) throws ReplicationException;
169
170  /**
171   * Add new hfile references to the queue.
172   * @param peerId peer cluster id to which the hfiles need to be replicated
173   * @param pairs list of pairs of { HFile location in staging dir, HFile path in region dir which
174   *          will be added in the queue }
175   * @throws ReplicationException if fails to add a hfile reference
176   */
177  void addHFileRefs(String peerId, List<Pair<Path, Path>> pairs) throws ReplicationException;
178
179  /**
180   * Remove hfile references from the queue.
181   * @param peerId peer cluster id from which this hfile references needs to be removed
182   * @param files list of hfile references to be removed
183   */
184  void removeHFileRefs(String peerId, List<String> files) throws ReplicationException;
185
186  /**
187   * Get list of all peers from hfile reference queue.
188   * @return a list of peer ids
189   */
190  List<String> getAllPeersFromHFileRefsQueue() throws ReplicationException;
191
192  /**
193   * Get a list of all hfile references in the given peer.
194   * @param peerId a String that identifies the peer
195   * @return a list of hfile references
196   */
197  List<String> getReplicableHFiles(String peerId) throws ReplicationException;
198
199  /**
200   * Load all hfile references in all replication queues. This method guarantees to return a
201   * snapshot which contains all hfile references at the start of this call. However, some newly
202   * created hfile references during the call may not be included.
203   */
204  Set<String> getAllHFileRefs() throws ReplicationException;
205
206  /**
207   * Get full znode name for given region server
208   * @param serverName the name of the region server
209   * @return full znode name
210   */
211  String getRsNode(ServerName serverName);
212}