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.replication.regionserver; 019 020import java.util.Optional; 021import java.util.function.BiPredicate; 022import org.apache.hadoop.hbase.TableName; 023import org.apache.hadoop.hbase.replication.ReplicationPeerImpl; 024import org.apache.hadoop.hbase.replication.ReplicationPeers; 025import org.apache.hadoop.hbase.replication.SyncReplicationState; 026import org.apache.hadoop.hbase.util.Pair; 027import org.apache.yetus.audience.InterfaceAudience; 028 029@InterfaceAudience.Private 030class SyncReplicationPeerInfoProviderImpl implements SyncReplicationPeerInfoProvider { 031 032 private final ReplicationPeers replicationPeers; 033 034 private final SyncReplicationPeerMappingManager mapping; 035 036 SyncReplicationPeerInfoProviderImpl(ReplicationPeers replicationPeers, 037 SyncReplicationPeerMappingManager mapping) { 038 this.replicationPeers = replicationPeers; 039 this.mapping = mapping; 040 } 041 042 @Override 043 public Optional<Pair<String, String>> getPeerIdAndRemoteWALDir(TableName table) { 044 if (table == null) { 045 return Optional.empty(); 046 } 047 String peerId = mapping.getPeerId(table); 048 if (peerId == null) { 049 return Optional.empty(); 050 } 051 ReplicationPeerImpl peer = replicationPeers.getPeer(peerId); 052 if (peer == null) { 053 return Optional.empty(); 054 } 055 Pair<SyncReplicationState, SyncReplicationState> states = 056 peer.getSyncReplicationStateAndNewState(); 057 if ( 058 (states.getFirst() == SyncReplicationState.ACTIVE 059 && states.getSecond() == SyncReplicationState.NONE) 060 || (states.getFirst() == SyncReplicationState.DOWNGRADE_ACTIVE 061 && states.getSecond() == SyncReplicationState.ACTIVE) 062 ) { 063 return Optional.of(Pair.newPair(peerId, peer.getPeerConfig().getRemoteWALDir())); 064 } else { 065 return Optional.empty(); 066 } 067 } 068 069 @Override 070 public boolean checkState(TableName table, 071 BiPredicate<SyncReplicationState, SyncReplicationState> checker) { 072 String peerId = mapping.getPeerId(table); 073 if (peerId == null) { 074 return false; 075 } 076 ReplicationPeerImpl peer = replicationPeers.getPeer(peerId); 077 if (peer == null) { 078 return false; 079 } 080 Pair<SyncReplicationState, SyncReplicationState> states = 081 peer.getSyncReplicationStateAndNewState(); 082 return checker.test(states.getFirst(), states.getSecond()); 083 } 084}