1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.zookeeper;
19
20 import java.io.IOException;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.Abortable;
26 import org.apache.hadoop.hbase.exceptions.DeserializationException;
27 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
28 import org.apache.hadoop.hbase.protobuf.generated.LoadBalancerProtos;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.apache.zookeeper.KeeperException;
31
32
33
34
35 @InterfaceAudience.Private
36 public class LoadBalancerTracker extends ZooKeeperNodeTracker {
37 private static final Log LOG = LogFactory.getLog(LoadBalancerTracker.class);
38
39 public LoadBalancerTracker(ZooKeeperWatcher watcher,
40 Abortable abortable) {
41 super(watcher, watcher.balancerZNode, abortable);
42 }
43
44
45
46
47 public boolean isBalancerOn() {
48 byte [] upData = super.getData(false);
49 try {
50
51 return upData == null || parseFrom(upData).getBalancerOn();
52 } catch (DeserializationException dex) {
53 LOG.error("ZK state for LoadBalancer could not be parsed " + Bytes.toStringBinary(upData));
54
55 return false;
56 }
57 }
58
59
60
61
62
63
64 public void setBalancerOn(boolean balancerOn) throws KeeperException {
65 byte [] upData = toByteArray(balancerOn);
66 try {
67 ZKUtil.setData(watcher, watcher.balancerZNode, upData);
68 } catch(KeeperException.NoNodeException nne) {
69 ZKUtil.createAndWatch(watcher, watcher.balancerZNode, upData);
70 }
71 super.nodeDataChanged(watcher.balancerZNode);
72 }
73
74 private byte [] toByteArray(boolean isBalancerOn) {
75 LoadBalancerProtos.LoadBalancerState.Builder builder =
76 LoadBalancerProtos.LoadBalancerState.newBuilder();
77 builder.setBalancerOn(isBalancerOn);
78 return ProtobufUtil.prependPBMagic(builder.build().toByteArray());
79 }
80
81 private LoadBalancerProtos.LoadBalancerState parseFrom(byte [] pbBytes)
82 throws DeserializationException {
83 ProtobufUtil.expectPBMagicPrefix(pbBytes);
84 LoadBalancerProtos.LoadBalancerState.Builder builder =
85 LoadBalancerProtos.LoadBalancerState.newBuilder();
86 try {
87 int magicLen = ProtobufUtil.lengthOfPBMagic();
88 ProtobufUtil.mergeFrom(builder, pbBytes, magicLen, pbBytes.length - magicLen);
89 } catch (IOException e) {
90 throw new DeserializationException(e);
91 }
92 return builder.build();
93 }
94 }