Search in sources :

Example 6 with Slot

use of org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot in project hadoop by apache.

the class TestShortCircuitCache method testUnlinkingReplicasInFileDescriptorCache.

/**
   * Test unlinking a file whose blocks we are caching in the DFSClient.
   * The DataNode will notify the DFSClient that the replica is stale via the
   * ShortCircuitShm.
   */
@Test(timeout = 60000)
public void testUnlinkingReplicasInFileDescriptorCache() throws Exception {
    BlockReaderTestUtil.enableShortCircuitShmTracing();
    TemporarySocketDirectory sockDir = new TemporarySocketDirectory();
    Configuration conf = createShortCircuitConf("testUnlinkingReplicasInFileDescriptorCache", sockDir);
    // We don't want the CacheCleaner to time out short-circuit shared memory
    // segments during the test, so set the timeout really high.
    conf.setLong(HdfsClientConfigKeys.Read.ShortCircuit.STREAMS_CACHE_EXPIRY_MS_KEY, 1000000000L);
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
    cluster.waitActive();
    DistributedFileSystem fs = cluster.getFileSystem();
    final ShortCircuitCache cache = fs.getClient().getClientContext().getShortCircuitCache();
    cache.getDfsClientShmManager().visit(new Visitor() {

        @Override
        public void visit(HashMap<DatanodeInfo, PerDatanodeVisitorInfo> info) throws IOException {
            // The ClientShmManager starts off empty.
            Assert.assertEquals(0, info.size());
        }
    });
    final Path TEST_PATH = new Path("/test_file");
    final int TEST_FILE_LEN = 8193;
    final int SEED = 0xFADE0;
    DFSTestUtil.createFile(fs, TEST_PATH, TEST_FILE_LEN, (short) 1, SEED);
    byte[] contents = DFSTestUtil.readFileBuffer(fs, TEST_PATH);
    byte[] expected = DFSTestUtil.calculateFileContentsFromSeed(SEED, TEST_FILE_LEN);
    Assert.assertTrue(Arrays.equals(contents, expected));
    // Loading this file brought the ShortCircuitReplica into our local
    // replica cache.
    final DatanodeInfo datanode = new DatanodeInfoBuilder().setNodeID(cluster.getDataNodes().get(0).getDatanodeId()).build();
    cache.getDfsClientShmManager().visit(new Visitor() {

        @Override
        public void visit(HashMap<DatanodeInfo, PerDatanodeVisitorInfo> info) throws IOException {
            Assert.assertTrue(info.get(datanode).full.isEmpty());
            Assert.assertFalse(info.get(datanode).disabled);
            Assert.assertEquals(1, info.get(datanode).notFull.values().size());
            DfsClientShm shm = info.get(datanode).notFull.values().iterator().next();
            Assert.assertFalse(shm.isDisconnected());
        }
    });
    // Remove the file whose blocks we just read.
    fs.delete(TEST_PATH, false);
    // Wait for the replica to be purged from the DFSClient's cache.
    GenericTestUtils.waitFor(new Supplier<Boolean>() {

        MutableBoolean done = new MutableBoolean(true);

        @Override
        public Boolean get() {
            try {
                done.setValue(true);
                cache.getDfsClientShmManager().visit(new Visitor() {

                    @Override
                    public void visit(HashMap<DatanodeInfo, PerDatanodeVisitorInfo> info) throws IOException {
                        Assert.assertTrue(info.get(datanode).full.isEmpty());
                        Assert.assertFalse(info.get(datanode).disabled);
                        Assert.assertEquals(1, info.get(datanode).notFull.values().size());
                        DfsClientShm shm = info.get(datanode).notFull.values().iterator().next();
                        // Check that all slots have been invalidated.
                        for (Iterator<Slot> iter = shm.slotIterator(); iter.hasNext(); ) {
                            Slot slot = iter.next();
                            if (slot.isValid()) {
                                done.setValue(false);
                            }
                        }
                    }
                });
            } catch (IOException e) {
                LOG.error("error running visitor", e);
            }
            return done.booleanValue();
        }
    }, 10, 60000);
    cluster.shutdown();
    sockDir.close();
}
Also used : Path(org.apache.hadoop.fs.Path) DatanodeInfo(org.apache.hadoop.hdfs.protocol.DatanodeInfo) MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) Configuration(org.apache.hadoop.conf.Configuration) Visitor(org.apache.hadoop.hdfs.shortcircuit.DfsClientShmManager.Visitor) CacheVisitor(org.apache.hadoop.hdfs.shortcircuit.ShortCircuitCache.CacheVisitor) DatanodeInfoBuilder(org.apache.hadoop.hdfs.protocol.DatanodeInfo.DatanodeInfoBuilder) HashMap(java.util.HashMap) DatanodeInfoBuilder(org.apache.hadoop.hdfs.protocol.DatanodeInfo.DatanodeInfoBuilder) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) IOException(java.io.IOException) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) TemporarySocketDirectory(org.apache.hadoop.net.unix.TemporarySocketDirectory) PerDatanodeVisitorInfo(org.apache.hadoop.hdfs.shortcircuit.DfsClientShmManager.PerDatanodeVisitorInfo) Slot(org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) Test(org.junit.Test)

