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.Optional;
022
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
025import org.apache.hadoop.hbase.master.procedure.PeerProcedureInterface;
026import org.apache.hadoop.hbase.master.procedure.RSProcedureDispatcher.ServerOperation;
027import org.apache.hadoop.hbase.master.procedure.ServerRemoteProcedure;
028import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
029import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher.RemoteOperation;
030import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher.RemoteProcedure;
031import org.apache.hadoop.hbase.replication.regionserver.RefreshPeerCallable;
032import org.apache.yetus.audience.InterfaceAudience;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
037import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.PeerModificationType;
038import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.RefreshPeerParameter;
039import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.RefreshPeerStateData;
040
041@InterfaceAudience.Private
042public class RefreshPeerProcedure extends ServerRemoteProcedure
043    implements PeerProcedureInterface, RemoteProcedure<MasterProcedureEnv, ServerName> {
044
045  private static final Logger LOG = LoggerFactory.getLogger(RefreshPeerProcedure.class);
046
047  private String peerId;
048
049  private PeerOperationType type;
050
051  private int stage;
052
053  public RefreshPeerProcedure() {
054  }
055
056  public RefreshPeerProcedure(String peerId, PeerOperationType type, ServerName targetServer) {
057    this(peerId, type, targetServer, 0);
058  }
059
060  public RefreshPeerProcedure(String peerId, PeerOperationType type, ServerName targetServer,
061      int stage) {
062    this.peerId = peerId;
063    this.type = type;
064    this.targetServer = targetServer;
065    this.stage = stage;
066  }
067
068  @Override
069  public String getPeerId() {
070    return peerId;
071  }
072
073  @Override
074  public PeerOperationType getPeerOperationType() {
075    return PeerOperationType.REFRESH;
076  }
077
078  private static PeerModificationType toPeerModificationType(PeerOperationType type) {
079    switch (type) {
080      case ADD:
081        return PeerModificationType.ADD_PEER;
082      case REMOVE:
083        return PeerModificationType.REMOVE_PEER;
084      case ENABLE:
085        return PeerModificationType.ENABLE_PEER;
086      case DISABLE:
087        return PeerModificationType.DISABLE_PEER;
088      case UPDATE_CONFIG:
089        return PeerModificationType.UPDATE_PEER_CONFIG;
090      case TRANSIT_SYNC_REPLICATION_STATE:
091        return PeerModificationType.TRANSIT_SYNC_REPLICATION_STATE;
092      default:
093        throw new IllegalArgumentException("Unknown type: " + type);
094    }
095  }
096
097  private static PeerOperationType toPeerOperationType(PeerModificationType type) {
098    switch (type) {
099      case ADD_PEER:
100        return PeerOperationType.ADD;
101      case REMOVE_PEER:
102        return PeerOperationType.REMOVE;
103      case ENABLE_PEER:
104        return PeerOperationType.ENABLE;
105      case DISABLE_PEER:
106        return PeerOperationType.DISABLE;
107      case UPDATE_PEER_CONFIG:
108        return PeerOperationType.UPDATE_CONFIG;
109      case TRANSIT_SYNC_REPLICATION_STATE:
110        return PeerOperationType.TRANSIT_SYNC_REPLICATION_STATE;
111      default:
112        throw new IllegalArgumentException("Unknown type: " + type);
113    }
114  }
115
116  @Override
117  public Optional<RemoteOperation> remoteCallBuild(MasterProcedureEnv env, ServerName remote) {
118    assert targetServer.equals(remote);
119    return Optional.of(new ServerOperation(this, getProcId(), RefreshPeerCallable.class,
120        RefreshPeerParameter.newBuilder().setPeerId(peerId).setType(toPeerModificationType(type))
121            .setTargetServer(ProtobufUtil.toServerName(remote)).setStage(stage).build()
122            .toByteArray()));
123  }
124
125  @Override
126  protected void complete(MasterProcedureEnv env, Throwable error) {
127    if (error != null) {
128      LOG.warn("Refresh peer {} for {} on {} failed", peerId, type, targetServer, error);
129      this.succ = false;
130    } else {
131      LOG.info("Refresh peer {} for {} on {} suceeded", peerId, type, targetServer);
132      this.succ = true;
133    }
134  }
135
136  @Override
137  protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException {
138    throw new UnsupportedOperationException();
139  }
140
141  @Override
142  protected boolean abort(MasterProcedureEnv env) {
143    // TODO: no correctness problem if we just ignore this, implement later.
144    return false;
145  }
146
147  @Override
148  protected boolean waitInitialized(MasterProcedureEnv env) {
149    return env.waitInitialized(this);
150  }
151
152  @Override
153  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
154    serializer.serialize(
155      RefreshPeerStateData.newBuilder().setPeerId(peerId).setType(toPeerModificationType(type))
156          .setTargetServer(ProtobufUtil.toServerName(targetServer)).setStage(stage).build());
157  }
158
159  @Override
160  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
161    RefreshPeerStateData data = serializer.deserialize(RefreshPeerStateData.class);
162    peerId = data.getPeerId();
163    type = toPeerOperationType(data.getType());
164    targetServer = ProtobufUtil.toServerName(data.getTargetServer());
165    stage = data.getStage();
166  }
167}