List of usage examples for java.util.concurrent.locks Lock lock
lock
From source file:com.cloudera.oryx.als.serving.ServerRecommender.java
/** * One-argument version of {@link #mostSimilarItems(String[], int, PairRescorer)}. *//* ww w. j a v a2s . c o m*/ @Override public List<IDValue> mostSimilarItems(String itemID, int howMany, PairRescorer rescorer) throws NoSuchItemException, NotReadyException { Preconditions.checkArgument(howMany > 0, "howMany must be positive"); long longItemID = StringLongMapping.toLong(itemID); Generation generation = getCurrentGeneration(); LongObjectMap<float[]> Y = generation.getY(); Lock yLock = generation.getYLock().readLock(); yLock.lock(); try { float[] itemFeatures = Y.get(longItemID); if (itemFeatures == null) { throw new NoSuchItemException(itemID); } return translateToStringIDs( TopN.selectTopN(new MostSimilarItemIterator(Y.entrySet().iterator(), new long[] { longItemID }, new float[][] { itemFeatures }, rescorer, generation.getIDMapping()), howMany)); } finally { yLock.unlock(); } }
From source file:com.funambol.pushlistener.service.taskexecutor.ScheduledTaskExecutor.java
/** * Executes the given task scheduling its execution to run after a given delay. * <p>Note that the task will be executed as soon as a thread * is available. Moreover, if the same task (or another equal task) is already * running, a new execution is planned with a delay of task.getPeriod()). * If the same task (or another equal task) is already waiting for its execution * this method doesn't have effect.//from ww w.ja v a 2 s .c o m * @return true if the task execution has been planned to be performed as soon as * possible, false otherwise * (maybe the same task or another equal task is waiting for its execution or * is already running) * @param newTask the task to execute * @param delay the execution delay */ public boolean executeTaskWrapper(TaskWrapper newTask, long delay) { if (newTask == null) { throw new IllegalArgumentException("Task must be not null"); } Lock handlingTaskLock = getHandlingTaskLock(newTask); handlingTaskLock.lock(); try { return submitTaskWrapper(newTask, delay); } finally { handlingTaskLock.unlock(); } }
From source file:com.cloudera.oryx.als.serving.ServerRecommender.java
/** * Computes items most similar to an item or items. The returned items have the highest average similarity * to the given items./*from ww w .java 2 s. com*/ * * @param itemIDs items for which most similar items are required * @param howMany maximum number of similar items to return; fewer may be returned * @param rescorer rescoring function used to modify item-item similarities before ranking results * @return {@link IDValue}s representing the top recommendations for the user, ordered by quality, * descending. The score associated to it is an opaque value. Larger means more similar, but no further * interpretation may necessarily be applied. * @throws NoSuchItemException if any of the items is not known in the model * @throws NotReadyException if the recommender has no model available yet */ @Override public List<IDValue> mostSimilarItems(String[] itemIDs, int howMany, PairRescorer rescorer) throws NoSuchItemException, NotReadyException { Preconditions.checkArgument(howMany > 0, "howMany must be positive"); long[] longItemIDs = new long[itemIDs.length]; for (int i = 0; i < longItemIDs.length; i++) { longItemIDs[i] = StringLongMapping.toLong(itemIDs[i]); } Generation generation = getCurrentGeneration(); LongObjectMap<float[]> Y = generation.getY(); Lock yLock = generation.getYLock().readLock(); yLock.lock(); try { List<float[]> itemFeatures = Lists.newArrayListWithCapacity(itemIDs.length); for (long longItemID : longItemIDs) { float[] features = Y.get(longItemID); if (features != null) { itemFeatures.add(features); } } if (itemFeatures.isEmpty()) { throw new NoSuchItemException(Arrays.toString(itemIDs)); } float[][] itemFeaturesArray = itemFeatures.toArray(new float[itemFeatures.size()][]); return translateToStringIDs(TopN.selectTopN(new MostSimilarItemIterator(Y.entrySet().iterator(), longItemIDs, itemFeaturesArray, rescorer, generation.getIDMapping()), howMany)); } finally { yLock.unlock(); } }
From source file:com.cloudera.oryx.als.serving.ServerRecommender.java
@Override public float[] estimatePreferences(String userID, String... itemIDs) throws NotReadyException { Generation generation = getCurrentGeneration(); LongObjectMap<float[]> X = generation.getX(); float[] userFeatures; Lock xLock = generation.getXLock().readLock(); xLock.lock(); try {/*from www. ja v a 2 s . co m*/ userFeatures = X.get(StringLongMapping.toLong(userID)); } finally { xLock.unlock(); } if (userFeatures == null) { return new float[itemIDs.length]; // All 0.0f } LongObjectMap<float[]> Y = generation.getY(); Lock yLock = generation.getYLock().readLock(); yLock.lock(); try { float[] result = new float[itemIDs.length]; for (int i = 0; i < itemIDs.length; i++) { String itemID = itemIDs[i]; float[] itemFeatures = Y.get(StringLongMapping.toLong(itemID)); if (itemFeatures != null) { float value = (float) SimpleVectorMath.dot(itemFeatures, userFeatures); Preconditions.checkState(Floats.isFinite(value), "Bad estimate"); result[i] = value; } // else leave value at 0.0f } return result; } finally { yLock.unlock(); } }
From source file:com.cloudera.oryx.als.serving.ServerRecommender.java
@Override public float[] similarityToItem(String toItemID, String... itemIDs) throws NotReadyException, NoSuchItemException { Generation generation = getCurrentGeneration(); LongObjectMap<float[]> Y = generation.getY(); float[] similarities = new float[itemIDs.length]; Lock yLock = generation.getYLock().readLock(); yLock.lock(); try {// w w w .j av a 2 s . c o m float[] toFeatures = Y.get(StringLongMapping.toLong(toItemID)); if (toFeatures == null) { throw new NoSuchItemException(toItemID); } double toFeaturesNorm = SimpleVectorMath.norm(toFeatures); boolean anyFound = false; for (int i = 0; i < similarities.length; i++) { float[] features = Y.get(StringLongMapping.toLong(itemIDs[i])); if (features == null) { similarities[i] = Float.NaN; } else { anyFound = true; double featuresNorm = SimpleVectorMath.norm(features); similarities[i] = (float) (SimpleVectorMath.dot(features, toFeatures) / (featuresNorm * toFeaturesNorm)); } } if (!anyFound) { throw new NoSuchItemException(Arrays.toString(itemIDs)); } } finally { yLock.unlock(); } return similarities; }
From source file:org.opendedup.collections.ProgressiveFileBasedCSMap.java
@Override public synchronized long claimRecords(SDFSEvent evt, LargeBloomFilter bf) throws IOException { if (this.isClosed()) throw new IOException("Hashtable " + this.fileName + " is close"); executor = new ThreadPoolExecutor(Main.writeThreads + 1, Main.writeThreads + 1, 10, TimeUnit.SECONDS, worksQueue, new ProcessPriorityThreadFactory(Thread.MIN_PRIORITY), executionHandler); csz = new AtomicLong(0); try {/* w w w.j av a 2 s .c om*/ Lock l = this.gcLock.writeLock(); l.lock(); this.runningGC = true; try { File _fs = new File(fileName); lbf = new LargeBloomFilter(_fs.getParentFile(), maxSz, .01, true, true, false); } finally { l.unlock(); } SDFSLogger.getLog().info("Claiming Records [" + this.getSize() + "] from [" + this.fileName + "]"); SDFSEvent tEvt = SDFSEvent .claimInfoEvent("Claiming Records [" + this.getSize() + "] from [" + this.fileName + "]", evt); tEvt.maxCt = this.maps.size(); Iterator<AbstractShard> iter = maps.iterator(); ArrayList<ClaimShard> excs = new ArrayList<ClaimShard>(); while (iter.hasNext()) { tEvt.curCt++; AbstractShard m = null; try { m = iter.next(); ClaimShard cms = new ClaimShard(m, bf, lbf, csz); excs.add(cms); executor.execute(cms); } catch (Exception e) { tEvt.endEvent("Unable to claim records for " + m + " because : [" + e.toString() + "]", SDFSEvent.ERROR); SDFSLogger.getLog().error("Unable to claim records for " + m, e); throw new IOException(e); } } executor.shutdown(); try { while (!executor.awaitTermination(10, TimeUnit.SECONDS)) { SDFSLogger.getLog().debug("Awaiting fdisk completion of threads."); } } catch (InterruptedException e) { throw new IOException(e); } for (ClaimShard cms : excs) { if (cms.ex != null) throw new IOException(cms.ex); } this.kSz.getAndAdd(-1 * csz.get()); tEvt.endEvent("removed [" + csz.get() + "] records"); SDFSLogger.getLog().info("removed [" + csz.get() + "] records"); iter = maps.iterator(); while (iter.hasNext()) { AbstractShard m = null; try { m = iter.next(); if (!m.isFull() && !m.isActive()) { // SDFSLogger.getLog().info("deleting " + // m.toString()); m.iterInit(); KVPair p = m.nextKeyValue(); while (p != null) { ProgressiveFileByteArrayLongMap _m = this.getWriteMap(); try { _m.put(p.key, p.value); this.lbf.put(p.key); p = m.nextKeyValue(); } catch (HashtableFullException e) { } } int mapsz = maps.size(); l = this.gcLock.writeLock(); l.lock(); try { maps.remove(m); } finally { l.unlock(); } mapsz = mapsz - maps.size(); SDFSLogger.getLog() .info("removing map " + m.toString() + " sz=" + maps.size() + " rm=" + mapsz); m.vanish(); m = null; } else if (m.isMaxed()) { SDFSLogger.getLog().info("deleting maxed " + m.toString()); m.iterInit(); KVPair p = m.nextKeyValue(); while (p != null) { ProgressiveFileByteArrayLongMap _m = this.getWriteMap(); try { _m.put(p.key, p.value); p = m.nextKeyValue(); } catch (HashtableFullException e) { } } int mapsz = maps.size(); l = this.gcLock.writeLock(); l.lock(); try { maps.remove(m); } finally { l.unlock(); } mapsz = mapsz - maps.size(); SDFSLogger.getLog() .info("removing map " + m.toString() + " sz=" + maps.size() + " rm=" + mapsz); m.vanish(); m = null; } } catch (Exception e) { tEvt.endEvent("Unable to compact " + m + " because : [" + e.toString() + "]", SDFSEvent.ERROR); SDFSLogger.getLog().error("to compact " + m, e); throw new IOException(e); } } l.lock(); this.runningGC = false; l.unlock(); return csz.get(); } finally { executor = null; } }
From source file:com.funambol.pushlistener.service.taskexecutor.ScheduledTaskExecutor.java
/** * Schedules a new task/*from w ww .ja v a2 s. co m*/ * * @param task the <code>ScheduledTaskWrapper</code> to schedule */ public void scheduleTask(ScheduledTaskWrapper task) { if (task == null) { if (log.isTraceEnabled()) { log.trace("Trying to schedule a null task. Request rejected"); } return; } // // Locking the lock for this task so no other thread can handle it avoiding // conflict (what happens if a thread is removing it an another threead is // updating it ?) // Lock handlingTaskLock = getHandlingTaskLock(task.getId()); handlingTaskLock.lock(); try { scheduleTask(task, 0); } finally { handlingTaskLock.unlock(); } }
From source file:com.funambol.pushlistener.service.taskexecutor.ScheduledTaskExecutor.java
/** * Verifies if the task with the given id is in the queue * @param newTask the searched task/* ww w. ja v a2s.c om*/ * @return true if the task is in the queue */ public boolean isTaskWrapperInQueue(TaskWrapper newTask) { if (newTask == null) { throw new IllegalArgumentException("Task must be not null"); } // // Locking the lock for this task so no other thread can handle it avoiding // conflict (what happens if a thread is removing it an another threead is // updating it ?) // Lock handlingTaskLock = getHandlingTaskLock(newTask); handlingTaskLock.lock(); try { synchronized (taskFutures) { Object o = taskFutures.get(newTask); if (o != null) { return true; } } } finally { handlingTaskLock.unlock(); } return false; }
From source file:com.cloudera.oryx.als.serving.ServerRecommender.java
@Override public float estimateForAnonymous(String toItemID, String[] itemIDs, float[] values) throws NotReadyException, NoSuchItemException { Generation generation = getCurrentGeneration(); LongObjectMap<float[]> Y = generation.getY(); Lock yLock = generation.getYLock().readLock(); float[] toItemFeatures; yLock.lock(); try {/*from ww w . j a v a2s .c o m*/ toItemFeatures = Y.get(StringLongMapping.toLong(toItemID)); } finally { yLock.unlock(); } if (toItemFeatures == null) { throw new NoSuchItemException(toItemID); } float[] anonymousUserFeatures = buildAnonymousUserFeatures(itemIDs, values); return (float) SimpleVectorMath.dot(anonymousUserFeatures, toItemFeatures); }
From source file:com.cloudera.oryx.als.serving.ServerRecommender.java
@Override public List<IDValue> mostPopularItems(int howMany, Rescorer rescorer) throws NotReadyException { Preconditions.checkArgument(howMany > 0, "howMany must be positive"); Generation generation = getCurrentGeneration(); LongObjectMap<LongSet> knownItemIDs = generation.getKnownItemIDs(); if (knownItemIDs == null) { throw new UnsupportedOperationException(); }/*from w ww .j a va 2s . c om*/ LongFloatMap itemCounts = new LongFloatMap(); Lock knownItemReadLock = generation.getKnownItemLock().readLock(); knownItemReadLock.lock(); try { Lock xReadLock = generation.getXLock().readLock(); xReadLock.lock(); try { for (LongObjectMap.MapEntry<LongSet> entry : generation.getKnownItemIDs().entrySet()) { LongSet itemIDs = entry.getValue(); synchronized (itemIDs) { LongPrimitiveIterator it = itemIDs.iterator(); while (it.hasNext()) { long itemID = it.nextLong(); itemCounts.increment(itemID, 1.0f); } } } } finally { xReadLock.unlock(); } } finally { knownItemReadLock.unlock(); } return translateToStringIDs(TopN.selectTopN( new MostPopularItemsIterator(itemCounts.entrySet().iterator(), rescorer, generation.getIDMapping()), howMany)); }