diff.py 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Copyright (C) 2011  Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

"""
This helps the XFR in process with accumulating parts of diff and applying
it to the datasource.
19
20
21
22

The name of the module is not yet fully decided. We might want to move it
under isc.datasrc or somewhere else, because we might want to reuse it with
future DDNS process. But until then, it lives here.
23
24
"""

25
import isc.dns
26
27
import isc.log
from isc.log_messages.libxfrin_messages import *
28

29
30
31
32
33
34
class NoSuchZone(Exception):
    """
    This is raised if a diff for non-existant zone is being created.
    """
    pass

35
36
37
38
"""
This is the amount of changes we accumulate before calling Diff.apply
automatically.

39
The number 100 is just taken from BIND 9. We don't know the rationale
40
41
42
43
44
45
for exactly this amount, but we think it is just some randomly chosen
number.
"""
# If changing this, modify the tests accordingly as well.
DIFF_APPLY_TRESHOLD = 100

46
47
logger = isc.log.Logger('libxfrin')

48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Diff:
    """
    The class represents a diff against current state of datasource on
    one zone. The usual way of working with it is creating it, then putting
    bunch of changes in and commiting at the end.

    If you change your mind, you can just stop using the object without
    really commiting it. In that case no changes will happen in the data
    sounce.

    The class works as a kind of a buffer as well, it does not direct
    the changes to underlying data source right away, but keeps them for
    a while.
    """
62
    def __init__(self, ds_client, zone, replace=False, journaling=False):
63
64
65
66
67
        """
        Initializes the diff to a ready state. It checks the zone exists
        in the datasource and if not, NoSuchZone is raised. This also creates
        a transaction in the data source.

68
69
        The ds_client is the datasource client containing the zone. Zone is
        isc.dns.Name object representing the name of the zone (its apex).
70
        If replace is True, the content of the whole zone is wiped out before
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
71
        applying the diff.
72

73
74
75
76
77
78
        If journaling is True, the history of subsequent updates will be
        recorded as well as the updates themselves as long as the underlying
        data source support the journaling.  If the data source allows
        incoming updates but does not support journaling, the Diff object
        will still continue applying the diffs with disabling journaling.

79
80
81
        You can also expect isc.datasrc.Error or isc.datasrc.NotImplemented
        exceptions.
        """
82
83
84
85
86
87
88
        try:
            self.__updater = ds_client.get_updater(zone, replace, journaling)
        except isc.datasrc.NotImplemented as ex:
            if not journaling:
                raise ex
            self.__updater = ds_client.get_updater(zone, replace, False)
            logger.info(LIBXFRIN_NO_JOURNAL, zone, ds_client)
89
90
91
92
        if self.__updater is None:
            # The no such zone case
            raise NoSuchZone("Zone " + str(zone) +
                             " does not exist in the data source " +
93
                             str(ds_client))
94
        self.__buffer = []
95

96
97
    def __check_commited(self):
        """
98
99
        This checks if the diff is already commited or broken. If it is, it
        raises ValueError. This check is for methods that need to work only on
100
101
102
        yet uncommited diffs.
        """
        if self.__updater is None:
103
104
            raise ValueError("The diff is already commited or it has raised " +
                             "an exception, you come late")
105

106
107
108
109
    def __data_common(self, rr, operation):
        """
        Schedules an operation with rr.

110
        It does all the real work of add_data and delete_data, including
111
112
        all checks.
        """
113
        self.__check_commited()
114
115
116
        if rr.get_rdata_count() != 1:
            raise ValueError('The rrset must contain exactly 1 Rdata, but ' +
                             'it holds ' + str(rr.get_rdata_count()))
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
117
118
119
120
        if rr.get_class() != self.__updater.get_class():
            raise ValueError("The rrset's class " + str(rr.get_class()) +
                             " does not match updater's " +
                             str(self.__updater.get_class()))
121
        self.__buffer.append((operation, rr))
122
        if len(self.__buffer) >= DIFF_APPLY_TRESHOLD:
123
124
            # Time to auto-apply, so the data don't accumulate too much
            self.apply()
125

126
127
128
129
130
131
132
    def add_data(self, rr):
        """
        Schedules addition of an RR into the zone in this diff.

        The rr is of isc.dns.RRset type and it must contain only one RR.
        If this is not the case or if the diff was already commited, this
        raises the ValueError exception.
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
133
134
135

        The rr class must match the one of the datasource client. If
        it does not, ValueError is raised.
136
        """
137
        self.__data_common(rr, 'add')
138

139
    def delete_data(self, rr):
