static int hdfsSingleNameNodeConnect(struct NativeMiniDfsCluster *cl, hdfsFS *fs, const char *username) { int ret; tPort port; hdfsFS hdfs; struct hdfsBuilder *bld; port = (tPort)nmdGetNameNodePort(cl); if (port < 0) { fprintf(stderr, "hdfsSingleNameNodeConnect: nmdGetNameNodePort " "returned error %d\n", port); return port; } bld = hdfsNewBuilder(); if (!bld) return -ENOMEM; hdfsBuilderSetForceNewInstance(bld); hdfsBuilderSetNameNode(bld, "localhost"); hdfsBuilderSetNameNodePort(bld, port); hdfsBuilderConfSetStr(bld, "dfs.block.size", TO_STR(TLH_DEFAULT_BLOCK_SIZE)); hdfsBuilderConfSetStr(bld, "dfs.blocksize", TO_STR(TLH_DEFAULT_BLOCK_SIZE)); if (username) { hdfsBuilderSetUserName(bld, username); } hdfs = hdfsBuilderConnect(bld); if (!hdfs) { ret = -errno; return ret; } *fs = hdfs; return 0; }
/** * Create a fuse_dfs process using the miniDfsCluster we set up. * * @param argv0 argv[0], as passed into main * @param cluster The NativeMiniDfsCluster to connect to * @param mntPoint The mount point * @param pid (out param) the fuse_dfs process * * @return 0 on success; error code otherwise */ static int spawnFuseServer(const char *argv0, const struct NativeMiniDfsCluster *cluster, const char *mntPoint, pid_t *pid) { int ret, procRet; char scratch[PATH_MAX], *dir, fusePath[PATH_MAX], portOpt[128]; snprintf(scratch, sizeof(scratch), "%s", argv0); dir = dirname(scratch); snprintf(fusePath, sizeof(fusePath), "%s/fuse_dfs", dir); if (access(fusePath, X_OK)) { fprintf(stderr, "FUSE_TEST: spawnFuseServer: failed to find fuse_dfs " "binary at %s!\n", fusePath); return -ENOENT; } /* Let's make sure no other FUSE filesystem is mounted at mntPoint */ ret = fuserMount(&procRet, "-u", mntPoint, NULL); if (ret) { fprintf(stderr, "FUSE_TEST: fuserMount -u %s failed with error %d\n", mntPoint, ret); return -EIO; } if (procRet == EXIT_STATUS_EXEC_ERROR) { fprintf(stderr, "FUSE_TEST: fuserMount probably could not be executed\n"); return -EIO; } /* fork and exec the fuse_dfs process */ *pid = fork(); if (*pid < 0) { ret = errno; fprintf(stderr, "FUSE_TEST: spawnFuseServer: failed to fork: " "error %d: %s\n", ret, strerror(ret)); return -ret; } else if (*pid == 0) { snprintf(portOpt, sizeof(portOpt), "-oport=%d", nmdGetNameNodePort(cluster)); if (execl(fusePath, fusePath, "-obig_writes", "-oserver=hdfs://localhost", portOpt, "-onopermissions", "-ononempty", "-oinitchecks", mntPoint, "-f", NULL)) { ret = errno; fprintf(stderr, "FUSE_TEST: spawnFuseServer: failed to execv %s: " "error %d: %s\n", fusePath, ret, strerror(ret)); exit(EXIT_STATUS_EXEC_ERROR); } } return 0; }
/** * Test that we can write a file with libhdfs and then read it back */ int main(void) { int port; struct NativeMiniDfsConf conf = { 1, /* doFormat */ 0, /* webhdfsEnabled */ 0, /* namenodeHttpPort */ 1, /* configureShortCircuit */ }; char testFileName[TEST_FILE_NAME_LENGTH]; hdfsFS fs; struct NativeMiniDfsCluster* cl; struct hdfsBuilder *bld; cl = nmdCreate(&conf); EXPECT_NONNULL(cl); EXPECT_ZERO(nmdWaitClusterUp(cl)); port = nmdGetNameNodePort(cl); if (port < 0) { fprintf(stderr, "TEST_ERROR: test_zerocopy: " "nmdGetNameNodePort returned error %d\n", port); return EXIT_FAILURE; } bld = hdfsNewBuilder(); EXPECT_NONNULL(bld); EXPECT_ZERO(nmdConfigureHdfsBuilder(cl, bld)); hdfsBuilderSetForceNewInstance(bld); hdfsBuilderConfSetStr(bld, "dfs.block.size", TO_STR(TEST_ZEROCOPY_FULL_BLOCK_SIZE)); /* ensure that we'll always get our mmaps */ hdfsBuilderConfSetStr(bld, "dfs.client.read.shortcircuit.skip.checksum", "true"); fs = hdfsBuilderConnect(bld); EXPECT_NONNULL(fs); EXPECT_ZERO(createZeroCopyTestFile(fs, testFileName, TEST_FILE_NAME_LENGTH)); EXPECT_ZERO(doTestZeroCopyReads(fs, testFileName)); EXPECT_ZERO(hdfsDisconnect(fs)); EXPECT_ZERO(nmdShutdown(cl)); nmdFree(cl); fprintf(stderr, "TEST_SUCCESS\n"); return EXIT_SUCCESS; }