/** * @param host * @param notify * @return */ private boolean remove(SiteHost host, boolean notify) { Logger.info("WorkPool.remove, work site %s", host); boolean success = false; this.lockSingle(); try { mapTime.remove(host); WorkSite site = mapSite.remove(host); if (site != null) { for (Naming naming : site.list()) { SiteSet set = mapNaming.get(naming); if (set != null) { set.remove(host); } if (set == null || set.isEmpty()) { mapNaming.remove(naming); } } success = true; if (notify) { CallPool.getInstance().refreshWorkSite(); } } } catch (Throwable exp) { Logger.error(exp); } finally { this.unlockSingle(); } return success; }
/** * get all worksite * * @return */ public WorkSite[] batch() { WorkSite[] sites = null; super.lockMulti(); try { int size = mapSite.size(); if (size > 0) { sites = new WorkSite[size]; mapSite.values().toArray(sites); } } catch (Throwable exp) { Logger.fatal(exp); } finally { super.unlockMulti(); } return sites; }
/* * (non-Javadoc) * @see com.lexst.db.view.View#getChunkIds() */ @Override public Set<Long> getChunkIds() { Set<Long> all = new TreeSet<Long>(); for (IdentitySet set : mapSet.values()) { all.addAll(set.keySet()); } return all; }
/* * (non-Javadoc) * @see com.lexst.db.view.View#add(com.lexst.util.host.SiteHost, com.lexst.db.sign.range.SignRange) */ @Override public boolean add(SiteHost host, IndexRange index) { if (index.getClass() != ShortIndexRange.class) { throw new ClassCastException("not small index"); } ShortIndexRange idx = (ShortIndexRange) index; short begin = idx.getBegin(); short end = idx.getEnd(); ShortRange range = new ShortRange(begin, end); long chunkId = idx.getChunkId(); IdentitySet set = mapSet.get(range); if (set == null) { set = new IdentitySet(); mapSet.put(range, set); } return set.add(host, chunkId); }
/** * refresh work site * * @param host * @return */ public short refresh(SiteHost host) { short code = Response.SERVER_ERROR; this.lockSingle(); try { WorkSite site = mapSite.get(host); if (site != null) { mapTime.put(host, System.currentTimeMillis()); code = Response.ISEE; } else { code = Response.NOTLOGIN; } } catch (Throwable exp) { Logger.error(exp); } finally { this.unlockSingle(); } Logger.debug("WorkPool.refresh, site %s refresh status %d", host, code); return code; }
/* * (non-Javadoc) * @see com.lexst.db.view.View#remove(com.lexst.util.host.SiteHost) */ @Override public int remove(SiteHost host) { int size = mapSet.size(); if (size == 0) return size; int count = 0; ArrayList<ShortRange> a = new ArrayList<ShortRange>(size); for (ShortRange range : mapSet.keySet()) { IdentitySet set = mapSet.get(range); if (set != null) { count += set.remove(host); if (set.isEmpty()) a.add(range); } else { a.add(range); } } for (ShortRange range : a) { mapSet.remove(range); } return count; }
/** check timeout site */ private void check() { int size = mapTime.size(); if (size == 0) return; ArrayList<SiteHost> dels = new ArrayList<SiteHost>(size); ArrayList<SiteHost> notifys = new ArrayList<SiteHost>(size); super.lockSingle(); try { long nowTime = System.currentTimeMillis(); for (SiteHost host : mapTime.keySet()) { Long value = mapTime.get(host); if (value == null) { dels.add(host); continue; } long time = value.longValue(); if (nowTime - time >= deleteTime) { dels.add(host); } else if (nowTime - time >= refreshTimeout) { notifys.add(host); } } } catch (Throwable exp) { exp.printStackTrace(); } finally { super.unlockSingle(); } // remove timeout site for (SiteHost host : dels) { Logger.error("WorkPool.check, delete timeout site:%s", host); remove(host); } // notify site SiteHost listen = Launcher.getInstance().getLocalHost(); for (SiteHost host : notifys) { Logger.warning("WorkPool.check, notify timeout site:%s", host); this.sendTimeout(host, listen, 2); } }
/* * (non-Javadoc) * @see com.lexst.db.view.View#remove(com.lexst.util.host.SiteHost, long) */ @Override public int remove(SiteHost host, long chunkId) { int size = mapSet.size(); if (size == 0) return 0; int count = 0; ArrayList<ShortRange> a = new ArrayList<ShortRange>(size); for (ShortRange range : mapSet.keySet()) { IdentitySet set = mapSet.get(range); if (set != null) { boolean success = set.remove(host, chunkId); if (success) count++; if (set.isEmpty()) a.add(range); } else { a.add(range); } } for (ShortRange range : a) { mapSet.remove(range); } return count; }
/* * (non-Javadoc) * @see com.lexst.db.view.View#delete(com.lexst.util.host.SiteHost) */ @Override public List<Long> delete(SiteHost host) { int size = mapSet.size(); if (size == 0) return null; ArrayList<Long> array = new ArrayList<Long>(256); ArrayList<ShortRange> a = new ArrayList<ShortRange>(size); for (ShortRange range : mapSet.keySet()) { IdentitySet set = mapSet.get(range); if (set != null) { set.remove(host, array); if (set.isEmpty()) a.add(range); } else { a.add(range); } } for (ShortRange range : a) { mapSet.remove(range); } return array; }
private boolean add(Site object, boolean notify) { if (object == null || !object.isWork()) { return false; } WorkSite site = (WorkSite) object; SiteHost host = site.getHost(); Logger.info("WorkPool.add, work site %s", host); boolean success = false; this.lockSingle(); try { if (mapSite.containsKey(host)) { return false; } // save site mapSite.put(host, site); mapTime.put(host, System.currentTimeMillis()); for (Naming naming : site.list()) { SiteSet set = mapNaming.get(naming); if (set == null) { set = new SiteSet(); mapNaming.put(naming, set); } set.add(host); } success = true; if (notify) { CallPool.getInstance().refreshWorkSite(); } } catch (Throwable exp) { Logger.error(exp); } finally { this.unlockSingle(); } return success; }
/** * find work site by naming * * @param naming * @return */ public SiteHost[] find(String naming) { Naming s = new Naming(naming); Logger.info("WorkPool.find, find work site by '%s'", s); ArrayList<SiteHost> a = new ArrayList<SiteHost>(); this.lockSingle(); try { SiteSet set = mapNaming.get(s); if (set != null) { a.addAll(set.list()); } } catch (Throwable exp) { Logger.error(exp); } finally { this.unlockSingle(); } if (a.isEmpty()) return null; SiteHost[] hosts = new SiteHost[a.size()]; return a.toArray(hosts); }
/** @return */ public List<SiteHost> gather() { ArrayList<SiteHost> array = new ArrayList<SiteHost>(); array.addAll(mapSite.keySet()); return array; }
/* * (non-Javadoc) * @see com.lexst.db.view.View#size() */ @Override public int size() { return mapSet.size(); }
/* * (non-Javadoc) * @see com.lexst.db.view.View#isEmpty() */ @Override public boolean isEmpty() { return mapSet.isEmpty(); }
/* * (non-Javadoc) * @see com.lexst.db.view.View#find(com.lexst.db.statement.Condition) */ @Override public Set<Long> find(Condition condi) { IndexColumn num = condi.getValue(); if (num == null || num.getClass() != ShortIndex.class) { throw new IllegalArgumentException("null pointer or invalid index!"); } short value = ((ShortIndex) num).getValue(); // 找到范围分片截止点 HashSet<Long> all = new HashSet<Long>(1024); // 列名在左,参数值在右(固定!, 检查可以的范围) switch (condi.getCompare()) { case Condition.EQUAL: for (ShortRange range : mapSet.keySet()) { if (range.inside(value)) { IdentitySet set = mapSet.get(range); all.addAll(set.keySet()); } } break; case Condition.NOT_EQUAL: for (ShortRange range : mapSet.keySet()) { if (range.getBegin() != value || range.getEnd() != value) { IdentitySet set = mapSet.get(range); all.addAll(set.keySet()); } } break; case Condition.LESS: for (ShortRange range : mapSet.keySet()) { if (range.getBegin() < value || range.getEnd() < value) { IdentitySet set = mapSet.get(range); all.addAll(set.keySet()); } } break; case Condition.LESS_EQUAL: for (ShortRange range : mapSet.keySet()) { if (range.getBegin() <= value || range.getEnd() <= value) { IdentitySet set = mapSet.get(range); all.addAll(set.keySet()); } } break; case Condition.GREATER: for (ShortRange range : mapSet.keySet()) { if (range.getBegin() > value || range.getEnd() > value) { IdentitySet set = mapSet.get(range); all.addAll(set.keySet()); } } break; case Condition.GREATER_EQUAL: for (ShortRange range : mapSet.keySet()) { if (range.getBegin() >= value || range.getEnd() >= value) { IdentitySet set = mapSet.get(range); all.addAll(set.keySet()); } } break; case Condition.LIKE: for (IdentitySet set : mapSet.values()) { all.addAll(set.keySet()); } break; } return all; }