示例#1
0
CNcbiApplication::CNcbiApplication(void)
{
    // Initialize UID and start timer
    GetDiagContext().GetUID();
    GetDiagContext().InitMessages(size_t(-1));
    GetDiagContext().SetGlobalAppState(eDiagAppState_AppBegin);

    m_DisableArgDesc = 0;
    m_HideArgs = 0;
    m_StdioFlags = 0;
    m_CinBuffer = 0;
    m_ExitCodeCond = eNoExits;

    // Register the app. instance
    if ( m_Instance ) {
        NCBI_THROW(CAppException, eSecond,
                   "Second instance of CNcbiApplication is prohibited");
    }
    m_Instance = this;

    // Create empty version info
    m_Version.Reset(new CVersion());

    // Create empty application arguments & name
    m_Arguments.reset(new CNcbiArguments(0,0));

    // Create empty application environment
    m_Environ.reset(new CNcbiEnvironment);

    // Create an empty registry
    m_Config.Reset(new CNcbiRegistry);

    m_DryRun = false;
}
示例#2
0
SIZE_TYPE CNcbiApplication::FlushDiag(CNcbiOstream* os, bool /*close_diag*/)
{
    if ( os ) {
        SetDiagStream(os, true, 0, 0, "STREAM");
    }
    GetDiagContext().FlushMessages(*GetDiagHandler());
    GetDiagContext().DiscardMessages();
    return 0;
}
    CGridWorkerTest(const IWorkerNodeInitContext& context)
    {
        GetDiagContext().SetOldPostFormat(false);

        m_SleepTimeDistr.InitFromParameter("sleep_time_distr",
            TParam_SleepTimeDistr::GetDefault().c_str(), &m_Random);
    }
