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;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.exceptions.DeserializationException;
022import org.apache.hadoop.hbase.util.Bytes;
023import org.apache.yetus.audience.InterfaceAudience;
024
025import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
026import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
027import org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos;
028
029/**
030 * State of a WAL log split during distributed splitting. State is kept up in zookeeper.
031 * Encapsulates protobuf serialization/deserialization so we don't leak generated pb outside of this
032 * class. Used by regionserver and master packages.
033 * <p>
034 * Immutable
035 * @deprecated since 2.4.0 and in 3.0.0, to be removed in 4.0.0, replaced by procedure-based
036 *             distributed WAL splitter, see SplitWALManager
037 */
038@Deprecated
039@InterfaceAudience.Private
040public class SplitLogTask {
041  private final ServerName originServer;
042  private final ZooKeeperProtos.SplitLogTask.State state;
043
044  public static class Unassigned extends SplitLogTask {
045    public Unassigned(final ServerName originServer) {
046      super(originServer, ZooKeeperProtos.SplitLogTask.State.UNASSIGNED);
047    }
048  }
049
050  public static class Owned extends SplitLogTask {
051    public Owned(final ServerName originServer) {
052      super(originServer, ZooKeeperProtos.SplitLogTask.State.OWNED);
053    }
054  }
055
056  public static class Resigned extends SplitLogTask {
057    public Resigned(final ServerName originServer) {
058      super(originServer, ZooKeeperProtos.SplitLogTask.State.RESIGNED);
059    }
060  }
061
062  public static class Done extends SplitLogTask {
063    public Done(final ServerName originServer) {
064      super(originServer, ZooKeeperProtos.SplitLogTask.State.DONE);
065    }
066  }
067
068  public static class Err extends SplitLogTask {
069    public Err(final ServerName originServer) {
070      super(originServer, ZooKeeperProtos.SplitLogTask.State.ERR);
071    }
072  }
073
074  SplitLogTask(final ZooKeeperProtos.SplitLogTask slt) {
075    this.originServer = ProtobufUtil.toServerName(slt.getServerName());
076    this.state = slt.getState();
077  }
078
079  SplitLogTask(final ServerName originServer, final ZooKeeperProtos.SplitLogTask.State state) {
080    this.originServer = originServer;
081    this.state = state;
082  }
083
084  public ServerName getServerName() {
085    return this.originServer;
086  }
087
088  public boolean isUnassigned(final ServerName sn) {
089    return this.originServer.equals(sn) && isUnassigned();
090  }
091
092  public boolean isUnassigned() {
093    return this.state == ZooKeeperProtos.SplitLogTask.State.UNASSIGNED;
094  }
095
096  public boolean isOwned(final ServerName sn) {
097    return this.originServer.equals(sn) && isOwned();
098  }
099
100  public boolean isOwned() {
101    return this.state == ZooKeeperProtos.SplitLogTask.State.OWNED;
102  }
103
104  public boolean isResigned(final ServerName sn) {
105    return this.originServer.equals(sn) && isResigned();
106  }
107
108  public boolean isResigned() {
109    return this.state == ZooKeeperProtos.SplitLogTask.State.RESIGNED;
110  }
111
112  public boolean isDone(final ServerName sn) {
113    return this.originServer.equals(sn) && isDone();
114  }
115
116  public boolean isDone() {
117    return this.state == ZooKeeperProtos.SplitLogTask.State.DONE;
118  }
119
120  public boolean isErr(final ServerName sn) {
121    return this.originServer.equals(sn) && isErr();
122  }
123
124  public boolean isErr() {
125    return this.state == ZooKeeperProtos.SplitLogTask.State.ERR;
126  }
127
128  @Override
129  public String toString() {
130    return this.state.toString() + " " + this.originServer.toString();
131  }
132
133  @Override
134  public boolean equals(Object obj) {
135    if (!(obj instanceof SplitLogTask)) return false;
136    SplitLogTask other = (SplitLogTask) obj;
137    return other.state.equals(this.state) && other.originServer.equals(this.originServer);
138  }
139
140  @Override
141  public int hashCode() {
142    int hash = 7;
143    hash = 31 * hash + this.state.hashCode();
144    return 31 * hash + this.originServer.hashCode();
145  }
146
147  /**
148   * @param data Serialized date to parse.
149   * @return An SplitLogTaskState instance made of the passed <code>data</code> n * @see
150   *         #toByteArray()
151   */
152  public static SplitLogTask parseFrom(final byte[] data) throws DeserializationException {
153    ProtobufUtil.expectPBMagicPrefix(data);
154    try {
155      int prefixLen = ProtobufUtil.lengthOfPBMagic();
156      ZooKeeperProtos.SplitLogTask.Builder builder = ZooKeeperProtos.SplitLogTask.newBuilder();
157      ProtobufUtil.mergeFrom(builder, data, prefixLen, data.length - prefixLen);
158      return new SplitLogTask(builder.build());
159    } catch (IOException e) {
160      throw new DeserializationException(Bytes.toStringBinary(data, 0, 64), e);
161    }
162  }
163
164  /**
165   * @return This instance serialized into a byte array
166   * @see #parseFrom(byte[])
167   */
168  public byte[] toByteArray() {
169    // First create a pb ServerName. Then create a ByteString w/ the TaskState
170    // bytes in it. Finally create a SplitLogTaskState passing in the two
171    // pbs just created.
172    HBaseProtos.ServerName snpb = ProtobufUtil.toServerName(this.originServer);
173    ZooKeeperProtos.SplitLogTask slts =
174      ZooKeeperProtos.SplitLogTask.newBuilder().setServerName(snpb).setState(this.state).build();
175    return ProtobufUtil.prependPBMagic(slts.toByteArray());
176  }
177}