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.wal;
019
020import java.util.HashMap;
021import java.util.Map;
022import org.apache.commons.lang3.StringUtils;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.util.Pair;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
028import org.apache.hbase.thirdparty.io.netty.channel.Channel;
029import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
030
031/**
032 * Helper class for passing netty event loop config to {@link AsyncFSWALProvider}.
033 * @since 2.0.0
034 */
035@InterfaceAudience.Private
036public final class NettyAsyncFSWALConfigHelper {
037
038  private static final String EVENT_LOOP_CONFIG = "hbase.wal.async.event-loop.config";
039
040  private static final String CONFIG_NAME = "global-event-loop";
041
042  private static final Map<String,
043    Pair<EventLoopGroup, Class<? extends Channel>>> EVENT_LOOP_CONFIG_MAP = new HashMap<>();
044
045  /**
046   * Set the EventLoopGroup and channel class for {@code AsyncFSWALProvider}.
047   */
048  public static void setEventLoopConfig(Configuration conf, EventLoopGroup group,
049    Class<? extends Channel> channelClass) {
050    Preconditions.checkNotNull(group, "group is null");
051    Preconditions.checkNotNull(channelClass, "channel class is null");
052    conf.set(EVENT_LOOP_CONFIG, CONFIG_NAME);
053    EVENT_LOOP_CONFIG_MAP.put(CONFIG_NAME,
054      Pair.<EventLoopGroup, Class<? extends Channel>> newPair(group, channelClass));
055  }
056
057  static Pair<EventLoopGroup, Class<? extends Channel>> getEventLoopConfig(Configuration conf) {
058    String name = conf.get(EVENT_LOOP_CONFIG);
059    if (StringUtils.isBlank(name)) {
060      return null;
061    }
062    return EVENT_LOOP_CONFIG_MAP.get(name);
063  }
064
065  private NettyAsyncFSWALConfigHelper() {
066  }
067}