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  package org.apache.hadoop.hbase.regionserver;
19  
20  import java.io.IOException;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.client.Connection;
29  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
30  import org.apache.omid.HBaseShims;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  public class RegionConnectionFactory {
35      public static final String COMMIT_TABLE_ACCESS_ON_COMPACTION_RETRIES_NUMBER = "omid.commit.table.access.on.compaction.retries";
36      public static final String COMMIT_TABLE_ACCESS_ON_COMPACTION_RETRY_PAUSE = "omid.commit.table.access.on.compaction.retry.pause";
37      public static final String COMMIT_TABLE_ACCESS_ON_READ_RETRIES_NUMBER = "omid.commit.table.access.on.read.retries";
38      public static final String COMMIT_TABLE_ACCESS_ON_READ_RETRY_PAUSE = "omid.commit.table.access.on.read.retry.pause";
39  
40      private static final Logger LOG = LoggerFactory.getLogger(RegionConnectionFactory.class);
41      private static final int DEFAULT_COMMIT_TABLE_ACCESS_ON_COMPACTION_RETRIES_NUMBER = 20;
42      private static final int DEFAULT_COMMIT_TABLE_ACCESS_ON_COMPACTION_RETRY_PAUSE = 100;
43      // This setting controls how many retries occur on the region server if an
44      // IOException occurs while trying to access the commit table. Because a
45      // handler thread will be in use while these retries occur and the client
46      // will be blocked waiting, it must not tie up the call for longer than
47      // the client RPC timeout. Otherwise, the client will initiate retries on it's
48      // end, tying up yet another handler thread. It's best if the retries can be
49      // zero, as in that case the handler is released and the retries occur on the
50      // client side. In testing, we've seen NoServerForRegionException occur which
51      // is a DoNotRetryIOException which are not retried on the client. It's not
52      // clear if this is a real issue or a test-only issue.
53      private static final int DEFAULT_COMMIT_TABLE_ACCESS_ON_READ_RETRIES_NUMBER = HBaseShims.getNoRetriesNumber() + 1;
54      private static final int DEFAULT_COMMIT_TABLE_ACCESS_ON_READ_RETRY_PAUSE = 100;
55  
56      private RegionConnectionFactory() {
57      }
58      
59      public static enum ConnectionType {
60          COMPACTION_CONNECTION,
61          READ_CONNECTION,
62          DEFAULT_SERVER_CONNECTION;
63      }
64  
65      private static Map<ConnectionType, Connection> connections =
66              new HashMap<ConnectionType, Connection>();
67  
68      /**
69       * Utility to work around the limitation of the copy constructor
70       * {@link Configuration#Configuration(Configuration)} provided by the {@link Configuration}
71       * class. See https://issues.apache.org/jira/browse/HBASE-18378.
72       * The copy constructor doesn't copy all the config settings, so we need to resort to
73       * iterating through all the settings and setting it on the cloned config.
74       * @param toCopy  configuration to copy
75       * @return
76       */
77      private static Configuration cloneConfig(Configuration toCopy) {
78          Configuration clone = new Configuration();
79          Iterator<Entry<String, String>> iterator = toCopy.iterator();
80          while (iterator.hasNext()) {
81              Entry<String, String> entry = iterator.next();
82              clone.set(entry.getKey(), entry.getValue());
83          }
84          return clone;
85      }
86      
87      public static void shutdown() {
88          synchronized (RegionConnectionFactory.class) {
89              for (Connection connection : connections.values()) {
90                  try {
91                      connection.close();
92                  } catch (IOException e) {
93                      LOG.warn("Unable to close coprocessor connection", e);
94                  }
95              }
96              connections.clear();
97          }
98      }
99  
100 
101     public static Connection getConnection(final ConnectionType connectionType, final RegionCoprocessorEnvironment env) throws IOException {
102         Connection connection = null;
103         if((connection = connections.get(connectionType)) == null) {
104             synchronized (RegionConnectionFactory.class) {
105                 if((connection = connections.get(connectionType)) == null) {
106                     connection = HBaseShims.newServerConnection(getTypeSpecificConfiguration(connectionType, env.getConfiguration()), env);
107                     connections.put(connectionType, connection);
108                     return connection;
109                 }
110             }
111         }
112         return connection;
113     }
114 
115     private static Configuration getTypeSpecificConfiguration(ConnectionType connectionType, Configuration conf) {
116         switch (connectionType) {
117         case COMPACTION_CONNECTION:
118             return getCompactionConfig(conf);
119         case DEFAULT_SERVER_CONNECTION:
120             return conf;
121         case READ_CONNECTION:
122             return getReadConfig(conf);
123         default:
124             return conf;
125         }
126     }
127     
128     private static Configuration getCompactionConfig(Configuration conf) {
129         Configuration compactionConfig = cloneConfig(conf);
130         // lower the number of rpc retries, so we don't hang the compaction
131         compactionConfig.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
132                 conf.getInt(COMMIT_TABLE_ACCESS_ON_COMPACTION_RETRIES_NUMBER,
133                         DEFAULT_COMMIT_TABLE_ACCESS_ON_COMPACTION_RETRIES_NUMBER));
134         compactionConfig.setInt(HConstants.HBASE_CLIENT_PAUSE,
135                 conf.getInt(COMMIT_TABLE_ACCESS_ON_COMPACTION_RETRY_PAUSE,
136                         DEFAULT_COMMIT_TABLE_ACCESS_ON_COMPACTION_RETRY_PAUSE));
137         return compactionConfig;
138     }
139 
140     private static Configuration getReadConfig(Configuration conf) {
141         Configuration compactionConfig = cloneConfig(conf);
142         // lower the number of rpc retries, so we don't hang the compaction
143         compactionConfig.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
144                 conf.getInt(COMMIT_TABLE_ACCESS_ON_READ_RETRIES_NUMBER,
145                         DEFAULT_COMMIT_TABLE_ACCESS_ON_READ_RETRIES_NUMBER));
146         compactionConfig.setInt(HConstants.HBASE_CLIENT_PAUSE,
147                 conf.getInt(COMMIT_TABLE_ACCESS_ON_READ_RETRY_PAUSE,
148                         DEFAULT_COMMIT_TABLE_ACCESS_ON_READ_RETRY_PAUSE));
149         return compactionConfig;
150     }
151 }