LCOV - code coverage report
Current view: top level - api - replication.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7028d852e609 Lines: 185 248 74.6 %
Date: 2019-02-17 14:59:59 Functions: 18 21 85.7 %
Branches: 165 436 37.8 %

           Branch data     Line data    Source code
       1                 :            : /** @file replication.cc
       2                 :            :  * @brief Replication support for Xapian databases.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2008 Lemur Consulting Ltd
       5                 :            :  * Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017 Olly Betts
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or modify
       8                 :            :  * it under the terms of the GNU General Public License as published by
       9                 :            :  * the Free Software Foundation; either version 2 of the License, or
      10                 :            :  * (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <config.h>
      23                 :            : 
      24                 :            : #include "replication.h"
      25                 :            : 
      26                 :            : #include "xapian/intrusive_ptr.h"
      27                 :            : #include "xapian/constants.h"
      28                 :            : #include "xapian/dbfactory.h"
      29                 :            : #include "xapian/error.h"
      30                 :            : #include "xapian/version.h"
      31                 :            : 
      32                 :            : #include "backends/databaseinternal.h"
      33                 :            : #include "backends/databasereplicator.h"
      34                 :            : #include "debuglog.h"
      35                 :            : #include "filetests.h"
      36                 :            : #include "fileutils.h"
      37                 :            : #include "io_utils.h"
      38                 :            : #include "omassert.h"
      39                 :            : #include "pack.h"
      40                 :            : #include "realtime.h"
      41                 :            : #include "net/remoteconnection.h"
      42                 :            : #include "replicationprotocol.h"
      43                 :            : #include "safesysstat.h"
      44                 :            : #include "safeunistd.h"
      45                 :            : #include "net/length.h"
      46                 :            : #include "str.h"
      47                 :            : #include "unicode/description_append.h"
      48                 :            : 
      49                 :            : #include <cerrno>
      50                 :            : #include <fstream>
      51                 :            : #include <memory>
      52                 :            : #include <string>
      53                 :            : 
      54                 :            : using namespace std;
      55                 :            : using namespace Xapian;
      56                 :            : 
      57                 :            : // The banner comment used at the top of the replica's stub database file.
      58                 :            : #define REPLICA_STUB_BANNER \
      59                 :            : "# Automatically generated by Xapian::DatabaseReplica v" XAPIAN_VERSION ".\n" \
      60                 :            : "# Do not manually edit - replication operations may regenerate this file.\n"
      61                 :            : 
      62                 :            : [[noreturn]]
      63                 :            : static void
      64                 :          1 : throw_connection_closed_unexpectedly()
      65                 :            : {
      66 [ +  - ][ +  - ]:          1 :     throw Xapian::NetworkError("Connection closed unexpectedly");
                 [ +  - ]
      67                 :            : }
      68                 :            : 
      69                 :            : void
      70                 :         23 : DatabaseMaster::write_changesets_to_fd(int fd,
      71                 :            :                                        const string & start_revision,
      72                 :            :                                        ReplicationInfo * info) const
      73                 :            : {
      74                 :            :     LOGCALL_VOID(REPLICA, "DatabaseMaster::write_changesets_to_fd", fd | start_revision | info);
      75         [ +  - ]:         23 :     if (info != NULL)
      76                 :         23 :         info->clear();
      77         [ +  - ]:         23 :     Database db;
      78                 :            :     try {
      79 [ +  - ][ +  - ]:         23 :         db = Database(path);
      80         [ #  # ]:          0 :     } catch (const Xapian::DatabaseError & e) {
      81   [ #  #  #  # ]:          0 :         RemoteConnection conn(-1, fd);
      82                 :            :         conn.send_message(REPL_REPLY_FAIL,
      83         [ #  # ]:          0 :                           "Can't open database: " + e.get_msg(),
      84         [ #  # ]:          0 :                           0.0);
      85                 :         23 :         return;
      86                 :            :     }
      87 [ +  - ][ -  + ]:         23 :     if (db.internal->size() != 1) {
      88 [ #  # ][ #  # ]:          0 :         throw Xapian::InvalidOperationError("DatabaseMaster needs to be pointed at exactly one subdatabase");
                 [ #  # ]
      89                 :            :     }
      90                 :            : 
      91                 :            :     // Extract the UUID from start_revision and compare it to the database.
      92                 :         23 :     bool need_whole_db = false;
      93 [ +  - ][ +  - ]:         46 :     string revision;
      94         [ +  + ]:         23 :     if (start_revision.empty()) {
      95                 :          2 :         need_whole_db = true;
      96                 :            :     } else {
      97                 :         21 :         const char * ptr = start_revision.data();
      98                 :         21 :         const char * end = ptr + start_revision.size();
      99                 :            :         size_t uuid_length;
     100         [ +  - ]:         21 :         decode_length_and_check(&ptr, end, uuid_length);
     101         [ +  - ]:         21 :         string request_uuid(ptr, uuid_length);
     102                 :         21 :         ptr += uuid_length;
     103         [ +  - ]:         42 :         string db_uuid = db.internal->get_uuid();
     104 [ +  - ][ +  + ]:         21 :         if (request_uuid != db_uuid) {
     105                 :          6 :             need_whole_db = true;
     106                 :            :         }
     107         [ +  - ]:         42 :         revision.assign(ptr, end - ptr);
     108                 :            :     }
     109                 :            : 
     110         [ +  - ]:         46 :     db.internal->write_changesets_to_fd(fd, revision, need_whole_db, info);
     111                 :            : }
     112                 :            : 
     113                 :            : string
     114                 :          0 : DatabaseMaster::get_description() const
     115                 :            : {
     116         [ #  # ]:          0 :     string desc = "DatabaseMaster(";
     117         [ #  # ]:          0 :     description_append(desc, path);
     118         [ #  # ]:          0 :     desc += ")";
     119                 :          0 :     return desc;
     120                 :            : }
     121                 :            : 
     122                 :            : /// Internal implementation of DatabaseReplica
     123                 :            : class DatabaseReplica::Internal : public Xapian::Internal::intrusive_base {
     124                 :            :     /// Don't allow assignment.
     125                 :            :     void operator=(const Internal &);
     126                 :            : 
     127                 :            :     /// Don't allow copying.
     128                 :            :     Internal(const Internal &);
     129                 :            : 
     130                 :            :     /// The path to the replica directory.
     131                 :            :     string path;
     132                 :            : 
     133                 :            :     /// The id of the currently live database in the replica (0 or 1).
     134                 :            :     int live_id;
     135                 :            : 
     136                 :            :     /** The live database being replicated.
     137                 :            :      *
     138                 :            :      *  This needs to be mutable because it is sometimes lazily opened.
     139                 :            :      */
     140                 :            :     mutable WritableDatabase live_db;
     141                 :            : 
     142                 :            :     /// Do we need to heal the replica?
     143                 :            :     bool live_db_corrupt = false;
     144                 :            : 
     145                 :            :     /** Do we have an offline database currently?
     146                 :            :      *
     147                 :            :      *  The offline database is a new copy of the database we're bringing up
     148                 :            :      *  to the required revision, which can't yet be made live.
     149                 :            :      */
     150                 :            :     bool have_offline_db;
     151                 :            : 
     152                 :            :     /** Flag to indicate that the only valid operation next is a full copy.
     153                 :            :      */
     154                 :            :     bool need_copy_next;
     155                 :            : 
     156                 :            :     /** The revision that the secondary database has been updated to.
     157                 :            :      */
     158                 :            :     string offline_revision;
     159                 :            : 
     160                 :            :     /** The UUID of the secondary database.
     161                 :            :      */
     162                 :            :     string offline_uuid;
     163                 :            : 
     164                 :            :     /** The revision that the secondary database must reach before it can be
     165                 :            :      *  made live.
     166                 :            :      */
     167                 :            :     string offline_needed_revision;
     168                 :            : 
     169                 :            :     /** The time at which a changeset was last applied to the live database.
     170                 :            :      *
     171                 :            :      *  Set to 0 if no changeset applied to the live database so far.
     172                 :            :      */
     173                 :            :     double last_live_changeset_time;
     174                 :            : 
     175                 :            :     /// The remote connection we're using.
     176                 :            :     RemoteConnection * conn;
     177                 :            : 
     178                 :            :     /** Update the stub database which points to a single database.
     179                 :            :      *
     180                 :            :      *  The stub database file is created at a separate path, and then
     181                 :            :      *  atomically moved into place to replace the old stub database.  This
     182                 :            :      *  should allow searches to continue uninterrupted.
     183                 :            :      */
     184                 :            :     void update_stub_database() const;
     185                 :            : 
     186                 :            :     /** Delete the offline database. */
     187                 :            :     void remove_offline_db();
     188                 :            : 
     189                 :            :     /** Apply a set of DB copy messages from the connection.
     190                 :            :      */
     191                 :            :     void apply_db_copy(double end_time);
     192                 :            : 
     193                 :            :     /** Check that a message type is as expected.
     194                 :            :      *
     195                 :            :      *  Throws a NetworkError if the type is not the expected one.
     196                 :            :      */
     197                 :            :     void check_message_type(int type, int expected) const;
     198                 :            : 
     199                 :            :     /** Check if the offline database has reached the required version.
     200                 :            :      *
     201                 :            :      *  If so, make it live, and remove the old live database.
     202                 :            :      *
     203                 :            :      *  @return true iff the offline database is made live
     204                 :            :      */
     205                 :            :     bool possibly_make_offline_live();
     206                 :            : 
     207                 :        102 :     string get_replica_path(int id) const {
     208                 :        102 :         string p = path;
     209         [ +  - ]:        102 :         p += "/replica_";
     210         [ +  - ]:        102 :         p += char('0' + id);
     211                 :        102 :         return p;
     212                 :            :     }
     213                 :            : 
     214                 :            :   public:
     215                 :            :     /// Open a new DatabaseReplica::Internal for the specified path.
     216                 :            :     explicit Internal(const string & path_);
     217                 :            : 
     218                 :            :     /// Destructor.
     219         [ +  - ]:         16 :     ~Internal() { delete conn; }
     220                 :            : 
     221                 :            :     /// Get a string describing the current revision of the replica.
     222                 :            :     string get_revision_info() const;
     223                 :            : 
     224                 :            :     /// Set the file descriptor to read changesets from.
     225                 :            :     void set_read_fd(int fd);
     226                 :            : 
     227                 :            :     /// Read and apply the next changeset.
     228                 :            :     bool apply_next_changeset(ReplicationInfo * info,
     229                 :            :                               double reader_close_time);
     230                 :            : 
     231                 :            :     /// Return a string describing this object.
     232                 :          0 :     string get_description() const { return path; }
     233                 :            : };
     234                 :            : 
     235                 :            : // Methods of DatabaseReplica
     236                 :            : 
     237                 :          8 : DatabaseReplica::DatabaseReplica(const string & path)
     238         [ +  - ]:          8 :         : internal(new DatabaseReplica::Internal(path))
     239                 :            : {
     240                 :            :     LOGCALL_CTOR(REPLICA, "DatabaseReplica", path);
     241                 :          8 : }
     242                 :            : 
     243                 :          8 : DatabaseReplica::~DatabaseReplica()
     244                 :            : {
     245                 :            :     LOGCALL_DTOR(REPLICA, "DatabaseReplica");
     246         [ +  - ]:          8 :     delete internal;
     247                 :          8 : }
     248                 :            : 
     249                 :            : string
     250                 :         22 : DatabaseReplica::get_revision_info() const
     251                 :            : {
     252                 :            :     LOGCALL(REPLICA, string, "DatabaseReplica::get_revision_info", NO_ARGS);
     253                 :         22 :     RETURN(internal->get_revision_info());
     254                 :            : }
     255                 :            : 
     256                 :            : void
     257                 :         69 : DatabaseReplica::set_read_fd(int fd)
     258                 :            : {
     259                 :            :     LOGCALL_VOID(REPLICA, "DatabaseReplica::set_read_fd", fd);
     260                 :         69 :     internal->set_read_fd(fd);
     261                 :         69 : }
     262                 :            : 
     263                 :            : bool
     264                 :         82 : DatabaseReplica::apply_next_changeset(ReplicationInfo * info,
     265                 :            :                                       double reader_close_time)
     266                 :            : {
     267                 :            :     LOGCALL(REPLICA, bool, "DatabaseReplica::apply_next_changeset", info | reader_close_time);
     268         [ +  - ]:         82 :     if (info != NULL)
     269                 :         82 :         info->clear();
     270                 :         82 :     RETURN(internal->apply_next_changeset(info, reader_close_time));
     271                 :            : }
     272                 :            : 
     273                 :            : string
     274                 :          0 : DatabaseReplica::get_description() const
     275                 :            : {
     276         [ #  # ]:          0 :     string desc("DatabaseReplica(");
     277 [ #  # ][ #  # ]:          0 :     desc += internal->get_description();
     278         [ #  # ]:          0 :     desc += ')';
     279                 :          0 :     return desc;
     280                 :            : }
     281                 :            : 
     282                 :            : // Methods of DatabaseReplica::Internal
     283                 :            : 
     284                 :            : void
     285                 :         15 : DatabaseReplica::Internal::update_stub_database() const
     286                 :            : {
     287         [ +  - ]:         15 :     string stub_path = path;
     288         [ +  - ]:         15 :     stub_path += "/XAPIANDB";
     289         [ +  - ]:         30 :     string tmp_path = stub_path;
     290         [ +  - ]:         15 :     tmp_path += ".tmp";
     291                 :            :     {
     292         [ +  - ]:         15 :         ofstream stub(tmp_path.c_str());
     293                 :            :         stub << REPLICA_STUB_BANNER
     294 [ +  - ][ +  - ]:         15 :                 "auto replica_" << live_id << endl;
                 [ +  - ]
     295                 :            :     }
     296 [ +  - ][ -  + ]:         15 :     if (!io_tmp_rename(tmp_path, stub_path)) {
     297         [ #  # ]:          0 :         string msg("Failed to update stub db file for replica: ");
     298         [ #  # ]:          0 :         msg += path;
     299         [ #  # ]:          0 :         throw Xapian::DatabaseOpeningError(msg, errno);
     300                 :         15 :     }
     301                 :         15 : }
     302                 :            : 
     303                 :          8 : DatabaseReplica::Internal::Internal(const string & path_)
     304                 :            :         : path(path_), live_id(0), live_db(), have_offline_db(false),
     305                 :            :           need_copy_next(false), offline_revision(), offline_needed_revision(),
     306 [ +  - ][ +  - ]:          8 :           last_live_changeset_time(), conn(NULL)
         [ +  - ][ +  - ]
     307                 :            : {
     308                 :            :     LOGCALL_CTOR(REPLICA, "DatabaseReplica::Internal", path_);
     309                 :            : #ifndef XAPIAN_HAS_GLASS_BACKEND
     310                 :            :     throw FeatureUnavailableError("Replication requires the glass backend to be enabled");
     311                 :            : #else
     312         [ +  + ]:          8 :     if (mkdir(path.c_str(), 0777) == 0) {
     313                 :            :         // The database doesn't already exist - make a directory, containing a
     314                 :            :         // stub database, and point it to a new database.
     315                 :            :         //
     316                 :            :         // Create an empty database - the backend doesn't matter as if the
     317                 :            :         // master is a different type, then the replica will become that type
     318                 :            :         // automatically.
     319 [ +  - ][ +  - ]:         12 :         live_db = WritableDatabase(get_replica_path(live_id),
     320         [ +  - ]:          6 :                                    Xapian::DB_CREATE);
     321         [ +  - ]:          6 :         update_stub_database();
     322                 :            :     } else {
     323         [ -  + ]:          2 :         if (errno != EEXIST) {
     324 [ #  # ][ #  # ]:          0 :             throw DatabaseOpeningError("Couldn't create directory '" + path + "'", errno);
                 [ #  # ]
     325                 :            :         }
     326         [ -  + ]:          2 :         if (!dir_exists(path)) {
     327 [ #  # ][ #  # ]:          0 :             throw DatabaseOpeningError("Replica path must be a directory");
                 [ #  # ]
     328                 :            :         }
     329         [ +  - ]:          2 :         string stub_path = path;
     330         [ +  - ]:          2 :         stub_path += "/XAPIANDB";
     331                 :            :         try {
     332 [ +  + ][ -  + ]:          3 :             live_db = WritableDatabase(stub_path,
     333         [ +  - ]:          1 :                                        Xapian::DB_OPEN|Xapian::DB_BACKEND_STUB);
     334         [ +  - ]:          1 :         } catch (const Xapian::DatabaseCorruptError &) {
     335                 :            :             // If the database is too corrupt to open, force a full copy so we
     336                 :            :             // auto-heal from this condition.  Instance seen in the wild was
     337                 :            :             // that the replica had all files truncated to size 0.
     338                 :          1 :             live_db_corrupt = true;
     339                 :            :         }
     340                 :            :         // FIXME: simplify all this?
     341         [ +  - ]:          4 :         ifstream stub(stub_path.c_str());
     342         [ +  - ]:          4 :         string line;
     343 [ +  - ][ +  - ]:          6 :         while (getline(stub, line)) {
                 [ +  - ]
     344 [ +  - ][ +  - ]:          6 :             if (!line.empty() && line[0] != '#') {
         [ +  + ][ +  + ]
     345         [ +  - ]:          2 :                 live_id = line[line.size() - 1] - '0';
     346                 :          2 :                 break;
     347                 :            :             }
     348                 :          2 :         }
     349                 :            :     }
     350                 :            : #endif
     351                 :          8 : }
     352                 :            : 
     353                 :            : string
     354                 :         22 : DatabaseReplica::Internal::get_revision_info() const
     355                 :            : {
     356                 :            :     LOGCALL(REPLICA, string, "DatabaseReplica::Internal::get_revision_info", NO_ARGS);
     357         [ +  + ]:         22 :     if (live_db_corrupt) {
     358         [ +  - ]:          1 :         RETURN(string());
     359                 :            :     }
     360                 :            : 
     361         [ +  - ]:         21 :     switch (live_db.internal->size()) {
              [ +  +  - ]
     362                 :            :         case 0:
     363 [ +  - ][ +  - ]:          1 :             live_db = WritableDatabase(get_replica_path(live_id), Xapian::DB_OPEN);
                 [ +  - ]
     364                 :          1 :             break;
     365                 :            :         case 1:
     366                 :            :             // OK
     367                 :         20 :             break;
     368                 :            :         default:
     369                 :            :             throw Xapian::InvalidOperationError("DatabaseReplica needs to be "
     370                 :            :                                                 "pointed at exactly one "
     371 [ #  # ][ #  # ]:          0 :                                                 "subdatabase");
                 [ #  # ]
     372                 :            :     }
     373                 :            : 
     374         [ +  - ]:         21 :     string uuid = live_db.get_uuid();
     375         [ +  - ]:         42 :     string buf = encode_length(uuid.size());
     376         [ +  - ]:         21 :     buf += uuid;
     377 [ +  - ][ +  - ]:         21 :     pack_uint(buf, live_db.get_revision());
     378                 :         43 :     RETURN(buf);
     379                 :            : }
     380                 :            : 
     381                 :            : void
     382                 :          9 : DatabaseReplica::Internal::remove_offline_db()
     383                 :            : {
     384                 :            :     // Delete the offline database.
     385         [ +  - ]:          9 :     removedir(get_replica_path(live_id ^ 1));
     386                 :          9 :     have_offline_db = false;
     387                 :          9 : }
     388                 :            : 
     389                 :            : void
     390                 :          9 : DatabaseReplica::Internal::apply_db_copy(double end_time)
     391                 :            : {
     392                 :          9 :     have_offline_db = true;
     393                 :          9 :     last_live_changeset_time = 0;
     394         [ +  - ]:          9 :     string offline_path = get_replica_path(live_id ^ 1);
     395                 :            :     // If there's already an offline database, discard it.  This happens if one
     396                 :            :     // copy of the database was sent, but further updates were needed before it
     397                 :            :     // could be made live, and the remote end was then unable to send those
     398                 :            :     // updates (probably due to not having changesets available, or the remote
     399                 :            :     // database being replaced by a new database).
     400         [ +  - ]:          9 :     removedir(offline_path);
     401         [ -  + ]:          9 :     if (mkdir(offline_path.c_str(), 0777)) {
     402         [ #  # ]:          0 :         throw Xapian::DatabaseError("Cannot make directory '" +
     403 [ #  # ][ #  # ]:          0 :                                     offline_path + "'", errno);
     404                 :            :     }
     405                 :            : 
     406                 :            :     {
     407         [ +  - ]:          9 :         string buf;
     408         [ +  - ]:          9 :         int type = conn->get_message(buf, end_time);
     409         [ +  - ]:          9 :         check_message_type(type, REPL_REPLY_DB_HEADER);
     410                 :          9 :         const char * ptr = buf.data();
     411                 :          9 :         const char * end = ptr + buf.size();
     412                 :            :         size_t uuid_length;
     413         [ +  - ]:          9 :         decode_length_and_check(&ptr, end, uuid_length);
     414         [ +  - ]:          9 :         offline_uuid.assign(ptr, uuid_length);
     415         [ +  - ]:          9 :         offline_revision.assign(buf, ptr + uuid_length - buf.data(), buf.npos);
     416                 :            :     }
     417                 :            : 
     418                 :            :     // Now, read the files for the database from the connection and create it.
     419                 :            :     while (true) {
     420         [ +  - ]:         52 :         string filename;
     421         [ +  - ]:         52 :         int type = conn->sniff_next_message_type(end_time);
     422 [ +  - ][ -  + ]:         52 :         if (type < 0 || type == REPL_REPLY_FAIL)
     423                 :          0 :             return;
     424         [ +  + ]:         52 :         if (type == REPL_REPLY_DB_FOOTER)
     425                 :          9 :             break;
     426                 :            : 
     427         [ +  - ]:         43 :         type = conn->get_message(filename, end_time);
     428         [ +  - ]:         43 :         check_message_type(type, REPL_REPLY_DB_FILENAME);
     429                 :            : 
     430                 :            :         // Check that the filename doesn't contain '..'.  No valid database
     431                 :            :         // file contains .., so we don't need to check that the .. is a path.
     432 [ +  - ][ -  + ]:         43 :         if (filename.find("..") != string::npos) {
     433 [ #  # ][ #  # ]:          0 :             throw NetworkError("Filename in database contains '..'");
                 [ #  # ]
     434                 :            :         }
     435                 :            : 
     436         [ +  - ]:         43 :         type = conn->sniff_next_message_type(end_time);
     437 [ +  - ][ -  + ]:         43 :         if (type < 0 || type == REPL_REPLY_FAIL)
     438                 :          0 :             return;
     439                 :            : 
     440 [ +  - ][ +  - ]:         95 :         string filepath = offline_path + "/" + filename;
              [ +  -  + ]
     441         [ +  - ]:         43 :         type = conn->receive_file(filepath, end_time);
     442         [ -  + ]:         43 :         if (type < 0)
     443                 :          0 :             throw_connection_closed_unexpectedly();
     444         [ +  - ]:         43 :         check_message_type(type, REPL_REPLY_DB_FILEDATA);
     445                 :         43 :     }
     446         [ +  - ]:          9 :     int type = conn->get_message(offline_needed_revision, end_time);
     447         [ +  - ]:          9 :     check_message_type(type, REPL_REPLY_DB_FOOTER);
     448         [ +  - ]:          9 :     need_copy_next = false;
     449                 :            : }
     450                 :            : 
     451                 :            : void
     452                 :        126 : DatabaseReplica::Internal::check_message_type(int type, int expected) const
     453                 :            : {
     454         [ -  + ]:        126 :     if (type != expected) {
     455         [ #  # ]:          0 :         if (type < 0)
     456                 :          0 :             throw_connection_closed_unexpectedly();
     457         [ #  # ]:          0 :         string m = "Expected replication protocol message type #";
     458 [ #  # ][ #  # ]:          0 :         m += str(expected);
     459         [ #  # ]:          0 :         m += ", got #";
     460 [ #  # ][ #  # ]:          0 :         m += str(type);
     461 [ #  # ][ #  # ]:          0 :         throw NetworkError(m);
     462                 :            :     }
     463                 :        126 : }
     464                 :            : 
     465                 :            : bool
     466                 :          9 : DatabaseReplica::Internal::possibly_make_offline_live()
     467                 :            : {
     468         [ +  - ]:          9 :     string replica_path(get_replica_path(live_id ^ 1));
     469                 :          9 :     unique_ptr<DatabaseReplicator> replicator;
     470                 :            :     try {
     471         [ +  - ]:          9 :         replicator.reset(DatabaseReplicator::open(replica_path));
     472                 :          0 :     } catch (const Xapian::DatabaseError &) {
     473                 :          0 :         return false;
     474                 :            :     }
     475         [ -  + ]:          9 :     if (offline_needed_revision.empty()) {
     476                 :          0 :         return false;
     477                 :            :     }
     478         [ -  + ]:         18 :     if (!replicator->check_revision_at_least(offline_revision,
     479         [ +  - ]:          9 :                                              offline_needed_revision)) {
     480                 :          0 :         return false;
     481                 :            :     }
     482                 :            : 
     483         [ +  - ]:         18 :     string replicated_uuid = replicator->get_uuid();
     484         [ -  + ]:          9 :     if (replicated_uuid.empty()) {
     485                 :          0 :         return false;
     486                 :            :     }
     487                 :            : 
     488 [ +  - ][ -  + ]:          9 :     if (replicated_uuid != offline_uuid) {
     489                 :          0 :         return false;
     490                 :            :     }
     491                 :            : 
     492                 :          9 :     live_id ^= 1;
     493                 :            :     // Open the database first, so that if there's a problem, an exception
     494                 :            :     // will be thrown before we make the new database live.
     495 [ +  - ][ +  - ]:          9 :     live_db = WritableDatabase(replica_path, Xapian::DB_OPEN);
     496                 :          9 :     live_db_corrupt = false;
     497         [ +  - ]:          9 :     update_stub_database();
     498         [ +  - ]:          9 :     remove_offline_db();
     499         [ #  # ]:         18 :     return true;
     500                 :            : }
     501                 :            : 
     502                 :            : void
     503                 :         69 : DatabaseReplica::Internal::set_read_fd(int fd)
     504                 :            : {
     505         [ +  + ]:         69 :     delete conn;
     506                 :         69 :     conn = NULL;
     507 [ +  - ][ +  - ]:         69 :     conn = new RemoteConnection(fd, -1);
     508                 :         69 : }
     509                 :            : 
     510                 :            : bool
     511                 :         82 : DatabaseReplica::Internal::apply_next_changeset(ReplicationInfo * info,
     512                 :            :                                                 double reader_close_time)
     513                 :            : {
     514                 :            :     LOGCALL(REPLICA, bool, "DatabaseReplica::Internal::apply_next_changeset", info | reader_close_time);
     515                 :            :     while (true) {
     516                 :         91 :         int type = conn->sniff_next_message_type(0.0);
     517   [ +  +  +  -  :         91 :         switch (type) {
                   +  - ]
     518                 :            :             case REPL_REPLY_END_OF_CHANGES: {
     519         [ +  - ]:         22 :                 string buf;
     520         [ +  - ]:         22 :                 type = conn->get_message(buf, 0.0);
     521         [ +  - ]:         22 :                 check_message_type(type, REPL_REPLY_END_OF_CHANGES);
     522                 :         22 :                 RETURN(false);
     523                 :            :             }
     524                 :            :             case REPL_REPLY_DB_HEADER:
     525                 :            :                 // Apply the copy - remove offline db in case of any error.
     526                 :            :                 try {
     527         [ +  - ]:          9 :                     apply_db_copy(0.0);
     528         [ +  - ]:          9 :                     if (info != NULL)
     529                 :          9 :                         ++(info->fullcopy_count);
     530         [ +  - ]:          9 :                     string replica_uuid;
     531                 :            :                     {
     532                 :            :                         unique_ptr<DatabaseReplicator> replicator(
     533 [ +  - ][ +  - ]:          9 :                                 DatabaseReplicator::open(get_replica_path(live_id ^ 1)));
     534 [ +  - ][ +  - ]:          9 :                         replica_uuid = replicator->get_uuid();
     535                 :            :                     }
     536 [ +  - ][ -  + ]:          9 :                     if (replica_uuid != offline_uuid) {
     537         [ #  # ]:          0 :                         remove_offline_db();
     538                 :            :                         // We've been sent an database with the wrong uuid,
     539                 :            :                         // which only happens if the database at the server
     540                 :            :                         // got changed during the copy, so the only safe
     541                 :            :                         // action next is a new copy.  Set a flag to ensure
     542                 :            :                         // that this happens, or we're at risk of database
     543                 :            :                         // corruption.
     544                 :          0 :                         need_copy_next = true;
     545                 :          9 :                     }
     546                 :          0 :                 } catch (...) {
     547         [ #  # ]:          0 :                     remove_offline_db();
     548                 :          0 :                     throw;
     549                 :            :                 }
     550         [ +  - ]:          9 :                 if (possibly_make_offline_live()) {
     551         [ +  - ]:          9 :                     if (info != NULL)
     552                 :          9 :                         info->changed = true;
     553                 :            :                 }
     554                 :          9 :                 break;
     555                 :            :             case REPL_REPLY_CHANGESET:
     556         [ -  + ]:         59 :                 if (need_copy_next) {
     557 [ #  # ][ #  # ]:          0 :                     throw NetworkError("Needed a database copy next");
                 [ #  # ]
     558                 :            :                 }
     559         [ +  - ]:         59 :                 if (!have_offline_db) {
     560                 :            :                     // Close the live db.
     561         [ +  - ]:         59 :                     string replica_path(get_replica_path(live_id));
     562 [ +  - ][ +  - ]:         59 :                     live_db = WritableDatabase();
     563                 :            : 
     564         [ +  + ]:         59 :                     if (last_live_changeset_time != 0.0) {
     565                 :            :                         // Wait until at least "reader_close_time" seconds have
     566                 :            :                         // passed since the last changeset was applied, to
     567                 :            :                         // allow any active readers to finish and be reopened.
     568                 :            :                         double until;
     569                 :          9 :                         until = last_live_changeset_time + reader_close_time;
     570         [ +  - ]:          9 :                         RealTime::sleep(until);
     571                 :            :                     }
     572                 :            : 
     573                 :            :                     // Open a replicator for the live path, and apply the
     574                 :            :                     // changeset.
     575                 :            :                     {
     576                 :            :                         unique_ptr<DatabaseReplicator> replicator(
     577         [ +  - ]:         59 :                                 DatabaseReplicator::open(replica_path));
     578                 :            : 
     579                 :            :                         // Ignore the returned revision number, since we are
     580                 :            :                         // live so the changeset must be safe to apply to a
     581                 :            :                         // live DB.
     582         [ +  + ]:         59 :                         replicator->apply_changeset_from_conn(*conn, 0.0, true);
     583                 :            :                     }
     584                 :         13 :                     last_live_changeset_time = RealTime::now();
     585                 :            : 
     586         [ +  - ]:         13 :                     if (info != NULL) {
     587                 :         13 :                         ++(info->changeset_count);
     588                 :         13 :                         info->changed = true;
     589                 :            :                     }
     590                 :            :                     // Now the replicator is closed, open the live db again.
     591 [ +  - ][ +  - ]:         13 :                     live_db = WritableDatabase(replica_path, Xapian::DB_OPEN);
     592                 :         13 :                     live_db_corrupt = false;
     593                 :         59 :                     RETURN(true);
     594                 :            :                 }
     595                 :            : 
     596                 :            :                 {
     597                 :            :                     unique_ptr<DatabaseReplicator> replicator(
     598 [ #  # ][ #  # ]:          0 :                             DatabaseReplicator::open(get_replica_path(live_id ^ 1)));
     599                 :            : 
     600         [ #  # ]:          0 :                     offline_revision = replicator->
     601         [ #  # ]:          0 :                             apply_changeset_from_conn(*conn, 0.0, false);
     602                 :            : 
     603         [ #  # ]:          0 :                     if (info != NULL) {
     604                 :          0 :                         ++(info->changeset_count);
     605                 :          0 :                     }
     606                 :            :                 }
     607         [ #  # ]:          0 :                 if (possibly_make_offline_live()) {
     608         [ #  # ]:          0 :                     if (info != NULL)
     609                 :          0 :                         info->changed = true;
     610                 :            :                 }
     611                 :          0 :                 RETURN(true);
     612                 :            :             case REPL_REPLY_FAIL: {
     613         [ #  # ]:          0 :                 string buf;
     614 [ #  # ][ #  # ]:          0 :                 if (conn->get_message(buf, 0.0) < 0)
     615                 :          0 :                     throw_connection_closed_unexpectedly();
     616 [ #  # ][ #  # ]:          0 :                 throw NetworkError("Unable to fully synchronise: " + buf);
                 [ #  # ]
     617                 :            :             }
     618                 :            :             case -1:
     619                 :          1 :                 throw_connection_closed_unexpectedly();
     620                 :            :             default:
     621 [ #  # ][ #  # ]:          0 :                 throw NetworkError("Unknown replication protocol message (" +
     622 [ #  # ][ #  # ]:          0 :                                    str(type) + ")");
                 [ #  # ]
     623                 :            :         }
     624                 :         44 :     }
     625                 :            : }

Generated by: LCOV version 1.11