summaryrefslogtreecommitdiffstats
path: root/memfilepersistence/src/vespa/memfilepersistence/device/directory.cpp
blob: 8614d857ac86cafe8ccfcaee99e8999ae40a5787 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "directory.h"
#include "devicemanager.h"
#include <vespa/vespalib/util/exceptions.h>

#include <vespa/log/log.h>
LOG_SETUP(".persistence.device.directory");

namespace storage {

namespace memfile {

const IOEvent*
Directory::getLastEvent() const
{
    if (!_events.empty()) return &_events.back();
    return _partition->getLastEvent();
}

Device::State
Directory::getState() const
{
    const IOEvent* event = getLastEvent();
    return (event ? event->getState() : Device::OK);
}

void
Directory::print(std::ostream& out, bool verbose,
                 const std::string& indent) const
{
    out << _path << " ";
    Device::print(out, verbose, indent);
}

Directory::Directory(DeviceManager& manager, uint16_t index,
                     const std::string& path)
    : Device(manager),
      _index(index),
      _path(path),
      _partition(manager.getPartition(path))
{
    assert(_partition.get());
}

namespace {
    struct Entry {
        std::string path;
        Device::State status;
        std::string description;
        Entry();
        ~Entry();
    };

    Entry::Entry() {}
    Entry::~Entry() {}

    Entry parseDirectoryString(const std::string& serialized) {
        while (1) {
            Entry e;
            std::string::size_type pos1 = serialized.find(' ');
            if (pos1 == std::string::npos) break;
            e.path = serialized.substr(0, pos1);
            std::string::size_type pos2 = serialized.find(' ', pos1 + 1);
            std::string num = serialized.substr(pos1 + 1, pos2 - pos1 - 1);
            char* c;
            e.status = static_cast<Device::State>(
                                strtoul(num.c_str(), &c, 10));
            if (*c != '\0') break;
            if (pos2 != std::string::npos) {
                e.description = serialized.substr(pos2 + 1);
            }
            return e;
        }
        std::string msg = "Illegal line in disk status file: '" + serialized
                        + "'. Ignoring it.";
        LOG(warning, "%s", msg.c_str());
        throw vespalib::IllegalArgumentException(msg, VESPA_STRLOC);
    }
}

Directory::Directory(const std::string& serialized,
                     DeviceManager& manager)
    : Device(manager),
      _index(0),
      _path(parseDirectoryString(serialized).path),
      _partition(manager.getPartition(_path))
{
    assert(_partition.get());
    Entry e = parseDirectoryString(serialized);
    if (e.status != Device::OK) {
        addEvent(IOEvent(manager.getClock().getTimeInSeconds().getTime(),
                         e.status, e.description, VESPA_STRLOC));
    }
}

void Directory::addEvent(const IOEvent& e)
{
    switch (e.getState()) {
        case Device::IO_FAILURE:
            _partition->addEvent(e);
            break;
        case Device::PATH_FAILURE:
        case Device::NO_PERMISSION:
        case Device::INTERNAL_FAILURE:
        case Device::DISABLED_BY_ADMIN:
        default:
            if (!e.isGlobal()) {
                _events.push_back(e);
            }
            _manager.notifyDirectoryEvent(*this, e);
    }
}

void
Directory::addEvent(Device::State s,
                    const std::string& description,
                    const std::string& location)
{
    addEvent(IOEvent(
                     _manager.getClock().getTimeInSeconds().getTime(),
                     s,
                     description,
                     location));

}

void Directory::addEvents(const Directory& d)
{
    std::list<IOEvent> events;
    events.insert(events.end(), d.getEvents().begin(), d.getEvents().end());
    events.insert(events.end(), d.getPartition().getEvents().begin(),
                                d.getPartition().getEvents().end());
    events.insert(events.end(), d.getPartition().getDisk().getEvents().begin(),
                                d.getPartition().getDisk().getEvents().end());
    for (std::list<IOEvent>::const_iterator it = events.begin();
        it != events.end(); ++it)
    {
        addEvent(*it);
    }
}

} // memfile

} // storage