Mercurial > p > roundup > code
annotate roundup/cgi/ZTUtils/Batch.py @ 6565:2c2dbfc332ba
Try to handle multiple connections better.
The session database is a hot spot. When multiple requests (e.g. 20)
come in at the same time session database contention can get great.
The original code didn't retry session database access when the open
failed. This resulted in errors at the client.
The second pass delayed 0.01 seconds and retried. It was better but we
still had multiple second stalls. I think the first request got in,
everybody else backed up and then retried at the same time. Again they
stepped on each other. With logging I would see many counters go all
the way to low single digits or to -1 indicating falure.
This pass uses randomint to generate delays from 0-.125 seconds in 5ms
increments. This performs better in testing. I rarely saw a counter
less than 13 (2 failed retries). Current logging starts after 6
failures and counts down until success or failure.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Thu, 16 Dec 2021 20:02:00 -0500 |
| parents | 35ea9b1efc14 |
| children |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. | |
| 4 # | |
| 5 # This software is subject to the provisions of the Zope Public License, | |
| 6 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | |
| 7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | |
| 8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | |
| 10 # FOR A PARTICULAR PURPOSE | |
| 11 # | |
| 12 ############################################################################## | |
| 13 __doc__='''Batch class, for iterating over a sequence in batches | |
| 14 | |
|
4570
6e3e4f24c753
Remove keyword expansions from CVS. All regression tests passed afterwards.
Eric S. Raymond <esr@thyrsus.com>
parents:
4532
diff
changeset
|
15 ''' |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1120
diff
changeset
|
16 __docformat__ = 'restructuredtext' |
| 1049 | 17 |
| 18 class LazyPrevBatch: | |
| 19 def __of__(self, parent): | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
20 return Batch(parent._sequence, parent.size, |
| 1049 | 21 parent.first - parent._size + parent.overlap, 0, |
| 22 parent.orphan, parent.overlap) | |
| 23 | |
| 24 class LazyNextBatch: | |
| 25 def __of__(self, parent): | |
| 26 try: parent._sequence[parent.end] | |
| 27 except IndexError: return None | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
28 return Batch(parent._sequence, parent.size, |
| 1049 | 29 parent.end - parent.overlap, 0, |
| 30 parent.orphan, parent.overlap) | |
| 31 | |
| 32 class LazySequenceLength: | |
| 33 def __of__(self, parent): | |
| 34 parent.sequence_length = l = len(parent._sequence) | |
| 35 return l | |
| 36 | |
| 37 class Batch: | |
| 38 """Create a sequence batch""" | |
| 39 __allow_access_to_unprotected_subobjects__ = 1 | |
| 40 | |
| 41 previous = LazyPrevBatch() | |
| 42 next = LazyNextBatch() | |
| 43 sequence_length = LazySequenceLength() | |
| 44 | |
| 45 def __init__(self, sequence, size, start=0, end=0, | |
| 46 orphan=0, overlap=0): | |
| 47 '''Encapsulate "sequence" in batches of "size". | |
| 48 | |
| 49 Arguments: "start" and "end" are 0-based indexes into the | |
| 50 sequence. If the next batch would contain no more than | |
| 51 "orphan" elements, it is combined with the current batch. | |
| 52 "overlap" is the number of elements shared by adjacent | |
| 53 batches. If "size" is not specified, it is computed from | |
|
4532
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
54 "start" and "end". If "size" is 0, it is the length of |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
55 the sequence. Failing that, it is 7. |
| 1049 | 56 |
| 57 Attributes: Note that the "start" attribute, unlike the | |
| 58 argument, is a 1-based index (I know, lame). "first" is the | |
| 59 0-based index. "length" is the actual number of elements in | |
| 60 the batch. | |
| 61 | |
| 62 "sequence_length" is the length of the original, unbatched, sequence | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
63 |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
64 Note: "_size" is the "actual" size used to perform batch calulcations, |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
65 while "size" is the "representative" size. (ie. a "special value" of |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
66 "size" used by the templates may translate to a different value for |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
67 "_size" which is used internally for batch calculations). |
| 1049 | 68 ''' |
| 69 | |
| 70 start = start + 1 | |
| 71 | |
| 72 start,end,sz = opt(start,end,size,orphan,sequence) | |
| 73 | |
| 74 self._sequence = sequence | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
75 self.size = size |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
76 self._size = sz |
| 1049 | 77 self.start = start |
| 78 self.end = end | |
| 79 self.orphan = orphan | |
| 80 self.overlap = overlap | |
| 81 self.first = max(start - 1, 0) | |
| 82 self.length = self.end - self.first | |
| 83 if self.first == 0: | |
| 84 self.previous = None | |
| 85 | |
| 86 | |
| 87 def __getitem__(self, index): | |
| 88 if index < 0: | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5087
diff
changeset
|
89 if index + self.end < self.first: raise IndexError(index) |
| 1049 | 90 return self._sequence[index + self.end] |
| 91 | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5087
diff
changeset
|
92 if index >= self.length: raise IndexError(index) |
|
1120
c26471971d18
Exposed the Batch mechanism through the top-level "utils" variable.
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
93 return self._sequence[index + self.first] |
| 1049 | 94 |
| 95 def __len__(self): | |
| 96 return self.length | |
| 97 | |
| 98 def opt(start,end,size,orphan,sequence): | |
| 99 if size < 1: | |
|
4532
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
100 if size == 0: |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
101 size=len(sequence) |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
102 elif start > 0 and end > 0 and end >= start: |
| 1049 | 103 size=end+1-start |
| 104 else: size=7 | |
| 105 | |
| 106 if start > 0: | |
| 107 | |
| 108 try: sequence[start-1] | |
| 109 except IndexError: start=len(sequence) | |
| 110 | |
| 111 if end > 0: | |
| 112 if end < start: end=start | |
| 113 else: | |
| 114 end=start+size-1 | |
| 115 try: sequence[end+orphan-1] | |
| 116 except IndexError: end=len(sequence) | |
| 117 elif end > 0: | |
| 118 try: sequence[end-1] | |
| 119 except IndexError: end=len(sequence) | |
| 120 start=end+1-size | |
| 121 if start - 1 < orphan: start=1 | |
| 122 else: | |
| 123 start=1 | |
| 124 end=start+size-1 | |
| 125 try: sequence[end+orphan-1] | |
| 126 except IndexError: end=len(sequence) | |
| 127 return start,end,size |
