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.procedure;
019
020import java.io.IOException;
021import java.util.Optional;
022import org.apache.hadoop.hbase.ServerName;
023import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
024import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher;
025import org.apache.hadoop.hbase.replication.regionserver.SwitchRpcThrottleRemoteCallable;
026import org.apache.hadoop.hbase.util.ForeignExceptionUtil;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
032import org.apache.hadoop.hbase.shaded.protobuf.generated.ErrorHandlingProtos;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.SwitchRpcThrottleRemoteStateData;
034
035/**
036 * The procedure to switch rpc throttle on region server
037 */
038@InterfaceAudience.Private
039public class SwitchRpcThrottleRemoteProcedure extends ServerRemoteProcedure
040  implements ServerProcedureInterface {
041
042  private static final Logger LOG = LoggerFactory.getLogger(SwitchRpcThrottleRemoteProcedure.class);
043  private boolean rpcThrottleEnabled;
044
045  public SwitchRpcThrottleRemoteProcedure() {
046  }
047
048  public SwitchRpcThrottleRemoteProcedure(ServerName serverName, boolean rpcThrottleEnabled) {
049    this.targetServer = serverName;
050    this.rpcThrottleEnabled = rpcThrottleEnabled;
051  }
052
053  @Override
054  protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException {
055  }
056
057  @Override
058  protected boolean abort(MasterProcedureEnv env) {
059    return false;
060  }
061
062  @Override
063  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
064    SwitchRpcThrottleRemoteStateData.Builder builder =
065      SwitchRpcThrottleRemoteStateData.newBuilder();
066    builder.setTargetServer(ProtobufUtil.toServerName(targetServer))
067      .setRpcThrottleEnabled(rpcThrottleEnabled).setState(state).build();
068    if (this.remoteError != null) {
069      ErrorHandlingProtos.ForeignExceptionMessage fem =
070        ForeignExceptionUtil.toProtoForeignException(remoteError);
071      builder.setError(fem);
072    }
073    serializer.serialize(builder.build());
074  }
075
076  @Override
077  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
078    SwitchRpcThrottleRemoteStateData data =
079      serializer.deserialize(SwitchRpcThrottleRemoteStateData.class);
080    targetServer = ProtobufUtil.toServerName(data.getTargetServer());
081    rpcThrottleEnabled = data.getRpcThrottleEnabled();
082    state = data.getState();
083    if (data.hasError()) {
084      this.remoteError = ForeignExceptionUtil.toException(data.getError());
085    }
086  }
087
088  @Override
089  public Optional<RemoteProcedureDispatcher.RemoteOperation>
090    remoteCallBuild(MasterProcedureEnv masterProcedureEnv, ServerName remote) {
091    assert targetServer.equals(remote);
092    return Optional.of(new RSProcedureDispatcher.ServerOperation(this, getProcId(),
093      SwitchRpcThrottleRemoteCallable.class,
094      SwitchRpcThrottleRemoteStateData.newBuilder()
095        .setTargetServer(ProtobufUtil.toServerName(remote))
096        .setRpcThrottleEnabled(rpcThrottleEnabled).build().toByteArray(),
097      masterProcedureEnv.getMasterServices().getMasterActiveTime()));
098  }
099
100  @Override
101  public ServerName getServerName() {
102    return targetServer;
103  }
104
105  @Override
106  public boolean hasMetaTableRegion() {
107    return false;
108  }
109
110  @Override
111  public ServerOperationType getServerOperationType() {
112    return ServerOperationType.SWITCH_RPC_THROTTLE;
113  }
114
115  @Override
116  protected boolean complete(MasterProcedureEnv env, Throwable error) {
117    if (error != null) {
118      LOG.warn("Failed to switch rpc throttle to {} on server {}", rpcThrottleEnabled, targetServer,
119        error);
120      return false;
121    } else {
122      return true;
123    }
124  }
125
126  @Override
127  public void toStringClassDetails(StringBuilder sb) {
128    sb.append(getClass().getSimpleName());
129    sb.append(" server=");
130    sb.append(targetServer);
131    sb.append(", rpcThrottleEnabled=");
132    sb.append(rpcThrottleEnabled);
133  }
134}