aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/TransactionsInfoLog.cpp
blob: 90f5022a444b2360eef89d09b0d2927a96e956d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <Interpreters/TransactionsInfoLog.h>
#include <Interpreters/TransactionVersionMetadata.h>
#include <Interpreters/Context.h>
#include <Common/TransactionID.h>
#include <Common/CurrentThread.h>
#include <Core/NamesAndTypes.h>
#include <DataTypes/DataTypeDateTime64.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeEnum.h>
#include <DataTypes/DataTypeFactory.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeUUID.h>
#include <base/getThreadId.h>

namespace DB
{

NamesAndTypesList TransactionsInfoLogElement::getNamesAndTypes()
{
    auto type_enum = std::make_shared<DataTypeEnum8>(
        DataTypeEnum8::Values
        {
            {"Begin",           static_cast<Int8>(BEGIN)},
            {"Commit",          static_cast<Int8>(COMMIT)},
            {"Rollback",        static_cast<Int8>(ROLLBACK)},

            {"AddPart",         static_cast<Int8>(ADD_PART)},
            {"LockPart",        static_cast<Int8>(LOCK_PART)},
            {"UnlockPart",      static_cast<Int8>(UNLOCK_PART)},
        });

    return
    {
        {"type", std::move(type_enum)},
        {"event_date", std::make_shared<DataTypeDate>()},
        {"event_time", std::make_shared<DataTypeDateTime64>(6)},
        {"thread_id", std::make_shared<DataTypeUInt64>()},

        {"query_id", std::make_shared<DataTypeString>()},
        {"tid", getTransactionIDDataType()},
        {"tid_hash", std::make_shared<DataTypeUInt64>()},

        {"csn", std::make_shared<DataTypeUInt64>()},

        {"database", std::make_shared<DataTypeString>()},
        {"table", std::make_shared<DataTypeString>()},
        {"uuid", std::make_shared<DataTypeUUID>()},
        {"part", std::make_shared<DataTypeString>()},
    };
}

void TransactionsInfoLogElement::fillCommonFields(const TransactionInfoContext * context)
{
    event_time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
    thread_id = getThreadId();

    query_id = std::string(CurrentThread::getQueryId());

    if (!context)
        return;

    table = context->table;
    part_name = context->part_name;
}

void TransactionsInfoLogElement::appendToBlock(MutableColumns & columns) const
{
    assert(type != UNKNOWN);
    size_t i = 0;

    columns[i++]->insert(type);
    auto event_time_seconds = event_time / 1000000;
    columns[i++]->insert(DateLUT::instance().toDayNum(event_time_seconds).toUnderType());
    columns[i++]->insert(event_time);
    columns[i++]->insert(thread_id);

    columns[i++]->insert(query_id);
    columns[i++]->insert(Tuple{tid.start_csn, tid.local_tid, tid.host_id});
    columns[i++]->insert(tid.getHash());

    columns[i++]->insert(csn);

    columns[i++]->insert(table.database_name);
    columns[i++]->insert(table.table_name);
    columns[i++]->insert(table.uuid);
    columns[i++]->insert(part_name);
}


void tryWriteEventToSystemLog(Poco::Logger * log,
                              TransactionsInfoLogElement::Type type, const TransactionID & tid,
                              const TransactionInfoContext & context)
try
{
    auto system_log =  Context::getGlobalContextInstance()->getTransactionsInfoLog();
    if (!system_log)
        return;

    TransactionsInfoLogElement elem;
    elem.type = type;
    elem.tid = tid;
    elem.fillCommonFields(&context);
    system_log->add(std::move(elem));
}
catch (...)
{
    tryLogCurrentException(log);
}

}