View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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  //TODO do better encapsulation of SerDe logic from GroupInfoManager and GroupTracker
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      //Overwrite any info stored by table, this takes precedence
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  }