worker.h 4.63 KB
Newer Older
Stanislav Bohm's avatar
Stanislav Bohm committed
1
2
3
4
5
#ifndef LOOM_WORKER_H
#define LOOM_WORKER_H

#include "interconnect.h"
#include "taskinstance.h"
6
#include "unpacking.h"
Stanislav Bohm's avatar
Stanislav Bohm committed
7
#include "taskfactory.h"
8
#include "dictionary.h"
Stanislav Bohm's avatar
Stanislav Bohm committed
9
10
11
12
13
14
15
16
17
18
19

#include <uv.h>

#include <string>
#include <vector>
#include <unordered_map>
#include <memory>

namespace loom {

class Worker;
20
class DataUnpacker;
Stanislav Bohm's avatar
Stanislav Bohm committed
21

22
/** Class representing connection to the server */
Stanislav Bohm's avatar
Stanislav Bohm committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class ServerConnection : public SimpleConnectionCallback {
    
public:
    ServerConnection(Worker &worker);

    bool is_connected() {
        return connection.get_state() == Connection::ConnectionOpen;
    }

protected:
    Worker &worker;

    void on_connection();
    void on_close();
    void on_message(const char *data, size_t size);
    void on_error(int error_code);
};

41
/** Main class of the libloom that represents whole worker */
Stanislav Bohm's avatar
Stanislav Bohm committed
42
43
44
45
46
47
48
49
50
51
52
53
class Worker {    
    friend class ServerConnection;

public:
    Worker(uv_loop_t* loop,
           const std::string& server_address,
           int server_port,
           const std::string& work_dir_root);

    uv_loop_t *get_loop() {
        return loop;
    }
54
55

    void register_basic_tasks();
Stanislav Bohm's avatar
Stanislav Bohm committed
56
57
    
    void new_task(std::unique_ptr<Task> task);
58
59
    void send_data(const std::string &address, Id id, std::shared_ptr<Data> &data, bool with_size);
    bool send_data(const std::string &address, Id id, bool with_size) {
Stanislav Bohm's avatar
Stanislav Bohm committed
60
61
62
63
        auto& data = public_data[id];
        if (data.get() == nullptr) {
            return false;
        }
64
        send_data(address, id, data, with_size);
Stanislav Bohm's avatar
Stanislav Bohm committed
65
66
67
        return true;
    }

68
    void task_finished(TaskInstance &task_instance, Data &data);
Stanislav Bohm's avatar
Stanislav Bohm committed
69
    void task_failed(TaskInstance &task_instance, const std::string &error_msg);
Stanislav Bohm's avatar
Stanislav Bohm committed
70
    void publish_data(Id id, std::shared_ptr<Data> &data);
Stanislav Bohm's avatar
Stanislav Bohm committed
71
    void remove_data(Id id);
Stanislav Bohm's avatar
Stanislav Bohm committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85

    bool has_data(Id id) const
    {
        return public_data.find(id) != public_data.end();
    }

    std::shared_ptr<Data>& get_data(Id id)
    {
        auto it = public_data.find(id);
        assert(it != public_data.end());
        return it->second;
    }

    void add_task_factory(std::unique_ptr<TaskFactory> factory)
86
87
    {        
        unregistered_task_factories.push_back(std::move(factory));
Stanislav Bohm's avatar
Stanislav Bohm committed
88
89
    }

90
91
92
93
94
    template<typename T> void add_task_factory(const std::string &name)
    {
        add_task_factory(std::make_unique<SimpleTaskFactory<T>>(name));
    }

Stanislav Bohm's avatar
Stanislav Bohm committed
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
    InterConnection &get_connection(const std::string &address);

    void close_all();

    int get_listen_port() const
    {
        return listen_port;
    }

    void add_connection(std::unique_ptr<InterConnection> connection)
    {
        nonregistered_connections.push_back(std::move(connection));
    }

    void register_connection(InterConnection &connection);
    void unregister_connection(InterConnection &connection);

    const std::string& get_server_address() {
        return server_address;
    }

    const std::string& get_work_dir() {
        return work_dir;
    }

    std::string get_run_dir(Id id);

    void check_waiting_tasks();
    void check_ready_tasks();

    void set_cpus(int value);
Stanislav Bohm's avatar
Stanislav Bohm committed
126
    void add_unpacker(std::unique_ptr<UnpackFactory> factory);
127
128
129
130
    template<typename T> void add_unpacker()
    {
        add_unpacker(std::make_unique<SimpleUnpackFactory<T>>());
    }
Stanislav Bohm's avatar
Stanislav Bohm committed
131

132
    std::unique_ptr<DataUnpacker> unpack(DataTypeId id);
Stanislav Bohm's avatar
Stanislav Bohm committed
133

134
135
136
137
138
139
    Dictionary& get_dictionary() {
        return dictionary;
    }

    void on_dictionary_updated();

Stanislav Bohm's avatar
Stanislav Bohm committed
140
141
142
143
private:
    void register_worker();
    void start_listen();

144
    void remove_task(TaskInstance &task);
Stanislav Bohm's avatar
Stanislav Bohm committed
145
146
147
148
149
150
151
152
153
154
    void start_task(std::unique_ptr<Task> task);
    //int get_listen_port();

    
    uv_loop_t *loop;

    int resource_cpus;
    std::vector<std::unique_ptr<TaskInstance>> active_tasks;
    std::vector<std::unique_ptr<Task>> ready_tasks;
    std::vector<std::unique_ptr<Task>> waiting_tasks;
155
    std::unordered_map<Id, std::unique_ptr<TaskFactory>> task_factories;
156

Stanislav Bohm's avatar
Stanislav Bohm committed
157
    std::unordered_map<int, std::shared_ptr<Data>> public_data;
158
    std::string work_dir;
Stanislav Bohm's avatar
Stanislav Bohm committed
159

160
    std::unordered_map<DataTypeId, std::unique_ptr<UnpackFactory>> unpack_factories;
Stanislav Bohm's avatar
Stanislav Bohm committed
161
162
163
164
165

    ServerConnection server_conn;
    std::unordered_map<std::string, std::unique_ptr<InterConnection>> connections;
    std::vector<std::unique_ptr<InterConnection>> nonregistered_connections;

166
167
    Dictionary dictionary;

Stanislav Bohm's avatar
Stanislav Bohm committed
168
169
170
171
172
173
    std::string server_address;
    int server_port;

    uv_tcp_t listen_socket;
    int listen_port;

174
    std::vector<std::unique_ptr<TaskFactory>> unregistered_task_factories;
Stanislav Bohm's avatar
Stanislav Bohm committed
175
    std::vector<std::unique_ptr<UnpackFactory>> unregistered_unpack_factories;
176

Stanislav Bohm's avatar
Stanislav Bohm committed
177
178
179
180
181
182
183
    static void _on_new_connection(uv_stream_t *stream, int status);
    static void _on_getaddrinfo(uv_getaddrinfo_t* handle, int status, struct addrinfo* response);
};

}

#endif // LOOM_WORKER_H