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;
019
020import java.util.Arrays;
021import org.apache.hadoop.hbase.client.replication.ReplicationPeerConfigUtil;
022import org.apache.yetus.audience.InterfaceAudience;
023
024import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
025
026import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
027import org.apache.hadoop.hbase.shaded.protobuf.generated.ReplicationProtos;
028
029/**
030 * Used by synchronous replication. Indicate the state of the current cluster in a synchronous
031 * replication peer. The state may be one of {@link SyncReplicationState#ACTIVE},
032 * {@link SyncReplicationState#DOWNGRADE_ACTIVE} or {@link SyncReplicationState#STANDBY}.
033 * <p>
034 * For asynchronous replication, the state is {@link SyncReplicationState#NONE}.
035 */
036@InterfaceAudience.Public
037public enum SyncReplicationState {
038  NONE(0), ACTIVE(1), DOWNGRADE_ACTIVE(2), STANDBY(3);
039
040  private final byte value;
041
042  private SyncReplicationState(int value) {
043    this.value = (byte) value;
044  }
045
046  public static SyncReplicationState valueOf(int value) {
047    switch (value) {
048      case 0:
049        return NONE;
050      case 1:
051        return ACTIVE;
052      case 2:
053        return DOWNGRADE_ACTIVE;
054      case 3:
055        return STANDBY;
056      default:
057        throw new IllegalArgumentException("Unknown synchronous replication state " + value);
058    }
059  }
060
061  public int value() {
062    return value & 0xFF;
063  }
064
065  public static byte[] toByteArray(SyncReplicationState state) {
066    return ProtobufUtil
067      .prependPBMagic(ReplicationPeerConfigUtil.toSyncReplicationState(state).toByteArray());
068  }
069
070  public static SyncReplicationState parseFrom(byte[] bytes) throws InvalidProtocolBufferException {
071    return ReplicationPeerConfigUtil.toSyncReplicationState(ReplicationProtos.SyncReplicationState
072      .parseFrom(Arrays.copyOfRange(bytes, ProtobufUtil.lengthOfPBMagic(), bytes.length)));
073  }
074}