<commit_before>//---------------------------------------------------------- -*- Mode: C++ -*-
// $Id$
//
// Created 2009/04/20
// Author: <NAME>
//
// Copyright 2008 Quantcast Corp.
// Copyright 2006-2008 Kosmix Corp.
//
// This file is part of Kosmos File System (KFS).
//
// Licensed under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// \file KfsClientInt.cc
// \brief Internal Kfs Client-library code.
//
//----------------------------------------------------------------------------

#include "KfsClientInt.h"

#include "common/config.h"
#include "common/properties.h"
#include "common/log.h"
#include "meta/kfstypes.h"
#include "libkfsIO/Checksum.h"
#include "Utils.h"

extern "C" {
#include <signal.h>
#include <openssl/md5.h>
}

#include <cstdio>
#include <cstdlib>

#include <cerrno>
#include <iostream>
#include <string>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <boost/scoped_array.hpp>

using std::string;
using std::ostringstream;
using std::istringstream;
using std::min;
using std::max;
using std::map;
using std::vector;
using std::sort;
using std::transform;
using std::random_shuffle;

using std::cout;
using std::endl;

using namespace KFS;

const int CMD_BUF_SIZE = 1024;

// Set the default timeout for server I/O's to be 5 mins for now.
// This is intentionally large so that we can do stuff in gdb and not
// have the client timeout in the midst of a debug session.
struct timeval gDefaultTimeout = {300, 0};


//
// Now, the real work is done by the impl object....
//

KfsClientImpl::KfsClientImpl()
    : mPendingOp(*this)
{
    pthread_mutexattr_t mutexAttr;
    int rval;
    const int hostnamelen = 256;
    char hostname[hostnamelen];

    if (gethostname(hostname, hostnamelen)) {
        perror("gethostname: ");
        exit(-1);
    }

    mHostname = hostname;

    // store the entry for "/"
    int UNUSED_ATTR rootfte = ClaimFileTableEntry(KFS::ROOTFID, "/", "/");
    assert(rootfte == 0);
    mFileTable[0]->fattr.fileId = KFS::ROOTFID;
    mFileTable[0]->fattr.isDirectory = true;

    mCwd = "/";
    mIsInitialized = false;
    mCmdSeqNum = 0;

    // Setup the mutex to allow recursive locking calls.  This
    // simplifies things when a public method (eg., read) in KFS client calls
    // another public method (eg., seek) and both need to take the lock
    rval = pthread_mutexattr_init(&mutexAttr);
    assert(rval == 0);
    rval = pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
    assert(rval == 0);
    rval = pthread_mutex_init(&mMutex, &mutexAttr);
    assert(rval == 0);

    // whenever a socket goes kaput, don't crash the app
    signal(SIGPIPE, SIG_IGN);

    // to reduce memory footprint, keep a decent size buffer; we used to have
    // 64MB before
    const size_t BUF_SIZE = min(KFS::CHUNKSIZE, size_t(4) << 20);
    mDefaultIoBufferSize  = BUF_SIZE;
    mDefaultReadAheadSize = min(BUF_SIZE, size_t(1) << 20);
    // for random # generation, seed it
    srand(getpid());
    // turn off the read-ahead thread for now
    mPendingOp.Stop();
}

KfsClientImpl::~KfsClientImpl()
{
    mPendingOp.Stop();
}

int KfsClientImpl::Init(const string & metaServerHost, int metaServerPort)
{
    // Initialize the logger
    MsgLogger::Init(NULL);

    MsgLogger::SetLevel(log4cpp::Priority::INFO);
    //MsgLogger::SetLevel(log4cpp::Priority::DEBUG);

    mRandSeed = time(NULL);

    mMetaServerLoc.hostname = metaServerHost;
    mMetaServerLoc.port = metaServerPort;

    KFS_LOG_VA_DEBUG("Connecting to metaserver at: %s:%d",
                     metaServerHost.c_str(), metaServerPort);

    if (!mMetaServerLoc.IsValid()) {
	mIsInitialized = false;
        KFS_LOG_VA_ERROR("Unable to connect to metaserver at: %s:%d",
                         metaServerHost.c_str(), metaServerPort);
	return -1;
    }

    if (!ConnectToMetaServer()) {
	mIsInitialized = false;
        KFS_LOG_VA_ERROR("Unable to connect to metaserver at: %s:%d",
                         metaServerHost.c_str(), metaServerPort);
	return -1;
    }


    // setup the telemetry stuff...
    struct ip_mreq imreq;
    string srvIp = "10.2.0.10";
    int srvPort = 12000;
    int multicastPort = 13000;

    imreq.imr_multiaddr.s_addr = inet_addr("172.16.17.32");
    imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface
    
    // will setup this for release
    mTelemetryReporter.Init(imreq, multicastPort, srvIp, srvPort);

    mIsInitialized = true;
    return 0;
}

bool
KfsClientImpl::ConnectToMetaServer()
{
    return mMetaServerSock.Connect(mMetaServerLoc) >= 0;
}


