"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-9.0.0/zaqar/storage/sqlalchemy/utils.py" (16 Oct 2019, 3722 Bytes) of package /linux/misc/openstack/zaqar-9.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "utils.py" see the Fossies "Dox" file reference documentation.

    1 # Copyright (c) 2014 Red Hat, Inc.
    2 # Copyright (c) 2014 Rackspace, Inc.
    3 #
    4 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
    5 # use this file except in compliance with the License.  You may obtain a copy
    6 # of the License at
    7 #
    8 #    http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 # Unless required by applicable law or agreed to in writing, software
   11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 # License for the specific language governing permissions and limitations under
   14 # the License.
   15 
   16 import functools
   17 
   18 from oslo_log import log as logging
   19 from oslo_serialization import jsonutils
   20 from oslo_utils import encodeutils
   21 import sqlalchemy as sa
   22 from sqlalchemy import exc
   23 from sqlalchemy.sql import func as sfunc
   24 
   25 from zaqar.storage import errors
   26 from zaqar.storage.sqlalchemy import tables
   27 
   28 
   29 LOG = logging.getLogger(__name__)
   30 UNIX_EPOCH_AS_JULIAN_SEC = 2440587.5 * 86400.0
   31 
   32 
   33 def raises_conn_error(func):
   34     """Handles sqlalchemy DisconnectionError
   35 
   36     When sqlalchemy detects a disconnect from the database server, it
   37     retries a number of times. After failing that number of times, it
   38     will convert the internal DisconnectionError into an
   39     InvalidRequestError. This decorator handles that error.
   40     """
   41     @functools.wraps(func)
   42     def wrapper(*args, **kwargs):
   43         try:
   44             return func(*args, **kwargs)
   45         except exc.InvalidRequestError as ex:
   46             LOG.exception(ex)
   47             raise errors.ConnectionError()
   48 
   49     return wrapper
   50 
   51 
   52 class NoResult(Exception):
   53     pass
   54 
   55 
   56 def get_qid(driver, queue, project):
   57     sel = sa.sql.select([tables.Queues.c.id], sa.and_(
   58                         tables.Queues.c.project == project,
   59                         tables.Queues.c.name == queue))
   60     try:
   61         return driver.get(sel)[0]
   62     except NoResult:
   63         raise errors.QueueDoesNotExist(queue, project)
   64 
   65 
   66 def get_age(created):
   67     return sfunc.now() - created
   68 
   69 # The utilities below make the database IDs opaque to the users
   70 # of Zaqar API.  The only purpose is to advise the users NOT to
   71 # make assumptions on the implementation of and/or relationship
   72 # between the message IDs, the markers, and claim IDs.
   73 #
   74 # The magic numbers are arbitrarily picked; the numbers themselves
   75 # come with no special functionalities.
   76 
   77 
   78 def msgid_encode(id):
   79     # NOTE(jeffrey4l): When using mysql-python, the id is converted to
   80     # long type, which will lead to a L letter in the last.
   81     return hex(int(id) ^ 0x5c693a53)[2:]
   82 
   83 
   84 def msgid_decode(id):
   85     try:
   86         return int(id, 16) ^ 0x5c693a53
   87 
   88     except ValueError:
   89         return None
   90 
   91 
   92 def marker_encode(id):
   93     # NOTE(AAzza): cannot use oct(id) here, because on Python 3 it returns
   94     # string with prefix '0o', whereas on Python 2 prefix is just '0'
   95     return '{0:o}'.format(id ^ 0x3c96a355)
   96 
   97 
   98 def marker_decode(id):
   99     try:
  100         return int(id, 8) ^ 0x3c96a355
  101 
  102     except ValueError:
  103         return None
  104 
  105 
  106 def cid_encode(id):
  107     return hex(id ^ 0x63c9a59c)[2:]
  108 
  109 
  110 def cid_decode(id):
  111     try:
  112         return int(id, 16) ^ 0x63c9a59c
  113 
  114     except ValueError:
  115         return None
  116 
  117 
  118 def julian_to_unix(julian_sec):
  119     """Converts Julian timestamp, in seconds, to a UNIX timestamp."""
  120     return int(round(julian_sec - UNIX_EPOCH_AS_JULIAN_SEC))
  121 
  122 
  123 def stat_message(message):
  124     """Creates a stat document based on a message."""
  125     return {
  126         'id': message['id'],
  127         'age': message['age'],
  128         'created': message['created'],
  129     }
  130 
  131 
  132 def json_encode(obj):
  133     return encodeutils.safe_encode(jsonutils.dumps(obj), 'utf-8')
  134 
  135 
  136 def json_decode(binary):
  137     return jsonutils.loads(binary, 'utf-8')