140
        """
141
        Schedules deleting an RR from the zone in this diff.
142
143
144
145

        The rr is of isc.dns.RRset type and it must contain only one RR.
        If this is not the case or if the diff was already commited, this
        raises the ValueError exception.
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
146
147
148

        The rr class must match the one of the datasource client. If
        it does not, ValueError is raised.
149
        """
150
        self.__data_common(rr, 'delete')
151
152
153
154
155
156

    def compact(self):
        """
        Tries to compact the operations in buffer a little by putting some of
        the operations together, forming RRsets with more than one RR.

157
158
        This is called by apply before putting the data into datasource. You
        may, but not have to, call this manually.
159

160
161
162
163
        Currently it merges consecutive same operations on the same
        domain/type. We could do more fancy things, like sorting by the domain
        and do more merging, but such diffs should be rare in practice anyway,
        so we don't bother and do it this simple way.
164
        """
165
166
167
168
        def same_type(rrset1, rrset2):
            '''A helper routine to identify whether two RRsets are of the
            same 'type'.  For RRSIGs we should consider type covered, too.
            '''
169
170
            if rrset1.get_type() != isc.dns.RRType.RRSIG() or \
                    rrset2.get_type != isc.dns.RRType.RRSIG():
171
172
173
174
175
176
177
                return rrset1.get_type() == rrset2.get_type()
            # RR type of the both RRsets is RRSIG.  Compare type covered.
            # We know they have exactly one RDATA.
            sigdata1 = rrset1.get_rdata()[0].to_text().split()[0]
            sigdata2 = rrset2.get_rdata()[0].to_text().split()[0]
            return sigdata1 == sigdata2

178
179
180
181
182
        buf = []
        for (op, rrset) in self.__buffer:
            old = buf[-1][1] if len(buf) > 0 else None
            if old is None or op != buf[-1][0] or \
                rrset.get_name() != old.get_name() or \
183
                (not same_type(rrset, old)):
184
185
186
187
                buf.append((op, isc.dns.RRset(rrset.get_name(),
                                              rrset.get_class(),
                                              rrset.get_type(),
                                              rrset.get_ttl())))
188
189
            if rrset.get_ttl() != buf[-1][1].get_ttl():
                logger.warn(LIBXFRIN_DIFFERENT_TTL, rrset.get_ttl(),
190
191
                            buf[-1][1].get_ttl(), rrset.get_name(),
                            rrset.get_class(), rrset.get_type())
192
193
194
            for rdatum in rrset.get_rdata():
                buf[-1][1].add_rdata(rdatum)
        self.__buffer = buf
195
196
197
198
199
200
201
202
203
204
205
206
207
208

    def apply(self):
        """
        Push the buffered changes inside this diff down into the data source.
        This does not stop you from adding more changes later through this
        diff and it does not close the datasource transaction, so the changes
        will not be shown to others yet. It just means the internal memory
        buffer is flushed.

        This is called from time to time automatically, but you can call it
        manually if you really want to.

        This raises ValueError if the diff was already commited.

209
210
        It also can raise isc.datasrc.Error. If that happens, you should stop
        using this object and abort the modification.
211
        """
212
        self.__check_commited()
213
214
        # First, compact the data
        self.compact()
215
216
217
218
219
        try:
            # Then pass the data inside the data source
            for (operation, rrset) in self.__buffer:
                if operation == 'add':
                    self.__updater.add_rrset(rrset)
220
221
                elif operation == 'delete':
                    self.__updater.delete_rrset(rrset)
222
223
224
225
226
227
228
229
                else:
                    raise ValueError('Unknown operation ' + operation)
            # As everything is already in, drop the buffer
        except:
            # If there's a problem, we can't continue.
            self.__updater = None
            raise

230
        self.__buffer = []
231
232
233
234
235
236
237
238
239

    def commit(self):
        """
        Writes all the changes into the data source and makes them visible.
        This closes the diff, you may not use it any more. If you try to use
        it, you'll get ValueError.

        This might raise isc.datasrc.Error.
        """
240
241
        self.__check_commited()
        # Push the data inside the data source
242
        self.apply()
243
        # Make sure they are visible.
244
245
246
247
248
249
        try:
            self.__updater.commit()
        finally:
            # Remove the updater. That will free some resources for one, but
            # mark this object as already commited, so we can check

250
            # We delete it even in case the commit failed, as that makes us
251
252
            # unusable.
            self.__updater = None
253
254
255
256

    def get_buffer(self):
        """
        Returns the current buffer of changes not yet passed into the data
257
258
        source. It is in a form like [('add', rrset), ('delete', rrset),
        ('delete', rrset), ...].
259

260
261
        Probably useful only for testing and introspection purposes. Don't
        modify the list.
262
263
        """
        return self.__buffer