42 Commits

Author SHA1 Message Date
Markus Mäkelä
d00c5b2838 Move thread initialization into Worker::run
By moving the initialization into Worker::run, all threads, including the
main thread, are properly initialized. This was not noticed before as
qc_sqlite initialized the main thread in the process initialization
callback.
2017-09-15 18:08:49 +03:00
Johan Wikman
402b27ad01 MXS-1392 Remove remnats of DCB reference counting 2017-09-08 12:41:41 +03:00
Johan Wikman
70a4ad6532 MXS-1392 Take new zombie mechanism into use
Next commit will remove the remnants of the reference counting
mechanism.
2017-09-08 12:41:41 +03:00
Johan Wikman
8414ce6e80 MXS-1392 Re-introduce zombie queue
- Extend Worker interface so that zombies can be registered
- Call deletion function at the end of event loop
2017-09-08 12:41:41 +03:00
Johan Wikman
7e17e2cd56 MXS-1392 Add reference count to MXS_POLL_DATA
The polling mechanism can now optionally be used for managing
the lifetime of an object placed into the poll set.

If a MXS_POLL_DATA has a non-null 'free', then the reference count
of the data will be increased before calling the handler and
decreased after. In that case, if the reference count reaches 0,
the free function will be called.

Note that the reference counts of *all* MXS_POLL_DATAs returned
by 'epoll_wait' will be increased before the events are delivered
to the handlers individually for each MXS_POLL_DATA, and then once
all events have been delivered, the reference count of each
MXS_POLL_DATA will be decreased.

This ensure that if there are interdependencies between different
MXS_POLL_DATAs returned by one call to 'epoll_wait', the case that
an MXS_POLL_DATA is deleted before its events have been delivered
can be avoided by using the reference count for lifetime management.

In subsequent commits, the reference count will be taken into use
in the lifetime management of DCBs.
2017-09-08 12:41:41 +03:00
Johan Wikman
9c25e6d995 MXS-1376 All zombie related code removed
As dcbs are now closed when dcb_close() is called and there is
no zombie queue, the zombie state can also be removed.
2017-08-25 14:48:16 +03:00
Johan Wikman
49ba1f8fa0 MXS-1360 Add 'thread_stack_size' configuration value
Using the 'thread_stack_size' configuration value, the stack size
of the worker threads can be adjusted.
2017-08-14 15:31:00 +03:00
Johan Wikman
e9b2a560b8 MXS-1360 Make it possible to specify thread stack size
It is now possible to specify the thread stack size to be used,
when a new thread is created. This will subsequently be used
for allowing the stack size to be specified for worker threads.
2017-08-14 15:24:16 +03:00
Johan Wikman
f546a17e77 Update change date of 2.2 2017-06-01 10:24:20 +03:00
Esa Korhonen
dbfd631fed Change session registry to a template class
The template class wraps a HashMap such that only a few operations
are allowed. Usage requires specializing a RegistryTraits class
template for each entry type.
2017-05-19 10:16:37 +03:00
Markus Mäkelä
157b7f853b MXS-1220: Fix resource self links
The top level resource self links pointed to the collection instead of the
resource itself. The individual resoures now also have a links field that
contains the self link to the resource. This should make navigation of the
API easier as all objects have valid links in them.
2017-05-09 15:32:42 +03:00
Markus Mäkelä
0e57bec4ef MXS-1220: Add threads resource
The threads are now a REST API resource exposed via the /maxscale/threads
resource collection.
2017-05-09 15:32:42 +03:00
Esa Korhonen
ee20191645 KILL [CONNECTION | QUERY] support, part2B
Various small changes to part2, as suggested by comments and otherwise.
Mostly renaming, working logic should not change.

Exception: session id changed to 64bit in the container and associated
functions. Another commit will change it to 64bit in the session itself.
2017-05-08 09:58:02 +03:00
Esa Korhonen
17f6e94cba KILL [CONNECTION | QUERY] support, part2
MySQL sessions are added to a hasmap when created, removed when closed.
MYSQL_COM_PROCESS_KILL is now detected, the thread_id is read and the kill
command sent to all worker threads to find the correct session. If found, a
fake hangup even is created for the client dcb.

