PageRenderTime 31ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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