Exemple #1
0
bool TestRunner::doBlock(unsigned int blocknum, unsigned char tag, unsigned int count)
{
    writeBlocks(blocknum,m_defbuf,tag,count);
    m_file->flush();
    if( m_opts.closeonwrite )
    {
        delete m_file;
        // will have to cache type somewhere later
        m_file = IDBDataFile::open( IDBDataFile::HDFS, m_fname.c_str(), "a", m_open_opts );
        if( !m_file )
            return false;
    }

    if( m_opts.reopen )
    {
        delete m_file;
        // will have to cache type somewhere later
        m_file = IDBDataFile::open( IDBDataFile::HDFS, m_fname.c_str(), "r", m_open_opts );
        if( !m_file )
            return false;
    }

    unsigned char buf[BLK_SIZE];
    return readBlocks(blocknum,buf,tag,count);
}
Exemple #2
0
bool TestRunner::rdwrTest( IDBDataFile::Types filetype )
{
	reset();
	m_file = IDBDataFile::open(filetype, m_fname.c_str(), "r+", m_open_opts);
	if( !m_file )
	{
		ostringstream errstr;
		errstr << "Unable to open " << m_fname << " for reading";
		logMsg( ERROR, errstr.str() );
		return false;
	}

	struct drand48_data d48data;
    srand48_r(0xdeadbeef, &d48data);

    // we will write to 5 random blocks and then come back and
    // verify the contents and undo them
    int blocks_to_touch = min( 5, m_opts.numBlocks);
    vector<int> touched;
    for( int i = 0; i < blocks_to_touch; ++i )
    {
    	long int blk_num;
    	// we need to make sure all the blocks we touch are unique or
    	// the pattern used by this test won't work
    	bool found = false;
    	while(!found)
    	{
    		lrand48_r( &d48data, &blk_num);
    		blk_num = blk_num % m_opts.numBlocks;
    		vector<int>::iterator pos = find( touched.begin(), touched.end(), blk_num );
    		if( pos == touched.end())
    			found = true;
    	}

    	if( m_file->seek(blk_num * BLK_SIZE, SEEK_SET) )
    	{
    		ostringstream errstr;
    		errstr << "failed to seek block " << blk_num;
    		logMsg( ERROR, errstr.str() );
    		return false;
    	}

    	unsigned char writeval = 0xb0;
    	size_t writect = m_file->write(&writeval,1);
       	if( writect != 1 )
    	{
    		ostringstream errstr;
    		errstr << "Only wrote " << writect << " bytes, expected 1";
    		logMsg( ERROR, errstr.str() );
    		return false;
    	}

       	touched.push_back(blk_num);
    }

    m_file->flush();

    for( int i = 0; i < (int) touched.size(); ++i )
    {
    	unsigned char readbuf;
    	size_t readct = m_file->pread(&readbuf,touched[i] * BLK_SIZE,1);
    	if( readct != 1 || readbuf != (unsigned char) 0xb0 )
    	{
    		ostringstream errstr;
    		errstr << "Error reading expected value, readct=" << readct << " bytes, value" << (int) readbuf;
    		logMsg( ERROR, errstr.str() );
    		return false;
    	}

    	readbuf = touched[i];

    	if( m_file->seek(-1, SEEK_CUR) )
    	{
    		ostringstream errstr;
    		errstr << "failed to seek -1";
    		logMsg( ERROR, errstr.str() );
    		return false;
    	}

    	size_t writect = m_file->write(&readbuf,1);
       	if( writect != 1 )
    	{
    		ostringstream errstr;
    		errstr << "Only wrote " << writect << " bytes, expected 1";
    		logMsg( ERROR, errstr.str() );
    		return false;
    	}
   }

    return true;
}
Exemple #3
0
bool TestRunner::flushTest( IDBDataFile::Types filetype )
{
    logMsg( INFO, "flushTest" );

    reset();

    string scratch = "/tmp/rdwr_scratch" + m_fname;  // scratch file name if exists
    boost::filesystem::remove(scratch);
    IDBPolicy::remove(m_fname.c_str());

    // scenario: writer opens the file, writes 8 bytes, flush;
    //           reader opens the file, reads 8 bytes, verifys the data, then closes file;
    //           writer writes 8M bytes (force to buffered file) if -m option used correctly;
    //           reader opens the file, verifys the file size and content, then closes the file;
    //           writer closes the file.
    ostringstream errstr;
    m_file = IDBDataFile::open(filetype, m_fname.c_str(), "w+", m_open_opts, /*default:4*/ 1);
    if (!m_file)
    {
        errstr << "flushTest: open " << m_fname.c_str() << " for write failed";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // write 8 "1" through mem buff
    const char w1[] = "11111111";
    ssize_t bytes = 0;
    if ((bytes = m_file->write(w1, 8)) != 8)
    {
        errstr << "flushTest: write count = 8, return = " << bytes;
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // for HDFS, force writing out to disk.
    m_file->flush();
    if (!IDBPolicy::exists(m_fname.c_str()))
    {
        errstr << "flushTest: " << m_fname.c_str() << " does not exist";
        logMsg( ERROR, errstr.str() );
        return false;
    }
    if (filetype == IDBDataFile::HDFS && boost::filesystem::exists(scratch))
    {
        errstr << "flushTest: " << scratch << " exists after 1st write";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // read from file in "r" mode
    IDBDataFile* file = IDBDataFile::open(filetype, m_fname.c_str(), "r", m_open_opts, 1);
    if (!file)
    {
        errstr << "flushTest: 1st open " << m_fname.c_str() << " to read failed";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    char r1[9] = {0};
    ssize_t n1 = file->pread(r1, 0, 8);
    if (n1 != 8 || strncmp(r1, w1, 8) != 0)
    {
        errstr << "flushTest: read " << n1 << " != 8 OR " << r1 << "!= 11111111";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    delete file;
    file = NULL;

    // write 8M "2", switched to file buffer if max size for mem buffer is small.
    //char w2[] = {[0 ... 8*1024*1024] = '2'};
    ssize_t m9 = 9*1024*1024;  // must be large than EXTENTSIZE = 8390656 to swith to file buffer
    boost::scoped_array<char> w2(new char[m9]);
    memset(w2.get(), '2', m9);
    m_file->write(w2.get(), m9);
    m_file->flush();

    // check file size
    if (IDBPolicy::size(m_fname.c_str()) != 8 + m9)
    {
        errstr << "flushTest: size of " << m_fname.c_str() << " is "
               << IDBPolicy::size(m_fname.c_str()) << ", expecting " << (8 + m9);
        logMsg( ERROR, errstr.str() );
        return false;
    }
    if (filetype == IDBDataFile::HDFS &&
            !boost::filesystem::exists(scratch) &&
            m_opts.hdfsMaxMem < m9)
    {
        errstr << "flushTest: " << scratch << " does not exist after 2nd write";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // 2nd read
    file = IDBDataFile::open(filetype, m_fname.c_str(), "r", m_open_opts, 1);
    if (!file)
    {
        errstr << "flushTest: 2nd open " << m_fname.c_str() << " to read failed";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    //char r2[9*1024*1024 + 8 + 1] = {0};
    boost::scoped_array<char> r2(new char[9*1024*1024 + 8 + 1]);
    memset(r2.get(), 0, m9 + 9);
    ssize_t n2 = file->pread(r2.get(), 0, m9 + 8);
    if (n2 != (m9+8) || strncmp(r2.get(), w1, 8) != 0 || memcmp(r2.get()+8, w2.get(), m9) != 0)
    {
        errstr << "flushTest: 2nd read " << m_fname.c_str() << " failed" << endl
               << "   return value: " << n2 << " bytes -- " << r2;  // need hex dump?
        logMsg( ERROR, errstr.str() );
        return false;
    }

    delete file;
    file = NULL;

    delete m_file;
    m_file = NULL;

    return true;
}
Exemple #4
0
bool TestRunner::seekTest( IDBDataFile::Types filetype )
{
    logMsg( INFO, "seekTest" );

    reset();

    // scenario: writer opens the file with w+, writes 8 bytes, seek to 4 from 0, write 4 bytes
    //           reader opens the file with r, verify size and contents,
    //           writer seeks 4 bytes beyond EOF, write 4 bytes, and close the file,
    //           reader rewinds, verify size and contents, and close the file.
    ostringstream errstr;
    m_file = IDBDataFile::open(filetype, m_fname.c_str(), "w+", m_open_opts);
    if (!m_file)
    {
        errstr << "seekTest: open " << m_fname.c_str() << " for write failed";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // write 8 "1" through mem buff
    const char w1[] = "11111111";
    ssize_t bytes = 0;
    if ((bytes = m_file->write(w1, 8)) != 8)
    {
        errstr << "seekTest: write1 count = 8, return = " << bytes;
        logMsg( ERROR, errstr.str() );
        return false;
    }
    if (m_file->seek(4, SEEK_SET) != 0)
    {
        errstr << "seekTest: seek(4, SEEK_SET) failed";
        logMsg( ERROR, errstr.str() );
        return false;
    }
    const char w2[] = "2222";
    if ((bytes = m_file->write(w2, 4)) != 4)
    {
        errstr << "seekTest: write2 count = 4, return = " << bytes;
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // for HDFS, force writing out to disk.
    m_file->flush();

    // read from file in "r" mode
    IDBDataFile* file = IDBDataFile::open(filetype, m_fname.c_str(), "r", m_open_opts);
    if (!file)
    {
        errstr << "seekTest: 1st open " << m_fname.c_str() << " to read failed";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    char r1[9] = {0};
    ssize_t n1 = file->pread(r1, 0, 8);
    if (IDBPolicy::size(m_fname.c_str()) != 8)
    {
        errstr << "seekTest: size of " << m_fname.c_str() << " is "
               << IDBPolicy::size(m_fname.c_str()) << ", expecting 8";
        logMsg( ERROR, errstr.str() );
        return false;
    }
    if (n1 != 8 || strncmp(r1, w1, 4) != 0 || strncmp(r1+4, w2, 4) != 0)
    {
        errstr << "seekTest: read " << n1 << " != 8 OR " << r1 << "!= 11112222";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // now seek beyond the eof, and write 4 bytes.
    const char w3[] = "3333";
    if (m_file->seek(4, SEEK_END) != 0)
    {
        errstr << "seekTest: seek(4, SEEK_END) failed";
        logMsg( ERROR, errstr.str() );
        return false;
    }
    if ((bytes = m_file->write(w3, 4)) != 4)
    {
        errstr << "seekTest: write3 count = 4, return = " << bytes;
        logMsg( ERROR, errstr.str() );
        return false;
    }
    m_file->flush();

    delete m_file;
    m_file = NULL;

    // check file size
    if (IDBPolicy::size(m_fname.c_str()) != 16)
    {
        errstr << "seekTest: size of " << m_fname.c_str() << " is "
               << IDBPolicy::size(m_fname.c_str()) << ", expecting 16";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // 2nd read
    file = IDBDataFile::open(filetype, m_fname.c_str(), "r", m_open_opts);
    if (!file)
    {
        errstr << "seekTest: 2nd open " << m_fname.c_str() << " to read failed";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    char r2[17] = {0};
    const char pd[4] = {0};  // padding
    ssize_t n2 = file->pread(r2, 0, 16);
    if (n2 != 16 ||
            strncmp(r2, w1, 4) != 0 || memcmp(r2+4, w2, 4) != 0 ||
            strncmp(r2+8, pd, 4) != 0 || memcmp(r2+12, w3, 4) != 0)
    {
        errstr << "seekTest: 2nd read " << m_fname.c_str() << " failed" << endl
               << "   return value: " << n2 << " bytes -- " << r2;  // need hex dump?
        logMsg( ERROR, errstr.str() );
        return false;
    }

    delete file;
    file = NULL;

    return true;
}