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