As is, this function is of little use since the client could just disconnect
itself instead. Later on, additional commands of this nature will be  added.
2017-05-08 09:51:07 +03:00
Markus Mäkelä
dd8a10f466 Remove old polling message system
The old polling message system is obsolete now that the worker messages
are implemented. The old system was only used to clean up the persistent
connection pool of a server.
2017-05-03 14:16:35 +03:00
Markus Mäkelä
4c6e0c75a5 Allow execution of tasks from within worker threads
It is now possible to define whether tasks are executed immediately or put
on the event queue of the worker thread. If task execution is in automatic
mode and the current executing thread is a worker thread, the
Task->execute method is called immediately.

This allows tasks to be posted from within worker threads. This is
intended to be used when purging of stale persistent connections and
printing diagnostic output via MaxAdmin. All of these actions are done
from within a worker thread.
2017-05-03 14:03:23 +03:00
Johan Wikman
00b6c10089 Adjust Worker terminology
- Posting a task to a worker for execution (without implicit wait)
  is called "post".
- Posting a task to every worker for execution (without implicit wait)
  is called "broadcast".

In these cases the task must be provided as a pointer or auto_ptr, to
indicate that the provided pointer must remain alive for longer than
the duration of the function call.

- Posting a task to a worker for execution *and* waiting for all workers
  to have executed the task is called "execute" and the two variants are
  now called "execute_concurrently" and "execute_serially".

In these cases the task is provided as a reference, since the functions
will return only when all workers have (in concurrent or serial fashion)
executed the task. That is, it need not remain alive for longer than the
duration of the function call.
2017-05-02 11:32:08 +03:00
Johan Wikman
1b58a75f42 Add concurrent execution helper to Worker
Concurrently executing a task on all workers *and* waiting until
all workers have executed the task seems to be common enough to
warrant a helper function for that purpose.
2017-05-02 10:54:29 +03:00
Johan Wikman
728c780187 Expose current worker id to c-files 2017-04-26 13:17:23 +03:00
Johan Wikman
48ed7792a5 Arrange so that startup connections are handled by main Worker
When the Worker mechanism has been initialized the current_worker_id
of the calling thread is set to 0. That way, connections can be created
after Worker::init() has been called, but before the workers have been
started. Such connections will be handled by the worker that is running
in the main thread.
2017-04-26 10:57:50 +03:00
Markus Mäkelä
963ff0216d Allow serial execution of worker tasks
The Worker::execute_on_all_wait is intended to be used with dcb_foreach
which expects a single-threaded context for its function.
2017-04-25 15:04:42 +03:00
Johan Wikman
55011c2951 Add safety check and rename ref mgmt functions 2017-04-25 13:10:01 +03:00
Johan Wikman
8174690f77 Introduce concept of Worker tasks
A Worker::Task is an object that can be sent to a worker for
execution. The task is sent to the worker using the messaging
mechanism where the `execute` function of the task will be
called in the thread context of the worker.

There are two kinds of tasks; regular tasks and disposable tasks.
The former are just sent to the worker for execution while the
latter are sent and subsequently disposed of, once the task has
been executed.

A disposable task can be sent to either one worker or to all
workers. In the latter case, the task will be deleted once it
has been executed by all workers.

A semaphore can be associated with a regular task. Once the task
has been executed by the worker, the semaphore will automatically
be posted. That way, it is trivial to send a task for execution
to a worker and wait until the task has been executed. For instance:

    Semaphore sem;
    MyTask task;

    pWorker->execute(&task, &sem);
    sem.wait();

    const MyResult& result = task.result();

The low level mechanism for posting and broadcasting messages will
be removed.
2017-04-24 14:52:54 +03:00
Johan Wikman
172cdbc5a3 Update comment regarding use of level-triggered events 2017-04-24 10:51:45 +03:00
Johan Wikman
56b411aea9 Add shared epoll instance to workers
All workers share an epoll instance that is added level-triggered
to the epoll instance of each Worker. This is intended to be used
together with listening sockets.

When a listening socket is added to the shared epoll instance the
effect is that EPOLLIN will be active for it whenever there is a
connection pending on a listening socket added to that epoll
instance.

