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.zookeeper;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.Abortable;
022import org.apache.hadoop.hbase.exceptions.DeserializationException;
023import org.apache.hadoop.hbase.util.Bytes;
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.zookeeper.KeeperException;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
030import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionNormalizerProtos;
031
032/**
033 * Tracks region normalizer state up in ZK
034 */
035@InterfaceAudience.Private
036public class RegionNormalizerTracker extends ZKNodeTracker {
037  private static final Logger LOG = LoggerFactory.getLogger(RegionNormalizerTracker.class);
038
039  public RegionNormalizerTracker(ZKWatcher watcher, Abortable abortable) {
040    super(watcher, watcher.getZNodePaths().regionNormalizerZNode, abortable);
041  }
042
043  /**
044   * Return true if region normalizer is on, false otherwise
045   */
046  public boolean isNormalizerOn() {
047    byte[] upData = super.getData(false);
048    try {
049      // if data in ZK is null, use default of on.
050      return upData == null || parseFrom(upData).getNormalizerOn();
051    } catch (DeserializationException dex) {
052      LOG
053        .error("ZK state for RegionNormalizer could not be parsed " + Bytes.toStringBinary(upData));
054      // return false to be safe.
055      return false;
056    }
057  }
058
059  /**
060   * Set region normalizer on/off
061   * @param normalizerOn whether normalizer should be on or off
062   * @throws KeeperException if a ZooKeeper operation fails
063   */
064  public void setNormalizerOn(boolean normalizerOn) throws KeeperException {
065    byte[] upData = toByteArray(normalizerOn);
066    try {
067      ZKUtil.setData(watcher, watcher.getZNodePaths().regionNormalizerZNode, upData);
068    } catch (KeeperException.NoNodeException nne) {
069      ZKUtil.createAndWatch(watcher, watcher.getZNodePaths().regionNormalizerZNode, upData);
070    }
071    super.nodeDataChanged(watcher.getZNodePaths().regionNormalizerZNode);
072  }
073
074  private byte[] toByteArray(boolean isNormalizerOn) {
075    RegionNormalizerProtos.RegionNormalizerState.Builder builder =
076      RegionNormalizerProtos.RegionNormalizerState.newBuilder();
077    builder.setNormalizerOn(isNormalizerOn);
078    return ProtobufUtil.prependPBMagic(builder.build().toByteArray());
079  }
080
081  private RegionNormalizerProtos.RegionNormalizerState parseFrom(byte[] pbBytes)
082    throws DeserializationException {
083    ProtobufUtil.expectPBMagicPrefix(pbBytes);
084    RegionNormalizerProtos.RegionNormalizerState.Builder builder =
085      RegionNormalizerProtos.RegionNormalizerState.newBuilder();
086    try {
087      int magicLen = ProtobufUtil.lengthOfPBMagic();
088      ProtobufUtil.mergeFrom(builder, pbBytes, magicLen, pbBytes.length - magicLen);
089    } catch (IOException e) {
090      throw new DeserializationException(e);
091    }
092    return builder.build();
093  }
094}