forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrent.py
More file actions
139 lines (114 loc) · 4.85 KB
/
concurrent.py
File metadata and controls
139 lines (114 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright 2013-2017 DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from concurrent.futures import Future
__all__ = ['CQLEngineFuture', 'CQLEngineFutureWaiter']
class CQLEngineFuture(Future):
"""
CQLEngineFuture provides a `concurrent.futures.Future` implementation to
work with the internal of CQLEngine.
"""
_future = None
_post_processing = None
_result = None
_check_applied_fn = None
def __init__(self, future=None, post_processing=None, result=None):
super(CQLEngineFuture, self).__init__()
self._future = future
if self._future is None:
self.set_result(result)
else:
if post_processing:
self._post_processing = []
if isinstance(post_processing, (list, tuple)):
self._post_processing += post_processing
else:
self._post_processing.append(post_processing)
if isinstance(self._future, (CQLEngineFuture, CQLEngineFutureWaiter)):
self._future.add_done_callback(self._future_callback)
else: # ResponseFuture
self._future.add_callbacks(self._response_future_callback, self._response_future_errback)
@classmethod
def _check_applied(cls, results):
"""CQLEngine LWT check"""
if cls._check_applied_fn is None:
from cassandra.cqlengine.query import check_applied
cls._check_applied_fn = staticmethod(check_applied)
cls._check_applied_fn(results)
def _execute_post_processing(self, results):
"""CQLEngine post-processing execution"""
# Execute all post_processing functions
if self._post_processing:
for fn in self._post_processing:
results = fn(results)
return results
def _future_callback(self, _):
"""Callback handler for `Future` types (e.g CQLEngineFuture). This is mostly used internally
when no request to server is required and/or we need to wrap another CQLEngineFuture"""
try:
results = _.result() # raise if error
results = self._execute_post_processing(results)
self.set_result(results)
except Exception as e:
self.set_exception(e)
def _response_future_callback(self, _):
"""Callback handler for `ResponseFuture` type"""
try:
# _handle_result needs to be executed in another thread
self._future.session.cluster.executor.submit(self._handle_result)
except Exception as e:
self.set_exception(e)
def _response_future_errback(self, exc):
"""Errback handler for `ResponseFuture` type"""
self.set_exception(exc)
def _handle_result(self):
"""
Handle a CQLEngine query result:
1- Fetch and materialize all rows if the response future has_more_pages
2- Do LWT check
3- Execute internal cqlengine post_processing functions
"""
try:
result_set = self._future.result()
self._future.clear_callbacks()
# Ensure all rows are fetched and materialized.. important with fetch_size()
results = list(result_set)
# LWT check if required
if len(results) == 1: # avoid this check if not we know it's not a LWT
self._check_applied(results)
results = self._execute_post_processing(results)
self.set_result(results)
except Exception as e:
self.set_exception(e)
class CQLEngineFutureWaiter(Future):
"""Wrap and wait that all futures are done."""
_count = 0
_futures = None
def __init__(self, futures):
super(CQLEngineFutureWaiter, self).__init__()
self._futures = [future for future in futures if future is not None]
self._count = len(self._futures)
if self._futures:
for future in self._futures:
future.add_done_callback(self._set_if_done)
else:
self.set_result(None)
def _set_if_done(self, _):
self._count -= 1
if self._count == 0:
for future in self._futures:
if future.exception() is not None:
self.set_exception(future.exception())
break
self.set_result(None) # No result, it's just a waiter