Beispiel #1
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestInputStreamClosedTwice()
        {
            //HADOOP-4760 according to Closeable#close() closing already-closed
            //streams should have no effect.
            Org.Apache.Hadoop.FS.Path src = Path("/test/hadoop/file");
            CreateFile(src);
            FSDataInputStream @in = fs.Open(src);

            @in.Close();
            @in.Close();
        }
        public virtual void TestFallbackRead()
        {
            HdfsConfiguration conf     = InitZeroCopyTest();
            MiniDFSCluster    cluster  = null;
            Path TestPath              = new Path("/a");
            int  TestFileLength        = 16385;
            int  RandomSeed            = 23453;
            FSDataInputStream     fsIn = null;
            DistributedFileSystem fs   = null;

            try
            {
                cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build();
                cluster.WaitActive();
                fs = cluster.GetFileSystem();
                DFSTestUtil.CreateFile(fs, TestPath, TestFileLength, (short)1, RandomSeed);
                try
                {
                    DFSTestUtil.WaitReplication(fs, TestPath, (short)1);
                }
                catch (Exception e)
                {
                    NUnit.Framework.Assert.Fail("unexpected InterruptedException during " + "waitReplication: "
                                                + e);
                }
                catch (TimeoutException e)
                {
                    NUnit.Framework.Assert.Fail("unexpected TimeoutException during " + "waitReplication: "
                                                + e);
                }
                fsIn = fs.Open(TestPath);
                byte[] original = new byte[TestFileLength];
                IOUtils.ReadFully(fsIn, original, 0, TestFileLength);
                fsIn.Close();
                fsIn = fs.Open(TestPath);
                TestFallbackImpl(fsIn, original);
            }
            finally
            {
                if (fsIn != null)
                {
                    fsIn.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
            }
        }
Beispiel #3
0
 /// <exception cref="System.IO.IOException"/>
 public override void Close()
 {
     datas.Close();
     if (sums != null)
     {
         sums.Close();
     }
     Set(fs.verifyChecksum, null, 1, 0);
 }
        public virtual void TestBufferedFSInputStream()
        {
            Configuration conf = new Configuration();

            conf.SetClass("fs.file.impl", typeof(RawLocalFileSystem), typeof(FileSystem));
            conf.SetInt(CommonConfigurationKeysPublic.IoFileBufferSizeKey, 4096);
            FileSystem fs = FileSystem.NewInstance(conf);

            byte[] buf = new byte[10 * 1024];
            new Random().NextBytes(buf);
            // Write random bytes to file
            FSDataOutputStream stream = fs.Create(TestPath);

            try
            {
                stream.Write(buf);
            }
            finally
            {
                stream.Close();
            }
            Random            r   = new Random();
            FSDataInputStream stm = fs.Open(TestPath);

            // Record the sequence of seeks and reads which trigger a failure.
            int[] seeks = new int[10];
            int[] reads = new int[10];
            try
            {
                for (int i = 0; i < 1000; i++)
                {
                    int seekOff = r.Next(buf.Length);
                    int toRead  = r.Next(Math.Min(buf.Length - seekOff, 32000));
                    seeks[i % seeks.Length] = seekOff;
                    reads[i % reads.Length] = toRead;
                    VerifyRead(stm, buf, seekOff, toRead);
                }
            }
            catch (Exception afe)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Sequence of actions:\n");
                for (int j = 0; j < seeks.Length; j++)
                {
                    sb.Append("seek @ ").Append(seeks[j]).Append("  ").Append("read ").Append(reads[j
                                                                                              ]).Append("\n");
                }
                System.Console.Error.WriteLine(sb.ToString());
                throw;
            }
            finally
            {
                stm.Close();
            }
        }
Beispiel #5
0
        /// <exception cref="System.IO.IOException"/>
        private string ReadFile(string @out)
        {
            Path              path = new Path(@out);
            FileStatus        stat = lfs.GetFileStatus(path);
            FSDataInputStream @in  = lfs.Open(path);

            byte[] buffer = new byte[(int)stat.GetLen()];
            @in.ReadFully(buffer);
            @in.Close();
            lfs.Delete(path, false);
            return(Runtime.GetStringForBytes(buffer));
        }
