View Javadoc

1   
2   /*
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.regionserver.wal;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.hbase.Coprocessor;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.coprocessor.*;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.conf.Configuration;
30  
31  /**
32   * Implements the coprocessor environment and runtime support for coprocessors
33   * loaded within a {@link FSHLog}.
34   */
35  @InterfaceAudience.Private
36  public class WALCoprocessorHost
37      extends CoprocessorHost<WALCoprocessorHost.WALEnvironment> {
38    
39    /**
40     * Encapsulation of the environment of each coprocessor
41     */
42    static class WALEnvironment extends CoprocessorHost.Environment
43      implements WALCoprocessorEnvironment {
44  
45      private FSHLog wal;
46  
47      @Override
48      public FSHLog getWAL() {
49        return wal;
50      }
51  
52      /**
53       * Constructor
54       * @param implClass - not used
55       * @param impl the coprocessor instance
56       * @param priority chaining priority
57       * @param seq load sequence
58       * @param conf configuration
59       * @param hlog HLog
60       */
61      public WALEnvironment(Class<?> implClass, final Coprocessor impl,
62          final int priority, final int seq, final Configuration conf,
63          final FSHLog hlog) {
64        super(impl, priority, seq, conf);
65        this.wal = hlog;
66      }
67    }
68  
69    FSHLog wal;
70    /**
71     * Constructor
72     * @param log the write ahead log
73     * @param conf the configuration
74     */
75    public WALCoprocessorHost(final FSHLog log, final Configuration conf) {
76      this.wal = log;
77      // load system default cp's from configuration.
78      loadSystemCoprocessors(conf, WAL_COPROCESSOR_CONF_KEY);
79    }
80  
81    @Override
82    public WALEnvironment createEnvironment(final Class<?> implClass,
83        final Coprocessor instance, final int priority, final int seq,
84        final Configuration conf) {
85      return new WALEnvironment(implClass, instance, priority, seq, conf,
86          this.wal);
87    }
88  
89    /**
90     * @param info
91     * @param logKey
92     * @param logEdit
93     * @return true if default behavior should be bypassed, false otherwise
94     * @throws IOException
95     */
96    public boolean preWALWrite(HRegionInfo info, HLogKey logKey, WALEdit logEdit)
97        throws IOException {
98      boolean bypass = false;
99      ObserverContext<WALCoprocessorEnvironment> ctx = null;
100     for (WALEnvironment env: coprocessors) {
101       if (env.getInstance() instanceof
102           org.apache.hadoop.hbase.coprocessor.WALObserver) {
103         ctx = ObserverContext.createAndPrepare(env, ctx);
104         ((org.apache.hadoop.hbase.coprocessor.WALObserver)env.getInstance()).
105             preWALWrite(ctx, info, logKey, logEdit);
106         bypass |= ctx.shouldBypass();
107         if (ctx.shouldComplete()) {
108           break;
109         }
110       }
111     }
112     return bypass;
113   }
114 
115   /**
116    * @param info
117    * @param logKey
118    * @param logEdit
119    * @throws IOException
120    */
121   public void postWALWrite(HRegionInfo info, HLogKey logKey, WALEdit logEdit)
122       throws IOException {
123     ObserverContext<WALCoprocessorEnvironment> ctx = null;
124     for (WALEnvironment env: coprocessors) {
125       if (env.getInstance() instanceof
126           org.apache.hadoop.hbase.coprocessor.WALObserver) {
127         ctx = ObserverContext.createAndPrepare(env, ctx);
128         ((org.apache.hadoop.hbase.coprocessor.WALObserver)env.getInstance()).
129             postWALWrite(ctx, info, logKey, logEdit);
130         if (ctx.shouldComplete()) {
131           break;
132         }
133       }
134     }
135   }
136 }