1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rsgroup;
22
23 import com.google.common.collect.Lists;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.util.List;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.client.Result;
32 import org.apache.hadoop.hbase.client.Scan;
33 import org.apache.hadoop.hbase.client.Table;
34 import org.apache.hadoop.hbase.exceptions.DeserializationException;
35 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
36 import org.apache.hadoop.hbase.protobuf.generated.RSGroupProtos;
37 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
38 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
39 import org.apache.zookeeper.KeeperException;
40
41
42 public class RSGroupSerDe {
43 private static final Log LOG = LogFactory.getLog(RSGroupSerDe.class);
44
45 public RSGroupSerDe() {
46
47 }
48
49 public List<RSGroupInfo> retrieveGroupList(Table groupTable) throws IOException {
50 List<RSGroupInfo> RSGroupInfoList = Lists.newArrayList();
51 for (Result result : groupTable.getScanner(new Scan())) {
52 RSGroupProtos.RSGroupInfo proto =
53 RSGroupProtos.RSGroupInfo.parseFrom(
54 result.getValue(
55 RSGroupInfoManager.META_FAMILY_BYTES,
56 RSGroupInfoManager.META_QUALIFIER_BYTES));
57 RSGroupInfoList.add(ProtobufUtil.toGroupInfo(proto));
58 }
59 return RSGroupInfoList;
60 }
61
62 public List<RSGroupInfo> retrieveGroupList(ZooKeeperWatcher watcher,
63 String groupBasePath) throws IOException {
64 List<RSGroupInfo> RSGroupInfoList = Lists.newArrayList();
65
66 try {
67 if(ZKUtil.checkExists(watcher, groupBasePath) != -1) {
68 for(String znode: ZKUtil.listChildrenAndWatchForNewChildren(watcher, groupBasePath)) {
69 byte[] data = ZKUtil.getData(watcher, ZKUtil.joinZNode(groupBasePath, znode));
70 if(data.length > 0) {
71 ProtobufUtil.expectPBMagicPrefix(data);
72 ByteArrayInputStream bis = new ByteArrayInputStream(
73 data, ProtobufUtil.lengthOfPBMagic(), data.length);
74 RSGroupInfoList.add(ProtobufUtil.toGroupInfo(RSGroupProtos.RSGroupInfo.parseFrom(bis)));
75 }
76 }
77 LOG.debug("Read ZK GroupInfo count:" + RSGroupInfoList.size());
78 }
79 } catch (KeeperException e) {
80 throw new IOException("Failed to read rsGroupZNode",e);
81 } catch (DeserializationException e) {
82 throw new IOException("Failed to read rsGroupZNode",e);
83 } catch (InterruptedException e) {
84 throw new IOException("Failed to read rsGroupZNode",e);
85 }
86 return RSGroupInfoList;
87 }
88 }