final V get(final Object key, final int hash, final MapCallback<K, V, ?, ?> readCallback) { final ReentrantReadWriteLock.ReadLock readLock = super.readLock(); readLock.lock(); try { if (this.count != 0) { // read-volatile HashEntry<K, V> e = getFirst(hash); while (e != null) { if (e.getEntryHash() == hash && equalityKeyCompare(key, e)) { final V v = e.getMapValue(); if (v != null) { if (readCallback != null) { readCallback.oldValueRead(v); } return v; } } e = e.getNextEntry(); } } } finally { readLock.unlock(); } return null; }
private TVector cloneRow(int matrixId, int rowIndex, TVector row, TaskContext taskContext) { if (row == null) { return null; } if (isNeedClone(matrixId)) { ReentrantReadWriteLock globalStorage = PSAgentContext.get().getMatrixStorageManager().getMatrixStoage(matrixId).getLock(); TVector taskRow = taskContext.getMatrixStorage().getRow(matrixId, rowIndex); try { globalStorage.readLock().lock(); if(taskRow == null || (taskRow.getClass() != row.getClass())){ taskRow = row.clone(); taskContext.getMatrixStorage().addRow(matrixId, rowIndex, taskRow); } else { taskRow.clone(row); } } finally { globalStorage.readLock().unlock(); } return taskRow; } else { return row; } }
/** * Save the state of the <tt>ConcurrentHashMap</tt> instance to a stream (i.e., serialize it). * * @param s the stream * @serialData the key (Object) and value (Object) for each key-value mapping, followed by a null * pair. The key-value mappings are emitted in no particular order. */ private void writeObject(final java.io.ObjectOutputStream s) throws IOException { s.defaultWriteObject(); for (int k = 0; k < this.segments.length; ++k) { final Segment<K, V> seg = this.segments[k]; final ReentrantReadWriteLock.ReadLock readLock = seg.readLock(); readLock.lock(); try { final HashEntry<K, V>[] tab = seg.table; for (int i = 0; i < tab.length; ++i) { for (HashEntry<K, V> e = tab[i]; e != null; e = e.getNextEntry()) { s.writeObject(e.getKey()); s.writeObject(e.getMapValue()); } } } finally { readLock.unlock(); } } s.writeObject(null); s.writeObject(null); }
@Override public boolean put(byte[] item) { long[] hashes = new long[2]; strategy.hashes(item, hashes); long bucketIdx = hashes[0] % numBuckets; long tag = fingerprint(hashes[1]); boolean itemAdded = false; ReentrantReadWriteLock.WriteLock lock = segments[(int)(bucketIdx & FAST_MOD_32)].writeLock(); lock.lock(); try { itemAdded = table.append(bucketIdx, tag); } finally { lock.unlock(); } if(!itemAdded) { itemAdded = putInAlt(bucketIdx, tag); } if(itemAdded) { count.incrementAndGet(); } else { log.log(Level.WARNING, String.format("Cucko table exceed capacity: %1$d elements", count.get())); } return itemAdded; }
public void testMultipleReadLocks(boolean fair) { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); lock.readLock().lock(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS)); lock.readLock().unlock(); lock.readLock().lock(); lock.readLock().unlock(); }}); awaitTermination(t); lock.readLock().unlock(); }
/** * Create a AMWorkerGroup * @param groupId worker group id * @param context master context * @param workerMap workers contains in worker group * @param leader leader worker of worker group * @param splitIndex training data block index assgined to this worker group */ public AMWorkerGroup(WorkerGroupId groupId, AMContext context, Map<WorkerId, AMWorker> workerMap, WorkerId leader, int splitIndex) { this.context = context; this.groupId = groupId; this.workerMap = workerMap; this.leader = leader; this.splitIndex = splitIndex; ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); stateMachine = stateMachineFactory.make(this); diagnostics = new ArrayList<String>(); successWorkerSet = new HashSet<WorkerId>(); failedWorkerSet = new HashSet<WorkerId>(); killedWorkerSet = new HashSet<WorkerId>(); }
final V replace(final K key, final int hash, final V newValue) { final ReentrantReadWriteLock.WriteLock writeLock = super.writeLock(); writeLock.lock(); try { HashEntry<K, V> e = getFirst(hash); while (e != null && (e.getEntryHash() != hash || !equalityKeyCompare(key, e))) { e = e.getNextEntry(); } V oldValue = null; if (e != null) { oldValue = e.getMapValue(); e.setMapValue(newValue); } return oldValue; } finally { writeLock.unlock(); } }
@Override public boolean load() { try { InstantFixClassMap.setClassLoader(getClass().getClassLoader()); HashMap<Integer, ReadWriteLock> lockMap = new HashMap<>(); HashMap<Integer, String> classIndexMap = new HashMap<>(); String[] patchedClasses = getPatchedClasses(); int[] patchedClassIndexes = getPatchedClassIndexes(); if (patchedClasses.length != patchedClassIndexes.length) { throw new IllegalArgumentException("patchedClasses's len is " + patchedClasses.length + ", but patchedClassIndexs's len is " + patchedClassIndexes.length); } for (int i = 0; i < patchedClasses.length; i++) { String className = patchedClasses[i]; int classIndex = patchedClassIndexes[i]; lockMap.put(classIndex, new ReentrantReadWriteLock()); classIndexMap.put(classIndex, className); Log.i(TAG, String.format("patched %s", className)); } InstantFixClassMap.setAtomMap(new InstantFixClassMap.AtomMap(classIndexMap, lockMap)); } catch (Throwable e) { e.printStackTrace(); return false; } return true; }
private void decrement() { long pivot = ThreadLocalRandom.current().nextLong(numOfBuckets); for(int i=0; i<bucketsToDecrement; i++) { long idx = (pivot + i) % numOfBuckets; ReentrantReadWriteLock.WriteLock currentLock = segments[(int)(idx & FAST_MOD_32)].writeLock(); currentLock.lock(); try { // just in case something goes wrong long bucketVal = bucketSet.readTag(idx, 0); if(bucketVal != 0L) { bucketSet.writeTag(idx, 0, bucketVal-1); } } finally { currentLock.unlock(); } } }
public YfyBaseClient(K key, YfyRequestConfig requestConfig, String accessToken, String refreshToken, YfyRefreshListener<K> refreshListener) { if (accessToken == null) { throw new NullPointerException("access token"); } this.requestConfig = requestConfig; this.host = YfyAppInfo.getHost(); this.refreshLock = new ReentrantReadWriteLock(); this.key = key; this.accessToken = accessToken; this.refreshToken = refreshToken; if (refreshToken != null && refreshListener != null) { this.autoRefresh = true; this.refreshListener = refreshListener; } }
StableBloomFilter(BitSet bitset, long numOfBuckets, int bitsPerBucket, long bucketsToDecrement, int numHashFunctions, HashFunction strategy) { // allow 1 item per bucket this.bucketSet = new BucketSet(bitsPerBucket, 1, numOfBuckets, bitset); this.numHashFunctions = numHashFunctions; this.numOfBuckets = numOfBuckets; this.bitsPerBucket = bitsPerBucket; this.strategy = strategy; this.bucketsToDecrement = bucketsToDecrement; for(int i=0;i<DEFAULT_CONCURRENCY_LEVEL;i++) { segments[i] = new ReentrantReadWriteLock(); } log.log( Level.FINE, String.format( "Stable Bloom filter: %1$d hash functions, %2$d bits, %3$d bits per elemnent", numHashFunctions, bitset.bitSize(), bitsPerBucket) ); }
public Translog(TranslogConfig config, String nodeId) { super(config.getShardId(), config.getIndexSettings()); this.config = null; recoveredTranslogs = null; syncScheduler = null; bigArrays = null; ReadWriteLock rwl = new ReentrantReadWriteLock(); readLock = new ReleasableLock(rwl.readLock()); writeLock = new ReleasableLock(rwl.writeLock()); location = null; current = null; currentCommittingTranslog = null; lastCommittedTranslogFileGeneration = -1; config = null; translogUUID = null; }
@Override public boolean put(byte[] bytes) { long[] hashes = new long[numHashFunctions]; strategy.hashes(bytes, hashes); // make room for new values decrement(); for (int i = 0; i < hashes.length; i++) { long idx = hashes[i] % numOfBuckets; ReentrantReadWriteLock.WriteLock currentLock = segments[(int)(idx & FAST_MOD_32)].writeLock(); currentLock.lock(); try { // just in case something goes wrong bucketSet.writeTag(idx, 0, Utils.MASKS[bitsPerBucket]); // write max val for bucket } finally { currentLock.unlock(); } } // forever true since we always overwrite bucket content return true; }
public HiveCommon () { final ReentrantReadWriteLock itemMapLock = new ReentrantReadWriteLock ( Boolean.getBoolean ( "org.eclipse.scada.da.server.common.fairItemMapLock" ) ); this.itemMapReadLock = itemMapLock.readLock (); this.itemMapWriteLock = itemMapLock.writeLock (); this.subscriptionValidator = new SubscriptionValidator<String> () { @Override public boolean validate ( final SubscriptionListener<String> listener, final String topic ) { return validateItem ( topic ); } }; }
public RMAppAttemptImpl(ApplicationAttemptId appAttemptId, RMContext rmContext, YarnScheduler scheduler, ApplicationMasterService masterService, ApplicationSubmissionContext submissionContext, Configuration conf, boolean maybeLastAttempt, ResourceRequest amReq) { this.conf = conf; this.applicationAttemptId = appAttemptId; this.rmContext = rmContext; this.eventHandler = rmContext.getDispatcher().getEventHandler(); this.submissionContext = submissionContext; this.scheduler = scheduler; this.masterService = masterService; ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); this.readLock = lock.readLock(); this.writeLock = lock.writeLock(); this.proxiedTrackingUrl = generateProxyUriWithScheme(); this.maybeLastAttempt = maybeLastAttempt; this.stateMachine = stateMachineFactory.make(this); this.attemptMetrics = new RMAppAttemptMetrics(applicationAttemptId, rmContext); this.amReq = amReq; }
/** * Creates an instance of OverlayPeer * * @param p_nodeID * the own NodeID * @param p_contactSuperpeer * the superpeer to contact for joining * @param p_initialNumberOfSuperpeers * the number of expeced superpeers * @param p_boot * the BootComponent * @param p_network * the NetworkComponent * @param p_event * the EventComponent */ public OverlayPeer(final short p_nodeID, final short p_contactSuperpeer, final int p_initialNumberOfSuperpeers, final AbstractBootComponent p_boot, final NetworkComponent p_network, final EventComponent p_event) { m_boot = p_boot; m_network = p_network; m_event = p_event; m_initialNumberOfSuperpeers = p_initialNumberOfSuperpeers; m_nodeID = p_nodeID; registerNetworkMessages(); registerNetworkMessageListener(); m_overlayLock = new ReentrantReadWriteLock(false); joinSuperpeerOverlay(p_contactSuperpeer); }
public LocalTranslog(TranslogConfig config) throws IOException { super(config.getShardId(), config.getIndexSettings()); ReadWriteLock rwl = new ReentrantReadWriteLock(); readLock = new ReleasableLock(rwl.readLock()); writeLock = new ReleasableLock(rwl.writeLock()); this.translogPath = config.getTranslogPath(); // clean all files Files.createDirectories(this.translogPath); Files.walkFileTree(this.translogPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } }); // create a new directory writeChannel = FileChannel.open(this.translogPath.resolve(getFileNameFromId(tmpTranslogGeneration.get())), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); writtenOffset = 0; }
@Test public void file_try_with_resources() { String path = "test.sbx"; SecretIdentifier secretIdentifier1 = new SecretIdentifier("MySecret"); long version1 = 1; State state1 = State.ENABLED; byte[] payload = Encoder.asUTF8("encryptedPayload"); RawSecretEntry entry1 = new RawSecretEntry(secretIdentifier1, version1, state1, Optional.empty(), Optional.empty(), payload); SecretIdentifier secretIdentifier2 = new SecretIdentifier("MySecret2"); long version2 = 2; Optional<ZonedDateTime> notBeforeValue = Optional.of(ZonedDateTime.of(2016, 5, 4, 2, 0 ,0, 0, ZoneId.of("UTC"))); RawSecretEntry entry2 = new RawSecretEntry(secretIdentifier2, version2, state1, notBeforeValue, Optional.empty(), payload); try (File store = new File(new java.io.File(path), new DummyEncryptor(), new FileEncryptionContext(group), new ReentrantReadWriteLock())) { if (store.exists()) { store.delete(); } store.create(entry1); store.create(entry2); } // auto closeable should write the results to disk when exiting the try clause, and thus be readable in the next section try (File file = new File(new java.io.File(path), new DummyEncryptor(), new FileEncryptionContext(group), new ReentrantReadWriteLock())) { List<RawSecretEntry> list = file.stream().toList(); boolean t = list.get(1).equals(entry2); assertThat(list, containsInAnyOrder(entry1, entry2)); } java.io.File f = new java.io.File(path); if (!f.delete()) { throw new UnexpectedStateException(path, "EXISTS", "DELETED", "File store deletion failed"); } }
/** * Create a PS2PSPusherImpl * @param context PS context */ public PS2PSPusherImpl(PSContext context) { this.context = context; this.psClient = new PSClient(context); this.failedUpdateCounters = new HashMap<>(); this.lock = new ReentrantReadWriteLock(); this.stopped = new AtomicBoolean(false); }
public void testReadLockToString(boolean fair) { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); assertTrue(lock.readLock().toString().contains("Read locks = 0")); lock.readLock().lock(); assertTrue(lock.readLock().toString().contains("Read locks = 1")); lock.readLock().lock(); assertTrue(lock.readLock().toString().contains("Read locks = 2")); lock.readLock().unlock(); assertTrue(lock.readLock().toString().contains("Read locks = 1")); lock.readLock().unlock(); assertTrue(lock.readLock().toString().contains("Read locks = 0")); }
/** * Creates a new expandable binary dictionary. * * @param context The application context of the parent. * @param dictName The name of the dictionary. Multiple instances with the same * name is supported. * @param locale the dictionary locale. * @param dictType the dictionary type, as a human-readable string * @param dictFile dictionary file path. if null, use default dictionary path based on * dictionary type. */ public ExpandableBinaryDictionary(final Context context, final String dictName, final Locale locale, final String dictType, final File dictFile) { super(dictType, locale); mDictName = dictName; mContext = context; mDictFile = getDictFile(context, dictName, dictFile); mBinaryDictionary = null; mIsReloading = new AtomicBoolean(); mNeedsToRecreate = false; mLock = new ReentrantReadWriteLock(); }
@Test public void testWriteLock() { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); AutoReadWriteLock al = new AutoReadWriteLock(lock); assertFalse(lock.isWriteLocked()); try (AutoLock al2 = al.autoWriteLock()) { assertTrue(lock.isWriteLocked()); } assertFalse(lock.isWriteLocked()); }
public ApplicationImpl(Dispatcher dispatcher, String user, ApplicationId appId, Credentials credentials, Context context) { this.dispatcher = dispatcher; this.user = user; this.appId = appId; this.credentials = credentials; this.aclsManager = context.getApplicationACLsManager(); this.context = context; ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); readLock = lock.readLock(); writeLock = lock.writeLock(); stateMachine = stateMachineFactory.make(this); }
public void testGetWaitQueueLengthIMSE(boolean fair) { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); try { lock.getWaitQueueLength(c); shouldThrow(); } catch (IllegalMonitorStateException success) {} }
/** * Init the Attempt for PS * @param ip excepted host for this ps attempt * @param psId ps id * @param attemptIndex attempt index * @param amContext Master context */ public PSAttempt(String ip, ParameterServerId psId, int attemptIndex, AMContext amContext) { this.expectedIp = ip; attemptId = new PSAttemptId(psId, attemptIndex); this.context = amContext; stateMachine = stateMachineFactory.make(this); ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); metrices = new HashMap<String, String>(); diagnostics = new ArrayList<String>(); }
public YarnContainerAllocator(AMContext context) { super(YarnContainerAllocator.class.getName()); this.context = context; this.recordFactory = RecordFactoryProvider.getRecordFactory(null); stopped = new AtomicBoolean(false); ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); }
public AMMatrixMetaManager(AMContext context) { this.context = context; matrixMetaManager = new MatrixMetaManager(); matrixPartitionsOnPS = new HashMap<>(); matrixIdToPSSetMap = new HashMap<>(); psIdToMatrixIdsMap = new HashMap<>(); psIdToRecoverPartsMap = new ConcurrentHashMap<>(); ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); }
private void readWriteLock() { ExecutorService executor = Executors.newFixedThreadPool(2); Map<String, String> map = new HashMap<>(); ReadWriteLock lock = new ReentrantReadWriteLock(); executor.submit(() -> { lock.writeLock().lock(); try { sleep(1); map.put("foo", "bar"); } finally { lock.writeLock().unlock(); } }); Runnable readTask = () -> { lock.readLock().lock(); try { System.out.println(map.get("foo")); sleep(1); } finally { lock.readLock().unlock(); } }; executor.submit(readTask); executor.submit(readTask); stop(executor); }
/** * Construct a new repository with the given default domain. */ public Repository(String domain, boolean fairLock) { lock = new ReentrantReadWriteLock(fairLock); domainTb = new HashMap<String,Map<String,NamedObject>>(5); if (domain != null && domain.length() != 0) this.domain = domain.intern(); // we use == domain later on... else this.domain = ServiceName.DOMAIN; // Creates a new hashtable for the default domain domainTb.put(this.domain, new HashMap<String,NamedObject>()); }
@Override public void writeLock(String resourceName) { synchronized(lockObject) { if (!binariesWriteLocks.containsKey(resourceName)) { binariesWriteLocks.put(resourceName, new ReentrantReadWriteLock()); } } binariesWriteLocks.get(resourceName).writeLock().lock(); LOGGER.info("Added write lock for resource {}", resourceName); }
PartitionedBloomFilter(BitSet bits, int numHashFunctions, HashFunction strategy, long sliceSize) { log.log(Level.FINE, String.format( "PartitionedBloomFilter: %1$d hash functions, %2$d bits, %3$d slice length", numHashFunctions, bits.bitSize(), sliceSize)); this.bits = bits; this.numHashFunctions = numHashFunctions; this.strategy = strategy; this.sliceSize = sliceSize; // sliceSize must be equals sliceSize*numHashFunctions this.numItems = new AtomicLong(0); for(int i=0;i<DEFAULT_CONCURRENCY_LEVEL;i++) { segments[i] = new ReentrantReadWriteLock(); } }
private PropertyValuesHolder(Property property) { this.mSetter = null; this.mGetter = null; this.mKeyframeSet = null; this.mPropertyMapLock = new ReentrantReadWriteLock(); this.mTmpValueArray = new Object[1]; this.mProperty = property; if (property != null) { this.mPropertyName = property.getName(); } }
public LocalizedResource(LocalResourceRequest rsrc, Dispatcher dispatcher) { this.rsrc = rsrc; this.dispatcher = dispatcher; this.ref = new LinkedList<ContainerId>(); ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); this.readLock = readWriteLock.readLock(); this.writeLock = readWriteLock.writeLock(); this.stateMachine = stateMachineFactory.make(this); }
public ClimateRepository() { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); this.readLock = lock.readLock(); this.writeLock = lock.writeLock(); ClimateDto climate = new ClimateDto(); climate.setTemperature(10); climate.setHumidity(80); this.climate = climate; }
@BeforeMethod public void setUp() throws Exception { this.mockDynamoDBClient = mock(AmazonDynamoDBClient.class); AWSCredentialsProvider mockCredentials = mock(AWSCredentialsProvider.class); ClientConfiguration mockConfig = mock(ClientConfiguration.class); this.dynamoDB = new DynamoDB(mockDynamoDBClient, mockCredentials, mockConfig, groupIdentifier, new ReentrantReadWriteLock()); }
public RowStoreAVLDisk(DataFileCache cache, Table table) { this(table); this.cache = cache; rowOut = cache.rowOut.duplicate(); cache.adjustStoreCount(1); largeData = database.logger.propLargeData; tableSpace = cache.spaceManager.getTableSpace(table.getSpaceID()); lock = new ReentrantReadWriteLock(); readLock = lock.readLock(); writeLock = lock.writeLock(); }
public RowStoreAVLDiskData(Table table) { this.database = table.database; this.table = table; this.indexList = table.getIndexList(); this.accessorList = new CachedObject[indexList.length]; lock = new ReentrantReadWriteLock(); readLock = lock.readLock(); writeLock = lock.writeLock(); }
public void testAwaitUntil_Timeout(boolean fair) { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); lock.writeLock().lock(); // We shouldn't assume that nanoTime and currentTimeMillis // use the same time source, so don't use nanoTime here. final java.util.Date delayedDate = delayedDate(timeoutMillis()); try { assertFalse(c.awaitUntil(delayedDate)); } catch (InterruptedException fail) { threadUnexpectedException(fail); } assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); lock.writeLock().unlock(); }
public void testReadUnlock_IMSE(boolean fair) { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); try { lock.readLock().unlock(); shouldThrow(); } catch (IllegalMonitorStateException success) {} }
public void testAwait_Timeout(boolean fair) { final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); final Condition c = lock.writeLock().newCondition(); final long timeoutMillis = timeoutMillis(); lock.writeLock().lock(); final long startTime = System.nanoTime(); try { assertFalse(c.await(timeoutMillis, MILLISECONDS)); } catch (InterruptedException fail) { threadUnexpectedException(fail); } assertTrue(millisElapsedSince(startTime) >= timeoutMillis); lock.writeLock().unlock(); }