LCOV - code coverage report
Current view: top level - api - replication.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 182 247 73.7 %
Date: 2019-06-30 05:20:33 Functions: 18 21 85.7 %
Branches: 166 442 37.6 %

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

Generated by: LCOV version 1.11