447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#ifndef REPLICATOR_QUEUE_H
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#define REPLICATOR_QUEUE_H
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#include "priorityq.h"
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#include "replication-common.h"
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenstruct replicator_user {
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen struct priorityq_item item;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen char *username;
99a9e14caaf54d884dac6a520111cfc8849d8323Timo Sirainen /* dsync state for incremental syncing */
593bf2d333dc67e9d9e6552273332157793731c0Timo Sirainen char *state;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen /* last time this user's state was updated */
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen time_t last_update;
f3e77a037a7ceef76d0d45432fb58c003dd61b32Timo Sirainen /* last_fast_sync is always >= last_full_sync. */
69c92cbe74dceb292f3de381edd39958ece148ebTimo Sirainen time_t last_fast_sync, last_full_sync, last_successful_sync;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
9b2129d5bdda49de45bc2f1e6160b2de338e0976Timo Sirainen int refcount;
593bf2d333dc67e9d9e6552273332157793731c0Timo Sirainen enum replication_priority priority;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen /* User isn't currently in replication queue */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool popped:1;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen /* Last replication sync failed */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool last_sync_failed:1;
f57e4c0f2824b93d09d7634974a7e2a47cb8ee0eTimo Sirainen /* Force a full sync on the next replication */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool force_full_sync:1;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen};
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainentypedef void replicator_sync_callback_t(bool success, void *context);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
f3e77a037a7ceef76d0d45432fb58c003dd61b32Timo Sirainenstruct replicator_queue *
f3e77a037a7ceef76d0d45432fb58c003dd61b32Timo Sirainenreplicator_queue_init(unsigned int full_sync_interval,
f3e77a037a7ceef76d0d45432fb58c003dd61b32Timo Sirainen unsigned int failure_resync_interval);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenvoid replicator_queue_deinit(struct replicator_queue **queue);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen/* Call the specified callback when data is added/removed/moved in queue
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen via _add(), _add_sync() or _remove() functions (not push/pop). */
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenvoid replicator_queue_set_change_callback(struct replicator_queue *queue,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen void (*callback)(void *context),
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen void *context);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
9b2129d5bdda49de45bc2f1e6160b2de338e0976Timo Sirainen/* Reference the user */
9b2129d5bdda49de45bc2f1e6160b2de338e0976Timo Sirainenvoid replicator_user_ref(struct replicator_user *user);
9b2129d5bdda49de45bc2f1e6160b2de338e0976Timo Sirainen/* Unreference the user. Returns TRUE if refcount is still >0. */
9b2129d5bdda49de45bc2f1e6160b2de338e0976Timo Sirainenbool replicator_user_unref(struct replicator_user **user);
9b2129d5bdda49de45bc2f1e6160b2de338e0976Timo Sirainen
ee8d619694b0b96ba2a8b603468147ce3e12b886Timo Sirainen/* Lookup an existing user */
ee8d619694b0b96ba2a8b603468147ce3e12b886Timo Sirainenstruct replicator_user *
ee8d619694b0b96ba2a8b603468147ce3e12b886Timo Sirainenreplicator_queue_lookup(struct replicator_queue *queue, const char *username);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen/* Add a user to queue and return it. If the user already exists, it's updated
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen only if the new priority is higher. */
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenstruct replicator_user *
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenreplicator_queue_add(struct replicator_queue *queue, const char *username,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen enum replication_priority priority);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenvoid replicator_queue_add_sync(struct replicator_queue *queue,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen const char *username,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen replicator_sync_callback_t *callback,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen void *context);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen/* Remove user from replication queue and free it. */
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenvoid replicator_queue_remove(struct replicator_queue *queue,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen struct replicator_user **user);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen/* Return the next user from replication queue, and remove it from the queue.
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen If there's nothing to be replicated currently, returns NULL and sets
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen next_secs_r to when there should be more work to do. */
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenstruct replicator_user *
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenreplicator_queue_pop(struct replicator_queue *queue,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen unsigned int *next_secs_r);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen/* Add user back to queue. */
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenvoid replicator_queue_push(struct replicator_queue *queue,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen struct replicator_user *user);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenint replicator_queue_import(struct replicator_queue *queue, const char *path);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenint replicator_queue_export(struct replicator_queue *queue, const char *path);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
5ca40db1f869421fc3798a304ad402834250ff50Timo Sirainen/* Returns TRUE if user replication can be started now, FALSE if not. When
5ca40db1f869421fc3798a304ad402834250ff50Timo Sirainen returning FALSE, next_secs_r is set to user's next replication time. */
5ca40db1f869421fc3798a304ad402834250ff50Timo Sirainenbool replicator_queue_want_sync_now(struct replicator_queue *queue,
5ca40db1f869421fc3798a304ad402834250ff50Timo Sirainen struct replicator_user *user,
5ca40db1f869421fc3798a304ad402834250ff50Timo Sirainen unsigned int *next_secs_r);
b5ffdf79df3d9ace5a7c1fc08bb2b1d177157a01Timo Sirainen/* Iterate through all users in the queue. */
b5ffdf79df3d9ace5a7c1fc08bb2b1d177157a01Timo Sirainenstruct replicator_queue_iter *
b5ffdf79df3d9ace5a7c1fc08bb2b1d177157a01Timo Sirainenreplicator_queue_iter_init(struct replicator_queue *queue);
b5ffdf79df3d9ace5a7c1fc08bb2b1d177157a01Timo Sirainenstruct replicator_user *
b5ffdf79df3d9ace5a7c1fc08bb2b1d177157a01Timo Sirainenreplicator_queue_iter_next(struct replicator_queue_iter *iter);
b5ffdf79df3d9ace5a7c1fc08bb2b1d177157a01Timo Sirainenvoid replicator_queue_iter_deinit(struct replicator_queue_iter **iter);
99a9e14caaf54d884dac6a520111cfc8849d8323Timo Sirainen
f59c16625dc8fb1b734fddbba58012ec42cf945dTimo Sirainenvoid replicator_queue_add_auth_users(struct replicator_queue *queue,
f59c16625dc8fb1b734fddbba58012ec42cf945dTimo Sirainen const char *auth_socket_path,
f59c16625dc8fb1b734fddbba58012ec42cf945dTimo Sirainen const char *usermask, time_t last_update);
f59c16625dc8fb1b734fddbba58012ec42cf945dTimo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#endif