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  private PeerOperationType type;
049
050  public RefreshPeerProcedure() {
051  }
052
053  public RefreshPeerProcedure(String peerId, PeerOperationType type, ServerName targetServer) {
054    this.peerId = peerId;
055    this.type = type;
056    this.targetServer = targetServer;
057  }
058
059  @Override
060  public String getPeerId() {
061    return peerId;
062  }
063
064  @Override
065  public PeerOperationType getPeerOperationType() {
066    return PeerOperationType.REFRESH;
067  }
068
069  private static PeerModificationType toPeerModificationType(PeerOperationType type) {
070    switch (type) {
071      case ADD:
072        return PeerModificationType.ADD_PEER;
073      case REMOVE:
074        return PeerModificationType.REMOVE_PEER;
075      case ENABLE:
076        return PeerModificationType.ENABLE_PEER;
077      case DISABLE:
078        return PeerModificationType.DISABLE_PEER;
079      case UPDATE_CONFIG:
080        return PeerModificationType.UPDATE_PEER_CONFIG;
081      default:
082        throw new IllegalArgumentException("Unknown type: " + type);
083    }
084  }
085
086  private static PeerOperationType toPeerOperationType(PeerModificationType type) {
087    switch (type) {
088      case ADD_PEER:
089        return PeerOperationType.ADD;
090      case REMOVE_PEER:
091        return PeerOperationType.REMOVE;
092      case ENABLE_PEER:
093        return PeerOperationType.ENABLE;
094      case DISABLE_PEER:
095        return PeerOperationType.DISABLE;
096      case UPDATE_PEER_CONFIG:
097        return PeerOperationType.UPDATE_CONFIG;
098      default:
099        throw new IllegalArgumentException("Unknown type: " + type);
100    }
101  }
102
103  @Override
104  public Optional<RemoteOperation> remoteCallBuild(MasterProcedureEnv env, ServerName remote) {
105    assert targetServer.equals(remote);
106    return Optional.of(new ServerOperation(this, getProcId(), RefreshPeerCallable.class,
107        RefreshPeerParameter.newBuilder().setPeerId(peerId).setType(toPeerModificationType(type))
108            .setTargetServer(ProtobufUtil.toServerName(remote)).build().toByteArray()));
109  }
110
111  @Override
112  protected void complete(MasterProcedureEnv env, Throwable error) {
113    if (error != null) {
114      LOG.warn("Refresh peer {} for {} on {} failed", peerId, type, targetServer, error);
115      this.succ = false;
116    } else {
117      LOG.info("Refresh peer {} for {} on {} suceeded", peerId, type, targetServer);
118      this.succ = true;
119    }
120  }
121
122  @Override
123  protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException {
124    throw new UnsupportedOperationException();
125  }
126
127  @Override
128  protected boolean abort(MasterProcedureEnv env) {
129    // TODO: no correctness problem if we just ignore this, implement later.
130    return false;
131  }
132
133  @Override
134  protected boolean waitInitialized(MasterProcedureEnv env) {
135    return env.waitInitialized(this);
136  }
137
138  @Override
139  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
140    serializer.serialize(
141      RefreshPeerStateData.newBuilder().setPeerId(peerId).setType(toPeerModificationType(type))
142          .setTargetServer(ProtobufUtil.toServerName(targetServer)).build());
143  }
144
145  @Override
146  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
147    RefreshPeerStateData data = serializer.deserialize(RefreshPeerStateData.class);
148    peerId = data.getPeerId();
149    type = toPeerOperationType(data.getType());
150    targetServer = ProtobufUtil.toServerName(data.getTargetServer());
151  }
152}