Example 7 with Slot

use of org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot in project hadoop by apache.

the class BlockReaderFactory method createShortCircuitReplicaInfo.

/**
   * Fetch a pair of short-circuit block descriptors from a local DataNode.
   *
   * @return    Null if we could not communicate with the datanode,
   *            a new ShortCircuitReplicaInfo object otherwise.
   *            ShortCircuitReplicaInfo objects may contain either an
   *            InvalidToken exception, or a ShortCircuitReplica object ready to
   *            use.
   */
@Override
public ShortCircuitReplicaInfo createShortCircuitReplicaInfo() {
    if (createShortCircuitReplicaInfoCallback != null) {
        ShortCircuitReplicaInfo info = createShortCircuitReplicaInfoCallback.createShortCircuitReplicaInfo();
        if (info != null)
            return info;
    }
    LOG.trace("{}: trying to create ShortCircuitReplicaInfo.", this);
    BlockReaderPeer curPeer;
    while (true) {
        curPeer = nextDomainPeer();
        if (curPeer == null)
            break;
        if (curPeer.fromCache)
            remainingCacheTries--;
        DomainPeer peer = (DomainPeer) curPeer.peer;
        Slot slot = null;
        ShortCircuitCache cache = clientContext.getShortCircuitCache();
        try {
            MutableBoolean usedPeer = new MutableBoolean(false);
            slot = cache.allocShmSlot(datanode, peer, usedPeer, new ExtendedBlockId(block.getBlockId(), block.getBlockPoolId()), clientName);
            if (usedPeer.booleanValue()) {
                LOG.trace("{}: allocShmSlot used up our previous socket {}.  " + "Allocating a new one...", this, peer.getDomainSocket());
                curPeer = nextDomainPeer();
                if (curPeer == null)
                    break;
                peer = (DomainPeer) curPeer.peer;
            }
            ShortCircuitReplicaInfo info = requestFileDescriptors(peer, slot);
            clientContext.getPeerCache().put(datanode, peer);
            return info;
        } catch (IOException e) {
            if (slot != null) {
                cache.freeSlot(slot);
            }
            if (curPeer.fromCache) {
                // Handle an I/O error we got when using a cached socket.
                // These are considered less serious, because the socket may be stale.
                LOG.debug("{}: closing stale domain peer {}", this, peer, e);
                IOUtilsClient.cleanup(LOG, peer);
            } else {
                // Handle an I/O error we got when using a newly created socket.
                // We temporarily disable the domain socket path for a few minutes in
                // this case, to prevent wasting more time on it.
                LOG.warn(this + ": I/O error requesting file descriptors.  " + "Disabling domain socket " + peer.getDomainSocket(), e);
                IOUtilsClient.cleanup(LOG, peer);
                clientContext.getDomainSocketFactory().disableDomainSocketPath(pathInfo.getPath());
                return null;
            }
        }
    }
    return null;
}
Also used : ExtendedBlockId(org.apache.hadoop.hdfs.ExtendedBlockId) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) Slot(org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot) IOException(java.io.IOException) ShortCircuitReplicaInfo(org.apache.hadoop.hdfs.shortcircuit.ShortCircuitReplicaInfo) ShortCircuitCache(org.apache.hadoop.hdfs.shortcircuit.ShortCircuitCache) DomainPeer(org.apache.hadoop.hdfs.net.DomainPeer)

