2016-03-18 21:04:05 +00:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
2019-01-08 20:43:41 +00:00
|
|
|
# Copyright 2016-2018 Lenovo
|
2016-03-18 21:04:05 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
# Overall, the result of this shall be:
|
|
|
|
# - Web clients can create the same out-of-order responsiveness as socket
|
|
|
|
# clients (but with more complexity on their end)
|
|
|
|
# - Web clients can share single request among console sessions
|
|
|
|
# - Web clients can get async notify of things like node add/remove, events
|
|
|
|
|
|
|
|
# This provides an async strategy to http clients. The design is that a http
|
|
|
|
# session may have an 'async' resource. In such a case, any requests are
|
|
|
|
# queued and immediately the response is given accepting the queued request.
|
|
|
|
# A request flags itself as queue-compatible through an HTTP header indicating
|
|
|
|
# the identifier of the async thread. As responses happen to the queued
|
|
|
|
# request, data is dispatched to the first registered poller for data on
|
|
|
|
# the session. This way, a client may elect to provide multiple pollers
|
|
|
|
# to mitigate general choppiness of http network pattern. It may not be
|
|
|
|
# worth it, but it's possible.
|
|
|
|
|
|
|
|
# Additionally support console session multiplexing, to mitigate needed
|
|
|
|
# connection count.
|
|
|
|
|
|
|
|
# Also, this should allow a client to register for notifications of things
|
|
|
|
# like node add/delete or an event firing, ultimately.
|
|
|
|
|
|
|
|
# Much like console sessions, these will be reaped if a client spends too
|
|
|
|
# far away.
|
|
|
|
|
|
|
|
import collections
|
|
|
|
import confluent.exceptions as exc
|
|
|
|
import confluent.messages as messages
|
|
|
|
import confluent.util as util
|
|
|
|
import eventlet
|
2016-03-20 12:19:33 +00:00
|
|
|
import greenlet
|
2016-03-25 18:50:47 +00:00
|
|
|
import time
|
2016-03-18 21:04:05 +00:00
|
|
|
|
|
|
|
_asyncsessions = {}
|
|
|
|
_cleanthread = None
|
2016-03-25 18:50:47 +00:00
|
|
|
_consolesessions = None
|
2016-03-18 21:04:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _assign_asyncid(asyncsession):
|
|
|
|
sessid = util.randomstring(32)
|
|
|
|
while sessid in _asyncsessions:
|
|
|
|
sessid = util.randomstring(32)
|
|
|
|
_asyncsessions[sessid] = {'asyncsession': asyncsession}
|
|
|
|
return sessid
|
|
|
|
|
|
|
|
|
2016-03-25 18:50:47 +00:00
|
|
|
class AsyncTermRelation(object):
|
|
|
|
# Need to keep an association of term object to async
|
|
|
|
# This allows the async handler to know the context of
|
|
|
|
# outgoing data to provide to calling code
|
2020-04-10 16:08:55 +00:00
|
|
|
def __init__(self, termid, asynchdl):
|
2020-04-10 16:11:45 +00:00
|
|
|
self.asynchdl = asynchdl
|
2016-03-25 18:50:47 +00:00
|
|
|
self.termid = termid
|
|
|
|
|
|
|
|
def got_data(self, data):
|
2020-04-10 16:11:45 +00:00
|
|
|
self.asynchdl.add(self.termid, data)
|
2016-03-25 18:50:47 +00:00
|
|
|
|
|
|
|
|
2016-03-18 21:04:05 +00:00
|
|
|
class AsyncSession(object):
|
|
|
|
|
2022-04-05 20:33:03 +00:00
|
|
|
def __init__(self, wshandler=None):
|
2016-03-18 21:04:05 +00:00
|
|
|
self.asyncid = _assign_asyncid(self)
|
|
|
|
self.responses = collections.deque()
|
2022-04-05 20:33:03 +00:00
|
|
|
self.wshandler = wshandler
|
2016-03-18 21:04:05 +00:00
|
|
|
self._evt = None
|
2016-03-25 18:50:47 +00:00
|
|
|
self.termrelations = []
|
|
|
|
self.consoles = set([])
|
2022-04-05 20:56:36 +00:00
|
|
|
if not wshandler:
|
|
|
|
self.reaper = eventlet.spawn_after(15, self.destroy)
|
2016-03-18 21:04:05 +00:00
|
|
|
|
2016-03-26 13:34:46 +00:00
|
|
|
def add(self, requestid, rsp):
|
2022-04-05 20:33:03 +00:00
|
|
|
if self.wshandler:
|
|
|
|
self.wshandler(messages.AsyncMessage((requestid, rsp)))
|
2019-01-08 20:43:41 +00:00
|
|
|
if self.responses is None:
|
|
|
|
return
|
2016-03-26 13:34:46 +00:00
|
|
|
self.responses.append((requestid, rsp))
|
2016-03-18 21:04:05 +00:00
|
|
|
if self._evt:
|
|
|
|
self._evt.send()
|
|
|
|
self._evt = None
|
|
|
|
|
2016-03-25 18:50:47 +00:00
|
|
|
def set_term_relation(self, env):
|
|
|
|
# need a term relation to keep track of what data belongs
|
|
|
|
# to what object (since the callback does not provide context
|
|
|
|
# for data, and here ultimately the client is responsible
|
|
|
|
# for sorting out which is which.
|
2016-03-26 13:34:46 +00:00
|
|
|
termrel = AsyncTermRelation(env['HTTP_CONFLUENTREQUESTID'], self)
|
2016-03-25 18:50:47 +00:00
|
|
|
self.termrelations.append(termrel)
|
|
|
|
return termrel
|
|
|
|
|
|
|
|
def add_console_session(self, sessionid):
|
|
|
|
self.consoles.add(sessionid)
|
|
|
|
|
2016-03-18 21:04:05 +00:00
|
|
|
def destroy(self):
|
|
|
|
if self._evt:
|
|
|
|
self._evt.send()
|
|
|
|
self._evt = None
|
2016-03-26 14:26:17 +00:00
|
|
|
for console in self.consoles:
|
|
|
|
_consolesessions[console]['session'].destroy()
|
2022-04-05 20:33:03 +00:00
|
|
|
self.consoles = set([])
|
2019-01-08 20:43:41 +00:00
|
|
|
self.responses = None
|
2016-03-18 21:04:05 +00:00
|
|
|
del _asyncsessions[self.asyncid]
|
|
|
|
|
|
|
|
def run_handler(self, handler, requestid):
|
2016-09-28 19:00:49 +00:00
|
|
|
try:
|
|
|
|
for rsp in handler:
|
|
|
|
self.add(requestid, rsp)
|
|
|
|
self.add(requestid, messages.AsyncCompletion())
|
|
|
|
except Exception as e:
|
|
|
|
self.add(requestid, e)
|
2016-03-18 21:04:05 +00:00
|
|
|
|
|
|
|
def get_responses(self, timeout=25):
|
|
|
|
self.reaper.cancel()
|
2016-03-26 14:26:17 +00:00
|
|
|
self.reaper = eventlet.spawn_after(timeout + 15, self.destroy)
|
2016-03-25 18:50:47 +00:00
|
|
|
nextexpiry = time.time() + 90
|
2016-03-26 14:45:47 +00:00
|
|
|
for csess in list(self.consoles):
|
|
|
|
try:
|
|
|
|
_consolesessions[csess]['expiry'] = nextexpiry
|
|
|
|
except KeyError: # session has been closed elsewhere
|
|
|
|
self.consoles.discard(csess)
|
2016-03-19 00:46:37 +00:00
|
|
|
if self._evt:
|
2016-03-18 21:04:05 +00:00
|
|
|
# TODO(jjohnson2): This precludes the goal of 'double barreled'
|
|
|
|
# access.... revisit if this could matter
|
|
|
|
raise Exception('get_responses is not re-entrant')
|
|
|
|
if not self.responses: # wait to accumulate some
|
|
|
|
self._evt = eventlet.event.Event()
|
2016-03-19 00:46:37 +00:00
|
|
|
with eventlet.Timeout(timeout, False):
|
2016-03-18 21:04:05 +00:00
|
|
|
self._evt.wait()
|
|
|
|
self._evt = None
|
|
|
|
while self.responses:
|
|
|
|
yield self.responses.popleft()
|
|
|
|
|
|
|
|
|
|
|
|
def run_handler(hdlr, env):
|
|
|
|
asyncsessid = env['HTTP_CONFLUENTASYNCID']
|
|
|
|
try:
|
2016-03-19 00:46:37 +00:00
|
|
|
asyncsession = _asyncsessions[asyncsessid]['asyncsession']
|
2016-03-18 21:04:05 +00:00
|
|
|
requestid = env['HTTP_CONFLUENTREQUESTID']
|
|
|
|
except KeyError:
|
|
|
|
raise exc.InvalidArgumentException(
|
|
|
|
'Invalid Session ID or missing request id')
|
|
|
|
eventlet.spawn_n(asyncsession.run_handler, hdlr, requestid)
|
|
|
|
return requestid
|
|
|
|
|
|
|
|
|
2016-03-25 18:50:47 +00:00
|
|
|
def get_async(env, querydict):
|
|
|
|
global _cleanthread
|
2016-03-26 13:34:46 +00:00
|
|
|
return _asyncsessions[env['HTTP_CONFLUENTASYNCID']]['asyncsession']
|
2016-03-25 18:50:47 +00:00
|
|
|
|
|
|
|
|
2022-04-13 20:08:13 +00:00
|
|
|
def handle_async(env, querydict, threadset, wshandler=None):
|
2016-03-18 21:04:05 +00:00
|
|
|
global _cleanthread
|
|
|
|
# This may be one of two things, a request for a new async stream
|
|
|
|
# or a request for next data from async stream
|
|
|
|
# httpapi otherwise handles requests an injecting them to queue
|
|
|
|
if 'asyncid' not in querydict or not querydict['asyncid']:
|
|
|
|
# This is a new request, create a new multiplexer
|
2022-04-05 20:33:03 +00:00
|
|
|
currsess = AsyncSession(wshandler)
|
2022-04-13 20:08:13 +00:00
|
|
|
if wshandler:
|
|
|
|
yield currsess
|
2022-04-05 20:33:03 +00:00
|
|
|
return
|
2016-03-18 21:04:05 +00:00
|
|
|
yield messages.AsyncSession(currsess.asyncid)
|
|
|
|
return
|
2019-01-08 20:43:41 +00:00
|
|
|
if querydict['asyncid'] not in _asyncsessions:
|
|
|
|
raise exc.InvalidArgumentException(
|
|
|
|
'Invalid or expired async id')
|
2016-03-20 12:19:33 +00:00
|
|
|
mythreadid = greenlet.getcurrent()
|
|
|
|
threadset.add(mythreadid)
|
|
|
|
loggedout = None
|
|
|
|
currsess = None
|
|
|
|
try:
|
|
|
|
currsess = _asyncsessions[querydict['asyncid']]['asyncsession']
|
|
|
|
for rsp in currsess.get_responses():
|
|
|
|
yield messages.AsyncMessage(rsp)
|
|
|
|
except greenlet.GreenletExit as ge:
|
|
|
|
loggedout = ge
|
|
|
|
threadset.discard(mythreadid)
|
|
|
|
if loggedout is not None:
|
|
|
|
currsess.destroy()
|
2016-03-25 18:50:47 +00:00
|
|
|
raise exc.LoggedOut()
|
|
|
|
|
|
|
|
|
|
|
|
def set_console_sessions(consolesessions):
|
|
|
|
global _consolesessions
|
2022-04-05 20:33:03 +00:00
|
|
|
_consolesessions = consolesessions
|