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