2015-03-05 11:30:33 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2015 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
"""
|
|
|
|
This module controls the reliability for application service transactions.
|
|
|
|
|
|
|
|
The nominal flow through this module looks like:
|
2015-03-06 05:25:50 -05:00
|
|
|
_________
|
|
|
|
---ASa[e]-->| Event |
|
|
|
|
----ASb[e]->| Grouper |<-poll 1/s--+
|
|
|
|
--ASa[e]--->|_________| | ASa[e,e] ASb[e]
|
|
|
|
V
|
2015-03-05 11:30:33 -05:00
|
|
|
-````````- +------------+
|
|
|
|
|````````|<--StoreTxn-|Transaction |
|
2015-03-05 12:35:07 -05:00
|
|
|
|Database| | Controller |---> SEND TO AS
|
2015-03-05 11:30:33 -05:00
|
|
|
`--------` +------------+
|
|
|
|
What happens on SEND TO AS depends on the state of the Application Service:
|
|
|
|
- If the AS is marked as DOWN, do nothing.
|
|
|
|
- If the AS is marked as UP, send the transaction.
|
|
|
|
* SUCCESS : Increment where the AS is up to txn-wise and nuke the txn
|
|
|
|
contents from the db.
|
|
|
|
* FAILURE : Marked AS as DOWN and start Recoverer.
|
|
|
|
|
|
|
|
Recoverer attempts to recover ASes who have died. The flow for this looks like:
|
|
|
|
,--------------------- backoff++ --------------.
|
|
|
|
V |
|
|
|
|
START ---> Wait exp ------> Get oldest txn ID from ----> FAILURE
|
|
|
|
backoff DB and try to send it
|
2015-03-06 05:25:50 -05:00
|
|
|
^ |___________
|
|
|
|
Mark AS as | V
|
|
|
|
UP & quit +---------- YES SUCCESS
|
|
|
|
| | |
|
|
|
|
NO <--- Have more txns? <------ Mark txn success & nuke <-+
|
2015-03-05 11:30:33 -05:00
|
|
|
from db; incr AS pos.
|
2015-03-05 12:45:52 -05:00
|
|
|
Reset backoff.
|
2015-03-05 12:35:07 -05:00
|
|
|
|
|
|
|
This is all tied together by the AppServiceScheduler which DIs the required
|
|
|
|
components.
|
2015-03-05 11:30:33 -05:00
|
|
|
"""
|
|
|
|
|
2015-03-06 06:50:27 -05:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2015-03-05 11:30:33 -05:00
|
|
|
|
2015-03-05 12:35:07 -05:00
|
|
|
class AppServiceScheduler(object):
|
|
|
|
""" Public facing API for this module. Does the required DI to tie the
|
|
|
|
components together. This also serves as the "event_pool", which in this
|
|
|
|
case is a simple array.
|
|
|
|
"""
|
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def __init__(self, clock, store, as_api):
|
|
|
|
self.clock = clock
|
|
|
|
self.store = store
|
|
|
|
self.as_api = as_api
|
|
|
|
self.event_grouper = _EventGrouper()
|
2015-03-05 12:35:07 -05:00
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def create_recoverer(service, callback):
|
|
|
|
return _Recoverer(clock, store, as_api, service, callback)
|
2015-03-05 12:35:07 -05:00
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
self.txn_ctrl = _TransactionController(
|
|
|
|
clock, store, as_api, self.event_grouper, create_recoverer
|
|
|
|
)
|
2015-03-05 12:35:07 -05:00
|
|
|
|
|
|
|
def start(self):
|
2015-03-06 05:25:50 -05:00
|
|
|
# check for any DOWN ASes and start recoverers for them.
|
|
|
|
_Recoverer.start(
|
|
|
|
self.clock, self.store, self.as_api, self.txn_ctrl.on_recovered
|
|
|
|
)
|
|
|
|
self.txn_ctrl.start_polling()
|
2015-03-05 12:35:07 -05:00
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def submit_event_for_as(self, service, event):
|
|
|
|
self.event_grouper.on_receive(service, event)
|
2015-03-05 12:35:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
class AppServiceTransaction(object):
|
|
|
|
"""Represents an application service transaction."""
|
|
|
|
|
|
|
|
def __init__(self, service, id, events):
|
|
|
|
self.service = service
|
|
|
|
self.id = id
|
|
|
|
self.events = events
|
|
|
|
|
|
|
|
def send(self, as_api):
|
2015-03-05 12:45:52 -05:00
|
|
|
# TODO sends this transaction using this as_api
|
2015-03-05 12:35:07 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
def complete(self, store):
|
2015-03-05 12:45:52 -05:00
|
|
|
# TODO increment txn id on AS and nuke txn contents from db
|
2015-03-05 12:35:07 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
class _EventGrouper(object):
|
|
|
|
"""Groups events for the same application service together.
|
|
|
|
"""
|
2015-03-05 12:35:07 -05:00
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def __init__(self):
|
|
|
|
self.groups = {} # dict of {service: [events]}
|
2015-03-05 12:35:07 -05:00
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def on_receive(self, service, event):
|
2015-03-06 06:50:27 -05:00
|
|
|
if service not in self.groups:
|
|
|
|
self.groups[service] = []
|
|
|
|
self.groups[service].append(event)
|
2015-03-05 12:35:07 -05:00
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def drain_groups(self):
|
2015-03-06 06:50:27 -05:00
|
|
|
groups = self.groups
|
|
|
|
self.groups = {}
|
|
|
|
return groups
|
2015-03-06 05:25:50 -05:00
|
|
|
|
2015-03-05 12:35:07 -05:00
|
|
|
|
|
|
|
class _TransactionController(object):
|
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def __init__(self, clock, store, as_api, event_grouper, recoverer_fn):
|
|
|
|
self.clock = clock
|
2015-03-05 12:35:07 -05:00
|
|
|
self.store = store
|
|
|
|
self.as_api = as_api
|
2015-03-06 05:25:50 -05:00
|
|
|
self.event_grouper = event_grouper
|
2015-03-05 12:35:07 -05:00
|
|
|
self.recoverer_fn = recoverer_fn
|
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def start_polling(self):
|
|
|
|
groups = self.event_grouper.drain_groups()
|
|
|
|
for service in groups:
|
|
|
|
txn_id = self._get_next_txn_id(service)
|
|
|
|
txn = AppServiceTransaction(service, txn_id, groups[service])
|
|
|
|
self._store_txn(txn)
|
|
|
|
if self._is_service_up(service):
|
|
|
|
if txn.send(self.as_api):
|
|
|
|
txn.complete(self.store)
|
|
|
|
else:
|
|
|
|
# TODO mark AS as down
|
|
|
|
self._start_recoverer(service)
|
|
|
|
self.clock.call_later(1000, self.start_polling)
|
|
|
|
|
|
|
|
def on_recovered(self, service):
|
|
|
|
# TODO mark AS as UP
|
|
|
|
pass
|
2015-03-05 12:35:07 -05:00
|
|
|
|
|
|
|
def _start_recoverer(self, service):
|
2015-03-06 05:25:50 -05:00
|
|
|
recoverer = self.recoverer_fn(service, self.on_recovered)
|
2015-03-05 12:35:07 -05:00
|
|
|
recoverer.recover()
|
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def _is_service_up(self, service):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _get_next_txn_id(self, service):
|
|
|
|
pass # TODO work out the next txn_id for this service
|
|
|
|
|
|
|
|
def _store_txn(self, txn):
|
|
|
|
pass
|
2015-03-05 12:35:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
class _Recoverer(object):
|
2015-03-05 11:30:33 -05:00
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
@staticmethod
|
|
|
|
def start(clock, store, as_api, callback):
|
|
|
|
# TODO check for DOWN ASes and init recoverers
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(self, clock, store, as_api, service, callback):
|
|
|
|
self.clock = clock
|
2015-03-05 12:35:07 -05:00
|
|
|
self.store = store
|
|
|
|
self.as_api = as_api
|
|
|
|
self.service = service
|
2015-03-06 05:25:50 -05:00
|
|
|
self.callback = callback
|
2015-03-05 12:35:07 -05:00
|
|
|
self.backoff_counter = 1
|
2015-03-05 11:30:33 -05:00
|
|
|
|
2015-03-05 12:35:07 -05:00
|
|
|
def recover(self):
|
2015-03-06 09:36:52 -05:00
|
|
|
self.clock.call_later(1000 * (2 ** self.backoff_counter), self.retry)
|
2015-03-06 05:25:50 -05:00
|
|
|
|
2015-03-06 06:50:27 -05:00
|
|
|
@defer.inlineCallbacks
|
2015-03-06 05:25:50 -05:00
|
|
|
def retry(self):
|
2015-03-06 06:50:27 -05:00
|
|
|
txn = yield self._get_oldest_txn()
|
2015-03-05 12:35:07 -05:00
|
|
|
if txn:
|
|
|
|
if txn.send(self.as_api):
|
|
|
|
txn.complete(self.store)
|
2015-03-06 05:25:50 -05:00
|
|
|
# reset the backoff counter and retry immediately
|
2015-03-05 12:45:52 -05:00
|
|
|
self.backoff_counter = 1
|
2015-03-06 09:36:52 -05:00
|
|
|
yield self.retry()
|
2015-03-05 12:35:07 -05:00
|
|
|
else:
|
|
|
|
self.backoff_counter += 1
|
2015-03-06 05:25:50 -05:00
|
|
|
self.recover()
|
2015-03-05 12:35:07 -05:00
|
|
|
else:
|
2015-03-06 05:25:50 -05:00
|
|
|
self._set_service_recovered()
|
2015-03-05 11:30:33 -05:00
|
|
|
|
2015-03-06 05:25:50 -05:00
|
|
|
def _set_service_recovered(self):
|
|
|
|
self.callback(self.service)
|
2015-03-05 11:30:33 -05:00
|
|
|
|
2015-03-06 06:50:27 -05:00
|
|
|
@defer.inlineCallbacks
|
2015-03-05 12:35:07 -05:00
|
|
|
def _get_oldest_txn(self):
|
2015-03-06 06:50:27 -05:00
|
|
|
txn = yield self.store.get_oldest_txn(self.service)
|
|
|
|
defer.returnValue(txn)
|