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.io.ByteArrayInputStream;
021import java.io.ByteArrayOutputStream;
022import java.io.IOException;
023import java.util.Arrays;
024import org.apache.hadoop.hbase.client.replication.ReplicationPeerConfigUtil;
025import org.apache.hadoop.hbase.protobuf.ProtobufMagic;
026import org.apache.hadoop.hbase.util.Pair;
027import org.apache.yetus.audience.InterfaceAudience;
028
029import org.apache.hbase.thirdparty.com.google.common.io.ByteStreams;
030import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
031
032import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.ReplicationProtos;
034
035/**
036 * Used by synchronous replication. Indicate the state of the current cluster in a synchronous
037 * replication peer. The state may be one of {@link SyncReplicationState#ACTIVE},
038 * {@link SyncReplicationState#DOWNGRADE_ACTIVE} or {@link SyncReplicationState#STANDBY}.
039 * <p>
040 * For asynchronous replication, the state is {@link SyncReplicationState#NONE}.
041 */
042@InterfaceAudience.Public
043public enum SyncReplicationState {
044  NONE(0),
045  ACTIVE(1),
046  DOWNGRADE_ACTIVE(2),
047  STANDBY(3);
048
049  private final byte value;
050
051  private SyncReplicationState(int value) {
052    this.value = (byte) value;
053  }
054
055  public static SyncReplicationState valueOf(int value) {
056    switch (value) {
057      case 0:
058        return NONE;
059      case 1:
060        return ACTIVE;
061      case 2:
062        return DOWNGRADE_ACTIVE;
063      case 3:
064        return STANDBY;
065      default:
066        throw new IllegalArgumentException("Unknown synchronous replication state " + value);
067    }
068  }
069
070  public int value() {
071    return value & 0xFF;
072  }
073
074  public static byte[] toByteArray(SyncReplicationState state) {
075    return ProtobufUtil
076      .prependPBMagic(ReplicationPeerConfigUtil.toSyncReplicationState(state).toByteArray());
077  }
078
079  public static SyncReplicationState parseFrom(byte[] bytes) throws InvalidProtocolBufferException {
080    return ReplicationPeerConfigUtil.toSyncReplicationState(ReplicationProtos.SyncReplicationState
081      .parseFrom(Arrays.copyOfRange(bytes, ProtobufUtil.lengthOfPBMagic(), bytes.length)));
082  }
083
084  public static byte[] toByteArray(SyncReplicationState state, SyncReplicationState newState) {
085    ByteArrayOutputStream out = new ByteArrayOutputStream();
086    try {
087      out.write(ProtobufMagic.PB_MAGIC);
088      ReplicationPeerConfigUtil.toSyncReplicationState(state).writeDelimitedTo(out);
089      ReplicationPeerConfigUtil.toSyncReplicationState(newState).writeDelimitedTo(out);
090    } catch (IOException e) {
091      // should not happen, all in memory operations
092      throw new AssertionError(e);
093    }
094    return out.toByteArray();
095  }
096
097  public static Pair<SyncReplicationState, SyncReplicationState>
098    parseStateAndNewStateFrom(byte[] bytes) throws IOException {
099    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
100    ByteStreams.skipFully(in, ProtobufMagic.lengthOfPBMagic());
101    SyncReplicationState state = ReplicationPeerConfigUtil
102      .toSyncReplicationState(ReplicationProtos.SyncReplicationState.parseDelimitedFrom(in));
103    SyncReplicationState newState = ReplicationPeerConfigUtil
104      .toSyncReplicationState(ReplicationProtos.SyncReplicationState.parseDelimitedFrom(in));
105    return Pair.newPair(state, newState);
106  }
107}