When that occurs all workers in their epoll_wait()-calls will return.
When the workers subsequently call epoll_wait() on the shared epoll
instance, that will return with an event provided some other thread(s)
has not yet called accept() on the listening socket.

As each worker extracts just one event at a time and calls accept just
once before calling epoll_wait(), it means that the client connections
will be distributed evenly across all workers, provided the load on
the workers is roughly the same. If it isn't then a worker with less
load will get more connections to handle (which will even out the load).
2017-04-21 21:24:20 +03:00
Johan Wikman
a27bec5e88 Add return value to Worker::init()
Cleaner to return status than exit upon failure.
2017-04-21 19:57:45 +03:00
Johan Wikman
db3153ee4e Move statistics to Worker
Now the statistics is in a single structure and the property of the
Worker instance in question. Methods are provided for obtaining the
statistics of all workers in one go.
2017-04-20 13:51:16 +03:00
Johan Wikman
722d6da46f The thread state is now a property of the worker
A worker's state is not statistics, but transient information.
2017-04-20 13:51:16 +03:00
Johan Wikman
effa2f5674 poll_waitevents moved to Worker
A direct move without any non-essential modifications. Poll_waitevents will
be turned into a regular methods using instance variables.
2017-04-20 13:51:16 +03:00
Johan Wikman
3ac619bfec Fold thread state into poll stats 2017-04-20 13:51:16 +03:00
Johan Wikman
f952a11eb8 Move queue statistics to Worker
Just like the thread stats and poll stats earlier, the queue stats
are now moved to worker.

A litte refactoring still, and the polling will only work on local
data.
2017-04-20 13:51:16 +03:00
Johan Wikman
76825eb2c5 Poll statistics moved to worker
Each worker now has a separate structure for collecting the
polling statistics that is passed to epoll_waitevents(). When
the stats are asked for, we loop over all separate stats and
combine them. So, instead of having every statistics of each
thread one cacheline apart, each thread has all its statistics
in one lump that, for obvious reasons, are going to be apart.

The primary purpose of this excersize is to remove the hardwired
nature of the statistics collection. For instance, the admin
thread will be doing I/O but that I/O should not be included
in the statistics of the workers.
2017-04-20 13:51:16 +03:00
Johan Wikman
c11ca1c328 Move thread data to workers
This is a step in the direction that any worker/thread related data
is the property of the worker/thread.
2017-04-20 13:51:16 +03:00
Johan Wikman
052975bccd Add MessageQueue to Worker
The Worker no longer creates a pipe and implements the cross
worker/thread message mechanism itself. Instead it has a
MessageQueue instance variable for that purpose.
2017-04-20 13:51:16 +03:00
Johan Wikman
d20c89be37 Use epoll instance of Worker
Now the epoll instance of the Worker is used when polling. The
work is still done in poll.cc and the worker provides the descriptor
and thread id.

The comments of poll_waitevents() have been removed; they were not
accurate anymore so better to let the code speak for itself.
2017-04-20 13:51:16 +03:00
Johan Wikman
21ac606ee1 poll_[add|remove]_fd_[from|to]_worker made boolean 2017-04-20 13:51:16 +03:00
Johan Wikman
9829d6e365 Add epoll instance to Worker
And add the Worker header...
The epoll instance is not used yet, but the common creation of epoll
instances in poll.cc will be removed and the epoll instances of each
worker used instead.
2017-04-20 13:51:16 +03:00
Johan Wikman
df5553c35a All worker data moved to Worker class
With the exception of the poll structure.
2017-04-20 13:51:16 +03:00
Johan Wikman
72eadff181 Further remove use of the worker C-API 2017-04-20 13:51:16 +03:00
Johan Wikman
f1e99a475a Remove unneded worker C-API 2017-04-20 13:51:16 +03:00
Johan Wikman
0e4e889c15 Turn worker into a C++ class
This is the first step in turning the worker mechanism and everything
around it into a set of C++ classes. In this change, the original C
API is still present, but in subsequent changes that will be removed.
2017-04-20 13:51:16 +03:00
Johan Wikman
ece77c4478 Compile worker.c as C++ 2017-04-20 13:51:16 +03:00