Beispiel #6
0
            /// <exception cref="System.IO.IOException"/>
            public virtual void Map(Text key, LongWritable value, OutputCollector <K, LongWritable
                                                                                   > collector, Reporter reporter)
            {
                string name = key.ToString();
                long   size = value.Get();
                long   seed = long.Parse(name);

                if (size == 0)
                {
                    return;
                }
                reporter.SetStatus("opening " + name);
                FSDataInputStream @in = fs.Open(new Path(DataDir, name));

                try
                {
                    for (int i = 0; i < SeeksPerFile; i++)
                    {
                        // generate a random position
                        long position = Math.Abs(random.NextLong()) % size;
                        // seek file to that position
                        reporter.SetStatus("seeking " + name);
                        @in.Seek(position);
                        byte b = @in.ReadByte();
                        // check that byte matches
                        byte checkByte = 0;
                        // advance random state to that position
                        random.SetSeed(seed);
                        for (int p = 0; p <= position; p += check.Length)
                        {
                            reporter.SetStatus("generating data for " + name);
                            if (fastCheck)
                            {
                                checkByte = unchecked ((byte)random.Next(byte.MaxValue));
                            }
                            else
                            {
                                random.NextBytes(check);
                                checkByte = check[(int)(position % check.Length)];
                            }
                        }
                        NUnit.Framework.Assert.AreEqual(b, checkByte);
                    }
                }
                finally
                {
                    @in.Close();
                }
            }
        /// <exception cref="System.IO.IOException"/>
        internal static string ReadFile(FileSystem fs, Path name, int buflen)
        {
            byte[]            b      = new byte[buflen];
            int               offset = 0;
            FSDataInputStream @in    = fs.Open(name);

            for (int remaining; (remaining = b.Length - offset) > 0 && (n = @in.Read(b, offset
                                                                                     , remaining)) != -1; offset += n)
            {
            }
            Assert.Equal(offset, Math.Min(b.Length, @in.GetPos()));
            @in.Close();
            string s = Runtime.GetStringForBytes(b, 0, offset);

            return(s);
        }
        /// <exception cref="System.IO.IOException"/>
        private void VerifyFile(FileSystem fs, Path file, int bytesToVerify, byte[] expectedBytes
                                )
        {
            FSDataInputStream @in = fs.Open(file);

            try
            {
                byte[] readBuf = new byte[bytesToVerify];
                @in.ReadFully(readBuf, 0, bytesToVerify);
                for (int i = 0; i < bytesToVerify; i++)
                {
                    Assert.Equal(expectedBytes[i], readBuf[i]);
                }
            }
            finally
            {
                @in.Close();
            }
        }
Beispiel #9
0
            /// <exception cref="System.IO.IOException"/>
            internal override object DoIO(Reporter reporter, string name, long offset)
            {
                // open file
                FSDataInputStream @in = null;
                Path p = new Path(name);

                try
                {
                    @in = fs.Open(p);
                }
                catch (IOException)
                {
                    return(name + "@(missing)");
                }
                @in.Seek(offset);
                long actualSize = 0;

                try
                {
                    long blockSize = fs.GetDefaultBlockSize(p);
                    reporter.SetStatus("reading " + name + "@" + offset + "/" + blockSize);
                    for (int curSize = bufferSize; curSize == bufferSize && actualSize < blockSize; actualSize
                             += curSize)
                    {
                        curSize = @in.Read(buffer, 0, bufferSize);
                    }
                }
                catch (IOException)
                {
                    Log.Info("Corrupted block detected in \"" + name + "\" at " + offset);
                    return(name + "@" + offset);
                }
                finally
                {
                    @in.Close();
                }
                return(actualSize);
            }
