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.master.replication;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Optional;
024
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
027import org.apache.hadoop.hbase.master.procedure.PeerProcedureInterface;
028import org.apache.hadoop.hbase.master.procedure.RSProcedureDispatcher.ServerOperation;
029import org.apache.hadoop.hbase.master.procedure.ServerRemoteProcedure;
030import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
031import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher.RemoteOperation;
032import org.apache.hadoop.hbase.replication.regionserver.ReplaySyncReplicationWALCallable;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
038import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.ReplaySyncReplicationWALParameter;
039import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.SyncReplicationReplayWALRemoteStateData;
040
041/**
042 * A remote procedure which is used to send replaying remote wal work to region server.
043 */
044@InterfaceAudience.Private
045public class SyncReplicationReplayWALRemoteProcedure extends ServerRemoteProcedure
046    implements PeerProcedureInterface {
047
048  private static final Logger LOG =
049    LoggerFactory.getLogger(SyncReplicationReplayWALRemoteProcedure.class);
050
051  private String peerId;
052
053  private List<String> wals;
054
055  public SyncReplicationReplayWALRemoteProcedure() {
056  }
057
058  public SyncReplicationReplayWALRemoteProcedure(String peerId, List<String> wals,
059      ServerName targetServer) {
060    this.peerId = peerId;
061    this.wals = wals;
062    this.targetServer = targetServer;
063  }
064
065  @Override
066  public Optional<RemoteOperation> remoteCallBuild(MasterProcedureEnv env, ServerName remote) {
067    ReplaySyncReplicationWALParameter.Builder builder =
068        ReplaySyncReplicationWALParameter.newBuilder();
069    builder.setPeerId(peerId);
070    wals.stream().forEach(builder::addWal);
071    return Optional
072        .of(new ServerOperation(this, getProcId(), ReplaySyncReplicationWALCallable.class,
073            builder.build().toByteArray()));
074  }
075
076  protected void complete(MasterProcedureEnv env, Throwable error) {
077    if (error != null) {
078      LOG.warn("Replay wals {} on {} failed for peer id={}", wals, targetServer, peerId, error);
079      this.succ = false;
080    } else {
081      truncateWALs(env);
082      LOG.info("Replay wals {} on {} succeed for peer id={}", wals, targetServer, peerId);
083      this.succ = true;
084    }
085  }
086
087  /**
088   * Only truncate wals one by one when task succeed. The parent procedure will check the first wal
089   * length to know whether this task succeed.
090   */
091  private void truncateWALs(MasterProcedureEnv env) {
092    String firstWal = wals.get(0);
093    try {
094      env.getMasterServices().getSyncReplicationReplayWALManager().finishReplayWAL(firstWal);
095    } catch (IOException e) {
096      // As it is idempotent to rerun this task. Just ignore this exception and return.
097      LOG.warn("Failed to truncate wal {} for peer id={}", firstWal, peerId, e);
098      return;
099    }
100    for (int i = 1; i < wals.size(); i++) {
101      String wal = wals.get(i);
102      try {
103        env.getMasterServices().getSyncReplicationReplayWALManager().finishReplayWAL(wal);
104      } catch (IOException e1) {
105        try {
106          // retry
107          env.getMasterServices().getSyncReplicationReplayWALManager().finishReplayWAL(wal);
108        } catch (IOException e2) {
109          // As the parent procedure only check the first wal length. Just ignore this exception.
110          LOG.warn("Failed to truncate wal {} for peer id={}", wal, peerId, e2);
111        }
112      }
113    }
114  }
115
116  @Override
117  protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException {
118    throw new UnsupportedOperationException();
119  }
120
121  @Override
122  protected boolean abort(MasterProcedureEnv env) {
123    return false;
124  }
125
126  @Override
127  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
128    SyncReplicationReplayWALRemoteStateData.Builder builder =
129      SyncReplicationReplayWALRemoteStateData.newBuilder().setPeerId(peerId)
130        .setTargetServer(ProtobufUtil.toServerName(targetServer));
131    wals.stream().forEach(builder::addWal);
132    serializer.serialize(builder.build());
133  }
134
135  @Override
136  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
137    SyncReplicationReplayWALRemoteStateData data =
138      serializer.deserialize(SyncReplicationReplayWALRemoteStateData.class);
139    peerId = data.getPeerId();
140    wals = new ArrayList<>();
141    data.getWalList().forEach(wals::add);
142    targetServer = ProtobufUtil.toServerName(data.getTargetServer());
143  }
144
145  @Override
146  public String getPeerId() {
147    return peerId;
148  }
149
150  @Override
151  public PeerOperationType getPeerOperationType() {
152    return PeerOperationType.SYNC_REPLICATION_REPLAY_WAL_REMOTE;
153  }
154}