use of org.apache.commons.lang3.tuple.ImmutableTriple in project alluxio by Alluxio.
the class ReplicationChecker method check.
private Set<Long> check(Set<Long> inodes, ReplicationHandler handler, Mode mode) throws InterruptedException {
Set<Long> processedFileIds = new HashSet<>();
for (long inodeId : inodes) {
if (mActiveJobToInodeID.size() >= mMaxActiveJobs) {
return processedFileIds;
}
if (mActiveJobToInodeID.containsValue(inodeId)) {
continue;
}
Set<Triple<AlluxioURI, Long, Integer>> requests = new HashSet<>();
// Throw if interrupted.
if (Thread.interrupted()) {
throw new InterruptedException("ReplicationChecker interrupted.");
}
// locking the entire path but just the inode file since this access is read-only.
try (LockedInodePath inodePath = mInodeTree.lockFullInodePath(inodeId, LockPattern.READ)) {
InodeFile file = inodePath.getInodeFile();
for (long blockId : file.getBlockIds()) {
BlockInfo blockInfo = null;
try {
blockInfo = mBlockMaster.getBlockInfo(blockId);
} catch (BlockInfoException e) {
// Cannot find this block in Alluxio from BlockMaster, possibly persisted in UFS
} catch (UnavailableException e) {
// The block master is not available, wait for the next heartbeat
LOG.warn("The block master is not available: {}", e.toString());
return processedFileIds;
}
int currentReplicas = (blockInfo == null) ? 0 : blockInfo.getLocations().size();
switch(mode) {
case EVICT:
int maxReplicas = file.getReplicationMax();
if (file.getPersistenceState() == PersistenceState.TO_BE_PERSISTED && file.getReplicationDurable() > maxReplicas) {
maxReplicas = file.getReplicationDurable();
}
if (currentReplicas > maxReplicas) {
requests.add(new ImmutableTriple<>(inodePath.getUri(), blockId, currentReplicas - maxReplicas));
}
break;
case REPLICATE:
int minReplicas = file.getReplicationMin();
if (file.getPersistenceState() == PersistenceState.TO_BE_PERSISTED && file.getReplicationDurable() > minReplicas) {
minReplicas = file.getReplicationDurable();
}
if (currentReplicas < minReplicas) {
// if this file is not persisted and block master thinks it is lost, no effort made
if (!file.isPersisted() && mBlockMaster.isBlockLost(blockId)) {
continue;
}
requests.add(new ImmutableTriple<>(inodePath.getUri(), blockId, minReplicas - currentReplicas));
}
break;
default:
LOG.warn("Unexpected replication mode {}.", mode);
}
}
} catch (FileDoesNotExistException e) {
LOG.warn("Failed to check replication level for inode id {} : {}", inodeId, e.toString());
}
for (Triple<AlluxioURI, Long, Integer> entry : requests) {
AlluxioURI uri = entry.getLeft();
long blockId = entry.getMiddle();
int numReplicas = entry.getRight();
try {
long jobId;
switch(mode) {
case EVICT:
jobId = handler.evict(uri, blockId, numReplicas);
break;
case REPLICATE:
jobId = handler.replicate(uri, blockId, numReplicas);
break;
default:
throw new RuntimeException(String.format("Unexpected replication mode {}.", mode));
}
processedFileIds.add(inodeId);
mActiveJobToInodeID.put(jobId, inodeId);
} catch (JobDoesNotExistException | ResourceExhaustedException e) {
LOG.warn("The job service is busy, will retry later. {}", e.toString());
return processedFileIds;
} catch (UnavailableException e) {
LOG.warn("Unable to complete the replication check: {}, will retry later.", e.toString());
return processedFileIds;
} catch (Exception e) {
SAMPLING_LOG.warn("Unexpected exception encountered when starting a {} job (uri={}," + " block ID={}, num replicas={}) : {}", mode, uri, blockId, numReplicas, e.toString());
LOG.debug("Job service unexpected exception: ", e);
}
}
}
return processedFileIds;
}
use of org.apache.commons.lang3.tuple.ImmutableTriple in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class WorldPhysicsCollider method updatePotentialCollisionCache.
// TODO: The greatest physics lag starts here.
private void updatePotentialCollisionCache() {
ticksSinceCacheUpdate = 0D;
// in the same tick
if (Math.random() > .5) {
ticksSinceCacheUpdate -= .05D;
}
int oldSize = cachedPotentialHits.size();
// Resets the potential hits array in O(1) time! Isn't that something.
// cachedPotentialHits.resetQuick();
cachedPotentialHits.clear();
AxisAlignedBB shipBBOriginal = parent.getPhysicsTransformAABB();
if (shipBBOriginal == null) {
return;
}
final AxisAlignedBB shipBB = shipBBOriginal.grow(3);
// Use the physics tick collision box instead of the game tick collision box.
// We are using grow(3) on both because for some reason if we don't then ships start
// jiggling through the ground. God I can't wait for a new physics engine.
final AxisAlignedBB collisionBB = shipBB.grow(AABB_EXPANSION).expand(calculator.getLinearVelocity().x * .2, calculator.getLinearVelocity().y * .2, calculator.getLinearVelocity().z * .2);
// Ship is outside of world blockSpace, just skip this all togvalkyrium
if (collisionBB.maxY < 0 || collisionBB.minY > 255) {
return;
}
// Has a -1 on the minY value, I hope this helps with preventing things from
// falling through the floor
BlockPos min = new BlockPos(collisionBB.minX, Math.max(collisionBB.minY - 1, 0), collisionBB.minZ);
BlockPos max = new BlockPos(collisionBB.maxX, Math.min(collisionBB.maxY, 255), collisionBB.maxZ);
centerPotentialHit = new BlockPos((min.getX() + max.getX()) / 2D, (min.getY() + max.getY()) / 2D, (min.getZ() + max.getZ()) / 2D);
ChunkCache cache = parent.getCachedSurroundingChunks();
if (cache == null) {
System.err.println("VS Cached Surrounding Chunks was null! This is going to cause catastophric terrible events!!");
return;
}
int chunkMinX = min.getX() >> 4;
int chunkMaxX = (max.getX() >> 4) + 1;
int chunkMinZ = min.getZ() >> 4;
int chunkMaxZ = (max.getZ() >> 4) + 1;
// long startTime = System.nanoTime();
int minX = min.getX();
int minY = min.getY();
int minZ = min.getZ();
int maxX = max.getX();
int maxY = max.getY();
int maxZ = max.getZ();
// More multithreading!
if (VSConfig.MULTITHREADING_SETTINGS.multithreadCollisionCacheUpdate && parent.getBlockPositions().size() > 100) {
List<Triple<Integer, Integer, TIntList>> tasks = new ArrayList<>();
for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
tasks.add(new ImmutableTriple<>(chunkX, chunkZ, new TIntArrayList()));
}
}
Consumer<Triple<Integer, Integer, TIntList>> consumer = i -> {
// i is a Tuple<Integer, Integer>
// updateCollisionCacheParrallel(cache, cachedPotentialHits, i.getFirst(),
// i.getSecond(), minX, minY, minZ, maxX, maxY, maxZ);
updateCollisionCacheSequential(cache, i.getLeft(), i.getMiddle(), minX, minY, minZ, maxX, maxY, maxZ, shipBB, i.getRight());
};
ValkyrienSkiesMod.getPhysicsThreadPool().submit(() -> tasks.parallelStream().forEach(consumer)).join();
tasks.forEach(task -> cachedPotentialHits.addAll(task.getRight()));
} else {
// Cast to double to avoid overflow errors
double size = ((double) (chunkMaxX - chunkMinX)) * ((double) (chunkMaxZ - chunkMinZ));
if (size > 300000) {
// Sanity check; don't execute the rest of the code because we'll just freeze the physics thread.
return;
}
// TODO: VS thread freezes here.
for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
updateCollisionCacheSequential(cache, chunkX, chunkZ, minX, minY, minZ, maxX, maxY, maxZ, shipBB, cachedPotentialHits);
}
}
}
}
use of org.apache.commons.lang3.tuple.ImmutableTriple in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class WorldWaterCollider method updatePotentialCollisionCache.
private void updatePotentialCollisionCache() {
secondsSinceCollisionCacheUpdate = 0;
// ships from all updating in the same tick
if (Math.random() > .5) {
secondsSinceCollisionCacheUpdate -= .01;
}
cachedPotentialHits.clear();
AxisAlignedBB shipBBOriginal = parent.getPhysicsTransformAABB();
if (shipBBOriginal == null) {
return;
}
// We are using grow(3) because its good.
final AxisAlignedBB shipBB = shipBBOriginal.grow(3);
final AxisAlignedBB collisionBB = shipBB.grow(AABB_EXPANSION).grow(2 * Math.ceil(RANGE_CHECK));
// Ship is outside of world blockSpace, just skip this
if (collisionBB.maxY < 0 || collisionBB.minY > 255) {
return;
}
// Has a -1 on the minY value, I hope this helps with preventing things from
// falling through the floor
final BlockPos min = new BlockPos(collisionBB.minX, Math.max(collisionBB.minY - 1, 0), collisionBB.minZ);
final BlockPos max = new BlockPos(collisionBB.maxX, Math.min(collisionBB.maxY, 255), collisionBB.maxZ);
centerPotentialHit = new BlockPos((min.getX() + max.getX()) / 2.0, (min.getY() + max.getY()) / 2.0, (min.getZ() + max.getZ()) / 2.0);
final ChunkCache cache = parent.getCachedSurroundingChunks();
if (cache == null) {
System.err.println("VS Cached Surrounding Chunks was null! This is going to cause catastophric terrible events!!");
return;
}
final int chunkMinX = min.getX() >> 4;
final int chunkMaxX = (max.getX() >> 4) + 1;
final int chunkMinZ = min.getZ() >> 4;
final int chunkMaxZ = (max.getZ() >> 4) + 1;
final int minX = min.getX();
final int minY = min.getY();
final int minZ = min.getZ();
final int maxX = max.getX();
final int maxY = max.getY();
final int maxZ = max.getZ();
// More multithreading!
if (VSConfig.MULTITHREADING_SETTINGS.multithreadCollisionCacheUpdate && parent.getBlockPositions().size() > 100) {
final List<Triple<Integer, Integer, TIntList>> tasks = new ArrayList<>();
for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
tasks.add(new ImmutableTriple<>(chunkX, chunkZ, new TIntArrayList()));
}
}
Consumer<Triple<Integer, Integer, TIntList>> consumer = i -> updateCollisionCacheSequential(cache, i.getLeft(), i.getMiddle(), minX, minY, minZ, maxX, maxY, maxZ, shipBB, i.getRight());
ValkyrienSkiesMod.getPhysicsThreadPool().submit(() -> tasks.parallelStream().forEach(consumer)).join();
tasks.forEach(task -> cachedPotentialHits.addAll(task.getRight()));
} else {
// Cast to double to avoid overflow errors
final double size = ((double) (chunkMaxX - chunkMinX)) * ((double) (chunkMaxZ - chunkMinZ));
if (size > 300000) {
// Sanity check; don't execute the rest of the code because we'll just freeze the physics thread.
return;
}
for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
updateCollisionCacheSequential(cache, chunkX, chunkZ, minX, minY, minZ, maxX, maxY, maxZ, shipBB, cachedPotentialHits);
}
}
}
}
use of org.apache.commons.lang3.tuple.ImmutableTriple in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method checkDifferentInstancesWithSameRepoHaveSameContentTest.
@Test
public void checkDifferentInstancesWithSameRepoHaveSameContentTest() throws Exception {
// This Is how I produced RepositoryBlockStore_data.ser. I had a bitcoind in regtest with 613 blocks + genesis block
// NetworkParameters params = RegTestParams.get();
// Context context = new Context(params);
// Wallet wallet = new Wallet(context);
// BlockStore store = new SPVBlockStore(params, new File("spvBlockstore"));
// AbstractBlockChain chain = new BlockChain(context, wallet, store);
// PeerGroup peerGroup = new PeerGroup(context, chain);
// peerGroup.start();
// final DownloadProgressTracker listener = new DownloadProgressTracker();
// peerGroup.startBlockChainDownload(listener);
// listener.await();
// peerGroup.stop();
// StoredBlock storedBlock = chain.getChainHead();
// FileOutputStream fos = new FileOutputStream("RepositoryBlockStore_data.ser");
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// for (int i = 0; i < 614; i++) {
// Triple<byte[], BigInteger , Integer> tripleStoredBlock = new ImmutableTriple<>(storedBlock.getHeader().bitcoinSerialize(), storedBlock.getChainWork(), storedBlock.getHeight());
// oos.writeObject(tripleStoredBlock);
// storedBlock = store.get(storedBlock.getHeader().getPrevBlockHash());
// }
// oos.close();
// Read original store
InputStream fileInputStream = ClassLoader.getSystemResourceAsStream("peg/RepositoryBlockStore_data.ser");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Repository repository = createRepository();
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
BtcBlockStoreWithCache store = btcBlockStoreFactory.newInstance(repository, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
for (int i = 0; i < 614; i++) {
Triple<byte[], BigInteger, Integer> tripleStoredBlock = (Triple<byte[], BigInteger, Integer>) objectInputStream.readObject();
BtcBlock header = RegTestParams.get().getDefaultSerializer().makeBlock(tripleStoredBlock.getLeft());
StoredBlock storedBlock = new StoredBlock(header, tripleStoredBlock.getMiddle(), tripleStoredBlock.getRight());
if (i == 0) {
store.setChainHead(storedBlock);
}
store.put(storedBlock);
}
// Create a new instance of the store
BtcBlockStoreWithCache store2 = btcBlockStoreFactory.newInstance(repository, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
// Check a specific block that used to fail when we had a bug
assertEquals(store.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")), store2.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")));
// Check new instance content is identical to the original one
StoredBlock storedBlock = store.getChainHead();
StoredBlock storedBlock2 = store2.getChainHead();
int headHeight = storedBlock.getHeight();
for (int i = 0; i < headHeight; i++) {
assertNotNull(storedBlock);
assertEquals(storedBlock, storedBlock2);
Sha256Hash prevBlockHash = storedBlock.getHeader().getPrevBlockHash();
storedBlock = store.get(prevBlockHash);
storedBlock2 = store2.get(prevBlockHash);
}
}
Aggregations