Beispiel #10
0
        public virtual void TestTruncatedChecksum()
        {
            Path testPath           = new Path(TestRootDir, "testtruncatedcrc");
            FSDataOutputStream fout = localFs.Create(testPath);

            fout.Write(Runtime.GetBytesForString("testing truncation"));
            fout.Close();
            // Read in the checksum
            Path              checksumFile   = localFs.GetChecksumFile(testPath);
            FileSystem        rawFs          = localFs.GetRawFileSystem();
            FSDataInputStream checksumStream = rawFs.Open(checksumFile);

            byte[] buf  = new byte[8192];
            int    read = checksumStream.Read(buf, 0, buf.Length);

            checksumStream.Close();
            // Now rewrite the checksum file with the last byte missing
            FSDataOutputStream replaceStream = rawFs.Create(checksumFile);

            replaceStream.Write(buf, 0, read - 1);
            replaceStream.Close();
            // Now reading the file should fail with a ChecksumException
            try
            {
                FileSystemTestHelper.ReadFile(localFs, testPath, 1024);
                NUnit.Framework.Assert.Fail("Did not throw a ChecksumException when reading truncated "
                                            + "crc file");
            }
            catch (ChecksumException)
            {
            }
            // telling it not to verify checksums, should avoid issue.
            localFs.SetVerifyChecksum(false);
            string str = FileSystemTestHelper.ReadFile(localFs, testPath, 1024).ToString();

            Assert.True("read", "testing truncation".Equals(str));
        }