示例#4
0
void CCgiApplication::LogRequest(void) const
{
    const CCgiContext& ctx = GetContext();
    string str;
    if ( TPrintSelfUrlParam::GetDefault() ) {
        // Print script URL
        string self_url = ctx.GetSelfURL();
        if ( !self_url.empty() ) {
            string args =
                ctx.GetRequest().GetRandomProperty("REDIRECT_QUERY_STRING", false);
            if ( args.empty() ) {
                args = ctx.GetRequest().GetProperty(eCgi_QueryString);
            }
            if ( !args.empty() ) {
                self_url += "?" + args;
            }
        }
        // Add target url
        string target_url = ctx.GetRequest().GetProperty(eCgi_ScriptName);
        if ( !target_url.empty() ) {
            string host = "http://" + GetDiagContext().GetHost();
            string port = ctx.GetRequest().GetProperty(eCgi_ServerPort);
            if (!port.empty()  &&  port != "80") {
                host += ":" + port;
            }
            target_url = host + target_url;
        }
        if ( !self_url.empty()  ||  !target_url.empty() ) {
            GetDiagContext().Extra().Print("SELF_URL", self_url).
                Print("TARGET_URL", target_url);
        }
    }
    // Print HTTP_REFERER
    if ( TPrintRefererParam::GetDefault() ) {
        str = ctx.GetRequest().GetProperty(eCgi_HttpReferer);
        if ( !str.empty() ) {
            GetDiagContext().Extra().Print("HTTP_REFERER", str);
        }
    }
    // Print USER_AGENT
    if ( TPrintUserAgentParam::GetDefault() ) {
        str = ctx.GetRequest().GetProperty(eCgi_HttpUserAgent);
        if ( !str.empty() ) {
            GetDiagContext().Extra().Print("USER_AGENT", str);
        }
    }
}
示例#5
0
int main(int argc, const char* argv[])
{
    GRID_APP_CHECK_VERSION_ARGS();

    SetSplitLogFile(true);
    GetDiagContext().SetOldPostFormat(false);

    return CNetCacheBlobFetchApp().AppMain(argc, argv);
}
示例#6
0
const string& CRequestContext::SetSessionID(void)
{
    CNcbiOstrstream oss;
    CDiagContext& ctx = GetDiagContext();
    oss << ctx.GetStringUID(ctx.UpdateUID()) << '_' << setw(4) << setfill('0')
        << GetRequestID() << "SID";
    SetSessionID(CNcbiOstrstreamToString(oss));
    return m_SessionID.GetOriginalString();
}
void  CStatisticsCounters::PrintTotal(size_t  affinities)
{
    CDiagContext_Extra      extra = GetDiagContext().Extra();

    // Prints the total counters
    extra.Print("counters", "total")
    .Print("submits", s_SubmitCounterTotal.Get())
    .Print("ns_submit_rollbacks", s_NSSubmitRollbackCounterTotal.Get())
    .Print("picked_as_outdated", s_PickedAsOutdatedTotal.Get())
    .Print("dbdeletions", s_DBDeleteCounterTotal.Get())
    .Print("affinities", affinities);

    for (size_t  index_from = 0;
            index_from < g_ValidJobStatusesSize; ++index_from) {
        for (size_t  index_to = 0;
                index_to < g_ValidJobStatusesSize; ++index_to) {

            // All invalid transitions are marked as -1
            if (s_TransitionsTotal[index_from][index_to].Get() !=
                    static_cast<TNCBIAtomicValue>(-1))
                extra.Print(x_GetTransitionCounterName(index_from, index_to),
                            s_TransitionsTotal[index_from][index_to].Get());
        }
    }
    extra.Print("Running_Pending_timeout",
                s_ToPendingDueToTimeoutCounterTotal.Get())
    .Print("Running_Pending_fail",
           s_ToPendingDueToFailCounterTotal.Get())
    .Print("Running_Pending_clear",
           s_ToPendingDueToClearCounterTotal.Get())
    .Print("Running_Failed_clear",
           s_ToFailedDueToClearCounterTotal.Get())
    .Print("Running_Pending_new_session",
           s_ToPendingDueToNewSessionCounterTotal.Get())
    .Print("Running_Failed_new_session",
           s_ToFailedDueToNewSessionCounterTotal.Get())
    .Print("Running_Failed_timeout",
           s_ToFailedDueToTimeoutCounterTotal.Get())
    .Print("Reading_Done_timeout",
           s_ToDoneDueToTimeoutCounterTotal.Get())
    .Print("Reading_Done_fail",
           s_ToDoneDueToFailCounterTotal.Get())
    .Print("Reading_Done_clear",
           s_ToDoneDueToClearCounterTotal.Get())
    .Print("Reading_ReadFailed_clear",
           s_ToReadFailedDueToClearCounterTotal.Get())
    .Print("Reading_Done_new_session",
           s_ToDoneDueToNewSessionCounterTotal.Get())
    .Print("Reading_ReadFailed_new_session",
           s_ToReadFailedDueToNewSessionCounterTotal.Get())
    .Print("Reading_ReadFailed_timeout",
           s_ToReadFailedDueToTimeoutCounterTotal.Get());
    extra.Flush();
}
示例#8
0
// Must be called under a lock
// When a client inactivity timeout is detected there is no need to touch the
// running or reading jobs. The only things to be done are:
// - cancel waiting
// - reset pref affinities
// - set reset affinity flag
// - log the event
void
CNSClientsRegistry::ClearOnTimeout(CNSClient &      client,
                                   const string &   client_node,
                                   bool             is_log,
                                   ECommandGroup    cmd_group)
{
    // Deregister preferred affinities
    bool    had_pref_affs = client.HasPreferredAffinities(cmd_group);
    if (had_pref_affs) {
        m_AffRegistry->RemoveClientFromAffinities(
                                    client.GetID(),
                                    client.GetPreferredAffinities(cmd_group),
                                    cmd_group);
        client.ClearPreferredAffinities(cmd_group);

        if (is_log) {
            string      aff_part = "get";
            if (cmd_group == eRead)
                aff_part = "read";

            CRef<CRequestContext>   ctx;
            ctx.Reset(new CRequestContext());
            ctx->SetRequestID();
            GetDiagContext().SetRequestContext(ctx);
            GetDiagContext().PrintRequestStart()
                            .Print("_type", "client_watch")
                            .Print("client_node", client_node)
                            .Print("client_session", client.GetSession())
                            .Print(aff_part + "_preferred_affinities_reset",
                                   "yes");
            ctx->SetRequestStatus(CNetScheduleHandler::eStatus_OK);
            GetDiagContext().PrintRequestStop();
        }

        client.SetAffinityReset(true, cmd_group);
    }

    CancelWaiting(client, cmd_group);
    if (had_pref_affs)
        x_BuildAffinities(cmd_group);
}
示例#9
0
int main(int argc, const char* argv[])
{
    //Applog
    SetSplitLogFile(true);
    GetDiagContext().SetOldPostFormat(false);

    openlog("hitmtrix", LOG_PID, LOG_LOCAL7);

    int result = CBlastHitMatrixCGIApplication().AppMain(argc, argv, 0, eDS_Default, "");    
    _TRACE("back to normal diags");
    return result;
}
示例#10
0
void CRequestContext::x_LogHitID(bool ignore_app_state) const
{
    if (m_LoggedHitID || m_HitID.empty()) return;

    // ignore_app_state can be set by CDiagContext in case if hit-id
    // was set for request context only (no default one), but request
    // start/stop never happened and the hit id may be lost. If this
    // happens, CDiagContext may force logging of the request's hit id
    // on application stop.

    if (!ignore_app_state  &&
        m_AppState != eDiagAppState_Request  &&
        m_AppState != eDiagAppState_RequestBegin  &&
        m_AppState != eDiagAppState_RequestEnd) return;
    GetDiagContext().Extra().Print(g_GetNcbiString(eNcbiStrings_PHID), m_HitID);
    m_LoggedHitID = true;
}
示例#11
0
string CRequestContext::x_GetHitID(CDiagContext::EDefaultHitIDFlags flag) const
{
    if ( x_IsSetProp(eProp_HitID) ) {
        x_LogHitID();
        return m_HitID;
    }
    string phid = GetDiagContext().x_GetDefaultHitID(CDiagContext::eHitID_NoCreate);
    if (!phid.empty()) {
        const_cast<CRequestContext*>(this)->SetHitID(phid);
        return phid;
    }
    if (flag != CDiagContext::eHitID_NoCreate) {
        // If there's no hit id available, create (and log) a new one.
        return const_cast<CRequestContext*>(this)->SetHitID();
    }
    return kEmptyStr;
}
示例#12
0
void CCgiContext::x_InitSession(CCgiRequest::TFlags flags)
{
    CCgiSessionParameters params;
    ICgiSessionStorage* impl = m_App.GetSessionStorage(params);
    m_Session.reset(new CCgiSession(*m_Request, 
                                    impl,
                                    params.m_ImplOwner,
                                    params.m_CookieEnabled ? 
                                    CCgiSession::eUseCookie :
                                    CCgiSession::eNoCookie)
                    );
    m_Session->SetSessionIdName(params.m_SessionIdName);
    m_Session->SetSessionCookieDomain(params.m_SessionCookieDomain);
    m_Session->SetSessionCookiePath(params.m_SessionCookiePath);
    m_Session->SetSessionCookieExpTime(params.m_SessionCookieExpTime);

    m_Request->x_SetSession(*m_Session);
    m_Response.x_SetSession(*m_Session);
    string track_cookie_value = RetrieveTrackingId();
    bool bad_tracking_cookie = false;
    if ((flags & CCgiRequest::fSkipDiagProperties) == 0) {
        try {
            CRequestContext& ctx = GetDiagContext().GetRequestContext();
            ctx.SetSessionID(track_cookie_value);
            if (ctx.GetSessionID() != track_cookie_value) {
                // Bad session-id was ignored
                bad_tracking_cookie = true;
            }
        }
        catch (CRequestContextException& e) {
            x_SetStatus(CCgiException::e400_BadRequest, e.GetMsg());
            bad_tracking_cookie = true;
        }
    }
    if( !bad_tracking_cookie  &&  !TCGI_DisableTrackingCookie::GetDefault() ) {
        m_Response.SetTrackingCookie(TCGI_TrackingCookieName::GetDefault(),
                                     track_cookie_value,
                                     TCGI_TrackingCookieDomain::GetDefault(),
                                     TCGI_TrackingCookiePath::GetDefault());
    }

    GetSelfURL();
    m_Response.Cookies().SetSecure(m_SelfURL.substr(0, 5) == "https");
}
示例#13
0
void CRequestContext::x_UpdateSubHitID(bool increment)
{
    static CAtomicCounter s_DefaultSubHitCounter;

    _ASSERT(IsSetHitID());

    // Use global sub-hit counter for default hit id to prevent
    // duplicate phids in different threads.
    m_SubHitIDCache = GetHitID();

    unsigned int sub_hit_id;
    if (m_SubHitIDCache == GetDiagContext().GetDefaultHitID()) {
        sub_hit_id = (unsigned int)(increment ? s_DefaultSubHitCounter.Add(1) :
            s_DefaultSubHitCounter.Get());
    }
    else {
        if ( increment ) ++m_SubHitID;
        sub_hit_id = m_SubHitID;
    }

    // Cache the string so that C code can use it.
    m_SubHitIDCache += "." + NStr::NumericToString(sub_hit_id);
}
示例#14
0
int CCgiApplication::Run(void)
{
    // Value to return from this method Run()
    int result;

    // Try to run as a Fast-CGI loop
    if ( x_RunFastCGI(&result) ) {
        return result;
    }

    /// Run as a plain CGI application

    // Make sure to restore old diagnostic state after the Run()
    CDiagRestorer diag_restorer;

#if defined(NCBI_OS_UNIX)
    // Disable SIGPIPE if not allowed.
    if ( !TParamAllowSigpipe::GetDefault() ) {
        signal(SIGPIPE, SIG_IGN);
        struct sigaction sigterm,  sigtermold;
        memset(&sigterm, 0, sizeof(sigterm));
        sigterm.sa_handler = SigTermHandler;
        sigterm.sa_flags = SA_RESETHAND;
        if (sigaction(SIGTERM, &sigterm, &sigtermold) == 0
            &&  sigtermold.sa_handler != SIG_DFL) {
            sigaction(SIGTERM, &sigtermold, 0);
        }
    }

    // Compose diagnostics prefix
    PushDiagPostPrefix(NStr::IntToString(getpid()).c_str());
#endif
    PushDiagPostPrefix(GetEnvironment().Get(m_DiagPrefixEnv).c_str());

    // Timing
    CTime start_time(CTime::eCurrent);

    // Logging for statistics
    bool is_stat_log = GetConfig().GetBool("CGI", "StatLog", false,
                                           0, CNcbiRegistry::eReturn);
    bool skip_stat_log = false;
    auto_ptr<CCgiStatistics> stat(is_stat_log ? CreateStat() : 0);

    CNcbiOstream* orig_stream = NULL;
    //int orig_fd = -1;
    CNcbiStrstream result_copy;
    auto_ptr<CNcbiOstream> new_stream;

    try {
        _TRACE("(CGI) CCgiApplication::Run: calling ProcessRequest");
        GetDiagContext().SetAppState(eDiagAppState_RequestBegin);

        m_Context.reset( CreateContext() );
        _ASSERT(m_Context.get());
        m_Context->CheckStatus();

        ConfigureDiagnostics(*m_Context);
        x_AddLBCookie();
        try {
            // Print request start message
            x_OnEvent(eStartRequest, 0);

            VerifyCgiContext(*m_Context);
            ProcessHttpReferer();
            LogRequest();

            try {
                m_Cache.reset( GetCacheStorage() );
            } catch( exception& ex ) {
                ERR_POST_X(1, "Couldn't create cache : " << ex.what());
            }
            bool skip_process_request = false;
            bool caching_needed = IsCachingNeeded(m_Context->GetRequest());
            if (m_Cache.get() && caching_needed) {
                skip_process_request = GetResultFromCache(m_Context->GetRequest(),
                                                           m_Context->GetResponse().out());
            }
            if (!skip_process_request) {
                if( m_Cache.get() ) {
                    list<CNcbiOstream*> slist;
                    orig_stream = m_Context->GetResponse().GetOutput();
                    slist.push_back(orig_stream);
                    slist.push_back(&result_copy);
                    new_stream.reset(new CWStream(new CMultiWriter(slist), 0,0,
                                                  CRWStreambuf::fOwnWriter));
                    m_Context->GetResponse().SetOutput(new_stream.get());
                }
                GetDiagContext().SetAppState(eDiagAppState_Request);
                result = CCgiContext::ProcessCORSRequest(
                    m_Context->GetRequest(), m_Context->GetResponse()) ?
                    0 : ProcessRequest(*m_Context);
                GetDiagContext().SetAppState(eDiagAppState_RequestEnd);
                m_Context->GetResponse().Finalize();
                if (result != 0) {
                    SetHTTPStatus(500);
                    m_ErrorStatus = true;
                } else {
                    if (m_Cache.get()) {
                        m_Context->GetResponse().Flush();
                        if (m_IsResultReady) {
                            if(caching_needed)
                                SaveResultToCache(m_Context->GetRequest(), result_copy);
                            else {
                                auto_ptr<CCgiRequest> request(GetSavedRequest(m_RID));
                                if (request.get()) 
                                    SaveResultToCache(*request, result_copy);
                            }
                        } else if (caching_needed) {
                            SaveRequest(m_RID, m_Context->GetRequest());
                        }
                    }
                }
            }
        }
        catch (CCgiException& e) {
            if ( x_DoneHeadRequest() ) {
                // Ignore errors after HEAD request has been finished.
                GetDiagContext().SetAppState(eDiagAppState_RequestEnd);
            }
            else {
                if ( e.GetStatusCode() <  CCgiException::e200_Ok  ||
                     e.GetStatusCode() >= CCgiException::e400_BadRequest ) {
                    throw;
                }
                GetDiagContext().SetAppState(eDiagAppState_RequestEnd);
                // If for some reason exception with status 2xx was thrown,
                // set the result to 0, update HTTP status and continue.
                m_Context->GetResponse().SetStatus(e.GetStatusCode(),
                                                   e.GetStatusMessage());
            }
            result = 0;
        }

#ifdef NCBI_OS_MSWIN
        // Logging - on MSWin this must be done before flushing the output.
        if ( is_stat_log  &&  !skip_stat_log ) {
            stat->Reset(start_time, result);
            stat->Submit(stat->Compose());
        }
        is_stat_log = false;
#endif

        _TRACE("CCgiApplication::Run: flushing");
        m_Context->GetResponse().Flush();
        _TRACE("CCgiApplication::Run: return " << result);
        x_OnEvent(result == 0 ? eSuccess : eError, result);
        x_OnEvent(eExit, result);
    }
    catch (exception& e) {
        GetDiagContext().SetAppState(eDiagAppState_RequestEnd);
        if ( x_DoneHeadRequest() ) {
            // Ignore errors after HEAD request has been finished.
            result = 0;
            x_OnEvent(eSuccess, result);
        }
        else {
            // Call the exception handler and set the CGI exit code
            result = OnException(e, NcbiCout);
            x_OnEvent(eException, result);

            // Logging
            {{
                string msg = "(CGI) CCgiApplication::ProcessRequest() failed: ";
                msg += e.what();

                if ( is_stat_log ) {
                    stat->Reset(start_time, result, &e);
                    msg = stat->Compose();
                    stat->Submit(msg);
                    skip_stat_log = true; // Don't print the same message again
                }
            }}

            // Exception reporting. Use different severity for broken connection.
            ios_base::failure* fex = dynamic_cast<ios_base::failure*>(&e);
            CNcbiOstream* os = m_Context.get() ? m_Context->GetResponse().GetOutput() : NULL;
            if ((fex  &&  os  &&  !os->good())  ||  m_OutputBroken) {
                if ( !TClientConnIntOk::GetDefault() ) {
                    ERR_POST_X(13, Severity(TClientConnIntSeverity::GetDefault()) <<
                        "Connection interrupted");
                }
            }
            else {
                NCBI_REPORT_EXCEPTION_X(13, "(CGI) CCgiApplication::Run", e);
            }
        }
    }

#ifndef NCBI_OS_MSWIN
    // Logging
    if ( is_stat_log  &&  !skip_stat_log ) {
        stat->Reset(start_time, result);
        stat->Submit(stat->Compose());
    }
#endif

    x_OnEvent(eEndRequest, 120);
    x_OnEvent(eExit, result);

    if (m_Context.get()) {
        m_Context->GetResponse().SetOutput(NULL);
    }
    return result;
}
示例#15
0
EDiagAppState CRequestContext::GetAppState(void) const
{
    return m_AppState != eDiagAppState_NotSet
        ? m_AppState : GetDiagContext().GetGlobalAppState();
}
示例#16
0
const string& CRequestContext::SetHitID(void)
{
    SetHitID(GetDiagContext().GetNextHitID());
    return m_HitID;
}