| private final Map scannerMap = new ConcurrentHashMap(); @Override public int openScanner(ByteBuffer table, TScan scan) throws TIOError, TException { Table htable = getTable(table); ResultScanner resultScanner = null; try { resultScanner = htable.getScanner(scanFromThrift(scan)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } // 将scanner放入到scannerMap中, // 如果客户端没有调用closeScanner,则导致该scanner泄漏,GC无法回收改部分内存 return addScanner(resultScanner); } /** * Assigns a unique ID to the scanner and adds the mapping to an internal HashMap. * @param scanner to add * @return Id for this Scanner */ private int addScanner(ResultScanner scanner) { int id = nextScannerId.getAndIncrement(); scannerMap.put(id, scanner); // 将scanner放入到scannerMap中 return id; } /** * Returns the Scanner associated with the specified Id. * @param id of the Scanner to get * @return a Scanner, or null if the Id is invalid */ private ResultScanner getScanner(int id) { return scannerMap.get(id); } @Override public void closeScanner(int scannerId) throws TIOError, TIllegalArgument, TException { LOG.debug("scannerClose: id=" + scannerId); ResultScanner scanner = getScanner(scannerId); if (scanner == null) { String message = "scanner ID is invalid"; LOG.warn(message); TIllegalArgument ex = new TIllegalArgument(); ex.setMessage("Invalid scanner Id"); throw ex; } scanner.close(); // 关闭scanner removeScanner(scannerId); // 从scannerMap中移除scanner } /** * Removes the scanner associated with the specified ID from the internal HashMap. * @param id of the Scanner to remove * @return the removed Scanner, or null if the Id is invalid */ protected ResultScanner removeScanner(int id) { return scannerMap.remove(id); // 从scannerMap中移除scanner } |