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 org.apache.hadoop.hbase.wal.WALProvider.WriterBase;
022import org.apache.yetus.audience.InterfaceAudience;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
027
028/**
029 * Base class for combined wal writer implementations.
030 */
031@InterfaceAudience.Private
032public class CombinedWriterBase<T extends WriterBase> implements WriterBase {
033
034  private static final Logger LOG = LoggerFactory.getLogger(CombinedWriterBase.class);
035
036  // the order of this list is not critical now as we have already solved the case where writing to
037  // local succeed but remote fail, so implementation should implement concurrent sync to increase
038  // performance
039  protected final ImmutableList<T> writers;
040
041  protected CombinedWriterBase(ImmutableList<T> writers) {
042    this.writers = writers;
043  }
044
045  @Override
046  public void close() throws IOException {
047    Exception error = null;
048    for (T writer : writers) {
049      try {
050        writer.close();
051      } catch (Exception e) {
052        LOG.warn("close writer failed", e);
053        if (error == null) {
054          error = e;
055        }
056      }
057    }
058    if (error != null) {
059      throw new IOException("Failed to close at least one writer, please see the warn log above. "
060        + "The cause is the first exception occurred", error);
061    }
062  }
063
064  @Override
065  public long getLength() {
066    return writers.get(0).getLength();
067  }
068
069  @Override
070  public long getSyncedLength() {
071    return writers.get(0).getSyncedLength();
072  }
073}