/// A notion of "cwd" in KFS.
///
int
KfsClientImpl::Cd(const string & pathname)<commit_msg> -- Remove telemetry client initialization.<commit_after>//---------------------------------------------------------- -*- Mode: C++ -*-
// $Id$
//
// Created 2009/04/20
// Author: <NAME>
//
// Copyright 2008 Quantcast Corp.
// Copyright 2006-2008 Kosmix Corp.
//
// This file is part of Kosmos File System (KFS).
//
// Licensed under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// \file KfsClientInt.cc
// \brief Internal Kfs Client-library code.
//
//----------------------------------------------------------------------------

#include "KfsClientInt.h"

#include "common/config.h"
#include "common/properties.h"
#include "common/log.h"
#include "meta/kfstypes.h"
#include "libkfsIO/Checksum.h"
#include "Utils.h"

extern "C" {
#include <signal.h>
#include <openssl/md5.h>
}

#include <cstdio>
#include <cstdlib>

#include <cerrno>
#include <iostream>
#include <string>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <boost/scoped_array.hpp>

using std::string;
using std::ostringstream;
using std::istringstream;
using std::min;
using std::max;
using std::map;
using std::vector;
using std::sort;
using std::transform;
using std::random_shuffle;

using std::cout;
using std::endl;

using namespace KFS;

const int CMD_BUF_SIZE = 1024;

// Set the default timeout for server I/O's to be 5 mins for now.
// This is intentionally large so that we can do stuff in gdb and not
// have the client timeout in the midst of a debug session.
struct timeval gDefaultTimeout = {300, 0};


//
// Now, the real work is done by the impl object....
//

KfsClientImpl::KfsClientImpl()
    : mPendingOp(*this)
{
    pthread_mutexattr_t mutexAttr;
    int rval;
    const int hostnamelen = 256;
    char hostname[hostnamelen];

    if (gethostname(hostname, hostnamelen)) {
        perror("gethostname: ");
        exit(-1);
    }

    mHostname = hostname;

    // store the entry for "/"
    int UNUSED_ATTR rootfte = ClaimFileTableEntry(KFS::ROOTFID, "/", "/");
    assert(rootfte == 0);
    mFileTable[0]->fattr.fileId = KFS::ROOTFID;
    mFileTable[0]->fattr.isDirectory = true;

    mCwd = "/";
    mIsInitialized = false;
    mCmdSeqNum = 0;

    // Setup the mutex to allow recursive locking calls.  This
    // simplifies things when a public method (eg., read) in KFS client calls
    // another public method (eg., seek) and both need to take the lock
    rval = pthread_mutexattr_init(&mutexAttr);
    assert(rval == 0);
    rval = pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
    assert(rval == 0);
    rval = pthread_mutex_init(&mMutex, &mutexAttr);
    assert(rval == 0);

    // whenever a socket goes kaput, don't crash the app
    signal(SIGPIPE, SIG_IGN);

    // to reduce memory footprint, keep a decent size buffer; we used to have
    // 64MB before
    const size_t BUF_SIZE = min(KFS::CHUNKSIZE, size_t(4) << 20);
    mDefaultIoBufferSize  = BUF_SIZE;
    mDefaultReadAheadSize = min(BUF_SIZE, size_t(1) << 20);
    // for random # generation, seed it
    srand(getpid());
    // turn off the read-ahead thread for now
    mPendingOp.Stop();
}

KfsClientImpl::~KfsClientImpl()
{
    mPendingOp.Stop();
}

int KfsClientImpl::Init(const string & metaServerHost, int metaServerPort)
{
    // Initialize the logger
    MsgLogger::Init(NULL);

    MsgLogger::SetLevel(log4cpp::Priority::INFO);
    //MsgLogger::SetLevel(log4cpp::Priority::DEBUG);

    mRandSeed = time(NULL);

    mMetaServerLoc.hostname = metaServerHost;
    mMetaServerLoc.port = metaServerPort;

    KFS_LOG_VA_DEBUG("Connecting to metaserver at: %s:%d",
                     metaServerHost.c_str(), metaServerPort);

    if (!mMetaServerLoc.IsValid()) {
	mIsInitialized = false;
        KFS_LOG_VA_ERROR("Unable to connect to metaserver at: %s:%d",
                         metaServerHost.c_str(), metaServerPort);
	return -1;
    }

    if (!ConnectToMetaServer()) {
	mIsInitialized = false;
        KFS_LOG_VA_ERROR("Unable to connect to metaserver at: %s:%d",
                         metaServerHost.c_str(), metaServerPort);
	return -1;
    }


#if 0
    // setup the telemetry stuff...
    struct ip_mreq imreq;
    string srvIp = "10.2.0.10";
    int srvPort = 12000;
    int multicastPort = 13000;

    imreq.imr_multiaddr.s_addr = inet_addr("172.16.17.32");
    imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface
    
    // will setup this for release
    mTelemetryReporter.Init(imreq, multicastPort, srvIp, srvPort);
#endif

    mIsInitialized = true;
    return 0;
}

bool
KfsClientImpl::ConnectToMetaServer()
{
    return mMetaServerSock.Connect(mMetaServerLoc) >= 0;
}


/// A notion of "cwd" in KFS.
///
int
KfsClientImpl::Cd(const string & pathname)
{'fraction_non_alphanumeric': 0.1481517415348792, 'fraction_numerical': 0.016396623653827495, 'mean_word_length': 2.9373567608861726, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1063194', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3322, 'n_words': 956}