View Javadoc

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