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;
022
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
025import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher;
026import org.apache.hadoop.hbase.replication.regionserver.SwitchRpcThrottleRemoteCallable;
027
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
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.newBuilder()
065        .setTargetServer(ProtobufUtil.toServerName(targetServer))
066        .setRpcThrottleEnabled(rpcThrottleEnabled).build();
067  }
068
069  @Override
070  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
071    SwitchRpcThrottleRemoteStateData data =
072        serializer.deserialize(SwitchRpcThrottleRemoteStateData.class);
073    targetServer = ProtobufUtil.toServerName(data.getTargetServer());
074    rpcThrottleEnabled = data.getRpcThrottleEnabled();
075  }
076
077  @Override
078  public Optional<RemoteProcedureDispatcher.RemoteOperation> remoteCallBuild(
079      MasterProcedureEnv masterProcedureEnv, ServerName remote) {
080    assert targetServer.equals(remote);
081    return Optional.of(new RSProcedureDispatcher.ServerOperation(this, getProcId(),
082        SwitchRpcThrottleRemoteCallable.class, SwitchRpcThrottleRemoteStateData.newBuilder()
083        .setTargetServer(ProtobufUtil.toServerName(remote))
084        .setRpcThrottleEnabled(rpcThrottleEnabled).build().toByteArray()));
085  }
086
087  @Override
088  public ServerName getServerName() {
089    return targetServer;
090  }
091
092  @Override
093  public boolean hasMetaTableRegion() {
094    return false;
095  }
096
097  @Override
098  public ServerOperationType getServerOperationType() {
099    return ServerOperationType.SWITCH_RPC_THROTTLE;
100  }
101
102  @Override
103  protected void complete(MasterProcedureEnv env, Throwable error) {
104    if (error != null) {
105      LOG.warn("Failed to switch rpc throttle to {} on server {}", rpcThrottleEnabled, targetServer,
106        error);
107      this.succ = false;
108    } else {
109      this.succ = true;
110    }
111  }
112
113  @Override
114  public void toStringClassDetails(StringBuilder sb) {
115    sb.append(getClass().getSimpleName());
116    sb.append(" server=");
117    sb.append(targetServer);
118    sb.append(", rpcThrottleEnabled=");
119    sb.append(rpcThrottleEnabled);
120  }
121}