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),
039  ACTIVE(1),
040  DOWNGRADE_ACTIVE(2),
041  STANDBY(3);
042
043  private final byte value;
044
045  private SyncReplicationState(int value) {
046    this.value = (byte) value;
047  }
048
049  public static SyncReplicationState valueOf(int value) {
050    switch (value) {
051      case 0:
052        return NONE;
053      case 1:
054        return ACTIVE;
055      case 2:
056        return DOWNGRADE_ACTIVE;
057      case 3:
058        return STANDBY;
059      default:
060        throw new IllegalArgumentException("Unknown synchronous replication state " + value);
061    }
062  }
063
064  public int value() {
065    return value & 0xFF;
066  }
067
068  public static byte[] toByteArray(SyncReplicationState state) {
069    return ProtobufUtil
070      .prependPBMagic(ReplicationPeerConfigUtil.toSyncReplicationState(state).toByteArray());
071  }
072
073  public static SyncReplicationState parseFrom(byte[] bytes) throws InvalidProtocolBufferException {
074    return ReplicationPeerConfigUtil.toSyncReplicationState(ReplicationProtos.SyncReplicationState
075      .parseFrom(Arrays.copyOfRange(bytes, ProtobufUtil.lengthOfPBMagic(), bytes.length)));
076  }
077}