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.regionserver.wal;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import java.util.concurrent.Callable;
024import java.util.concurrent.ExecutorService;
025import java.util.concurrent.Executors;
026import java.util.concurrent.Future;
027import org.apache.hadoop.hbase.util.FutureUtils;
028import org.apache.hadoop.hbase.wal.WAL.Entry;
029import org.apache.hadoop.hbase.wal.WALProvider.Writer;
030import org.apache.yetus.audience.InterfaceAudience;
031
032import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
033import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
034
035@InterfaceAudience.Private
036public final class CombinedWriter extends CombinedWriterBase<Writer> implements Writer {
037
038  private final ImmutableList<ExecutorService> executors;
039
040  private CombinedWriter(ImmutableList<Writer> writers) {
041    super(writers);
042    ImmutableList.Builder<ExecutorService> builder =
043      ImmutableList.builderWithExpectedSize(writers.size() - 1);
044    for (int i = 0; i < writers.size() - 1; i++) {
045      Writer writer = writers.get(i);
046      builder.add(Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
047        .setNameFormat("WAL-Writer-" + writer + "-%d").setDaemon(true).build()));
048    }
049    this.executors = builder.build();
050  }
051
052  private interface Action {
053    void action(Writer writer) throws IOException;
054  }
055
056  private void apply(Action action) throws IOException {
057    List<Future<?>> futures = new ArrayList<>(writers.size() - 1);
058    for (int i = 0; i < writers.size() - 1; i++) {
059      Writer writer = writers.get(i);
060      futures.add(executors.get(i).submit(new Callable<Void>() {
061
062        @Override
063        public Void call() throws Exception {
064          action.action(writer);
065          return null;
066        }
067      }));
068    }
069    action.action(writers.get(writers.size() - 1));
070    for (Future<?> future : futures) {
071      FutureUtils.get(future);
072    }
073  }
074
075  @Override
076  public void sync(boolean forceSync) throws IOException {
077    apply(writer -> writer.sync(forceSync));
078  }
079
080  @Override
081  public void append(Entry entry) throws IOException {
082    apply(writer -> writer.append(entry));
083  }
084
085  @Override
086  public void close() throws IOException {
087    executors.forEach(ExecutorService::shutdown);
088    super.close();
089  }
090
091  public static CombinedWriter create(Writer writer, Writer... writers) {
092    return new CombinedWriter(ImmutableList.<Writer> builder().add(writer).add(writers).build());
093  }
094}