PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/release-0.1-rc2/hive/external/hwi/src/java/org/apache/hadoop/hive/hwi/HWISessionManager.java

#
Java | 222 lines | 124 code | 24 blank | 74 comment | 21 complexity | 18a908ecb98c465ef873271d677f8fc2 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.hwi;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Set;
  22. import java.util.TreeMap;
  23. import java.util.TreeSet;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. /**
  27. * HiveSessionManager is a Runnable started inside a web application context.
  28. * It's basic function is to hold a collection of SessionItem(s). It also works
  29. * as a facade, as jsp clients can not create a Hive Session directly. Hive
  30. * Sessions are long lived, unlike a traditional Query and Block system clients
  31. * set up the query to be started with an instance of this class.
  32. *
  33. */
  34. public class HWISessionManager implements Runnable {
  35. protected static final Log l4j = LogFactory.getLog(HWISessionManager.class
  36. .getName());
  37. private boolean goOn;
  38. private TreeMap<HWIAuth, Set<HWISessionItem>> items;
  39. protected HWISessionManager() {
  40. goOn = true;
  41. items = new TreeMap<HWIAuth, Set<HWISessionItem>>();
  42. }
  43. /**
  44. * This method scans the SessionItem collection. If a SessionItem is in the
  45. * QUERY_SET state that signals that its thread should be started. If the
  46. * SessionItem is in the DESTROY state it should be cleaned up and removed
  47. * from the collection. Currently we are using a sleep. A wait/notify could be
  48. * implemented. Queries will run for a long time, a one second wait on start
  49. * will not be noticed.
  50. *
  51. */
  52. public void run() {
  53. l4j.debug("Entered run() thread has started");
  54. while (goOn) {
  55. l4j.debug("locking items");
  56. synchronized (items) {
  57. for (HWIAuth a : items.keySet()) {
  58. for (HWISessionItem i : items.get(a)) {
  59. if (i.getStatus() == HWISessionItem.WebSessionItemStatus.DESTROY) {
  60. items.get(a).remove(i);
  61. }
  62. if (i.getStatus() == HWISessionItem.WebSessionItemStatus.KILL_QUERY) {
  63. l4j.debug("Killing item: " + i.getSessionName());
  64. i.killIt();
  65. l4j.debug("Killed item: " + i.getSessionName());
  66. items.get(a).remove(i);
  67. }
  68. }
  69. }
  70. } // end sync
  71. try {
  72. Thread.sleep(100);
  73. } catch (InterruptedException ex) {
  74. l4j.error("Could not sleep ", ex);
  75. }
  76. } // end while
  77. l4j.debug("goOn is false. Loop has ended.");
  78. // Cleanup used here to stop all threads
  79. synchronized (items) {
  80. for (HWIAuth a : items.keySet()) {
  81. for (HWISessionItem i : items.get(a)) {
  82. try {
  83. if (i.getStatus() == HWISessionItem.WebSessionItemStatus.QUERY_RUNNING) {
  84. l4j.debug(i.getSessionName() + "Joining ");
  85. i.runnable.join(1000);
  86. l4j.debug(i.getSessionName() + "Joined ");
  87. }
  88. } catch (InterruptedException ex) {
  89. l4j.error(i.getSessionName() + "while joining ", ex);
  90. }
  91. }
  92. }
  93. }
  94. } // end run
  95. protected boolean isGoOn() {
  96. return goOn;
  97. }
  98. protected void setGoOn(boolean goOn) {
  99. this.goOn = goOn;
  100. }
  101. protected TreeMap<HWIAuth, Set<HWISessionItem>> getItems() {
  102. return items;
  103. }
  104. protected void setItems(TreeMap<HWIAuth, Set<HWISessionItem>> items) {
  105. this.items = items;
  106. }
  107. // client methods called from JSP
  108. /**
  109. * Rather then return the actual items we return a list copies. This enforces
  110. * our HWISessionManager by preventing the ability of the client(jsp) to
  111. * create SessionItems.
  112. *
  113. * @return A set of SessionItems this framework manages
  114. */
  115. public ArrayList<HWISessionItem> findAllSessionItems() {
  116. ArrayList<HWISessionItem> otherItems = new ArrayList<HWISessionItem>();
  117. for (HWIAuth a : items.keySet()) {
  118. otherItems.addAll(items.get(a));
  119. }
  120. return otherItems;
  121. }
  122. /**
  123. * Here we handle creating the SessionItem, we do this for the JSP client
  124. * because we need to set parameters the client is not aware of. One such
  125. * parameter is the command line arguments the server was started with.
  126. *
  127. * @param a
  128. * Authenticated user
  129. * @param sessionName
  130. * Represents the session name
  131. * @return a new SessionItem or null if a session with that name already
  132. * exists
  133. */
  134. public HWISessionItem createSession(HWIAuth a, String sessionName) {
  135. l4j.debug("Creating session: " + sessionName);
  136. HWISessionItem si = null;
  137. synchronized (items) {
  138. if (findSessionItemByName(a, sessionName) == null) {
  139. l4j.debug("Initializing session: " + sessionName + " a for "
  140. + a.getUser());
  141. si = new HWISessionItem(a, sessionName);
  142. if (!items.containsKey(a)) {
  143. l4j.debug("SessionList is empty " + a.getUser());
  144. TreeSet<HWISessionItem> list = new TreeSet<HWISessionItem>();
  145. list.add(si);
  146. items.put(a, list);
  147. l4j.debug("Item added " + si.getSessionName() + " for user "
  148. + a.getUser());
  149. } else {
  150. items.get(a).add(si);
  151. l4j.debug("Item added " + si.getSessionName() + " for user "
  152. + a.getUser());
  153. }
  154. } else {
  155. l4j.debug("Creating session: " + sessionName + " already exists "
  156. + a.getUser());
  157. }
  158. }
  159. return si;
  160. }
  161. /**
  162. * Helper method useful when you know the session name you wish to reference.
  163. *
  164. * @param sessionname
  165. * @return A SessionItem matching the sessionname or null if it does not
  166. * exists
  167. */
  168. public HWISessionItem findSessionItemByName(HWIAuth auth, String sessionname) {
  169. Collection<HWISessionItem> sessForUser = items.get(auth);
  170. if (sessForUser == null) {
  171. return null;
  172. }
  173. for (HWISessionItem si : sessForUser) {
  174. if (si.getSessionName().equals(sessionname)) {
  175. return si;
  176. }
  177. }
  178. return null;
  179. }
  180. /**
  181. * Used to list all users that have at least one session.
  182. *
  183. * @return keySet of items all users that have any sessions
  184. */
  185. public Set<HWIAuth> findAllUsersWithSessions() {
  186. return items.keySet();
  187. }
  188. /**
  189. * Used to list all the sessions of a user.
  190. *
  191. * @param auth
  192. * the user being enquired about
  193. * @return all the sessions of that user
  194. */
  195. public Set<HWISessionItem> findAllSessionsForUser(HWIAuth auth) {
  196. return items.get(auth);
  197. }
  198. }