List of usage examples for com.google.common.collect Lists newCopyOnWriteArrayList
@GwtIncompatible("CopyOnWriteArrayList") public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList()
From source file:io.druid.query.aggregation.atomcube.AtomCubeQuery.java
private static List<Interval> sortIntervals(List<Interval> intervals) { List<Interval> _intervals = Lists.newCopyOnWriteArrayList(); List<Interval> ret = Lists.newCopyOnWriteArrayList(); for (Interval interval : intervals) { _intervals.add(interval);/*w w w .ja v a 2 s .c o m*/ } while (!_intervals.isEmpty()) { Interval aInterval = null; for (Interval interval : _intervals) { if (aInterval == null) { aInterval = interval; } else { if (aInterval.getStartMillis() > interval.getStartMillis()) { aInterval = interval; } } } ret.add(aInterval); _intervals.remove(aInterval); } return ret; }
From source file:kr.debop4j.core.parallelism.Parallels.java
/** * Run list.//from w w w . j av a2 s . c o m * * @param fromInclude the from include * @param toExclude the to exclude * @param step the step * @param callable the callable * @return the list */ public static <V> List<V> run(int fromInclude, int toExclude, int step, final Callable<V> callable) { shouldNotBeNull(callable, "callable"); if (isDebugEnabled) log.debug( " ? ... fromInclude=[{}], toExclude=[{}], step=[{}], workerCount=[{}]", fromInclude, toExclude, step, getWorkerCount()); ExecutorService executor = Executors.newFixedThreadPool(getWorkerCount()); try { List<NumberRange.IntRange> partitions = NumberRange.partition(fromInclude, toExclude, step, getWorkerCount()); final Map<Integer, List<V>> localResults = Maps.newLinkedHashMap(); List<Callable<List<V>>> tasks = Lists.newLinkedList(); // False Sharing? for (int p = 0; p < partitions.size(); p++) { final NumberRange.IntRange partition = partitions.get(p); final List<V> localResult = Lists.newArrayListWithCapacity(partition.size()); localResults.put(p, localResult); Callable<List<V>> task = new Callable<List<V>>() { @Override public List<V> call() throws Exception { for (final int element : partition) localResult.add(callable.call()); return localResult; } }; tasks.add(task); } executor.invokeAll(tasks); List<V> results = Lists.newCopyOnWriteArrayList(); for (int i = 0; i < partitions.size(); i++) { results.addAll(localResults.get(i)); } if (isDebugEnabled) log.debug(" ? . workerCount=[{}]", getWorkerCount()); return results; } catch (Exception e) { log.error("??? ?.", e); throw new RuntimeException(e); } finally { executor.shutdown(); } }
From source file:com.cisco.oss.foundation.http.AbstractHttpClient.java
private void createServerListFromConfig() { if (!metadata.isServiceDirectoryEnabled()) { List<InternalServerProxy> serversList = Lists.newCopyOnWriteArrayList(); // based on the data collected from the config file - updates the server // list//from w ww . ja v a2 s . c o m AbstractLoadBalancerStrategy.readWriteLock.writeLock().lock(); try { serversList = updateServerListBasedOnConfig(serversList, metadata); } finally { AbstractLoadBalancerStrategy.readWriteLock.writeLock().unlock(); } if (serversList.isEmpty()) { LOGGER.debug("No hosts defined for api: \"" + apiName + "\". Please check your config files!"); } loadBalancerStrategy.setServerProxies(serversList); } }
From source file:com.jivesoftware.os.amza.service.AmzaServiceInitializer.java
public AmzaService initialize(AmzaServiceConfig config, AmzaInterner amzaInterner, AquariumStats aquariumStats, AmzaStats amzaSystemStats, AmzaStats amzaStats, HealthTimer quorumLatency, SystemRingSizeProvider systemRingSizeProvider, SickThreads sickThreads, SickPartitions sickPartitions, BinaryPrimaryRowMarshaller primaryRowMarshaller, BinaryHighwaterRowMarshaller highwaterRowMarshaller, RingMember ringMember, RingHost ringHost, Set<RingMember> blacklistRingMembers, TimestampedOrderIdProvider orderIdProvider, IdPacker idPacker, PartitionPropertyMarshaller partitionPropertyMarshaller, IndexProviderRegistryCallback indexProviderRegistryCallback, AvailableRowsTaker availableRowsTaker, RowsTakerFactory systemRowsTakerFactory, RowsTakerFactory rowsTakerFactory, Optional<TakeFailureListener> takeFailureListener, RowChanges allRowChanges, AmzaThreadPoolProvider amzaThreadPoolProvider) throws Exception { AmzaPartitionWatcher amzaSystemPartitionWatcher = new AmzaPartitionWatcher(true, allRowChanges); int numberOfStripes = config.workingDirectories.length; //TODO configure MemoryBackedRowIOProvider ephemeralRowIOProvider = new MemoryBackedRowIOProvider( config.initialBufferSegmentSize, config.maxBufferSegmentSize, config.updatesBetweenLeaps, config.maxLeaps, new DirectByteBufferFactory()); BinaryRowIOProvider persistentRowIOProvider = new BinaryRowIOProvider(config.updatesBetweenLeaps, config.maxLeaps, config.useMemMap); File[] workingWALDirectories = new File[config.workingDirectories.length]; File[] workingIndexDirectories = new File[config.workingDirectories.length]; for (int i = 0; i < workingWALDirectories.length; i++) { workingWALDirectories[i] = new File(config.workingDirectories[i], "wal"); workingIndexDirectories[i] = new File(config.workingDirectories[i], "index"); }// w ww . j a v a2 s . co m WALIndexProviderRegistry indexProviderRegistry = new WALIndexProviderRegistry(ephemeralRowIOProvider, persistentRowIOProvider); indexProviderRegistryCallback.call(workingIndexDirectories, indexProviderRegistry, ephemeralRowIOProvider, persistentRowIOProvider, numberOfStripes); IndexedWALStorageProvider walStorageProvider = new IndexedWALStorageProvider(amzaStats, workingWALDirectories, numberOfStripes, indexProviderRegistry, primaryRowMarshaller, highwaterRowMarshaller, orderIdProvider, sickPartitions, config.tombstoneCompactionFactor, config.rebalanceIfImbalanceGreaterThanNBytes); int numProc = Runtime.getRuntime().availableProcessors(); PartitionIndex partitionIndex = new PartitionIndex(amzaSystemStats, amzaStats, orderIdProvider, walStorageProvider, numProc, amzaThreadPoolProvider.allocateThreadPool(numProc, "partition-loader")); AsyncStripeFlusher systemFlusher = new AsyncStripeFlusher(-1, config.asyncFsyncIntervalMillis, null); SystemWALStorage systemWALStorage = new SystemWALStorage(amzaSystemStats, partitionIndex, primaryRowMarshaller, highwaterRowMarshaller, amzaSystemPartitionWatcher, systemFlusher, config.hardFsync); File[] walDirs = new File[numberOfStripes]; long[] stripeVersions = new long[numberOfStripes]; FileLock[] stripeLocks = new FileLock[numberOfStripes]; for (int i = 0; i < numberOfStripes; i++) { walDirs[i] = new File(config.workingDirectories[i % config.workingDirectories.length], "delta-wal-" + i); if (!walDirs[i].exists()) { if (!walDirs[i].mkdirs()) { throw new IllegalStateException( "Please check your file permission. " + walDirs[i].getAbsolutePath()); } } File versionFile = new File(walDirs[i], "version"); if (versionFile.exists()) { stripeLocks[i] = FileChannel.open(versionFile.toPath(), StandardOpenOption.WRITE).lock(); try (FileInputStream fileInputStream = new FileInputStream(versionFile)) { DataInput input = new DataInputStream(fileInputStream); stripeVersions[i] = input.readLong(); LOG.info("Loaded stripeVersion:" + stripeVersions[i] + " for stripe:" + i + " from " + versionFile); } } else if (versionFile.createNewFile()) { stripeLocks[i] = FileChannel.open(versionFile.toPath(), StandardOpenOption.WRITE).lock(); try (FileOutputStream fileOutputStream = new FileOutputStream(versionFile)) { DataOutput output = new DataOutputStream(fileOutputStream); stripeVersions[i] = orderIdProvider.nextId(); output.writeLong(stripeVersions[i]); LOG.info("Created stripeVersion:" + stripeVersions[i] + " for stripe:" + i + " to " + versionFile); } } else { throw new IllegalStateException( "Please check your file permission. " + versionFile.getAbsolutePath()); } } ConcurrentBAHash<CacheId<RingTopology>> ringsCache = new ConcurrentBAHash<>(13, true, numProc); ConcurrentBAHash<CacheId<RingSet>> ringMemberRingNamesCache = new ConcurrentBAHash<>(13, true, numProc); List<WALUpdated> walUpdateDelegates = Lists.newCopyOnWriteArrayList(); WALUpdated walUpdated = (versionedPartitionName, txId) -> { for (WALUpdated delegate : walUpdateDelegates) { delegate.updated(versionedPartitionName, txId); } }; PartitionCreator partitionCreator = new PartitionCreator(orderIdProvider, partitionPropertyMarshaller, partitionIndex, systemWALStorage, walUpdated, allRowChanges, amzaInterner); TakeFullySystemReady systemReady = new TakeFullySystemReady(systemRingSizeProvider, partitionCreator, sickPartitions, sickThreads); AtomicLong nodeCacheId = new AtomicLong(0); AmzaRingStoreReader ringStoreReader = new AmzaRingStoreReader(systemReady, amzaInterner, ringMember, ringsCache, ringMemberRingNamesCache, nodeCacheId, ImmutableSet.copyOf(blacklistRingMembers)); AwaitNotify<PartitionName> awaitOnline = new AwaitNotify<>(config.awaitOnlineStripingLevel); long maxUpdatesBeforeCompaction = config.maxUpdatesBeforeDeltaStripeCompaction; AckWaters ackWaters = new AckWaters(amzaSystemStats, amzaStats, quorumLatency, config.ackWatersStripingLevel, config.ackWatersVerboseLogTimeouts); DeltaStripeWALStorage[] deltaStripeWALStorages = new DeltaStripeWALStorage[numberOfStripes]; BinaryRowIOProvider deltaRowIOProvider = new BinaryRowIOProvider(-1, 0, config.useMemMap); int deltaMergeThreads = config.deltaMergeThreads; if (deltaMergeThreads <= 0) { deltaMergeThreads = numProc; } for (int i = 0; i < numberOfStripes; i++) { DeltaWALFactory deltaWALFactory = new DeltaWALFactory(orderIdProvider, walDirs[i], deltaRowIOProvider, primaryRowMarshaller, highwaterRowMarshaller, config.corruptionParanoiaFactor); deltaStripeWALStorages[i] = new DeltaStripeWALStorage(amzaInterner, i, amzaStats, ackWaters, sickThreads, ringStoreReader, deltaWALFactory, config.deltaMaxValueSizeInIndex, indexProviderRegistry, maxUpdatesBeforeCompaction, amzaThreadPoolProvider.allocateThreadPool(deltaMergeThreads, "merge-deltas-" + i)); } long stripeMaxFreeWithinNBytes = config.rebalanceIfImbalanceGreaterThanNBytes / 2; //TODO config separately StorageVersionProvider storageVersionProvider = new StorageVersionProvider(amzaInterner, orderIdProvider, ringMember, systemWALStorage, partitionCreator, ringStoreReader, workingIndexDirectories, stripeVersions, stripeLocks, stripeMaxFreeWithinNBytes, deltaStripeWALStorages, walUpdated, awaitOnline); amzaSystemPartitionWatcher.watch(PartitionCreator.PARTITION_VERSION_INDEX.getPartitionName(), storageVersionProvider); amzaSystemPartitionWatcher.watch(PartitionCreator.REGION_PROPERTIES.getPartitionName(), partitionCreator); TakeCoordinator takeCoordinator = new TakeCoordinator(systemWALStorage, ringMember, amzaSystemStats, amzaStats, orderIdProvider, idPacker, partitionCreator, config.takeCyaIntervalInMillis, config.takeSlowThresholdInMillis, config.takeSystemReofferDeltaMillis, config.takeReofferDeltaMillis, config.takeReofferMaxElectionsPerHeartbeat, config.hangupAvailableRowsAfterUnresponsiveMillis); walUpdateDelegates.add((versionedPartitionName, txId) -> { takeCoordinator.update(ringStoreReader, Preconditions.checkNotNull(versionedPartitionName), txId); }); long startupVersion = orderIdProvider.nextId(); Member rootAquariumMember = ringMember.asAquariumMember(); AmzaLivelinessStorage livelinessStorage = new AmzaLivelinessStorage(systemWALStorage, orderIdProvider, walUpdated, rootAquariumMember, startupVersion); AtQuorum livelinessAtQuorm = count -> { int ringSize = systemRingSizeProvider.get(); return ringSize > 0 && count > ringSize / 2; }; Liveliness liveliness = new Liveliness(aquariumStats, System::currentTimeMillis, livelinessStorage, rootAquariumMember, livelinessAtQuorm, config.aquariumLeaderDeadAfterMillis, new AtomicLong(-1)); AmzaAquariumProvider aquariumProvider = new AmzaAquariumProvider(aquariumStats, amzaInterner, ringMember, orderIdProvider, ringStoreReader, systemWALStorage, storageVersionProvider, partitionCreator, takeCoordinator, walUpdated, liveliness, config.aquariumLivelinessFeedEveryMillis, awaitOnline, sickThreads); amzaSystemPartitionWatcher.watch(PartitionCreator.AQUARIUM_STATE_INDEX.getPartitionName(), aquariumProvider); AmzaPartitionWatcher amzaStripedPartitionWatcher = new AmzaPartitionWatcher(false, allRowChanges); HighwaterStorage highwaterStorage = new PartitionBackedHighwaterStorage(amzaSystemStats, amzaStats, amzaInterner, orderIdProvider, ringMember, partitionCreator, systemWALStorage, walUpdated, config.flushHighwatersAfterNUpdates, numberOfStripes); AsyncStripeFlusher[] stripeFlusher = new AsyncStripeFlusher[numberOfStripes]; for (int i = 0; i < numberOfStripes; i++) { int index = i; stripeFlusher[i] = new AsyncStripeFlusher(index, config.asyncFsyncIntervalMillis, () -> { deltaStripeWALStorages[index].flush(true); return null; }); } PartitionStripeProvider partitionStripeProvider = new PartitionStripeProvider(amzaStats, partitionCreator, partitionIndex, primaryRowMarshaller, deltaStripeWALStorages, highwaterStorage, highwaterRowMarshaller, ringMember, ringStoreReader, aquariumProvider, storageVersionProvider, takeCoordinator, awaitOnline, amzaStripedPartitionWatcher, systemFlusher, stripeFlusher, config.deltaStripeCompactionIntervalInMillis, amzaThreadPoolProvider.allocateThreadPool(deltaStripeWALStorages.length, "compact-deltas"), amzaThreadPoolProvider.allocateThreadPool(deltaStripeWALStorages.length + 1, "stripe-flusher")); PartitionComposter partitionComposter = new PartitionComposter(amzaSystemStats, amzaStats, partitionIndex, partitionCreator, ringStoreReader, partitionStripeProvider, storageVersionProvider, amzaInterner, numProc); amzaSystemPartitionWatcher.watch(PartitionCreator.REGION_INDEX.getPartitionName(), partitionComposter); amzaSystemPartitionWatcher.watch(PartitionCreator.PARTITION_VERSION_INDEX.getPartitionName(), partitionComposter); amzaSystemPartitionWatcher.watch(PartitionCreator.AQUARIUM_STATE_INDEX.getPartitionName(), partitionComposter); AmzaRingStoreWriter amzaRingWriter = new AmzaRingStoreWriter(ringStoreReader, systemWALStorage, orderIdProvider, walUpdated, ringsCache, ringMemberRingNamesCache, nodeCacheId, config.rackDistributionEnabled); amzaSystemPartitionWatcher.watch(PartitionCreator.RING_INDEX.getPartitionName(), amzaRingWriter); amzaSystemPartitionWatcher.watch(PartitionCreator.NODE_INDEX.getPartitionName(), amzaRingWriter); int systemReadyInitConcurrencyLevel = config.systemReadyInitConcurrencyLevel; for (int i = 0; i < systemReadyInitConcurrencyLevel; i++) { int index = i; systemReady.onReady(() -> { LOG.info("Loading highest txIds for index:{} after system ready...", index); int count = 0; for (PartitionName partitionName : partitionCreator.getMemberPartitions(ringStoreReader)) { if (index == Math.abs(partitionName.hashCode() % systemReadyInitConcurrencyLevel)) { count++; try { partitionStripeProvider.txPartition(partitionName, (txPartitionStripe, highwaterStorage1, versionedAquarium) -> { return txPartitionStripe.tx((deltaIndex, stripeIndex, partitionStripe) -> { VersionedPartitionName versionedPartitionName = versionedAquarium .getVersionedPartitionName(); PartitionStore partitionStore = partitionCreator .get(versionedPartitionName, stripeIndex); if (partitionStore != null) { takeCoordinator.update(ringStoreReader, versionedPartitionName, partitionStore.highestTxId()); } else { LOG.warn( "Skipped system ready init for a partition, likely because it is only partially defined: {}", versionedPartitionName); } return null; }); }); } catch (PartitionIsDisposedException x) { LOG.info("Skipped a partition because its disposed: {}", partitionName); } catch (PropertiesNotPresentException x) { LOG.warn( "Skipped system ready init for a partition because its properties were missing: {}", partitionName); } catch (Exception x) { LOG.error("Failed system ready init for a partition, please fix: {}", new Object[] { partitionName }, x); } } } LOG.info("Finished loading {} highest txIds for index:{} after system ready!", count, index); return null; }); } RowChangeTaker changeTaker = new RowChangeTaker(amzaSystemStats, amzaStats, numberOfStripes, storageVersionProvider, ringStoreReader, systemReady, ringHost, systemRowsTakerFactory.create(), rowsTakerFactory.create(), partitionStripeProvider, availableRowsTaker, amzaThreadPoolProvider.allocateThreadPool(config.numberOfTakerThreads, "row-taker"), new SystemPartitionCommitChanges(storageVersionProvider, systemWALStorage, highwaterStorage, walUpdated), new StripedPartitionCommitChanges(partitionStripeProvider, config.hardFsync, walUpdated), new OrderIdProviderImpl(new ConstantWriterIdProvider(1)), takeFailureListener, config.takeLongPollTimeoutMillis, config.pongIntervalMillis, config.rowsTakerLimit, primaryRowMarshaller, highwaterRowMarshaller); PartitionTombstoneCompactor partitionCompactor = new PartitionTombstoneCompactor(amzaStats, walStorageProvider, partitionCreator, partitionIndex, storageVersionProvider, config.checkIfCompactionIsNeededIntervalInMillis, config.rebalanceableEveryNMillis, numberOfStripes); return new AmzaService(orderIdProvider, amzaSystemStats, amzaStats, numberOfStripes, indexProviderRegistry, storageVersionProvider, ringStoreReader, amzaRingWriter, ackWaters, systemWALStorage, highwaterStorage, takeCoordinator, changeTaker, partitionCompactor, partitionComposter, // its all about being GREEN!! partitionIndex, partitionCreator, partitionStripeProvider, walUpdated, amzaSystemPartitionWatcher, amzaStripedPartitionWatcher, aquariumProvider, systemReady, liveliness); }
From source file:silentium.gameserver.model.CharEffectList.java
protected void addEffectFromQueue(L2Effect newEffect) { if (newEffect == null) return;//from ww w . j a v a2s .co m L2Skill newSkill = newEffect.getSkill(); // array modified, then rebuild on next request _rebuildCache = true; if (newSkill.isDebuff()) { if (_debuffs == null) _debuffs = Lists.newCopyOnWriteArrayList(); for (L2Effect e : _debuffs) { if (e != null && e.getSkill().getId() == newEffect.getSkill().getId() && e.getEffectType() == newEffect.getEffectType() && e.getStackOrder() == newEffect.getStackOrder() && e.getStackType().equals(newEffect.getStackType())) { // Started scheduled timer needs to be canceled. newEffect.stopEffectTask(); return; } } _debuffs.add(newEffect); } else { if (_buffs == null) _buffs = Lists.newCopyOnWriteArrayList(); for (L2Effect e : _buffs) { if (e != null && e.getSkill().getId() == newEffect.getSkill().getId() && e.getEffectType() == newEffect.getEffectType() && e.getStackOrder() == newEffect.getStackOrder() && e.getStackType().equals(newEffect.getStackType())) { e.exit(); // exit this } } // if max buffs, no herb effects are used, even if they would replace one old if (newEffect.isHerbEffect() && getBuffCount() >= _owner.getMaxBuffCount()) { newEffect.stopEffectTask(); return; } // Remove first buff when buff list is full if (!doesStack(newSkill) && !newSkill.is7Signs()) { int effectsToRemove = getBuffCount() - _owner.getMaxBuffCount(); if (effectsToRemove >= 0) { switch (newSkill.getSkillType()) { case BUFF: case REFLECT: case HEAL_PERCENT: case MANAHEAL_PERCENT: for (L2Effect e : _buffs) { if (e == null) continue; switch (e.getSkill().getSkillType()) { case BUFF: case REFLECT: case HEAL_PERCENT: case MANAHEAL_PERCENT: e.exit(); effectsToRemove--; break; // break switch() default: continue; // continue for() } if (effectsToRemove < 0) break; // break for() } } } } // Icons order: buffs then toggles if (newSkill.isToggle()) _buffs.add(newEffect); else { int pos = 0; for (L2Effect e : _buffs) { if (e == null || e.getSkill().isToggle() || e.getSkill().is7Signs()) continue; pos++; } _buffs.add(pos, newEffect); } } // Check if a stack group is defined for this effect if ("none".equals(newEffect.getStackType())) { // Set this L2Effect to In Use if (newEffect.setInUse(true)) // Add Funcs of this effect to the Calculator set of the L2Character _owner.addStatFuncs(newEffect.getStatFuncs()); return; } List<L2Effect> stackQueue; L2Effect effectToAdd = null; L2Effect effectToRemove = null; if (_stackedEffects == null) _stackedEffects = new FastMap<>(); // Get the list of all stacked effects corresponding to the stack type of the L2Effect to add stackQueue = _stackedEffects.get(newEffect.getStackType()); if (stackQueue != null) { int pos = 0; if (!stackQueue.isEmpty()) { // Get the first stacked effect of the Stack group selected effectToRemove = listsContains(stackQueue.get(0)); // Create an Iterator to go through the list of stacked effects in progress on the L2Character Iterator<L2Effect> queueIterator = stackQueue.iterator(); while (queueIterator.hasNext()) { if (newEffect.getStackOrder() < queueIterator.next().getStackOrder()) pos++; else break; } // Add the new effect to the Stack list in function of its position in the Stack group stackQueue.add(pos, newEffect); // skill.exit() could be used, if the users don't wish to see "effect // removed" always when a timer goes off, even if the buff isn't active // any more (has been replaced). but then check e.g. npc hold and raid petrification. if (PlayersConfig.EFFECT_CANCELING && !newEffect.isHerbEffect() && stackQueue.size() > 1) { if (newSkill.isDebuff()) _debuffs.remove(stackQueue.remove(1)); else _buffs.remove(stackQueue.remove(1)); } } else stackQueue.add(0, newEffect); } else { stackQueue = new FastList<>(); stackQueue.add(0, newEffect); } // Update the Stack Group table _stackedEffects of the L2Character _stackedEffects.put(newEffect.getStackType(), stackQueue); // Get the first stacked effect of the Stack group selected if (!stackQueue.isEmpty()) effectToAdd = listsContains(stackQueue.get(0)); if (effectToRemove != effectToAdd) { if (effectToRemove != null) { // Remove all Func objects corresponding to this stacked effect from the Calculator set of the L2Character _owner.removeStatsOwner(effectToRemove); // Set the L2Effect to Not In Use effectToRemove.setInUse(false); } if (effectToAdd != null) { // Set this L2Effect to In Use if (effectToAdd.setInUse(true)) // Add all Func objects corresponding to this stacked effect to the Calculator set of the L2Character _owner.addStatFuncs(effectToAdd.getStatFuncs()); } } }