PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.2.0-rc0/hive/external/common/src/java/org/apache/hadoop/hive/common/metrics/MetricsMBeanImpl.java

#
Java | 155 lines | 115 code | 17 blank | 23 comment | 7 complexity | 53fa4807f7bd86f012e952d69ebd017b MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.common.metrics;
  19. import java.io.IOException;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import javax.management.Attribute;
  23. import javax.management.AttributeList;
  24. import javax.management.AttributeNotFoundException;
  25. import javax.management.InvalidAttributeValueException;
  26. import javax.management.MBeanAttributeInfo;
  27. import javax.management.MBeanConstructorInfo;
  28. import javax.management.MBeanException;
  29. import javax.management.MBeanInfo;
  30. import javax.management.MBeanNotificationInfo;
  31. import javax.management.MBeanOperationInfo;
  32. import javax.management.ReflectionException;
  33. public class MetricsMBeanImpl implements MetricsMBean {
  34. Map<String,Object> metricsMap = new HashMap<String,Object>();
  35. MBeanAttributeInfo[] attributeInfos;
  36. boolean dirtyAttributeInfoCache = true;
  37. MBeanConstructorInfo[] ctors = null;
  38. MBeanOperationInfo[] ops = null;
  39. MBeanNotificationInfo[] notifs = null;
  40. @Override
  41. public Object getAttribute(String arg0) throws AttributeNotFoundException,
  42. MBeanException, ReflectionException {
  43. synchronized(metricsMap) {
  44. if (metricsMap.containsKey(arg0)) {
  45. return metricsMap.get(arg0);
  46. } else {
  47. throw new AttributeNotFoundException("Key [" + arg0 + "] not found/tracked");
  48. }
  49. }
  50. }
  51. @Override
  52. public AttributeList getAttributes(String[] arg0) {
  53. AttributeList results = new AttributeList();
  54. synchronized(metricsMap) {
  55. for (String key : arg0) {
  56. results.add(new Attribute(key,metricsMap.get(key)));
  57. }
  58. }
  59. return results;
  60. }
  61. @Override
  62. public MBeanInfo getMBeanInfo() {
  63. if (dirtyAttributeInfoCache) {
  64. synchronized(metricsMap) {
  65. attributeInfos = new MBeanAttributeInfo[metricsMap.size()];
  66. int i = 0;
  67. for (String key : metricsMap.keySet()) {
  68. attributeInfos[i] = new MBeanAttributeInfo(
  69. key, metricsMap.get(key).getClass().getName(), key, true, false, false);
  70. i++;
  71. }
  72. dirtyAttributeInfoCache = false;
  73. }
  74. }
  75. return new MBeanInfo(
  76. this.getClass().getName(), "metrics information",
  77. attributeInfos, ctors, ops, notifs);
  78. }
  79. @Override
  80. public Object invoke(String arg0, Object[] arg1, String[] arg2)
  81. throws MBeanException, ReflectionException {
  82. // no invocations.
  83. return null;
  84. }
  85. @Override
  86. public void setAttribute(Attribute attr) throws AttributeNotFoundException,
  87. InvalidAttributeValueException, MBeanException, ReflectionException {
  88. try {
  89. put(attr.getName(),attr.getValue());
  90. } catch (Exception e) {
  91. throw new MBeanException(e);
  92. }
  93. }
  94. @Override
  95. public AttributeList setAttributes(AttributeList arg0) {
  96. AttributeList attributesSet = new AttributeList();
  97. for (Attribute attr : arg0.asList()) {
  98. try {
  99. setAttribute(attr);
  100. attributesSet.add(attr);
  101. } catch (AttributeNotFoundException e) {
  102. // ignore exception - we simply don't add this attribute
  103. // back in to the resultant set.
  104. } catch (InvalidAttributeValueException e) {
  105. // ditto
  106. } catch (MBeanException e) {
  107. // likewise
  108. } catch (ReflectionException e) {
  109. // and again, one last time.
  110. }
  111. }
  112. return attributesSet;
  113. }
  114. public boolean hasKey(String name) {
  115. synchronized(metricsMap) {
  116. return metricsMap.containsKey(name);
  117. }
  118. }
  119. public void put(String name, Object value) throws IOException {
  120. synchronized(metricsMap) {
  121. if (!metricsMap.containsKey(name)) {
  122. dirtyAttributeInfoCache = true;
  123. }
  124. metricsMap.put(name, value);
  125. }
  126. }
  127. public Object get(String name) throws IOException {
  128. try {
  129. return getAttribute(name);
  130. } catch (AttributeNotFoundException e) {
  131. throw new IOException(e);
  132. } catch (MBeanException e) {
  133. throw new IOException(e);
  134. } catch (ReflectionException e) {
  135. throw new IOException(e);
  136. }
  137. }
  138. }