Search in sources :

Example 16 with ImmutableTriple

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;
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) JobDoesNotExistException(alluxio.exception.JobDoesNotExistException) UnavailableException(alluxio.exception.status.UnavailableException) BlockInfoException(alluxio.exception.BlockInfoException) InodeFile(alluxio.master.file.meta.InodeFile) JobDoesNotExistException(alluxio.exception.JobDoesNotExistException) ResourceExhaustedException(alluxio.exception.status.ResourceExhaustedException) BlockInfoException(alluxio.exception.BlockInfoException) IOException(java.io.IOException) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) UnavailableException(alluxio.exception.status.UnavailableException) Triple(org.apache.commons.lang3.tuple.Triple) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) LockedInodePath(alluxio.master.file.meta.LockedInodePath) ResourceExhaustedException(alluxio.exception.status.ResourceExhaustedException) BlockInfo(alluxio.wire.BlockInfo) HashSet(java.util.HashSet) AlluxioURI(alluxio.AlluxioURI)

Example 17 with ImmutableTriple

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);
            }
        }
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Triple(org.apache.commons.lang3.tuple.Triple) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) SpatialDetector(org.valkyrienskies.mod.common.ships.block_relocation.SpatialDetector) TIntArrayList(gnu.trove.list.array.TIntArrayList) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ITerrainOctreeProvider(org.valkyrienskies.mod.common.util.datastructures.ITerrainOctreeProvider) ArrayList(java.util.ArrayList) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) ValkyrienSkiesMod(org.valkyrienskies.mod.common.ValkyrienSkiesMod) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) Vector3d(org.joml.Vector3d) PhysicsCalculations(org.valkyrienskies.mod.common.physics.PhysicsCalculations) Chunk(net.minecraft.world.chunk.Chunk) Triple(org.apache.commons.lang3.tuple.Triple) TIntList(gnu.trove.list.TIntList) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) IBitOctree(org.valkyrienskies.mod.common.util.datastructures.IBitOctree) ChunkCache(net.minecraft.world.ChunkCache) Iterator(java.util.Iterator) Collection(java.util.Collection) TransformType(valkyrienwarfare.api.TransformType) BlockPos(net.minecraft.util.math.BlockPos) Consumer(java.util.function.Consumer) IBlockState(net.minecraft.block.state.IBlockState) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) List(java.util.List) VSConfig(org.valkyrienskies.mod.common.config.VSConfig) MathHelper(net.minecraft.util.math.MathHelper) Vector3dc(org.joml.Vector3dc) ExtendedBlockStorage(net.minecraft.world.chunk.storage.ExtendedBlockStorage) ChunkCache(net.minecraft.world.ChunkCache) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) BlockPos(net.minecraft.util.math.BlockPos) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 18 with ImmutableTriple

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);
            }
        }
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) Triple(org.apache.commons.lang3.tuple.Triple) TIntList(gnu.trove.list.TIntList) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) SpatialDetector(org.valkyrienskies.mod.common.ships.block_relocation.SpatialDetector) IBitOctree(org.valkyrienskies.mod.common.util.datastructures.IBitOctree) TIntArrayList(gnu.trove.list.array.TIntArrayList) ChunkCache(net.minecraft.world.ChunkCache) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) TransformType(valkyrienwarfare.api.TransformType) BlockPos(net.minecraft.util.math.BlockPos) ITerrainOctreeProvider(org.valkyrienskies.mod.common.util.datastructures.ITerrainOctreeProvider) ArrayList(java.util.ArrayList) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) Consumer(java.util.function.Consumer) List(java.util.List) VSConfig(org.valkyrienskies.mod.common.config.VSConfig) MathHelper(net.minecraft.util.math.MathHelper) ValkyrienSkiesMod(org.valkyrienskies.mod.common.ValkyrienSkiesMod) Vector3d(org.joml.Vector3d) PhysicsCalculations(org.valkyrienskies.mod.common.physics.PhysicsCalculations) Chunk(net.minecraft.world.chunk.Chunk) Triple(org.apache.commons.lang3.tuple.Triple) ExtendedBlockStorage(net.minecraft.world.chunk.storage.ExtendedBlockStorage) ChunkCache(net.minecraft.world.ChunkCache) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.util.math.BlockPos) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 19 with ImmutableTriple

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);
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BigInteger(java.math.BigInteger) Triple(org.apache.commons.lang3.tuple.Triple) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

ImmutableTriple (org.apache.commons.lang3.tuple.ImmutableTriple)17 Triple (org.apache.commons.lang3.tuple.Triple)12 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 LockedInodePath (alluxio.master.file.meta.LockedInodePath)4 SetAndRestoreAuthenticatedUser (alluxio.SetAndRestoreAuthenticatedUser)3 MutableLockedInodePath (alluxio.master.file.meta.MutableLockedInodePath)3 Mode (alluxio.security.authorization.Mode)3 List (java.util.List)3 ConfigProperty (alluxio.grpc.ConfigProperty)2 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)2 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)2 StoredBlock (co.rsk.bitcoinj.core.StoredBlock)2 TIntList (gnu.trove.list.TIntList)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 InputStream (java.io.InputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 BigInteger (java.math.BigInteger)2 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2