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.io.InterruptedIOException;
022import java.util.List;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.client.ConnectionUtils;
027import org.apache.hadoop.hbase.wal.WALProvider.AsyncWriter;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
033import org.apache.hbase.thirdparty.io.netty.channel.Channel;
034import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
035
036/**
037 * An AsyncFSWAL which writes data to two filesystems.
038 */
039@InterfaceAudience.Private
040public class DualAsyncFSWAL extends AsyncFSWAL {
041
042  private static final Logger LOG = LoggerFactory.getLogger(DualAsyncFSWAL.class);
043
044  private final FileSystem remoteFs;
045
046  private final Path remoteWALDir;
047
048  private volatile boolean skipRemoteWAL = false;
049
050  private volatile boolean markerEditOnly = false;
051
052  public DualAsyncFSWAL(FileSystem fs, FileSystem remoteFs, Path rootDir, Path remoteWALDir,
053    String logDir, String archiveDir, Configuration conf, List<WALActionsListener> listeners,
054    boolean failIfWALExists, String prefix, String suffix, EventLoopGroup eventLoopGroup,
055    Class<? extends Channel> channelClass) throws FailedLogCloseException, IOException {
056    super(fs, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix, suffix,
057      eventLoopGroup, channelClass);
058    this.remoteFs = remoteFs;
059    this.remoteWALDir = remoteWALDir;
060  }
061
062  // will be overridden in testcase
063  protected AsyncWriter createCombinedAsyncWriter(AsyncWriter localWriter,
064    AsyncWriter remoteWriter) {
065    return CombinedAsyncWriter.create(remoteWriter, localWriter);
066  }
067
068  @Override
069  protected AsyncWriter createWriterInstance(Path path) throws IOException {
070    AsyncWriter localWriter = super.createWriterInstance(path);
071    // retry forever if we can not create the remote writer to prevent aborting the RS due to log
072    // rolling error, unless the skipRemoteWal is set to true.
073    // TODO: since for now we only have one thread doing log rolling, this may block the rolling for
074    // other wals
075    Path remoteWAL = new Path(remoteWALDir, path.getName());
076    for (int retry = 0;; retry++) {
077      if (skipRemoteWAL) {
078        return localWriter;
079      }
080      AsyncWriter remoteWriter;
081      try {
082        remoteWriter = createAsyncWriter(remoteFs, remoteWAL);
083      } catch (IOException e) {
084        LOG.warn("create remote writer {} failed, retry = {}", remoteWAL, retry, e);
085        try {
086          Thread.sleep(ConnectionUtils.getPauseTime(100, retry));
087        } catch (InterruptedException ie) {
088          // restore the interrupt state
089          Thread.currentThread().interrupt();
090          Closeables.close(localWriter, true);
091          throw (IOException) new InterruptedIOException().initCause(ie);
092        }
093        continue;
094      }
095      return createCombinedAsyncWriter(localWriter, remoteWriter);
096    }
097  }
098
099  @Override
100  protected boolean markerEditOnly() {
101    return markerEditOnly;
102  }
103
104  // Allow temporarily skipping the creation of remote writer. When failing to write to the remote
105  // dfs cluster, we need to reopen the regions and switch to use the original wal writer. But we
106  // need to write a close marker when closing a region, and if it fails, the whole rs will abort.
107  // So here we need to skip the creation of remote writer and make it possible to write the region
108  // close marker.
109  // Setting markerEdit only to true is for transiting from A to S, where we need to give up writing
110  // any pending wal entries as they will be discarded. The remote cluster will replicated the
111  // correct data back later. We still need to allow writing marker edits such as close region event
112  // to allow closing a region.
113  public void skipRemoteWAL(boolean markerEditOnly) {
114    if (markerEditOnly) {
115      this.markerEditOnly = true;
116    }
117    this.skipRemoteWAL = true;
118  }
119}