Beispiel #11
0
        /// <summary>Write a file and read it in, validating the result.</summary>
        /// <remarks>
        /// Write a file and read it in, validating the result. Optional flags control
        /// whether file overwrite operations should be enabled, and whether the
        /// file should be deleted afterwards.
        /// If there is a mismatch between what was written and what was expected,
        /// a small range of bytes either side of the first error are logged to aid
        /// diagnosing what problem occurred -whether it was a previous file
        /// or a corrupting of the current file. This assumes that two
        /// sequential runs to the same path use datasets with different character
        /// moduli.
        /// </remarks>
        /// <param name="path">path to write to</param>
        /// <param name="len">length of data</param>
        /// <param name="overwrite">should the create option allow overwrites?</param>
        /// <param name="delete">
        /// should the file be deleted afterwards? -with a verification
        /// that it worked. Deletion is not attempted if an assertion has failed
        /// earlier -it is not in a <code>finally{}</code> block.
        /// </param>
        /// <exception cref="System.IO.IOException">IO problems</exception>
        protected internal virtual void WriteAndRead(Org.Apache.Hadoop.FS.Path path, byte
                                                     [] src, int len, bool overwrite, bool delete)
        {
            Assert.True("Not enough data in source array to write " + len +
                        " bytes", src.Length >= len);
            fs.Mkdirs(path.GetParent());
            FSDataOutputStream @out = fs.Create(path, overwrite, fs.GetConf().GetInt("io.file.buffer.size"
                                                                                     , 4096), (short)1, GetBlockSize());

            @out.Write(src, 0, len);
            @out.Close();
            Assert.True("Exists", fs.Exists(path));
            Assert.Equal("Length", len, fs.GetFileStatus(path).GetLen());
            FSDataInputStream @in = fs.Open(path);

            byte[] buf = new byte[len];
            @in.ReadFully(0, buf);
            @in.Close();
            Assert.Equal(len, buf.Length);
            int errors           = 0;
            int first_error_byte = -1;

            for (int i = 0; i < len; i++)
            {
                if (src[i] != buf[i])
                {
                    if (errors == 0)
                    {
                        first_error_byte = i;
                    }
                    errors++;
                }
            }
            if (errors > 0)
            {
                string message = string.Format(" %d errors in file of length %d", errors, len);
                Log.Warn(message);
                // the range either side of the first error to print
                // this is a purely arbitrary number, to aid user debugging
                int overlap = 10;
                for (int i_1 = Math.Max(0, first_error_byte - overlap); i_1 < Math.Min(first_error_byte
                                                                                       + overlap, len); i_1++)
                {
                    byte   actual   = buf[i_1];
                    byte   expected = src[i_1];
                    string letter   = ToChar(actual);
                    string line     = string.Format("[%04d] %2x %s\n", i_1, actual, letter);
                    if (expected != actual)
                    {
                        line = string.Format("[%04d] %2x %s -expected %2x %s\n", i_1, actual, letter, expected
                                             , ToChar(expected));
                    }
                    Log.Warn(line);
                }
                Fail(message);
            }
            if (delete)
            {
                bool deleted = fs.Delete(path, false);
                Assert.True("Deleted", deleted);
                NUnit.Framework.Assert.IsFalse("No longer exists", fs.Exists(path));
            }
        }
        /// <summary>
        /// Test that we can zero-copy read cached data even without disabling
        /// checksums.
        /// </summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestZeroCopyReadOfCachedData()
        {
            BlockReaderTestUtil.EnableShortCircuitShmTracing();
            BlockReaderTestUtil.EnableBlockReaderFactoryTracing();
            BlockReaderTestUtil.EnableHdfsCachingTracing();
            int  TestFileLength    = BlockSize;
            Path TestPath          = new Path("/a");
            int  RandomSeed        = 23453;
            HdfsConfiguration conf = InitZeroCopyTest();

            conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitSkipChecksumKey, false);
            string Context = "testZeroCopyReadOfCachedData";

            conf.Set(DFSConfigKeys.DfsClientContext, Context);
            conf.SetLong(DFSConfigKeys.DfsDatanodeMaxLockedMemoryKey, DFSTestUtil.RoundUpToMultiple
                             (TestFileLength, (int)NativeIO.POSIX.GetCacheManipulator().GetOperatingSystemPageSize
                                 ()));
            MiniDFSCluster cluster = null;
            ByteBuffer     result  = null;
            ByteBuffer     result2 = null;

            cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build();
            cluster.WaitActive();
            FsDatasetSpi <object> fsd = cluster.GetDataNodes()[0].GetFSDataset();
            DistributedFileSystem fs  = cluster.GetFileSystem();

            DFSTestUtil.CreateFile(fs, TestPath, TestFileLength, (short)1, RandomSeed);
            DFSTestUtil.WaitReplication(fs, TestPath, (short)1);
            byte[] original = DFSTestUtil.CalculateFileContentsFromSeed(RandomSeed, TestFileLength
                                                                        );
            // Prior to caching, the file can't be read via zero-copy
            FSDataInputStream fsIn = fs.Open(TestPath);

            try
            {
                result = fsIn.Read(null, TestFileLength / 2, EnumSet.NoneOf <ReadOption>());
                NUnit.Framework.Assert.Fail("expected UnsupportedOperationException");
            }
            catch (NotSupportedException)
            {
            }
            // expected
            // Cache the file
            fs.AddCachePool(new CachePoolInfo("pool1"));
            long directiveId = fs.AddCacheDirective(new CacheDirectiveInfo.Builder().SetPath(
                                                        TestPath).SetReplication((short)1).SetPool("pool1").Build());
            int numBlocks = (int)Math.Ceil((double)TestFileLength / BlockSize);

            DFSTestUtil.VerifyExpectedCacheUsage(DFSTestUtil.RoundUpToMultiple(TestFileLength
                                                                               , BlockSize), numBlocks, cluster.GetDataNodes()[0].GetFSDataset());
            try
            {
                result = fsIn.Read(null, TestFileLength, EnumSet.NoneOf <ReadOption>());
            }
            catch (NotSupportedException)
            {
                NUnit.Framework.Assert.Fail("expected to be able to read cached file via zero-copy"
                                            );
            }
            Assert.AssertArrayEquals(Arrays.CopyOfRange(original, 0, BlockSize), ByteBufferToArray
                                         (result));
            // Test that files opened after the cache operation has finished
            // still get the benefits of zero-copy (regression test for HDFS-6086)
            FSDataInputStream fsIn2 = fs.Open(TestPath);

            try
            {
                result2 = fsIn2.Read(null, TestFileLength, EnumSet.NoneOf <ReadOption>());
            }
            catch (NotSupportedException)
            {
                NUnit.Framework.Assert.Fail("expected to be able to read cached file via zero-copy"
                                            );
            }
            Assert.AssertArrayEquals(Arrays.CopyOfRange(original, 0, BlockSize), ByteBufferToArray
                                         (result2));
            fsIn2.ReleaseBuffer(result2);
            fsIn2.Close();
            // check that the replica is anchored
            ExtendedBlock     firstBlock = DFSTestUtil.GetFirstBlock(fs, TestPath);
            ShortCircuitCache cache      = ClientContext.Get(Context, new DFSClient.Conf(conf)).GetShortCircuitCache
                                               ();

            WaitForReplicaAnchorStatus(cache, firstBlock, true, true, 1);
            // Uncache the replica
            fs.RemoveCacheDirective(directiveId);
            WaitForReplicaAnchorStatus(cache, firstBlock, false, true, 1);
            fsIn.ReleaseBuffer(result);
            WaitForReplicaAnchorStatus(cache, firstBlock, false, false, 1);
            DFSTestUtil.VerifyExpectedCacheUsage(0, 0, fsd);
            fsIn.Close();
            fs.Close();
            cluster.Shutdown();
        }
        /// <summary>
        /// When mark() is used on BufferedInputStream, the request
        /// size on the checksum file system can be small.
        /// </summary>
        /// <remarks>
        /// When mark() is used on BufferedInputStream, the request
        /// size on the checksum file system can be small.  However,
        /// checksum file system currently depends on the request size
        /// &gt;= bytesPerSum to work properly.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestTruncatedInputBug()
        {
            int           ioBufSize = 512;
            int           fileSize  = ioBufSize * 4;
            int           filePos   = 0;
            Configuration conf      = new Configuration();

            conf.SetInt("io.file.buffer.size", ioBufSize);
            FileSystem fileSys = FileSystem.GetLocal(conf);

            try
            {
                // First create a test input file.
                Path testFile = new Path(TestRootDir, "HADOOP-1489");
                WriteFile(fileSys, testFile, fileSize);
                Assert.True(fileSys.Exists(testFile));
                Assert.True(fileSys.GetFileStatus(testFile).GetLen() == fileSize
                            );
                // Now read the file for ioBufSize bytes
                FSDataInputStream @in = fileSys.Open(testFile, ioBufSize);
                // seek beyond data buffered by open
                filePos += ioBufSize * 2 + (ioBufSize - 10);
                @in.Seek(filePos);
                // read 4 more bytes before marking
                for (int i = 0; i < 4; ++i)
                {
                    if (@in.Read() == -1)
                    {
                        break;
                    }
                    ++filePos;
                }
                // Now set mark() to trigger the bug
                // NOTE: in the fixed code, mark() does nothing (not supported) and
                //   hence won't trigger this bug.
                @in.Mark(1);
                System.Console.Out.WriteLine("MARKED");
                // Try to read the rest
                while (filePos < fileSize)
                {
                    if (@in.Read() == -1)
                    {
                        break;
                    }
                    ++filePos;
                }
                @in.Close();
                System.Console.Out.WriteLine("Read " + filePos + " bytes." + " file size=" + fileSize
                                             );
                Assert.True(filePos == fileSize);
            }
            finally
            {
                try
                {
                    fileSys.Close();
                }
                catch (Exception)
                {
                }
            }
        }
        public virtual void TestClientMmapDisable()
        {
            HdfsConfiguration conf = InitZeroCopyTest();

            conf.SetBoolean(DFSConfigKeys.DfsClientMmapEnabled, false);
            MiniDFSCluster        cluster        = null;
            Path                  TestPath       = new Path("/a");
            int                   TestFileLength = 16385;
            int                   RandomSeed     = 23453;
            string                Context        = "testClientMmapDisable";
            FSDataInputStream     fsIn           = null;
            DistributedFileSystem fs             = null;

            conf.Set(DFSConfigKeys.DfsClientContext, Context);
            try
            {
                // With DFS_CLIENT_MMAP_ENABLED set to false, we should not do memory
                // mapped reads.
                cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build();
                cluster.WaitActive();
                fs = cluster.GetFileSystem();
                DFSTestUtil.CreateFile(fs, TestPath, TestFileLength, (short)1, RandomSeed);
                DFSTestUtil.WaitReplication(fs, TestPath, (short)1);
                fsIn = fs.Open(TestPath);
                try
                {
                    fsIn.Read(null, 1, EnumSet.Of(ReadOption.SkipChecksums));
                    NUnit.Framework.Assert.Fail("expected zero-copy read to fail when client mmaps "
                                                + "were disabled.");
                }
                catch (NotSupportedException)
                {
                }
            }
            finally
            {
                if (fsIn != null)
                {
                    fsIn.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
            }
            fsIn    = null;
            fs      = null;
            cluster = null;
            try
            {
                // Now try again with DFS_CLIENT_MMAP_CACHE_SIZE == 0.  It should work.
                conf.SetBoolean(DFSConfigKeys.DfsClientMmapEnabled, true);
                conf.SetInt(DFSConfigKeys.DfsClientMmapCacheSize, 0);
                conf.Set(DFSConfigKeys.DfsClientContext, Context + ".1");
                cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build();
                cluster.WaitActive();
                fs = cluster.GetFileSystem();
                DFSTestUtil.CreateFile(fs, TestPath, TestFileLength, (short)1, RandomSeed);
                DFSTestUtil.WaitReplication(fs, TestPath, (short)1);
                fsIn = fs.Open(TestPath);
                ByteBuffer buf = fsIn.Read(null, 1, EnumSet.Of(ReadOption.SkipChecksums));
                fsIn.ReleaseBuffer(buf);
                // Test EOF behavior
                IOUtils.SkipFully(fsIn, TestFileLength - 1);
                buf = fsIn.Read(null, 1, EnumSet.Of(ReadOption.SkipChecksums));
                NUnit.Framework.Assert.AreEqual(null, buf);
            }
            finally
            {
                if (fsIn != null)
                {
                    fsIn.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
            }
        }
        public virtual void TestZeroCopyReads()
        {
            HdfsConfiguration conf    = InitZeroCopyTest();
            MiniDFSCluster    cluster = null;
            Path TestPath             = new Path("/a");
            FSDataInputStream fsIn    = null;
            int        TestFileLength = 3 * BlockSize;
            FileSystem fs             = null;

            try
            {
                cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build();
                cluster.WaitActive();
                fs = cluster.GetFileSystem();
                DFSTestUtil.CreateFile(fs, TestPath, TestFileLength, (short)1, 7567L);
                try
                {
                    DFSTestUtil.WaitReplication(fs, TestPath, (short)1);
                }
                catch (Exception e)
                {
                    NUnit.Framework.Assert.Fail("unexpected InterruptedException during " + "waitReplication: "
                                                + e);
                }
                catch (TimeoutException e)
                {
                    NUnit.Framework.Assert.Fail("unexpected TimeoutException during " + "waitReplication: "
                                                + e);
                }
                fsIn = fs.Open(TestPath);
                byte[] original = new byte[TestFileLength];
                IOUtils.ReadFully(fsIn, original, 0, TestFileLength);
                fsIn.Close();
                fsIn = fs.Open(TestPath);
                ByteBuffer result = fsIn.Read(null, BlockSize, EnumSet.Of(ReadOption.SkipChecksums
                                                                          ));
                NUnit.Framework.Assert.AreEqual(BlockSize, result.Remaining());
                HdfsDataInputStream dfsIn = (HdfsDataInputStream)fsIn;
                NUnit.Framework.Assert.AreEqual(BlockSize, dfsIn.GetReadStatistics().GetTotalBytesRead
                                                    ());
                NUnit.Framework.Assert.AreEqual(BlockSize, dfsIn.GetReadStatistics().GetTotalZeroCopyBytesRead
                                                    ());
                Assert.AssertArrayEquals(Arrays.CopyOfRange(original, 0, BlockSize), ByteBufferToArray
                                             (result));
                fsIn.ReleaseBuffer(result);
            }
            finally
            {
                if (fsIn != null)
                {
                    fsIn.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
            }
        }
        public virtual void TestZeroCopyMmapCache()
        {
            HdfsConfiguration conf           = InitZeroCopyTest();
            MiniDFSCluster    cluster        = null;
            Path              TestPath       = new Path("/a");
            int               TestFileLength = 5 * BlockSize;
            int               RandomSeed     = 23453;
            string            Context        = "testZeroCopyMmapCacheContext";
            FSDataInputStream fsIn           = null;

            ByteBuffer[]          results = new ByteBuffer[] { null, null, null, null };
            DistributedFileSystem fs      = null;

            conf.Set(DFSConfigKeys.DfsClientContext, Context);
            cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build();
            cluster.WaitActive();
            fs = cluster.GetFileSystem();
            DFSTestUtil.CreateFile(fs, TestPath, TestFileLength, (short)1, RandomSeed);
            try
            {
                DFSTestUtil.WaitReplication(fs, TestPath, (short)1);
            }
            catch (Exception e)
            {
                NUnit.Framework.Assert.Fail("unexpected InterruptedException during " + "waitReplication: "
                                            + e);
            }
            catch (TimeoutException e)
            {
                NUnit.Framework.Assert.Fail("unexpected TimeoutException during " + "waitReplication: "
                                            + e);
            }
            fsIn = fs.Open(TestPath);
            byte[] original = new byte[TestFileLength];
            IOUtils.ReadFully(fsIn, original, 0, TestFileLength);
            fsIn.Close();
            fsIn = fs.Open(TestPath);
            ShortCircuitCache cache = ClientContext.Get(Context, new DFSClient.Conf(conf)).GetShortCircuitCache
                                          ();

            cache.Accept(new TestEnhancedByteBufferAccess.CountingVisitor(0, 5, 5, 0));
            results[0] = fsIn.Read(null, BlockSize, EnumSet.Of(ReadOption.SkipChecksums));
            fsIn.Seek(0);
            results[1] = fsIn.Read(null, BlockSize, EnumSet.Of(ReadOption.SkipChecksums));
            // The mmap should be of the first block of the file.
            ExtendedBlock firstBlock = DFSTestUtil.GetFirstBlock(fs, TestPath);

            cache.Accept(new _CacheVisitor_373(firstBlock));
            // The replica should not yet be evictable, since we have it open.
            // Read more blocks.
            results[2] = fsIn.Read(null, BlockSize, EnumSet.Of(ReadOption.SkipChecksums));
            results[3] = fsIn.Read(null, BlockSize, EnumSet.Of(ReadOption.SkipChecksums));
            // we should have 3 mmaps, 1 evictable
            cache.Accept(new TestEnhancedByteBufferAccess.CountingVisitor(3, 5, 2, 0));
            // After we close the cursors, the mmaps should be evictable for
            // a brief period of time.  Then, they should be closed (we're
            // using a very quick timeout)
            foreach (ByteBuffer buffer in results)
            {
                if (buffer != null)
                {
                    fsIn.ReleaseBuffer(buffer);
                }
            }
            fsIn.Close();
            GenericTestUtils.WaitFor(new _Supplier_407(cache), 10, 60000);
            cache.Accept(new TestEnhancedByteBufferAccess.CountingVisitor(0, -1, -1, -1));
            fs.Close();
            cluster.Shutdown();
        }
Beispiel #17
0
 /// <exception cref="System.IO.IOException"/>
 public virtual void Close()
 {
     stream.Close();
 }
        public virtual void Test2GBMmapLimit()
        {
            Assume.AssumeTrue(BlockReaderTestUtil.ShouldTestLargeFiles());
            HdfsConfiguration conf = InitZeroCopyTest();
            long TestFileLength    = 2469605888L;

            conf.Set(DFSConfigKeys.DfsChecksumTypeKey, "NULL");
            conf.SetLong(DFSConfigKeys.DfsBlockSizeKey, TestFileLength);
            MiniDFSCluster cluster  = null;
            Path           TestPath = new Path("/a");
            string         Context  = "test2GBMmapLimit";

            conf.Set(DFSConfigKeys.DfsClientContext, Context);
            FSDataInputStream fsIn  = null;
            FSDataInputStream fsIn2 = null;
            ByteBuffer        buf1  = null;
            ByteBuffer        buf2  = null;

            try
            {
                cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build();
                cluster.WaitActive();
                DistributedFileSystem fs = cluster.GetFileSystem();
                DFSTestUtil.CreateFile(fs, TestPath, TestFileLength, (short)1, unchecked ((int)(0xB
                                                                                                )));
                DFSTestUtil.WaitReplication(fs, TestPath, (short)1);
                fsIn = fs.Open(TestPath);
                buf1 = fsIn.Read(null, 1, EnumSet.Of(ReadOption.SkipChecksums));
                NUnit.Framework.Assert.AreEqual(1, buf1.Remaining());
                fsIn.ReleaseBuffer(buf1);
                buf1 = null;
                fsIn.Seek(2147483640L);
                buf1 = fsIn.Read(null, 1024, EnumSet.Of(ReadOption.SkipChecksums));
                NUnit.Framework.Assert.AreEqual(7, buf1.Remaining());
                NUnit.Framework.Assert.AreEqual(int.MaxValue, buf1.Limit());
                fsIn.ReleaseBuffer(buf1);
                buf1 = null;
                NUnit.Framework.Assert.AreEqual(2147483647L, fsIn.GetPos());
                try
                {
                    buf1 = fsIn.Read(null, 1024, EnumSet.Of(ReadOption.SkipChecksums));
                    NUnit.Framework.Assert.Fail("expected UnsupportedOperationException");
                }
                catch (NotSupportedException)
                {
                }
                // expected; can't read past 2GB boundary.
                fsIn.Close();
                fsIn = null;
                // Now create another file with normal-sized blocks, and verify we
                // can read past 2GB
                Path TestPath2 = new Path("/b");
                conf.SetLong(DFSConfigKeys.DfsBlockSizeKey, 268435456L);
                DFSTestUtil.CreateFile(fs, TestPath2, 1024 * 1024, TestFileLength, 268435456L, (short
                                                                                                )1, unchecked ((int)(0xA)));
                fsIn2 = fs.Open(TestPath2);
                fsIn2.Seek(2147483640L);
                buf2 = fsIn2.Read(null, 1024, EnumSet.Of(ReadOption.SkipChecksums));
                NUnit.Framework.Assert.AreEqual(8, buf2.Remaining());
                NUnit.Framework.Assert.AreEqual(2147483648L, fsIn2.GetPos());
                fsIn2.ReleaseBuffer(buf2);
                buf2 = null;
                buf2 = fsIn2.Read(null, 1024, EnumSet.Of(ReadOption.SkipChecksums));
                NUnit.Framework.Assert.AreEqual(1024, buf2.Remaining());
                NUnit.Framework.Assert.AreEqual(2147484672L, fsIn2.GetPos());
                fsIn2.ReleaseBuffer(buf2);
                buf2 = null;
            }
            finally
            {
                if (buf1 != null)
                {
                    fsIn.ReleaseBuffer(buf1);
                }
                if (buf2 != null)
                {
                    fsIn2.ReleaseBuffer(buf2);
                }
                IOUtils.Cleanup(null, fsIn, fsIn2);
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
            }
        }