Example 8 with Slot

use of org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot in project hadoop by apache.

the class ShortCircuitRegistry method removeShm.

public synchronized void removeShm(ShortCircuitShm shm) {
    if (LOG.isTraceEnabled()) {
        LOG.debug("removing shm " + shm);
    }
    // Stop tracking the shmId.
    RegisteredShm removedShm = segments.remove(shm.getShmId());
    Preconditions.checkState(removedShm == shm, "failed to remove " + shm.getShmId());
    // Stop tracking the slots.
    for (Iterator<Slot> iter = shm.slotIterator(); iter.hasNext(); ) {
        Slot slot = iter.next();
        boolean removed = slots.remove(slot.getBlockId(), slot);
        Preconditions.checkState(removed);
        slot.makeInvalid();
    }
    // De-allocate the memory map and close the shared file. 
    shm.free();
}
Also used : Slot(org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot)

Example 9 with Slot

use of org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot in project hadoop by apache.

the class ShortCircuitRegistry method getClientNames.

public synchronized String getClientNames(ExtendedBlockId blockId) {
    if (!enabled)
        return "";
    final HashSet<String> clientNames = new HashSet<String>();
    final Set<Slot> affectedSlots = slots.get(blockId);
    for (Slot slot : affectedSlots) {
        clientNames.add(((RegisteredShm) slot.getShm()).getClientName());
    }
    return Joiner.on(",").join(clientNames);
}
Also used : Slot(org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot) HashSet(java.util.HashSet)

Example 10 with Slot

use of org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot in project hadoop by apache.

the class ShortCircuitRegistry method processBlockInvalidation.

/**
   * Invalidate any slot associated with a blockId that we are invalidating
   * (deleting) from this DataNode.  When a slot is invalid, the DFSClient will
   * not use the corresponding replica for new read or mmap operations (although
   * existing, ongoing read or mmap operations will complete.)
   *
   * @param blockId        The block ID.
   */
public synchronized void processBlockInvalidation(ExtendedBlockId blockId) {
    if (!enabled)
        return;
    final Set<Slot> affectedSlots = slots.get(blockId);
    if (!affectedSlots.isEmpty()) {
        final StringBuilder bld = new StringBuilder();
        String prefix = "";
        bld.append("Block ").append(blockId).append(" has been invalidated.  ").append("Marking short-circuit slots as invalid: ");
        for (Slot slot : affectedSlots) {
            slot.makeInvalid();
            bld.append(prefix).append(slot.toString());
            prefix = ", ";
        }
        LOG.info(bld.toString());
    }
}
Also used : Slot(org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot)

Aggregations

Slot (org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.Slot)11 MutableBoolean (org.apache.commons.lang.mutable.MutableBoolean)4 IOException (java.io.IOException)3 ExtendedBlockId (org.apache.hadoop.hdfs.ExtendedBlockId)3 CacheVisitor (org.apache.hadoop.hdfs.shortcircuit.ShortCircuitCache.CacheVisitor)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Configuration (org.apache.hadoop.conf.Configuration)2 InvalidRequestException (org.apache.hadoop.fs.InvalidRequestException)2 DistributedFileSystem (org.apache.hadoop.hdfs.DistributedFileSystem)2 MiniDFSCluster (org.apache.hadoop.hdfs.MiniDFSCluster)2 DomainPeer (org.apache.hadoop.hdfs.net.DomainPeer)2 DatanodeInfo (org.apache.hadoop.hdfs.protocol.DatanodeInfo)2 DatanodeInfoBuilder (org.apache.hadoop.hdfs.protocol.DatanodeInfo.DatanodeInfoBuilder)2 PerDatanodeVisitorInfo (org.apache.hadoop.hdfs.shortcircuit.DfsClientShmManager.PerDatanodeVisitorInfo)2 Visitor (org.apache.hadoop.hdfs.shortcircuit.DfsClientShmManager.Visitor)2 ShmId (org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm.ShmId)2 TemporarySocketDirectory (org.apache.hadoop.net.unix.TemporarySocketDirectory)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1