Commit c1138d13 authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

[master] Merge branch 'trac1332' with fixing conflicts.

Conflicts:
	ChangeLog
	src/lib/datasrc/sqlite3_accessor.h
	src/lib/datasrc/tests/database_unittest.cc
parents ed91f985 9f89f07a
......@@ -15,6 +15,8 @@
#ifndef __DATA_SOURCE_CLIENT_H
#define __DATA_SOURCE_CLIENT_H 1
#include <utility>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
......@@ -311,6 +313,55 @@ public:
virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
bool replace, bool journaling = false)
const = 0;
/// Return a journal reader to retrieve differences of a zone.
///
/// A derived version of this method creates a concrete
/// \c ZoneJournalReader object specific to the underlying data source
/// for the specified name of zone and differences between the versions
/// specified by the beginning and ending serials of the corresponding
/// SOA RRs.
/// The RR class of the zone is the one that the client is expected to
/// handle (see the detailed description of this class).
///
/// Note that the SOA serials are compared by the semantics of the serial
/// number arithmetic. So, for example, \c begin_serial can be larger than
/// \c end_serial as bare unsigned integers. The underlying data source
/// implementation is assumed to keep track of sufficient history to
/// identify (if exist) the corresponding difference between the specified
/// versions.
///
/// This method returns the result as a pair of a result code and
/// a pointer to a \c ZoneJournalReader object. On success, the result
/// code is \c SUCCESS and the pointer must be non NULL; otherwise
/// the result code is something other than \c SUCCESS and the pinter
/// must be NULL.
///
/// If the specified zone is not found in the data source, the result
/// code is \c NO_SUCH_ZONE.
/// Otherwise, if specified range of difference for the zone is not found
/// in the data source, the result code is \c NO_SUCH_VERSION.
///
/// Handling differences is an optional feature of data source.
/// If the underlying data source does not support difference handling,
/// this method for that type of data source can throw an exception of
/// class \c NotImplemented.
///
/// \exception NotImplemented The data source does not support differences.
/// \exception DataSourceError Other operational errors at the data source
/// level.
///
/// \param zone The name of the zone for which the difference should be
/// retrieved.
/// \param begin_serial The SOA serial of the beginning version of the
/// differences.
/// \param end_serial The SOA serial of the ending version of the
/// differences.
///
/// \return A pair of result code and a pointer to \c ZoneJournalReader.
virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
getJournalReader(const isc::dns::Name& zone, uint32_t begin_serial,
uint32_t end_serial) const = 0;
};
}
}
......
......@@ -13,6 +13,7 @@
// PERFORMANCE OF THIS SOFTWARE.
#include <string>
#include <utility>
#include <vector>
#include <datasrc/database.h>
......@@ -1069,5 +1070,105 @@ DatabaseClient::getUpdater(const isc::dns::Name& name, bool replace,
return (ZoneUpdaterPtr(new DatabaseUpdater(update_accessor, zone.second,
name, rrclass_, journaling)));
}
//
// Zone journal reader using some database system as the underlying data
// source.
//
class DatabaseJournalReader : public ZoneJournalReader {
private:
// A shortcut typedef to keep the code concise.
typedef DatabaseAccessor Accessor;
public:
DatabaseJournalReader(shared_ptr<Accessor> accessor, const Name& zone,
int zone_id, const RRClass& rrclass, uint32_t begin,
uint32_t end) :
accessor_(accessor), zone_(zone), rrclass_(rrclass),
begin_(begin), end_(end), finished_(false)
{
context_ = accessor_->getDiffs(zone_id, begin, end);
}
virtual ~DatabaseJournalReader() {}
virtual ConstRRsetPtr getNextDiff() {
if (finished_) {
isc_throw(InvalidOperation,
"Diff read attempt past the end of sequence on "
<< accessor_->getDBName());
}
string data[Accessor::COLUMN_COUNT];
if (!context_->getNext(data)) {
finished_ = true;
LOG_DEBUG(logger, DBG_TRACE_BASIC,
DATASRC_DATABASE_JOURNALREADER_END).
arg(zone_).arg(rrclass_).arg(accessor_->getDBName()).
arg(begin_).arg(end_);
return (ConstRRsetPtr());
}
try {
RRsetPtr rrset(new RRset(Name(data[Accessor::NAME_COLUMN]),
rrclass_,
RRType(data[Accessor::TYPE_COLUMN]),
RRTTL(data[Accessor::TTL_COLUMN])));
rrset->addRdata(rdata::createRdata(rrset->getType(), rrclass_,
data[Accessor::RDATA_COLUMN]));
LOG_DEBUG(logger, DBG_TRACE_DETAILED,
DATASRC_DATABASE_JOURNALREADER_NEXT).
arg(rrset->getName()).arg(rrset->getType()).
arg(zone_).arg(rrclass_).arg(accessor_->getDBName());
return (rrset);
} catch (const Exception& ex) {
LOG_ERROR(logger, DATASRC_DATABASE_JOURNALREADR_BADDATA).
arg(zone_).arg(rrclass_).arg(accessor_->getDBName()).
arg(begin_).arg(end_).arg(ex.what());
isc_throw(DataSourceError, "Failed to create RRset from diff on "
<< accessor_->getDBName());
}
}
private:
shared_ptr<Accessor> accessor_;
const Name zone_;
const RRClass rrclass_;
Accessor::IteratorContextPtr context_;
const uint32_t begin_;
const uint32_t end_;
bool finished_;
};
// The JournalReader factory
pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
DatabaseClient::getJournalReader(const isc::dns::Name& zone,
uint32_t begin_serial,
uint32_t end_serial) const
{
shared_ptr<DatabaseAccessor> jnl_accessor(accessor_->clone());
const pair<bool, int> zoneinfo(jnl_accessor->getZone(zone.toText()));
if (!zoneinfo.first) {
return (pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>(
ZoneJournalReader::NO_SUCH_ZONE,
ZoneJournalReaderPtr()));
}
try {
const pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> ret(
ZoneJournalReader::SUCCESS,
ZoneJournalReaderPtr(new DatabaseJournalReader(jnl_accessor,
zone,
zoneinfo.second,
rrclass_,
begin_serial,
end_serial)));
LOG_DEBUG(logger, DBG_TRACE_BASIC,
DATASRC_DATABASE_JOURNALREADER_START).arg(zone).arg(rrclass_).
arg(jnl_accessor->getDBName()).arg(begin_serial).arg(end_serial);
return (ret);
} catch (const NoSuchSerial&) {
return (pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>(
ZoneJournalReader::NO_SUCH_VERSION,
ZoneJournalReaderPtr()));
}
}
}
}
......@@ -23,6 +23,8 @@
#include <dns/rrclass.h>
#include <dns/rrset.h>
#include <datasrc/data_source.h>
#include <datasrc/client.h>
#include <datasrc/client.h>
#include <dns/name.h>
......@@ -544,12 +546,10 @@ public:
/// is not for the SOA RR; it passes TTL for a diff that deletes an RR
/// while in \c deleteRecordInZone() it's omitted. This is because
/// the stored diffs are expected to be retrieved in the form that
/// \c getRecordDiffs() is expected to meet. This means if the caller
/// \c getDiffs() is expected to meet. This means if the caller
/// wants to use this method with other update operations, it must
/// ensure the additional information is ready when this method is called.
///
/// \note \c getRecordDiffs() is not yet implemented.
///
/// The caller of this method must ensure that the added diffs via
/// this method in a single transaction form an IXFR-style difference
/// sequences: Each difference sequence is a sequence of RRs:
......@@ -562,7 +562,7 @@ public:
/// an SOA RR, \c serial must be identical to the serial of that SOA).
/// The underlying derived class implementation may or may not check
/// this condition, but if the caller doesn't meet the condition
/// a subsequent call to \c getRecordDiffs() will not work as expected.
/// a subsequent call to \c getDiffs() will not work as expected.
///
/// Any call to this method must be in a transaction, and, for now,
/// it must be a transaction triggered by \c startUpdateZone() (that is,
......@@ -932,6 +932,15 @@ public:
bool replace,
bool journaling = false) const;
/// This implementation internally clones the accessor from the one
/// used in the client for retrieving diffs and iterating over them.
/// The returned reader object will be able to work separately from
/// the original client.
virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
getJournalReader(const isc::dns::Name& zone, uint32_t begin_serial,
uint32_t end_serial) const;
private:
/// \brief The RR class that this client handles.
const isc::dns::RRClass rrclass_;
......
......@@ -630,3 +630,31 @@ database module are shown in the log message.
Debug information. A set of updates to a zone has been successfully
committed to the corresponding database backend. The zone name,
its class and the database name are printed.
% DATASRC_DATABASE_JOURNALREADER_START %1/%2 on %3 from %4 to %5
This is a debug message indicating that the program starts reading
a zone's difference sequences from a database-based data source. The
zone's name and class, database name, and the start and end serials
are shown in the message.
% DATASRC_DATABASE_JOURNALREADER_NEXT %1/%2 in %3/%4 on %5
This is a debug message indicating that the program retrieves one
difference in difference sequences of a zone and successfully converts
it to an RRset. The zone's name and class, database name, and the
name and RR type of the retrieved diff are shown in the message.
% DATASRC_DATABASE_JOURNALREADER_END %1/%2 on %3 from %4 to %5
This is a debug message indicating that the program (successfully)
reaches the end of sequences of a zone's differences. The zone's name
and class, database name, and the start and end serials are shown in
the message.
% DATASRC_DATABASE_JOURNALREADR_BADDATA failed to convert a diff to RRset in %1/%2 on %3 between %4 and %5: %6
This is an error message indicating that a zone's diff is broken and
the data source library failed to convert it to a valid RRset. The
most likely cause of this is that someone has manually modified the
zone's diff in the database and inserted invalid data as a result.
The zone's name and class, database name, and the start and end
serials, and an additional detail of the error are shown in the
message. The administrator should examine the diff in the database
to find any invalid data and fix it.
......@@ -850,6 +850,13 @@ InMemoryClient::getUpdater(const isc::dns::Name&, bool, bool) const {
isc_throw(isc::NotImplemented, "Update attempt on in memory data source");
}
pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
InMemoryClient::getJournalReader(const isc::dns::Name&, uint32_t,
uint32_t) const
{
isc_throw(isc::NotImplemented, "Journaling isn't supported for "
"in memory data source");
}
namespace {
// convencience function to add an error message to a list of those
......
......@@ -287,6 +287,10 @@ public:
bool replace, bool journaling = false)
const;
virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
getJournalReader(const isc::dns::Name& zone, uint32_t begin_serial,
uint32_t end_serial) const;
private:
// TODO: Do we still need the PImpl if nobody should manipulate this class
// directly any more (it should be handled through DataSourceClient)?
......
......@@ -71,7 +71,6 @@ public:
DataSourceError(file, line, what) {}
};
struct SQLite3Parameters;
/**
......
......@@ -12,6 +12,8 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <utility>
#include <datasrc/client.h>
#include <dns/name.h>
......@@ -37,6 +39,11 @@ public:
{
return (ZoneUpdaterPtr());
}
virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
getJournalReader(const isc::dns::Name&, uint32_t, uint32_t) const {
isc_throw(isc::NotImplemented, "Journaling isn't supported "
"in Nop data source");
}
};
class ClientTest : public ::testing::Test {
......
This diff is collapsed.
CLEANFILES = *.copied
BUILT_SOURCES = rwtest.sqlite3.copied
# We use install-sh with the -m option to make sure it's writable
rwtest.sqlite3.copied: $(srcdir)/rwtest.sqlite3
$(top_srcdir)/install-sh -m 644 $(srcdir)/rwtest.sqlite3 $@
......@@ -560,6 +560,98 @@ public:
/// \brief A pointer-like type pointing to a \c ZoneUpdater object.
typedef boost::shared_ptr<ZoneUpdater> ZoneUpdaterPtr;
/// The base class for retrieving differences between two versions of a zone.
///
/// On construction, each derived class object will internally set up
/// retrieving sequences of differences between two specific version of
/// a specific zone managed in a particular data source. So the constructor
/// of a derived class would normally take parameters to identify the zone
/// and the two versions for which the differences should be retrieved.
/// See \c DataSourceClient::getJournalReader for more concrete details
/// used in this API.
///
/// Once constructed, an object of this class will act like an iterator
/// over the sequences. Every time the \c getNextDiff() method is called
/// it returns one element of the differences in the form of an \c RRset
/// until it reaches the end of the entire sequences.
class ZoneJournalReader {
public:
/// Result codes used by a factory method for \c ZoneJournalReader
enum Result {
SUCCESS, ///< A \c ZoneJournalReader object successfully created
NO_SUCH_ZONE, ///< Specified zone does not exist in the data source
NO_SUCH_VERSION ///< Specified versions do not exist in the diff storage
};
protected:
/// The default constructor.
///
/// This is intentionally defined as protected to ensure that this base
/// class is never instantiated directly.
ZoneJournalReader() {}
public:
/// The destructor
virtual ~ZoneJournalReader() {}
/// Return the next difference RR of difference sequences.
///
/// In this API, the difference between two versions of a zone is
/// conceptually represented as IXFR-style difference sequences:
/// Each difference sequence is a sequence of RRs: an older version of
/// SOA (to be deleted), zero or more other deleted RRs, the
/// post-transaction SOA (to be added), and zero or more other
/// added RRs. (Note, however, that the underlying data source
/// implementation may or may not represent the difference in
/// straightforward realization of this concept. The mapping between
/// the conceptual difference and the actual implementation is hidden
/// in each derived class).
///
/// This method provides an application with a higher level interface
/// to retrieve the difference along with the conceptual model: the
/// \c ZoneJournalReader object iterates over the entire sequences
/// from the beginning SOA (which is to be deleted) to one of the
/// added RR of with the ending SOA, and each call to this method returns
/// one RR in the form of an \c RRset that contains exactly one RDATA
/// in the order of the sequences.
///
/// Note that the ordering of the sequences specifies the semantics of
/// each difference: add or delete. For example, the first RR is to
/// be deleted, and the last RR is to be added. So the return value
/// of this method does not explicitly indicate whether the RR is to be
/// added or deleted.
///
/// This method ensures the returned \c RRset represents an RR, that is,
/// it contains exactly one RDATA. However, it does not necessarily
/// ensure that the resulting sequences are in the form of IXFR-style.
/// For example, the first RR is supposed to be an SOA, and it should
/// normally be the case, but this interface does not necessarily require
/// the derived class implementation ensure this. Normally the
/// differences are expected to be stored using this API (via a
/// \c ZoneUpdater object), and as long as that is the case and the
/// underlying implementation follows the requirement of the API, the
/// result of this method should be a valid IXFR-style sequences.
/// So this API does not mandate the almost redundant check as part of
/// the interface. If the application needs to make it sure 100%, it
/// must check the resulting sequence itself.
///
/// Once the object reaches the end of the sequences, this method returns
/// \c Null. Any subsequent call will result in an exception of
/// class \c InvalidOperation.
///
/// \exception InvalidOperation The method is called beyond the end of
/// the difference sequences.
/// \exception DataSourceError Underlying data is broken and the RR
/// cannot be created or other low level data source error.
///
/// \return An \c RRset that contains one RDATA corresponding to the
/// next difference in the sequences.
virtual isc::dns::ConstRRsetPtr getNextDiff() = 0;
};
/// \brief A pointer-like type pointing to a \c ZoneUpdater object.
typedef boost::shared_ptr<ZoneJournalReader> ZoneJournalReaderPtr;
} // end of datasrc
} // end of isc
......
......@@ -126,6 +126,17 @@ public:
isc::Exception(file, line, what) {}
};
/// \brief A generic exception that is thrown if a function is called
/// in a prohibited way.
///
/// For example, this can happen if a class method is called when the object's
/// state does not allow that particular method.
class InvalidOperation : public Exception {
public:
InvalidOperation(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) {}
};
///
/// \brief A generic exception that is thrown when an unexpected
/// error condition occurs.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment