List of usage examples for java.util.concurrent ConcurrentMap replace
boolean replace(K key, V oldValue, V newValue);
From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java
@Test(dataProvider = "guardedMap") public void replaceConditionally_whenPopulated(ConcurrentMap<Integer, Integer> map) { map.put(1, 2);//from www . j ava 2s . com assertThat(map.replace(1, 3, 4), is(false)); assertThat(map.replace(1, 2, 3), is(true)); assertThat(map.get(1), is(3)); assertThat(map.size(), is(1)); }
From source file:com.palantir.atlasdb.transaction.impl.SerializableTransaction.java
private void setRangeEnd(String table, RangeRequest range, byte[] maxRow) { Validate.notNull(maxRow);//from w ww.j a va 2 s . co m ConcurrentMap<RangeRequest, byte[]> rangeEnds = rangeEndByTable.get(table); if (rangeEnds == null) { ConcurrentMap<RangeRequest, byte[]> newMap = Maps.newConcurrentMap(); rangeEndByTable.putIfAbsent(table, newMap); rangeEnds = rangeEndByTable.get(table); } if (maxRow.length == 0) { rangeEnds.put(range, maxRow); } while (true) { byte[] curVal = rangeEnds.get(range); if (curVal == null) { byte[] oldVal = rangeEnds.putIfAbsent(range, maxRow); if (oldVal == null) { return; } else { continue; } } if (curVal.length == 0) { return; } if (UnsignedBytes.lexicographicalComparator().compare(curVal, maxRow) >= 0) { return; } if (rangeEnds.replace(range, curVal, maxRow)) { return; } } }
From source file:org.apache.hadoop.hbase.client.MetaCache.java
/** * Put a newly discovered HRegionLocation into the cache. * @param tableName The table name./*from www.j av a 2s .c om*/ * @param source the source of the new location * @param location the new location */ public void cacheLocation(final TableName tableName, final ServerName source, final HRegionLocation location) { assert source != null; byte[] startKey = location.getRegionInfo().getStartKey(); ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations locations = new RegionLocations(new HRegionLocation[] { location }); RegionLocations oldLocations = tableLocations.putIfAbsent(startKey, locations); boolean isNewCacheEntry = (oldLocations == null); if (isNewCacheEntry) { if (LOG.isTraceEnabled()) { LOG.trace("Cached location: " + location); } addToCachedServers(locations); return; } // If the server in cache sends us a redirect, assume it's always valid. HRegionLocation oldLocation = oldLocations.getRegionLocation(location.getRegionInfo().getReplicaId()); boolean force = oldLocation != null && oldLocation.getServerName() != null && oldLocation.getServerName().equals(source); // For redirect if the number is equal to previous // record, the most common case is that first the region was closed with seqNum, and then // opened with the same seqNum; hence we will ignore the redirect. // There are so many corner cases with various combinations of opens and closes that // an additional counter on top of seqNum would be necessary to handle them all. RegionLocations updatedLocations = oldLocations.updateLocation(location, false, force); if (oldLocations != updatedLocations) { boolean replaced = tableLocations.replace(startKey, oldLocations, updatedLocations); if (replaced && LOG.isTraceEnabled()) { LOG.trace("Changed cached location to: " + location); } addToCachedServers(updatedLocations); } }
From source file:org.apache.hadoop.hbase.client.MetaCache.java
/** * Put a newly discovered HRegionLocation into the cache. * @param tableName The table name./*from www.ja v a2 s . c o m*/ * @param locations the new locations */ public void cacheLocation(final TableName tableName, final RegionLocations locations) { byte[] startKey = locations.getRegionLocation().getRegionInfo().getStartKey(); ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations oldLocation = tableLocations.putIfAbsent(startKey, locations); boolean isNewCacheEntry = (oldLocation == null); if (isNewCacheEntry) { if (LOG.isTraceEnabled()) { LOG.trace("Cached location: " + locations); } addToCachedServers(locations); return; } // merge old and new locations and add it to the cache // Meta record might be stale - some (probably the same) server has closed the region // with later seqNum and told us about the new location. RegionLocations mergedLocation = oldLocation.mergeLocations(locations); boolean replaced = tableLocations.replace(startKey, oldLocation, mergedLocation); if (replaced && LOG.isTraceEnabled()) { LOG.trace("Merged cached locations: " + mergedLocation); } addToCachedServers(locations); }
From source file:org.apache.hadoop.hbase.client.MetaCache.java
/** * Delete all cached entries of a server. *//*from ww w . j a v a 2 s.c o m*/ public void clearCache(final ServerName serverName) { if (!this.cachedServers.contains(serverName)) { return; } boolean deletedSomething = false; synchronized (this.cachedServers) { // We block here, because if there is an error on a server, it's likely that multiple // threads will get the error simultaneously. If there are hundreds of thousand of // region location to check, it's better to do this only once. A better pattern would // be to check if the server is dead when we get the region location. if (!this.cachedServers.contains(serverName)) { return; } for (ConcurrentMap<byte[], RegionLocations> tableLocations : cachedRegionLocations.values()) { for (Entry<byte[], RegionLocations> e : tableLocations.entrySet()) { RegionLocations regionLocations = e.getValue(); if (regionLocations != null) { RegionLocations updatedLocations = regionLocations.removeByServer(serverName); if (updatedLocations != regionLocations) { if (updatedLocations.isEmpty()) { deletedSomething |= tableLocations.remove(e.getKey(), regionLocations); } else { deletedSomething |= tableLocations.replace(e.getKey(), regionLocations, updatedLocations); } } } } } this.cachedServers.remove(serverName); } if (deletedSomething && LOG.isTraceEnabled()) { LOG.trace("Removed all cached region locations that map to " + serverName); } }
From source file:org.apache.hadoop.hbase.client.MetaCache.java
/** * Delete a cached location, no matter what it is. Called when we were told to not use cache. * @param tableName tableName//from w w w . j a v a 2 s. c o m * @param row */ public void clearCache(final TableName tableName, final byte[] row, int replicaId) { ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); boolean removed = false; RegionLocations regionLocations = getCachedLocation(tableName, row); if (regionLocations != null) { HRegionLocation toBeRemoved = regionLocations.getRegionLocation(replicaId); RegionLocations updatedLocations = regionLocations.remove(replicaId); if (updatedLocations != regionLocations) { byte[] startKey = regionLocations.getRegionLocation().getRegionInfo().getStartKey(); if (updatedLocations.isEmpty()) { removed = tableLocations.remove(startKey, regionLocations); } else { removed = tableLocations.replace(startKey, regionLocations, updatedLocations); } } if (removed && LOG.isTraceEnabled() && toBeRemoved != null) { LOG.trace("Removed " + toBeRemoved + " from cache"); } } }
From source file:org.apache.hadoop.hbase.client.MetaCache.java
/** * Delete a cached location for a table, row and server *//*from www.j a va 2 s . com*/ public void clearCache(final TableName tableName, final byte[] row, ServerName serverName) { ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations regionLocations = getCachedLocation(tableName, row); if (regionLocations != null) { RegionLocations updatedLocations = regionLocations.removeByServer(serverName); if (updatedLocations != regionLocations) { byte[] startKey = regionLocations.getRegionLocation().getRegionInfo().getStartKey(); boolean removed = false; if (updatedLocations.isEmpty()) { removed = tableLocations.remove(startKey, regionLocations); } else { removed = tableLocations.replace(startKey, regionLocations, updatedLocations); } if (removed && LOG.isTraceEnabled()) { LOG.trace("Removed locations of table: " + tableName + " ,row: " + Bytes.toString(row) + " mapping to server: " + serverName + " from cache"); } } } }
From source file:org.apache.hadoop.hbase.client.MetaCache.java
/** * Deletes the cached location of the region if necessary, based on some error from source. * @param hri The region in question./* w w w. java 2s .c o m*/ */ public void clearCache(HRegionInfo hri) { ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(hri.getTable()); RegionLocations regionLocations = tableLocations.get(hri.getStartKey()); if (regionLocations != null) { HRegionLocation oldLocation = regionLocations.getRegionLocation(hri.getReplicaId()); if (oldLocation == null) return; RegionLocations updatedLocations = regionLocations.remove(oldLocation); boolean removed = false; if (updatedLocations != regionLocations) { if (updatedLocations.isEmpty()) { removed = tableLocations.remove(hri.getStartKey(), regionLocations); } else { removed = tableLocations.replace(hri.getStartKey(), regionLocations, updatedLocations); } if (removed && LOG.isTraceEnabled()) { LOG.trace("Removed " + oldLocation + " from cache"); } } } }
From source file:org.apache.hadoop.hbase.client.MetaCache.java
public void clearCache(final HRegionLocation location) { if (location == null) { return;/* www. jav a 2 s . c om*/ } TableName tableName = location.getRegionInfo().getTable(); ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations regionLocations = tableLocations.get(location.getRegionInfo().getStartKey()); if (regionLocations != null) { RegionLocations updatedLocations = regionLocations.remove(location); boolean removed = false; if (updatedLocations != regionLocations) { if (updatedLocations.isEmpty()) { removed = tableLocations.remove(location.getRegionInfo().getStartKey(), regionLocations); } else { removed = tableLocations.replace(location.getRegionInfo().getStartKey(), regionLocations, updatedLocations); } if (removed && LOG.isTraceEnabled()) { LOG.trace("Removed " + location + " from cache"); } } } }
From source file:org.danann.cernunnos.DynamicCacheHelper.java
public V getCachedObject(TaskRequest req, TaskResponse res, K key, Factory<K, V> factory) { final CacheMode cacheMode = CacheMode.valueOf((String) this.cacheModelPhrase.evaluate(req, res)); if (this.logger.isDebugEnabled()) { this.logger.debug("Getting cached object for '" + key + "' using cache mode " + cacheMode + " and factory " + factory); }/* ww w .ja va 2 s . c o m*/ //Load the cache only if cache-all is enabled final ConcurrentMap<Tuple<Serializable, K>, Object> cache; final Tuple<Serializable, K> compoundCacheKey; switch (cacheMode) { case NONE: { return factory.createObject(key); } default: case ONE: { cache = null; compoundCacheKey = null; } break; case ALL: { cache = (ConcurrentMap<Tuple<Serializable, K>, Object>) this.cachePhrase.evaluate(req, res); final Serializable cacheNamespace = factory.getCacheNamespace(key); compoundCacheKey = new Tuple<Serializable, K>(cacheNamespace, key); } break; } //Determine the object to synchronize around final Object syncTarget = factory.getMutex(key); //get or create & cache the target object V instance = null; synchronized (syncTarget) { //Get the object from the local variables if no cache is available if (cache == null) { //Try for a thread-local instance first if (this.compareKeys(key, this.threadKeyHolder.get())) { instance = this.threadInstanceHolder.get(); } //Next try for a singleton instance else if (this.compareKeys(key, this.key)) { instance = this.instance; } } //Look in the passed cache for the instance else { final Object object = cache.get(compoundCacheKey); //If the cached object is a ThreadLocal use it for the instance if (object instanceof ThreadLocal<?>) { instance = ((ThreadLocal<V>) object).get(); } //If not assume it is the instance else { instance = (V) object; } } //If no instance was found create and cache one if (instance == null) { instance = factory.createObject(key); final boolean threadSafe = factory.isThreadSafe(key, instance); if (this.logger.isDebugEnabled()) { this.logger.debug( "Cache miss for '" + key + "' created '" + instance + "' threadSafe=" + threadSafe); } //If no cache is available store the instance in the local variables if (cache == null) { if (threadSafe) { this.instance = instance; this.key = key; } else { this.threadInstanceHolder.set(instance); this.threadKeyHolder.set(key); } } //Cache available store there else { if (threadSafe) { cache.put(compoundCacheKey, instance); } else { ThreadLocal<V> threadInstanceHolder = (ThreadLocal<V>) cache.get(compoundCacheKey); if (threadInstanceHolder == null) { threadInstanceHolder = new ThreadLocal<V>(); while (true) { Object existing = cache.putIfAbsent(compoundCacheKey, threadInstanceHolder); if (existing == null) { //nothing existed for that key, put was successful break; } if (existing instanceof ThreadLocal) { //Existing ThreadLocal, just use it threadInstanceHolder = (ThreadLocal) existing; break; } //something other than a ThreadLocal already exists, try replacing with the ThreadLocal final boolean replaced = cache.replace(compoundCacheKey, threadInstanceHolder, existing); if (replaced) { //Replace worked! break; } //Replace didn't work, try the whole process again, yay non-blocking! } if (cache instanceof EvictionAwareCache) { ((EvictionAwareCache) cache) .registerCacheEvictionListener(ThreadLocalCacheEvictionListener.INSTANCE); } } threadInstanceHolder.set(instance); } } } else if (this.logger.isDebugEnabled()) { this.logger.debug("Cache hit for '" + key + "' using '" + instance + "'"); } } return instance; }