View Javadoc

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.replication;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.UUID;
24  
25  import org.apache.hadoop.hbase.Abortable;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
30  import org.apache.hadoop.hbase.TableDescriptors;
31  import org.apache.hadoop.hbase.wal.WAL.Entry;
32  import org.apache.hadoop.hbase.replication.regionserver.MetricsSource;
33  
34  import com.google.common.util.concurrent.Service;
35  
36  /**
37   * ReplicationEndpoint is a plugin which implements replication
38   * to other HBase clusters, or other systems. ReplicationEndpoint implementation
39   * can be specified at the peer creation time by specifying it
40   * in the {@link ReplicationPeerConfig}. A ReplicationEndpoint is run in a thread
41   * in each region server in the same process.
42   * <p>
43   * ReplicationEndpoint is closely tied to ReplicationSource in a producer-consumer
44   * relation. ReplicationSource is an HBase-private class which tails the logs and manages
45   * the queue of logs plus management and persistence of all the state for replication.
46   * ReplicationEndpoint on the other hand is responsible for doing the actual shipping
47   * and persisting of the WAL entries in the other cluster.
48   */
49  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.REPLICATION)
50  public interface ReplicationEndpoint extends Service, ReplicationPeerConfigListener {
51  
52    @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.REPLICATION)
53    class Context {
54      private final Configuration localConf;
55      private final Configuration conf;
56      private final FileSystem fs;
57      private final TableDescriptors tableDescriptors;
58      private final ReplicationPeer replicationPeer;
59      private final String peerId;
60      private final UUID clusterId;
61      private final MetricsSource metrics;
62      private final Abortable abortable;
63  
64      @InterfaceAudience.Private
65      public Context(
66          final Configuration localConf,
67          final Configuration conf,
68          final FileSystem fs,
69          final String peerId,
70          final UUID clusterId,
71          final ReplicationPeer replicationPeer,
72          final MetricsSource metrics,
73          final TableDescriptors tableDescriptors,
74          final Abortable abortable) {
75        this.localConf = localConf;
76        this.conf = conf;
77        this.fs = fs;
78        this.clusterId = clusterId;
79        this.peerId = peerId;
80        this.replicationPeer = replicationPeer;
81        this.metrics = metrics;
82        this.tableDescriptors = tableDescriptors;
83        this.abortable = abortable;
84      }
85      public Configuration getConfiguration() {
86        return conf;
87      }
88      public Configuration getLocalConfiguration() {
89        return localConf;
90      }
91      public FileSystem getFilesystem() {
92        return fs;
93      }
94      public UUID getClusterId() {
95        return clusterId;
96      }
97      public String getPeerId() {
98        return peerId;
99      }
100     public ReplicationPeerConfig getPeerConfig() {
101       return replicationPeer.getPeerConfig();
102     }
103     public ReplicationPeer getReplicationPeer() {
104       return replicationPeer;
105     }
106     public MetricsSource getMetrics() {
107       return metrics;
108     }
109     public TableDescriptors getTableDescriptors() {
110       return tableDescriptors;
111     }
112     public Abortable getAbortable() { return abortable; }
113   }
114 
115   /**
116    * Initialize the replication endpoint with the given context.
117    * @param context replication context
118    * @throws IOException
119    */
120   void init(Context context) throws IOException;
121 
122   /** Whether or not, the replication endpoint can replicate to it's source cluster with the same
123    * UUID */
124   boolean canReplicateToSameCluster();
125 
126   /**
127    * Returns a UUID of the provided peer id. Every HBase cluster instance has a persisted
128    * associated UUID. If the replication is not performed to an actual HBase cluster (but
129    * some other system), the UUID returned has to uniquely identify the connected target system.
130    * @return a UUID or null if the peer cluster does not exist or is not connected.
131    */
132   UUID getPeerUUID();
133 
134   /**
135    * Returns a WALEntryFilter to use for filtering out WALEntries from the log. Replication
136    * infrastructure will call this filter before sending the edits to shipEdits().
137    * @return a {@link WALEntryFilter} or null.
138    */
139   WALEntryFilter getWALEntryfilter();
140 
141   /**
142    * A context for {@link ReplicationEndpoint#replicate(ReplicateContext)} method.
143    */
144   @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.REPLICATION)
145   static class ReplicateContext {
146     List<Entry> entries;
147     int size;
148     String walGroupId;
149     @InterfaceAudience.Private
150     public ReplicateContext() {
151     }
152 
153     public ReplicateContext setEntries(List<Entry> entries) {
154       this.entries = entries;
155       return this;
156     }
157     public ReplicateContext setSize(int size) {
158       this.size = size;
159       return this;
160     }
161     public ReplicateContext setWalGroupId(String walGroupId) {
162       this.walGroupId = walGroupId;
163       return this;
164     }
165     public List<Entry> getEntries() {
166       return entries;
167     }
168     public int getSize() {
169       return size;
170     }
171     public String getWalGroupId(){
172       return walGroupId;
173     }
174   }
175 
176   /**
177    * Replicate the given set of entries (in the context) to the other cluster.
178    * Can block until all the given entries are replicated. Upon this method is returned,
179    * all entries that were passed in the context are assumed to be persisted in the
180    * target cluster.
181    * @param replicateContext a context where WAL entries and other
182    * parameters can be obtained.
183    */
184   boolean replicate(ReplicateContext replicateContext);
185 
186 }